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
00057 import com.hp.hpl.jena.query.Query;
00058 import com.hp.hpl.jena.query.QueryFactory;
00059 import com.hp.hpl.jena.query.ResultSetFormatter;
00060 import com.hp.hpl.jena.query.QueryExecutionFactory;
00061 import com.hp.hpl.jena.query.ResultSet;
00062 import com.hp.hpl.jena.query.QueryExecution;
00063 import com.hp.hpl.jena.query.QuerySolution;
00064 import com.hp.hpl.jena.ontology.Individual;
00065 import com.hp.hpl.jena.shared.JenaException;
00066 import ros.*;
00067 import ros.communication.*;
00068
00069
00070 import ros.pkg.srs_knowledge.srv.QuerySparQL;
00071 import ros.pkg.srs_knowledge.msg.*;
00072 import ros.pkg.srs_msgs.msg.SRSSpatialInfo;
00073
00074 import ros.pkg.srs_knowledge.srv.PlanNextAction;
00075 import ros.pkg.srs_knowledge.srv.TaskRequest;
00076 import ros.pkg.srs_knowledge.srv.GetObjectsOnMap;
00077 import ros.pkg.srs_knowledge.srv.GetWorkspaceOnMap;
00078 import ros.pkg.srs_knowledge.srv.GetObjectsOnTray;
00079 import ros.pkg.srs_knowledge.srv.InsertInstance;
00080 import ros.pkg.srs_knowledge.srv.DeleteInstance;
00081 import ros.pkg.srs_knowledge.srv.UpdatePosInfo;
00082 import ros.pkg.srs_knowledge.srv.GetRoomsOnMap;
00083 import ros.pkg.srs_knowledge.srv.GetPredefinedPoses;
00084 import ros.pkg.srs_knowledge.srv.GetWorkspaceForObject;
00085
00086 import com.hp.hpl.jena.rdf.model.Statement;
00087 import org.srs.srs_knowledge.task.*;
00088 import ros.pkg.geometry_msgs.msg.Pose2D;
00089
00090 import java.util.Properties;
00091
00092 import java.io.*;
00093 import java.util.StringTokenizer;
00094 import java.util.ArrayList;
00095 import java.util.Iterator;
00096 import java.util.Map;
00097 import java.util.HashMap;
00098 import org.srs.srs_knowledge.utils.*;
00099
00100 import tfjava.*;
00101
00102 import javax.vecmath.Quat4d;
00103 import javax.vecmath.Vector3d;
00104 import javax.vecmath.Point3d;
00105 import javax.vecmath.Matrix4d;
00106
00107
00108 public class KnowledgeEngine
00109 {
00110 public static Ros ros;
00111 public static OntologyDB ontoDB;
00112
00113 public static NodeHandle nodeHandle;
00114
00115 public KnowledgeEngine()
00116 {
00117 this.defaultContextPath();
00118 }
00119
00120 public KnowledgeEngine(String contextPath)
00121 {
00122 this.setContextPath(contextPath);
00123 }
00124
00125 public void setContextPath(String path)
00126 {
00127 this.confPath = path;
00128 }
00129
00130 public boolean init(String cfgFile)
00131 {
00132 ros = Ros.getInstance();
00133 ros.init(nodeName);
00134 ros.logInfo("INFO: Start RosJava_JNI service");
00135
00136 nodeHandle = ros.createNodeHandle();
00137
00138 try {
00139 initProperties(cfgFile);
00140 }
00141 catch(Exception e) {
00142 System.out.println(e.getMessage());
00143 return false;
00144 }
00145
00146 try{
00147 initQuerySparQL();
00148 initPlanNextAction();
00149 initTaskRequest();
00150 initGetObjectsOnMap();
00151 initGetWorkspaceOnMap();
00152 initGetObjectsOnTray();
00153 initInsertInstance();
00154 initDeleteInstance();
00155 initUpdatePosInfo();
00156 initGetRoomsOnMap();
00157 initGetPredefinedPoses();
00158 initGetWorkspaceForObject();
00159 }
00160 catch(RosException e){
00161 System.out.println(e.getMessage());
00162 return false;
00163 }
00164
00166
00168
00169 OntoQueryUtil.computeOnSpatialRelation();
00170
00171 ros.spin();
00172
00173 return true;
00174 }
00175
00176 private ArrayList<String> parseOntologyFileNames(String names)
00177 {
00178 ArrayList<String> nameList = new ArrayList<String>();
00179
00180 StringTokenizer st = new StringTokenizer(names, " ");
00181
00182 while(st.hasMoreTokens()) {
00183 nameList.add(this.confPath + st.nextToken());
00184 }
00185 for(String v: nameList) {
00186 System.out.println(v);
00187 }
00188 return nameList;
00189 }
00190
00191 private void initProperties(String cfgFile) throws Exception
00192 {
00193 InputStream is = new FileInputStream(this.confPath + cfgFile);
00194 this.config = new Properties();
00195 this.config.load(is);
00196
00197 String ontoDBFile = config.getProperty("ontologyFile", "house.owl");
00198
00199 ArrayList<String> nameList = parseOntologyFileNames(ontoDBFile);
00200
00201 ontoDB = new OntologyDB(nameList);
00202
00203
00204
00205 this.nodeName = config.getProperty("nodename", "knowledge_srs_node");
00206
00207 taskRequestService = config.getProperty("taskRequestService", "task_request");
00208 planNextActionService = config.getProperty("planNextActionService", "plan_next_action");
00209 generateSequenceService = config.getProperty("generateSequenceService", "generate_sequence");
00210 querySparQLService = config.getProperty("querySparQLService", "query_sparql");
00211 getObjectsOnMapService = config.getProperty("getObjectsOnMapService", "get_objects_on_map");
00212 getWorkSpaceOnMapService = config.getProperty("getWorkSpaceOnMapService", "get_workspace_on_map");
00213 getObjectsOnTrayService = config.getProperty("getObjectsOnTrayService", "get_objects_on_tray");
00214 insertInstanceService = config.getProperty("insertInstanceService", "insert_instance");
00215 deleteInstanceService = config.getProperty("deleteInstanceService", "delete_instance");
00216 updatePosInfoService = config.getProperty("updatePosInfoService", "update_pos_info");
00217 getRoomsOnMapService = config.getProperty("getRoomsOnMapService", "get_rooms_on_map");
00218 getPredefinedPosesService = config.getProperty("getPredefinedPosesService", "get_predefined_poses");
00219 getWorkspaceForObjectService = config.getProperty("getWorkspaceForObjectService", "get_workspace_for_object");
00220
00221 graspActionMode = this.readGraspModeParam("/srs/grasping_type");
00222
00223
00224 String langEnv = this.readLanguageEnvParam("/srs/language_short", "en");
00225
00226
00227 mapName = config.getProperty("map_name", "ipa-kitchen-map");
00228
00229 String robotName = config.getProperty("robot_name", System.getenv("ROBOT"));
00230
00231
00232
00233
00234
00235
00236
00237
00238 mapNamespace = config.getProperty("env_namespace", "http://www.srs-project.eu/ontologies/ipa-kitchen-map.owl#");
00239 globalNamespace = config .getProperty("ont_namespace", "http://www.srs-project.eu/ontologies/srs.owl#");
00240
00241
00242 OntoQueryUtil.ObjectNameSpace = mapNamespace;
00243 OntoQueryUtil.GlobalNameSpace = globalNamespace;
00244
00245 OntoQueryUtil.MapName = mapName;
00246 OntoQueryUtil.RobotName = robotName;
00247 OntoQueryUtil.Language = langEnv;
00248 }
00249
00250 private String readGraspModeParam(String paramServer) {
00251 String graspMode = "Simple";
00252 try{
00253 graspMode = nodeHandle.getStringParam(paramServer);
00254 System.out.println("Read Parameter --- > " + graspMode);
00255 }
00256 catch(RosException e) {
00257 System.out.println("Caught RosException -- > " + e.toString());
00258 }
00259 return graspMode;
00260 }
00261
00262 private String readLanguageEnvParam(String paramServer, String defaultLang) {
00263 String lang = defaultLang;
00264 try{
00265 lang = nodeHandle.getStringParam(paramServer);
00266 System.out.println("Read Parameter --- > " + lang);
00267 }
00268 catch(RosException e) {
00269 System.out.println("Caught RosException -- > " + e.toString() + " \nUse English as default");
00270 }
00271 return lang;
00272 }
00273
00274 public void testOnto(String className)
00275 {
00276 try{
00277 Iterator<Individual> instances = ontoDB.getInstancesOfClass(className);
00278
00279 if(instances.hasNext()) {
00280 while (instances.hasNext()) {
00281 Individual temp = (Individual)instances.next();
00282 System.out.println( temp.getNameSpace() + " " + temp.getLocalName());
00283 }
00284 }
00285 else
00286 System.out.println("<EMPTY>");
00287
00288 System.out.println();
00289
00290 }
00291 catch(Exception e) {
00292 System.out.println(e.getMessage());
00293 }
00294
00295 }
00296
00297 private QuerySparQL.Response handleQuerySparQL(QuerySparQL.Request req)
00298 {
00299 QuerySparQL.Response re = new QuerySparQL.Response();
00300 String queryString = req.query;
00301
00302
00303 re.result = ontoDB.executeQuery(queryString);
00304 return re;
00305 }
00306
00307 private void initQuerySparQL() throws RosException
00308 {
00309 ServiceServer.Callback<QuerySparQL.Request, QuerySparQL.Response> scb = new ServiceServer.Callback<QuerySparQL.Request, QuerySparQL.Response>() {
00310 public QuerySparQL.Response call(QuerySparQL.Request request) {
00311 return handleQuerySparQL(request);
00312 }
00313 };
00314
00315 ServiceServer<QuerySparQL.Request, QuerySparQL.Response, QuerySparQL> srv = nodeHandle.advertiseService( querySparQLService , new QuerySparQL(), scb);
00316 }
00317
00318 private PlanNextAction.Response handlePlanNextAction( PlanNextAction.Request request) throws NullPointerException {
00319 PlanNextAction.Response res = new PlanNextAction.Response();
00320 CUAction ca = new CUAction();
00321
00322 if(currentTask == null) {
00323 System.out.println("Current Task is NULL. Send task request first");
00324 res.nextAction = new CUAction();
00325 return res;
00326 }
00327
00328 if(request.resultLastAction == 0) {
00329 String jsonFeedback = request.jsonFeedback;
00330 ca = currentTask.getNextCUActionNew(true, jsonFeedback);
00331 }
00332 else if (request.resultLastAction == 2) {
00333 ros.logInfo("INFO: possible hardware failure with robot. cancel current task");
00334 ros.logInfo("INFO: Task termintated");
00335
00336 currentTask = null;
00337
00338
00339
00340 res.nextAction = new CUAction();
00341 return res;
00342 }
00343 else{
00344 String jsonFeedback = request.jsonFeedback;
00345 ca = currentTask.getNextCUActionNew(false, jsonFeedback);
00346 }
00347
00348 if(ca == null) {
00349 currentTask = null;
00350 System.out.println("No further action can be planned. Terminate the task. ");
00351
00352 res.nextAction = new CUAction();
00353 return res;
00354 }
00355 if(ca.status == 1) {
00356 currentTask = null;
00357 System.out.println("Reached the end of the task. No further action to be executed. ");
00358 res.nextAction = ca;
00359 return res;
00360 }
00361 else if( ca.status == -1) {
00362 currentTask = null;
00363
00364 System.out.println("Reached the end of the task. No further action to be executed. ");
00365 res.nextAction = ca;
00366 return res;
00367 }
00368
00369 res.nextAction = ca;
00370 return res;
00371 }
00372
00373 private void initPlanNextAction() throws RosException
00374 {
00375 ServiceServer.Callback<PlanNextAction.Request, PlanNextAction.Response> scb = new ServiceServer.Callback<PlanNextAction.Request, PlanNextAction.Response>() {
00376 public PlanNextAction.Response call(PlanNextAction.Request request) {
00377 return handlePlanNextAction(request);
00378 }
00379 };
00380
00381 ServiceServer<PlanNextAction.Request, PlanNextAction.Response, PlanNextAction> srv = nodeHandle.advertiseService(planNextActionService, new PlanNextAction(), scb);
00382 }
00383
00384 private TaskRequest.Response handleTaskRequestJSON(TaskRequest.Request request)
00385 {
00386 TaskRequest.Response res = new TaskRequest.Response();
00387 System.out.println("Received request for new task -- JSON command received");
00388
00389 currentTask = SRSJSONParser.parseJSONToTask(request.json_parameters);
00390
00391
00392 if(currentTask == null) {
00393
00394 res.result = 1;
00395 res.description = "No action";
00396 System.out.println("No action. Task is null");
00397 }
00398 else if(currentTask.isEmpty()) {
00399
00400 res.result = 1;
00401 res.description = "No action";
00402 System.out.println("No action. Task is empty");
00403 }
00404 else {
00405 res.result = 0;
00406 currentSessionId++;
00407 res.sessionId = currentSessionId;
00408 res.description = "No";
00409 System.out.println("SESSION ID IS--> " + res.sessionId);
00410 }
00411
00412 return res;
00413 }
00414
00415 private TaskRequest.Response handleTaskRequest(TaskRequest.Request request)
00416 {
00417 if(request.json_parameters != null) {
00418 if(!request.json_parameters.trim().equals("")) {
00419 return handleTaskRequestJSON(request);
00420 }
00421 }
00423
00424 return handleTaskRequestOld(request);
00425 }
00426
00427 private TaskRequest.Response handleTaskRequestOld(TaskRequest.Request request)
00428 {
00429 TaskRequest.Response res = new TaskRequest.Response();
00430
00431 System.out.println("Received request for new task");
00432
00433 if(request.task.equals("move")) {
00434 if(ontoDB == null) {
00435 System.out.println(" ONTOLOGY FILE IS NULL ");
00436 }
00437
00438 try{
00439 currentTask = new MoveTask(request.content);
00440 System.out.println("Created CurrentTask " + "move " + request.content);
00441 }
00442 catch(Exception e) {
00443 System.out.println(">>> " + e.getMessage());
00444 currentTask = null;
00445 res.result = 1;
00446 res.description = "No action";
00447 }
00448
00449 }
00450 else if(request.task.equals("get")){
00451 if(ontoDB == null) {
00452 System.out.println(" ONTOLOGY FILE IS NULL ");
00453 }
00454
00455 try{
00456 GetObjectTask got = null;
00457 if(this.graspActionMode.equals("Simple")) {
00458 got = new GetObjectTask(request.content, ConfigInfo.GraspType.MOVE_AND_GRASP);
00459 currentTask = (Task)got;
00460 }
00461 else if(this.graspActionMode.equals("Planned")) {
00462 got = new GetObjectTask(request.content, ConfigInfo.GraspType.JUST_GRASP);
00463 currentTask = (Task)got;
00464 }
00465 else {
00467 got = new GetObjectTask(request.content, ConfigInfo.GraspType.MOVE_AND_GRASP);
00468 currentTask = (Task)got;
00469 }
00470 System.out.println("Created CurrentTask " + "get " + request.content);
00471 }
00472 catch(Exception e) {
00473 System.out.println(">>> " + e.getMessage());
00474 currentTask = null;
00475 res.result = 1;
00476 res.description = "No action";
00477 }
00478
00479
00480
00481 }
00482 else if(request.task.equals("search")){
00483 if(ontoDB == null) {
00484 System.out.println(" ONTOLOGY FILE IS NULL ");
00485 }
00486 try{
00487
00488
00489 SearchObjectTask sot = null;
00490 sot = new SearchObjectTask(request.content);
00491 currentTask = (Task)sot;
00492
00493 System.out.println("Created CurrentTask " + "search " + request.content);
00494 }
00495 catch(Exception e) {
00496 System.out.println(">>> " + e.getMessage());
00497 currentTask = null;
00498 res.result = 1;
00499 res.description = "No action";
00500 }
00501
00502 }
00503 else if(request.task.equals("fetch")){
00504
00505 if(ontoDB == null) {
00506 System.out.println(" ONTOLOGY FILE IS NULL ");
00507 }
00508 try{
00509 FetchObjectTask got = null;
00510 if(this.graspActionMode.equals("Simple")) {
00511 got = new FetchObjectTask(request.content, request.userPose, ConfigInfo.GraspType.MOVE_AND_GRASP);
00512 currentTask = (Task)got;
00513 }
00514 else if(this.graspActionMode.equals("Planned")) {
00515 got = new FetchObjectTask(request.content, request.userPose, ConfigInfo.GraspType.JUST_GRASP);
00516 currentTask = (Task)got;
00517 }
00518 else {
00520 got = new FetchObjectTask(request.content, request.userPose, ConfigInfo.GraspType.MOVE_AND_GRASP);
00521 currentTask = (Task)got;
00522 }
00523 System.out.println("Created CurrentTask " + "fetch " + request.content);
00524 }
00525 catch(Exception e) {
00526 System.out.println(">>> " + e.getMessage());
00527 currentTask = null;
00528 res.result = 1;
00529 res.description = "No action";
00530 }
00531 }
00532 else if(request.task.equals("deliver")){
00533 if(ontoDB == null) {
00534 System.out.println(" ONTOLOGY FILE IS NULL ");
00535 }
00536 try{
00537 GetObjectTask got = new GetObjectTask(request.content);
00538 currentTask = (Task)got;
00539 System.out.println("Created CurrentTask " + "get " + request.content);
00540 }
00541 catch(Exception e) {
00542 System.out.println(">>> " + e.getMessage());
00543 currentTask = null;
00544 res.result = 1;
00545 res.description = "No action";
00546 }
00547 }
00548 else if(request.task.equals("charging")) {
00549 if(ontoDB == null) {
00550 System.out.println(" ONTOLOGY FILE IS NULL ");
00551 }
00552 currentTask = new ChargingTask();
00553 System.out.println("Created CurrentTask " + "charge ");
00554
00555 }
00556 else if(request.task.equals("check") || request.task.equals("verify")){
00557
00558 if(ontoDB == null) {
00559 System.out.println(" ONTOLOGY FILE IS NULL ");
00560 }
00561
00562 currentTask = new CheckWorkspaceTask(request.content);
00563 System.out.println("Created CurrentTask " + "check workspace " + request.content);
00564 }
00565 else if(request.task.equals("stop")) {
00566 if(ontoDB == null) {
00567 System.out.println(" ONTOLOGY FILE IS NULL ");
00568 }
00569 currentTask = new StopTask();
00570 System.out.println("Created CurrentTask " + " STOP ");
00571
00572 }
00573 else {
00574
00575
00576
00577
00578
00579 currentTask = null;
00580 res.result = 1;
00581 res.description = "No action";
00582 }
00583
00584
00585 if(currentTask == null) {
00586
00587 res.result = 1;
00588 res.description = "No action";
00589 System.out.println("No action. Task is null");
00590 }
00591 else if(currentTask.isEmpty()) {
00592
00593 res.result = 1;
00594 res.description = "No action";
00595 System.out.println("No action. Task is empty");
00596 }
00597 else {
00598 res.result = 0;
00599 currentSessionId++;
00600 res.sessionId = currentSessionId;
00601 res.description = "No";
00602 System.out.println("SESSION ID IS--> " + res.sessionId);
00603 }
00604
00605
00606
00607 return res;
00608 }
00609
00610 private void initTaskRequest() throws RosException
00611 {
00612 ServiceServer.Callback<TaskRequest.Request, TaskRequest.Response> scb = new ServiceServer.Callback<TaskRequest.Request, TaskRequest.Response>() {
00613 public TaskRequest.Response call(TaskRequest.Request request) {
00614 return handleTaskRequest(request);
00615 }
00616 };
00617
00618 System.out.println(taskRequestService);
00619 ServiceServer<TaskRequest.Request, TaskRequest.Response, TaskRequest> srv = nodeHandle.advertiseService(taskRequestService, new TaskRequest(), scb);
00620 }
00621
00622
00623 private GetObjectsOnMap.Response handleGetObjectsOnMap(GetObjectsOnMap.Request req)
00624 {
00625 String map = req.map;
00626 boolean ifGeometryInfo = req.ifGeometryInfo;
00627
00628 return OntoQueryUtil.getObjectsOnMap(map, ifGeometryInfo);
00629 }
00630
00631 private void initGetObjectsOnMap() throws RosException
00632 {
00633 ServiceServer.Callback<GetObjectsOnMap.Request, GetObjectsOnMap.Response> scb = new ServiceServer.Callback<GetObjectsOnMap.Request, GetObjectsOnMap.Response>() {
00634 public GetObjectsOnMap.Response call(GetObjectsOnMap.Request request) {
00635 return handleGetObjectsOnMap(request);
00636 }
00637 };
00638
00639 System.out.println(getObjectsOnMapService);
00640 ServiceServer<GetObjectsOnMap.Request, GetObjectsOnMap.Response, GetObjectsOnMap> srv = nodeHandle.advertiseService(getObjectsOnMapService, new GetObjectsOnMap(), scb);
00641 }
00642
00643
00644 private GetWorkspaceOnMap.Response handleGetWorkspaceOnMap(GetWorkspaceOnMap.Request req)
00645 {
00646 return OntoQueryUtil.getWorkspaceOnMap(req.map, req.ifGeometryInfo);
00647 }
00648
00649 private float getFloatOfStatement(Statement stm)
00650 {
00651 float t = -1000;
00652 try {
00653 t = stm.getFloat();
00654 }
00655 catch(Exception e) {
00656 System.out.println(e.getMessage());
00657 }
00658 return t;
00659 }
00660
00661 private int getIntOfStatement(Statement stm)
00662 {
00663 int t = -1000;
00664 try {
00665 t = stm.getInt();
00666 }
00667 catch(Exception e) {
00668 System.out.println(e.getMessage());
00669 }
00670 return t;
00671 }
00672
00673 private void initInsertInstance() throws RosException
00674 {
00675 ServiceServer.Callback<InsertInstance.Request, InsertInstance.Response> scb = new ServiceServer.Callback<InsertInstance.Request, InsertInstance.Response>() {
00676 public InsertInstance.Response call(InsertInstance.Request request) {
00677 return handleInsertInstance(request);
00678 }
00679 };
00680
00681 System.out.println(insertInstanceService);
00682 ServiceServer<InsertInstance.Request, InsertInstance.Response, InsertInstance> srv = nodeHandle.advertiseService(insertInstanceService, new InsertInstance(), scb);
00683 }
00684
00685 private InsertInstance.Response handleInsertInstance(InsertInstance.Request request)
00686 {
00687 InsertInstance.Response res = new InsertInstance.Response();
00688 String objectName = request.objectName;
00689 String objectClass = request.objectClass;
00690 String hhId = request.houseHoldId;
00691 int id = -1000;
00692 try{
00693 id = Integer.valueOf(hhId);
00694 }
00695 catch(NumberFormatException e) {
00696 System.out.println(e.getMessage());
00697 id = -1000;
00698 }
00699
00700 try {
00701 ontoDB.insertInstance(this.globalNamespace, objectClass, this.mapNamespace, objectName);
00702 OntoQueryUtil.updateHHIdOfObject(id, this.globalNamespace, this.mapNamespace, objectName);
00703 res.status = 0;
00704 }
00705 catch(DuplicatedEntryException de) {
00706 res.status = 1;
00707 }
00708 catch(UnknownClassException ue) {
00709 res.status = 2;
00710 }
00711 catch(Exception e) {
00712 res.status = -1;
00713 }
00714
00715 return res;
00716 }
00717
00718 private void initDeleteInstance() throws RosException
00719 {
00720 ServiceServer.Callback<DeleteInstance.Request, DeleteInstance.Response> scb = new ServiceServer.Callback<DeleteInstance.Request, DeleteInstance.Response>() {
00721 public DeleteInstance.Response call(DeleteInstance.Request request) {
00722 return handleDeleteInstance(request);
00723 }
00724 };
00725
00726 System.out.println(deleteInstanceService);
00727 ServiceServer<DeleteInstance.Request, DeleteInstance.Response, DeleteInstance> srv = nodeHandle.advertiseService(deleteInstanceService, new DeleteInstance(), scb);
00728 }
00729
00730 private DeleteInstance.Response handleDeleteInstance(DeleteInstance.Request request)
00731 {
00732 DeleteInstance.Response res = new DeleteInstance.Response();
00733 String objectName = request.objectName;
00734 String objectURI = this.mapNamespace;
00735 if(request.objectURI == null) {
00736 objectURI = request.objectURI;
00737 }
00738 try {
00739 ontoDB.deleteInstance( objectURI, request.objectName);
00740 res.status = 0;
00741 }
00742 catch(NonExistenceEntryException ne) {
00743 res.status = 1;
00744 }
00745 catch(UnknownException e) {
00746 System.out.println(e.toString() + " --- " + e.getMessage());
00747 res.status = -1;
00748 }
00749 catch(JenaException e) {
00750 System.out.println(e.toString() + " --- " + e.getMessage());
00751 }
00752 catch(Exception e) {
00753 System.out.println(e.toString() + " --- " + e.getMessage());
00754 res.status = -1;
00755 }
00756 return res;
00757 }
00758
00759 private void initUpdatePosInfo() throws RosException
00760 {
00761 ServiceServer.Callback<UpdatePosInfo.Request, UpdatePosInfo.Response> scb = new ServiceServer.Callback<UpdatePosInfo.Request, UpdatePosInfo.Response>() {
00762 public UpdatePosInfo.Response call(UpdatePosInfo.Request request) {
00763 return handleUpdatePosInfo(request);
00764 }
00765 };
00766
00767 System.out.println(updatePosInfoService);
00768 ServiceServer<UpdatePosInfo.Request, UpdatePosInfo.Response, UpdatePosInfo> srv = nodeHandle.advertiseService(updatePosInfoService, new UpdatePosInfo(), scb);
00769 }
00770
00771 private UpdatePosInfo.Response handleUpdatePosInfo(UpdatePosInfo.Request request)
00772 {
00773 UpdatePosInfo.Response res = new UpdatePosInfo.Response();
00774 String objectName = request.objectName;
00775 SRSSpatialInfo spa = request.spatialInfo;
00776 try {
00777
00778
00779
00780 OntoQueryUtil.updatePoseOfObject(spa.pose, this.globalNamespace, this.mapNamespace, objectName);
00781 OntoQueryUtil.updateDimensionOfObject(spa.l, spa.w, spa.h, this.globalNamespace, this.mapNamespace, objectName);
00782 res.success = true;
00783 }
00784 catch(Exception e) {
00785 res.success = false;
00786 System.out.println(e.toString() + " === " + e.getMessage());
00787 }
00788
00789 return res;
00790 }
00791
00792 private void initGetWorkspaceOnMap() throws RosException
00793 {
00794 ServiceServer.Callback<GetWorkspaceOnMap.Request, GetWorkspaceOnMap.Response> scb = new ServiceServer.Callback<GetWorkspaceOnMap.Request, GetWorkspaceOnMap.Response>() {
00795 public GetWorkspaceOnMap.Response call(GetWorkspaceOnMap.Request request) {
00796 return handleGetWorkspaceOnMap(request);
00797 }
00798 };
00799
00800 System.out.println(getWorkSpaceOnMapService);
00801 ServiceServer<GetWorkspaceOnMap.Request, GetWorkspaceOnMap.Response, GetWorkspaceOnMap> srv = nodeHandle.advertiseService(getWorkSpaceOnMapService, new GetWorkspaceOnMap(), scb);
00802 }
00803
00804 private GetObjectsOnTray.Response handleGetObjectsOnTray(GetObjectsOnTray.Request request)
00805 {
00806 GetObjectsOnTray.Response res = new GetObjectsOnTray.Response();
00807 String mapName = request.map;
00808
00809
00810
00811
00812 String prefix = "PREFIX srs: <http://www.srs-project.eu/ontologies/srs.owl#>\n"
00813 + "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"
00814 + "PREFIX mapname: " + "<" + OntoQueryUtil.ObjectNameSpace + ">\n"
00815 + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n";
00816 String queryString = "SELECT DISTINCT ?objs ?tray WHERE { "
00817 + "?tray rdf:type srs:COBTray . "
00818 + "<" + OntoQueryUtil.ObjectNameSpace + OntoQueryUtil.RobotName + ">"
00819 + " srs:hasPart ?tray . "
00820 + "?objs srs:spatiallyRelated ?tray . "
00821 + "}";
00822
00823 if (this.ontoDB == null) {
00824 ros.logInfo("INFO: Ontology Database is NULL. Nothing executed. ");
00825 return res;
00826 }
00827
00828 try {
00829 ArrayList<QuerySolution> rset = ontoDB.executeQueryRaw(prefix + queryString);
00830 if (rset.size() == 0) {
00831 ros.logInfo("No found from database");
00832 }
00833
00834 for(int i = 0; i < rset.size(); i++) {
00835 QuerySolution qs = rset.get(i);
00836 RDFNode rn = qs.get("objs");
00837 Individual tmpInd = KnowledgeEngine.ontoDB.getIndividual(rn.toString());
00838 res.objects.add(rn.asResource().getLocalName());
00839 res.classesOfObjects.add(tmpInd.getRDFType(true).getLocalName());
00840 }
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859 }
00860 catch (Exception e) {
00861 System.out.println("Exception --> " + e.getMessage());
00862 }
00863
00864 return res;
00865 }
00866
00867 private void initGetObjectsOnTray() throws RosException
00868 {
00869 ServiceServer.Callback<GetObjectsOnTray.Request, GetObjectsOnTray.Response> scb = new ServiceServer.Callback<GetObjectsOnTray.Request, GetObjectsOnTray.Response>() {
00870 public GetObjectsOnTray.Response call(GetObjectsOnTray.Request request) {
00871 return handleGetObjectsOnTray(request);
00872 }
00873 };
00874
00875 ServiceServer<GetObjectsOnTray.Request,GetObjectsOnTray.Response,GetObjectsOnTray> srv = nodeHandle.advertiseService(getObjectsOnTrayService, new GetObjectsOnTray(), scb);
00876 }
00877
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905 private void initGetRoomsOnMap() throws RosException
00906 {
00907 ServiceServer.Callback<GetRoomsOnMap.Request, GetRoomsOnMap.Response> scb = new ServiceServer.Callback<GetRoomsOnMap.Request, GetRoomsOnMap.Response>() {
00908 public GetRoomsOnMap.Response call(GetRoomsOnMap.Request request) {
00909 return handleGetRoomsOnMap(request);
00910 }
00911 };
00912
00913 System.out.println(getRoomsOnMapService);
00914 ServiceServer<GetRoomsOnMap.Request, GetRoomsOnMap.Response, GetRoomsOnMap> srv = nodeHandle.advertiseService(getRoomsOnMapService, new GetRoomsOnMap(), scb);
00915 }
00916
00917 private GetRoomsOnMap.Response handleGetRoomsOnMap(GetRoomsOnMap.Request req)
00918 {
00919 GetRoomsOnMap.Response re = new GetRoomsOnMap.Response();
00920
00921 String className = globalNamespace;
00922 String mapNS = mapNamespace;
00923
00924 if(req.map != null) {
00925 if(ontoDB.getNamespaceByPrefix(req.map) != null) {
00926 mapNS = ontoDB.getNamespaceByPrefix(req.map);
00927 }
00928 }
00929
00930 className = className + "RoomInAConstruction";
00931
00932
00933 try{
00934 Iterator<Individual> instances = ontoDB.getInstancesOfClass(className);
00935 if(instances == null) {
00936 return re;
00937 }
00938
00939 while (instances.hasNext()) {
00940 Individual temp = (Individual)instances.next();
00941
00942 if(temp.getNameSpace().equals(mapNamespace)) {
00943 re.rooms.add(temp.getLocalName());
00944
00945
00946 String readableName = temp.getLocalName();
00947 try{
00948 com.hp.hpl.jena.rdf.model.NodeIterator nit = KnowledgeEngine.ontoDB.listPropertiesOf(OntoQueryUtil.GlobalNameSpace, "objectReadableName", temp);
00949 while(nit.hasNext()) {
00950 RDFNode n = nit.next();
00951 Literal l = n.asLiteral();
00952 if(l.getLanguage().equalsIgnoreCase(OntoQueryUtil.Language)) {
00953 readableName = l.getString();
00954 break;
00955 }
00956 }
00957 }
00958 catch(Exception e) {
00959 System.out.println("CAUGHT exception: " + e.toString() + " Cannot retrieve readable name for object. Use Empty instead");
00960 }
00961 finally {
00962 re.readableNames.add(readableName);
00963 }
00964
00965 if(req.ifGeometryInfo == true) {
00966 SRSSpatialInfo spatialInfo = new SRSSpatialInfo();
00967 com.hp.hpl.jena.rdf.model.Statement stm = ontoDB.getPropertyOf(globalNamespace, "xCoord", temp);
00968 spatialInfo.pose.position.x = getFloatOfStatement(stm);
00969 stm = ontoDB.getPropertyOf(globalNamespace, "yCoord", temp);
00970 spatialInfo.pose.position.y = getFloatOfStatement(stm);
00971 stm = ontoDB.getPropertyOf(globalNamespace, "zCoord", temp);
00972 spatialInfo.pose.position.z = getFloatOfStatement(stm);
00973
00974 stm = ontoDB.getPropertyOf(globalNamespace, "widthOfObject", temp);
00975 spatialInfo.w = getFloatOfStatement(stm);
00976 stm = ontoDB.getPropertyOf(globalNamespace, "heightOfObject", temp);
00977 spatialInfo.h = getFloatOfStatement(stm);
00978 stm = ontoDB.getPropertyOf(globalNamespace, "lengthOfObject", temp);
00979 spatialInfo.l = getFloatOfStatement(stm);
00980
00981 stm = ontoDB.getPropertyOf(globalNamespace, "qu", temp);
00982 spatialInfo.pose.orientation.w = getFloatOfStatement(stm);
00983 stm = ontoDB.getPropertyOf(globalNamespace, "qx", temp);
00984 spatialInfo.pose.orientation.x = getFloatOfStatement(stm);
00985 stm = ontoDB.getPropertyOf(globalNamespace, "qy", temp);
00986 spatialInfo.pose.orientation.y = getFloatOfStatement(stm);
00987 stm = ontoDB.getPropertyOf(globalNamespace, "qz", temp);
00988 spatialInfo.pose.orientation.z = getFloatOfStatement(stm);
00989
00990 re.roomsInfo.add(spatialInfo);
00991 }
00992 }
00993 }
00994 }
00995 catch(Exception e) {
00996 System.out.println(e.getMessage());
00997 }
00998
00999 return re;
01000 }
01001
01002 private void initGetPredefinedPoses() throws RosException
01003 {
01004 ServiceServer.Callback<GetPredefinedPoses.Request, GetPredefinedPoses.Response> scb = new ServiceServer.Callback<GetPredefinedPoses.Request, GetPredefinedPoses.Response>() {
01005 public GetPredefinedPoses.Response call(GetPredefinedPoses.Request request) {
01006 return handleGetPredefinedPoses(request);
01007 }
01008 };
01009
01010 System.out.println(getPredefinedPosesService);
01011 ServiceServer<GetPredefinedPoses.Request, GetPredefinedPoses.Response, GetPredefinedPoses> srv = nodeHandle.advertiseService(getPredefinedPosesService, new GetPredefinedPoses(), scb);
01012 }
01013
01014 private GetPredefinedPoses.Response handleGetPredefinedPoses(GetPredefinedPoses.Request req)
01015 {
01016 GetPredefinedPoses.Response re = new GetPredefinedPoses.Response();
01017
01018 String className = globalNamespace;
01019 String mapNS = mapNamespace;
01020
01021 if(req.map != null) {
01022 if(ontoDB.getNamespaceByPrefix(req.map) != null) {
01023 mapNS = ontoDB.getNamespaceByPrefix(req.map);
01024 }
01025 }
01026
01027 className = className + "Point2D";
01028
01029 try{
01030 Iterator<Individual> instances = ontoDB.getInstancesOfClass(className);
01031 if(instances == null) {
01032 return re;
01033 }
01034
01035 while (instances.hasNext()) {
01036 Individual temp = (Individual)instances.next();
01037
01038 if(temp.getNameSpace().equals(mapNamespace)) {
01039 Pose2D pos2d = new Pose2D();
01040 try {
01041 com.hp.hpl.jena.rdf.model.Statement stm = ontoDB.getPropertyOf(globalNamespace, "xCoordinate", temp);
01042 pos2d.x = getFloatOfStatement(stm);
01043 stm = ontoDB.getPropertyOf(globalNamespace, "yCoordinate", temp);
01044 pos2d.y = getFloatOfStatement(stm);
01045 stm = ontoDB.getPropertyOf(globalNamespace, "orientationTheta", temp);
01046 pos2d.theta = getFloatOfStatement(stm);
01047
01048
01049 String readableName = temp.getLocalName();
01050 com.hp.hpl.jena.rdf.model.NodeIterator nit = KnowledgeEngine.ontoDB.listPropertiesOf(OntoQueryUtil.GlobalNameSpace, "objectReadableName", temp);
01051
01052 while(nit.hasNext()) {
01053 RDFNode n = nit.next();
01054 Literal l = n.asLiteral();
01055
01056 if(l.getLanguage().equalsIgnoreCase(OntoQueryUtil.Language)) {
01057 readableName = l.getString();
01058 break;
01059 }
01060 }
01061
01062
01063 Map<String, String> pros = new HashMap<String, String>();
01064 try{
01065 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "insideOf", temp);
01066 String r = stm.getObject().asResource().getLocalName();
01067 pros.put("insideOf", r);
01068 }
01069 catch(Exception e) {
01070 System.out.println("CAUGHT exception: " + e.toString());
01071 }
01072 String json_pro = SRSJSONParser.encodeObjectProperties(pros);
01073
01074 re.locations.add(temp.getLocalName());
01075 re.poses.add(pos2d);
01076 re.readableNames.add(readableName);
01077 re.json_properties.add(json_pro);
01078 }
01079 catch(Exception e) {
01080 System.out.println("invalid instance. ignored.");
01081 }
01082 }
01083 }
01084 }
01085 catch(Exception e) {
01086 System.out.println(e.getMessage());
01087 }
01088
01089 return re;
01090 }
01091
01092 private void initGetWorkspaceForObject() throws RosException
01093 {
01094 ServiceServer.Callback<GetWorkspaceForObject.Request, GetWorkspaceForObject.Response> scb = new ServiceServer.Callback<GetWorkspaceForObject.Request, GetWorkspaceForObject.Response>() {
01095 public GetWorkspaceForObject.Response call(GetWorkspaceForObject.Request request) {
01096 return handleGetWorkspaceForObject(request);
01097 }
01098 };
01099
01100 System.out.println(getWorkspaceForObjectService);
01101 ServiceServer<GetWorkspaceForObject.Request, GetWorkspaceForObject.Response, GetWorkspaceForObject> srv = nodeHandle.advertiseService(getWorkspaceForObjectService, new GetWorkspaceForObject(), scb);
01102 }
01103
01104 private GetWorkspaceForObject.Response handleGetWorkspaceForObject(GetWorkspaceForObject.Request req)
01105 {
01106 GetWorkspaceForObject.Response re = new GetWorkspaceForObject.Response();
01107 try{
01108 if(req.config == 0) {
01109 re.workspaces = OntoQueryUtil.getFurnituresLinkedToObject(req.objectType);
01110 }
01111 else if(req.config == 1) {
01112 re.workspaces = OntoQueryUtil.getWorkspaceNamesOfObject(req.objectType);
01113 }
01114 }
01115 catch(Exception e) {
01116 System.out.println("Please check if the object is spelled correctly. ");
01117 }
01118 return re;
01119 }
01120
01121 private String defaultContextPath()
01122 {
01123 this.confPath = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
01124 this.confPath = this.confPath + "../conf/";
01125 return this.confPath;
01126 }
01127
01128 public String getContextPath()
01129 {
01130 return this.confPath;
01131 }
01132
01133 public void testFunction() {
01134
01135 System.out.println("++++++++++++++++++++++++++++++++++");
01136 OntoQueryUtil.computeOnSpatialRelation();
01137
01138
01139
01140
01141
01142
01143 System.out.println("++++++++++++++++++++++++++++++++++");
01144
01145
01146
01147
01148 }
01149
01150 public static void main(String[] args)
01151 {
01152
01153 String configFile = new String();
01154 System.out.print("There are " + args.length + " input arguments: ");
01155
01156 if(args.length == 1) {
01157 configFile = args[0];
01158 System.out.println(configFile);
01159 }
01160 else {
01161 String env = System.getenv("ROBOT_ENV");
01162 configFile = env + ".cfg";
01163
01164 System.out.println(configFile);
01165 }
01166
01167 Properties conf = new Properties();
01168 KnowledgeEngine knowEng = new KnowledgeEngine();
01169
01170
01171 if (knowEng.init(configFile)) {
01172 System.out.println("OK");
01173
01174 }
01175 else {
01176 System.out.println("Something wrong with initialisation");
01177 }
01178 }
01179
01180 private Task currentTask;
01181 private int currentSessionId = 1;
01182 private String nodeName;
01183
01184 private Properties config;
01185
01186 private String taskRequestService = "task_request";
01187 private String planNextActionService = "plan_next_action";
01188 private String generateSequenceService = "generate_sequence";
01189 private String getObjectsOnTrayService = "get_objects_on_tray";
01190 private String querySparQLService = "query_sparql";
01191 private String getObjectsOnMapService = "get_objects_on_map";
01192 private String getWorkSpaceOnMapService = "get_workspace_on_map";
01193 private String insertInstanceService = "insert_instance";
01194 private String deleteInstanceService = "delete_instance";
01195 private String updatePosInfoService = "update_pos_info";
01196 private String getRoomsOnMapService = "get_rooms_on_map";
01197 private String getPredefinedPosesService = "get_predefined_poses";
01198 private String getWorkspaceForObjectService = "get_workspace_for_object";
01199
01200 private String mapName = "ipa-kitchen-map";
01201 private String mapNamespace = "http://www.srs-project.eu/ontologies/ipa-kitchen-map.owl#";
01202
01203 private String globalNamespace = "http://www.srs-project.eu/ontologies/srs.owl#";
01204
01205
01206 private String graspActionMode = "Simple";
01207 private String confPath;
01208
01209
01210
01211 }