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.task;
00052
00053 import java.io.*;
00054 import java.util.StringTokenizer;
00055 import java.util.ArrayList;
00056 import java.util.Iterator;
00057 import ros.pkg.srs_knowledge.msg.*;
00058 import ros.pkg.geometry_msgs.msg.Pose2D;
00059 import ros.pkg.geometry_msgs.msg.Pose;
00060 import org.srs.srs_knowledge.knowledge_engine.*;
00061 import com.hp.hpl.jena.rdf.model.*;
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 org.srs.srs_knowledge.task.*;
00068
00069 import ros.pkg.srs_symbolic_grounding.srv.*;
00070
00071
00072 import ros.*;
00073 import ros.communication.*;
00074
00075 import org.json.simple.JSONArray;
00076 import org.json.simple.JSONObject;
00077 import org.json.simple.JSONValue;
00078 import org.json.simple.parser.ParseException;
00079 import org.json.simple.parser.JSONParser;
00080
00081 import org.srs.srs_knowledge.utils.*;
00082
00083 public class FetchObjectTask extends org.srs.srs_knowledge.task.Task
00084 {
00085
00086 private ConfigInfo.GraspType graspType = ConfigInfo.GraspType.MOVE_AND_GRASP;
00087 public FetchObjectTask(String targetContent, String userPose)
00088 {
00089
00090 this.userPose = userPose;
00091
00092 this.initTask(targetContent, this.userPose);
00093 }
00094 public FetchObjectTask(String targetContent, String userPose, ConfigInfo.GraspType graspMode)
00095 {
00096 this.graspType = graspMode;
00097
00098 this.userPose = userPose;
00099
00100
00101
00102
00103 if (this.initTask(targetContent, this.userPose)) {
00104 if(graspMode == ConfigInfo.GraspType.MOVE_AND_GRASP) {
00105 System.out.println("MOVE_AND_GRASP " + targetContent);
00106 }
00107 else {
00108 System.out.println("JUST_GRASP " + targetContent);
00109 }
00110 }
00111 else {
00112 System.out.println("Task not constructed successfully");
00113 }
00114
00115
00116 }
00117
00118 @Override
00119 protected boolean constructTask() {
00120 return createGetObjectTask();
00121 }
00122
00123 private boolean createGetObjectTask() {
00124
00125
00126
00127
00128
00129
00130
00131 System.out.println("Create New GET OBJECT Task --- ");
00132
00133 try {
00134 workspaces = OntoQueryUtil.getWorkspaceOfObject(this.targetContent, OntoQueryUtil.ObjectNameSpace, OntoQueryUtil.GlobalNameSpace, KnowledgeEngine.ontoDB);
00135 }
00136 catch(Exception e) {
00137 System.out.println(e.getMessage() + "\n" + e.toString());
00138 return false;
00139 }
00140
00141 for(Individual u : workspaces) {
00142 System.out.println(u.getLocalName());
00143 try{
00144
00145 HighLevelActionSequence subSeq = createSubSequenceForSingleWorkspace(u);
00146 allSubSeqs.add(subSeq);
00147
00148 }
00149 catch(RosException e) {
00150 System.out.println("ROSEXCEPTION -- when calling symbolic grounding for scanning positions. \t" + e.getMessage() + "\t" + e.toString());
00151
00152 }
00153 catch(Exception e) {
00154 System.out.println(e.getMessage());
00155 System.out.println(e.toString());
00156 }
00157 }
00158
00159 if(allSubSeqs.size() > 0) {
00160 currentSubAction = 0;
00161 return true;
00162 }
00163 else {
00164 currentSubAction = -1;
00165 return false;
00166 }
00167 }
00168
00169
00170 private HighLevelActionSequence createSubSequenceForSingleWorkspace(Individual workspace) throws RosException, Exception {
00171 HighLevelActionSequence actionList = new HighLevelActionSequence();
00172
00173
00174
00175 ros.pkg.srs_msgs.msg.SRSSpatialInfo spatialInfo = new ros.pkg.srs_msgs.msg.SRSSpatialInfo();
00176 com.hp.hpl.jena.rdf.model.Statement stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "xCoord", workspace);
00177
00178 spatialInfo.pose.position.x = stm.getFloat();
00179 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "yCoord", workspace);
00180 spatialInfo.pose.position.y = stm.getFloat();
00181 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "zCoord", workspace);
00182 spatialInfo.pose.position.z = stm.getFloat();
00183
00184 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "widthOfObject", workspace);
00185 spatialInfo.w = stm.getFloat();
00186 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "heightOfObject", workspace);
00187 spatialInfo.h = stm.getFloat();
00188 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "lengthOfObject", workspace);
00189 spatialInfo.l = stm.getFloat();
00190
00191 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "qu", workspace);
00192 spatialInfo.pose.orientation.w = stm.getFloat();
00193 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "qx", workspace);
00194 spatialInfo.pose.orientation.x = stm.getFloat();
00195 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "qy", workspace);
00196 spatialInfo.pose.orientation.y = stm.getFloat();
00197 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "qz", workspace);
00198 spatialInfo.pose.orientation.z = stm.getFloat();
00199
00200
00201 ArrayList<Pose2D> posList;
00202 try {
00203 posList = calculateScanPositions(spatialInfo);
00204
00205 }
00206 catch(RosException e) {
00207 System.out.println(e.toString());
00208 System.out.println(e.getMessage());
00209 throw e;
00210 }
00211
00212 Iterator<Individual> itInd = KnowledgeEngine.ontoDB.getInstancesOfClass(OntoQueryUtil.GlobalNameSpace + this.targetContent);
00213 int hhid = -1000;
00214 if(itInd.hasNext()) {
00215 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "houseHoldObjectID", (Individual)itInd.next());
00216 hhid = OntoQueryUtil.getIntOfStatement(stm);
00217 }
00218
00219
00220 MoveAndDetectionActionUnit mdAction = new MoveAndDetectionActionUnit(posList, targetContent, hhid, workspace.asResource().getLocalName());
00221
00222
00223 HighLevelActionUnit mgAction = null;
00224
00225 if(this.graspType == ConfigInfo.GraspType.MOVE_AND_GRASP) {
00226 mgAction = new MoveAndGraspActionUnit(null, targetContent, hhid, "side", workspace.asResource().getLocalName());
00227 }
00228 else if(this.graspType == ConfigInfo.GraspType.JUST_GRASP) {
00229 mgAction = new JustGraspActionUnit(targetContent, hhid, "side", workspace.asResource().getLocalName());
00230 }
00231 else {
00232 mgAction = new MoveAndGraspActionUnit(null, targetContent, hhid, "side", workspace.asResource().getLocalName());
00233 }
00234
00235
00236 PutOnTrayActionUnit trayAction = new PutOnTrayActionUnit("side");
00237
00238
00239
00240
00241
00242 Pose2D posUser = OntoQueryUtil.parsePose2D(userPose);
00243 MoveActionUnit mau = new MoveActionUnit(posUser);
00244
00245
00246 FinishActionUnit fau = new FinishActionUnit(true);
00247
00248
00249
00250 actionList.appendHighLevelAction(mdAction);
00251 actionList.appendHighLevelAction(mgAction);
00252 actionList.appendHighLevelAction(trayAction);
00253
00254 actionList.appendHighLevelAction(mau);
00255 actionList.appendHighLevelAction(fau);
00256
00257
00258
00259
00260 actionList.setParameter("workspaceURI", workspace.asResource().getURI());
00261 actionList.setParameter("workspace", workspace.asResource().getLocalName());
00262 actionList.setParameter("workspaceNameSpace", workspace.asResource().getNameSpace());
00263
00264 return actionList;
00265 }
00266
00267 private CUAction handleFailedMessage() {
00268
00269
00270 updateTargetOfFailedAct();
00271
00272 currentSubAction++;
00273
00274
00275
00276 if(currentSubAction >= allSubSeqs.size()) {
00277 return null;
00278 }
00279 HighLevelActionSequence nextHLActSeq = allSubSeqs.get(currentSubAction);
00280
00281
00282 HighLevelActionUnit nextHighActUnit = nextHLActSeq.getCurrentHighLevelActionUnit();
00283 if(nextHighActUnit != null) {
00284 int tempI = nextHighActUnit.getNextCUActionIndex(true);
00285
00286 CUAction ca = nextHighActUnit.getCUActionAt(tempI);
00287
00288 while (ca == null) {
00289
00290 System.out.println("CUACTION IS NULL....... Keep trying " + currentSubAction);
00291 currentSubAction++;
00292 if(currentSubAction >= allSubSeqs.size()) {
00293 return null;
00294 }
00295 nextHLActSeq = allSubSeqs.get(currentSubAction);
00296 nextHighActUnit = nextHLActSeq.getCurrentHighLevelActionUnit();
00297
00298 tempI = nextHighActUnit.getNextCUActionIndex(true);
00299 ca = nextHighActUnit.getCUActionAt(tempI);
00300 }
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340 return ca;
00341
00342 }
00343
00344 return null;
00345 }
00346
00347 private boolean updateDBObjectPose() {
00348
00349 return true;
00350 }
00351
00352 private ArrayList<Pose2D> calculateScanPositions(ros.pkg.srs_msgs.msg.SRSSpatialInfo furnitureInfo) throws RosException {
00353 ArrayList<Pose2D> posList = new ArrayList<Pose2D>();
00354 ServiceClient<SymbolGroundingScanBasePose.Request, SymbolGroundingScanBasePose.Response, SymbolGroundingScanBasePose> sc =
00355 KnowledgeEngine.nodeHandle.serviceClient("symbol_grounding_scan_base_pose" , new SymbolGroundingScanBasePose(), false);
00356
00357 SymbolGroundingScanBasePose.Request rq = new SymbolGroundingScanBasePose.Request();
00358 rq.parent_obj_geometry = furnitureInfo;
00359
00360 SymbolGroundingScanBasePose.Response res = sc.call(rq);
00361 posList = res.scan_base_pose_list;
00362 sc.shutdown();
00363 return posList;
00364 }
00365
00366 private Pose2D calculateGraspPosition(ros.pkg.srs_msgs.msg.SRSSpatialInfo furnitureInfo, Pose targetPose) throws RosException {
00367 Pose2D pos = new Pose2D();
00368
00369 ServiceClient<SymbolGroundingGraspBasePoseExperimental.Request, SymbolGroundingGraspBasePoseExperimental.Response, SymbolGroundingGraspBasePoseExperimental> sc = KnowledgeEngine.nodeHandle.serviceClient("symbol_grounding_grasp_base_pose_experimental" , new SymbolGroundingGraspBasePoseExperimental(), false);
00370
00371 SymbolGroundingGraspBasePoseExperimental.Request rq = new SymbolGroundingGraspBasePoseExperimental.Request();
00372 rq.parent_obj_geometry = furnitureInfo;
00373 rq.target_obj_pose = targetPose;
00374
00375 SymbolGroundingGraspBasePoseExperimental.Response res = sc.call(rq);
00376 boolean obstacleCheck = res.obstacle_check;
00377 double reach = res.reach;
00378 pos = res.grasp_base_pose;
00379
00380 sc.shutdown();
00381 if(obstacleCheck) {
00382 return null;
00383 }
00384 return pos;
00385 }
00386
00387 private ros.pkg.srs_msgs.msg.SRSSpatialInfo getFurnitureGeometryOf(Individual workspace) {
00388 ros.pkg.srs_msgs.msg.SRSSpatialInfo spatialInfo = new ros.pkg.srs_msgs.msg.SRSSpatialInfo();
00389
00390 com.hp.hpl.jena.rdf.model.Statement stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "xCoord", workspace);
00391 spatialInfo.pose.position.x = stm.getFloat();
00392 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "yCoord", workspace);
00393 spatialInfo.pose.position.y = stm.getFloat();
00394 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "zCoord", workspace);
00395 spatialInfo.pose.position.z = stm.getFloat();
00396
00397 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "widthOfObject", workspace);
00398 spatialInfo.w = stm.getFloat();
00399 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "heightOfObject", workspace);
00400 spatialInfo.h = stm.getFloat();
00401 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "lengthOfObject", workspace);
00402 spatialInfo.l = stm.getFloat();
00403
00404 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "qu", workspace);
00405 spatialInfo.pose.orientation.w = stm.getFloat();
00406 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "qx", workspace);
00407 spatialInfo.pose.orientation.x = stm.getFloat();
00408 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "qy", workspace);
00409 spatialInfo.pose.orientation.y = stm.getFloat();
00410 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "qz", workspace);
00411 spatialInfo.pose.orientation.z = stm.getFloat();
00412 return spatialInfo;
00413 }
00414
00415 private boolean initTask(String targetContent, String userPose) {
00416 acts = new ArrayList<ActionTuple>();
00417
00418 setTaskTarget(targetContent);
00419 System.out.println("TASK.JAVA: Created CurrentTask " + "get "
00420 + targetContent);
00421 return constructTask();
00422 }
00423
00424 @Override
00425 public boolean replan(OntologyDB onto, OntoQueryUtil ontoQuery) {
00426 return false;
00427 }
00428 @Override
00429 public boolean isEmpty() {
00430 try {
00431 if(allSubSeqs.size() == 0) {
00432 return true;
00433 }
00434 }
00435 catch(NullPointerException e) {
00436 System.out.println(e.getMessage() + "\n" + e.toString());
00437 return true;
00438 }
00439 return false;
00440 }
00441
00442 private void updateTargetOfFailedAct() {
00443 HighLevelActionSequence currentHLActSeq = allSubSeqs.get(currentSubAction);
00444 HighLevelActionUnit currentActUnit = currentHLActSeq.getCurrentHighLevelActionUnit();
00445 if(currentActUnit.getActionType().equals("MoveAndDetection")) {
00446
00447
00448
00449 String currentWorkSpace = (String)currentHLActSeq.getParameter("workspaceURI");
00450 String mapNameSpace = OntoQueryUtil.ObjectNameSpace;
00451 String prefix = "PREFIX srs: <http://www.srs-project.eu/ontologies/srs.owl#>\n"
00452 + "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"
00453 + "PREFIX mapNamespacePrefix: <" + mapNameSpace + ">\n";
00454 String queryString = "SELECT DISTINCT ?obj WHERE { \n"
00455 + "?obj srs:spatiallyRelated <" + currentWorkSpace + "> . \n"
00456 + " ?obj a srs:" + this.targetContent + " . \n"
00457 + "}";
00458
00459
00460 ArrayList<QuerySolution> rset = KnowledgeEngine.ontoDB.executeQueryRaw(prefix + queryString);
00461 if(rset.size() == 0) {
00462
00463 }
00464 else {
00465
00466
00467
00468 for (QuerySolution qs : rset) {
00469 String temp = qs.get("obj").toString();
00470
00471
00472
00473 OntoQueryUtil.removeAllSubPropertiesOf(temp, OntoQueryUtil.GlobalNameSpace + "spatiallyRelated");
00474 Pose tmpPose = new Pose();
00475 tmpPose.position.x = -1000;
00476 tmpPose.position.y = -1000;
00477 tmpPose.position.z = -1000;
00478 tmpPose.orientation.x = -1000;
00479 tmpPose.orientation.y = -1000;
00480 tmpPose.orientation.z = -1000;
00481 tmpPose.orientation.w = -1000;
00482
00483
00484 try{
00485 OntoQueryUtil.updatePoseOfObject(tmpPose, OntoQueryUtil.GlobalNameSpace, temp.trim());
00486 OntoQueryUtil.computeOnSpatialRelation();
00487 }
00488 catch(Exception e) {
00489 System.out.println(e.getMessage());
00490 }
00491
00492 }
00493 }
00494 }
00495 else if(currentActUnit.getActionType().equals("MoveAndGrasp")) {
00496
00497 }
00498 else if(currentActUnit.getActionType().equals("PutOnTray")) {
00499
00500 }
00501 }
00502
00503
00507
00508 @Override
00509 public CUAction getNextCUActionNew(boolean stateLastAction, String jsonFeedback) {
00510 System.out.println("===> Get Next CUACTION -- from GetObjectTask.java");
00511 CUAction ca = new CUAction();
00512 if(allSubSeqs.size() == 0 ) {
00513 System.out.println("Sequence size is zero");
00514 return null;
00515 }
00516 if(currentSubAction >= 0 && currentSubAction < allSubSeqs.size()) {
00517
00518 System.out.println("Sequence size is " + allSubSeqs.size());
00519 HighLevelActionSequence subActSeq = allSubSeqs.get(currentSubAction);
00520
00521 HighLevelActionUnit highAct = subActSeq.getCurrentHighLevelActionUnit();
00522
00523
00524
00525 if(highAct != null) {
00526
00527
00528 updateDBObjectPose();
00529
00530 int ni = highAct.getNextCUActionIndex(stateLastAction);
00531 switch(ni) {
00532 case HighLevelActionUnit.COMPLETED_SUCCESS:
00533 System.out.println("COMPLETED_SUCCESS");
00534 lastStepActUnit = highAct;
00535
00536 CUAction retact = null;
00537 try {
00538 retact = handleSuccessMessageNew(new ActionFeedback(jsonFeedback));
00539 }
00540 catch(ParseException pe) {
00541 System.out.println(pe.toString());
00542 return null;
00543 }
00544
00545 return retact;
00546 case HighLevelActionUnit.COMPLETED_FAIL:
00547
00548 lastStepActUnit = null;
00549 System.out.println("COMPLETED_FAIL");
00550 return handleFailedMessage();
00551 case HighLevelActionUnit.INVALID_INDEX:
00552
00553 lastStepActUnit = null;
00554 System.out.println("INVALID_INDEX");
00555
00556 return handleFailedMessage();
00557 default:
00558 System.out.println(highAct.getActionType());
00559 if(!highAct.ifParametersSet()) {
00560 System.out.println("Parameters not set");
00561 lastStepActUnit = null;
00562 return handleFailedMessage();
00563 }
00564 ca = highAct.getCUActionAt(ni);
00565
00566
00567 if (ca == null) {
00568 return handleFailedMessage();
00569 }
00570
00571
00572
00573
00574 lastActionType = (String)(SRSJSONParser.decodeJsonActionInfo(ca.generic.jsonActionInfo).get("action"));
00575 return ca;
00576 }
00577 }
00578 else {
00579 return null;
00580 }
00581
00582 }
00583 else if (currentSubAction == -1) {
00584 }
00585
00586 return ca;
00587 }
00588
00592 private void updateTargetOfSucceededActNew(ActionFeedback fb) {
00593 HighLevelActionSequence currentHLActSeq = allSubSeqs.get(currentSubAction);
00594 HighLevelActionUnit currentActUnit = currentHLActSeq.getCurrentHighLevelActionUnit();
00595 if(currentActUnit.getActionType().equals("MoveAndDetection")) {
00596
00597 this.recentDetectedObject = fb.getDetectedObjectPose();
00598 BoundingBoxDim bbDim = InformationRetrieval.retrieveBoundingBoxInfo(OntoQueryUtil.GlobalNameSpace + this.targetContent);
00599 ros.pkg.srs_msgs.msg.SRSSpatialInfo spaObj = new ros.pkg.srs_msgs.msg.SRSSpatialInfo();
00600 spaObj.l = bbDim.l;
00601 spaObj.h = bbDim.h;
00602 spaObj.w = bbDim.w;
00603
00604 spaObj.pose = this.recentDetectedObject;
00605
00606
00607
00608
00609
00610
00611 String neighbour = SpatialCalculator.nearestObject(spaObj.pose, OntoQueryUtil.GlobalNameSpace + this.targetContent);
00612 if(!neighbour.trim().equals("")) {
00613
00614
00615 try{
00616 OntoQueryUtil.updatePoseOfObject(spaObj.pose, OntoQueryUtil.GlobalNameSpace, neighbour.trim());
00617 OntoQueryUtil.computeOnSpatialRelation();
00618 }
00619 catch(Exception e) {
00620 System.out.println(e.getMessage());
00621 }
00622 }
00623
00624
00625
00626
00627
00628 }
00629 else if(currentActUnit.getActionType().equals("MoveAndGrasp")) {
00630
00631
00632
00633 String mapNameSpace = OntoQueryUtil.ObjectNameSpace;
00634 String prefix = "PREFIX srs: <http://www.srs-project.eu/ontologies/srs.owl#>\n"
00635 + "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"
00636 + "PREFIX mapNamespacePrefix: <" + mapNameSpace + ">\n";
00637 String queryString = "SELECT DISTINCT ?gripper WHERE { "
00638 + "<" + mapNameSpace + OntoQueryUtil.RobotName + ">"
00639 + " srs:hasPart ?gripper . "
00640 + " ?gripper a srs:RobotGripper . "
00641 + "}";
00642
00643 try {
00644 String targetObj = SpatialCalculator.nearestObject(this.recentDetectedObject, OntoQueryUtil.GlobalNameSpace + this.targetContent);
00645
00646 if(!targetObj.trim().equals("")) {
00647
00648 Pose tmpPose = new Pose();
00649 tmpPose.position.x = -1000;
00650 tmpPose.position.y = -1000;
00651 tmpPose.position.z = -1000;
00652 tmpPose.orientation.x = -1000;
00653 tmpPose.orientation.y = -1000;
00654 tmpPose.orientation.z = -1000;
00655 tmpPose.orientation.w = -1000;
00656
00657
00658 try{
00659 OntoQueryUtil.updatePoseOfObject(tmpPose, OntoQueryUtil.GlobalNameSpace, targetObj.trim());
00660 OntoQueryUtil.computeOnSpatialRelation();
00661 }
00662 catch(Exception e) {
00663 System.out.println(e.getMessage());
00664 }
00665 }
00666
00667
00668 ArrayList<QuerySolution> rset = KnowledgeEngine.ontoDB.executeQueryRaw(prefix + queryString);
00669 if(rset.size() == 0) {
00670
00671 }
00672 else {
00673
00674 Individual targetInd = KnowledgeEngine.ontoDB.getIndividual(targetObj);
00675
00676 OntoQueryUtil.removeAllSubPropertiesOf(targetObj, OntoQueryUtil.GlobalNameSpace + "spatiallyRelated");
00677 QuerySolution qs = rset.get(0);
00678
00679 String gripper = qs.get("gripper").toString();
00680
00681
00682 Individual gripInd = KnowledgeEngine.ontoDB.getIndividual(gripper);
00683 Property proExist = KnowledgeEngine.ontoDB.getProperty(OntoQueryUtil.GlobalNameSpace + "spatiallyRelated");
00684
00685 Property pro = KnowledgeEngine.ontoDB.getProperty(OntoQueryUtil.GlobalNameSpace + "grippedBy");
00686
00687 targetInd.setPropertyValue(pro, gripInd);
00688 }
00689 }
00690 catch (Exception e) {
00691
00692 }
00693 }
00694 else if(currentActUnit.getActionType().equals("PutOnTray")) {
00695
00696
00697
00698 String mapNameSpace = OntoQueryUtil.ObjectNameSpace;
00699 String prefix = "PREFIX srs: <http://www.srs-project.eu/ontologies/srs.owl#>\n"
00700 + "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"
00701 + "PREFIX mapNamespacePrefix: <" + mapNameSpace + ">\n";
00702 String queryString = "SELECT DISTINCT ?tray WHERE { "
00703 + "<" + mapNameSpace + OntoQueryUtil.RobotName + ">"
00704 + " srs:hasPart ?tray . "
00705 + " ?tray a srs:COBTray . "
00706 + "}";
00707
00708 try {
00709 String targetObj = SpatialCalculator.nearestObject(this.recentDetectedObject, OntoQueryUtil.GlobalNameSpace + this.targetContent);
00710
00711 if(!targetObj.trim().equals("")) {
00712
00713 Pose tmpPose = new Pose();
00714 tmpPose.position.x = -1000;
00715 tmpPose.position.y = -1000;
00716 tmpPose.position.z = -1000;
00717 tmpPose.orientation.x = -1000;
00718 tmpPose.orientation.y = -1000;
00719 tmpPose.orientation.z = -1000;
00720 tmpPose.orientation.w = -1000;
00721
00722
00723 try{
00724 OntoQueryUtil.updatePoseOfObject(tmpPose, OntoQueryUtil.GlobalNameSpace, targetObj.trim());
00725 OntoQueryUtil.computeOnSpatialRelation();
00726 }
00727 catch(Exception e) {
00728
00729 }
00730 }
00731
00732
00733 ArrayList<QuerySolution> rset = KnowledgeEngine.ontoDB.executeQueryRaw(prefix + queryString);
00734 if(rset.size() == 0) {
00735
00736 }
00737 else {
00738
00739 Individual targetInd = KnowledgeEngine.ontoDB.getIndividual(targetObj);
00740
00741 OntoQueryUtil.removeAllSubPropertiesOf(targetObj, OntoQueryUtil.GlobalNameSpace + "spatiallyRelated");
00742 QuerySolution qs = rset.get(0);
00743
00744 String gripper = qs.get("tray").toString();
00745
00746
00747 Individual gripInd = KnowledgeEngine.ontoDB.getIndividual(gripper);
00748 Property proExist = KnowledgeEngine.ontoDB.getProperty(OntoQueryUtil.GlobalNameSpace + "spatiallyRelated");
00749
00750 Property pro = KnowledgeEngine.ontoDB.getProperty(OntoQueryUtil.GlobalNameSpace + "aboveOf");
00751
00752 targetInd.setPropertyValue(pro, gripInd);
00753 }
00754 }
00755 catch (Exception e) {
00756 System.out.println(" ================== " + e.getMessage() + " ---- " + e.toString());
00757 }
00758
00759 }
00760 }
00761
00762 private CUAction handleSuccessMessageNew(ActionFeedback fb) {
00763
00764 updateTargetOfSucceededActNew(fb);
00765
00766 HighLevelActionSequence currentHLActSeq = allSubSeqs.get(currentSubAction);
00767
00768 if(currentHLActSeq.hasNextHighLevelActionUnit()) {
00769 HighLevelActionUnit nextHighActUnit = currentHLActSeq.getNextHighLevelActionUnit();
00770
00771 if(nextHighActUnit.getActionType().equals("MoveAndGrasp") && !nextHighActUnit.ifParametersSet()) {
00772
00773 Pose2D posBase = calculateGraspPosFromFBNew(fb);
00774 if(posBase == null) {
00775 return handleFailedMessage();
00776 }
00777
00778 String jsonMove = SRSJSONParser.encodeMoveAction("move", posBase.x, posBase.y, posBase.theta);
00779 if(!nextHighActUnit.setParameters("move", jsonMove, "")) {
00780
00781 return handleFailedMessage();
00782 }
00783 }
00784
00785 if(nextHighActUnit != null) {
00786 int tempI = nextHighActUnit.getNextCUActionIndex(true);
00787
00788
00789
00790 if(tempI == HighLevelActionUnit.COMPLETED_SUCCESS) {
00791 CUAction ca = new CUAction();
00792 ca.status = 1;
00793 return ca;
00794 }
00795 else if(tempI == HighLevelActionUnit.COMPLETED_FAIL || tempI == HighLevelActionUnit.INVALID_INDEX) {
00796 CUAction ca = new CUAction();
00797 ca.status = -1;
00798 return ca;
00799 }
00800 else {
00801 CUAction ca = nextHighActUnit.getCUActionAt(tempI);
00802
00803 return ca == null ? handleFailedMessage() : ca;
00804
00805
00806 }
00807 }
00808 }
00809 return null;
00810 }
00811
00812 private Pose2D calculateGraspPosFromFBNew(ActionFeedback fb) {
00813
00814
00815 try {
00816 ros.pkg.srs_msgs.msg.SRSSpatialInfo furGeo = getFurnitureGeometryOf(workspaces.get(currentSubAction));
00817
00818
00819
00820 recentDetectedObject = fb.getDetectedObjectPose();
00821 if(recentDetectedObject == null) {
00822 return null;
00823 }
00824
00825 Pose2D pos = calculateGraspPosition(furGeo, recentDetectedObject);
00826
00827 return pos;
00828 }
00829 catch (Exception e) {
00830 System.out.println(e.getMessage() + " ++ " + e.toString());
00831 return null;
00832 }
00833
00834 }
00835
00836 private ArrayList<Individual> workspaces = new ArrayList<Individual>();
00837 private int currentSubAction;
00838 private Pose recentDetectedObject;
00839 private String lastActionType;
00840 private String userPose;
00841 private HighLevelActionUnit lastStepActUnit;
00842 }