1 package it.unical.mat.embasp.specializations.solver_planning_domains;
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStreamReader;
6 import java.io.OutputStream;
7 import java.net.HttpURLConnection;
10 import it.unical.mat.embasp.base.InputProgram;
11 import it.unical.mat.embasp.languages.pddl.PDDLException;
12 import it.unical.mat.embasp.languages.pddl.PDDLInputProgram;
16 private final String solverUrl =
"http://solver.planning.domains/solve";
21 private static final String escape(
final String escapable) {
22 final StringBuilder builder =
new StringBuilder();
24 for(
int i = 0; i < escapable.length(); i++) {
25 final char character = escapable.charAt(i);
28 case '"': builder.append(
"\\\"");
break;
29 case '\\': builder.append(
"\\\\");
break;
30 case '\b': builder.append(
"\\b");
break;
31 case '\f': builder.append(
"\\f");
break;
32 case '\n': builder.append(
"\\n");
break;
33 case '\r': builder.append(
"\\r");
break;
34 case '\t': builder.append(
"\\t");
break;
35 case '/': builder.append(
"\\/");
break;
37 if(((character >=
'\u0000') && (character <=
'\u001F')) || ((character >=
'\u007F') && (character <=
'\u009F')) || ((character >=
'\u2000') && (character <=
'\u20FF'))) {
38 final String characterHexCode = Integer.toHexString(character);
40 builder.append(
"\\u");
42 for(
int k = 0; k < (4 - characterHexCode.length()); k++)
45 builder.append(characterHexCode.toUpperCase());
47 builder.append(character);
51 return builder.toString();
54 public String createJson(
final List<InputProgram> pddlInputProgram)
throws PDDLException {
63 switch (pip.getProgramsType()) {
66 domain += getFromFile(pip.getFilesPaths(), pip.
getSeparator());
70 problem += getFromFile(pip.getFilesPaths(), pip.
getSeparator());
73 throw new PDDLException(
"Program type : " + pip.getProgramsType() +
" not valid.");
77 if (problem.equals(
""))
79 if (domain.equals(
""))
82 return "{\"problem\":\"" + escape(problem) +
"\", \"domain\":\"" + escape(domain) +
"\"}";
85 private String getFromFile(
final List<String> filesPaths,
final String separator) {
86 final StringBuilder toReturn =
new StringBuilder();
87 for (
final String s : filesPaths)
89 toReturn.append(readFile(s)).append(separator);
90 }
catch (
final IOException e) {
93 return toReturn.toString();
96 public String postJsonToURL(
final String json)
throws PDDLException {
100 final URL myurl =
new URL(solverUrl);
101 final HttpURLConnection con = (HttpURLConnection) myurl.openConnection();
102 con.setDoOutput(
true);
103 con.setDoInput(
true);
105 con.setRequestProperty(
"Content-Type",
"application/json");
107 con.setRequestProperty(
"Method",
"POST");
108 final OutputStream os = con.getOutputStream();
109 os.write(json.getBytes(
"UTF-8"));
113 final StringBuilder sb =
new StringBuilder();
114 final int HttpResult = con.getResponseCode();
115 if (HttpResult == HttpURLConnection.HTTP_OK) {
116 final BufferedReader br =
new BufferedReader(
new InputStreamReader(con.getInputStream(),
"utf-8"));
118 while ((line = br.readLine()) !=
null)
119 sb.append(line +
"\n");
121 result = sb.toString();
123 throw new PDDLException(
"HTTP connection error, response code : " + con.getResponseCode() +
" response message : " + con.getResponseMessage());
124 }
catch (
final Exception e) {
125 throw new PDDLException(
"Impossible to perform HTTP connection: " + e.getMessage());
131 protected abstract String readFile(String s)
throws IOException;