OWLImportExport.java
Go to the documentation of this file.
00001 package edu.tum.cs.ias.knowrob.owl.utils;
00002 
00003 import java.io.*;
00004 import java.util.ArrayList;
00005 import java.util.HashMap;
00006 import java.util.Map;
00007 import java.util.Set;
00008 
00009 import javax.vecmath.Matrix4d;
00010 import javax.vecmath.Vector3d;
00011 
00012 import org.semanticweb.owlapi.apibinding.OWLManager;
00013 import org.semanticweb.owlapi.io.RDFXMLOntologyFormat;
00014 import org.semanticweb.owlapi.model.*;
00015 import org.semanticweb.owlapi.util.DefaultPrefixManager;
00016 import org.semanticweb.owlapi.reasoner.*;
00017 import org.semanticweb.owlapi.reasoner.structural.*;
00018 
00019 import edu.tum.cs.ias.knowrob.owl.OWLThing;
00020 import edu.tum.cs.ias.knowrob.owl.ObjectInstance;
00021 import edu.tum.cs.ias.knowrob.owl.JointInstance;
00022 import edu.tum.cs.ias.knowrob.utils.ros.RosUtilities;
00023 
00024 
00025 
00037 public class OWLImportExport {
00038 
00039 
00041         // Set IRIs for the ontologies used here
00042         //
00043 
00044         // Base IRI for KnowRob ontology
00045         public final static String KNOWROB = "http://ias.cs.tum.edu/kb/knowrob.owl#";
00046 
00047         // Base IRI for OWL ontology
00048         public final static String OWL = "http://www.w3.org/2002/07/owl#";
00049 
00050         // Base IRI for RDFS
00051         public final static String RDFS = "http://www.w3.org/2000/01/rdf-schema#";
00052 
00053         // Base IRI for semantic map ontology   
00054         public final static String IAS_MAP = "http://ias.cs.tum.edu/kb/ias_semantic_map.owl#";
00055 
00056         // ROS package name for KnowRob
00057         public final static String KNOWROB_PKG = "ias_knowledge_base";
00058 
00059         // OWL file of the KnowRob ontology (relative to KNOWROB_PKG)
00060         public final static String KNOWROB_OWL = "owl/knowrob.owl";
00061 
00062         // Prefix manager
00063         public final static DefaultPrefixManager PREFIX_MANAGER = new DefaultPrefixManager(KNOWROB);
00064         static {
00065                 PREFIX_MANAGER.setPrefix("knowrob:", KNOWROB);
00066                 PREFIX_MANAGER.setPrefix("owl:",    OWL);
00067                 PREFIX_MANAGER.setPrefix("rdfs:", RDFS);
00068         }
00069 
00070         // mapping ROS-KnowRob identifiers
00071         protected static final HashMap<String, String> rosToKnowrob = new HashMap<String, String>();
00072 
00073         OWLDataFactory factory;
00074         OWLOntologyManager manager;
00075         DefaultPrefixManager pm;
00076 
00077         public OWLImportExport() {
00078                 //readKnowRobObjectClasses();
00079         }
00080 
00081 
00093         public OWLOntology createOWLMapDescription(String namespace, String map_id, ArrayList<ObjectInstance> map) {
00094                 return createOWLMapDescription(namespace, map_id, map, null);
00095         }
00096         
00097         public OWLOntology createOWLMapDescription(String namespace, String map_id, ArrayList<ObjectInstance> map, ArrayList<String[]> address) {
00098 
00099                 OWLOntology ontology = null;
00100                 HashMap<String, OWLNamedIndividual> idToInst = new HashMap<String, OWLNamedIndividual>(); 
00101                 
00102                 try {
00103                         
00104                         // Create ontology manager and data factory
00105                         manager = OWLManager.createOWLOntologyManager();
00106                         factory = manager.getOWLDataFactory();
00107 
00108                         // Get prefix manager using the base IRI of the JoystickDrive ontology as default namespace
00109                         pm = PREFIX_MANAGER;
00110 
00111                         // Create empty OWL ontology
00112                         ontology = manager.createOntology(IRI.create(namespace));
00113                         PREFIX_MANAGER.setPrefix("map:", namespace);
00114                         manager.setOntologyFormat(ontology, new RDFXMLOntologyFormat());
00115 
00116                         // Import KnowRob ontology
00117                         OWLImportsDeclaration oid = factory.getOWLImportsDeclaration(IRI.create(KNOWROB));
00118                         AddImport addImp = new AddImport(ontology,oid);
00119                         manager.applyChange(addImp);
00120 
00121                         
00122                         // create SemanticMap object in the ontology
00123                         idToInst.put(namespace + map_id, createSemMapInst("map:", map_id, ontology));
00124 
00125                         
00126                         // create address part of the map if 'address' variable is not null
00127                         if(address!=null) {
00128                                 
00129                                 OWLNamedIndividual child = null;
00130                                 
00131 
00132                                 OWLNamedIndividual sem_map_inst = idToInst.get(namespace+map_id);
00133                                 
00134                                 for(String[] component : address) {
00135 
00136                                         child = createAndLinkAddressPart(component, child, sem_map_inst, namespace, ontology);
00137 
00138                                 }
00139                         }
00140                         
00141 
00142                         // create time point 
00143                         OWLNamedIndividual time_inst = createTimePointInst(System.currentTimeMillis()/1000, ontology);
00144 
00145                         
00146                         // iterate over all objects and create the respective OWL representations
00147                         for(ObjectInstance map_obj : map) {
00148                                 idToInst.put(map_obj.getIRI(), createSemObjectInstanceDescription(map_obj, time_inst, ontology));
00149                         }
00150 
00151                         
00152                         // link to parent objects (second loop to avoid problems due to wrong ordering)
00153                         for(ObjectInstance map_obj : map) {
00154                                 
00155                                 OWLNamedIndividual obj_inst = idToInst.get(map_obj.getIRI());
00156 
00157                                 // link high-level objects to the map
00158                                 if( !(map_obj.hasType("Handle") ||
00159                                                 map_obj.hasType("HingedJoint") ||
00160                                                 map_obj.hasType("PrismaticJoint") ||
00161                                                 map_obj.hasType("ControlKnob") ||
00162                                                 map_obj.hasType("Door"))) {
00163 
00164                                         // top-level object, link to map
00165                                         OWLObjectProperty describedInMap = factory.getOWLObjectProperty("knowrob:describedInMap", pm);
00166                                         manager.addAxiom(ontology, factory.getOWLObjectPropertyAssertionAxiom(describedInMap, obj_inst, idToInst.get(namespace + map_id)));
00167                                 }
00168                                 
00169                                 // write dimensions
00170                                 if(map_obj.getDimensions()!=null) {
00171 
00172                                         OWLDataProperty width = factory.getOWLDataProperty("knowrob:depthOfObject",  pm);
00173                                         if(width!=null)
00174                                                 manager.addAxiom(ontology, factory.getOWLDataPropertyAssertionAxiom(width, obj_inst, map_obj.getDimensions().x));
00175                                         
00176                                         OWLDataProperty depth = factory.getOWLDataProperty("knowrob:widthOfObject",  pm);
00177                                         if(depth!=null)
00178                                                 manager.addAxiom(ontology, factory.getOWLDataPropertyAssertionAxiom(depth, obj_inst, map_obj.getDimensions().y));
00179                                         
00180                                         OWLDataProperty height = factory.getOWLDataProperty("knowrob:heightOfObject",  pm);
00181                                         if(height!=null)
00182                                                 manager.addAxiom(ontology, factory.getOWLDataPropertyAssertionAxiom(height, obj_inst, map_obj.getDimensions().z));
00183                                         
00184                                 }
00185                                 
00186 
00187                                 // write all normal properties contained in the properties hashmap      
00188                                 for(String prop : map_obj.getObjProperties().keySet()) {
00189                                         for(String val : map_obj.getObjPropValues(prop)) {
00190                                                 
00191                                                 // skip values that are written later on
00192                                                 if(prop.contains("direction") ||
00193                                                         prop.contains("describedInMap"))
00194                                                         continue;
00195                                                 
00196                                                 OWLObjectProperty prop_short_name = factory.getOWLObjectProperty("knowrob:" + prop.split("#")[1],  pm);
00197                                                 if(prop_short_name!=null && val!=null) {
00198                                                         OWLIndividual value = idToInst.get(val);
00199                                                         
00200                                                         if(value==null || obj_inst==null)
00201                                                                 continue;
00202                                                         
00203                                                         manager.addAxiom(ontology, factory.getOWLObjectPropertyAssertionAxiom(prop_short_name, obj_inst, value));
00204                                                 }
00205                                                 
00206                                         }
00207                                 }
00208                                 
00209                                 // also link proper physical parts of an object         
00210                                 for(ObjectInstance p: map_obj.getPhysicalParts()) {
00211 
00212                                         OWLIndividual part = idToInst.get(p.getIRI());
00213                                         OWLObjectProperty properPhysicalParts = factory.getOWLObjectProperty("knowrob:properPhysicalParts", pm);
00214                                         manager.addAxiom(ontology, factory.getOWLObjectPropertyAssertionAxiom(properPhysicalParts, obj_inst, part));
00215                                 }
00216 
00217                                 
00218                                 // link hinges in a special way (set child, parent, hingedTo)
00219                                 if(map_obj instanceof JointInstance && 
00220                                                 (((JointInstance) map_obj).parent)!=null && 
00221                                                 (((JointInstance) map_obj).child)!=null) {
00222 
00223                                         OWLIndividual child = idToInst.get(((JointInstance) map_obj).child.getIRI());
00224                                         OWLIndividual parent = idToInst.get(((JointInstance) map_obj).parent.getIRI());
00225                                 
00226                                         if(parent==null || child ==null)
00227                                                 continue;
00228                                         
00229                                         // set joint connection between parent and child
00230                                         if(map_obj.hasType("HingedJoint")) {
00231                                                 OWLObjectProperty hingedTo = factory.getOWLObjectProperty("knowrob:hingedTo", pm);
00232                                                 manager.addAxiom(ontology, factory.getOWLObjectPropertyAssertionAxiom(hingedTo, parent, child));
00233                                                 
00234                                         } else if(map_obj.hasType("PrismaticJoint")) {
00235                                                 OWLObjectProperty prismaticallyConnectedTo = factory.getOWLObjectProperty("knowrob:prismaticallyConnectedTo", pm);
00236                                                 manager.addAxiom(ontology, factory.getOWLObjectPropertyAssertionAxiom(prismaticallyConnectedTo, parent, child));
00237                                         }
00238                                         
00239                                         // set rigid connection between joint and parent/child resp.
00240                                         OWLObjectProperty connectedTo = factory.getOWLObjectProperty("knowrob:connectedTo-Rigidly", pm);
00241                                                 manager.addAxiom(ontology, factory.getOWLObjectPropertyAssertionAxiom(connectedTo, obj_inst, child));
00242                                                 manager.addAxiom(ontology, factory.getOWLObjectPropertyAssertionAxiom(connectedTo, obj_inst, parent));
00243                                 }
00244                         }
00245                         
00246                 } catch (Exception e) {
00247                         ontology = null;
00248                         e.printStackTrace();
00249                 }
00250 
00251                 return ontology;
00252         }
00253 
00254 
00255         
00266         public OWLNamedIndividual createAndLinkAddressPart(String[] component, OWLNamedIndividual child, OWLNamedIndividual sem_map_inst, String namespace, OWLOntology ontology) {
00267                 
00268                 // create individual 
00269                 OWLClass obj_class = factory.getOWLClass(component[0], pm);
00270                 OWLNamedIndividual obj_inst = factory.getOWLNamedIndividual("map:"+edu.tum.cs.ias.knowrob.owl.OWLIndividual.getOWLIndividualOfClass(obj_class.getIRI().toString()).getShortName(), pm);
00271                 manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(obj_class, obj_inst));
00272                 
00273                 
00274                 // link lowest level with 'describedInMap'
00275                 
00276                 OWLObjectProperty describedInMap = factory.getOWLObjectProperty("knowrob:describedInMap", pm);
00277                 OWLObjectProperty properPhysicalParts = factory.getOWLObjectProperty("knowrob:properPhysicalParts", pm);
00278                 
00279                 if(child==null) {
00280                         manager.addAxiom(ontology, factory.getOWLObjectPropertyAssertionAxiom(describedInMap, obj_inst, sem_map_inst));
00281                         
00282                 } else { // link higher levels with 'properPhysicalParts'
00283                         manager.addAxiom(ontology, factory.getOWLObjectPropertyAssertionAxiom(properPhysicalParts, obj_inst, child));
00284                 }
00285                 
00286                 // set properties (roomNumber, label, etc)
00287                 OWLDataProperty property = factory.getOWLDataProperty(component[1],  pm);
00288                 if(property!=null)
00289                         manager.addAxiom(ontology, factory.getOWLDataPropertyAssertionAxiom(property, obj_inst, component[2]));
00290                 
00291                 return obj_inst;
00292         }
00293 
00294 
00295         
00305         public OWLNamedIndividual createSemObjectInstanceDescription(ObjectInstance map_obj, OWLNamedIndividual timestamp, OWLOntology ontology) {
00306 
00307                 // create object instance
00308                 OWLNamedIndividual obj_inst = createObjectInst(map_obj, ontology);
00309 
00310                 // create pose matrix instance
00311                 OWLNamedIndividual pose_inst = createPoseInst(map_obj.getPoseMatrix(), manager, factory, pm, ontology);
00312 
00313                 // create perception instance
00314                 createPerceptionInst("knowrob:SemanticMapPerception", obj_inst, pose_inst, timestamp, ontology);
00315 
00316                 return obj_inst;
00317         }
00318 
00319 
00320         
00329         public OWLNamedIndividual createSemMapInst(String namespace, String map_id, OWLOntology ontology) {
00330 
00331                 OWLClass sem_map_class = factory.getOWLClass("knowrob:SemanticEnvironmentMap", pm);
00332                 OWLNamedIndividual sem_map_inst = factory.getOWLNamedIndividual(namespace + map_id, pm);
00333                 manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(sem_map_class, sem_map_inst));
00334 
00335                 return sem_map_inst;
00336         }
00337 
00338         
00339         
00347         public OWLNamedIndividual createObjectInst(ObjectInstance map_obj, OWLOntology ontology) {
00348 
00349                 OWLNamedIndividual obj_inst = factory.getOWLNamedIndividual("map:"+map_obj.getShortName(), pm);
00350                 
00351                 for(edu.tum.cs.ias.knowrob.owl.OWLClass t : map_obj.getTypes()) {
00352                         OWLClass obj_class = factory.getOWLClass("knowrob:"+t.getShortName(), pm);
00353                         manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(obj_class, obj_inst));     
00354                 }
00355         
00356                 
00357                 // write all normal data properties contained in the properties hashmap 
00358                 for(String prop : map_obj.getDataProperties().keySet()) {
00359                         for(String val : map_obj.getDataPropValues(prop)) {
00360 
00361 
00362                                 if( prop.endsWith("depthOfObject") || 
00363                                         prop.endsWith("widthOfObject") || 
00364                                         prop.endsWith("heightOfObject") )
00365                                         continue;
00366                                 
00367                                 
00368                                 OWLDataProperty property = factory.getOWLDataProperty("knowrob:" + prop.split("#")[1],  pm);
00369                                 if(property!=null)
00370                                         manager.addAxiom(ontology, factory.getOWLDataPropertyAssertionAxiom(property, obj_inst, val));
00371                                 
00372                         }
00373                 }
00374                         
00375                 // create hinge-specific properties
00376                 if(map_obj instanceof JointInstance) {
00377                         
00378                         // set direction for prismatic joints
00379                         if(map_obj.hasType("PrismaticJoint")) {
00380                                 
00381                                 OWLNamedIndividual dir_vec = createDirVector(((JointInstance) map_obj).direction, manager, factory, pm, ontology);
00382                                 
00383                                 OWLObjectProperty direction = factory.getOWLObjectProperty("knowrob:direction", pm);
00384                                 manager.addAxiom(ontology, factory.getOWLObjectPropertyAssertionAxiom(direction, obj_inst, dir_vec));
00385                         }
00386                 }
00387                 
00388                 return obj_inst;
00389         }
00390 
00391         
00392         
00400         public OWLNamedIndividual createTimePointInst(long stamp, OWLOntology ontology) {
00401 
00402                 OWLNamedIndividual time_inst = factory.getOWLNamedIndividual("map:timepoint_"+stamp, pm);
00403                 OWLClass time_class = factory.getOWLClass("knowrob:TimePoint", pm);
00404                 manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(time_class, time_inst));
00405 
00406                 return time_inst;
00407         }
00408 
00409         
00410         
00418         public static OWLNamedIndividual createPoseInst(Matrix4d pose, OWLOntologyManager manager, OWLDataFactory factory, DefaultPrefixManager pm, OWLOntology ontology) { 
00419 
00420                 // create pose matrix instance
00421                 OWLClass pose_class = factory.getOWLClass("knowrob:RotationMatrix3D", pm);
00422                 OWLNamedIndividual pose_inst = factory.getOWLNamedIndividual(OWLThing.getUniqueID("knowrob:RotationMatrix3D"), pm);
00423                 manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(pose_class, pose_inst));
00424 
00425                 
00426                 // set pose properties
00427                 for(int i=0;i<4;i++) {
00428                         for(int j=0;j<4;j++) {
00429                                 OWLDataProperty prop = factory.getOWLDataProperty("knowrob:m"+i+j, pm);
00430                                 manager.addAxiom(ontology, factory.getOWLDataPropertyAssertionAxiom(prop,  pose_inst, pose.getElement(i,j)));
00431                         }
00432                 }
00433 
00434                 return pose_inst;
00435         }       
00436 
00444         public static OWLNamedIndividual createDirVector(Vector3d dir_vec, OWLOntologyManager manager, OWLDataFactory factory, DefaultPrefixManager pm, OWLOntology ontology) { 
00445 
00446 
00447                 // create vector instance
00448                 OWLClass vec_class = factory.getOWLClass("knowrob:Vector", pm);
00449                 OWLNamedIndividual vec_inst = factory.getOWLNamedIndividual(OWLThing.getUniqueID("knowrob:Vector"), pm);
00450                 manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(vec_class, vec_inst));
00451 
00452                 // set vector dimensions
00453                 OWLDataProperty vecX = factory.getOWLDataProperty("knowrob:vectorX", pm);
00454                 OWLDataProperty vecY = factory.getOWLDataProperty("knowrob:vectorY", pm);
00455                 OWLDataProperty vecZ = factory.getOWLDataProperty("knowrob:vectorZ", pm);
00456                 
00457                 manager.addAxiom(ontology, factory.getOWLDataPropertyAssertionAxiom(vecX,  vec_inst, dir_vec.x));
00458                 manager.addAxiom(ontology, factory.getOWLDataPropertyAssertionAxiom(vecY,  vec_inst, dir_vec.y));
00459                 manager.addAxiom(ontology, factory.getOWLDataPropertyAssertionAxiom(vecZ,  vec_inst, dir_vec.z));
00460 
00461                 return vec_inst;
00462         }
00463         
00474         public OWLNamedIndividual createPerceptionInst(String type, OWLNamedIndividual obj_inst, OWLNamedIndividual pose_inst, OWLNamedIndividual timestamp, OWLOntology ontology) {
00475 
00476                 // create perception instance
00477                 OWLClass perc_class = factory.getOWLClass(type, pm);
00478                 OWLNamedIndividual perc_inst = factory.getOWLNamedIndividual(
00479                                 OWLThing.getUniqueID(type), pm);
00480                 manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(perc_class, perc_inst));
00481 
00482                 // link to the object instance and the pose instance
00483                 OWLObjectProperty objectActedOn = factory.getOWLObjectProperty("knowrob:objectActedOn", pm);
00484                 OWLObjectProperty eventOccursAt = factory.getOWLObjectProperty("knowrob:eventOccursAt", pm);
00485 
00486                 manager.addAxiom(ontology, factory.getOWLObjectPropertyAssertionAxiom(objectActedOn, perc_inst, obj_inst));
00487                 manager.addAxiom(ontology, factory.getOWLObjectPropertyAssertionAxiom(eventOccursAt, perc_inst, pose_inst));
00488 
00489                 // set time stamp
00490                 OWLObjectProperty startTime = factory.getOWLObjectProperty("knowrob:startTime", pm);
00491                 manager.addAxiom(ontology, factory.getOWLObjectPropertyAssertionAxiom(startTime,  perc_inst, timestamp));
00492 
00493                 return perc_inst;
00494         }
00495 
00496 
00497         
00505         static public HashMap<String, ObjectInstance> readObjectInstanceFromOWL(String filename) {
00506                 
00507                 HashMap<String, ObjectInstance> objects = new HashMap<String, ObjectInstance>();
00508         OWLOntology ont = null;
00509         try {
00510                 
00511                         OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
00512                         OWLDataFactory factory = manager.getOWLDataFactory();
00513                         DefaultPrefixManager pm = OWLImportExport.PREFIX_MANAGER;
00514                         OWLClass semanticMapPerception = factory.getOWLClass("knowrob:SemanticMapPerception", pm);
00515                         OWLClass rotationmatrix3d = factory.getOWLClass("knowrob:RotationMatrix3D", pm);
00516                         OWLClass timepoint = factory.getOWLClass("knowrob:TimePoint", pm);
00517                         
00518                         ont = OWLFileUtils.loadOntologyFromFile(filename);
00519                         
00520                         if(ont!=null) {
00521 
00522                                 // iterate over objects, add to objects hashmap 
00523                                 for(OWLNamedIndividual inst : ont.getIndividualsInSignature()) {
00524                                                                                 
00525                                         Set<OWLClassExpression> types = inst.getTypes(ont);
00526                                         
00527                                         if(types.contains(semanticMapPerception)) {
00528                                                 
00529                                                 Map<OWLObjectPropertyExpression, Set<OWLIndividual>> perc_props = 
00530                                                         inst.getObjectPropertyValues(ont);
00531                                                 
00532                                                 OWLObjectProperty objectActedOn = factory.getOWLObjectProperty("knowrob:objectActedOn", pm);
00533                                                 OWLObjectProperty eventOccursAt = factory.getOWLObjectProperty("knowrob:eventOccursAt", pm);
00534 
00535                                                 Set<OWLIndividual> objs  = perc_props.get(objectActedOn);
00536                                                 Set<OWLIndividual> poses = perc_props.get(eventOccursAt);
00537 
00538                                                 
00539                                                 // assuming there is only one object and one pose per perception:
00540                                                 if(objs != null && poses != null) {
00541                                                         for(OWLIndividual obj : objs) {
00542                                                                 for(OWLIndividual pose : poses) {
00543 
00544                                                                         String iri = obj.toStringID();
00545 
00546                                                                         // create map object
00547                                                                         ObjectInstance cur = ObjectInstance.getObjectInstance(iri);
00548 
00549                                                                         // get types
00550                                                                         for(OWLClassExpression c: obj.getTypes(ont)) {
00551                                                                                 cur.addType(edu.tum.cs.ias.knowrob.owl.OWLClass.getOWLClass(c.asOWLClass().toStringID()));
00552                                                                         }
00553 
00554                                                                         // special treatment for MapJoints
00555                                                                         if(cur.hasType("HingedJoint") || cur.hasType("PrismaticJoint")) {
00556                                                                                 cur = JointInstance.getMapJoint(iri);
00557                                                                                 for(OWLClassExpression c: obj.getTypes(ont)) {
00558                                                                                         cur.addType(edu.tum.cs.ias.knowrob.owl.OWLClass.getOWLClass(c.asOWLClass().toStringID()));
00559                                                                                 }
00560                                                                         }
00561 
00562 
00563                                                                         // iterate over all data properties and read them to the internal representation
00564                                                                         Map<OWLDataPropertyExpression, Set<OWLLiteral>> data_props = 
00565                                                                                         obj.getDataPropertyValues(ont);
00566 
00567                                                                         for(OWLDataPropertyExpression prop : data_props.keySet()) {
00568                                                                                 for(OWLLiteral d : data_props.get(prop)) {
00569                                                                                         cur.addDataPropValue(prop.asOWLDataProperty().toStringID(), d.getLiteral());
00570                                                                                 }
00571                                                                         }
00572 
00573 
00574                                                                         // iterate over all object properties and read them to the internal representation
00575                                                                         Map<OWLObjectPropertyExpression, Set<OWLIndividual>> obj_props = 
00576                                                                                         obj.getObjectPropertyValues(ont);
00577 
00578                                                                         for(OWLObjectPropertyExpression prop: obj_props.keySet()) {
00579                                                                                 for(OWLIndividual d : obj_props.get(prop)) {
00580                                                                                         cur.addObjPropValue(prop.asOWLObjectProperty().toStringID(), d.toStringID());
00581                                                                                 }
00582                                                                         }
00583 
00584 
00585                                                                         // get dimensions
00586                                                                         OWLDataProperty width  = factory.getOWLDataProperty("knowrob:widthOfObject", pm);
00587                                                                         OWLDataProperty depth  = factory.getOWLDataProperty("knowrob:depthOfObject", pm);
00588                                                                         OWLDataProperty height = factory.getOWLDataProperty("knowrob:heightOfObject", pm);
00589 
00590                                                                         if(data_props.get(depth) != null) {
00591                                                                                 for(OWLLiteral d : data_props.get(depth)) {
00592                                                                                         cur.getDimensions().x = Double.valueOf(d.getLiteral());
00593                                                                                 }
00594                                                                         }
00595 
00596                                                                         if(data_props.get(width) != null) {
00597                                                                                 for(OWLLiteral w : data_props.get(width)) {
00598                                                                                         cur.getDimensions().y = Double.valueOf(w.getLiteral());
00599                                                                                 }
00600                                                                         }
00601 
00602                                                                         if(data_props.get(height) != null) {
00603                                                                                 for(OWLLiteral h : data_props.get(height)) {
00604                                                                                         cur.getDimensions().z = Double.valueOf(h.getLiteral());
00605                                                                                 }
00606                                                                         }
00607 
00608                                                                         // read hinge-specific properties
00609                                                                         if(cur.hasType("HingedJoint") || cur.hasType("PrismaticJoint")) {
00610 
00611                                                                                 if(cur.hasType("PrismaticJoint")) {
00612 
00613                                                                                         OWLObjectProperty direction = factory.getOWLObjectProperty("knowrob:direction", pm);
00614 
00615                                                                                         if(obj_props.containsKey(direction)) {
00616                                                                                                 for(OWLIndividual dir : obj_props.get(direction)) {
00617 
00618                                                                                                         Map<OWLDataPropertyExpression, Set<OWLLiteral>> vec_props = 
00619                                                                                                                         dir.getDataPropertyValues(ont);
00620 
00621                                                                                                         OWLDataProperty vectorx  = factory.getOWLDataProperty("knowrob:vectorX", pm);
00622                                                                                                         OWLDataProperty vectory  = factory.getOWLDataProperty("knowrob:vectorY", pm);
00623                                                                                                         OWLDataProperty vectorz  = factory.getOWLDataProperty("knowrob:vectorZ", pm);
00624 
00625                                                                                                         if(vec_props.containsKey(vectorx)) {
00626                                                                                                                 for(OWLLiteral x : vec_props.get(vectorx)) {
00627                                                                                                                         ((JointInstance) cur).direction.x = Double.valueOf(x.getLiteral());
00628                                                                                                                 }
00629                                                                                                         }
00630                                                                                                         if(vec_props.containsKey(vectory)) {
00631                                                                                                                 for(OWLLiteral y : vec_props.get(vectory)) {
00632                                                                                                                         ((JointInstance) cur).direction.y = Double.valueOf(y.getLiteral());
00633                                                                                                                 }
00634                                                                                                         }
00635                                                                                                         if(vec_props.containsKey(vectorz)) {
00636                                                                                                                 for(OWLLiteral z : vec_props.get(vectorz)) {
00637                                                                                                                         ((JointInstance) cur).direction.z = Double.valueOf(z.getLiteral());
00638                                                                                                                 }
00639                                                                                                         }
00640                                                                                                 }
00641                                                                                         }
00642                                                                                 }
00643                                                                         }
00644 
00645 
00646                                                                         // get pose elements
00647                                                                         Map<OWLDataPropertyExpression, Set<OWLLiteral>> matrix_elems = 
00648                                                                                         pose.getDataPropertyValues(ont);
00649 
00650                                                                         for(int i=0;i<4;i++) {
00651                                                                                 for(int j=0;j<4;j++){
00652                                                                                         OWLDataProperty m_ij = factory.getOWLDataProperty("knowrob:m"+i+j, pm);
00653                                                                                         Set<OWLLiteral> elem = matrix_elems.get(m_ij);
00654 
00655                                                                                         for(OWLLiteral e : elem) {
00656                                                                                                 cur.getPoseMatrix().setElement(i, j, Double.valueOf(e.getLiteral()) );  
00657                                                                                         }
00658 
00659 
00660                                                                                 }
00661                                                                         }
00662                                                                         objects.put(cur.getShortName(), cur);
00663 
00664                                                                 }
00665                                                         }
00666                                                 }
00667                                         }
00668                                 }
00669                                 
00670                                 
00671                                 // add objects that have not been perceived (e.g. street, building)
00672                                 for(OWLNamedIndividual inst : ont.getIndividualsInSignature()) {
00673                                         
00674                                         String iri = inst.toStringID();
00675                                         
00676                                         if(objects.containsKey(OWLThing.getShortNameOfIRI(iri)))
00677                                                 continue;
00678                                         
00679                                         // skip those types we have already  
00680                                         if( inst.getTypes(ont).contains(semanticMapPerception) ||
00681                                                 inst.getTypes(ont).contains(rotationmatrix3d) ||
00682                                                 inst.getTypes(ont).contains(timepoint))
00683                                                 continue;
00684 
00685                                         
00686                                         // create map object
00687                                         ObjectInstance cur = ObjectInstance.getObjectInstance(iri);
00688                                         
00689                                         // get types
00690                                         for(OWLClassExpression c: inst.getTypes(ont)) {
00691                                                 cur.addType(edu.tum.cs.ias.knowrob.owl.OWLClass.getOWLClass(c.asOWLClass().toStringID()));
00692                                         }
00693                                         
00694                                         // iterate over all data properties and read them to the internal representation
00695                                         Map<OWLDataPropertyExpression, Set<OWLLiteral>> data_props = 
00696                                                 inst.getDataPropertyValues(ont);
00697 
00698                                         for(OWLDataPropertyExpression prop : data_props.keySet()) {
00699                                                 for(OWLLiteral d : data_props.get(prop)) {
00700                                                         cur.addDataPropValue(prop.asOWLDataProperty().toStringID(), d.getLiteral());
00701                                                 }
00702                                         }
00703 
00704                                         // iterate over all object properties and read them to the internal representation
00705                                         Map<OWLObjectPropertyExpression, Set<OWLIndividual>> obj_props = 
00706                                                 inst.getObjectPropertyValues(ont);
00707 
00708                                         for(OWLObjectPropertyExpression prop: obj_props.keySet()) {
00709                                                 for(OWLIndividual d : obj_props.get(prop)) {
00710                                                         cur.addObjPropValue(prop.asOWLObjectProperty().toStringID(), d.toStringID());
00711                                                 }
00712                                         }
00713                                         objects.put(cur.getShortName(), cur);
00714                                 }
00715                         
00716                                 
00717                                 
00718                                 
00719                                 // link objects to their physical parts
00720                                 for(OWLNamedIndividual inst : ont.getIndividualsInSignature()) {
00721 
00722                                         Set<OWLClassExpression> types = inst.getTypes(ont);
00723 
00724                                         if(types.contains(semanticMapPerception)) {
00725 
00726                                                 Map<OWLObjectPropertyExpression, Set<OWLIndividual>> perc_props = 
00727                                                         inst.getObjectPropertyValues(ont);
00728 
00729                                                 OWLObjectProperty objectActedOn = factory.getOWLObjectProperty("knowrob:objectActedOn", pm);
00730 
00731                                                 Set<OWLIndividual> objs  = perc_props.get(objectActedOn);
00732 
00733                                                 if(objs != null) {
00734                                                         for(OWLIndividual obj : objs) {
00735 
00736                                                                 // get proper physical parts
00737                                                                 Map<OWLObjectPropertyExpression, Set<OWLIndividual>> obj_props = 
00738                                                                                 obj.getObjectPropertyValues(ont);
00739 
00740                                                                 OWLObjectProperty parts = factory.getOWLObjectProperty("knowrob:properPhysicalParts", pm);
00741                                                                 if(obj_props.containsKey(parts)) {
00742                                                                         for(OWLIndividual p : obj_props.get(parts)) {
00743                                                                                 ObjectInstance part = objects.get(OWLThing.getShortNameOfIRI(p.toStringID()));
00744                                                                                 if(part!=null) {
00745                                                                                         objects.get(OWLThing.getShortNameOfIRI(obj.toStringID())).addPhysicalPart(part);
00746                                                                                 }
00747                                                                         }
00748                                                                 }
00749                                                         }
00750                                                 }
00751                                         }
00752                                 }
00753                                 
00754                                 // read parent and child
00755                                 for(OWLNamedIndividual inst : ont.getIndividualsInSignature()) {
00756 
00757                                         Set<OWLClassExpression> types = inst.getTypes(ont);
00758 
00759                                         if(types.contains(semanticMapPerception)) {
00760 
00761                                                 Map<OWLObjectPropertyExpression, Set<OWLIndividual>> perc_props = 
00762                                                         inst.getObjectPropertyValues(ont);
00763 
00764                                                 OWLObjectProperty objectActedOn = factory.getOWLObjectProperty("knowrob:objectActedOn", pm);
00765 
00766                                                 Set<OWLIndividual> objs  = perc_props.get(objectActedOn);
00767 
00768 
00769                                                 if(objs!=null) {
00770                                                         for(OWLIndividual obj : objs) {
00771 
00772                                                                 Map<OWLObjectPropertyExpression, Set<OWLIndividual>> obj_props = 
00773                                                                                 obj.getObjectPropertyValues(ont);
00774                                                                 OWLObjectProperty connectedTo = factory.getOWLObjectProperty("knowrob:connectedTo-Rigidly", pm);
00775 
00776                                                                 if(obj_props.containsKey(connectedTo)) {
00777 
00778                                                                         ObjectInstance cur = objects.get(OWLThing.getShortNameOfIRI(obj.toStringID()));
00779 
00780                                                                         for(OWLIndividual rel : obj_props.get(connectedTo)) {
00781 
00782                                                                                 ObjectInstance connectedObj = objects.get(OWLThing.getShortNameOfIRI(rel.toStringID()));
00783 
00784                                                                                 if(connectedObj!=null) {
00785 
00786                                                                                         // connectedObj is a child if it is also contained 
00787                                                                                         // in the physicalParts list of the current object
00788                                                                                         if(connectedObj.hasPhysicalPart(cur)) {
00789                                                                                                 ((JointInstance) cur).parent = connectedObj;
00790                                                                                         } else {
00791                                                                                                 ((JointInstance) cur).child = connectedObj;
00792                                                                                         }
00793 
00794                                                                                 }
00795 
00796                                                                         }
00797                                                                 }
00798                                                         }
00799                                                 }
00800 
00801                                         }
00802                                 }
00803                         }
00804                         
00805                 } catch (OWLOntologyCreationException e) {
00806                         e.printStackTrace();
00807                 }
00808                 return objects;
00809         }
00810 
00811         
00812         
00817         protected static void readKnowRobObjectClasses() {
00818 
00819                 try {
00820                         
00821                         // Create ontology manager, data factory, and prefix manager
00822                         OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
00823                         OWLDataFactory factory = manager.getOWLDataFactory();
00824                         DefaultPrefixManager pm = PREFIX_MANAGER;
00825 
00826                         // Find ros package holding the knowrob ontology
00827                         String knowrob_pkg = RosUtilities.rospackFind(KNOWROB_PKG);
00828                         String knowrob_owl = knowrob_pkg + "/" + KNOWROB_OWL;
00829 
00830 
00831                         // Load the knowrob ontology  
00832                         OWLOntology ont = manager.loadOntologyFromOntologyDocument(new File(knowrob_owl));
00833 
00834                         // Retrieve only subclasses of SpatialThing-Localized
00835                         OWLReasoner reasoner = new StructuralReasoner(ont, new SimpleConfiguration(), BufferingMode.NON_BUFFERING);
00836                         OWLClass spatialThing = factory.getOWLClass("knowrob:SpatialThing-Localized", pm);
00837                         NodeSet<OWLClass> ns = reasoner.getSubClasses(spatialThing, false);
00838 
00839                         java.util.Set<Node<OWLClass>>  set = ns.getNodes();       
00840 
00841                         // Iterate over all subclasses and put them into the mapping hashmap
00842                         for(Node<OWLClass> n : set) {
00843                                 OWLClass c = (OWLClass) n.getRepresentativeElement();
00844 
00845                                 String iri = c.toStringID().replaceAll(KNOWROB, "knowrob:");
00846                                 String key = c.toStringID().substring(c.toStringID().lastIndexOf('#') + 1).toLowerCase();
00847 
00848                                 rosToKnowrob.put(key, iri);
00849                         }
00850                         // to support backward compatibility (should be removed)
00851                         rosToKnowrob.put("hinge", "knowrob:HingedJoint");
00852                         rosToKnowrob.put("knob",  "knowrob:ControlKnob");
00853                         rosToKnowrob.put("horizontal_plane", "knowrob:CounterTop");
00854 
00855                 }
00856                 catch (Exception e) {
00857                         e.printStackTrace();
00858                 }
00859         }
00860 
00861 
00865         protected static void printObjectTypes() {
00866 
00867                 for(Object o : rosToKnowrob.values()) {
00868                         System.out.println(((String)o).replaceAll("knowrob:",""));
00869                 }
00870         }
00871 
00872 
00873 }


knowrob_common
Author(s): Moritz Tenorth
autogenerated on Sat Dec 28 2013 17:09:28