Go to the documentation of this file.00001
00004 import java.net.*;
00005 import java.io.IOException;
00006 import java.util.*;
00007 import pddl4j.Parser;
00008 import pddl4j.PDDLObject;
00009 import java.io.*;
00010 import java.util.Set;
00011 import pddl4j.exp.AtomicFormula;
00012 import java.util.List;
00013 import pddl4j.exp.term.Term;
00014
00015 class First {
00016 private final int CODE_OK = 100;
00017 private final int CODE_ERROR_COMMAND = 200;
00018 private final int CODE_ERROR_FILENAME = 300;
00019 private final int CODE_RESULT = 400;
00020 private final int CODE_EXIT = 500;
00021
00022 private final String MSG_OK = "OK";
00023 private final String MSG_ILLEGAL_FIRST_KEYWORD = "Illigal first command word";
00024 private final String MSG_ILLEGAL_SECOND_KEYWORD = "Illigal second command word";
00025 private final String MSG_ILLEGAL_FILENAME = "File not exists";
00026 private final String MSG_EXIT = "Server shutdown";
00027
00028 private final String COMMAND_UPLOAD = "UPLOAD";
00029 private final String COMMAND_DOMAIN = "DOMAIN_FILE";
00030 private final String COMMAND_PROBLEM = "PROBLEM_FILE";
00031 private final String COMMAND_RESULT = "RESULT";
00032 private final String COMMAND_EXIT = "EXIT";
00033
00034 private Properties _options;
00035 private Parser _parser;
00036 private PDDLObject _domain;
00037 private PDDLObject _problem;
00038 private PDDLObject _pb;
00039 protected InetAddress ip;
00040 protected int port;
00041 protected DataOutputStream outputStream;
00042 First(){
00043 _options = Graphplan.getParserOptions();
00044 _parser = new Parser(_options);
00045 }
00046 public void run() {
00047 ServerSocket srvSocket;
00048 port = 10000;
00049 try {
00050 ip = InetAddress.getByName("127.0.0.1");
00051 } catch (UnknownHostException e) {
00052 System.err.println("Unknown host IP address: ");
00053 return;
00054 }
00055 try {
00056 srvSocket = new ServerSocket(port, 5, ip);
00057 } catch (IOException e) {
00058 System.err.println("FATAL ERROR: server socket could not be created!");
00059 System.err.println("IOException message: " + e.getMessage());
00060 return;
00061 }
00062 System.out.println("Diagnosis Server socket created!");
00063 try {
00064 Socket newSocket;
00065 newSocket = null;
00066
00067 try {
00068 newSocket = srvSocket.accept();
00069 } catch (IOException e) {
00070 System.err.println("Server cannot make new Connection! ");
00071 return;
00072 }
00073
00074 System.out.println("Incoming connection accepted!");
00075 try{
00076 InputStream inStream = newSocket.getInputStream();
00077 BufferedReader reader = new BufferedReader(new InputStreamReader(inStream));
00078
00079 OutputStream outStream = newSocket.getOutputStream();
00080 outputStream = new DataOutputStream(new BufferedOutputStream(outStream));
00081 String line = "";
00082 while (!line.equalsIgnoreCase(COMMAND_EXIT)) {
00083 line = reader.readLine();
00084 System.out.println(line);
00085 line = line.trim();
00086 String[] firstLine = ((String)line).split(" ");
00087 String send_msg;
00088
00089 if(firstLine[0].equalsIgnoreCase(COMMAND_UPLOAD)){
00090 if (firstLine[1].equals(COMMAND_DOMAIN)){
00091 boolean domain_loaded = load_domain_file(firstLine[2]);
00092 if(domain_loaded)
00093 send_response(CODE_OK, MSG_OK);
00094 else
00095 send_response(CODE_ERROR_FILENAME, MSG_ILLEGAL_FILENAME);
00096 }
00097 else
00098 if (firstLine[1].equalsIgnoreCase(COMMAND_PROBLEM)){
00099 boolean problem_loaded = load_problem_file(firstLine[2]);
00100 if(problem_loaded)
00101 send_response(CODE_OK, MSG_OK);
00102 else
00103 send_response(CODE_ERROR_FILENAME, MSG_ILLEGAL_FILENAME);
00104 }
00105 else
00106 send_response(CODE_ERROR_COMMAND, MSG_ILLEGAL_SECOND_KEYWORD);
00107 }
00108 else if (firstLine[0].equalsIgnoreCase(COMMAND_RESULT)){
00109 String results = processResult();
00110 send_response(CODE_OK, results);
00111 }
00112 else if (firstLine[0].equalsIgnoreCase(COMMAND_EXIT))
00113 send_response(CODE_EXIT, MSG_EXIT);
00114 else send_response(CODE_ERROR_COMMAND, MSG_ILLEGAL_FIRST_KEYWORD);
00115
00116 }
00117 } catch (IOException e) {
00118 System.err.println("IOException in connection: " + e.getMessage());
00119 if (!newSocket.isClosed()) {
00120 try {
00121 newSocket.close();
00122 } catch (IOException eIO) {}
00123 }
00124 }
00125
00126 closeSocket(srvSocket);
00127
00128 } finally {
00129 closeSocket(srvSocket);
00130 }
00131 }
00132 boolean load_domain_file(String domain_file){
00133 try{
00134 _domain = _parser.parse(new File(domain_file));
00135 return true;
00136 }catch(FileNotFoundException e){
00137 System.out.println("Domain file does not exist!");
00138 return false;
00139 }
00140 }
00141
00142 boolean load_problem_file(String problem_file){
00143 try{
00144 _problem = _parser.parse(new File(problem_file));
00145 return true;
00146 }catch(FileNotFoundException e){
00147 System.out.println("Repair Goal file does not exist!");
00148 return false;
00149 }
00150 }
00151 String processResult(){
00152 String result = "";
00153 _pb = _parser.link(_domain, _problem);
00154
00155 try{
00156 Graphplan gplan = new Graphplan(_pb);
00157 gplan.preprocessing();
00158 Plan plan = gplan.solve();
00159 if (plan != Plan.FAILURE) {
00160 String actionServer=null;
00161 String param = null;
00162 for (Set<AtomicFormula> layer : plan) {
00163 for (AtomicFormula action : layer) {
00164 actionServer = action.getPredicate();
00165 ArrayList<String> params = new ArrayList<String>();
00166 for (Term parameter : action){
00167 params.add(parameter.getImage());
00168 result += actionServer+"("+params.get(0).toString()+") ";
00169 }
00170 }
00171 }
00172 }
00173 return result;
00174 }catch(Throwable t){
00175 System.err.println(t.getMessage());
00176 t.printStackTrace(System.err);
00177 return result;
00178 }
00179 }
00180 void send_response(int err_code, String err_msg){
00181 String response = String.valueOf(err_code) + ": " + err_msg + "\n";
00182 try{
00183 outputStream.writeBytes(response);
00184 outputStream.flush();
00185 }
00186 catch (IOException e){
00187 System.out.println(e.getMessage());
00188 }
00189 }
00190 void closeSocket(ServerSocket sock){
00191 try {
00192 sock.close();
00193 } catch (IOException eIO) {
00194 System.out.println("Error while closing the socket");
00195 }
00196 }
00197 public static void main(String[] args) {
00198 First first = new First();
00199 first.run();
00200 }
00201 }