import java.io.File; import java.util.Iterator; import org.eclipse.emf.common.util.URI; import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.resource.ResourceSet; import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl; import org.eclipse.mofscript.MOFScriptModel.MOFScriptSpecification; import org.eclipse.mofscript.parser.MofScriptParseError; import org.eclipse.mofscript.parser.ParserUtil; import org.eclipse.mofscript.runtime.ExecutionManager; import org.eclipse.mofscript.runtime.ExecutionMessageListener; import org.eclipse.mofscript.runtime.MofScriptExecutionException; import org.eclipse.uml2.uml.UMLPackage; public class TestAPI implements ExecutionMessageListener { private ParserUtil parserUtil = null; private ExecutionManager execMgr = null; /** * Constructor */ public TestAPI () { UMLPackage lePackage = UMLPackage.eINSTANCE; ParserUtil parserUtil = new ParserUtil(); ExecutionManager execMgr = ExecutionManager.getExecutionManager(); } /** * Parses a transformation * @return numebr of parse errors */ protected int parse (String transformation) { File f = new File (transformation); MOFScriptSpecification spec = parserUtil.parse(f, true); int errorCount = ParserUtil.getModelChecker().getErrorCount(); // check for errors: Iterator errorIt = ParserUtil.getModelChecker().getErrors(); // Iterator of MofScriptParseError objects System.out.println ("Parsing result: " + errorCount + " errors"); if (errorCount > 0) { for (;errorIt.hasNext();) { MofScriptParseError parseError = (MofScriptParseError) errorIt.next(); System.out.println("\t \t: Error: " + parseError.toString()); } } return errorCount; } /** * Executes the transformation * @param inputModel - the name (path) of the inputmodel */ protected void execute (String inputModel) { XMIResourceFactoryImpl _xmiFac = new XMIResourceFactoryImpl(); EObject sourceModel = null; File sourceModelFile = new File (inputModel); ResourceSet rSet = new ResourceSetImpl (); rSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("*", _xmiFac); URI uri = URI.createFileURI(sourceModelFile.getAbsolutePath()); Resource resource = rSet.getResource(uri, true); if (resource != null) { if (resource.getContents().size() > 0) { sourceModel = (EObject) resource.getContents().get(0); } } // set the source model for the exeution manager execMgr.addSourceModel(sourceModel); // sets the root output directory, if any is desired (e.g. "c:/temp") execMgr.setRootDirectory(""); // if true, files are not generated to the file systsm, but populated into a filemodel // which can be fetched afterwards. Value false will result in standard file generation execMgr.setUseFileModel(false); // Turns on/off system logging execMgr.setUseLog(false); // Adds an output listener for the transformation execution. execMgr.getExecutionStack().addOutputMessageListener(this); try { execMgr.executeTransformation(); // execMgr.getOutputModels(); } catch (MofScriptExecutionException mex) { mex.printStackTrace(); } } /** * Parsing and executing */ public void test (String transformation, String inputModel) { int errs = parse(transformation); if (errs == 0) { execute(inputModel); } } /** * ExecutionMessageListener interface operations */ public void executionMessage (String type, String description) { System.out.println (type + " - " + description); } /** * Main * @param args */ public static void main (String[] args){ final String transformation = "UMLTest.m2t"; final String inputModel = "ServiceModel.uml"; TestAPI api = new TestAPI (); api.test(transformation, inputModel); } }