M2CStandalone.java
Go to the documentation of this file.
00001 package org.ros.model.compilers;
00002 
00003 import java.net.URI;
00004 import java.io.BufferedReader;
00005 import java.io.File;
00006 import java.io.IOException;
00007 import java.io.InputStreamReader;
00008 import java.net.URISyntaxException; 
00009         
00010 
00011 
00012 import org.eclipse.epsilon.egl.EglFileGeneratingTemplateFactory;
00013 import org.eclipse.epsilon.egl.EglTemplateFactoryModuleAdapter;
00014 import org.eclipse.epsilon.eol.IEolExecutableModule;
00015 import org.eclipse.epsilon.eol.exceptions.EolRuntimeException;
00016 import org.eclipse.epsilon.emc.emf.EmfModel;
00017 import org.eclipse.epsilon.eol.exceptions.models.EolModelLoadingException;
00018 import org.eclipse.epsilon.commons.util.StringProperties;
00019 //import org.ros.model.ROS_PackageStandaloneSetup;
00020 
00021 //import org.eclipse.emf.common.util.URI;
00022 import org.eclipse.epsilon.eol.dt.ExtensionPointToolNativeTypeDelegate;
00023 import org.best_of_robotics.transform.ros.to.cplusplus.tools.CHModToolNativeTypeDelegate;
00024 import org.best_of_robotics.transform.ros.to.cplusplus.tools.ConfigFileCreatorNativeTypeDelegate;
00025 
00026 
00027 
00028 
00029 
00030 public class M2CStandalone {
00031         static String output_package = "";
00032         static String input_package = "";
00033         static String model_path = "";
00034         static String language = "";
00035 
00036         
00037         /*protected static File getFile(String fileName) throws URISyntaxException {
00038                 
00039                 URI binUri = M2CStandalone.class.getResource(fileName).toURI();
00040                 URI uri = null;
00041                 
00042                 if (binUri.toString().indexOf("bin") > -1) {
00043                         uri = new URI(binUri.toString().replaceAll("bin", "src"));
00044                 }
00045                 else {
00046                         uri = binUri;
00047                 }
00048                 return new File(uri);
00049         }*/
00050         
00051         protected static EmfModel createEmfModel(String name, String model, String metamodel, boolean readOnLoad, boolean storeOnDisposal) throws EolModelLoadingException, URISyntaxException {
00052                 EmfModel emfModel = new EmfModel();
00053                 StringProperties properties = new StringProperties();
00054                 properties.put(EmfModel.PROPERTY_NAME, name);
00055                 properties.put(EmfModel.PROPERTY_FILE_BASED_METAMODEL_URI, "file:/" + new File(metamodel).getAbsolutePath());
00056                 properties.put(EmfModel.PROPERTY_MODEL_URI, "file:/" + new File(model).getAbsolutePath());
00057                 properties.put(EmfModel.PROPERTY_IS_METAMODEL_FILE_BASED, "true");
00058                 properties.put(EmfModel.PROPERTY_READONLOAD, readOnLoad + "");
00059                 properties.put(EmfModel.PROPERTY_STOREONDISPOSAL, storeOnDisposal + "");
00060                 emfModel.load(properties, null);
00061                 return emfModel;
00062         }
00063         
00069 /*      public static void convertXTextToXMI(final org.eclipse.emf.common.util.URI inputURI) {
00070                 ROS_PackageStandaloneSetup.doSetup();
00071                 if ((inputURI != null) && inputURI.isFile() && !inputURI.isEmpty()) {
00072                         final ResourceSet ressourceSet = new ResourceSetImpl();
00073                         final Resource resource = ressourceSet.getResource(inputURI, true);
00074                         final EObject eobject = resource.getContents().get(0);
00075 
00076                         final XMIResourceFactoryImpl resFactory = new XMIResourceFactoryImpl();
00077                         final org.eclipse.emf.common.util.URI xmiuri = inputURI.trimFileExtension().appendFileExtension("xmi");
00078                         final XMIResource xmiresource = (XMIResource) resFactory.createResource(xmiuri);
00079                         xmiresource.getContents().add(eobject);
00080                         try {
00081                                 xmiresource.save(new HashMap<Object, Object>());
00082                         } catch (final IOException ex) {
00083                                 System.out.println("Exception" + ex.toString());
00084                         }
00085                 } else {
00086                         System.out.println("please provide a real not empty file");
00087                 }
00088         }*/
00089         
00090         public static String findPackageURI(String packagename)
00091         {
00092                 String cmd = "rospack find " + packagename;
00093                 Runtime run = Runtime.getRuntime();
00094                 Process pr;
00095                 try {
00096                         pr = run.exec(cmd);
00097                         pr.waitFor();
00098                         BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
00099                         return buf.readLine();
00100                 } catch (IOException e) {
00101                         // TODO Auto-generated catch block
00102                         e.printStackTrace();
00103                 } catch (InterruptedException e) {
00104                         // TODO Auto-generated catch block
00105                         e.printStackTrace();
00106                 }
00107                 return "";
00108         }
00109         
00110         public static void parseArgs(String [] args)
00111         {
00112                 // -o brics_test [-p brics_test] model/brics_test.ros_package
00113    
00114             for (int i=0; i < args.length; i+=2) {
00115                  switch (args[i].charAt(0)) {
00116                  case '-':
00117                      if (args[i].charAt(1) == 'o') {
00118                          output_package = args[i+1];
00119                          break;   
00120                      } 
00121                      if (args[i].charAt(1) == 'p') {
00122                          input_package = args[i+1];
00123                          break;   
00124                      }
00125                      if (args[i].charAt(1) == 'l') {
00126                          language = args[i+1];
00127                          break;   
00128                      }
00129                      break;         
00130                  default:
00131                          model_path = args[i];
00132                          break;         
00133                  }     
00134              }
00135              if(model_path == "" || output_package == "")
00136              {
00137                  System.out.println("Usage: rosrun bride_compilers m2t -o brics_test [-l language] [-p brics_test] model/brics_test.ros_package");
00138                      return;
00139              }
00140                  
00141          } 
00142         
00143         //
00144         // rosrun bride_compilers m2t -o brics_test [-p brics_test] model/brics_test.ros_package
00145         
00146          public static void main(String[] args) {
00147 
00148                 parseArgs(args);
00149                 System.out.println("InputPackage: " + input_package);
00150             System.out.println("OutputPackage: " + output_package);
00151             System.out.println("ModelPath: " + model_path);
00152             System.out.println("Language: " + language);
00153             if(model_path != "" && output_package != "")
00154                 {
00155                 try {
00156                         // Try to find model file
00157                         String model_uri = findPackageURI(input_package) + "/" + model_path;
00158                         System.out.println("Will use model from " + model_uri);
00159                         
00160                         //Check what model we want to generate for
00161                         String ext_type = model_uri.substring(model_uri.lastIndexOf('.')+1, model_uri.length());
00162                         System.out.println("File extension is " + ext_type);
00163                         String templ_type = "";
00164                         if(ext_type.compareTo("ros_package") == 0)
00165                         {
00166                                 templ_type = "package";
00167                                 if(language.compareTo("cpp") != 0 && language.compareTo("python") != 0)
00168                                 {
00169                                         language = "cpp";
00170                                 }
00171                         }
00172                         if(ext_type.compareTo("ros_coordinator") == 0)
00173                         {
00174                                 templ_type = "coordinator";
00175                                 language = "python";
00176                         }
00177                         if(ext_type.compareTo("ros_system") == 0)
00178                                 templ_type = "system";
00179                         if(templ_type == "")
00180                         {
00181                                 System.out.println("File Extension unknown.");
00182                                 return;
00183                         }
00184                         
00185                         // Try to find template entrance point
00186                         String template_uri = findPackageURI("bride_templates") + "/" + language + "/" + templ_type + "/package.egl";
00187                         System.out.println("Will use template from " + template_uri);
00188                         
00189                         // Try to find output directory
00190                         String output_uri = findPackageURI(output_package);
00191                         System.out.println("Will generate code to " + output_uri);
00192                 
00193                         
00194                         EglFileGeneratingTemplateFactory eglTemplateFactory = new EglFileGeneratingTemplateFactory();
00195                         IEolExecutableModule module = new EglTemplateFactoryModuleAdapter(eglTemplateFactory);
00196                         System.out.println("Parsing template files...");
00197                         eglTemplateFactory.setOutputRoot(output_uri);
00198                         module.parse(new File(template_uri));
00199                         
00200                         System.out.println("Creating final model out of dsl and specific model...");
00201                         EmfModel model = createEmfModel("Model", model_uri, findPackageURI("bride_compilers")+"/bin/models/ros.ecore", true, false);
00202                                 
00203                         module.getContext().getNativeTypeDelegates().add(new CHModToolNativeTypeDelegate());
00204                         module.getContext().getNativeTypeDelegates().add(new ConfigFileCreatorNativeTypeDelegate());
00205                         
00206                                 System.out.println("Adding model to template egl engine...");
00207                         module.getContext().getModelRepository().addModel(model);
00208                         
00209                         Object result = new Object();
00210                         try {
00211                                 System.out.println("Executing engine...");
00212                                 result = module.execute();
00213                         } catch (EolRuntimeException e) {
00214                                 e.printStackTrace();
00215                         }
00216                         System.out.println(result);
00217                         System.out.println("Done.");
00218                 } catch (Throwable t) {
00219                     System.err.println("An unexpected error occured");
00220                     t.printStackTrace();
00221                 }
00222                 }
00223          }
00224         
00225 }


bride_compilers
Author(s): Alexander Bubeck
autogenerated on Sun Oct 5 2014 22:38:26