PlanExporter.java
Go to the documentation of this file.
00001 package instruction.exporter;
00002 
00003 import instruction.opencyc.OpenCyc;
00004 
00005 import java.io.IOException;
00006 import java.net.UnknownHostException;
00007 import java.util.ArrayList;
00008 import java.util.HashMap;
00009 import java.util.Iterator;
00010 import java.util.List;
00011 import java.util.Map;
00012 import org.opencyc.api.CycAccess;
00013 import org.opencyc.api.CycApiException;
00014 import org.opencyc.api.CycObjectFactory;
00015 import org.opencyc.cycobject.CycFort;
00016 import org.opencyc.cycobject.CycList;
00017 import org.opencyc.cycobject.CycObject;
00018 import org.opencyc.cycobject.CycVariable;
00019 
00020 public class PlanExporter {
00021         public static final String MT_UNIVERSAL_VOCABULARY = "#$UniversalVocabularyMt";
00022         public static final String MT_TUM_KITCHEN_ENVIRONMENT = "#$TUMKitchenEnvironmentMt";
00023         public static final String MT_TUM_KITCHEN_ENVIRONMENT_PLANNING = "#$TUMKitchenEnvironmentPlanningMt";
00024 
00025         private static String[] spatialRelations = { "TopSide", "RightSide",
00026                         "BottomSide", "CornerOnObject", "CenterOfGeographicalRegion",
00027                         "FrontSide", "Side" };
00032         private ArrayList<String> plan;
00033 
00034         private HashMap<String, String> rplName; // mapping from Cyc concepts to RPL names
00035 
00040         private HashMap<String, String> actionTypes;
00041 
00048         private HashMap<String, CycList> actionParameters;
00049 
00055         private HashMap<String, CycList> objects;
00056 
00061         private HashMap<String, String> locations;
00062 
00063         private HashMap<String, String> objLocations; // object goal locations
00064 
00068         private List<String> locationDesignators = new ArrayList<String>();
00069 
00073         private List<String> objectDesignators = new ArrayList<String>();
00074 
00079         private Map<String, String> actionLocations = null;
00080 
00081         private static CycAccess cyc = null;
00082         private static PlanExporter me = null;
00083 
00084         private CycObject kitchenEnvMt = null;
00085 
00086         String designators = new String();
00087         String goals = new String();
00088 
00089         int locCounter = 0;
00090 
00091         private PlanExporter() throws UnknownHostException, CycApiException,
00092                         IOException {
00093 
00094                 cyc = OpenCyc.getInstance().getCycAcces();
00095                 kitchenEnvMt = OpenCyc.getInstance().getKitchenEnvMt();
00096 
00097         }
00098 
00099         public static PlanExporter getInstance() throws UnknownHostException,
00100                         CycApiException, IOException {
00101 
00102                 if (me == null)
00103                         me = new PlanExporter();
00104 
00105                 return me;
00106 
00107         }
00108 
00109         public void readRplNames() {
00110                 this.rplName = new HashMap<String, String>();
00111 
00112                 // TODO: read these values from the KB
00113                 rplName.put("PuttingSomethingSomewhere", "object-at-place");
00114 
00115                 // /////////////////////////////////////////////////////////////////////
00116                 // object classes
00117 
00118                 rplName.put("StoveTopCookingPot", "cooking-pot");
00119                 rplName.put("Water", "water");
00120                 rplName.put("CookingRange", "cooker");
00121                 rplName.put("MeasuringCup", "");
00122                 rplName.put("Tap-FlowControlDevice", "tap");
00123                 rplName.put("Burner", "hot-plate");
00124                 rplName.put("Soup", "");
00125                 rplName.put("Packet", "");
00126                 rplName.put("StoveTop", "hot-plate");
00127                 rplName.put("HeatingDevice", "hot-plate");
00128                 rplName.put("Content", "");
00129                 rplName.put("Noodle", "pasta");
00130                 rplName.put("Pasta", "pasta");
00131                 rplName.put("TableSalt", "salt");
00132                 rplName.put("Colander", "colander");
00133                 rplName.put("Chair-PieceOfFurniture", "chair");
00134                 rplName.put("Sink", "sink");
00135                 rplName.put("OliveOil", "");
00136                 rplName.put("Tea-Iced", "");
00137                 rplName.put("BakingSoda", "");
00138                 rplName.put("CookingVessel", "pan");
00139                 rplName.put("MixForBakedGoods", "pancake-mix");
00140                 rplName.put("Pancake", "pancake");
00141                 rplName.put("TeaBag", "");
00142                 rplName.put("Tea-Beverage", "");
00143                 rplName.put("ContainerArtifact", "");
00144                 rplName.put("Table-PieceOfFurniture", "table");
00145                 rplName.put("Refrigerator", "refrigerator");
00146                 rplName.put("Kettle", "cooking-pot");
00147                 rplName.put("HotBeveragePot", "cup");
00148                 rplName.put("ContainerLid", "");
00149                 rplName.put("Bottle", "");
00150                 rplName.put("Milk", "");
00151                 rplName.put("Sugar-Table", "");
00152                 rplName.put("SugarBowl", "");
00153                 rplName.put("Teaspoon", "table-spoon");
00154                 rplName.put("DrinkingMug", "cup");
00155                 rplName.put("Saucer", "plate");
00156                 rplName.put("DinnerPlate", "plate");
00157                 rplName.put("Oven", "oven");
00158                 rplName.put("Butter", "");
00159                 rplName.put("Toast", "toast");
00160                 rplName.put("Bread", "toast");
00161                 rplName.put("Bread-Slice", "toast");
00162                 rplName.put("ElectricalToaster", "toaster");
00163                 rplName.put("BreakfastCereal", "");
00164                 rplName.put("Bowl-Eating", "");
00165                 rplName.put("Spoon-SilverwarePiece", "table-spoon");
00166                 rplName.put("Fruit", "");
00167                 rplName.put("MapleSyrup", "");
00168                 rplName.put("PlaceMat", "place-mat");
00169                 rplName.put("Napkin", "napkin");
00170                 rplName.put("Fork-SilverwarePiece", "fork");
00171                 rplName.put("Knife", "knife");
00172 
00173                 // /////////////////////////////////////////////////////////////////////
00174                 // action types
00175 
00176                 // finding something
00177                 rplName.put("SelectingSomething", "find-object");
00178                 rplName.put("LookingForSomething", "find-object");
00179                 rplName.put("Adopting-SelectingSomething", "find-object");
00180 
00181                 // transporting something
00182                 rplName.put("PuttingSomethingSomewhere", "loc");
00183                 rplName.put("TransportationEvent", "loc");
00184                 rplName.put("LiftingAnObject", "object-lifted");
00185                 rplName.put("GettingSomething-HittingAnObject", "object-in-hand");
00186 
00187                 rplName.put("TakingSomething", "object-in-hand");
00188                 rplName.put("RemovingSomething", "object-in-hand");
00189 
00190                 rplName.put("CoveringSomething", "object-on-object");
00191 
00192                 rplName.put("DoingAddition", "object-added-to-container");
00193                 rplName.put("PuttingSomethingIntoSomething", "object-added-to-container");
00194                 rplName.put("Incorporation-Physical", "object-added-to-container");
00195 
00196                 // device-related stuff
00197                 rplName.put("TurningOnPoweredDevice", "hot-plate-temperature-on");
00198                 rplName.put("TurningOffPoweredDevice", "hot-plate-temperature-off");
00199                 rplName.put("ChangingDeviceState", "hot-plate-temperature-change");
00200 
00201                 // cupboard-related stuff
00202                 rplName.put("OpeningAContainer", "enclosing-object-opened");
00203                 rplName.put("ClosingAContainer", "object-closed");
00204 
00205                 // filling and transfilling containers
00206                 rplName.put("FillingProcess", "container-filled");
00207                 rplName.put("FluidFlow-Translation", "container-filled");
00208                 rplName.put("FluidFlow-Translation", "container-content-transfilled");
00209 
00210                 // stirring either a container or some stuff inside a container
00211                 rplName.put("Stirring", "container-content-stirred");
00212                 rplName.put("Flipping", "object-flipped");
00213 
00214                 // wait-like statements
00215                 rplName.put("Checking-Evaluating", "sleep");
00216                 rplName.put("Observing", "sleep");
00217                 rplName.put("Waiting", "sleep");
00218                 rplName.put("KeepingInACertainState", "sleep");
00219 
00220                 rplName.put("CookingFood", "cook");
00221 
00222                 // achieve-like statements
00223                 rplName.put("CausingToBeInACertainCondition", "achieve");
00224                 rplName.put("Remembering", "achieve");
00225 
00226                 // general
00227                 rplName.put("UsingAnObject", "use");
00228 
00229                 // future work:
00230                 // rplName.put("ServingFoodOrDrink", "");
00231                 // rplName.put("IntrinsicStateChangeEvent", ""); // bring the water to a
00232                 // boil
00233                 // rplName.put("EatingAMorsel", "");
00234                 // rplName.put("SpreadingOntoSurface", "");
00235                 // rplName.put("SlicingSomething", "");
00236                 // rplName.put("Cleaning", "");
00237                 // rplName.put("MoppingAFloor", "water-wiped-up");
00238                 // rplName.put("BeingSomething", "");
00239                 // rplName.put("Garnishment", "");
00240 
00241                 // Plan categories:
00242                 // CookingFood
00243                 // BoilingFood
00244                 // PreparingFoodOrDrink
00245                 // ArrangingObjects
00246                 // 
00247 
00248                 // /////////////////////////////////////////////////////////////////////
00249                 // instances
00250                 // rplName.put("placemat1", "placemat-1");
00251                 // rplName.put("napkin1", "napkin-1");
00252                 // rplName.put("dinnerplate1", "plate-1");
00253                 // rplName.put("fork-silverwarepiece1", "fork-1");
00254                 // rplName.put("knife1", "knife-1");
00255                 // rplName.put("spoon-silverwarepiece1", "spoon-1");
00256                 // rplName.put("drinkingmug1", "cup-1");
00257 
00258         }
00259 
00260         @SuppressWarnings({ "deprecation" })
00261         public void readActionsForPlanFromKB(String plan)
00262                         throws UnknownHostException, IOException {
00263 
00264                 this.plan = new ArrayList<String>();
00265 
00266                 CycList query = cyc.makeCycList("(#$methodForAction " + plan
00267                                 + "(#$actionSequence ?LIST))");
00268                 CycVariable var = CycObjectFactory.makeCycVariable("?LIST");
00269                 CycFort mt = cyc.getConstantByName("#$TUMKitchenEnvironmentPlanningMt");
00270 
00271                 CycList ret = cyc.askWithVariable(query, var, mt);
00272 
00273                 ArrayList<String> list = new ArrayList<String>();
00274                 for (Iterator i = ret.iterator(); i.hasNext();) {
00275                         String str = i.next().toString();
00276                         list.add(str);
00277                 }
00278                 String[] actions = list.get(0).substring(9, list.get(0).length() - 1)
00279                                 .split(" ");
00280                 for (String act : actions) {
00281                         this.plan.add(act);
00282                 }
00283         }
00284 
00285         public void readActionTypesFromKB() throws UnknownHostException,
00286                         IOException {
00287 
00288                 // get isa-type of the action in TUMKitchenEnvironmentMt
00289                 actionTypes = new HashMap<String, String>();
00290                 for (String action : this.plan) {
00291                         actionTypes.put(action, cyc.getMinIsas(
00292                                         cyc.getConstantByName(action), kitchenEnvMt).toString());
00293                 }
00294 
00295         }
00296 
00297         public void readActionParametersFromKB(ArrayList<String> actionList)
00298                         throws UnknownHostException, IOException {
00299 
00300                 actionParameters = new HashMap<String, CycList>();
00301 
00302                 // query list of parameters per action
00303                 for (String action : actionList) {
00304                         actionParameters.put(action, arg1GAFsAbout(action,
00305                                         MT_TUM_KITCHEN_ENVIRONMENT));
00306                 }
00307         }
00308 
00309         public void readObjectsFromKB(HashMap<String, CycList> paramList)
00310                         throws UnknownHostException, IOException {
00311                 objects = new HashMap<String, CycList>();
00312 
00313                 // iterate over whole list of action parameters and extract objects
00314                 for (String key : paramList.keySet()) {
00315 
00316                         CycList curParamList = paramList.get(key);
00317 
00318                         for (Iterator i = curParamList.iterator(); i.hasNext();) {
00319                                 String curParam = i.next().toString();
00320                                 if ((curParam.contains("objectActedOn"))
00321                                                 || (curParam.contains("objectMoving"))
00322                                                 || (curParam.contains("objectOfStateChange"))) {
00323 
00324                                         String objectName = curParam.substring(1,
00325                                                         curParam.length() - 1).split(" ")[1];
00326                                         objects.put(objectName.trim(), arg1GAFsAbout(objectName,
00327                                                         MT_TUM_KITCHEN_ENVIRONMENT));
00328                                 }
00329                         }
00330 
00331                 }
00332 
00333         }
00334 
00335         public void readLocationsFromKB(HashMap<String, CycList> paramList)
00336                         throws UnknownHostException, IOException {
00337                 locations = new HashMap<String, String>();
00338                 actionLocations = new HashMap<String, String>();
00339 
00340                 locCounter = 0;
00341 
00342                 // iterate over whole list of action parameters and extract locations
00343                 for (String action : paramList.keySet()) {
00344 
00345                         CycList curParamList = paramList.get(action);
00346 
00347                         for (Iterator i = curParamList.iterator(); i.hasNext();) {
00348                                 String curParam = i.next().toString();
00349                                 if (curParam.contains("purposeOf-Generic")) {
00350 
00351                                         String locName = "location" + locCounter++;
00352                                         locations.put(locName, curParam);
00353                                         actionLocations.put(action, locName);
00354                                 }
00355                         }
00356                 }
00357         }
00358 
00359         @SuppressWarnings({ "deprecation" })
00360         public CycList arg1GAFsAbout(String concept, String microtheory)
00361                         throws UnknownHostException, IOException {
00362 
00363                 CycObject mt = cyc.getConstantByName(microtheory);
00364                 CycList query = cyc.makeCycList("(?PRED #$" + concept + " ?ARG)");
00365 
00366                 CycVariable pred = CycObjectFactory.makeCycVariable("?PRED");
00367                 CycVariable arg = CycObjectFactory.makeCycVariable("?ARG");
00368                 ArrayList<CycVariable> vars = new ArrayList<CycVariable>();
00369                 vars.add(pred);
00370                 vars.add(arg);
00371 
00372                 CycList l = cyc.askWithVariables(query, vars, mt);
00373                 return l;
00374         }
00375 
00376         @SuppressWarnings({ "deprecation" })
00377         public CycList arg2GAFsAbout(String concept, String microtheory)
00378                         throws UnknownHostException, IOException {
00379 
00380                 CycObject mt = cyc.getConstantByName(microtheory);
00381                 CycList query = cyc.makeCycList("(?PRED ?ARG #$" + concept + ")");
00382 
00383                 CycVariable pred = CycObjectFactory.makeCycVariable("?PRED");
00384                 CycVariable arg = CycObjectFactory.makeCycVariable("?ARG");
00385                 ArrayList<CycVariable> vars = new ArrayList<CycVariable>();
00386                 vars.add(pred);
00387                 vars.add(arg);
00388 
00389                 return cyc.askWithVariables(query, vars, mt);
00390         }
00391 
00392         public String exportPlanToRPL(String planName) {
00393                 String res = "";
00394                 
00395                 if(actionLocations!=null)
00396                         actionLocations.clear();
00397                 
00398                 if(actionParameters!=null)
00399                         actionParameters.clear();
00400                 if(actionTypes!=null)
00401                         actionTypes.clear();
00402                 if(locationDesignators!=null)
00403                         locationDesignators.clear();
00404                 if(locations!=null)
00405                         locations.clear();
00406                 if(objectDesignators!=null)
00407                         objectDesignators.clear();
00408                 if(objects!=null)               
00409                         objects.clear();
00410                 if(objLocations!=null)
00411                         objLocations.clear();
00412                 
00413                 goals="";
00414                 designators="";
00415                 
00416                 try {
00417 
00418                         // init Cyc-Lisp name mapping
00419                         readRplNames();
00420 
00421                         // get the list of actions for the plan
00422                         readActionsForPlanFromKB(planName);
00423 
00424                         // check for parameters for each action
00425                         readActionParametersFromKB(this.plan);
00426 
00427                         // determine action types
00428                         readActionTypesFromKB();
00429 
00430                         // create lists of objects and locations
00431                         readObjectsFromKB(this.actionParameters);
00432                         readLocationsFromKB(this.actionParameters);
00433 
00434                         // build object designators
00435                         readObjectDesignators(this.objects);
00436 
00437                         // build location designators
00438                         readLocationDesignators(this.locations);
00439 
00440                         // compose object designators
00441                         for (String objDesig : objectDesignators)
00442                                 designators += "         " + objDesig + "\n";
00443 
00444                         // compose location designators
00445                         for (String locDesig : locationDesignators)
00446                                 designators += "         " + locDesig + "\n";
00447 
00448                         // build the achieve statements
00449 
00450                         for (String act : plan) {
00451 
00452                                 String actType = actionTypes.get(act);
00453                                 actType = actType.substring(1, actType.length() - 1);
00454                                 String rplAction = rplName.get(actType);
00455                                 if (rplAction == null) {
00456                                         continue;
00457                                 }
00458                                 if (rplAction.equals("find-entity")) {
00459                                         goals += readFindObject(act);
00460                                 }
00461 
00462                                 else if (rplAction.equals("loc")) {
00463                                         goals += readObjectAtPlace(act);
00464                                 }
00465 
00466                                 else if (rplAction.equals("object-in-hand")) {
00467                                         goals += readObjectPickedUp(act);
00468                                 }
00469 
00470                                 else if (rplAction.equals("object-on-object")) {
00471                                         goals += readObjectOnObject(act);
00472                                 }
00473 
00474                                 else if (rplAction.equals("object-added-to-container")) {
00475                                         goals += readObjectAddedToContainer(act);
00476                                 }
00477 
00478                                 else if (rplAction.matches("hot-plate-temperature")) {
00479                                         goals += readHotPlateTemperature(act);
00480                                 }
00481 
00482                                 else if (rplAction.equals("enclosing-object-opened")) {
00483                                         goals += readEnclosingObjectOpened(act);
00484                                 }
00485 
00486                                 else if (rplAction.equals("object-flipped")) {
00487                                         goals += readObjectFlipped(act);
00488                                 }
00489 
00490                                 else if (rplAction.equals("object-closed")) {
00491                                         goals += readObjectClosed(act);
00492                                 }
00493 
00494                                 else if (rplAction.equals("container-filled")) {
00495                                         goals += readContainerFilled(act);
00496                                 }
00497 
00498                                 else if (rplAction.equals("container-content-transfilled")) {
00499                                         goals += readContainerContentTransfilled(act);
00500                                 }
00501 
00502                                 else if (rplAction.equals("container-content-stirred")) {
00503                                         goals += readContainerContentStirred(act);
00504                                 }
00505 
00506                                 else if (rplAction.equals("sleep")) {
00507                                         goals += readWait(act);
00508                                 }
00509 
00510                                 else if (rplAction.equals("cook")) {
00511                                         goals += readCook(act);
00512                                 }
00513 
00514                                 else if (rplAction.equals("achieve")) {
00515                                         goals += readAchieve(act);
00516                                 }
00517 
00518                                 else if (rplAction.equals("use")) {
00519                                         goals += readUse(act);
00520                                 }
00521 
00522                                 else {
00523                                         goals += "";
00524                                 }
00525                         }
00526 
00527                         // combine designators and achieve statements to a plan
00528                         res += "(def-top-level-plan ehow-"
00529                                         + planName.substring(3, planName.length() - 1).split(" ")[0]
00530                                                         .replaceAll("_", "-") + " ()"
00531                                         + "\n" + "  (with-designators (\n";
00532 
00533                         res += designators;
00534                         res += ")\n";
00535 
00536                         res += goals;
00537 
00538                         res += ")))";
00539 
00540                 //      System.out.println(res);
00541 
00542                 } catch (UnknownHostException e) {
00543                         e.printStackTrace();
00544                 } catch (CycApiException e) {
00545                         e.printStackTrace();
00546                 } catch (IOException e) {
00547                         e.printStackTrace();
00548                 }
00549 
00550                 return res;
00551         }
00552 
00553         private void readObjectDesignators(HashMap<String, CycList> objects) {
00554 
00555                 // create the appropriate object designators
00556                 for (String obj : objects.keySet()) {
00557 
00558                         String object = "";
00559 
00560                         object += "(" + obj + " (object ";
00561 
00562                         // get the list of {parameter - value} mappings
00563                         HashMap<String, String> objparams = cycParamListToStringMap(objects
00564                                         .get(obj));
00565                         for (String par : objparams.keySet()) {
00566                                 String val = objparams.get(par);
00567 
00568                                 // TODO: check this list and verify that the mappings are
00569                                 // correct
00570                                 if (par.equals("isa")) {
00571                                         object += "'((type " + rplName.get(val)
00572                                                         + "))";
00573                                 } else if (par.equals("name")) {
00574                                         object += " (name " + rplName.get(val) + ")";
00575                                 } else if (par.equals("matches")) {
00576                                         object += " (matches " + rplName.get(val)
00577                                                         + ")";
00578                                 } else if (par.equals("matches-internal")) {
00579                                         object += " (matches-internal "
00580                                                         + rplName.get(val) + ")";
00581                                 } else if (par.equals("referenced-by")) {
00582                                         object += " (referenced-by "
00583                                                         + rplName.get(val) + ")";
00584                                 } else if (par.equals("properPhysicalParts")) {
00585                                         object += " (part-of " + rplName.get(val)
00586                                                         + ")";
00587                                 } else if (par.equals("containsPart")) {
00588                                         object += "\n              (parent-of " + rplName.get(val)
00589                                                         + ")";
00590                                 } else if (par.equals("regulating-temperature")) {
00591                                         object += "\n              (regulating-temperature "
00592                                                         + rplName.get(val) + ")";
00593                                 } else if (par.equals("regulating-water-flow")) {
00594                                         object += "\n              (regulating-water-flow "
00595                                                         + rplName.get(val) + ")";
00596                                 } else if (par.equals("fitting")) {
00597                                         object += "\n              (fitting " + rplName.get(val)
00598                                                         + ")";
00599                                 } else if (par.equals("status")) {
00600                                         object += "\n              (status " + rplName.get(val)
00601                                                         + ")";
00602                                 } else if (par.equals("exclude")) {
00603                                         object += "\n              (exclude " + rplName.get(val)
00604                                                         + ")";
00605                                 }
00606                         }
00607                         object += "))";
00608 
00609                         objectDesignators.add(object);
00610                 }
00611         }
00612 
00613         private void readLocationDesignators(HashMap<String, String> locations)
00614                         throws UnknownHostException, IOException {
00615 
00616                 this.objLocations = new HashMap<String, String>();
00617 
00618                 for (String action : plan) {
00619 
00620                         String loc = actionLocations.get(action);
00621 
00622                         if (loc == null)
00623                                 continue;
00624 
00625                         // -> first param is the object, second param is the loc description
00626                         String locExpr = locations.get(loc);
00627 
00628                         String preposition = locExpr.substring(20, locExpr.length() - 2)
00629                                         .split(" ")[0];
00630                         String forObject = locExpr.substring(20, locExpr.length() - 2)
00631                                         .split(" ")[1];
00632                         String locDesc = locExpr.substring(20, locExpr.length() - 2).split(
00633                                         " ")[2];
00634                         String locationDesignator = "";
00635 
00636                         // recursively build the location designator
00637                         locationDesignator += "(" + loc + " ";
00638 
00639                         if (preposition.equals("nextToLikeObjects")) {
00640                                 locationDesignator += "(location `((next-to ,"
00641                                                 + constructLocDesignatorFromCycString(locDesc, action)
00642                                                 + ") (for ," + forObject + ")))";
00643 
00644                                 this.objLocations.put(forObject, loc);
00645 
00646                         } else if ((preposition.equals("in-UnderspecifiedContainer"))||(preposition.equals("into-UnderspecifiedContainer"))) {
00647 
00648                                 locationDesignator += "(location `((in ,"
00649                                                 + constructLocDesignatorFromCycString(locDesc, action)
00650                                                 + ") (of ," + forObject + ")))";
00651                                 this.objLocations.put(forObject, loc);
00652 
00653                         } else if (preposition.equals("from-UnderspecifiedLocation")) {
00654 
00655                                 locationDesignator += "(location `((in ,"
00656                                                 + constructLocDesignatorFromCycString(locDesc, action)
00657                                                 + ") (of ," + forObject + ")))";
00658                                 this.objLocations.put(forObject, loc);
00659 
00660                         } else if (preposition.equals("to-UnderspecifiedLocation")) {
00661                                 System.out.println(locDesc);
00662                                 locationDesignator += "(location `((in ,"
00663                                                 + constructLocDesignatorFromCycString(locDesc, action)
00664                                                 + ") (for ," + forObject + ")))";
00665                                 this.objLocations.put(forObject, loc);
00666 
00667                         } else if (preposition.equals("on-UnderspecifiedSurface")) {
00668 
00669                                 locationDesignator += "(location `((on ,"
00670                                                 + constructLocDesignatorFromCycString(locDesc, action)
00671                                                 + ") (for ," + forObject + ")))";
00672                                 this.objLocations.put(forObject, loc);
00673 
00674                         } else {
00675                                 locationDesignator += "(location `((on ,"
00676                                                 + constructLocDesignatorFromCycString(locDesc, action)
00677                                                 + ") (for ," + forObject + ")))";
00678                                 this.objLocations.put(forObject, loc);
00679                         }
00680                         locationDesignator += ")";
00681 
00682                         locationDesignators.add(locationDesignator);
00683 
00684                 }
00685         }
00686 
00687         private String constructLocDesignatorFromCycString(String locDesc,
00688                         String action) throws UnknownHostException, IOException {
00689 
00690                 // if(this.objects.containsKey(locDesc)) {
00691                 // // location relative to another object -> break the loop
00692                 // return locDesc;
00693                 // } else {
00694                 // GAFs describing the location as part of something else have the
00695                 // location as the second argument (e.g. physicalParts)
00696                 CycList tmpList = arg2GAFsAbout(locDesc, MT_TUM_KITCHEN_ENVIRONMENT);
00697                 HashMap<String, String> locparams = cycParamListToStringMap(tmpList);
00698 
00699                 if (locparams.isEmpty()) {
00700                         // has no parts-underspecified: infer that one of the previous
00701                         // instruction
00702                         String locDescType = cyc.getMinIsas(cyc.getConstantByName(locDesc),
00703                                         kitchenEnvMt).toString();
00704                         locDescType = locDescType.substring(1, locDescType.length() - 1);
00705 
00706                         if (containsString(spatialRelations, locDescType)) {
00707                                 String prevLocation = getObjectLocationOfPreviousAction(action);
00708                                 String locExpr = locations.get(prevLocation);
00709 
00710 //                              String preposition = locExpr
00711 //                                              .substring(20, locExpr.length() - 2).split(" ")[0];
00712 //                              String forObject = locExpr.substring(20, locExpr.length() - 2)
00713 //                                              .split(" ")[1];
00714                                 String locDesc2 = locExpr.substring(20, locExpr.length() - 2)
00715                                                 .split(" ")[2];
00716                                 return constructLocDesignatorFromCycString(locDesc2, getPreviousAction(action));
00717                         }
00718                         
00719                         // if the object is not contained in the object designators, add it
00720                         if(!getAllObjectIds().contains(locDesc)) {
00721                                 
00722                                 String objType = cyc.getMinIsas(cyc.getConstantByName(locDesc), kitchenEnvMt).toString();
00723                                 objType = objType.replace((CharSequence) "(", ""); 
00724                                 objType = objType.replace((CharSequence) ")", "");
00725                                 objType = objType.split(" ")[0];
00726                 
00727                                 objectDesignators.add("(" + locDesc + " (object '((type " + rplName.get(objType) + "))))");
00728                         }
00729 
00730                         return locDesc;
00731                 } else {
00732                         String locDescType = cyc.getMinIsas(cyc.getConstantByName(locDesc),
00733                                         kitchenEnvMt).toString();
00734                         locDescType = locDescType.substring(1, locDescType.length() - 1);
00735                         String[] types = locDescType.split(" ");
00736 
00737                         // CornerOnObject has to be processed first because it's possible
00738                         // that the entity is also an instance of LeftSide, RightSide,
00739                         // TopSide or FrontSide (e.g. "top left corner")
00740 
00741                         if (containsString(types, "CornerOnObject")) {
00742 
00743                                 String location = "on-";
00744 
00745                                 if (containsString(types, "TopSide"))
00746                                         location += "back";
00747                                 else if (containsString(types, "BottomSide"))
00748                                         location += "front";
00749                                 if (containsString(types, "LeftSide"))
00750                                         location += "-left";
00751                                 else if (containsString(types, "RightSide"))
00752                                         location += "-right";
00753 
00754                                 String locDesignator = constructLocDesignatorFromCycString(
00755                                                 locparams.get("parts-Underspecified"), action);
00756 
00757                                 if (locDesignator.split(" ").length > 1) {
00758                                         // Nested location: build a new location on top level
00759                                         String locId = "location" + this.locCounter++;
00760                                         locationDesignators.add("(" + locId + " `(a location "
00761                                                         + locDesignator + "))");
00762                                         return "(" + location + " ," + locId + ")";
00763                                 } else
00764                                         return "(" + location + " ," + locDesignator + ")";
00765 
00766                         } else if (types[0].equals("LeftSide")) {
00767                                 String locDesignator = constructLocDesignatorFromCycString(
00768                                                 locparams.get("parts-Underspecified"), action);
00769                                 if (locDesignator.split(" ").length > 1) {
00770                                         // Nested location: build a new location on top level
00771                                         String locId = "location" + this.locCounter++;
00772                                         locationDesignators.add("(" + locId + " `(a location "
00773                                                         + locDesignator + "))");
00774                                         return "(left-of ," + locId + ")";
00775                                 } else
00776                                         return "(left-of ," + locDesignator + ")";
00777 
00778                         } else if (types[0].equals("RightSide")) {
00779                                 String locDesignator = constructLocDesignatorFromCycString(
00780                                                 locparams.get("parts-Underspecified"), action);
00781                                 if (locDesignator.split(" ").length > 1) {
00782                                         // Nested location: build a new location on top level
00783                                         String locId = "location" + this.locCounter++;
00784                                         locationDesignators.add("(" + locId + " `(a location "
00785                                                         + locDesignator + "))");
00786                                         return "(right-of ," + locId + ")";
00787                                 } else
00788                                         return "(right-of ," + locDesignator + ")";
00789 
00790                         } else if (types[0].equals("CenterOfGeographicalRegion")) {
00791                                 String locDesignator = constructLocDesignatorFromCycString(
00792                                                 locparams.get("parts-Underspecified"), action);
00793                                 if (locDesignator.split(" ").length > 1) {
00794                                         // Nested location: build a new location on top level
00795                                         String locId = "location" + this.locCounter++;
00796                                         locationDesignators.add("(" + locId + " `(a location "
00797                                                         + locDesignator + "))");
00798                                         return "(on-center ," + locId + ")";
00799                                 } else
00800                                         return "(on-center ," + locDesignator + ")";
00801 
00802                         } else if (types[0].equals("Side")) {
00803                                 String locDesignator = constructLocDesignatorFromCycString(
00804                                                 locparams.get("parts-Underspecified"), action);
00805                                 if (locDesignator.split(" ").length > 1) {
00806                                         // Nested location: build a new location on top level
00807                                         String locId = "location" + this.locCounter++;
00808                                         locationDesignators.add("(" + locId + " `(a location "
00809                                                         + locDesignator + "))");
00810                                         return "(next-to ," + locId + ")";
00811                                 } else
00812                                         return "(next-to ," + locDesignator + ")";
00813 
00814                         } else if (types[0].equals("FrontSide")) {
00815                                 String locDesignator = constructLocDesignatorFromCycString(
00816                                                 locparams.get("parts-Underspecified"), action);
00817                                 if (locDesignator.split(" ").length > 1) {
00818                                         // Nested location: build a new location on top level
00819                                         String locId = "location" + this.locCounter++;
00820                                         locationDesignators.add("(" + locId + " `(a location "
00821                                                         + locDesignator + "))");
00822                                         return "(on-front ," + locId + ")";
00823                                 } else
00824                                         return "(on-front ," + locDesignator + ")";
00825 
00826                         } else {
00827                                 return locDesc;
00828                         }
00829                 }
00830                 // }
00831         }
00832 
00840         private void getInvolvedEntities(String id, List<String> entities) {
00841 
00842                 if (actionParameters.get(id) != null) {
00843                         // id is an action
00844                         HashMap<String, String> params = cycParamListToStringMap(actionParameters
00845                                         .get(id));
00846                         entities.add(params.get("objectActedOn"));
00847 
00848                         getInvolvedEntities(objLocations.get(params.get("objectActedOn")),
00849                                         entities);
00850 
00851                 } else if (getAllLocationIds().contains(id)) {
00852                         // id is a location
00853 
00854                         for (String locDesig : locationDesignators) {
00855                                 if (id.equals(locDesig.substring(1, locDesig.indexOf(" ")))) {
00856                                         for (String obj : getAllObjectIds()) {
00857                                                 if (locDesig.contains(obj) && !entities.contains(obj))
00858                                                         entities.add(obj);
00859                                         }
00860                                         for (String loc : getAllLocationIds()) {
00861                                                 if (locDesig.indexOf(loc) > 1) {
00862                                                         getInvolvedEntities(loc, entities);
00863                                                 }
00864                                         }
00865                                 }
00866                         }
00867 
00868                 }
00869 
00870         }
00871 
00872         private List<String> getAllObjectIds() {
00873                 List<String> ids = new ArrayList<String>();
00874 
00875                 for (String objDesig : objectDesignators) {
00876                         String objId = objDesig.substring(1, objDesig.indexOf(" "));
00877                         ids.add(objId);
00878                 }
00879                 return ids;
00880         }
00881 
00882         private List<String> getAllLocationIds() {
00883                 List<String> ids = new ArrayList<String>();
00884 
00885                 for (String locDesig : locationDesignators) {
00886                         String locId = locDesig.substring(1, locDesig.indexOf(" "));
00887                         ids.add(locId);
00888                 }
00889                 return ids;
00890         }
00891 
00892         private String makePerceiveForEntities(List<String> entities) {
00893                 String str = "";
00894 
00895                 for (String e : entities)
00896                         str += "(perceive " + e + ")\n";
00897 
00898                 return str;
00899         }
00900 
00901         private String getPreviousAction(String action) {
00902                 for (int i = 0; i < plan.size(); i++) {
00903 
00904                         if (i > 0 && action.equals(plan.get(i)))
00905                                 return plan.get(i - 1);
00906                 }
00907                 return null;
00908         }
00909 
00910         private String getObjectLocationOfPreviousAction(String action) {
00911                 for (int i = 0; i < plan.size(); i++) {
00912 
00913                         String loc = actionLocations.get(plan.get(i));
00914 
00915                         if (loc != null && action.equals(plan.get(i)) && i > 0)
00916                                 return actionLocations.get(plan.get(i - 1));
00917                 }
00918                 return null;
00919         }
00920 
00921         @SuppressWarnings("unused")
00922         private String getObjectLocationForAction(String action) {
00923                 HashMap<String, String> params = cycParamListToStringMap(actionParameters
00924                                 .get(action));
00925                 return objLocations.get(params.get("objectActedOn").trim());
00926         }
00927 
00928         // read parameters for the object-at-place action and create the achieve
00929         // statement
00930         private String readObjectAtPlace(String action) {
00931 
00932                 HashMap<String, String> params = cycParamListToStringMap(actionParameters
00933                                 .get(action));
00934 
00935                 List<String> entities = new ArrayList<String>();
00936                 getInvolvedEntities(action, entities);
00937 
00938                 // return the achieve statement
00939                 return "(perceive " + params.get("objectActedOn") + ")\n"
00940                                 + "(achieve `(loc ," + params.get("objectActedOn") + " ," 
00941                                 + objLocations.get(params.get("objectActedOn").trim()) + "))\n";
00942         }
00943 
00944         private String readFindObject(String action) {
00945 
00946                 // translate the parameters into a lisp representation
00947                 HashMap<String, String> params = cycParamListToStringMap(actionParameters
00948                                 .get(action));
00949 
00950                 // return the achieve statement
00951                 return "(achieve (findEntity\n" + "      "
00952                                 + params.get("objectActedOn") + "))\n";
00953         }
00954 
00955         private String readObjectPickedUp(String action) {
00956 
00957                 // translate the parameters into a lisp representation
00958                 HashMap<String, String> params = cycParamListToStringMap(actionParameters
00959                                 .get(action));
00960                 
00961                 List<String> entities = new ArrayList<String>();
00962                 getInvolvedEntities(action, entities);
00963                 
00964                 return  "(perceive " + params.get("objectActedOn") + ")\n"
00965                                 + "(achieve `(object-in-hand ," 
00966                                 + params.get("objectActedOn") + "))\n";
00967 
00968         }
00969 
00970         private String readObjectOnObject(String action) {
00971 
00972                 // translate the parameters into a lisp representation
00973                 HashMap<String, String> params = cycParamListToStringMap(actionParameters
00974                                 .get(action));
00975 
00976                 try {
00977                         if (cyc.getMinIsas(
00978                                         cyc.getConstantByName(params.get("objectActedOn")),
00979                                         kitchenEnvMt).toString().matches("StoveTopCookingPot")) {
00980 
00981                                 // object is a pot, has to be covered with a lid which we add to
00982                                 // the object designators
00983                                 this.designators += "(lid1 `(an entity" + "     (type lid)))";
00984                                 return "(achieve (object-on-entity lid1 "
00985                                                 + params.get("objectActedOn") + "))\n";
00986 
00987                         }
00988                 } catch (Exception e) {
00989                 }
00990 
00991                 return "(achieve (object-on-entity))\n";
00992         }
00993 
00994         private String readObjectAddedToContainer(String action) { // TODO: infer
00995                 // the container
00996                 // the object
00997                 // shall be
00998                 // added to
00999 
01000                 HashMap<String, String> params = cycParamListToStringMap(actionParameters
01001                                 .get(action));
01002 
01003                 return "(achieve (object-added-to-container "
01004                                 + params.get("objectActedOn") + "))\n";
01005         }
01006 
01007         private String readHotPlateTemperature(String action) {
01008 
01009                 // translate the parameters into a lisp representation
01010                 HashMap<String, String> params = cycParamListToStringMap(actionParameters
01011                                 .get(action));
01012 
01013                 if (action.equals("hot-plate-temperature-on")) {
01014 
01015                         return "(achieve (hot-plate-temperature\n" + "      "
01016                                         + params.get("objectActedOn") + "\n" + " :on))\n";
01017 
01018                 } else if (action.equals("hot-plate-temperature-off")) {
01019 
01020                         return "(achieve (hot-plate-temperature\n" + "      "
01021                                         + params.get("objectActedOn") + "\n" + " :off))\n";
01022 
01023                 } else {
01024                         // TODO: read the current state and change it
01025                         return "(achieve (hot-plate-temperature\n" + "      "
01026                                         + params.get("objectActedOn") + "\n" + " :off))\n";
01027                 }
01028 
01029         }
01030 
01031         private String readEnclosingObjectOpened(String action) {
01032 
01033                 HashMap<String, String> params = cycParamListToStringMap(actionParameters
01034                                 .get(action));
01035                 return "(achieve (enclosing-object-opened\n" + "      "
01036                                 + params.get("objectActedOn") + "))\n";
01037 
01038         }
01039 
01040         private String readObjectFlipped(String action) {
01041 
01042                 HashMap<String, String> params = cycParamListToStringMap(actionParameters
01043                                 .get(action));
01044                 
01045                 List<String> entities = new ArrayList<String>();
01046                 getInvolvedEntities(action, entities);
01047                 return "(perceive " + params.get("objectActedOn") + ")\n" + 
01048                                 "(achieve `(object-flipped ,"
01049                                 + params.get("objectActedOn") + "))\n";
01050 
01051         }
01052         
01053         private String readObjectClosed(String action) {
01054 
01055                 HashMap<String, String> params = cycParamListToStringMap(actionParameters
01056                                 .get(action));
01057                 return "(achieve (object-closed\n" + "      "
01058                                 + params.get("objectActedOn") + "))\n";
01059         }
01060 
01061         private String readContainerFilled(String action) {
01062 
01063                 HashMap<String, String> params = cycParamListToStringMap(actionParameters
01064                                 .get(action));
01065                 // TODO
01066                 // if(objectActedOn subClassOf Container)
01067 
01068                 // get purposeOf -> withUnderSpecAgent for the filler
01069                 String stuff = params.get("purposeOf-Generic");
01070                 if (stuff != null && stuff.matches(".*with-Underspecified.*")) {
01071                         stuff = stuff.substring(28, stuff.length() - 2).split(" ")[1];
01072                 }
01073 
01074                 return "(achieve (container-filled\n" + "      "
01075                                 + params.get("objectActedOn") + "\n" + "      :" + stuff
01076                                 + " 1.0l))\n"; // TODO: quantifier
01077 
01078                 // TODO: check if the parameters better fit
01079                 // container-content-transfilled
01080         }
01081 
01082         private String readContainerContentTransfilled(String action) { // TODO
01083 
01084                 // translate the parameters into a lisp representation
01085                 HashMap<String, String> params = cycParamListToStringMap(actionParameters.get(action));
01086 
01087                 
01088                 String stuff = params.get("purposeOf-Generic");
01089                 String cont="";
01090                 if (stuff != null && stuff.matches(".*into-UnderspecifiedContainer.*")) {
01091                         cont  = stuff.substring(30, stuff.length() - 1).split(" ")[1];
01092                         stuff = stuff.substring(30, stuff.length() - 1).split(" ")[0];
01093                 }
01094                 
01095                 List<String> entities = new ArrayList<String>();
01096                 getInvolvedEntities(action, entities);
01097                 return "(perceive " + stuff + ")\n" + "(perceive " + cont + ")\n" +
01098                            "(achieve `(container-content-transfilled ,"+stuff+" ,"+cont+"))\n"; // TODO: check
01099 
01100         }
01101 
01102         private String readContainerContentStirred(String action) { // TODO
01103 
01104                 // translate the parameters into a lisp representation
01105                 // HashMap<String, String> params =
01106                 // cycParamListToStringMap(actionParameters.get(action));
01107 
01108                 // create object designator for wooden spoon
01109                 // (:tag search-wooden-spoon
01110                 // (setf wooden-spoon (perceive wooden-spoon))
01111                 // (setf wooden-spoon-original-pose (copy-object-values [pose [reference
01112                 // wooden-spoon]])))
01113 
01114                 // (achieve (container-content-stirred cooking-pot wooden-spoon))
01115                 // (achieve (loc
01116                 // wooden-spoon
01117                 // (=>designator `(a location (pose ,wooden-spoon-original-pose))) )))
01118 
01119                 // find wooden spoon, stir, put spoon back
01120                 // (achieve (container-content-stirred cooking-pot wooden-spoon)
01121                 return "(achieve (container-content-stirred))\n";
01122         }
01123 
01124         private String readWait(String action) {
01125 
01126                 // translate the parameters into a lisp representation
01127                 HashMap<String, String> params = cycParamListToStringMap(actionParameters
01128                                 .get(action));
01129 
01130                 String timespan = params.get("timeSpan");
01131 
01132                 String[] unit_value = { "" };
01133                 if (timespan != null)
01134                         unit_value = timespan.substring(1, timespan.length() - 1)
01135                                         .split(" ");
01136 
01137                 int time=0;
01138                 if (unit_value.length == 2) {
01139 
01140                         // determine the value
01141                         if (unit_value[1].matches("[0-9.]*")) { // time directly given as
01142                                 // numeric value
01143                                 time = Integer.valueOf(unit_value[1]);
01144                         } else if (unit_value[1].equals("#$Few-Quant")) {
01145                                 time = 2;
01146                         }
01147 
01148                         // compute value in seconds
01149                         if (unit_value[0].equals("MinutesDuration")) {
01150                                 time *= 60;
01151                         } else if (unit_value[0].equals("HoursDuration")) {
01152                                 time *= 3600;
01153                         }
01154 
01155                 }
01156 
01157                 return "(sleep " + time + ")\n";
01158         }
01159 
01160         private String readCook(String action) { // TODO
01161 
01162                 // translate the parameters into a lisp representation
01163                 // HashMap<String, String> params =
01164                 // cycParamListToStringMap(actionParameters.get(action));
01165 
01166                 return "(achieve (cook))\n";
01167         }
01168 
01169         private String readAchieve(String action) { // TODO
01170 
01171                 // translate the parameters into a lisp representation
01172                 // HashMap<String, String> params =
01173                 // cycParamListToStringMap(actionParameters.get(action));
01174 
01175                 // build and return the achieve statement based on the parameters
01176                 return "(achieve ())\n";
01177         }
01178 
01179         private String readUse(String action) { // TODO
01180 
01181                 // translate the parameters into a lisp representation
01182                 // HashMap<String, String> params =
01183                 // cycParamListToStringMap(actionParameters.get(action));
01184 
01185                 // retrieve from the knowledge base how to use the objectActedOn in the
01186                 // given context
01187 
01188                 return "(achieve (container-content-transfilled))\n";
01189         }
01190 
01191         // transform the Cyc list into a mapping {paramName -> paramValue}
01192         private HashMap<String, String> cycParamListToStringMap(CycList cycPar) {
01193                 HashMap<String, String> res = new HashMap<String, String>();
01194 
01195                 for (Iterator i = cycPar.iterator(); i.hasNext();) {
01196                         String curParam = i.next().toString();
01197 
01198                         String[] key_val = curParam.substring(1, curParam.length() - 1)
01199                                         .split(" ");
01200 
01201                         String key = key_val[0];
01202                         String val = "";
01203                         for (int j = 1; j < key_val.length; j++) {
01204                                 if (j > 1)
01205                                         val += " ";
01206                                 val += key_val[j];
01207                         }
01208                         res.put(key, val);
01209 
01210                 }
01211 
01212                 return res;
01213         }
01214 
01215         private boolean containsString(String[] list, String str) {
01216                 for (String s : list)
01217                         if (s.equals(str))
01218                                 return true;
01219                 return false;
01220         }
01221 
01222 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends


comp_ehow
Author(s): Moritz Tenorth, Daniel Nyga
autogenerated on Tue Apr 16 2013 00:18:02