OWLExporter.java
Go to the documentation of this file.
00001 package instruction.exporter.owl;
00002 
00003 import instruction.configuration.ConfigurationManager;
00004 import instruction.exceptions.InstructionException;
00005 import instruction.importer.PlanImporter;
00006 import instruction.opencyc.OpenCyc20;
00007 import instruction.semanticObjects.Instruction;
00008 import instruction.semanticObjects.ObjectX;
00009 import instruction.semanticObjects.Preposition;
00010 import instruction.semanticObjects.Word;
00011 import instruction.wrapper.LocalFileWrapper;
00012 
00013 import java.io.File;
00014 import java.io.FileWriter;
00015 import java.io.IOException;
00016 import java.io.StringWriter;
00017 import java.net.UnknownHostException;
00018 import java.util.Collection;
00019 import java.util.HashMap;
00020 import java.util.HashSet;
00021 import java.util.Iterator;
00022 import java.util.List;
00023 import java.util.Map;
00024 import java.util.Set;
00025 
00026 import edu.stanford.smi.protege.exception.OntologyLoadException;
00027 import edu.stanford.smi.protegex.owl.ProtegeOWL;
00028 import edu.stanford.smi.protegex.owl.model.OWLClass;
00029 import edu.stanford.smi.protegex.owl.model.OWLModel;
00030 import edu.stanford.smi.protegex.owl.model.OWLNamedClass;
00031 import edu.stanford.smi.protegex.owl.model.OWLObjectProperty;
00032 import edu.stanford.smi.protegex.owl.model.OWLSomeValuesFrom;
00033 import edu.stanford.smi.protegex.owl.model.ProtegeInstance;
00034 import edu.stanford.smi.protegex.owl.model.RDFSClass;
00035 import edu.stanford.smi.protegex.owl.writer.rdfxml.rdfwriter.OWLModelWriter;
00036 
00037 public class OWLExporter {
00038 
00039         static {
00040                 ConfigurationManager.loadSettings();
00041         }
00042         
00043         public static final String KITCHEN_ONTOLOGY_URI = ConfigurationManager.getPathKnowRob();
00044         public static final String ABSTRACT_ACTION_CLASS = "knowrob:Action";
00045         public static final String ABSTRACT_OBJECT_CLASS = "knowrob:SpatialThing-Localized";
00046         public static final String ABSTRACT_OBJECT_PROPERTY = "knowrob:spatiallyRelated";
00047 
00048         private static Map<String, OWLNamedClass> actionStore = new HashMap<String, OWLNamedClass>();
00049         private static Map<String, OWLNamedClass> objectStore = new HashMap<String, OWLNamedClass>();
00050         private static Map<String, OWLObjectProperty> propertyStore = new HashMap<String, OWLObjectProperty>();
00051 
00052         private static int locationCounter = 1;
00053 
00054         private PlanImporter importer;
00055 
00056         public OWLExporter() {
00057                 
00058                 try {
00059                         this.initOWLConverter();
00060                         
00061                 } catch (UnknownHostException e) {
00062                         e.printStackTrace();
00063                 } catch (InstructionException e) {
00064                         e.printStackTrace();
00065                 } catch (IOException e) {
00066                         e.printStackTrace();
00067                 } catch (Exception e) {
00068                         e.printStackTrace();
00069                 }
00070         }
00071         
00072         
00079         public String convertHowtoToOWLOntology(String howto) {
00080 
00081                 try {
00082 
00083                         // Convert Howto
00084                         String filePath = ConfigurationManager.getPathHowtos() + "/"
00085                                         + howto.replace(" ", "_");
00086 
00087                         if (new File(filePath).exists()) {
00088                                 LocalFileWrapper wrapper = new LocalFileWrapper();
00089                                 wrapper.load(filePath);
00090                                 importer.setWrapper(wrapper);
00091                                 importer.parseInstructions();
00092 
00093                                 importer.recognizeAndDisambiguateInstructions();
00094 
00095                                 String owlContent = convertInstructionsToOWL(importer
00096                                                 .getInstructions(), importer.getTitle(), howto);
00097 
00098                                 return owlContent;
00099                         }
00100 
00101                 } catch (InstructionException e) {
00102                         e.printStackTrace();
00103                 } catch (IOException e) {
00104                         e.printStackTrace();
00105                 } catch (Exception e) {
00106                         e.printStackTrace();
00107                 }
00108 
00109                 return null;
00110         }
00111 
00112         
00113         
00122         public String convertHowtoToOWLFile(String howto) {
00123 
00124                 try {
00125 
00126                         // Convert Howto
00127                         String owlContent = convertHowtoToOWLOntology(howto);
00128 
00129                         // Create temporary owl file
00130                         String owlFileName = howto.replace(" ", "_") + ".owl";
00131                         File owlFile = new File(new File(".").getAbsolutePath() + "/" + owlFileName);
00132 
00133                         if (!owlFile.exists())
00134                                 owlFile.createNewFile();
00135                         
00136                         System.out.println("Writing OWL file...");
00137                         FileWriter writer = new FileWriter(owlFile);
00138                         writer.write(owlContent);
00139                         writer.close();
00140                         
00141                         return owlFile.getAbsolutePath();
00142 
00143 
00144                 } catch (IOException e) {
00145                         e.printStackTrace();
00146                 } catch (Exception e) {
00147                         e.printStackTrace();
00148                 }
00149 
00150                 return null;
00151         }
00152         
00153         
00154         
00155         
00156         
00167         private PlanImporter initOWLConverter() throws InstructionException,
00168                         Exception, UnknownHostException, IOException {
00169                 
00170                 readAvailableActions();
00171                 readAvailableObjects();
00172                 readAvailableObjectProperties();
00173 
00174                 ConfigurationManager.loadSettings();
00175                 ConfigurationManager
00176                                 .setPathDisambiguator("./etc/disambiguator.xml");
00177                 ConfigurationManager.setPathHowtos("./howtos");
00178 
00179                 importer = new PlanImporter();
00180                 importer.initialize();
00181 
00182                 System.out.println("Initializing Plan-Importer...");
00183                 Map<String, List<String>> mappings = ConfigurationManager
00184                                 .getMappings();
00185                 Set<String> synsets = mappings.keySet();
00186                 for (Iterator<String> i = synsets.iterator(); i.hasNext();) {
00187                         String synset = i.next();
00188                         List<String> concepts = mappings.get(synset);
00189                         for (Iterator<String> j = concepts.iterator(); j.hasNext();) {
00190                                 OpenCyc20.getInstance().addMapping(synset, j.next());
00191                         }
00192                 }
00193                 importer.getDisambiguator().load(
00194                                 ConfigurationManager.getPathDisambiguator());
00195                 System.out.println("Plan-Importer initialized.");
00196                 return importer;
00197         }
00198 
00199         
00200         
00201         
00212         private String convertInstructionsToOWL(List<Instruction> inst,
00213                         Instruction title, String howtoName) throws InstructionException {
00214 
00215                 try {
00216                         OWLModel owlModel = ProtegeOWL.createJenaOWLModelFromURI("http://ias.cs.tum.edu/kb/ehow_input.owl");
00217                         
00218                         int actionCounter = 1;
00219 
00220                         // Create ComplexTask
00221 
00222                         // Create an easy to read task name composed by the howto name
00223                         String[] howtoTokens = howtoName.split(" ");
00224                         String task = "";
00225                         Set<ProtegeInstance> subTasks = new HashSet<ProtegeInstance>();
00226 
00227                         for (String t : howtoTokens)
00228                                 task += t.substring(0, 1).toUpperCase() + t.substring(1);
00229 
00230                         OWLNamedClass complexTask = owlModel.createOWLNamedSubclass(task,
00231                                         actionStore.get("Action"));
00232 
00233                         // add the howto name as a label so that the plan can be found from Prolog
00234                         complexTask.addLabel(howtoName, null);
00235                         
00236                         for (Iterator<Instruction> i = inst.iterator(); i.hasNext();) {
00237                                 Instruction in = i.next();
00238 
00239                                 String action = in.getAction().getAction().getCycConcepts()
00240                                                 .get(0);
00241 
00242                                 OWLNamedClass owlClass = actionStore.get(action);
00243 
00244                                 if (owlClass != null) {
00245 
00246                                         // Create action subclass
00247                                         OWLNamedClass actionClass = owlModel
00248                                                         .createOWLNamedSubclass(action + actionCounter++, owlClass);
00249 
00250                                         // Set the restriction on the "subEvents" property of the
00251                                         // complex task
00252                                         OWLSomeValuesFrom value = owlModel.createOWLSomeValuesFrom(
00253                                                         propertyStore.get("subEvents"), actionClass);
00254 
00255                                         // Set the restriction on the "objectActedOn" property
00256                                         List<ObjectX> objects = in.getObjects();
00257                                         Set<RDFSClass> objectRestrictions = new HashSet<RDFSClass>();
00258 
00259                                         objectRestrictions.add(owlClass);
00260 
00261                                         for (Iterator<ObjectX> i2 = objects.iterator(); i2.hasNext();) {
00262                                                 
00263                                                 ObjectX o = i2.next();
00264                                                 String objectDesignator = o.getName().get(0).getCycConcepts().get(0);
00265 
00266                                                 String mappedName = objectDesignator;
00267                                                 if (objectStore.get(mappedName) != null) {
00268                                                         OWLSomeValuesFrom objectActedOn = owlModel.createOWLSomeValuesFrom(propertyStore
00269                                                                                                                                                 .get("objectActedOn"), objectStore
00270                                                                                                                                                 .get(mappedName));
00271                                                         objectRestrictions.add(objectActedOn);
00272                                                 } else
00273                                                         System.err.println("Unkown Object Class: " + mappedName);
00274                                         }
00275 
00276                                         if (in.getPrepositions().size() > 0) {
00277                                                 Preposition p = in.getPrepositions().get(0);
00278                                                 if (p.getObjects().size() > 0) {
00279                                                         OWLNamedClass locations = getSpatialPostconditions(
00280                                                                         p.getObjects().get(0), owlModel);
00281                                                         if (locations != null && (p.getPrepositions().contains(new Word( Word.TYPE_PREPOSITION, "from")) ||
00282                                                                         p.getPrepositions().contains(new Word( Word.TYPE_PREPOSITION, "off"))))
00283                                                                 objectRestrictions.add(owlModel
00284                                                                                 .createOWLSomeValuesFrom(propertyStore
00285                                                                                                 .get("fromLocation"), locations));
00286                                                         else if (locations != null)
00287                                                                 objectRestrictions.add(owlModel
00288                                                                                 .createOWLSomeValuesFrom(propertyStore
00289                                                                                                 .get("toLocation"), locations));
00290                                                 }
00291                                         }
00292 
00293                                         // Add the restrictions
00294                                         actionClass.addEquivalentClass(owlModel
00295                                                         .createOWLIntersectionClass(objectRestrictions));
00296                                         // Add the new action class as subEvent to the ComplexTask
00297                                         subTasks.add(value);
00298                                 } else
00299                                         System.err.println("Unknown Action Class: " + action);
00300                         }
00301 
00302                         complexTask.addEquivalentClass(owlModel
00303                                         .createOWLIntersectionClass(subTasks));
00304 
00305                         StringWriter writer = new StringWriter();
00306 
00307                         OWLModelWriter modelWwriter = new OWLModelWriter(owlModel, owlModel
00308                                         .getTripleStoreModel().getActiveTripleStore(), writer);
00309 
00310                         modelWwriter.write();
00311                         writer.close();
00312 
00313                         return writer.toString();
00314 
00315                 } catch (Exception e) {
00316                         e.printStackTrace();
00317                         throw new InstructionException("Could not create OWL Ontology: "
00318                                         + e.getMessage());
00319                 }
00320         }
00321 
00322         
00323 
00329         @SuppressWarnings("unchecked")
00330         private static void readAvailableActions() throws InstructionException {
00331 
00332                 try {
00333                         OWLModel owlModel = ProtegeOWL
00334                                         .createJenaOWLModelFromURI(KITCHEN_ONTOLOGY_URI);
00335 
00336                         OWLNamedClass actionClass = owlModel
00337                                         .getOWLNamedClass(ABSTRACT_ACTION_CLASS);
00338 
00339                         actionStore.put(actionClass.getLocalName(), actionClass);
00340 
00341                         Collection<OWLNamedClass> classes = owlModel
00342                                         .getUserDefinedOWLNamedClasses();
00343 
00344                         for (Iterator<OWLNamedClass> it = classes.iterator(); it.hasNext();) {
00345                                 OWLNamedClass clazz = it.next();
00346                                 if (clazz.getSuperclasses(true).contains(actionClass)) {
00347                                         actionStore.put(clazz.getLocalName(), clazz);
00348                                 }
00349                         }
00350 
00351                 } catch (Exception e) {
00352                         e.printStackTrace();
00353                 }
00354         }
00355 
00361         @SuppressWarnings("unchecked")
00362         private static void readAvailableObjects() throws InstructionException {
00363 
00364                 try {
00365 
00366                         OWLModel owlModel = ProtegeOWL
00367                                         .createJenaOWLModelFromURI(KITCHEN_ONTOLOGY_URI);
00368 
00369                         OWLClass actionClass = owlModel
00370                                         .getOWLNamedClass(ABSTRACT_OBJECT_CLASS);
00371 
00372                         Collection<OWLNamedClass> classes = owlModel
00373                                         .getUserDefinedOWLNamedClasses();
00374 
00375                         for (Iterator<OWLNamedClass> it = classes.iterator(); it.hasNext();) {
00376                                 OWLNamedClass clazz = it.next();
00377                                 if (clazz.getSuperclasses(true).contains(actionClass)) {
00378 
00379                                         objectStore.put(clazz.getLocalName(), clazz);
00380                                 }
00381                         }
00382 
00383 
00384                 } catch (Exception e) {
00385                         e.printStackTrace();
00386                 }
00387         }
00388 
00394         @SuppressWarnings("unchecked")
00395         private static void readAvailableObjectProperties()
00396                         throws InstructionException {
00397 
00398                 try {
00399 
00400                         OWLModel owlModel = ProtegeOWL
00401                                         .createJenaOWLModelFromURI(KITCHEN_ONTOLOGY_URI);
00402 
00403 
00404                         Collection<OWLObjectProperty> properties = owlModel
00405                                         .getUserDefinedOWLObjectProperties();
00406 
00407                         for (Iterator<OWLObjectProperty> it = properties.iterator(); it
00408                                         .hasNext();) {
00409                                 OWLObjectProperty prop = it.next();
00410                                 propertyStore.put(prop.getLocalName(), prop);
00411                         }
00412 
00413                 } catch (OntologyLoadException e) {
00414                         e.printStackTrace();
00415                 }
00416         }
00417 
00418         
00425         public static OWLNamedClass getSpatialPostconditions(ObjectX obj, OWLModel owlModel) {
00426 
00427                 List<Preposition> prep = null;
00428 
00429                 prep = obj.getPrepositions();
00430 
00431                 if (prep.size() > 0) {
00432                         Preposition p = prep.get(0);
00433 
00434                         List<ObjectX> objects = p.getObjects();
00435 
00436                         if (objects.size() > 0) {
00437                                 // go recursively through the objects
00438                                 OWLNamedClass toLocationClass = getSpatialPostconditions(
00439                                                 objects.get(0), owlModel);
00440 
00441                                 if (toLocationClass != null) {
00442 
00443                                         // Create a new "Location" subclass
00444                                         String sub = "Place" + locationCounter++;
00445                                         OWLNamedClass sup = objectStore.get("Place");
00446                                         OWLNamedClass locationClass = owlModel.createOWLNamedSubclass(sub, sup);
00447                                         Set<RDFSClass> locationRestrictions = new HashSet<RDFSClass>();
00448                                         locationRestrictions.add(objectStore.get("Place"));
00449 
00450                                         String objectPredicate = obj.getName().get(0)
00451                                                         .getCycConcepts().get(0);
00452 
00453                                         // map the preposition to one of the "spatiallyRelated"
00454                                         // properties
00455                                         OWLObjectProperty spatialRelation = null;
00456                                         if (objectPredicate.equalsIgnoreCase("FrontSide"))
00457                                                 spatialRelation = propertyStore
00458                                                                 .get("inFrontOf-Generally");
00459                                         
00460                                         else if (objectPredicate.equalsIgnoreCase("TopSide"))
00461                                                 spatialRelation = propertyStore.get("aboveOf");
00462                                         
00463                                         else if (objectPredicate.equalsIgnoreCase("BottomSide"))
00464                                                 spatialRelation = propertyStore.get("belowOf");
00465 
00466                                         else if (objectPredicate.equalsIgnoreCase("LeftSide"))
00467                                                 spatialRelation = propertyStore.get("toTheLeftOf");
00468 
00469                                         else if (objectPredicate.equalsIgnoreCase("RightSide"))
00470                                                 spatialRelation = propertyStore.get("toTheRightOf");
00471 
00472                                         else if (objectPredicate.equalsIgnoreCase("Side"))
00473                                                 spatialRelation = propertyStore.get("toTheSideOf");
00474 
00475                                         else if (objectPredicate
00476                                                         .equalsIgnoreCase("CenterOfGeographicalRegion"))
00477                                                 spatialRelation = propertyStore.get("center");
00478 
00479                                         if (spatialRelation != null)
00480                                                 locationRestrictions.add(owlModel
00481                                                                 .createOWLSomeValuesFrom(spatialRelation,
00482                                                                                 toLocationClass));
00483                                         else
00484                                                 return null;
00485                                         locationClass.addEquivalentClass(owlModel
00486                                                         .createOWLIntersectionClass(locationRestrictions));
00487 
00488                                         return locationClass;
00489                                 } else
00490                                         return null;
00491                         } else
00492                                 return null;
00493                 } else
00494                         return objectStore
00495                                         .get(obj.getName().get(0).getCycConcepts().get(0));
00496 
00497         }
00498 
00499         public static void main(String[] args) {
00500                 OWLExporter owl = new OWLExporter();
00501                 String filePath = owl.convertHowtoToOWLOntology("set a table");
00502                 System.out.println(filePath);
00503         }
00504 }
 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