00001 package edu.tum.cs.probcog;
00002
00003 import java.io.IOException;
00004 import java.util.Collection;
00005 import java.util.Vector;
00006
00007 import edu.tum.cs.logic.parser.ParseException;
00008 import edu.tum.cs.yarprpc.Bottle;
00009 import edu.tum.cs.yarprpc.Network;
00010 import edu.tum.cs.yarprpc.YarpRpcCall;
00011 import edu.tum.cs.yarprpc.YarpRpcReply;
00012 import edu.tum.cs.yarprpc.YarpRpcServer;
00013
00014 public class YarpServer extends Server {
00015
00019 YarpRpcServer port;
00020
00028 public YarpServer(String modelPoolFile, String portName) throws IOException, ParseException, Exception {
00029 super(modelPoolFile);
00030 port = new YarpRpcServer(portName);
00031 }
00032
00038 public static Vector<String[]> readListOfLists(Bottle listOfLists) {
00039 Vector<String[]> ret = new Vector<String[]>();
00040 for(int i = 0; i < listOfLists.size(); i++) {
00041 Bottle list = listOfLists.get(i).asList();
00042 String[] tuple = new String[list.size()];
00043 for(int j = 0; j < list.size(); j++)
00044 tuple[j] = list.get(j).toString();
00045 ret.add(tuple);
00046 }
00047 return ret;
00048 }
00049
00055 public static void writeListOfLists(Collection<String[]> listOfLists, Bottle target) {
00056 for(String[] l : listOfLists) {
00057 Bottle b = target.addList();
00058 for(String item : l)
00059 b.addString(item);
00060 }
00061 }
00062
00063 protected void checkNumParams(YarpRpcCall call, int n) throws Exception {
00064 if(call.size() != n)
00065 throw new Exception(String.format("Call to procedure '%s' requires exactly %d parameters.", call.procName(), n));
00066 }
00067
00074 public YarpRpcReply handleCall(YarpRpcCall call) {
00075 try {
00076 YarpRpcReply result = new YarpRpcReply(call);
00077 if(call.procName().equals("query")) {
00078 checkNumParams(call, 3);
00079
00080 String modelName = call.get(0).toString();
00081 Model model = this.modelPool.getModel(modelName);
00082 Vector<String> queries = queriesFromTuples(readListOfLists(call.get(1).asList()));
00083 Vector<String[]> evidence = readListOfLists(call.get(2).asList());
00084 Vector<InferenceResult> results = query(modelName, queries, evidence);
00085
00086 Bottle listOfResults = result;
00087 for(InferenceResult r : results) {
00088 Bottle tuple = listOfResults.addList();
00089 tuple.addString(r.functionName);
00090 for(String param : r.params)
00091 tuple.addString(param);
00092 tuple.addDouble(r.probability);
00093 }
00094 }
00095 else if(call.procName().equals("getPredicates")) {
00096 checkNumParams(call, 1);
00097 String modelName = call.get(0).toString();
00098 writeListOfLists(getPredicates(modelName), result);
00099 }
00100 else if(call.procName().equals(("getDomains"))) {
00101 checkNumParams(call, 1);
00102 String modelName = call.get(0).toString();
00103 writeListOfLists(getDomains(modelName), result);
00104 }
00105 else
00106 throw new Exception("Don't know how to handle calls to method '" + call.procName() + "'");
00107 return result;
00108 }
00109 catch(Exception e) {
00110 e.printStackTrace();
00111 YarpRpcReply result = new YarpRpcReply(call);
00112 result.addString("error");
00113 result.addString(e.getMessage());
00114 return result;
00115 }
00116 }
00117
00121 public void run() {
00122 while(true) {
00123 System.out.println("Waiting for call...");
00124 YarpRpcCall cl = port.read();
00125 System.out.println("\nCall: " + cl.procName() + " " + cl.toString());
00126 port.reply(handleCall(cl));
00127 }
00128 }
00129
00130 public void test(YarpRpcCall cl) {
00131 System.out.println("\nCall: " + cl.procName() + " " + cl.toString());
00132 System.out.println("Result: " + handleCall(cl).toString());
00133 }
00134
00138 public void test() {
00139
00140 Vector<String[]> query = readListOfLispTuples("((sitsAtIn ?PERSON ?SEATING-LOCATION M) (usesAnyIn ?PERSON ?UTENSIL M))");
00141 Vector<String[]> evidence = readListOfLispTuples("((takesPartIn P1 M) (name P1 Anna) (takesPartIn P2 M) (name P2 Bert) (takesPartIn P3 M) (name P3 Dorothy) (mealT M Breakfast))");
00142 YarpRpcCall cl = new YarpRpcCall("query");
00143 cl.addString("tableSetting");
00144 writeListOfLists(query, cl.addList());
00145 writeListOfLists(evidence, cl.addList());
00146 test(cl);
00147
00148 cl = new YarpRpcCall("getPredicates");
00149 cl.addString("tableSetting");
00150 test(cl);
00151
00152 cl = new YarpRpcCall("getDomains");
00153 cl.addString("tableSetting");
00154 test(cl);
00155 }
00156
00157 public static void main(String[] args) {
00158
00159 System.loadLibrary("jyarprpc");
00160 Network.init();
00161 try {
00162 System.out.println("\nProbCog YARP Server\n");
00163
00164 String modelPoolFile = null;
00165 boolean doTest = false;
00166 for(int i = 0; i < args.length; i++) {
00167 if(args[i].charAt(0) == '-') {
00168 if(args[i].equals("-?")) {
00169 System.out.println("usage: YarpServer [-test] [XML file containing pool of models]\n");
00170 return;
00171 }
00172 else if(args[i].equals("-test"))
00173 doTest = true;
00174 }
00175 else
00176 modelPoolFile = args[i];
00177 }
00178 if(modelPoolFile == null) {
00179 modelPoolFile = "/tmp/buildd/ros-diamondback-knowrob-0.2.0/debian/ros-diamondback-knowrob/opt/ros/diamondback/stacks/knowrob/srldb/models/models.xml";
00180 System.out.println("No model pool file specified, defaulting to " + modelPoolFile);
00181 }
00182
00183 YarpServer server = new YarpServer(modelPoolFile, "/rpc/probcog");
00184 if(doTest)
00185 server.test();
00186 server.run();
00187 }
00188 catch(Exception e) {
00189 e.printStackTrace();
00190 }
00191 }
00192 }