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 package roboearth.wp1;
00050
00051 import java.io.File;
00052 import java.io.FileInputStream;
00053 import java.io.FileOutputStream;
00054 import java.io.IOException;
00055 import java.text.SimpleDateFormat;
00056 import java.util.ArrayList;
00057 import java.util.Date;
00058 import java.util.HashSet;
00059 import java.util.Set;
00060 import java.util.zip.ZipEntry;
00061 import java.util.zip.ZipOutputStream;
00062
00063 import org.semanticweb.owlapi.apibinding.OWLManager;
00064 import org.semanticweb.owlapi.io.RDFXMLOntologyFormat;
00065 import org.semanticweb.owlapi.model.AddImport;
00066 import org.semanticweb.owlapi.model.IRI;
00067 import org.semanticweb.owlapi.model.OWLClass;
00068 import org.semanticweb.owlapi.model.OWLDataFactory;
00069 import org.semanticweb.owlapi.model.OWLDataProperty;
00070 import org.semanticweb.owlapi.model.OWLImportsDeclaration;
00071 import org.semanticweb.owlapi.model.OWLNamedIndividual;
00072 import org.semanticweb.owlapi.model.OWLObjectProperty;
00073 import org.semanticweb.owlapi.model.OWLOntology;
00074 import org.semanticweb.owlapi.model.OWLOntologyManager;
00075 import org.semanticweb.owlapi.util.DefaultPrefixManager;
00076
00077 import roboearth.wp5.conn.REConnectionHadoop;
00078 import roboearth.wp5.owl.IRIDepot;
00079 import roboearth.wp5.util.Util;
00080 import ros.NodeHandle;
00081 import ros.Publisher;
00082 import ros.Ros;
00083 import ros.RosException;
00084 import ros.ServiceClient;
00085 import ros.ServiceServer;
00086 import ros.pkg.re_srvs.srv.RoboEarthExportCopModel;
00087 import ros.pkg.re_srvs.srv.RoboEarthRetrieveCopModel;
00088 import ros.pkg.vision_srvs.srv.cop_save;
00089 import edu.tum.cs.ias.knowrob.vis.applets.CommunicationVisApplet;
00090 import edu.tum.cs.ias.knowrob.CopROSClient;
00091
00092
00106 public class UnizarRoboEarthInterface {
00107
00111 protected Ros ros;
00112
00116 protected NodeHandle n;
00117
00121 protected REConnectionHadoop re_interface;
00122
00123 protected SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd_HH-mm-ss-SSS");
00124
00131 public UnizarRoboEarthInterface(Ros ros, NodeHandle n) throws RosException {
00132
00133 this.ros = ros;
00134 this.n = n;
00135
00136 this.re_interface = new REConnectionHadoop("6e6574726f6d40b699e442ebdca5850e7cb7486679768aec3c70");
00137
00138 n.advertiseService("/re_cop_interface/exportCopModel", new RoboEarthExportCopModel(), new ExportCopModelCallback());
00139 n.advertiseService("/re_cop_interface/retrieveCopModelByName", new RoboEarthRetrieveCopModel(), new RetrieveCopModelCallback());
00140
00141 }
00142
00148 protected OWLOntology buildOWLObjectDescription(String modelType, String objClass) {
00149
00150 OWLOntology ontology = null;
00151 try {
00152
00153
00154 OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
00155 OWLDataFactory factory = manager.getOWLDataFactory();
00156
00157
00158 DefaultPrefixManager pm = IRIDepot.PREFIX_MANAGER;
00159
00160
00161 ontology = manager.createOntology(IRI.create(IRIDepot.ROBOEARTH));
00162 manager.setOntologyFormat(ontology, new RDFXMLOntologyFormat());
00163
00164
00165 OWLImportsDeclaration oid = factory.getOWLImportsDeclaration(IRI.create(IRIDepot.KNOWROB));
00166 AddImport addImp = new AddImport(ontology,oid);
00167 manager.applyChange(addImp);
00168
00169
00170 OWLClass clsRoboEarthObjRecModelPlanar = factory.getOWLClass(IRIDepot.ROBOEARTH_OBJ_REC_MODEL_PLANAR, pm);
00171 OWLClass clsClass = factory.getOWLClass(IRIDepot.OWL_CLASS, pm);
00172
00173
00174 OWLNamedIndividual clsKRCup = factory.getOWLNamedIndividual(IRIDepot.KNOWROB_CUP, pm);
00175 manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(clsClass, clsKRCup));
00176 OWLNamedIndividual clsRECup = factory.getOWLNamedIndividual(IRIDepot.ROBOEARTH_CUP, pm);
00177 manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(clsClass, clsRECup));
00178
00179
00180
00181 OWLObjectProperty objSubClassOf = factory.getOWLObjectProperty(IRIDepot.RDFS_SUB_CLASS_OF, pm);
00182 manager.addAxiom(ontology, factory.getOWLObjectPropertyAssertionAxiom(objSubClassOf, clsRECup, clsKRCup));
00183
00184 OWLNamedIndividual objModelInst = factory.getOWLNamedIndividual("ObjModelWorkshop0710", pm);
00185 manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(clsRoboEarthObjRecModelPlanar, objModelInst));
00186
00187
00188 OWLObjectProperty objProvidesModelFor = factory.getOWLObjectProperty(IRIDepot.ROBOEARTH_PROVIDES_MODEL_FOR, pm);
00189
00190 manager.addAxiom(ontology, factory.getOWLObjectPropertyAssertionAxiom(objProvidesModelFor, objModelInst, clsRECup));
00191
00192
00193
00194 OWLDataProperty dataLinkToRecMod = factory.getOWLDataProperty(IRIDepot.ROBOEARTH_LINK_TO_RECOGNITION_MODEL, pm);
00195 OWLDataProperty dataLinkToImgData = factory.getOWLDataProperty(IRIDepot.ROBOEARTH_LINK_TO_IMAGE_DATA, pm);
00196 OWLDataProperty dataCreationDate = factory.getOWLDataProperty(IRIDepot.ROBOEARTH_CREATION_DATE_TIME, pm);
00197
00198
00199
00200 manager.addAxiom(ontology, factory.getOWLDataPropertyAssertionAxiom(dataCreationDate, objModelInst, ""));
00201 manager.addAxiom(ontology, factory.getOWLDataPropertyAssertionAxiom(dataLinkToRecMod, objModelInst, ""));
00202 manager.addAxiom(ontology, factory.getOWLDataPropertyAssertionAxiom(dataLinkToImgData, objModelInst, ""));
00203
00204
00205 } catch (Exception e) {
00206 ontology = null;
00207 e.printStackTrace();
00208 }
00209
00210 return ontology;
00211
00212 }
00213
00214
00215
00222 public static void createZipFromFiles(String xmlFile, Set<String> filenames, String target) {
00223
00224 byte[] buffer = new byte[1024];
00225 try {
00226
00227 ZipOutputStream out = new ZipOutputStream(new FileOutputStream(target));
00228
00229 FileInputStream in = new FileInputStream(xmlFile);
00230 out.putNextEntry(new ZipEntry(new File(xmlFile).getName()));
00231
00232 int size;
00233 while ((size = in.read(buffer)) > 0) {
00234 out.write(buffer, 0, size);
00235 }
00236
00237 out.closeEntry();
00238 in.close();
00239
00240
00241 for (String f : filenames) {
00242
00243 in = new FileInputStream(f);
00244 out.putNextEntry(new ZipEntry(new File(f).getName()));
00245
00246 while ((size = in.read(buffer)) > 0) {
00247 out.write(buffer, 0, size);
00248 }
00249
00250 out.closeEntry();
00251 in.close();
00252 }
00253 out.close();
00254
00255 } catch (IOException e) {
00256 e.printStackTrace();
00257 }
00258
00259 }
00260
00261
00262
00272 class ExportCopModelCallback implements ServiceServer.Callback<RoboEarthExportCopModel.Request, RoboEarthExportCopModel.Response> {
00273
00274 @Override
00275 public RoboEarthExportCopModel.Response call(RoboEarthExportCopModel.Request req) {
00276
00277 RoboEarthExportCopModel.Response res = new RoboEarthExportCopModel.Response();
00278 res.success = 0;
00279
00280 if (req.object_id >0 ) {
00281
00282
00283
00284 CommunicationVisApplet.visualizeCommunication("Retrieving model from the vision system... \n\nSending: /cop/save "+req.object_id, "", null, "cop.png");
00285
00286
00287 try {
00288 Thread.sleep(1500);
00289 } catch (InterruptedException e) {
00290 e.printStackTrace();
00291 }
00292
00293 ServiceClient<cop_save.Request, cop_save.Response, cop_save> client = n.serviceClient("/cop/save", new ros.pkg.vision_srvs.srv.cop_save());
00294 cop_save.Request rq = new cop_save().createRequest();
00295 rq.object_id=req.object_id;
00296 String modelFile = null;
00297 String imageFile = null;
00298
00299
00300 try {
00301
00302 cop_save.Response resp = client.call(rq);
00303
00304 String vis_string = resp.xmlfilename+"\n";
00305 String remoteXmlFile = resp.xmlfilename;
00306 File remoteParent = new File(remoteXmlFile).getParentFile();
00307 String remoteModelDir = remoteParent.getAbsolutePath()+"/";
00308 String modelName = remoteParent.getName()+"/";
00309
00310
00311 String cop_uri = Util.getURIforService("/cop/save");
00312 cop_uri = cop_uri.substring(9, cop_uri.length()-7);
00313
00314 File localModelDirFile = new File(Util.modelDir + modelName);
00315 if (localModelDirFile.exists()) {
00316 Util.deleteFolderRec(localModelDirFile, false);
00317 } else {
00318 localModelDirFile.mkdir();
00319 }
00320 String localModelDir = localModelDirFile.getAbsolutePath()+"/";
00321
00322 Util.sshCopyFromRemote(remoteXmlFile, cop_uri, localModelDir);
00323 HashSet<String> filenames = new HashSet<String>();
00324 for(String f:resp.filenames) {
00325 Util.sshCopyFromRemote(remoteModelDir+f, cop_uri, localModelDir);
00326
00327 filenames.add(localModelDir+"/"+f);
00328 vis_string+=f+"\n";
00329
00330
00331 if(f.toLowerCase().endsWith(".png") || f.toLowerCase().endsWith(".jpg")) {
00332 imageFile = localModelDir + f;
00333 }
00334 }
00335 CommunicationVisApplet.visualizeCommunication("", vis_string, null, null);
00336
00337
00338 modelFile = localModelDir + "re_obj_model_"+sdf.format(new Date())+".zip";
00339 createZipFromFiles(localModelDir + new File(remoteXmlFile).getName(), filenames, modelFile);
00340
00341
00342 String[] model_info = CopROSClient.copModelTypeSemClassForID(req.object_id);
00343
00344
00345 OWLOntology objectmodel = buildOWLObjectDescription(model_info[0], model_info[1]);
00346
00347
00348
00349 try {
00350 Thread.sleep(1500);
00351 } catch (InterruptedException e) {
00352 e.printStackTrace();
00353 }
00354
00355
00356
00357
00358
00359 String uid = "";
00360 if(model_info[1].equals("cabinet1")) {
00361 uid="cabinetrecmodel";
00362 } else if(model_info[1].equals("bed1")) {
00363 uid="bedrecmodel";
00364 } else if(model_info[1].equals("bottle1")) {
00365 uid="bottlerecmodel";
00366 } else {
00367 uid=model_info[1];
00368 }
00369
00370 ArrayList<File> fileList = new ArrayList<File>();
00371 if (imageFile != null) {
00372 fileList.add(new File(imageFile));
00373 }
00374 if (modelFile != null) {
00375 fileList.add(new File(modelFile));
00376 }
00377
00378 if(re_interface.submitObject(objectmodel, uid, uid, "Cop object id: "+req.object_id, fileList)) {
00379 res.success = 1;
00380 } else {
00381 System.err.println("ERROR: Failed to upload object model.");
00382 }
00383
00384
00385
00386
00387 } catch (Exception e) {
00388
00389 e.printStackTrace();
00390
00391 CommunicationVisApplet.visualizeCommunication("", "Export of model for Cop ID "+req.object_id+" failed.", null, null);
00392 ros.logError("CopRoboEarthInterface: Export of model for Cop ID "+req.object_id+" failed");
00393 }
00394 }
00395 return res;
00396 }
00397 }
00398
00399
00400
00401
00410 class RetrieveCopModelCallback implements ServiceServer.Callback<RoboEarthRetrieveCopModel.Request, RoboEarthRetrieveCopModel.Response> {
00411
00412 @Override
00413 public RoboEarthRetrieveCopModel.Response call(RoboEarthRetrieveCopModel.Request req) {
00414
00415 Publisher<ros.pkg.std_msgs.msg.String> pub = null;
00416 RoboEarthRetrieveCopModel.Response res = new RoboEarthRetrieveCopModel.Response();
00417 res.success = 0;
00418 if (req.object_name != null && req.object_name.length() > 0) {
00419
00420 System.err.println("Retrieving "+ req.object_name);
00421
00422 try {
00423
00424
00425 pub = n.advertise("/cop/new_signatures", new ros.pkg.std_msgs.msg.String(), 100);
00426
00427
00428 String targetFolder = Util.modelDir+"re_obj_model_"+sdf.format(new Date())+"/";
00429
00430 File targetFolderFile = new File(targetFolder);
00431 if (targetFolderFile.exists()) {
00432 Util.deleteFolderRec(targetFolderFile, false);
00433 } else {
00434 targetFolderFile.mkdir();
00435 }
00436
00437 CommunicationVisApplet.visualizeCommunication("Requesting model for object '"+req.object_name+"' from RoboEarth...", "", null, "roboearth.png");
00438
00439 ArrayList<String> outFilenames = new ArrayList<String>();
00440 ArrayList<String> outFileURLs = new ArrayList<String>();
00441
00442 String owldesc = re_interface.requestObject(req.object_name.toLowerCase(), outFilenames, outFileURLs);
00443
00444 if (owldesc !=null) {
00445
00446 res.owldata = owldesc;
00447
00448 File zipfile = null;
00449 for (String fileName : outFilenames) {
00450 if (fileName.startsWith("re_obj_model_")) {
00451 zipfile = re_interface.requestObjectBinaryFile(req.object_name.toLowerCase(), fileName, targetFolder);
00452 break;
00453 }
00454 }
00455 if (zipfile == null) {
00456 System.err.println("Error: No object detection model file found for uid '"+req.object_name.toLowerCase()+"'!");
00457 return res;
00458 }
00459
00460 CommunicationVisApplet.visualizeCommunication("", "Object model download finished.\n\n"+targetFolderFile.getName(), null, null);
00461
00462
00463 try {
00464 Thread.sleep(2000);
00465 } catch (InterruptedException e) {
00466 e.printStackTrace();
00467 }
00468
00469
00470 if (!Util.extractZipFile(zipfile.getAbsolutePath(), targetFolder)) {
00471 System.err.println("Error: Extracting files from zip file failed!");
00472 return res;
00473 } else {
00474 zipfile.delete();
00475 }
00476
00477
00478 String cop_uri = Util.getURIforService("/cop/save");
00479
00480
00481
00482 if(cop_uri.length()<16) {
00483 System.err.println("Error: Couldn't find remote computer running CoP!\n"+cop_uri);
00484 return res;
00485 }
00486 cop_uri = cop_uri.substring(9, cop_uri.length()-7);
00487
00488 String remote_cop_path = Util.getRemoteRosPackagePath(cop_uri, "re_gazebo_vslam");
00489 if (remote_cop_path == null) {
00490 System.err.println("Error: Couldn't find remote ROS package path for 're_gazebo_vslam' on '"+cop_uri+"'!");
00491 return res;
00492 } else {
00493 remote_cop_path += "/resource/";
00494 }
00495
00496 String xmlFileName = null;
00497 for (String filename : new File( targetFolder ).list()) {
00498 Util.sshCopyToRemote(targetFolder+filename, cop_uri, remote_cop_path);
00499 if(filename.toLowerCase().endsWith(".xml")) {
00500 if (xmlFileName == null) {
00501 xmlFileName = filename;
00502 } else {
00503 System.err.println("Error: More than one XML files found in zip file!");
00504 return res;
00505 }
00506
00507 }
00508 }
00509
00510 if (xmlFileName != null) {
00511 ros.pkg.std_msgs.msg.String m = new ros.pkg.std_msgs.msg.String();
00512 m.data = remote_cop_path+"/"+xmlFileName;
00513 CommunicationVisApplet.visualizeCommunication("Sending model for object '"+req.object_name+"' to the vision system...\n\n"+xmlFileName, "", null, "cop.png");
00514 pub.publish(m);
00515
00516
00517 try {
00518 Thread.sleep(1500);
00519 } catch (InterruptedException e) {
00520 e.printStackTrace();
00521 }
00522
00523 CommunicationVisApplet.visualizeCommunication("", "Object model "+xmlFileName+" received.", null, null);
00524
00525 } else {
00526 System.err.println("Error: No XML file found in zip file!");
00527 return res;
00528 }
00529
00530 res.success = 1;
00531
00532 System.err.println("published \n" + owldesc);
00533
00534
00535 } else {
00536 CommunicationVisApplet.visualizeCommunication("", "No model for "+req.object_name+" found.", null, null);
00537 ros.logWarn("CopRoboEarthInterface: No model for "+req.object_name+" found");
00538 }
00539
00540 } catch(Exception e) {
00541 e.printStackTrace();
00542 } finally {
00543 if (pub != null) {
00544 pub.shutdown();
00545 }
00546 }
00547 } else {
00548 CommunicationVisApplet.visualizeCommunication("", "No model for "+req.object_name+" found.", null, null);
00549 ros.logWarn("CopRoboEarthInterface: No model for "+req.object_name+" found");
00550 }
00551 return res;
00552 }
00553
00554 }
00555
00556
00557 public static void main(String[] args) {
00558
00559
00560
00561 Ros ros = Ros.getInstance();
00562 if (!ros.isInitialized()) {
00563 ros.init("RE_Unizar_interface");
00564 }
00565 NodeHandle n = ros.createNodeHandle();
00566
00567 try {
00568
00569 new UnizarRoboEarthInterface(ros, n);
00570
00571 } catch (RosException e) {
00572
00573 ros.logFatal("Fatal error occurred. Shutting down!");
00574 n.shutdown();
00575 e.printStackTrace();
00576 return;
00577
00578 }
00579
00580 ros.spin();
00581
00582 }
00583
00584
00585 }