EmbASP-CSharp
SPDUtility.cs
2 using System;
3 using System.Collections.Generic;
4 using System.IO;
5 using System.Net;
6 using System.Text;
7 
9 {
10  using InputProgram = it.unical.mat.embasp.@base.InputProgram;
13 
14  public abstract class SPDUtility
15  {
16  private readonly string solverUrl = "http://solver.planning.domains/solve";
17 
18  public SPDUtility() { }
19 
20  private static string Escape(string escapable)
21  {
22  StringBuilder builder = new StringBuilder();
23 
24  for (int i = 0; i < escapable.Length; i++)
25  {
26  char character = escapable[i];
27 
28  switch (character)
29  {
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;
38  default:
39  if (((character >= '\u0000') && (character <= '\u001F')) || ((character >= '\u007F') && (character <= '\u009F')) || ((character >= '\u2000') && (character <= '\u20FF')))
40  {
41  string characterHexCode = Convert.ToInt32(character).ToString("X");
42 
43  builder.Append("\\u");
44 
45  for (int k = 0; k < (4 - characterHexCode.Length); k++)
46  builder.Append("0");
47 
48  builder.Append(characterHexCode.ToUpper());
49  }
50  else
51  builder.Append(character);
52  break;
53  }
54  }
55 
56  return builder.ToString();
57  }
58 
59  public virtual string CreateJson(IList<InputProgram> pddlInputProgram)
60  {
61  string problem = "";
62  string domain = "";
63 
64  foreach (InputProgram ip in pddlInputProgram)
65  {
66  if (!(ip is PDDLInputProgram))
67  continue;
68 
69  PDDLInputProgram pip = (PDDLInputProgram)ip;
70  switch (pip.ProgramsType)
71  {
72  case PDDLProgramType.DOMAIN:
73  domain += pip.Programs + pip.Separator;
74  domain += GetFromFile(pip.FilesPaths, pip.Separator);
75  break;
76  case PDDLProgramType.PROBLEM:
77  problem += pip.Programs + pip.Separator;
78  problem += GetFromFile(pip.FilesPaths, pip.Separator);
79  break;
80  default:
81  throw new PDDLException("Program type : " + pip.ProgramsType + " not valid.");
82  }
83  }
84 
85  if (problem.Equals(""))
86  throw new PDDLException("Problem file not specified");
87  if (domain.Equals(""))
88  throw new PDDLException("Domain file not specified");
89 
90  return "{\"problem\":\"" + Escape(problem) + "\", \"domain\":\"" + Escape(domain) + "\"}"; ;
91  }
92 
93  private string GetFromFile(IList<string> filesPaths, string separator)
94  {
95  StringBuilder toReturn = new StringBuilder();
96  foreach (String s in filesPaths)
97  {
98  try
99  {
100  toReturn.Append(ReadFile(s)).Append(separator);
101  }
102  catch (IOException e)
103  {
104  Console.WriteLine(e.ToString());
105  Console.Write(e.StackTrace);
106  }
107  }
108  return toReturn.ToString();
109  }
110 
111  public virtual string PostJsonToURL(string json)
112  {
113  string result = "";
114  try
115  {
116  System.Uri myUri = new Uri(solverUrl);
117  HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(myUri);
118 
119  //If this line is decommented an exception is thrown (option not allowed)
120  //httpRequest.AllowReadStreamBuffering = false;
121  httpRequest.AllowWriteStreamBuffering = true;
122 
123  httpRequest.ContentType = "application/json";
124  //httpRequest.Accept = "application/json,text/plain";
125 
126  httpRequest.Method = "POST";
127 
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);
132  os.Flush();
133  os.Close();
134 
135  StringBuilder sb = new StringBuilder();
136  HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
137 
138  if (httpResponse.StatusCode == HttpStatusCode.OK)
139  {
140  StreamReader br = new StreamReader(httpResponse.GetResponseStream(), Encoding.UTF8);
141  string line = null;
142 
143  while (!((line = br.ReadLine()) is null))
144  sb.Append(line + "\n");
145 
146  br.Close();
147  result = sb.ToString();
148  }
149  else
150  {
151  throw new PDDLException("HTTP connection error, response code : " + httpResponse.StatusCode + " response message : " + new StreamReader(httpResponse.GetResponseStream()).ReadToEnd());
152  }
153  }
154  catch (Exception e)
155  {
156  throw new PDDLException("Impossible to perform HTTP connection: " + e.Message);
157  }
158  return result;
159  }
160 
161  protected internal abstract string ReadFile(string s);
162  }
163 }
it.unical.mat.embasp.languages
Definition: AnswerSet.cs:5
base
Definition: Callback.cs:1
it.unical.mat.embasp.languages.pddl.PDDLInputProgram
Definition: PDDLInputProgram.cs:5
it.unical.mat.embasp
Definition: AnswerSet.cs:5
it.unical
Definition: AnswerSet.cs:5
it.unical.mat.embasp.specializations.solver_planning_domains.SPDUtility
Definition: SPDUtility.cs:14
it
Definition: AnswerSet.cs:5
it.unical.mat.embasp.specializations.solver_planning_domains
Definition: SPDDesktopService.cs:5
it.unical.mat.embasp.languages.pddl
Definition: Action.cs:1
it.unical.mat.embasp.languages.pddl.PDDLException
Definition: PDDLException.cs:5
base.InputProgram
Definition: InputProgram.cs:7
it.unical.mat
Definition: AnswerSet.cs:5