3 using System.Collections.Generic;
16 private readonly
string solverUrl =
"http://solver.planning.domains/solve";
20 private static string Escape(
string escapable)
22 StringBuilder builder =
new StringBuilder();
24 for (
int i = 0; i < escapable.Length; i++)
26 char character = escapable[i];
30 case '"': builder.Append(
"\\\"");
break;
31 case '\\': builder.Append(
"\\\\");
break;
32 case '\b': builder.Append(
"\\b");
break;
33 case '\f': builder.Append(
"\\f");
break;
34 case '\n': builder.Append(
"\\n");
break;
35 case '\r': builder.Append(
"\\r");
break;
36 case '\t': builder.Append(
"\\t");
break;
37 case '/': builder.Append(
"\\/");
break;
39 if (((character >=
'\u0000') && (character <=
'\u001F')) || ((character >=
'\u007F') && (character <=
'\u009F')) || ((character >=
'\u2000') && (character <=
'\u20FF')))
41 string characterHexCode = Convert.ToInt32(character).ToString(
"X");
43 builder.Append(
"\\u");
45 for (
int k = 0; k < (4 - characterHexCode.Length); k++)
48 builder.Append(characterHexCode.ToUpper());
51 builder.Append(character);
56 return builder.ToString();
59 public virtual string CreateJson(IList<InputProgram> pddlInputProgram)
64 foreach (InputProgram ip
in pddlInputProgram)
66 if (!(ip is PDDLInputProgram))
69 PDDLInputProgram pip = (PDDLInputProgram)ip;
70 switch (pip.ProgramsType)
72 case PDDLProgramType.DOMAIN:
73 domain += pip.Programs + pip.Separator;
74 domain += GetFromFile(pip.FilesPaths, pip.Separator);
76 case PDDLProgramType.PROBLEM:
77 problem += pip.Programs + pip.Separator;
78 problem += GetFromFile(pip.FilesPaths, pip.Separator);
81 throw new PDDLException(
"Program type : " + pip.ProgramsType +
" not valid.");
85 if (problem.Equals(
""))
86 throw new PDDLException(
"Problem file not specified");
87 if (domain.Equals(
""))
88 throw new PDDLException(
"Domain file not specified");
90 return "{\"problem\":\"" + Escape(problem) +
"\", \"domain\":\"" + Escape(domain) +
"\"}"; ;
93 private string GetFromFile(IList<string> filesPaths,
string separator)
95 StringBuilder toReturn =
new StringBuilder();
96 foreach (String s
in filesPaths)
100 toReturn.Append(ReadFile(s)).Append(separator);
102 catch (IOException e)
104 Console.WriteLine(e.ToString());
105 Console.Write(e.StackTrace);
108 return toReturn.ToString();
111 public virtual string PostJsonToURL(
string json)
116 System.Uri myUri =
new Uri(solverUrl);
117 HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(myUri);
121 httpRequest.AllowWriteStreamBuffering =
true;
123 httpRequest.ContentType =
"application/json";
126 httpRequest.Method =
"POST";
128 Stream os = httpRequest.GetRequestStream();
129 UTF8Encoding encoding =
new UTF8Encoding();
130 byte[] json_bytes = encoding.GetBytes(json);
131 os.Write(json_bytes, 0, json_bytes.Length);
135 StringBuilder sb =
new StringBuilder();
136 HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
138 if (httpResponse.StatusCode == HttpStatusCode.OK)
140 StreamReader br =
new StreamReader(httpResponse.GetResponseStream(), Encoding.UTF8);
143 while (!((line = br.ReadLine()) is
null))
144 sb.Append(line +
"\n");
147 result = sb.ToString();
151 throw new PDDLException(
"HTTP connection error, response code : " + httpResponse.StatusCode +
" response message : " +
new StreamReader(httpResponse.GetResponseStream()).ReadToEnd());
156 throw new PDDLException(
"Impossible to perform HTTP connection: " + e.Message);
161 protected internal abstract string ReadFile(
string s);