$search
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 int inst_counter=0; // counter to create unique instance identifiers 00078 00079 public OWLImportExport() { 00080 //readKnowRobObjectClasses(); 00081 } 00082 00083 00095 public OWLOntology createOWLMapDescription(String namespace, String map_id, ArrayList<ObjectInstance> map) { 00096 return createOWLMapDescription(namespace, map_id, map, null); 00097 } 00098 00099 public OWLOntology createOWLMapDescription(String namespace, String map_id, ArrayList<ObjectInstance> map, ArrayList<String[]> address) { 00100 00101 OWLOntology ontology = null; 00102 HashMap<String, OWLNamedIndividual> idToInst = new HashMap<String, OWLNamedIndividual>(); 00103 00104 try { 00105 00106 // Create ontology manager and data factory 00107 manager = OWLManager.createOWLOntologyManager(); 00108 factory = manager.getOWLDataFactory(); 00109 00110 // Get prefix manager using the base IRI of the JoystickDrive ontology as default namespace 00111 pm = PREFIX_MANAGER; 00112 00113 // Create empty OWL ontology 00114 ontology = manager.createOntology(IRI.create(namespace)); 00115 PREFIX_MANAGER.setPrefix("map:", namespace); 00116 manager.setOntologyFormat(ontology, new RDFXMLOntologyFormat()); 00117 00118 // Import KnowRob ontology 00119 OWLImportsDeclaration oid = factory.getOWLImportsDeclaration(IRI.create(KNOWROB)); 00120 AddImport addImp = new AddImport(ontology,oid); 00121 manager.applyChange(addImp); 00122 00123 00124 // create SemanticMap object in the ontology 00125 idToInst.put(namespace + map_id, createSemMapInst("map:", map_id, ontology)); 00126 00127 00128 // create address part of the map if 'address' variable is not null 00129 if(address!=null) { 00130 00131 OWLNamedIndividual child = null; 00132 00133 00134 OWLNamedIndividual sem_map_inst = idToInst.get(namespace+map_id); 00135 00136 for(String[] component : address) { 00137 00138 child = createAndLinkAddressPart(component, child, sem_map_inst, namespace, ontology); 00139 00140 } 00141 } 00142 00143 00144 // create time point 00145 OWLNamedIndividual time_inst = createTimePointInst(System.currentTimeMillis()/1000, ontology); 00146 00147 00148 // iterate over all objects and create the respective OWL representations 00149 for(ObjectInstance map_obj : map) { 00150 idToInst.put(map_obj.getIRI(), createSemObjectInstanceDescription(map_obj, time_inst, ontology)); 00151 } 00152 00153 00154 // link to parent objects (second loop to avoid problems due to wrong ordering) 00155 for(ObjectInstance map_obj : map) { 00156 00157 OWLNamedIndividual obj_inst = idToInst.get(map_obj.getIRI()); 00158 00159 // link high-level objects to the map 00160 if( !(map_obj.hasType("Handle") || 00161 map_obj.hasType("HingedJoint") || 00162 map_obj.hasType("PrismaticJoint") || 00163 map_obj.hasType("ControlKnob") || 00164 map_obj.hasType("Door"))) { 00165 00166 // top-level object, link to map 00167 OWLObjectProperty describedInMap = factory.getOWLObjectProperty("knowrob:describedInMap", pm); 00168 manager.addAxiom(ontology, factory.getOWLObjectPropertyAssertionAxiom(describedInMap, obj_inst, idToInst.get(namespace + map_id))); 00169 } 00170 00171 // write dimensions 00172 if(map_obj.getDimensions()!=null) { 00173 00174 OWLDataProperty width = factory.getOWLDataProperty("knowrob:depthOfObject", pm); 00175 if(width!=null) 00176 manager.addAxiom(ontology, factory.getOWLDataPropertyAssertionAxiom(width, obj_inst, map_obj.getDimensions().x)); 00177 00178 OWLDataProperty depth = factory.getOWLDataProperty("knowrob:widthOfObject", pm); 00179 if(depth!=null) 00180 manager.addAxiom(ontology, factory.getOWLDataPropertyAssertionAxiom(depth, obj_inst, map_obj.getDimensions().y)); 00181 00182 OWLDataProperty height = factory.getOWLDataProperty("knowrob:heightOfObject", pm); 00183 if(height!=null) 00184 manager.addAxiom(ontology, factory.getOWLDataPropertyAssertionAxiom(height, obj_inst, map_obj.getDimensions().z)); 00185 00186 } 00187 00188 00189 // write all normal properties contained in the properties hashmap 00190 for(String prop : map_obj.getObjProperties().keySet()) { 00191 for(String val : map_obj.getObjPropValues(prop)) { 00192 00193 // skip values that are written later on 00194 if(prop.contains("direction") || 00195 prop.contains("describedInMap")) 00196 continue; 00197 00198 OWLObjectProperty prop_short_name = factory.getOWLObjectProperty("knowrob:" + prop.split("#")[1], pm); 00199 if(prop_short_name!=null && val!=null) { 00200 OWLIndividual value = idToInst.get(val); 00201 00202 if(value==null || obj_inst==null) 00203 continue; 00204 00205 manager.addAxiom(ontology, factory.getOWLObjectPropertyAssertionAxiom(prop_short_name, obj_inst, value)); 00206 } 00207 00208 } 00209 } 00210 00211 // also link proper physical parts of an object 00212 for(ObjectInstance p: map_obj.getPhysicalParts()) { 00213 00214 OWLIndividual part = idToInst.get(p.getIRI()); 00215 OWLObjectProperty properPhysicalParts = factory.getOWLObjectProperty("knowrob:properPhysicalParts", pm); 00216 manager.addAxiom(ontology, factory.getOWLObjectPropertyAssertionAxiom(properPhysicalParts, obj_inst, part)); 00217 } 00218 00219 00220 // link hinges in a special way (set child, parent, hingedTo) 00221 if(map_obj instanceof JointInstance && 00222 (((JointInstance) map_obj).parent)!=null && 00223 (((JointInstance) map_obj).child)!=null) { 00224 00225 OWLIndividual child = idToInst.get(((JointInstance) map_obj).child.getIRI()); 00226 OWLIndividual parent = idToInst.get(((JointInstance) map_obj).parent.getIRI()); 00227 00228 if(parent==null || child ==null) 00229 continue; 00230 00231 // set joint connection between parent and child 00232 if(map_obj.hasType("HingedJoint")) { 00233 OWLObjectProperty hingedTo = factory.getOWLObjectProperty("knowrob:hingedTo", pm); 00234 manager.addAxiom(ontology, factory.getOWLObjectPropertyAssertionAxiom(hingedTo, parent, child)); 00235 00236 } else if(map_obj.hasType("PrismaticJoint")) { 00237 OWLObjectProperty prismaticallyConnectedTo = factory.getOWLObjectProperty("knowrob:prismaticallyConnectedTo", pm); 00238 manager.addAxiom(ontology, factory.getOWLObjectPropertyAssertionAxiom(prismaticallyConnectedTo, parent, child)); 00239 } 00240 00241 // set rigid connection between joint and parent/child resp. 00242 OWLObjectProperty connectedTo = factory.getOWLObjectProperty("knowrob:connectedTo-Rigidly", pm); 00243 manager.addAxiom(ontology, factory.getOWLObjectPropertyAssertionAxiom(connectedTo, obj_inst, child)); 00244 manager.addAxiom(ontology, factory.getOWLObjectPropertyAssertionAxiom(connectedTo, obj_inst, parent)); 00245 } 00246 } 00247 00248 } catch (Exception e) { 00249 ontology = null; 00250 e.printStackTrace(); 00251 } 00252 00253 return ontology; 00254 } 00255 00256 00257 00268 public OWLNamedIndividual createAndLinkAddressPart(String[] component, OWLNamedIndividual child, OWLNamedIndividual sem_map_inst, String namespace, OWLOntology ontology) { 00269 00270 // create individual 00271 OWLClass obj_class = factory.getOWLClass(component[0], pm); 00272 OWLNamedIndividual obj_inst = factory.getOWLNamedIndividual("map:"+edu.tum.cs.ias.knowrob.owl.OWLIndividual.getOWLIndividualOfClass(obj_class.getIRI().toString()).getShortName(), pm); 00273 manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(obj_class, obj_inst)); 00274 00275 00276 // link lowest level with 'describedInMap' 00277 00278 OWLObjectProperty describedInMap = factory.getOWLObjectProperty("knowrob:describedInMap", pm); 00279 OWLObjectProperty properPhysicalParts = factory.getOWLObjectProperty("knowrob:properPhysicalParts", pm); 00280 00281 if(child==null) { 00282 manager.addAxiom(ontology, factory.getOWLObjectPropertyAssertionAxiom(describedInMap, obj_inst, sem_map_inst)); 00283 00284 } else { // link higher levels with 'properPhysicalParts' 00285 manager.addAxiom(ontology, factory.getOWLObjectPropertyAssertionAxiom(properPhysicalParts, obj_inst, child)); 00286 } 00287 00288 // set properties (roomNumber, label, etc) 00289 OWLDataProperty property = factory.getOWLDataProperty(component[1], pm); 00290 if(property!=null) 00291 manager.addAxiom(ontology, factory.getOWLDataPropertyAssertionAxiom(property, obj_inst, component[2])); 00292 00293 return obj_inst; 00294 } 00295 00296 00297 00307 public OWLNamedIndividual createSemObjectInstanceDescription(ObjectInstance map_obj, OWLNamedIndividual timestamp, OWLOntology ontology) { 00308 00309 // create object instance 00310 OWLNamedIndividual obj_inst = createObjectInst(map_obj, ontology); 00311 00312 // create pose matrix instance 00313 OWLNamedIndividual pose_inst = createPoseInst(map_obj.getPoseMatrix(), ontology); 00314 00315 // create perception instance 00316 createPerceptionInst("knowrob:SemanticMapPerception", obj_inst, pose_inst, timestamp, ontology); 00317 00318 return obj_inst; 00319 } 00320 00321 00322 00331 public OWLNamedIndividual createSemMapInst(String namespace, String map_id, OWLOntology ontology) { 00332 00333 OWLClass sem_map_class = factory.getOWLClass("knowrob:SemanticEnvironmentMap", pm); 00334 OWLNamedIndividual sem_map_inst = factory.getOWLNamedIndividual(namespace + map_id, pm); 00335 manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(sem_map_class, sem_map_inst)); 00336 00337 return sem_map_inst; 00338 } 00339 00340 00341 00349 public OWLNamedIndividual createObjectInst(ObjectInstance map_obj, OWLOntology ontology) { 00350 00351 OWLNamedIndividual obj_inst = factory.getOWLNamedIndividual("map:"+map_obj.getShortName(), pm); 00352 00353 for(edu.tum.cs.ias.knowrob.owl.OWLClass t : map_obj.getTypes()) { 00354 OWLClass obj_class = factory.getOWLClass("knowrob:"+t.getShortName(), pm); 00355 manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(obj_class, obj_inst)); 00356 } 00357 00358 00359 // write all normal data properties contained in the properties hashmap 00360 for(String prop : map_obj.getDataProperties().keySet()) { 00361 for(String val : map_obj.getDataPropValues(prop)) { 00362 00363 00364 if( prop.endsWith("depthOfObject") || 00365 prop.endsWith("widthOfObject") || 00366 prop.endsWith("heightOfObject") ) 00367 continue; 00368 00369 00370 OWLDataProperty property = factory.getOWLDataProperty("knowrob:" + prop.split("#")[1], pm); 00371 if(property!=null) 00372 manager.addAxiom(ontology, factory.getOWLDataPropertyAssertionAxiom(property, obj_inst, val)); 00373 00374 } 00375 } 00376 00377 // create hinge-specific properties 00378 if(map_obj instanceof JointInstance) { 00379 00380 // set direction for prismatic joints 00381 if(map_obj.hasType("PrismaticJoint")) { 00382 00383 OWLNamedIndividual dir_vec = createDirVector(((JointInstance) map_obj).direction, ontology); 00384 00385 OWLObjectProperty direction = factory.getOWLObjectProperty("knowrob:direction", pm); 00386 manager.addAxiom(ontology, factory.getOWLObjectPropertyAssertionAxiom(direction, obj_inst, dir_vec)); 00387 } 00388 } 00389 00390 return obj_inst; 00391 } 00392 00393 00394 00402 public OWLNamedIndividual createTimePointInst(long stamp, OWLOntology ontology) { 00403 00404 OWLNamedIndividual time_inst = factory.getOWLNamedIndividual("map:timepoint_"+stamp, pm); 00405 OWLClass time_class = factory.getOWLClass("knowrob:TimePoint", pm); 00406 manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(time_class, time_inst)); 00407 00408 return time_inst; 00409 } 00410 00411 00412 00420 public OWLNamedIndividual createPoseInst(Matrix4d pose, OWLOntology ontology) { 00421 00422 // create pose matrix instance 00423 OWLClass pose_class = factory.getOWLClass("knowrob:RotationMatrix3D", pm); 00424 OWLNamedIndividual pose_inst = factory.getOWLNamedIndividual( 00425 instForClass("knowrob:RotationMatrix3D"), pm); 00426 manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(pose_class, pose_inst)); 00427 00428 // set pose properties 00429 for(int i=0;i<4;i++) { 00430 for(int j=0;j<4;j++) { 00431 OWLDataProperty prop = factory.getOWLDataProperty("knowrob:m"+i+j, pm); 00432 manager.addAxiom(ontology, factory.getOWLDataPropertyAssertionAxiom(prop, pose_inst, pose.getElement(i,j))); 00433 } 00434 } 00435 00436 return pose_inst; 00437 } 00438 00446 public OWLNamedIndividual createDirVector(Vector3d dir_vec, OWLOntology ontology) { 00447 00448 // create pose matrix instance 00449 OWLClass vec_class = factory.getOWLClass("knowrob:Vector", pm); 00450 OWLNamedIndividual vec_inst = factory.getOWLNamedIndividual(instForClass("knowrob:Vector"), pm); 00451 manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(vec_class, vec_inst)); 00452 00453 // set vector dimensions 00454 OWLDataProperty vecX = factory.getOWLDataProperty("knowrob:vectorX", pm); 00455 OWLDataProperty vecY = factory.getOWLDataProperty("knowrob:vectorY", pm); 00456 OWLDataProperty vecZ = factory.getOWLDataProperty("knowrob:vectorZ", pm); 00457 00458 manager.addAxiom(ontology, factory.getOWLDataPropertyAssertionAxiom(vecX, vec_inst, dir_vec.x)); 00459 manager.addAxiom(ontology, factory.getOWLDataPropertyAssertionAxiom(vecY, vec_inst, dir_vec.y)); 00460 manager.addAxiom(ontology, factory.getOWLDataPropertyAssertionAxiom(vecZ, vec_inst, dir_vec.z)); 00461 00462 return vec_inst; 00463 } 00464 00475 public OWLNamedIndividual createPerceptionInst(String type, OWLNamedIndividual obj_inst, OWLNamedIndividual pose_inst, OWLNamedIndividual timestamp, OWLOntology ontology) { 00476 00477 // create perception instance 00478 OWLClass perc_class = factory.getOWLClass(type, pm); 00479 OWLNamedIndividual perc_inst = factory.getOWLNamedIndividual( 00480 instForClass(type), pm); 00481 manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(perc_class, perc_inst)); 00482 00483 // link to the object instance and the pose instance 00484 OWLObjectProperty objectActedOn = factory.getOWLObjectProperty("knowrob:objectActedOn", pm); 00485 OWLObjectProperty eventOccursAt = factory.getOWLObjectProperty("knowrob:eventOccursAt", pm); 00486 00487 manager.addAxiom(ontology, factory.getOWLObjectPropertyAssertionAxiom(objectActedOn, perc_inst, obj_inst)); 00488 manager.addAxiom(ontology, factory.getOWLObjectPropertyAssertionAxiom(eventOccursAt, perc_inst, pose_inst)); 00489 00490 // set time stamp 00491 OWLObjectProperty startTime = factory.getOWLObjectProperty("knowrob:startTime", pm); 00492 manager.addAxiom(ontology, factory.getOWLObjectPropertyAssertionAxiom(startTime, perc_inst, timestamp)); 00493 00494 return perc_inst; 00495 } 00496 00497 00498 00506 static public HashMap<String, ObjectInstance> readObjectInstanceFromOWL(String filename) { 00507 00508 HashMap<String, ObjectInstance> objects = new HashMap<String, ObjectInstance>(); 00509 OWLOntology ont = null; 00510 try { 00511 00512 OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); 00513 OWLDataFactory factory = manager.getOWLDataFactory(); 00514 DefaultPrefixManager pm = OWLImportExport.PREFIX_MANAGER; 00515 OWLClass semanticMapPerception = factory.getOWLClass("knowrob:SemanticMapPerception", pm); 00516 OWLClass rotationmatrix3d = factory.getOWLClass("knowrob:RotationMatrix3D", pm); 00517 OWLClass timepoint = factory.getOWLClass("knowrob:TimePoint", pm); 00518 00519 ont = OWLFileUtils.loadOntologyFromFile(filename); 00520 00521 if(ont!=null) { 00522 00523 // iterate over objects, add to objects hashmap 00524 for(OWLNamedIndividual inst : ont.getIndividualsInSignature()) { 00525 00526 Set<OWLClassExpression> types = inst.getTypes(ont); 00527 00528 if(types.contains(semanticMapPerception)) { 00529 00530 Map<OWLObjectPropertyExpression, Set<OWLIndividual>> perc_props = 00531 inst.getObjectPropertyValues(ont); 00532 00533 OWLObjectProperty objectActedOn = factory.getOWLObjectProperty("knowrob:objectActedOn", pm); 00534 OWLObjectProperty eventOccursAt = factory.getOWLObjectProperty("knowrob:eventOccursAt", pm); 00535 00536 Set<OWLIndividual> objs = perc_props.get(objectActedOn); 00537 Set<OWLIndividual> poses = perc_props.get(eventOccursAt); 00538 00539 00540 // assuming there is only one object and one pose per perception: 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 // add objects that have not been perceived (e.g. street, building) 00671 for(OWLNamedIndividual inst : ont.getIndividualsInSignature()) { 00672 00673 String iri = inst.toStringID(); 00674 00675 if(objects.containsKey(OWLThing.getShortNameOfIRI(iri))) 00676 continue; 00677 00678 // skip those types we have already 00679 if( inst.getTypes(ont).contains(semanticMapPerception) || 00680 inst.getTypes(ont).contains(rotationmatrix3d) || 00681 inst.getTypes(ont).contains(timepoint)) 00682 continue; 00683 00684 00685 // create map object 00686 ObjectInstance cur = ObjectInstance.getObjectInstance(iri); 00687 00688 // get types 00689 for(OWLClassExpression c: inst.getTypes(ont)) { 00690 cur.addType(edu.tum.cs.ias.knowrob.owl.OWLClass.getOWLClass(c.asOWLClass().toStringID())); 00691 } 00692 00693 // iterate over all data properties and read them to the internal representation 00694 Map<OWLDataPropertyExpression, Set<OWLLiteral>> data_props = 00695 inst.getDataPropertyValues(ont); 00696 00697 for(OWLDataPropertyExpression prop : data_props.keySet()) { 00698 for(OWLLiteral d : data_props.get(prop)) { 00699 cur.addDataPropValue(prop.asOWLDataProperty().toStringID(), d.getLiteral()); 00700 } 00701 } 00702 00703 // iterate over all object properties and read them to the internal representation 00704 Map<OWLObjectPropertyExpression, Set<OWLIndividual>> obj_props = 00705 inst.getObjectPropertyValues(ont); 00706 00707 for(OWLObjectPropertyExpression prop: obj_props.keySet()) { 00708 for(OWLIndividual d : obj_props.get(prop)) { 00709 cur.addObjPropValue(prop.asOWLObjectProperty().toStringID(), d.toStringID()); 00710 } 00711 } 00712 objects.put(cur.getShortName(), cur); 00713 } 00714 00715 00716 00717 00718 // link objects to their physical parts 00719 for(OWLNamedIndividual inst : ont.getIndividualsInSignature()) { 00720 00721 Set<OWLClassExpression> types = inst.getTypes(ont); 00722 00723 if(types.contains(semanticMapPerception)) { 00724 00725 Map<OWLObjectPropertyExpression, Set<OWLIndividual>> perc_props = 00726 inst.getObjectPropertyValues(ont); 00727 00728 OWLObjectProperty objectActedOn = factory.getOWLObjectProperty("knowrob:objectActedOn", pm); 00729 00730 Set<OWLIndividual> objs = perc_props.get(objectActedOn); 00731 00732 00733 for(OWLIndividual obj : objs) { 00734 00735 // get proper physical parts 00736 Map<OWLObjectPropertyExpression, Set<OWLIndividual>> obj_props = 00737 obj.getObjectPropertyValues(ont); 00738 00739 OWLObjectProperty parts = factory.getOWLObjectProperty("knowrob:properPhysicalParts", pm); 00740 if(obj_props.containsKey(parts)) { 00741 for(OWLIndividual p : obj_props.get(parts)) { 00742 ObjectInstance part = objects.get(OWLThing.getShortNameOfIRI(p.toStringID())); 00743 if(part!=null) { 00744 objects.get(OWLThing.getShortNameOfIRI(obj.toStringID())).addPhysicalPart(part); 00745 } 00746 } 00747 } 00748 } 00749 } 00750 } 00751 00752 // read parent and child 00753 for(OWLNamedIndividual inst : ont.getIndividualsInSignature()) { 00754 00755 Set<OWLClassExpression> types = inst.getTypes(ont); 00756 00757 if(types.contains(semanticMapPerception)) { 00758 00759 Map<OWLObjectPropertyExpression, Set<OWLIndividual>> perc_props = 00760 inst.getObjectPropertyValues(ont); 00761 00762 OWLObjectProperty objectActedOn = factory.getOWLObjectProperty("knowrob:objectActedOn", pm); 00763 00764 Set<OWLIndividual> objs = perc_props.get(objectActedOn); 00765 00766 00767 00768 for(OWLIndividual obj : objs) { 00769 00770 Map<OWLObjectPropertyExpression, Set<OWLIndividual>> obj_props = 00771 obj.getObjectPropertyValues(ont); 00772 OWLObjectProperty connectedTo = factory.getOWLObjectProperty("knowrob:connectedTo-Rigidly", pm); 00773 00774 if(obj_props.containsKey(connectedTo)) { 00775 00776 ObjectInstance cur = objects.get(OWLThing.getShortNameOfIRI(obj.toStringID())); 00777 00778 for(OWLIndividual rel : obj_props.get(connectedTo)) { 00779 00780 ObjectInstance connectedObj = objects.get(OWLThing.getShortNameOfIRI(rel.toStringID())); 00781 00782 if(connectedObj!=null) { 00783 00784 // connectedObj is a child if it is also contained 00785 // in the physicalParts list of the current object 00786 if(connectedObj.hasPhysicalPart(cur)) { 00787 ((JointInstance) cur).parent = connectedObj; 00788 } else { 00789 ((JointInstance) cur).child = connectedObj; 00790 } 00791 00792 } 00793 00794 } 00795 } 00796 } 00797 00798 } 00799 } 00800 } 00801 00802 } catch (OWLOntologyCreationException e) { 00803 e.printStackTrace(); 00804 } 00805 return objects; 00806 } 00807 00808 00809 00814 protected static void readKnowRobObjectClasses() { 00815 00816 try { 00817 00818 // Create ontology manager, data factory, and prefix manager 00819 OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); 00820 OWLDataFactory factory = manager.getOWLDataFactory(); 00821 DefaultPrefixManager pm = PREFIX_MANAGER; 00822 00823 // Find ros package holding the knowrob ontology 00824 String knowrob_pkg = RosUtilities.rospackFind(KNOWROB_PKG); 00825 String knowrob_owl = knowrob_pkg + "/" + KNOWROB_OWL; 00826 00827 00828 // Load the knowrob ontology 00829 OWLOntology ont = manager.loadOntologyFromOntologyDocument(new File(knowrob_owl)); 00830 00831 // Retrieve only subclasses of SpatialThing-Localized 00832 OWLReasoner reasoner = new StructuralReasoner(ont, new SimpleConfiguration(), BufferingMode.NON_BUFFERING); 00833 OWLClass spatialThing = factory.getOWLClass("knowrob:SpatialThing-Localized", pm); 00834 NodeSet<OWLClass> ns = reasoner.getSubClasses(spatialThing, false); 00835 00836 java.util.Set<Node<OWLClass>> set = ns.getNodes(); 00837 00838 // Iterate over all subclasses and put them into the mapping hashmap 00839 for(Node<OWLClass> n : set) { 00840 OWLClass c = (OWLClass) n.getRepresentativeElement(); 00841 00842 String iri = c.toStringID().replaceAll(KNOWROB, "knowrob:"); 00843 String key = c.toStringID().substring(c.toStringID().lastIndexOf('#') + 1).toLowerCase(); 00844 00845 rosToKnowrob.put(key, iri); 00846 } 00847 // to support backward compatibility (should be removed) 00848 rosToKnowrob.put("hinge", "knowrob:HingedJoint"); 00849 rosToKnowrob.put("knob", "knowrob:ControlKnob"); 00850 rosToKnowrob.put("horizontal_plane", "knowrob:CounterTop"); 00851 00852 } 00853 catch (Exception e) { 00854 e.printStackTrace(); 00855 } 00856 } 00857 00858 00864 protected String instForClass(String cl) { 00865 return cl+(inst_counter++); 00866 } 00867 00868 00872 protected static void printObjectTypes() { 00873 00874 for(Object o : rosToKnowrob.values()) { 00875 System.out.println(((String)o).replaceAll("knowrob:","")); 00876 } 00877 } 00878 00879 00880 }