Go to the documentation of this file.00001 package edu.tum.cs.ias.knowrob.map;
00002
00003 import java.text.SimpleDateFormat;
00004 import java.util.ArrayList;
00005 import java.util.Calendar;
00006 import java.util.HashMap;
00007 import org.semanticweb.owlapi.model.*;
00008
00009 import edu.tum.cs.ias.knowrob.owl.OWLClass;
00010 import edu.tum.cs.ias.knowrob.owl.ObjectInstance;
00011 import edu.tum.cs.ias.knowrob.owl.utils.OWLFileUtils;
00012 import edu.tum.cs.ias.knowrob.owl.utils.OWLImportExport;
00013
00014 import ros.*;
00015 import ros.pkg.mod_semantic_map.srv.*;
00016 import ros.pkg.mod_semantic_map.msg.*;
00017
00018
00027 public class SemanticMapToOWL {
00028
00029 static Boolean rosInitialized = false;
00030 static Ros ros;
00031 static NodeHandle n;
00032
00039 public SemanticMapToOWL() {
00040
00041 try {
00042
00043 initRos();
00044
00045 n.advertiseService("/knowrob_semantic_map_to_owl/generate_owl_map",
00046 new GenerateSemanticMapOWL(),
00047 new ConvertToOwlCallback());
00048 ros.spin();
00049
00050 } catch (RosException e) {
00051 e.printStackTrace();
00052 }
00053
00054 }
00055
00056
00057
00061 protected static void initRos() {
00062
00063 ros = Ros.getInstance();
00064
00065 if(!Ros.getInstance().isInitialized()) {
00066 ros.init("knowrob_semantic_map_to_owl");
00067 }
00068 n = ros.createNodeHandle();
00069
00070 }
00071
00079 class ConvertToOwlCallback implements ServiceServer.Callback<GenerateSemanticMapOWL.Request, GenerateSemanticMapOWL.Response> {
00080
00081 @Override
00082 public GenerateSemanticMapOWL.Response call(GenerateSemanticMapOWL.Request req) {
00083
00084 GenerateSemanticMapOWL.Response res = new GenerateSemanticMapOWL.Response();
00085 res.owlmap="";
00086
00087 if (req.map != null && req.map.objects.size()>0) {
00088
00089 OWLImportExport export = new OWLImportExport();
00090
00091
00092 ArrayList<String[]> address = new ArrayList<String[]>();
00093 String namespace = OWLImportExport.IAS_MAP;
00094
00095 try {
00096
00097 if(n.hasParam("map_address_room_nr") && n.getStringParam("map_address_room_nr")!=null)
00098 address.add(new String[]{"knowrob:RoomInAConstruction", "knowrob:roomNumber", n.getStringParam("map_address_room_nr")});
00099
00100 if(n.hasParam("map_address_floor_nr") && n.getStringParam("map_address_floor_nr")!=null)
00101 address.add(new String[]{"knowrob:LevelOfAConstruction", "knowrob:floorNumber", n.getStringParam("map_address_floor_nr")});
00102
00103 if(n.hasParam("map_address_street_nr") && n.getStringParam("map_address_street_nr")!=null)
00104 address.add(new String[]{"knowrob:Building", "knowrob:streetNumber", n.getStringParam("map_address_street_nr")});
00105
00106 if(n.hasParam("map_address_street_name") && n.getStringParam("map_address_street_name")!=null)
00107 address.add(new String[]{"knowrob:Street", "rdfs:label", n.getStringParam("map_address_street_name")});
00108
00109 if(n.hasParam("map_address_city_name") && n.getStringParam("map_address_city_name")!=null)
00110 address.add(new String[]{"knowrob:City", "rdfs:label", n.getStringParam("map_address_city_name")});
00111
00112
00113 if (n.hasParam("map_owl_namespace") && n.getStringParam("map_owl_namespace")!=null) {
00114 namespace = n.getStringParam("map_owl_namespace");
00115
00116 if(!namespace.endsWith("#"))
00117 namespace += "#";
00118 }
00119
00120 System.err.println("Using map id: " + namespace);
00121
00122 } catch (RosException e) {
00123 e.printStackTrace();
00124 }
00125
00126
00127 OWLOntology owlmap = export.createOWLMapDescription(namespace,
00128 "SemanticEnvironmentMap" + new SimpleDateFormat("yyyyMMddHHmmss").format(Calendar.getInstance().getTime()),
00129 semMapObj2MapObj(namespace, req.map.objects), address);
00130 res.owlmap = OWLFileUtils.saveOntologytoString(owlmap, owlmap.getOWLOntologyManager().getOntologyFormat(owlmap));
00131
00132 }
00133
00134 return res;
00135 }
00136 }
00137
00138
00139 private ArrayList<ObjectInstance> semMapObj2MapObj(String map_id, ArrayList<SemMapObject> smos) {
00140
00141 HashMap<Integer, ObjectInstance> intIdToID = new HashMap<Integer, ObjectInstance>();
00142 ArrayList<ObjectInstance> mos = new ArrayList<ObjectInstance>();
00143
00144 for(SemMapObject smo : smos) {
00145
00146 ObjectInstance mo = ObjectInstance.getObjectInstance(smo.type + smo.id);
00147 intIdToID.put(smo.id, mo);
00148
00149 mo.addType(OWLClass.getOWLClass(smo.type));
00150
00151 mo.getDimensions().x=smo.width;
00152 mo.getDimensions().y=smo.depth;
00153 mo.getDimensions().z=smo.height;
00154
00155 for(int i=0;i<4;i++) {
00156 for(int j=0;j<4;j++) {
00157 mo.getPoseMatrix().setElement(i, j, smo.pose[4*i+j]);
00158 }
00159 }
00160
00161 if(intIdToID.get(smo.partOf) != null)
00162 intIdToID.get(smo.partOf).addPhysicalPart(mo);
00163
00164 mos.add(mo);
00165 }
00166
00167 return mos;
00168 }
00169
00170
00171
00172 public static void main(String[] args) {
00173
00174
00175
00176
00177 new SemanticMapToOWL();
00178
00179
00180
00181
00182
00183
00184
00185
00186 }
00187
00188
00189 }