Advertisement
Guest User

addsjunior

a guest
Oct 14th, 2009
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.41 KB | None | 0 0
  1. package br.edu.ufcg.mdaveritas.mofscriptExecutor;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.Iterator;
  6.  
  7. import org.eclipse.emf.common.util.URI;
  8. import org.eclipse.emf.ecore.EObject;
  9. import org.eclipse.emf.ecore.EPackage;
  10. import org.eclipse.emf.ecore.resource.Resource;
  11. import org.eclipse.emf.ecore.resource.ResourceSet;
  12. import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
  13. import org.eclipse.emf.ecore.xmi.XMIResource;
  14. import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
  15. import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl;
  16. import org.eclipse.mofscript.parser.MofScriptParseError;
  17. import org.eclipse.mofscript.parser.ParserUtil;
  18. import org.eclipse.mofscript.runtime.ExecutionManager;
  19. import org.eclipse.mofscript.runtime.ExecutionMessageListener;
  20. import org.eclipse.mofscript.runtime.MofScriptExecutionException;
  21.  
  22. /**
  23.  * @author Antonio Dias dos Santos Junior
  24.  *
  25.  */
  26. public class MOFScriptExecutor implements ExecutionMessageListener {
  27.  
  28.     private String compilepath;
  29.     private String metamodel;
  30.     private String metamodelname;
  31.     private String model;
  32.     private String transformation;
  33.     private String outputdir;
  34.  
  35.     private String getMetamodelname() {
  36.         return metamodelname;
  37.     }
  38.  
  39.     private void setMetamodelname(String metamodelname) {
  40.         this.metamodelname = metamodelname;
  41.     }
  42.  
  43.     private String getCompilepath() {
  44.         return compilepath;
  45.     }
  46.  
  47.     private void setCompilepath(String compilepath) {
  48.         this.compilepath = compilepath;
  49.     }
  50.  
  51.     private String getMetamodel() {
  52.         return metamodel;
  53.     }
  54.  
  55.     private void setMetamodel(String metamodel) {
  56.         this.metamodel = metamodel;
  57.     }
  58.  
  59.     private String getModel() {
  60.         return model;
  61.     }
  62.  
  63.     private void setModel(String model) {
  64.         this.model = model;
  65.     }
  66.  
  67.     private String getTransformation() {
  68.         return transformation;
  69.     }
  70.  
  71.     private void setTransformation(String transformation) {
  72.         this.transformation = transformation;
  73.     }
  74.  
  75.     private String getOutputdir() {
  76.         return outputdir;
  77.     }
  78.  
  79.     private void setOutputdir(String outputdir) {
  80.         this.outputdir = outputdir;
  81.     }
  82.  
  83.     /**
  84.      * Sets the parameters necessary to the transformation.
  85.      *
  86.      * @param compilepath
  87.      *            the compile path
  88.      * @param metamodel
  89.      *            the path to the metamodel file
  90.      * @param metamodelname
  91.      *            the name used to identify the metamodel in the MOFScript
  92.      *            transformation
  93.      * @param model
  94.      *            the path to the model file
  95.      * @param transformation
  96.      *            the path to the transformation file
  97.      * @param outputdir
  98.      *            the output directory, where the code will be generated
  99.      */
  100.     public void setParameters(String compilepath, String metamodel,
  101.             String metamodelname, String model, String transformation,
  102.             String outputdir) {
  103.         setCompilepath(compilepath);
  104.         setMetamodel(metamodel);
  105.         setMetamodelname(metamodelname);
  106.         setModel(model);
  107.         setTransformation(transformation);
  108.         setOutputdir(outputdir);
  109.     }
  110.  
  111.     private void makeTransformation() throws MofScriptExecutionException,
  112.             IOException {
  113.         ParserUtil parserUtil = new ParserUtil();
  114.         ExecutionManager execMgr = ExecutionManager.getExecutionManager();
  115.  
  116.         execMgr.lookupAndAddSourceMetaModel("uml","http://www.eclipse.org/uml2/2.1.0/UML");
  117.  
  118.         parserUtil.setCompilePath(getCompilepath());
  119.  
  120.         parserUtil.parse(new File(getTransformation()), true);
  121.  
  122.         // check for errors:
  123.         int errorCount = ParserUtil.getModelChecker().getErrorCount();
  124.         Iterator<?> errorIt = ParserUtil.getModelChecker().getErrors();
  125.         writeErroLog(errorCount, errorIt);
  126.  
  127.         execMgr.setRootDirectory(getOutputdir());
  128.  
  129.         registryFactoryToJAS();
  130.  
  131.         executeTransformation(execMgr, getModel());
  132.     }
  133.  
  134.     private EObject loadSourceModel(String path) {
  135.         XMIResourceFactoryImpl _xmiFac = new XMIResourceFactoryImpl();
  136.         EObject sourceModel = null;
  137.         File sourceModelFile = new File(path);
  138.         ResourceSet rSet = new ResourceSetImpl();
  139.         rSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("*",
  140.                 _xmiFac);
  141.         URI uri = URI.createFileURI(sourceModelFile.getAbsolutePath());
  142.         Resource resource = rSet.getResource(uri, true);
  143.  
  144.         if (resource != null) {
  145.             if (resource.getContents().size() > 0) {
  146.                 sourceModel = (EObject) resource.getContents().get(0);
  147.             }
  148.         }
  149.         return sourceModel;
  150.     }
  151.  
  152.     private void executeTransformation(ExecutionManager execMgr, String fileLoad)
  153.             throws MofScriptExecutionException {
  154.         // load source model
  155.         EObject sourceModel = loadSourceModel(fileLoad);
  156.  
  157.         // set the source model for the execution manager
  158.         execMgr.addSourceModel(sourceModel);
  159.  
  160.         // if true, files are not generated to the file system, but populated
  161.         // into a filemodel which can be fetched afterwards.
  162.         // Value false will result in standard file generation
  163.         execMgr.setUseFileModel(false);
  164.         // Turns on/off system logging
  165.         execMgr.setUseLog(false);
  166.         // Adds an output listener for the transformation execution.
  167.         execMgr.getExecutionStack().addOutputMessageListener(this);
  168.         execMgr.executeTransformation();
  169.         execMgr.clearModelTypeMap();
  170.     }
  171.  
  172.     private void registryFactoryToJAS() {
  173.         Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(
  174.                 "txt", new XMIResourceFactoryImpl() {
  175.                     public Resource createResource(URI uri) {
  176.                         XMIResource xmiResource = new XMIResourceImpl(uri);
  177.                         return xmiResource;
  178.                     }
  179.                 });
  180.     }
  181.  
  182.     private void writeErroLog(int errorCount, Iterator<?> errorIt) {
  183.         System.out.println("Parsing result: " + errorCount + " errors");
  184.         if (errorCount > 0) {
  185.             for (; errorIt.hasNext();) {
  186.                 MofScriptParseError parseError = (MofScriptParseError) errorIt
  187.                         .next();
  188.                 System.out.println("\t \t: Error: " + parseError.toString());
  189.             }
  190.             return;
  191.         }
  192.     }
  193.  
  194.     public void executionMessage(String type, String description) {
  195.         System.out.println(type + " - " + description);
  196.     }
  197.  
  198.     /**
  199.      * Executes the transformation.
  200.      */
  201.     public void executeTransformations() {
  202.         try {
  203.             makeTransformation();
  204.         } catch (MofScriptExecutionException e) {
  205.             new RuntimeException(e);
  206.         } catch (IOException e) {
  207.             new RuntimeException(e);
  208.         }
  209.     }
  210.  
  211.     public static void main(String[] args) {
  212.         MOFScriptExecutor mof = new MOFScriptExecutor();
  213.         mof
  214.                 .setParameters(
  215.                         "D:\\workspace\\MOFScriptExec",
  216.                         "",
  217.                         "uml", "D:\\workspace\\UML2Alloy\\My.uml",
  218.                         "D:\\workspace\\UML2Alloy\\UML2Alloy.m2t",
  219.                         "D:\\workspace\\MOFScriptExec");
  220.         mof.executeTransformations();
  221.     }
  222. }
  223.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement