00001 package edu.tum.cs.ias.knowrob;
00002
00003 import java.io.BufferedInputStream;
00004 import java.io.File;
00005 import java.io.FileInputStream;
00006 import java.io.IOException;
00007 import java.util.ArrayList;
00008 import java.util.Arrays;
00009 import java.util.HashMap;
00010 import java.util.Vector;
00011
00012 import com.google.common.base.CaseFormat;
00013 import com.google.common.base.Joiner;
00014
00015 import net.sf.json.*;
00016 import edu.tum.cs.ias.knowrob.prolog.PrologInterface;
00017 import edu.tum.cs.ias.knowrob.utils.ros.RosUtilities;
00018
00019 public class PlanExporter {
00020
00021
00022 public static final String MAPPING_CONFIG_FILE = "etc/knowrob2cpl.json";
00023
00028 private ArrayList<String> plan;
00029
00033 private JSONObject knowrobToCPL;
00034 private HashMap<String, String> designators;
00035 private int inst_counter = 100;
00036
00037 public PlanExporter() throws IOException {
00038
00039 designators = new HashMap<String, String>();
00040 knowrobToCPL = readCplMapping(RosUtilities.rospackFind("knowrob_actions") + "/" +MAPPING_CONFIG_FILE);
00041 plan = new ArrayList<String>();
00042 }
00043
00044 public String exportPlanToCPL(String plan_name) {
00045
00046 plan_name = PrologInterface.addSingleQuotes(plan_name);
00047 String res = "";
00048
00049 HashMap<String, Vector<String>> plan_steps =
00050 PrologInterface.executeQuery("plan_subevents("+ plan_name +", Steps)");
00051
00052
00053 for(String dottedpairs : (plan_steps.get("Steps"))) {
00054
00055
00056 for(String[] actions: PrologInterface.dottedPairsToArrayList(dottedpairs)) {
00057
00058 for(String action : new ArrayList<String>(Arrays.asList(actions))) {
00059
00060 HashMap<String, Vector<String>> params =
00061 PrologInterface.executeQuery("class_properties("+action+", Prop, Val)");
00062
00063 String obj_desig="", loc_desig="", device="";
00064
00065 for(int i=0;i<params.get("Prop").size();i++) {
00066
00067 String prop = PrologInterface.removeSingleQuotes(params.get("Prop").get(i));
00068 String val = PrologInterface.removeSingleQuotes(params.get("Val").get(i));
00069
00070
00071 if(prop.endsWith("objectActedOn")) {
00072
00073 obj_desig = objectDesignatorFromOWLclass(val);
00074
00075 } else if(prop.endsWith("toLocation") || prop.endsWith("fromLocation")) {
00076
00077 int idx = params.get("Prop").lastIndexOf(PrologInterface.addSingleQuotes("http://ias.cs.tum.edu/kb/knowrob.owl#objectActedOn"));
00078
00079
00080 String obj = "";
00081 if(idx>-1 && params.get("Val").contains(idx)) {
00082 obj = params.get("Val").get(idx);
00083 }
00084
00085 loc_desig = locationDesignatorFromOWLclass(val, obj);
00086
00087 } else if(prop.endsWith("deviceUsed")) {
00088 device = val;
00089 }
00090
00091 }
00092
00093
00094
00095 String rplAction = knowrobToCpl(action);
00096
00097 String action_spec = "";
00098 if(rplAction.endsWith("object-in-hand")) {
00099 action_spec = "(achieve (object-in-hand "+ obj_desig +" ?side))";
00100
00101 } else if(rplAction.endsWith("object-placed-at")) {
00102 action_spec = "(achieve (object-placed-at "+ obj_desig +" "+ loc_desig +"))";
00103
00104 } else if(rplAction.endsWith("arm-parked")) {
00105 action_spec = "(achieve (arm-parked "+device+"))";
00106
00107 } else if(rplAction.endsWith("arms-at")) {
00108 action_spec = "(achieve (arms-at " + loc_desig + "))";
00109
00110 } else if(rplAction.endsWith("looking-at")) {
00111 action_spec = "(achieve (looking-at "+ loc_desig +"))";
00112
00113 } else if(rplAction.endsWith("perceive-all")) {
00114 action_spec = "(perceive-all "+ obj_desig +")";
00115
00116 } else if(rplAction.endsWith("perceive")) {
00117 action_spec = "(perceive "+ obj_desig +")";
00118
00119 } else if(rplAction.endsWith("loc")) {
00120 action_spec = "(achieve (loc "+ loc_desig +"))";
00121
00122 } else if(rplAction.endsWith("gripper-opened")) {
00123 action_spec = "(achieve (gripper-opened "+ device +"))";
00124 }
00125
00126 plan.add(action_spec);
00127
00128 }
00129
00130 }
00131 }
00132
00133
00134
00135
00136 res += "(def-top-level-plan " + lispify(plan_name) + " () \n";
00137 res += "(with-designators (\n\n ";
00138 res += Joiner.on("\n ").join(designators.values());
00139
00140 res += ")\n\n";
00141
00142 res += Joiner.on("\n").join(plan);
00143
00144 res += "))";
00145
00146 return res;
00147 }
00148
00149
00150
00151
00158 private String objectDesignatorFromOWLclass(String objdef) {
00159
00160
00161
00162 HashMap<String, Vector<String>> types = PrologInterface
00163 .executeQuery("rdf_has(" + PrologInterface.addSingleQuotes(objdef) + ", rdf:type, T)");
00164
00165 String obj_type = "";
00166 String obj_inst = "";
00167
00168 String obj_desig = "";
00169
00170
00171 if (!types.get("T").isEmpty()) {
00172
00173 obj_type = knowrobToCpl(types.get("T").firstElement());
00174 obj_inst = knowrobToCpl(objdef);
00175
00176 obj_desig = "(" + lispify(obj_inst) + " (an object\n `((name ," + knowrobToCpl(obj_inst) + ")\n (type ,"+lispify(obj_type)+")) ))\n";
00177
00178 } else {
00179
00180 obj_type = knowrobToCpl(objdef);
00181 obj_inst = knowrobToCpl(instanceFromClass(obj_type));
00182
00183 obj_desig = "(" + lispify(obj_inst) + " (an object\n `((type ," + knowrobToCpl(obj_type) + ")) ))\n";
00184 }
00185
00186
00187 designators.put(obj_inst, obj_desig);
00188 return obj_inst;
00189 }
00190
00191
00199 private String locationDesignatorFromOWLclass(String loc, String obj_desig) {
00200
00201
00202
00203
00204 HashMap<String, Vector<String>> params = PrologInterface
00205 .executeQuery("class_properties(" + PrologInterface.addSingleQuotes(loc) + ", Prop, Val); " +
00206 "owl_has("+PrologInterface.addSingleQuotes(loc)+", Prop, Val)");
00207
00208
00209 loc = knowrobToCpl(loc);
00210
00211
00212 String loc_desig = "(" + loc + " ";
00213 loc_desig += "(a location `(";
00214
00215
00216 if(params!=null && params.get("Prop")!=null) {
00217
00218 for (int i = 0; i < params.get("Prop").size(); i++) {
00219
00220 String prop = PrologInterface.removeSingleQuotes(params.get("Prop").get(i));
00221 String val = params.get("Val").get(i);
00222
00223
00224 if ((prop.endsWith("in-ContGeneric")
00225 || prop.endsWith("in-UnderspecifiedContainer")
00226 || prop.endsWith("into-UnderspecifiedContainer")
00227 || prop.endsWith("from-UnderspecifiedLocation"))) {
00228
00229 loc_desig += "\n (in ,"
00230 + knowrobToCpl(locationDesignatorFromOWLclass(val, obj_desig));
00231
00232 if(!obj_desig.isEmpty())
00233 loc_desig += ")\n (of ," + obj_desig + ")";
00234
00235 } else if (prop.endsWith("to-UnderspecifiedLocation")
00236 || prop.endsWith("on-Physical")
00237 || prop.endsWith("aboveOf")
00238 || prop.endsWith("inCenterOf")) {
00239 loc_desig += "\n (on ,"
00240 + knowrobToCpl(locationDesignatorFromOWLclass(val, obj_desig));
00241
00242 if(!obj_desig.isEmpty())
00243 loc_desig += ")\n (for ," + obj_desig + ")";
00244
00245 } else if (prop.endsWith("inReachOf") ||
00246 prop.endsWith("inFrontOf-Generally")) {
00247 loc_desig += "\n (to reach ," + knowrobToCpl(objectDesignatorFromOWLclass(val)) + ")";
00248
00249 } else if (prop.endsWith("visibleFrom")) {
00250 loc_desig += "\n (to see ," + knowrobToCpl(objectDesignatorFromOWLclass(val)) + ")";
00251
00252 } else if (prop.endsWith("orientation")) {
00253 loc_desig += "\n (pose ," + knowrobToCpl(objectDesignatorFromOWLclass(val)) + ")";
00254
00255 } else if (prop.endsWith("type")) {
00256 loc_desig += "\n (type ," + knowrobToCpl(val) + ")";
00257
00258 } else {
00259 loc_desig += "\n (name ," + knowrobToCpl(val) + ")";
00260 }
00261
00262 }
00263 }
00264 loc_desig += "))\n";
00265
00266 designators.put(loc, loc_desig);
00267 return loc;
00268 }
00269
00270
00271
00283 private String knowrobToCpl(String val) {
00284
00285 if(knowrobToCPL.containsKey(PrologInterface.valueFromIRI(val))) {
00286 return knowrobToCPL.getString(PrologInterface.valueFromIRI(val));
00287
00288 } else {
00289
00290
00291 HashMap<String, Vector<String>> supercl = PrologInterface.
00292 executeQuery("owl_subclass_of(" + PrologInterface.addSingleQuotes(val) + ", Super)");
00293
00294 for(String sup : supercl.get("Super")) {
00295 if(knowrobToCPL.containsKey(PrologInterface.valueFromIRI(sup))) {
00296 return knowrobToCPL.getString(PrologInterface.valueFromIRI(sup));
00297 }
00298 }
00299 return lispify(val);
00300 }
00301 }
00302
00310 private String lispify(String pl) {
00311
00312
00313 String val = PrologInterface.valueFromIRI(PrologInterface.removeSingleQuotes(pl));
00314 val = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, val);
00315 val = val.replaceAll("--", "-");
00316
00317 return val;
00318 }
00319
00320
00321
00330 public JSONObject readCplMapping(String configFile)
00331 throws java.io.IOException {
00332
00333 byte[] buffer = new byte[(int) new File(configFile).length()];
00334 BufferedInputStream f = null;
00335 try {
00336 f = new BufferedInputStream(new FileInputStream(configFile));
00337 f.read(buffer);
00338 } finally {
00339 if (f != null)
00340 try {
00341 f.close();
00342 } catch (IOException ignored) {
00343 }
00344 }
00345
00346 return (JSONObject) JSONSerializer.toJSON(new String(buffer));
00347 }
00348
00349
00357 private String instanceFromClass(String cl) {
00358 return (cl + (inst_counter++)).toLowerCase();
00359 }
00360
00361
00367 public static void main(String[] args) {
00368
00369 try {
00370
00371 PrologInterface.initJPLProlog("knowrob_actions");
00372 PlanExporter pl = new PlanExporter();
00373 String plan = pl.exportPlanToCPL("http://www.roboearth.org/kb/serve_drink.owl#ServeADrink");
00374 System.out.println(plan);
00375
00376 } catch (IOException e) {
00377 e.printStackTrace();
00378 }
00379
00380 }
00381
00382 }