InformationRetrieval.java
Go to the documentation of this file.
00001 /****************************************************************
00002  *
00003  * Copyright (c) 2011, 2012
00004  *
00005  * School of Engineering, Cardiff University, UK
00006  *
00007  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00008  *
00009  * Project name: srs EU FP7 (www.srs-project.eu)
00010  * ROS stack name: srs
00011  * ROS package name: srs_knowledge
00012  * Description: 
00013  *                                                              
00014  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00015  *
00016  * @author Ze Ji, email: jiz1(at)cf.ac.uk
00017  *
00018  * Date of creation: April 2012
00019  * ToDo: 
00020  *
00021  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00022  *
00023  * Redistribution and use in source and binary forms, with or without
00024  * modification, are permitted provided that the following conditions are met:
00025  *
00026  *       * Redistributions of source code must retain the above copyright
00027  *         notice, this list of conditions and the following disclaimer.
00028  *       * Redistributions in binary form must reproduce the above copyright
00029  *         notice, this list of conditions and the following disclaimer in the
00030  *         documentation and/or other materials provided with the distribution.
00031  *       * Neither the name of the school of engineering, Cardiff University
00032  *         nor the names of its contributors may be used to endorse or promote 
00033  *         products derived from this software without specific prior written 
00034  *         permission.
00035  *
00036  * This program is free software: you can redistribute it and/or modify
00037  * it under the terms of the GNU Lesser General Public License LGPL as 
00038  * published by the Free Software Foundation, either version 3 of the 
00039  * License, or (at your option) any later version.
00040  * 
00041  * This program is distributed in the hope that it will be useful,
00042  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00043  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00044  * GNU Lesser General Public License LGPL for more details.
00045  * 
00046  * You should have received a copy of the GNU Lesser General Public 
00047  * License LGPL along with this program. 
00048  * If not, see <http://www.gnu.org/licenses/>.
00049  *
00050  ****************************************************************/
00051 
00052 package org.srs.srs_knowledge.utils;
00053 
00054 import java.io.*;
00055 import java.util.StringTokenizer;
00056 import java.util.ArrayList;
00057 import java.util.Iterator;
00058 import ros.pkg.srs_knowledge.msg.*;
00059 import ros.pkg.geometry_msgs.msg.Pose2D;
00060 import org.srs.srs_knowledge.knowledge_engine.*;
00061 
00062 import ros.*;
00063 import ros.communication.*;
00064 
00065 import com.hp.hpl.jena.rdf.model.*;
00066 import com.hp.hpl.jena.vocabulary.*;
00067 import com.hp.hpl.jena.util.FileManager;
00068 
00069 import com.hp.hpl.jena.rdf.model.Property;
00070 import com.hp.hpl.jena.ontology.OntClass;
00071 import com.hp.hpl.jena.ontology.OntModel;
00072 import com.hp.hpl.jena.rdf.model.Statement;
00073 import com.hp.hpl.jena.ontology.Individual;
00074 import com.hp.hpl.jena.shared.Lock;
00075 import com.hp.hpl.jena.ontology.OntResource;
00076 
00077 import tfjava.*;
00078 
00079 import javax.vecmath.Quat4d;
00080 import javax.vecmath.Vector3d;
00081 import javax.vecmath.Point3d;
00082 import javax.vecmath.Matrix4d;
00083 
00084 import math.geom2d.*;
00085 import math.geom2d.line.LineSegment2D;
00086 import math.geom2d.polygon.Polygon2DUtils;
00087 import math.geom2d.polygon.Polygon2D;
00088 import math.geom2d.polygon.SimplePolygon2D;
00089 
00090 import math.geom2d.Point2D;
00091 
00092 import ros.pkg.srs_object_database_msgs.srv.GetObjectId;
00093 //import ros.pkg.srs_object_database_msgs.msg.*;
00094 
00095 public class InformationRetrieval 
00096 {
00100     public static BoundingBoxDim retrieveBoundingBoxInfo(String objectClassURI) {
00101         // global variables (ROS, Ontology handler etc)
00102         // if there does not exist such an object, then insert a new one
00103         // bounding box can be obtained from HHDB
00104         BoundingBoxDim d = new BoundingBoxDim();
00105         // look for existing or history data
00106         
00107         Iterator<Individual> instances = KnowledgeEngine.ontoDB.getInstancesOfClass(objectClassURI);
00108         if(instances != null) {
00109             if(instances.hasNext()) {
00110                 com.hp.hpl.jena.rdf.model.Statement stm;
00111                 Individual temp = instances.next();
00112                 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "widthOfObject", temp);
00113                 d.w = getFloatOfStatement(stm, -1000);
00114                 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "heightOfObject", temp);
00115                 d.h = getFloatOfStatement(stm, -1000);
00116                 stm = KnowledgeEngine.ontoDB.getPropertyOf(OntoQueryUtil.GlobalNameSpace, "lengthOfObject", temp);
00117                 d.l = getFloatOfStatement(stm, -1000);
00118                 if(d.w != -1000 && d.h != -1000 && d.l != -1000) {
00119                     return d;
00120                 }
00121             }
00122         }
00123         
00124         // if no, check hhdb by calling ros service 
00125 
00126         ServiceClient<GetObjectId.Request, GetObjectId.Response, GetObjectId> sc = KnowledgeEngine.nodeHandle.serviceClient("get_models" , new GetObjectId(), false);
00127         
00128         GetObjectId.Request rq = new GetObjectId.Request();     
00129         
00130         rq.type = "item";
00131         String[] subStrs = objectClassURI.trim().split("#");
00132         String objectClass = objectClassURI;
00133         if(subStrs.length == 2) {
00134             objectClass = subStrs[1];
00135         }
00136         rq.item = objectClass;
00137         ArrayList<String> modelDesc = new ArrayList<String>();
00138         ArrayList<String> modelCategory = new ArrayList<String>();
00139         int id = -1000;
00140         
00141         try {
00142             GetObjectId.Response res = sc.call(rq);
00143             int[] ids = res.model_ids;
00144             modelDesc = res.model_desc;
00145             modelCategory = res.model_category;
00146             for (int i = 0; i < modelDesc.size(); i++) {
00147                 if(modelDesc.get(i).equals(objectClass)) {
00148                     // objectClass in Knowledgebase is model_desc in HHDB, not model_category... 
00149                     id = ids[i];
00150                     
00151                     d.l = res.model_x_size[i];
00152                     d.w = res.model_y_size[i];
00153                     d.h = res.model_z_size[i];
00154                     if(d.l != 0 && d.h != 0 && d.w != 0) {
00155                         sc.shutdown();
00156                         return d;
00157                     }
00158                 }
00159             }
00160         }
00161         catch(RosException e) {
00162             System.out.println(e.getMessage());
00163             sc.shutdown();
00164             return new BoundingBoxDim();
00165         }       
00166         sc.shutdown();
00167 
00168         return new BoundingBoxDim();
00169     }
00170 
00171     public static float getFloatOfStatement(com.hp.hpl.jena.rdf.model.Statement stm, float defaultIfInvalid) {
00172         float t = defaultIfInvalid;
00173         try { 
00174             t = stm.getFloat();
00175         }
00176         catch(Exception e) {
00177             System.out.println(e.getMessage());
00178         }
00179         return t;
00180     }
00181 
00182     private static int getIntOfStatement(Statement stm, int defaultIfInvalid)
00183     {
00184         int t = defaultIfInvalid;
00185         try { 
00186             t = stm.getInt();
00187         }
00188         catch(Exception e) {
00189             System.out.println(e.getMessage());
00190         }
00191         return t;
00192     }
00193 }


srs_knowledge
Author(s): Ze Ji
autogenerated on Mon Oct 6 2014 08:27:32