Generator command line followup

112 views
Skip to first unread message

Dan Lapid

unread,
Nov 6, 2015, 10:37:36 PM11/6/15
to YAKINDU User
Hey so following up on generating the src files from command line.
I'm trying to create a jar to run that receives model/sgen file path and generates src code
I found the function executeGenerator(Ifile/GeneratorModel)
Obviously since I'm running it as a jar and not a plugin then I can't use Ifile
How do I create a GeneratorModel and inject parameters such as dir and model file into it or how do I import GeneratorModel type from .sgen file?

Andreas Mülder

unread,
Nov 9, 2015, 3:09:07 AM11/9/15
to YAKINDU User
Hi Dan,

you could try to execute the CCodeGenerator directly, it implements the org.yakindu.sct.generator.core.ISCTGenerator interface
that should have no direct dependencies to Eclipse resources like IFile.

You can create an instance of a GeneratorModel either programmatically using EMF API for example:

GeneratorModel model = SGenFactory.eINSTANCE.createGeneratorModel();
model.setGeneratorId("yakindu::c");
GeneratorEntry entry = SGenFactory.eINSTANCE.createGeneratorEntry();
entry.setContentType("statechart");
entry.setElementRef(myStatechartModel);
...

or you load an existing sgen model file via EMFs Resource loading API, for example:

URI uri = URI.createPlatformResourceURI(filename, true);
Factory factory = ResourceFactoryRegistryImpl.INSTANCE.getFactory(uri);
Resource resource = factory.createResource(uri);
ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.getResources().add(resource);
resource.load(Collections.EMPTY_MAP);
//here is your model
GeneratorModel model = (GeneratorModel)resource.getContents().get(0)
For this to work in a standalone, non osgi environment make sure to 
call SGenStandaloneSetup.doSetup first.

Another (somehow heavyweight) approach would be to implement an Eclipse Application (org.eclipse.core.runtime.applications extension point)
This basically allows the execution of a headless eclipse with running osgi, so you could reuse more of the exiting infrastructure.

Let me know if this works!

Best,

Andreas

Dan Lapid

unread,
Nov 23, 2015, 11:41:03 AM11/23/15
to YAKINDU User
Hey tried to do that but there is some weird exception
Exception in thread "main" org.eclipse.xtext.parser.ParseException: java.lang.IllegalStateException: Unresolved proxy http://www.yakindu.org/sct/statechart/SGen#//GeneratorModel. Make sure the EPackage has been registered.
at org.eclipse.xtext.parser.antlr.AbstractAntlrParser.doParse(AbstractAntlrParser.java:105)
at org.eclipse.xtext.parser.antlr.AbstractAntlrParser.parse(AbstractAntlrParser.java:84)
at org.eclipse.xtext.parser.antlr.AbstractAntlrParser.doParse(AbstractAntlrParser.java:62)
at org.eclipse.xtext.parser.AbstractParser.parse(AbstractParser.java:32)
at org.eclipse.xtext.resource.XtextResource.doLoad(XtextResource.java:160)
at org.eclipse.xtext.linking.lazy.LazyLinkingResource.doLoad(LazyLinkingResource.java:100)
at org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:1518)
at org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:1297)
at Main.main(Main.java:26)
Caused by: org.eclipse.emf.common.util.WrappedException: java.lang.IllegalStateException: Unresolved proxy http://www.yakindu.org/sct/statechart/SGen#//GeneratorModel. Make sure the EPackage has been registered.
at org.eclipse.xtext.parser.antlr.AbstractInternalAntlrParser.parse(AbstractInternalAntlrParser.java:555)
at org.eclipse.xtext.parser.antlr.AbstractAntlrParser.doParse(AbstractAntlrParser.java:102)
... 8 more
Caused by: java.lang.IllegalStateException: Unresolved proxy http://www.yakindu.org/sct/statechart/SGen#//GeneratorModel. Make sure the EPackage has been registered.
at org.eclipse.xtext.parser.DefaultEcoreElementFactory.create(DefaultEcoreElementFactory.java:57)
at org.eclipse.xtext.parser.antlr.AbstractInternalAntlrParser.createModelElement(AbstractInternalAntlrParser.java:637)
at org.eclipse.xtext.parser.antlr.AbstractInternalAntlrParser.createModelElementForParent(AbstractInternalAntlrParser.java:621)
at org.yakindu.sct.generator.genmodel.parser.antlr.internal.InternalSGenParser.ruleGeneratorModel(InternalSGenParser.java:166)
at org.yakindu.sct.generator.genmodel.parser.antlr.internal.InternalSGenParser.entryRuleGeneratorModel(InternalSGenParser.java:97)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.eclipse.xtext.parser.antlr.AbstractInternalAntlrParser.parse(AbstractInternalAntlrParser.java:532)
... 9 more

Dan Lapid

unread,
Nov 23, 2015, 11:42:20 AM11/23/15
to YAKINDU User
This is the code
SGenStandaloneSetup.doSetup();
String filepath= args[0];
// URI uri = URI.createPlatformResourceURI(filepath, true);
URI uri = URI.createFileURI(new File(filepath).getAbsolutePath());
Factory factory = ResourceFactoryRegistryImpl.INSTANCE.getFactory(uri);
Resource resource = factory.createResource(uri);
ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.getResources().add(resource);
try {
resource.load(Collections.EMPTY_MAP);
} catch (IOException e) { 
// TODO Auto-generated catch block
e.printStackTrace();
//Here is your model
GeneratorModel model = (GeneratorModel)resource.getContents().get(0);
//This runs the generator
   new GeneratorExecutor().executeGenerator(model);

Andreas Mülder

unread,
Nov 24, 2015, 1:58:43 AM11/24/15
to YAKINDU User

Hi Dan,

this exeption is because the EMF EPackage registry was not initialized. You can force this with a call to SGenPackage.eINSTANCE.
However, there are a few other problems with this approach and our current code base when running your code. Since we execute the code in a non-osgi environment 
we can't read out extension points. So we have to at least refactor the GeneratorExtensions class to allow manually registration of code generators.
Another problem is that the cross references to the statechart models can not be resolved by name, because there is no index for the workspace anymore.
For this to work we need a second parameter for the folder where the statechart models are contained and load them manually into the resource set to allow proper linking of the GeneratorModel

Best regards,

Andreas

Need professional support for Yakindu Statechart Tools?
Contact us: state...@itemis.de
Reply all
Reply to author
Forward
0 new messages