Diagnosis_planner.java
Go to the documentation of this file.
00001 /* Diagnosis Planner TCP Server 
00002 
00003 # Copyright (c). 2012. OWNER: Institute for Software Technology TU-Graz Austria.
00004 # Authors: Safdar Zaman, Gerald Steinbauer. (szaman@ist.tugraz.at, steinbauer@ist.tugraz.at)
00005 # All rights reserved.
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions are met:
00008 # 1. Redistributions of source code must retain the above copyright notice,
00009 # this list of conditions and the following disclaimer.
00010 # 2. Redistributions in binary form must reproduce the above copyright notice,
00011 # this list of conditions and the following disclaimer in the documentation 
00012 # and/or other materials provided with the distribution.
00013 # 3. Neither the name of the <ORGANIZATION> nor the names of its contributors
00014 # may be used to endorse or promote products derived from this software without
00015 # specific prior written permission.
00016 #
00017 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
00019 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
00021 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSE-
00022 # QUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00023 # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
00026 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00027 # DAMAGE.
00028 ##
00029 # Diagnosis Planner is a TCP protocol based Server that interacts with diagnosis_repair for repair.
00030 
00031 */
00032 import java.net.*;
00033 import java.io.IOException;
00034 import java.util.*;
00035 import pddl4j.Parser;
00036 import pddl4j.PDDLObject;
00037 import java.io.*;
00038 import java.util.Set;
00039 import pddl4j.exp.AtomicFormula;
00040 import java.util.List;
00041 import pddl4j.exp.term.Term;
00042 
00043 class Diagnosis_planner {
00044 private final int CODE_OK = 100;
00045 private final int CODE_ERROR_COMMAND = 200;
00046 private final int CODE_ERROR_FILENAME = 300;
00047 private final int CODE_RESULT = 400;
00048 private final int CODE_ERROR_RESULT = 500;
00049 private final int CODE_EXIT = 600;
00050 
00051 private final String MSG_OK = "OK";
00052 private final String MSG_ILLEGAL_FIRST_KEYWORD = "Illigal first command word";
00053 private final String MSG_ILLEGAL_SECOND_KEYWORD = "Illigal second command word";
00054 private final String MSG_ERROR_RESULT = "Nil result, domain and/or problem file error";
00055 private final String MSG_ILLEGAL_FILENAME = "File not exists";
00056 private final String MSG_EXIT = "Server shutdown";
00057 
00058 private final String COMMAND_UPLOAD = "UPLOAD";
00059 private final String COMMAND_DOMAIN = "DOMAIN_FILE";
00060 private final String COMMAND_PROBLEM = "PROBLEM_FILE";
00061 private final String COMMAND_RESULT = "RESULT";
00062 private final String COMMAND_EXIT = "EXIT";
00063 
00064 private Properties _options;
00065 private Parser _parser;
00066 private PDDLObject _domain;
00067 private PDDLObject _problem;
00068 private PDDLObject _pb;
00069 protected InetAddress ip;
00070 protected int port;
00071 protected String header,body;
00072 protected DataOutputStream outputStream;
00073 private String domain_file, problem_file_path;
00074     Diagnosis_planner(String domain_file){
00075       this.domain_file = domain_file;
00076       problem_file_path = this.getClass().getProtectionDomain().getCodeSource().getLocation().toString().substring(5);
00077       System.out.println("you gave path:"+this.domain_file);
00078       _options = Graphplan.getParserOptions();
00079       _parser = new Parser(_options);
00080     }
00081     public void run() {
00082         ServerSocket srvSocket;
00083         port = 10001;
00084         try {
00085             ip = InetAddress.getByName("127.0.0.1");
00086         } catch (UnknownHostException e) {
00087             System.err.println("Unknown host IP address: ");
00088             return;
00089         }
00090         try {
00091             srvSocket = new ServerSocket(port, 5, ip);
00092         } catch (IOException e) {
00093             System.err.println("FATAL ERROR: server socket could not be created!");
00094             System.err.println("IOException message: " + e.getMessage());
00095             return;
00096         }
00097         System.out.println("Diagnosis Server socket created!");
00098         try {
00099                 Socket newSocket;
00100                 newSocket = null;
00101                 
00102                 try {
00103                     newSocket = srvSocket.accept();
00104                 } catch (IOException e) {
00105                     System.err.println("Server cannot make new Connection! ");
00106                     return;
00107                 }
00108                 
00109                System.out.println("Incoming connection accepted!");
00110                try{
00111                InputStream inStream = newSocket.getInputStream();
00112                BufferedReader reader = new BufferedReader(new InputStreamReader(inStream));
00113             
00114                OutputStream outStream = newSocket.getOutputStream();
00115                outputStream = new DataOutputStream(new BufferedOutputStream(outStream));
00116                String line = "";
00117                boolean first_line = true;
00118                boolean end_data = false;
00119                while (!line.equalsIgnoreCase(COMMAND_EXIT)) {               
00120                 line = reader.readLine();
00121                 if(line.length() > 0){
00122                         if(first_line){
00123                                 header = line;
00124                                 body = "";
00125                                 first_line = false;
00126                         }
00127                         else
00128                                 body += line;
00129                         end_data = false;
00130                 }else{ if(first_line)
00131                         first_line = false;
00132                       else if(end_data){
00133                                 first_line = true;
00134                                 System.out.println("header:"+header);
00135                                 System.out.println("body:"+body);
00136                                 String[] headerLine = ((String)header).split(" ");
00137                                 header = "";
00138                                 String send_msg;
00139                 
00140                                 if(headerLine[0].equalsIgnoreCase(COMMAND_UPLOAD)){
00141                                         if (headerLine[1].equalsIgnoreCase(COMMAND_DOMAIN)){
00142                                                 boolean domain_loaded = load_domain_file(domain_file);
00143                                                 if(domain_loaded) 
00144                                                         send_response(CODE_OK, MSG_OK);
00145                                                 else
00146                                                         send_response(CODE_ERROR_FILENAME, MSG_ILLEGAL_FILENAME);
00147                                         }else if (headerLine[1].equalsIgnoreCase(COMMAND_PROBLEM)){
00148                                                 boolean problem_loaded = load_problem_file(problem_file_path+"repair_problem.pddl");
00149                                                 if(problem_loaded)
00150                                                         send_response(CODE_OK, MSG_OK);
00151                                                 else
00152                                                         send_response(CODE_ERROR_FILENAME, MSG_ILLEGAL_FILENAME);
00153                                         }else
00154                                                 send_response(CODE_ERROR_COMMAND, MSG_ILLEGAL_SECOND_KEYWORD);
00155                                 }else if (headerLine[0].equalsIgnoreCase(COMMAND_RESULT)){
00156                                                 String result = processResult();
00157                                                 if(result.equals("nil"))
00158                                                         send_response(CODE_ERROR_RESULT, MSG_ERROR_RESULT);
00159                                                 else
00160                                                         send_response(CODE_RESULT, result);
00161                                 }else if (headerLine[0].equalsIgnoreCase(COMMAND_EXIT))
00162                                                 send_response(CODE_EXIT, MSG_EXIT);
00163                                         else    send_response(CODE_ERROR_COMMAND, MSG_ILLEGAL_FIRST_KEYWORD);
00164                                                         
00165                      } 
00166             end_data = true;
00167             }
00168                                 
00169                 
00170 
00171                }
00172                } catch (IOException e) {
00173                      System.err.println("IOException in connection: " + e.getMessage());
00174                      e.printStackTrace(System.err);
00175                if (!newSocket.isClosed()) {
00176                  try {
00177                     newSocket.close();
00178                  } catch (IOException eIO) {}
00179              }
00180              }
00181                        
00182              closeSocket(srvSocket);      
00183 
00184         } finally {
00185             closeSocket(srvSocket);
00186         }
00187     }  // run()
00188     boolean load_domain_file(String domain_file){
00189        try{
00190           _domain = _parser.parse(new File(domain_file));
00191           return true;
00192        }catch(FileNotFoundException e){
00193                 System.out.println("Domain file does not exist!"+e.toString());
00194                 return false;
00195       }
00196     }
00197 
00198     boolean load_problem_file(String problem_file){
00199        try{
00200           BufferedWriter out=new BufferedWriter(new FileWriter(problem_file));
00201           out.write(body);
00202           out.close();
00203           _problem = _parser.parse(new File(problem_file));
00204           return true;
00205        }catch(FileNotFoundException e){
00206                 System.out.println("Repair Goal file does not exist!");
00207                 return false;
00208       } catch(IOException e){System.out.println(e.toString());return false;}
00209     }
00210     String processResult(){
00211       String result = "";
00212       
00213       try{
00214          _pb = _parser.link(_domain, _problem);
00215          Graphplan gplan = new Graphplan(_pb);
00216          gplan.preprocessing();
00217          Plan plan = gplan.solve();
00218          if (plan != Plan.FAILURE) {
00219                 String actionServer=null;
00220                 String param = null;
00221                 for (Set<AtomicFormula> layer : plan) {
00222                      for (AtomicFormula action : layer) {
00223                         actionServer = action.getPredicate();
00224                         ArrayList<String> params = new ArrayList<String>();
00225                             for (Term parameter : action){ 
00226                                params.add(parameter.getImage());
00227                                result += actionServer+"("+params.get(0).toString()+") ";
00228                             }
00229                       }
00230                 }
00231           }
00232       plan.actions.clear();
00233       return result;                                                                                    
00234       }catch(Throwable t){
00235                 return "nil";
00236       }
00237     }
00238     void send_response(int msg_code, String msg){
00239         String response = String.valueOf(msg_code) + ": " + msg + "\n";
00240         System.out.println(response);
00241         try{
00242            outputStream.writeBytes(response);
00243            outputStream.flush();
00244         }
00245         catch (IOException e){
00246         System.out.println(e.getMessage());
00247         }
00248     }
00249     void closeSocket(ServerSocket sock){
00250         try {
00251                 sock.close();
00252             } catch (IOException eIO) {
00253                   System.out.println("Error while closing the socket");
00254               }
00255     }
00256     public static void main(String[] args) {
00257          if(args.length < 1){
00258               System.out.println("Error: repair_domain.pddl file required with complete path as argument!");
00259               System.out.println("e.g. \"/home/../repair_domain.pddl\"");
00260          }else{
00261                Diagnosis_planner diag_planner = new Diagnosis_planner(args[0]);
00262                diag_planner.run();
00263          }
00264     }
00265 }


tug_ist_diagnosis_repair
Author(s): Safdar Zaman
autogenerated on Mon Jan 6 2014 11:51:12