[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Newsgroup Home]
|
[news.eclipse.modeling.gmt] Re: [MOFScript] loading Metamodel
|
Hello again,
I am not sure what is wrong, take a look at the example below. It works.
Cheers Gøran
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();
}
}
"Netuh" <waldemar.neto@xxxxxxxxx> wrote in message
news:a1b13d3f55b5bba18badb656ef34844b$1@xxxxxxxxxxxxxxxxxx
> Thanks Gøran,
>
> i did it...
>
> File metamodel = new File("Z:\\<<Filepath>>\\JavaAbstractSyntax.ecore");
> ResourceSet rsSource = new ResourceSetImpl();
> rsSource.getResourceFactoryRegistry().getExtensionToFactoryMap().put("*",
> new XMIResourceFactoryImpl());
> Resource resSource = rsSource.getResource(
> URI.createFileURI(metamodel.getAbsolutePath()), true);
> EPackage package = (EPackage) resSource.getContents().get(0);
> EPackage.Registry.INSTANCE.put("JavaAbstractSyntax", package);
>
> But now i have other problem. When i load the model.
>
> i m doing it:
>
> execMgr.lookupAndAddSourceMetaModel("JavaAbstractSyntax","JavaAbstractSyntax"));
> execMgr.loadSourceModel(new
> File("Z:\\<<Filepath>>\\test.javaabstractsyntax"));
>
> But a exception is thrown. The model test.javaabstractsyntax and the
> transfomation works well on the eclipse plugin.
>
> Exception in thread "main" java.lang.NullPointerException
> at
> org.eclipse.mofscript.runtime.ExecutionManager.getSourceMetaModelName(ExecutionManager.java:887)
> at
> org.eclipse.mofscript.runtime.ExecutionManager.loadSourceModel(ExecutionManager.java:585)
> at code.JavaCodeGeneration.test(JavaCodeGeneration.java:123)
> at code.JavaCodeGeneration.main(JavaCodeGeneration.java:151)
>
> Any ideia?
>
> Thanks in advance
>
> Gøran K. Olsen wrote:
>
>> Hello again,
>
>> And if this doesn't work have you added
>
>> //
>> //Change to your ovn metamodel
>> //
>> import org.eclipse.uml2.uml.UMLPackage;
>
>> public class TestAPI implements ExecutionMessageListener {
>
>> public TestAPI () {
>
>> //
>> //Maks MOFScript aware of your metamodel chenge to your own
>> //
>> UMLPackage lePackage = UMLPackage.eINSTANCE;
>
>
>> /Gøran
>
>
>> "Gøran K. Olsen" <Goran.K.Olsen@xxxxxxxxx> wrote in message
>> news:g3ntt1$qo5$1@xxxxxxxxxxxxxxxxxxxx
>>> Hello Netuh,
>>>
>>> Have you added your model plugin to the classpath?
>>>
>>> Cheers,
>>> Gøran
>>>
>>> "Netuh" <waldemar.neto@xxxxxxxxx> wrote in message
>>> news:8fc95745bbb80a8e313ec69317d7e804$1@xxxxxxxxxxxxxxxxxxxxx
>>>> Hi,
>>>>
>>>> I'm defining some textual tranformations for models based on the
>>>> JavaAbstractSyntax metamodel. It works fine! However, when I try to
>>>> integrate my MOFScript transformation in a Java environment, using the
>>>> example given in the MOFScript User Guide v0.6, it doesn't work.
>>>>
>>>> The following line
>>>> **int errorCount = ParserUtil.getModelChecker().getErrorCount();**
>>>> returns 85 erros. They are all concerning the lack of a metamodel
>>>> definition.
>>>>
>>>> I see that the exemple in the User Guide doesn't present a means of
>>>> setting the metamodel before calling
>>>> **execMgr.executeTransformation();**
>>>> So, the errors are expected.
>>>>
>>>> Although I've searched a lot, I have not found any way of setting the
>>>> metamodel, any kind of MOFScript API and any other kind of
>>>> ducumentation.
>>>>
>>>> Has anyone had the same problem? May someone help me?
>>>>
>>>> Best regards!
>>>>
>>>> ---
>>>> The output I get from my Java program is this:
>>>> Parsing result: 85 errors
>>>> : Error: MTTParseError: Illegal/unknown context: jast, line: 484,
>>>> column: 0
>>>> : Error: MTTParseError: Can't find feature 'interface' for type
>>>> 'TypeDeclaration'., line: 28, column: 5
>>>> : Error: MTTParseError: Type not found: 'FieldDeclaration' in metamodel
>>>> 'null', line: 0, column: 0
>>>> : Error: MTTParseError: Can't find feature 'bodyDeclarations' for type
>>>> 'TypeDeclaration'., line: 31, column: 2
>>>> : Error: MTTParseError: Type not found: 'MethodDeclaration' in
>>>> metamodel 'null', line: 0, column: 0
>>>> .
>>>> .
>>>> .
>>>> .
>>>> ---
>>>> jast is a reference to the metamodel.
>>>>
>>>
>>>
>
>