Java API Integration
You can run MOFScript using its Java API. Below is an example that shows how.
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.sintef.mofscript.MOFScriptModel.MOFScriptSpecification;
import org.sintef.mofscript.parser.MofScriptParseError;
import org.sintef.mofscript.parser.ParserUtil;
import org.sintef.mofscript.runtime.ExecutionManager;
import org.sintef.mofscript.runtime.ExecutionMessageListener;
import org.sintef.mofscript.runtime.MofScriptExecutionException;
public class TestAPI implements ExecutionMessageListener {
public TestAPI () {
}
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 ("EcoreTest.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");
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();
}
}
/**
* 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();
}
}