Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.IOException;
- import java.util.Iterator;
- import java.util.List;
- import org.eclipse.emf.common.util.EList;
- 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.uml2.uml.UMLPackage;
- import org.eclipse.mofscript.MOFScriptModel.MOFScriptSpecification;
- import org.eclipse.mofscript.MOFScriptModel.MOFScriptTransformation;
- import org.eclipse.mofscript.fileresourcemodel.frm.FileResource;
- import org.eclipse.mofscript.fileresourcemodel.frm.FileResourcesType;
- 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.w3c.dom.events.UIEvent;
- import com.sun.corba.se.impl.resolver.FileResolverImpl;
- public class TestAPI implements ExecutionMessageListener {
- public TestAPI () {
- UMLPackage lePackage = UMLPackage.eINSTANCE;
- }
- public void test () {
- ParserUtil parserUtil = new ParserUtil();
- ExecutionManager execMgr = ExecutionManager.getExecutionManager();
- //
- // The parserutil parses and sets the input transformation model
- // for the execution manager.
- //
- File f = new File ("UMLTest.m2t");
- MOFScriptSpecification spec = parserUtil.parse(f, true);
- // check for errors:
- int errorCount = ParserUtil.getModelChecker().getErrorCount();
- 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;
- }
- // load source model
- XMIResourceFactoryImpl _xmiFac = new XMIResourceFactoryImpl();
- EObject sourceModel = null;
- //File sourceModelFile = new File ("SM.ecore");
- File sourceModelFile = new File ("ServiceModel.uml");
- 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();
- } catch (MofScriptExecutionException mex) {
- mex.printStackTrace();
- }
- //
- // Alternatively, we can use the file model,
- // in which case no files will be stored, only added to
- // an file model (org.eclipse.mofscript.fileresourcemodel)
- //
- //
- execMgr.setUseFileModel(true);
- try {
- execMgr.executeTransformation();
- FileResourcesType fileResource = execMgr.getFileModel();
- EList<FileResource> fileResources=fileResource.getFileResource();
- for (Iterator<FileResource> fileit = fileResources.iterator();
- fileit.hasNext();) {
- FileResource fResource = fileit.next();
- byte[] contents = fResource.getContent();
- // do something with the contents
- }
- } catch (MofScriptExecutionException mfex) {
- }
- }
- /**
- * Executing a transformation model directly (no parsing)
- */
- public void executeModelDirectly() {
- ResourceSet rSet = new ResourceSetImpl ();
- URI fileUri = URI.createFileURI("someTransformationFile.mofscript");
- Resource res = rSet.getResource(fileUri, true);
- // get the transformation specification from some resource...
- MOFScriptSpecification trSpec = (MOFScriptSpecification)
- res.getContents().get(0);
- // extract (one) transformation from the specification (it could in theory
- be several)
- MOFScriptTransformation transformation = (MOFScriptTransformation)
- trSpec.getTransformation().get(0);
- // set the transformation on the execution manager
- ExecutionManager execMgr = ExecutionManager.getExecutionManager();
- execMgr.setTransformationModel(transformation);
- // load source model(s)
- EObject src = null;
- // add source model(s)
- execMgr.addSourceModel(src);
- // execute transformation
- try {
- execMgr.executeTransformation();
- } catch(MofScriptExecutionException mex) {
- }
- // ...
- }
- /**
- * ExecutionMessageListener interface operations
- */
- public void executionMessage (String type, String description) {
- System.out.println (type + " - " + description);
- }
- public static void main (String[] args){
- TestAPI api = new TestAPI ();
- api.test();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement