$search
00001 package edu.tum.cs.ias.knowrob.map; 00002 00003 import java.util.ArrayList; 00004 import java.util.HashMap; 00005 import org.semanticweb.owlapi.model.*; 00006 00007 import edu.tum.cs.ias.knowrob.utils.owl.MapObject; 00008 import edu.tum.cs.ias.knowrob.utils.owl.OWLImportExport; 00009 import edu.tum.cs.ias.knowrob.utils.owl.OWLFileUtils; 00010 00011 import ros.*; 00012 import ros.pkg.mod_semantic_map.srv.*; 00013 import ros.pkg.mod_semantic_map.msg.*; 00014 00015 00024 public class SemanticMapToOWL { 00025 00026 static Boolean rosInitialized = false; 00027 static Ros ros; 00028 static NodeHandle n; 00029 00036 public SemanticMapToOWL() { 00037 00038 try { 00039 00040 initRos(); 00041 00042 n.advertiseService("/knowrob_semantic_map_to_owl/generate_owl_map", 00043 new GenerateSemanticMapOWL(), 00044 new ConvertToOwlCallback()); 00045 ros.spin(); 00046 00047 } catch (RosException e) { 00048 e.printStackTrace(); 00049 } 00050 00051 } 00052 00053 00054 00058 protected static void initRos() { 00059 00060 ros = Ros.getInstance(); 00061 00062 if(!Ros.getInstance().isInitialized()) { 00063 ros.init("knowrob_semantic_map_to_owl"); 00064 } 00065 n = ros.createNodeHandle(); 00066 00067 } 00068 00076 class ConvertToOwlCallback implements ServiceServer.Callback<GenerateSemanticMapOWL.Request, GenerateSemanticMapOWL.Response> { 00077 00078 @Override 00079 public GenerateSemanticMapOWL.Response call(GenerateSemanticMapOWL.Request req) { 00080 00081 GenerateSemanticMapOWL.Response res = new GenerateSemanticMapOWL.Response(); 00082 res.owlmap=""; 00083 00084 if (req.map != null && req.map.objects.size()>0) { 00085 00086 OWLImportExport export = new OWLImportExport(); 00087 00088 // Get IRI of target map from the frame_id of header msg 00089 String map_id = req.map.header.frame_id.toString(); 00090 if (map_id.length() != 0) { 00091 // use provided map_id 00092 OWLImportExport.PREFIX_MANAGER.setPrefix("ias_map:", map_id); 00093 } else { 00094 //use IAS_MAP as default, PREFIX_MANAGER ist set by default 00095 map_id = OWLImportExport.IAS_MAP; 00096 } 00097 System.out.println("Using map id: " + map_id); 00098 00099 OWLOntology owlmap = export.createOWLMapDescription(map_id, semMapObj2MapObj(map_id, req.map.objects)); 00100 res.owlmap = OWLFileUtils.saveOntologytoString(owlmap, owlmap.getOWLOntologyManager().getOntologyFormat(owlmap)); 00101 00102 } 00103 00104 return res; 00105 } 00106 } 00107 00108 00109 private ArrayList<MapObject> semMapObj2MapObj(String map_id, ArrayList<SemMapObject> smos) { 00110 00111 HashMap<Integer, MapObject> intIdToID = new HashMap<Integer, MapObject>(); 00112 ArrayList<MapObject> mos = new ArrayList<MapObject>(); 00113 00114 for(SemMapObject smo : smos) { 00115 00116 MapObject mo = new MapObject(); 00117 00118 mo.id = smo.type + smo.id; 00119 intIdToID.put(smo.id, mo); 00120 00121 mo.types.add(smo.type); 00122 00123 mo.dimensions.x=smo.width; 00124 mo.dimensions.y=smo.depth; 00125 mo.dimensions.z=smo.height; 00126 00127 for(int i=0;i<4;i++) { 00128 for(int j=0;j<4;j++) { 00129 mo.pose_matrix.setElement(i, j, smo.pose[4*i+j]); 00130 } 00131 } 00132 00133 if(intIdToID.get(smo.partOf) != null) 00134 intIdToID.get(smo.partOf).physicalParts.add(mo); 00135 00136 mos.add(mo); 00137 } 00138 00139 return mos; 00140 } 00141 00142 00143 00144 public static void main(String[] args) { 00145 00146 00147 // if (args.length == 0) { 00148 // run service 00149 new SemanticMapToOWL(); 00150 00151 // } else { 00152 // System.out.println("usage: rosrun mod_semantic_map SemanticMapToOWL"); 00153 // System.out.println("Commands:"); 00154 // System.out.println(" rosrun mod_semantic_map SemanticMapToOWL Runs the service"); 00155 // System.out.println(); 00156 // } 00157 00158 } 00159 00160 00161 }