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 package roboearth.wp5.util;
00051
00052 import java.io.BufferedOutputStream;
00053 import java.io.BufferedReader;
00054 import java.io.File;
00055 import java.io.FileInputStream;
00056 import java.io.FileOutputStream;
00057 import java.io.IOException;
00058 import java.io.InputStreamReader;
00059 import java.util.ArrayList;
00060 import java.util.Set;
00061 import java.util.zip.ZipEntry;
00062 import java.util.zip.ZipInputStream;
00063 import java.util.zip.ZipOutputStream;
00064
00065 import org.semanticweb.owlapi.apibinding.OWLManager;
00066 import org.semanticweb.owlapi.io.RDFXMLOntologyFormat;
00067 import org.semanticweb.owlapi.model.AddImport;
00068 import org.semanticweb.owlapi.model.IRI;
00069 import org.semanticweb.owlapi.model.OWLDataFactory;
00070 import org.semanticweb.owlapi.model.OWLImportsDeclaration;
00071 import org.semanticweb.owlapi.model.OWLOntology;
00072 import org.semanticweb.owlapi.model.OWLOntologyCreationException;
00073 import org.semanticweb.owlapi.model.OWLOntologyManager;
00074
00075 import roboearth.wp5.conn.REConnectionHadoop;
00076 import roboearth.wp5.owl.IRIDepot;
00077 import ros.pkg.re_msgs.msg.RosFile;
00078
00088 public class Util {
00089
00090 public final static String modelDir;
00091 public final static String tmpDir;
00092 public final static String re_commDir;
00093 static {
00094 re_commDir = Util.getLocalRosPackagePath("re_comm");
00095 if (re_commDir == null) {
00096 System.out.println("Couldn't find local path of re_comm package. "+
00097 "(rospack problem?)");
00098 System.exit(1);
00099 }
00100 tmpDir = re_commDir + "tmp/";
00101 modelDir = re_commDir + "models/";
00102 }
00103
00109 public static String getURIforService(String service) {
00110
00111 try {
00112
00113 String rosservice = System.getenv().get("ROS_ROOT")+"/bin/rosservice";
00114 Process p = new ProcessBuilder( rosservice, "uri", service ).start();
00115
00116 StringBuilder sb = new StringBuilder();
00117 String line;
00118
00119 try {
00120 BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream(), "UTF-8"));
00121 while ((line = reader.readLine()) != null) {
00122 sb.append(line).append("\n");
00123 }
00124 } finally {
00125 p.getInputStream().close();
00126 }
00127 return sb.toString();
00128
00129 } catch (IOException e) {
00130 e.printStackTrace();
00131 }
00132 return "";
00133
00134 }
00135
00143 public static boolean sshCopyFromRemote(String remotefile, String remoteHost, String localdir) {
00144
00145 boolean ok = false;
00146
00147
00148 String uri = Util.getURIforService("/re_comm/set_object");
00149 int index = uri.lastIndexOf(":");
00150 String localHostname = (index > 9) ? uri.substring(9, index):uri.substring(9);
00151
00152 try {
00153
00154 Process p;
00155 if (remoteHost.equals("localhost") || localHostname.equals(remoteHost)) {
00156 p = new ProcessBuilder( "/bin/cp", remotefile, localdir ).start();
00157 } else {
00158 p = new ProcessBuilder( "/usr/bin/scp", remoteHost+":"+remotefile, localdir ).start();
00159 }
00160
00161 try {
00162 BufferedReader reader = new BufferedReader(new InputStreamReader(p.getErrorStream(), "UTF-8"));
00163
00164 String line;
00165 while ((line = reader.readLine()) != null) {
00166 System.err.println(line);
00167 }
00168
00169 ok = true;
00170
00171 } finally {
00172 p.getInputStream().close();
00173 }
00174
00175 } catch (Exception e) {
00176 e.printStackTrace(System.err);
00177 System.exit(2);
00178 }
00179
00180 return ok;
00181
00182 }
00183
00191 public static boolean sshCopyToRemote(String localfile, String remoteHost, String remotedir) {
00192
00193 boolean ok = false;
00194
00195
00196 String uri = Util.getURIforService("/re_comm/get_object");
00197 int index = uri.lastIndexOf(":");
00198 String localHostname = (index > 9) ? uri.substring(9, index):uri.substring(9);
00199
00200 try {
00201 String line;
00202
00203 Process copy_file;
00204 if (remoteHost.equals("localhost") || localHostname.equals(remoteHost)) {
00205 copy_file = new ProcessBuilder( "/bin/cp", localfile, remotedir ).start();
00206 } else {
00207 copy_file = new ProcessBuilder( "/usr/bin/scp", localfile, remoteHost+":"+remotedir ).start();
00208 }
00209
00210 try {
00211 BufferedReader errors = new BufferedReader(new InputStreamReader(copy_file.getErrorStream(), "UTF-8"));
00212
00213 while ((line = errors.readLine()) != null) {
00214 System.err.println(line);
00215 }
00216
00217 ok = true;
00218
00219 } finally {
00220 copy_file.getInputStream().close();
00221 }
00222 }
00223 catch (IOException e) {
00224 e.printStackTrace(System.err);
00225 System.exit(2);
00226 }
00227
00228 return ok;
00229
00230 }
00231
00240 public static String getRemoteRosPackagePath(String hostname, String rosPackageName) {
00241
00242 String dir = null;
00243
00244 try {
00245
00246
00247 String line;
00248 Process get_path = new ProcessBuilder( "/usr/bin/ssh", hostname, "bash -i -c 'rospack find "+rosPackageName+"'" ).start();
00249
00250 try {
00251 BufferedReader pathreader = new BufferedReader(new InputStreamReader(get_path.getInputStream(), "UTF-8"));
00252
00253 if( (line = pathreader.readLine()) != null) {
00254 dir = line+File.separator;
00255 }
00256 } finally {
00257 if (get_path != null) {
00258 get_path.getInputStream().close();
00259 }
00260 }
00261
00262 } catch (IOException e) {
00263 e.printStackTrace(System.err);
00264 }
00265
00266 return dir;
00267
00268 }
00269
00276 public static String getLocalRosPackagePath(String rosPackageName) {
00277
00278 String dir = null;
00279
00280 try {
00281
00282
00283 String line;
00284 Process get_path = new ProcessBuilder( "rospack", "find", rosPackageName).start();
00285
00286 try {
00287 BufferedReader pathreader = new BufferedReader(new InputStreamReader(get_path.getInputStream(), "UTF-8"));
00288
00289 if( (line = pathreader.readLine()) != null) {
00290 dir = line+File.separator;
00291 }
00292 } finally {
00293 if (get_path != null) {
00294 get_path.getInputStream().close();
00295 }
00296 }
00297
00298 } catch (IOException e) {
00299 e.printStackTrace(System.err);
00300 }
00301
00302 return dir;
00303
00304 }
00305
00313 public static String getClassFromUID(String uid) {
00314
00315 String cls = null;
00316
00317 if (uid != null) {
00318 int index = uid.lastIndexOf(".");
00319 if (index > 0) {
00320 cls = uid.substring(0,index);
00321 } else {
00322 cls = "";
00323 }
00324 }
00325
00326 return cls;
00327
00328 }
00329
00337 public static String getIDFromUID(String uid) {
00338
00339 String id = null;
00340
00341 if (uid != null) {
00342 id = uid.substring(uid.lastIndexOf(".")+1);
00343 }
00344
00345 return id;
00346
00347 }
00348
00355 public static void createZipFromFiles(Set<String> filenames, String target) {
00356
00357 byte[] buffer = new byte[1024];
00358 try {
00359
00360 int size;
00361 FileInputStream in;
00362 ZipOutputStream out = new ZipOutputStream(new FileOutputStream(target));
00363
00364
00365 for (String f : filenames) {
00366
00367 in = new FileInputStream(f);
00368 out.putNextEntry(new ZipEntry(new File(f).getName()));
00369
00370 while ((size = in.read(buffer)) > 0) {
00371 out.write(buffer, 0, size);
00372 }
00373
00374 out.closeEntry();
00375 in.close();
00376 }
00377 out.close();
00378
00379 } catch (IOException e) {
00380 e.printStackTrace();
00381 }
00382
00383 }
00384
00391 public static boolean extractZipFile(String filename, String targetFolder) {
00392
00393 boolean ok = false;
00394
00395 try {
00396
00397 byte[] buffer = new byte[1024];
00398 ZipInputStream in = new ZipInputStream(new FileInputStream(filename));
00399 ZipEntry zipentry = in.getNextEntry();
00400
00401 while (zipentry != null) {
00402
00403 File newFile = new File(zipentry.getName());
00404 String directory = newFile.getParent();
00405
00406 if(directory == null) {
00407 if(newFile.isDirectory())
00408 break;
00409 }
00410
00411 FileOutputStream out = new FileOutputStream(targetFolder+zipentry.getName());
00412
00413 int n;
00414 while ((n = in.read(buffer, 0, 1024)) > -1)
00415 out.write(buffer, 0, n);
00416
00417 out.close();
00418 in.closeEntry();
00419 zipentry = in.getNextEntry();
00420 }
00421 in.close();
00422 ok = true;
00423 } catch (Exception e) {
00424 e.printStackTrace();
00425 }
00426
00427 return ok;
00428
00429 }
00430
00440 public static String getSimpleFilename(String longFilename) {
00441
00442 if (longFilename == null) {
00443 return null;
00444 } else {
00445 return longFilename.substring(longFilename.lastIndexOf(File.separator)+1, longFilename.length());
00446 }
00447
00448 }
00449
00457 public static String getFilenameFromURL(String url) {
00458
00459 if (url == null) {
00460 return null;
00461 } else {
00462 return url.substring(url.lastIndexOf("/")+1, url.length());
00463 }
00464
00465 }
00466
00477 public static boolean deleteFolderRec(File path, boolean alsoDeleteGivenFolder) {
00478
00479 boolean ok;
00480
00481 if (path.exists()) {
00482 ok = true;
00483 if (path.isDirectory()) {
00484 File[] files = path.listFiles();
00485 for (int i=0; i<files.length; i++) {
00486 if(files[i].isDirectory()) {
00487 deleteFolderRec(files[i], true);
00488 } else {
00489 files[i].delete();
00490 }
00491 }
00492 if (alsoDeleteGivenFolder) {
00493 ok = ok && path.delete();
00494 }
00495 }
00496 } else {
00497 ok = false;
00498 }
00499
00500 return ok;
00501 }
00502
00503 public static double[] matrixToQuaternion(float[] m) {
00504
00505 double[] res = new double[4];
00506
00507
00508 res[0] = Math.sqrt(1.0 + m[0] + m[5] + m[10]) / 2.0;
00509
00510 double w4 = (4.0 * res[0]);
00511 res[1] = (m[9] - m[6]) / w4 ;
00512 res[2] = (m[2] - m[8]) / w4 ;
00513 res[3] = (m[4] - m[1]) / w4 ;
00514
00515 return res;
00516 }
00517
00527 public static boolean writeRosFile(String targetPath, RosFile content) {
00528
00529 boolean ok = false;
00530
00531 if (content != null && targetPath != null && targetPath.length() > 0) {
00532 ok = writeFile(targetPath, content.name, content.data);
00533 }
00534
00535 return ok;
00536
00537 }
00538
00539 public static boolean writeFile(String targetPath, String filename, byte[] content) {
00540
00541 boolean ok = false;
00542
00543 if (content != null && targetPath != null && targetPath.length() > 0
00544 && filename != null && filename.length() > 0) {
00545
00546 BufferedOutputStream bos = null;
00547
00548 if (!targetPath.endsWith(File.separator)) {
00549 targetPath += File.separator;
00550 }
00551
00552 try {
00553 FileOutputStream fos;
00554 fos = new FileOutputStream(new File(targetPath+filename));
00555 bos = new BufferedOutputStream(fos);
00556 bos.write(content);
00557 bos.flush();
00558 ok = true;
00559 } catch (Exception e) {
00560 e.printStackTrace();
00561 } finally {
00562 if (bos != null) {
00563 try {
00564 bos.close();
00565 } catch (Exception e) {
00566 }
00567 }
00568 }
00569
00570 }
00571
00572 return ok;
00573
00574 }
00575
00580 public static void main(String[] args) {
00581
00582 if (args.length < 1 || args.length > 1) {
00583 System.out.println("\nUsage:\n" +
00584 "This test program assumes exactly one argument, which " +
00585 "is the API key for the RoboEarthDB interface.\nYou " +
00586 "may get yours at:\n" +
00587 "http://roboearth.informatik.uni-stuttgart.de\n" +
00588 "If no valid API key is given, reading from the " +
00589 "RoboEarthDB is still possible, but submitting data " +
00590 "will fail.\n" +
00591 "\nExample:\n" +
00592 "rosrun re_comm test XYZ1234567890XYZ\n");
00593 return;
00594 }
00595
00596 String key = args[0];
00597
00598 REConnectionHadoop re = new REConnectionHadoop(key);
00599
00600
00601 OWLOntology ontology = null;
00602
00603 try{
00604
00605 OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
00606 OWLDataFactory factory = manager.getOWLDataFactory();
00607
00608
00609 ontology = manager.createOntology(IRI.create(IRIDepot.ROBOEARTH));
00610 manager.setOntologyFormat(ontology, new RDFXMLOntologyFormat());
00611
00612
00613 OWLImportsDeclaration oid = factory.getOWLImportsDeclaration(IRI.create(IRIDepot.KNOWROB));
00614 AddImport addImp = new AddImport(ontology,oid);
00615 manager.applyChange(addImp);
00616 } catch(OWLOntologyCreationException e) {e.printStackTrace();}
00617
00618
00619
00620 long sleepTime = 200;
00621
00622 try {
00623
00624 String id ="recipe1";
00625 String cls = "testrecipe";
00626 String uid = cls+"."+id;
00627
00628
00629 System.out.println("\nTesting recipes:");
00630 re.deleteActionRecipe(uid);
00631 Thread.sleep(sleepTime);
00632 re.submitActionRecipe(ontology, cls, id, "submitted recipe");
00633 Thread.sleep(sleepTime);
00634 re.updateActionRecipe(uid, ontology, "updated recipe");
00635 Thread.sleep(sleepTime);
00636 re.requestActionRecipe(uid);
00637 Thread.sleep(sleepTime);
00638 re.deleteActionRecipe(uid);
00639 Thread.sleep(sleepTime);
00640
00641 id = "object1";
00642 cls = "testobject";
00643 uid = cls+"."+id;
00644
00645
00646 System.out.println("\nTesting objects:");
00647 re.deleteObject(uid);
00648 Thread.sleep(sleepTime);
00649 re.submitObject(ontology, cls, id, "submitted object", null);
00650 Thread.sleep(sleepTime);
00651 re.updateObject(uid, ontology, "updated object description");
00652 Thread.sleep(sleepTime);
00653
00654 ArrayList<String> outFilenames = new ArrayList<String>();
00655 ArrayList<String> outFileURLs = new ArrayList<String>();
00656 String res = re.requestObject(uid, outFilenames, outFileURLs);
00657 if (res != null) {
00658 for (String filename : outFilenames) {
00659 re.requestObjectBinaryFile(uid, filename);
00660 }
00661 }
00662 Thread.sleep(sleepTime);
00663 re.deleteObject(uid);
00664 Thread.sleep(sleepTime);
00665
00666 id = "map1";
00667 cls = "testenv";
00668 uid = cls+"."+id;
00669
00670
00671 System.out.println("\nTesting environments:");
00672 re.deleteEnvironment(uid);
00673 Thread.sleep(sleepTime);
00674 re.submitEnvironment(ontology, cls, id, "submitted environemnt");
00675 Thread.sleep(sleepTime);
00676 re.updateEnvironment(uid, ontology, "updated environment");
00677 Thread.sleep(sleepTime);
00678 re.requestEnvironment(uid);
00679 Thread.sleep(sleepTime);
00680 re.deleteEnvironment(uid);
00681 System.out.println();
00682
00683 } catch (InterruptedException e) {
00684 e.printStackTrace();
00685 }
00686
00687 String service = "/re_comm/get_action_recipe";
00688 String uri = Util.getURIforService(service);
00689 if (uri == null || uri.length()==0) {
00690 uri = "(re_comm must be running)";
00691 }
00692 System.out.println("URI for service '"+service+"':\n"+uri);
00693
00694 }
00695
00696 }