00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 package org.srs.srs_knowledge.knowledge_engine;
00052
00053 import com.hp.hpl.jena.rdf.model.*;
00054 import com.hp.hpl.jena.vocabulary.*;
00055 import com.hp.hpl.jena.util.FileManager;
00056 import com.hp.hpl.jena.ontology.OntClass;
00057 import com.hp.hpl.jena.ontology.SomeValuesFromRestriction;
00058 import com.hp.hpl.jena.ontology.Restriction;
00059 import com.hp.hpl.jena.query.Query;
00060 import com.hp.hpl.jena.query.QueryFactory;
00061 import com.hp.hpl.jena.query.ResultSetFormatter;
00062 import com.hp.hpl.jena.query.QueryExecutionFactory;
00063 import com.hp.hpl.jena.query.ResultSet;
00064 import com.hp.hpl.jena.query.QueryExecution;
00065 import com.hp.hpl.jena.query.QuerySolution;
00066 import com.hp.hpl.jena.ontology.Individual;
00067 import com.hp.hpl.jena.ontology.OntProperty;
00068 import java.io.*;
00069 import java.util.ArrayList;
00070 import java.util.ConcurrentModificationException;
00071 import java.util.NoSuchElementException;
00072 import java.util.HashSet;
00073 import java.util.Iterator;
00074 import java.util.Collections;
00075 import java.util.Comparator;
00076 import ros.*;
00077 import ros.communication.*;
00078 import ros.pkg.srs_knowledge.srv.QuerySparQL;
00079 import ros.pkg.srs_knowledge.msg.*;
00080
00081 import ros.pkg.srs_msgs.msg.SRSSpatialInfo;
00082 import com.hp.hpl.jena.shared.Lock;
00083 import ros.pkg.srs_knowledge.srv.PlanNextAction;
00084 import ros.pkg.srs_knowledge.srv.TaskRequest;
00085 import ros.pkg.srs_knowledge.srv.GetObjectsOnMap;
00086 import ros.pkg.srs_knowledge.srv.GetWorkspaceOnMap;
00087 import com.hp.hpl.jena.rdf.model.Statement;
00088 import org.srs.srs_knowledge.task.*;
00089 import ros.pkg.geometry_msgs.msg.Pose2D;
00090 import ros.pkg.geometry_msgs.msg.Pose;
00091 import ros.pkg.srs_knowledge.srv.GetObjectsOnMap;
00092 import ros.pkg.srs_knowledge.srv.GetWorkspaceOnMap;
00093
00094 import java.util.Properties;
00095
00096 import java.io.IOException;
00097 import java.io.*;
00098 import java.util.StringTokenizer;
00099 import java.util.ArrayList;
00100 import java.util.HashMap;
00101 import java.util.Iterator;
00102 import java.util.Map;
00103 import java.util.Set;
00104
00105 import org.srs.srs_knowledge.utils.*;
00106
00111 public class OntoQueryUtil
00112 {
00113 public static ArrayList<Individual> getWorkspaceOfObject(String objectClassName, String objectNameSpace, String globalNameSpace, OntologyDB onto) {
00114
00115 ArrayList<String> workspaceList = getWorkspaceNamesOfObject(objectClassName, objectNameSpace, globalNameSpace);
00116 ArrayList<Individual> workspaceIndList = new ArrayList<Individual>();
00117 for(String s : workspaceList) {
00118 workspaceIndList.add(onto.getModel().getIndividual(objectNameSpace + s));
00119 }
00120 return workspaceIndList;
00121 }
00122
00123 public static ArrayList<String> getWorkspaceNamesOfObject(String objectClassName) {
00124 return OntoQueryUtil.getWorkspaceNamesOfObject(objectClassName, OntoQueryUtil.ObjectNameSpace, OntoQueryUtil.GlobalNameSpace);
00125 }
00126
00130 public static ArrayList<String> getWorkspaceNamesOfObject(String objectClassName, String objectNameSpace, String globalNameSpace) {
00131 ArrayList<String> workspaceList = new ArrayList<String>();
00132
00133 String className = globalNameSpace + objectClassName;
00134
00135
00136 Iterator<Individual> instancesOfObject = KnowledgeEngine.ontoDB.getInstancesOfClass(className);
00137
00138
00139 com.hp.hpl.jena.rdf.model.Statement stm;
00140
00141 for( ; instancesOfObject.hasNext(); ) {
00142 stm = KnowledgeEngine.ontoDB.getPropertyOf(globalNameSpace, "spatiallyRelated", instancesOfObject.next());
00143 if (stm != null) {
00144 workspaceList.add(stm.getObject().asResource().getLocalName());
00145 }
00146 }
00147
00148 ArrayList<String> otherWorkspaces = getFurnituresLinkedToObject(objectClassName);
00149
00150 Iterator<Individual> otherInstances;
00151
00152 for(int i = 0; i < otherWorkspaces.size(); i++) {
00153 otherInstances = KnowledgeEngine.ontoDB.getInstancesOfClass(globalNameSpace + otherWorkspaces.get(i));
00154 workspaceList = OntoQueryUtil.addUniqueInstances(workspaceList, otherInstances);
00155
00156 }
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173 return workspaceList;
00174 }
00175
00176 public static Pose getPoseOfObject(String objectURI) {
00177
00178 Pose p = new Pose();
00179
00180 com.hp.hpl.jena.rdf.model.Statement stm;
00181 try{
00182 Individual temp = KnowledgeEngine.ontoDB.getIndividual(objectURI);
00183 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "xCoord", temp);
00184 p.position.x = getFloatOfStatement(stm);
00185 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "yCoord", temp);
00186 p.position.y = getFloatOfStatement(stm);
00187 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "zCoord", temp);
00188 p.position.z = getFloatOfStatement(stm);
00189
00190 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "qu", temp);
00191 p.orientation.w = getFloatOfStatement(stm);
00192 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "qx", temp);
00193 p.orientation.x = getFloatOfStatement(stm);
00194 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "qy", temp);
00195 p.orientation.y = getFloatOfStatement(stm);
00196 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "qz", temp);
00197 p.orientation.z = getFloatOfStatement(stm);
00198 }
00199 catch(NonExistenceEntryException e) {
00200 return null;
00201 }
00202 catch(Exception e) {
00203
00204
00205 p.position.x = -1000;
00206 p.position.y = -1000;
00207 p.position.z = -1000;
00208
00209 p.orientation.w = -1000;
00210 p.orientation.x = -1000;
00211 p.orientation.y = -1000;
00212 p.orientation.z = -1000;
00213 }
00214
00215 return p;
00216 }
00217
00218 public static ArrayList<String> addUniqueInstances(ArrayList<String> original, Iterator<Individual> newList) {
00219 while(newList.hasNext()) {
00220 Individual tempIndividual = newList.next();
00221 String temp = tempIndividual.getLocalName();
00222 if (!original.contains(temp)) {
00223 original.add(temp);
00224 }
00225 }
00226
00227 return original;
00228 }
00229
00230 public static ArrayList<String> tempGetFurnituresLinkedToObject(String objectClassName) {
00231
00232
00233
00234 Map<String, ArrayList<String>> mpWorkspaces = new HashMap<String, ArrayList<String>>();
00235 ArrayList<String> mb = new ArrayList<String>();
00236 mb.add("Table-PieceOfFurniture");
00237 mb.add("Dishwasher");
00238 mpWorkspaces.put("Milkbox", mb);
00239 mpWorkspaces.put("Salt", mb);
00240 mpWorkspaces.put("Bottle", mb);
00241 mpWorkspaces.put("Pringles", mb);
00242 mpWorkspaces.put("Medicine", mb);
00243
00244 ArrayList<String> mbook = new ArrayList<String>();
00245 mbook.add("Table-PieceOfFurniture");
00246 mbook.add("Dishwasher");
00247 mbook.add("BookShelf");
00248 mpWorkspaces.put("Book", mbook);
00249 mpWorkspaces.put("BookCopy", mbook);
00250
00251 return mpWorkspaces.get(objectClassName);
00252 }
00253
00254 private static HashSet<String> getSuperAndSubClassesOf(String classURI, String rootClassURI, boolean includeSelf) {
00255 HashSet<String> ret = new HashSet<String>();
00256 String nsDef =
00257 "PREFIX srs: <http://www.srs-project.eu/ontologies/srs.owl#>\n"
00258 + " PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"
00259 + " PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n";
00260 String queryString = " SELECT ?sup WHERE {\n"
00261 + "<" + classURI + "> rdfs:subClassOf ?sup . \n"
00262 + " ?sup rdfs:subClassOf <" + rootClassURI + "> .}\n";
00263 ArrayList<QuerySolution> qr = KnowledgeEngine.ontoDB.executeQueryRaw(nsDef + queryString);
00264 for(int i = 0; i < qr.size(); i++) {
00265 String tmp = qr.get(i).getResource("sup").getURI();
00266 ret.add(tmp);
00267 }
00268 if(includeSelf) {
00269 ret.add(classURI);
00270 }
00271
00272 return ret;
00273 }
00274
00275 private static ArrayList<String> getSRSObjectCategory(String classURI) {
00276 ArrayList<String> ret = new ArrayList<String>();
00277 String catURI = OntoQueryUtil.GlobalNameSpace + "SRSCatogory";
00278 String nsDef =
00279 "PREFIX srs: <http://www.srs-project.eu/ontologies/srs.owl#>\n"
00280 + " PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"
00281 + " PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n";
00282 String queryString = " SELECT ?cat WHERE {\n"
00283 + "<" + classURI + "> rdfs:subClassOf ?cat . \n"
00284 + " ?cat rdfs:subClassOf <" + catURI + "> .}\n";
00285
00286 ArrayList<QuerySolution> qr = KnowledgeEngine.ontoDB.executeQueryRaw(nsDef + queryString);
00287 for(int i = 0; i < qr.size(); i++) {
00288 String tmp = qr.get(i).getResource("cat").getURI();
00289 ret.add(tmp);
00290 }
00291
00292 return ret;
00293 }
00294
00295
00296
00297 public static ArrayList<String> getFurnituresLinkedToObject(String objectClassName) {
00298 ArrayList<String> ret = new ArrayList<String>();
00299 OntClass objClass = KnowledgeEngine.ontoDB.model.getOntClass(OntoQueryUtil.GlobalNameSpace + objectClassName );
00300
00301
00302
00303
00304
00305 HashSet<String> supClasses = OntoQueryUtil.getSuperAndSubClassesOf(OntoQueryUtil.GlobalNameSpace + objectClassName, OntoQueryUtil.GlobalNameSpace + "GraspableObject", true);
00306 Iterator<String> itSup = supClasses.iterator();
00307 while(itSup.hasNext()) {
00308 objClass = KnowledgeEngine.ontoDB.model.getOntClass(itSup.next());
00309 for (Iterator<OntClass> supers = objClass.listSuperClasses(false); supers.hasNext(); ) {
00310 OntClass sup = supers.next();
00311 if (sup.isRestriction()) {
00312 if (sup.asRestriction().isSomeValuesFromRestriction()) {
00313
00314
00315
00316 OntProperty pro = sup.asRestriction().asSomeValuesFromRestriction().getOnProperty();
00317
00318
00319 if(pro.getURI().equals(OntoQueryUtil.GlobalNameSpace + "storedAtPlace")) {
00320 Resource objRes = sup.asRestriction().asSomeValuesFromRestriction().getSomeValuesFrom();
00321 ret.add(objRes.getLocalName());
00322
00323 }
00324 }
00325
00326 }
00327 }
00328 }
00329
00330 return ret;
00331 }
00332
00333 public static Pose2D parsePose2D(String targetContent) {
00334 Pose2D pos = new Pose2D();
00335
00336 double x = 1;
00337 double y = 1;
00338 double theta = 0;
00339
00340 if (targetContent.charAt(0) == '['
00341 && targetContent.charAt(targetContent.length() - 1) == ']') {
00342 StringTokenizer st = new StringTokenizer(targetContent, " [],");
00343 if (st.countTokens() == 3) {
00344 try {
00345 x = Double.parseDouble(st.nextToken());
00346 y = Double.parseDouble(st.nextToken());
00347 theta = Double.parseDouble(st.nextToken());
00348 System.out.println(x + " " + y + " " + theta);
00349 } catch (Exception e) {
00350 System.out.println(e.getMessage());
00351 return null;
00352 }
00353 }
00354 } else {
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367 String prefix = "PREFIX srs: <http://www.srs-project.eu/ontologies/srs.owl#>\n"
00368 + "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"
00369 + "PREFIX map: <" + OntoQueryUtil.ObjectNameSpace + ">\n";
00370
00371 String queryString = "SELECT ?x ?y ?theta WHERE { "
00372 + "map:" + targetContent
00373 + " srs:xCoordinate ?x . "
00374 + "map:" + targetContent + " srs:yCoordinate ?y . "
00375 + "map:" + targetContent + " srs:orientationTheta ?theta .}";
00376
00377
00378 if (KnowledgeEngine.ontoDB == null) {
00379 System.out.println("Ontology Database is NULL");
00380 return null;
00381 }
00382
00383 try {
00384 ArrayList<QuerySolution> rset = KnowledgeEngine.ontoDB.executeQueryRaw(prefix
00385 + queryString);
00386 if (rset.size() == 0) {
00387 System.out.println("ERROR: No move target found from database");
00388 return null;
00389 } else if (rset.size() == 1) {
00390 System.out
00391 .println("INFO: OK info retrieved from DB... ");
00392 QuerySolution qs = rset.get(0);
00393 x = qs.getLiteral("x").getFloat();
00394 y = qs.getLiteral("y").getFloat();
00395 theta = qs.getLiteral("theta").getFloat();
00396 System.out.println("x is " + x + ". y is " + y
00397 + ". theta is " + theta);
00398 } else {
00399 System.out.println("WARNING: Multiple options... ");
00400 QuerySolution qs = rset.get(0);
00401 x = qs.getLiteral("x").getFloat();
00402 y = qs.getLiteral("y").getFloat();
00403 theta = qs.getLiteral("theta").getFloat();
00404 System.out.println("x is " + x + ". y is " + y
00405 + ". theta is " + theta);
00406 }
00407 } catch (Exception e) {
00408 System.out.println("Exception --> " + e.getMessage());
00409 return null;
00410 }
00411 }
00412
00413 pos.x = x;
00414 pos.y = y;
00415 pos.theta = theta;
00416 return pos;
00417 }
00418
00419 public static ArrayList<Individual> getWorkspaceByName(String objectClassName, String objectNameSpace, String globalNameSpace) {
00420
00421 String className = globalNameSpace + objectClassName;
00422
00423
00424 Iterator<Individual> workspaceIndList = KnowledgeEngine.ontoDB.getInstancesOfClass(className);
00425 ArrayList<Individual> wList = new ArrayList<Individual>();
00426 for( ; workspaceIndList.hasNext(); ) {
00427 wList.add(workspaceIndList.next());
00428 }
00429 return wList;
00430 }
00431
00432 public static boolean testUpdateObjectProperty(String proNSURI, String objectNSURI, String objectName) throws NonExistenceEntryException
00433 {
00434 try {
00435 Individual ind = KnowledgeEngine.ontoDB.getIndividual(objectNSURI + objectName);
00436
00437 Property pro = KnowledgeEngine.ontoDB.getProperty(proNSURI + "xCoord");
00438
00439 com.hp.hpl.jena.rdf.model.Statement stm = ind.getProperty(pro);
00440
00441
00442 Literal x = KnowledgeEngine.ontoDB.model.createTypedLiteral(10.0f);
00443 ind.setPropertyValue(pro, x);
00444
00445
00446
00447
00448
00449
00450 }
00451 catch(NonExistenceEntryException e) {
00452 throw e;
00453 }
00454 return true;
00455 }
00456
00457 public static boolean updatePoseOfObject(Pose pos, String propertyNSURI, String objectURI) throws NonExistenceEntryException {
00458 System.out.println("Update the pose of an object or furniture in the semantic map");
00459 try {
00460 Individual ind = KnowledgeEngine.ontoDB.getIndividual(objectURI);
00461
00462 Property pro = KnowledgeEngine.ontoDB.getProperty(propertyNSURI + "xCoord");
00463
00464
00465 Literal x = KnowledgeEngine.ontoDB.model.createTypedLiteral(new Float(pos.position.x));
00466 ind.setPropertyValue(pro, x);
00467
00468 pro = KnowledgeEngine.ontoDB.getProperty(propertyNSURI + "yCoord");
00469 Literal y = KnowledgeEngine.ontoDB.model.createTypedLiteral(new Float(pos.position.y));
00470 ind.setPropertyValue(pro, y);
00471
00472 pro = KnowledgeEngine.ontoDB.getProperty(propertyNSURI + "zCoord");
00473 Literal z = KnowledgeEngine.ontoDB.model.createTypedLiteral(new Float(pos.position.z));
00474 ind.setPropertyValue(pro, z);
00475
00476 pro = KnowledgeEngine.ontoDB.getProperty(propertyNSURI + "qx");
00477 Literal qx = KnowledgeEngine.ontoDB.model.createTypedLiteral(new Float(pos.orientation.x));
00478 ind.setPropertyValue(pro, qx);
00479
00480 pro = KnowledgeEngine.ontoDB.getProperty(propertyNSURI + "qy");
00481 Literal qy = KnowledgeEngine.ontoDB.model.createTypedLiteral(new Float(pos.orientation.y));
00482 ind.setPropertyValue(pro, qy);
00483
00484 pro = KnowledgeEngine.ontoDB.getProperty(propertyNSURI + "qz");
00485 Literal qz = KnowledgeEngine.ontoDB.model.createTypedLiteral(new Float(pos.orientation.z));
00486 ind.setPropertyValue(pro, qz);
00487
00488 pro = KnowledgeEngine.ontoDB.getProperty(propertyNSURI + "qu");
00489 Literal qw = KnowledgeEngine.ontoDB.model.createTypedLiteral(new Float(pos.orientation.w));
00490 ind.setPropertyValue(pro, qw);
00491
00492
00493 OntoQueryUtil.computeOnSpatialRelation();
00494 }
00495 catch(NonExistenceEntryException e) {
00496 throw e;
00497 }
00498
00499 return true;
00500 }
00501
00502 public static boolean updatePoseOfObject(Pose pos, String propertyNSURI, String objectNSURI, String objectName) throws NonExistenceEntryException {
00503 return updatePoseOfObject(pos, propertyNSURI, objectNSURI + objectName);
00504 }
00505
00506 public static boolean updateDimensionOfObject(float l, float w, float h, String propertyNSURI, String objectNSURI, String objectName) throws NonExistenceEntryException {
00507 System.out.println("Update the dimension of an object or furniture in the semantic map");
00508 try{
00509 Individual ind = KnowledgeEngine.ontoDB.getIndividual(objectNSURI + objectName);
00510
00511 Property pro = KnowledgeEngine.ontoDB.getProperty(propertyNSURI + "lengthOfObject");
00512 com.hp.hpl.jena.rdf.model.Statement stm = ind.getProperty(pro);
00513
00514 Literal ll = KnowledgeEngine.ontoDB.model.createTypedLiteral(new Float(l));
00515 ind.setPropertyValue(pro, ll);
00516
00517 pro = KnowledgeEngine.ontoDB.getProperty(propertyNSURI + "widthOfObject");
00518 stm = ind.getProperty(pro);
00519 Literal lw = KnowledgeEngine.ontoDB.model.createTypedLiteral(new Float(w));
00520 ind.setPropertyValue(pro, lw);
00521
00522 pro = KnowledgeEngine.ontoDB.getProperty(propertyNSURI + "heightOfObject");
00523 stm = ind.getProperty(pro);
00524 Literal lh = KnowledgeEngine.ontoDB.model.createTypedLiteral(new Float(h));
00525 ind.setPropertyValue(pro, lh);
00526 }
00527 catch(NonExistenceEntryException e) {
00528 throw e;
00529 }
00530
00531 return true;
00532 }
00533
00534 public static boolean updateHHIdOfObject(int id, String propertyNSURI, String objectNSURI, String objectName) throws NonExistenceEntryException {
00535 System.out.println("Update the household object ID of an object or furniture in the semantic map");
00536 try{
00537 Individual ind = KnowledgeEngine.ontoDB.getIndividual(objectNSURI + objectName);
00538
00539 Property pro = KnowledgeEngine.ontoDB.getProperty(propertyNSURI + "houseHoldObjectID");
00540 com.hp.hpl.jena.rdf.model.Statement stm = ind.getProperty(pro);
00541 Literal litId = KnowledgeEngine.ontoDB.model.createTypedLiteral(new Integer(id));
00542 ind.setPropertyValue(pro, litId);
00543 }
00544 catch(NonExistenceEntryException e) {
00545 throw e;
00546 }
00547 return true;
00548 }
00549
00550 public static boolean computeOnSpatialRelation() {
00551
00552 GetObjectsOnMap.Response resOBJ = getObjectsOnMap(OntoQueryUtil.MapName, true);
00553 GetWorkspaceOnMap.Response resWS = getWorkspaceOnMap(OntoQueryUtil.MapName, true);
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565 for (int i = 0; i < resOBJ.objectsInfo.size(); i++) {
00566 for (int j = 0; j < resWS.objectsInfo.size(); j++) {
00567 if(SpatialCalculator.ifOnObject(resOBJ.objectsInfo.get(i), resWS.objectsInfo.get(j), -1)) {
00568
00569
00570 try {
00571 Individual ind1 = KnowledgeEngine.ontoDB.getIndividual(OntoQueryUtil.ObjectNameSpace + resOBJ.objects.get(i));
00572 Individual ind2 = KnowledgeEngine.ontoDB.getIndividual(OntoQueryUtil.ObjectNameSpace + resWS.objects.get(j));
00573
00574
00575 OntoQueryUtil.removeAllSubPropertiesOf(OntoQueryUtil.ObjectNameSpace + resOBJ.objects.get(i), OntoQueryUtil.GlobalNameSpace + "spatiallyRelated");
00576
00577 if (OntoQueryUtil.updateOnSpatialRelation(ind1, ind2) ) {
00578
00579 }
00580 else {
00581
00582 }
00583
00584 }
00585 catch (NonExistenceEntryException e) {
00586 System.out.println("Individual not found in ontology: --- " + e.getMessage());
00587 }
00588
00589 }
00590 else {
00591
00592
00593 }
00594 }
00595 }
00596
00597
00598
00599 return true;
00600 }
00601
00602 private static boolean updateOnSpatialRelation(Individual obj1, Individual obj2) {
00603 try {
00604 Property pro = KnowledgeEngine.ontoDB.getProperty(OntoQueryUtil.GlobalNameSpace + "aboveOf");
00605
00606 obj1.setPropertyValue(pro, obj2);
00607 }
00608 catch(NonExistenceEntryException e) {
00609 System.out.println("NonExistenceEntryException --> " + e.getMessage());
00610 return false;
00611 }
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621 return true;
00622 }
00623
00624 public static GetWorkspaceOnMap.Response getWorkspaceOnMap(String map, boolean ifGeometryInfo) {
00625 GetWorkspaceOnMap.Response re = new GetWorkspaceOnMap.Response();
00626 String className = GlobalNameSpace;
00627 String mapNS = ObjectNameSpace;
00628
00629 if(map != null) {
00630 if(KnowledgeEngine.ontoDB.getNamespaceByPrefix(map) != null) {
00631 mapNS = KnowledgeEngine.ontoDB.getNamespaceByPrefix(map);
00632 }
00633 }
00634
00635 className = className + "FurniturePiece";
00636
00637 try{
00638 Iterator<Individual> instances = KnowledgeEngine.ontoDB.getInstancesOfClass(className);
00639 if(instances == null) {
00640 return re;
00641 }
00642 com.hp.hpl.jena.rdf.model.Statement stm;
00643
00644
00645 while (instances.hasNext()) {
00646 Individual temp = (Individual)instances.next();
00647
00648 if(temp.getNameSpace().equals(ObjectNameSpace)) {
00649 re.objects.add(temp.getLocalName());
00650 re.classesOfObjects.add(temp.getRDFType(true).getLocalName());
00651 try{
00652 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "houseHoldObjectID", temp);
00653 re.houseHoldId.add(Integer.toString(getIntOfStatement(stm)));
00654 }
00655 catch(Exception e) {
00656
00657 re.houseHoldId.add("NA");
00658 }
00659
00660 String readableName = temp.getLocalName();
00661 try{
00662 com.hp.hpl.jena.rdf.model.NodeIterator nit = KnowledgeEngine.ontoDB.listPropertiesOf(OntoQueryUtil.GlobalNameSpace, "objectReadableName", temp);
00663
00664 while(nit.hasNext()) {
00665 RDFNode n = nit.next();
00666 Literal l = n.asLiteral();
00667
00668 if(l.getLanguage().equalsIgnoreCase(OntoQueryUtil.Language)) {
00669 readableName = l.getString();
00670 break;
00671 }
00672 }
00673 }
00674 catch(Exception e) {
00675 System.out.println("CAUGHT exception: " + e.toString() + " Cannot retrieve readable name for object. Use Empty instead");
00676
00677 }
00678 finally {
00679 re.readableNames.add(readableName);
00680 }
00681
00682
00683 Map<String, String> pros = new HashMap<String, String>();
00684
00685 try{
00686 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "insideOf", temp);
00687 String r = stm.getObject().asResource().getLocalName();
00688 pros.put("insideOf", r);
00689 }
00690 catch(Exception e) {
00691 System.out.println("CAUGHT exception: " + e.toString());
00692 }
00693 re.json_properties.add(SRSJSONParser.encodeObjectProperties(pros));
00694
00695 if(ifGeometryInfo == true) {
00696 SRSSpatialInfo spatialInfo = new SRSSpatialInfo();
00697
00698 try{
00699 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "xCoord", temp);
00700 spatialInfo.pose.position.x = getFloatOfStatement(stm);
00701 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "yCoord", temp);
00702 spatialInfo.pose.position.y = getFloatOfStatement(stm);
00703 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "zCoord", temp);
00704 spatialInfo.pose.position.z = getFloatOfStatement(stm);
00705
00706 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "widthOfObject", temp);
00707 spatialInfo.w = getFloatOfStatement(stm);
00708 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "heightOfObject", temp);
00709 spatialInfo.h = getFloatOfStatement(stm);
00710 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "lengthOfObject", temp);
00711 spatialInfo.l = getFloatOfStatement(stm);
00712
00713 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "qu", temp);
00714 spatialInfo.pose.orientation.w = getFloatOfStatement(stm);
00715 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "qx", temp);
00716 spatialInfo.pose.orientation.x = getFloatOfStatement(stm);
00717 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "qy", temp);
00718 spatialInfo.pose.orientation.y = getFloatOfStatement(stm);
00719 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "qz", temp);
00720 spatialInfo.pose.orientation.z = getFloatOfStatement(stm);
00721
00722 }
00723 catch(Exception e) {
00724
00725
00726 spatialInfo.pose.position.x = -1000;
00727 spatialInfo.pose.position.y = -1000;
00728 spatialInfo.pose.position.z = -1000;
00729
00730 spatialInfo.w = -1000;
00731 spatialInfo.h = -1000;
00732 spatialInfo.l = -1000;
00733
00734 spatialInfo.pose.orientation.w = -1000;
00735 spatialInfo.pose.orientation.x = -1000;
00736 spatialInfo.pose.orientation.y = -1000;
00737 spatialInfo.pose.orientation.z = -1000;
00738 }
00739
00740 re.objectsInfo.add(spatialInfo);
00741 }
00742 }
00743 }
00744 }
00745 catch(Exception e) {
00746 System.out.println(e.getMessage());
00747 }
00748
00749 return re;
00750 }
00751
00752 private static String objectCategory(Individual ind, boolean asJson) {
00753 String catStr = "";
00754 StringBuffer catTemp = new StringBuffer("");
00755 com.hp.hpl.jena.util.iterator.ExtendedIterator<Resource> it = ind.listRDFTypes(false);
00756 HashSet<String> sset = new HashSet<String>();
00757 String temp = "";
00758 while (it.hasNext()) {
00759 com.hp.hpl.jena.util.iterator.ExtendedIterator<RDFNode> itLabels = KnowledgeEngine.ontoDB.model.getOntResource(it.next()).listLabels("en");
00760 while (itLabels.hasNext()) {
00761 temp = itLabels.next().asLiteral().getString();
00762 catTemp.append(temp);
00763 sset.add(temp);
00764 catTemp.append(",");
00765 }
00766 }
00767
00768 if (asJson)
00769 return SRSJSONParser.encodeObjectCategoryInfo(sset);
00770 else {
00771 catStr = catTemp.toString();
00772 return catStr;
00773 }
00774 }
00775
00776
00777 public static GetObjectsOnMap.Response getObjectsOnMapOfType(String objectTypeURI, boolean ifGeometryInfo) {
00778 GetObjectsOnMap.Response re = new GetObjectsOnMap.Response();
00779
00780 Iterator<Individual> instances = KnowledgeEngine.ontoDB.getInstancesOfClass(objectTypeURI);
00781 if(instances == null) {
00782 return re;
00783 }
00784 com.hp.hpl.jena.rdf.model.Statement stm;
00785 if(instances.hasNext()) {
00786 while (instances.hasNext()) {
00787 Individual temp = (Individual)instances.next();
00788
00789 if(temp.getNameSpace().equals(ObjectNameSpace)) {
00790 re.objects.add(temp.getLocalName());
00791 re.classesOfObjects.add(temp.getRDFType(true).getLocalName());
00792
00793
00794
00795 String catStr = OntoQueryUtil.objectCategory(temp, true);
00796
00797
00798
00799
00800
00801 re.jsonProperties.add(catStr);
00802
00803 try{
00804 stm = KnowledgeEngine.ontoDB.getPropertyOf(GlobalNameSpace, "spatiallyRelated", temp);
00805
00806 re.spatialRelation.add(stm.getPredicate().getLocalName());
00807 re.spatialRelatedObject.add(stm.getObject().asResource().getLocalName());
00808 }
00809 catch(Exception e) {
00810
00811 re.spatialRelation.add("NA");
00812 re.spatialRelatedObject.add("NA");
00813 }
00814 try{
00815 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "houseHoldObjectID", temp);
00816 re.houseHoldId.add(Integer.toString(getIntOfStatement(stm)));
00817 }
00818 catch(Exception e) {
00819
00820 re.houseHoldId.add("NA");
00821 }
00822
00823 String readableName = temp.getLocalName();
00824 try{
00825 com.hp.hpl.jena.rdf.model.NodeIterator nit = KnowledgeEngine.ontoDB.listPropertiesOf(OntoQueryUtil.GlobalNameSpace, "objectReadableName", temp);
00826
00827 while(nit.hasNext()) {
00828 RDFNode n = nit.next();
00829 Literal l = n.asLiteral();
00830
00831 if(l.getLanguage().equalsIgnoreCase(OntoQueryUtil.Language)) {
00832 readableName = l.getString();
00833 break;
00834 }
00835 }
00836 }
00837 catch(Exception e) {
00838 System.out.println("CAUGHT exception: " + e.toString() + " Cannot retrieve readable name for object. Use Empty instead");
00839
00840 }
00841 finally {
00842 re.readableNames.add(readableName);
00843 }
00844
00845 if(ifGeometryInfo == true) {
00846 SRSSpatialInfo spatialInfo = new SRSSpatialInfo();
00847 try{
00848 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "xCoord", temp);
00849 spatialInfo.pose.position.x = getFloatOfStatement(stm);
00850 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "yCoord", temp);
00851 spatialInfo.pose.position.y = getFloatOfStatement(stm);
00852 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "zCoord", temp);
00853 spatialInfo.pose.position.z = getFloatOfStatement(stm);
00854
00855 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "widthOfObject", temp);
00856 spatialInfo.w = getFloatOfStatement(stm);
00857 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "heightOfObject", temp);
00858 spatialInfo.h = getFloatOfStatement(stm);
00859 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "lengthOfObject", temp);
00860 spatialInfo.l = getFloatOfStatement(stm);
00861
00862 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "qu", temp);
00863 spatialInfo.pose.orientation.w = getFloatOfStatement(stm);
00864 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "qx", temp);
00865 spatialInfo.pose.orientation.x = getFloatOfStatement(stm);
00866 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "qy", temp);
00867 spatialInfo.pose.orientation.y = getFloatOfStatement(stm);
00868 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "qz", temp);
00869 spatialInfo.pose.orientation.z = getFloatOfStatement(stm);
00870 }
00871 catch(Exception e) {
00872
00873
00874 spatialInfo.pose.position.x = -1000;
00875 spatialInfo.pose.position.y = -1000;
00876 spatialInfo.pose.position.z = -1000;
00877
00878 spatialInfo.w = -1000;
00879 spatialInfo.h = -1000;
00880 spatialInfo.l = -1000;
00881
00882 spatialInfo.pose.orientation.w = -1000;
00883 spatialInfo.pose.orientation.x = -1000;
00884 spatialInfo.pose.orientation.y = -1000;
00885 spatialInfo.pose.orientation.z = -1000;
00886 }
00887
00888 re.objectsInfo.add(spatialInfo);
00889 }
00890 }
00891 }
00892 }
00893 else {
00894 System.out.println("<EMPTY>");
00895 }
00896
00897 return re;
00898 }
00899
00900 public static GetObjectsOnMap.Response getObjectsOnMap(String map, boolean ifGeometryInfo) {
00901
00902
00903 String className = GlobalNameSpace;
00904 String mapNS = ObjectNameSpace;
00905
00906 if(map != null) {
00907 if(KnowledgeEngine.ontoDB.getNamespaceByPrefix(map) != null) {
00908 mapNS = KnowledgeEngine.ontoDB.getNamespaceByPrefix(map);
00909 }
00910 }
00911
00912
00913 className = className + "GraspableObject";
00914
00915 return OntoQueryUtil.getObjectsOnMapOfType(className, ifGeometryInfo);
00916 }
00917
00918 public static void removeAllSubPropertiesOf(String subjectURI, String propertyURI) {
00919 try{
00920 Individual targetInd = KnowledgeEngine.ontoDB.getIndividual(subjectURI);
00921 OntProperty proExist = KnowledgeEngine.ontoDB.getOntProperty(propertyURI);
00922
00923 com.hp.hpl.jena.util.iterator.ExtendedIterator<OntProperty> subPros = (com.hp.hpl.jena.util.iterator.ExtendedIterator<OntProperty>)proExist.listSubProperties();
00924
00925 while(subPros.hasNext()) {
00926 com.hp.hpl.jena.rdf.model.Statement stm = targetInd.getProperty(subPros.next());
00927 if(stm != null) {
00928 KnowledgeEngine.ontoDB.removeStatement(stm);
00929 targetInd.removeAll(proExist);
00930 }
00931 }
00932 }
00933 catch(Exception e) {
00934
00935 return;
00936 }
00937 }
00938
00939 public static void testRemoveProperty() {
00940 try {
00941 String targetObj = OntoQueryUtil.ObjectNameSpace + "MilkBox0";
00942 System.out.println("TARGET OBJECT IS ::: " + targetObj);
00943 if(!targetObj.trim().equals("")) {
00944 Pose tmpPose = new Pose();
00945 tmpPose.position.x = -1000;
00946 tmpPose.position.y = -1000;
00947 tmpPose.position.z = -1000;
00948 tmpPose.orientation.x = -1000;
00949 tmpPose.orientation.y = -1000;
00950 tmpPose.orientation.z = -1000;
00951 tmpPose.orientation.w = -1000;
00952
00953
00954 try{
00955 OntoQueryUtil.updatePoseOfObject(tmpPose, OntoQueryUtil.GlobalNameSpace, targetObj.trim());
00956 OntoQueryUtil.computeOnSpatialRelation();
00957 }
00958 catch(Exception e) {
00959 System.out.println(e.getMessage() + " " + e.toString());
00960 }
00961 }
00962
00963 Individual targetInd = KnowledgeEngine.ontoDB.getIndividual(targetObj);
00964
00965 OntoQueryUtil.removeAllSubPropertiesOf(targetObj, OntoQueryUtil.GlobalNameSpace + "spatiallyRelated");
00966
00967 String gripper = OntoQueryUtil.ObjectNameSpace + "SRSCOBGripper";
00968
00969
00970 Individual gripInd = KnowledgeEngine.ontoDB.getIndividual(gripper);
00971
00972
00973
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983
00984
00985 Property pro = KnowledgeEngine.ontoDB.getProperty(OntoQueryUtil.GlobalNameSpace + "grippedBy");
00986
00987 targetInd.setPropertyValue(pro, gripInd);
00988
00989 }
00990 catch (Exception e) {
00991
00992 }
00993
00994 }
00995
00996 public static float getFloatOfStatement(Statement stm)
00997 {
00998 float t = -1000;
00999 try {
01000 t = stm.getFloat();
01001 }
01002 catch(Exception e) {
01003 System.out.println(e.getMessage());
01004 }
01005 return t;
01006 }
01007
01008 public static int getIntOfStatement(Statement stm)
01009 {
01010 int t = -1000;
01011 try {
01012 t = stm.getInt();
01013 }
01014 catch(Exception e) {
01015 System.out.println(e.getMessage());
01016 }
01017 return t;
01018 }
01019
01020 public static String getStringOfStatement(Statement stm)
01021 {
01022 String ret = "";
01023 try {
01024 ret = stm.getString();
01025 }
01026 catch(Exception e) {
01027 System.out.println(" + " + e.toString() + " " + e.getMessage());
01028 }
01029 return ret;
01030 }
01031
01032 public static String ObjectNameSpace = "";
01033 public static String GlobalNameSpace = "";
01034 public static String MapName = "";
01035 public static String RobotName = "cob3-3";
01036 public static String Language = "en";
01037 }
01038