Home » Modeling » TMF (Xtext) » HELP! run a xtext-generator workflow (located in the parent workspace) programmatically
HELP! run a xtext-generator workflow (located in the parent workspace) programmatically [message #650240] |
Sun, 23 January 2011 16:16 |
Christian Hecht Messages: 4 Registered: January 2011 |
Junior Member |
|
|
Hi,
we are trying to run a xtext-generator workflow (located in the parent workspace) programmatically from a button located in the child xtext editor instance.
we tried the solution mentioned in https://bugs.eclipse.org/bugs/show_bug.cgi?id=318721
which throws
Exception in thread "Thread-6" java.lang.IllegalStateException: [XtextLinkingDiagnostic: null:19 Couldn't resolve reference to JvmType 'org.xtext.metapig.f9.eimer.F9EStandaloneSetup'.]
at org.eclipse.emf.mwe2.launch.runtime.Mwe2Runner.run(Mwe2Runner.java:66)
at org.eclipse.emf.mwe2.launch.runtime.Mwe2Runner.run(Mwe2Runner.java:52)
at de.tu_berlin.swt.metapig.launchers.ModelToCodeLauncher.run(ModelToCodeLauncher.java:78)
Any Suggestions ?
Thanks in advance!
Our Code:
public class ModelToCodeLauncher implements Runnable {
public static IPath getAbsoluteWorkflowPath(String relPath, String bundlename) {
Bundle bundle = Platform
.getBundle(bundlename);
URL fileLocation = null;
try {
fileLocation = FileLocator.toFileURL(bundle.getEntry(relPath));
} catch (IOException e) {
throw new RuntimeException(e);
}
IPath p = new Path(new File(fileLocation.getFile()).getAbsolutePath());
return p;
}
public static void runModelToCodeWorkflow(IPath p) {
ModelToCodeLauncher modelToCodeLauncher = new ModelToCodeLauncher();
Thread t = new Thread(modelToCodeLauncher);
t.start();
}
static class OSGiResourceSetInitializer extends
RuntimeResourceSetInitializer {
public List<String> getClassPathEntries() {
List<String> classPathEntries = super.getClassPathEntries();
classPathEntries.add(0, getAbsoluteWorkflowPath("/", "org.xtext.metapig.f9.eimer").toOSString());
classPathEntries.add(1, getAbsoluteWorkflowPath("/", "org.xtext.metapig.f9.eimer.generator").toOSString());
return classPathEntries;
}
}
public void run() {
Injector injector = new Mwe2StandaloneSetup() {
@Override
public Injector createInjector() {
return Guice.createInjector(new Mwe2RuntimeModule() {
public Class<? extends RuntimeResourceSetInitializer> bindRuntimeResourceSetInitializer() {
return OSGiResourceSetInitializer.class;
}
});
}
}.createInjectorAndDoEMFRegistration();
Mwe2Runner runner = injector.getInstance(Mwe2Runner.class);
Map<String, String> map = new HashMap<String, String>();
map.put("modelPath", "d:\\test");
map.put("targetDir", "d:\\test");
URI createFileURI = URI.createFileURI(getAbsoluteWorkflowPath(
"/src/workflow/F9EGenerator.mwe2", "org.xtext.metapig.f9.eimer.generator").toOSString());
runner.run(createFileURI, map);
}
}
|
|
|
Re: HELP! run a xtext-generator workflow (located in the parent workspace) programmatically [message #650292 is a reply to message #650240] |
Mon, 24 January 2011 09:18 |
Jan Koehnlein Messages: 760 Registered: July 2009 Location: Hamburg |
Senior Member |
|
|
Running a standalone setup (which is one component in your workflow)
from within a running Eclipse will not work, as you're going to mess up
the singleton EMF registries.
There is no simple fix, because we are using completely different
strategies to handle Java references in the Eclipse scenario (using JDT)
and in non-Eclipse (Java reflection based). It makes a lot of difference
if you are dealing with compiled JARs or workspace projects.
The components in your workflow are just plain Java classes, so you can
run them without using MWE2. Have a look at the domainmodel example how
to run an Xpand based code generator from within an Eclipse action.
Am 23.01.11 17:16, schrieb Christian Hecht:
> Hi,
>
> we are trying to run a xtext-generator workflow (located in the parent
> workspace) programmatically from a button located in the child xtext
> editor instance.
> we tried the solution mentioned in
> https://bugs.eclipse.org/bugs/show_bug.cgi?id=318721
>
>
> which throws
>
> Exception in thread "Thread-6" java.lang.IllegalStateException:
> [XtextLinkingDiagnostic: null:19 Couldn't resolve reference to JvmType
> 'org.xtext.metapig.f9.eimer.F9EStandaloneSetup'.]
> at org.eclipse.emf.mwe2.launch.runtime.Mwe2Runner.run(Mwe2Runne r.java:66)
> at org.eclipse.emf.mwe2.launch.runtime.Mwe2Runner.run(Mwe2Runne r.java:52)
> at
> de.tu_berlin.swt.metapig.launchers.ModelToCodeLauncher.run(M odelToCodeLauncher.java:78)
>
>
>
>
>
> Any Suggestions ?
>
>
> Thanks in advance!
>
>
> Our Code:
>
>
>
> public class ModelToCodeLauncher implements Runnable {
>
> public static IPath getAbsoluteWorkflowPath(String relPath, String
> bundlename) {
> Bundle bundle = Platform
> .getBundle(bundlename);
> URL fileLocation = null;
> try {
> fileLocation = FileLocator.toFileURL(bundle.getEntry(relPath));
>
> } catch (IOException e) {
> throw new RuntimeException(e);
> }
> IPath p = new Path(new File(fileLocation.getFile()).getAbsolutePath());
> return p;
>
> }
>
> public static void runModelToCodeWorkflow(IPath p) {
> ModelToCodeLauncher modelToCodeLauncher = new ModelToCodeLauncher();
> Thread t = new Thread(modelToCodeLauncher);
> t.start();
> }
>
> static class OSGiResourceSetInitializer extends
> RuntimeResourceSetInitializer {
> public List<String> getClassPathEntries() {
> List<String> classPathEntries = super.getClassPathEntries();
>
> classPathEntries.add(0, getAbsoluteWorkflowPath("/",
> "org.xtext.metapig.f9.eimer").toOSString());
> classPathEntries.add(1, getAbsoluteWorkflowPath("/",
> "org.xtext.metapig.f9.eimer.generator").toOSString());
> return classPathEntries;
> }
> }
>
> public void run() {
> Injector injector = new Mwe2StandaloneSetup() {
> @Override
> public Injector createInjector() {
> return Guice.createInjector(new Mwe2RuntimeModule() {
> public Class<? extends RuntimeResourceSetInitializer>
> bindRuntimeResourceSetInitializer() {
> return OSGiResourceSetInitializer.class;
> }
> });
> }
> }.createInjectorAndDoEMFRegistration();
> Mwe2Runner runner = injector.getInstance(Mwe2Runner.class);
>
> Map<String, String> map = new HashMap<String, String>();
> map.put("modelPath", "d:\\test");
> map.put("targetDir", "d:\\test");
>
> URI createFileURI = URI.createFileURI(getAbsoluteWorkflowPath(
> "/src/workflow/F9EGenerator.mwe2",
> "org.xtext.metapig.f9.eimer.generator").toOSString());
> runner.run(createFileURI, map);
> }
>
> }
>
>
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
---
Get professional support from the Xtext committers at www.typefox.io
|
|
|
Re: HELP! run a xtext-generator workflow (located in the parent workspace) programmatically [message #650419 is a reply to message #650240] |
Mon, 24 January 2011 19:00 |
Christian Hecht Messages: 4 Registered: January 2011 |
Junior Member |
|
|
Hi Jan,
first, thank your for your answer !
I think you understood our problem quite well. If we unterstand your answer correctly, we can't use mwe2 in this scenario and need to run the xpand generator directly.
Could you clarify, how this is supposed to work ? We could not find any information on this problem in the xtext-documentation (http://www.eclipse.org/Xtext/documentation/1_0_0/xtext.html). Is this issue related to the one described in this post: http://www.eclipse.org/forums/index.php?t=msg&th=199822& amp;S=c9f5f88416c0988abfc54451919e887f#msg_637685 ?
But maybe there is another way to solve this problem. If it helps, here are some more details:
We are only dealing with workspace projects.
In our main workflow we have following elements:
workspace of eclipse instance 1:
org.xtext.metapig.f9.eimer
org.xtext.metapig.f9.eimer.generator
org.xtext.metapig.f9.eimer.ui
location of xpand file (org.xtext.metapig.f9.eimer.generator\src\templates\Template .xpt)
location of mwe2 workflow for model to code transformation (org.xtext.metapig.f9.eimer.generator\src\workflow\F9EGenera tor.mwe2)
From this instance we start the ui edtior with an additional button on it.
In this second instance the user writes his models
If the user clicks a button the code generation should be started bases on the model the user has created and the results should be saved to a directory the user specifies, e.g. the workflow parameters have to be set accordingly.
What is, in your opinion, the best way to achieve this, e.g. launch the workflow in org.xtext.metapig.f9.eimer.generator\src\workflow\F9Generato r.mwe2 from eclipse instance 2?
How can we set the dependencies?
|
|
|
Re: HELP! run a xtext-generator workflow (located in the parent workspace) programmatically [message #650492 is a reply to message #650419] |
Tue, 25 January 2011 09:17 |
Jan Koehnlein Messages: 760 Registered: July 2009 Location: Hamburg |
Senior Member |
|
|
I'd run the Xpand generator programmatically, as we do in the
domainmodel example. I'd recommend to import the example (New ->
Example...) and have a look at
/org.eclipse.xtext.example.domainmodel.ui/src/org/eclipse/xt ext/example/ui/generator/Generator.java
and the other files in that package.
GeneAm 24.01.11 20:00, schrieb Christian Hecht:
> Hi Jan,
>
> first, thank your for your answer !
>
> I think you understood our problem quite well. If we unterstand your
> answer correctly, we can't use mwe2 in this scenario and need to run the
> xpand generator directly.
>
> Could you clarify, how this is supposed to work ? We could not find any
> information on this problem in the xtext-documentation
> (http://www.eclipse.org/Xtext/documentation/1_0_0/xtext.html). Is this
> issue related to the one described in this post:
> http://www.eclipse.org/forums/index.php?t=msg&th=199822& amp;S=c9f5f88416c0988abfc54451919e887f#msg_637685
> ?
>
>
> But maybe there is another way to solve this problem. If it helps, here
> are some more details:
>
> We are only dealing with workspace projects.
>
> In our main workflow we have following elements:
>
> workspace of eclipse instance 1:
> org.xtext.metapig.f9.eimer
> org.xtext.metapig.f9.eimer.generator
> org.xtext.metapig.f9.eimer.ui
> location of xpand file
> (org.xtext.metapig.f9.eimer.generator\src\templates\Template .xpt)
> location of mwe2 workflow for model to code transformation
> (org.xtext.metapig.f9.eimer.generator\src\workflow\F9EGenera tor.mwe2)
>
> From this instance we start the ui edtior with an additional button on it.
> In this second instance the user writes his models
> If the user clicks a button the code generation should be started bases
> on the model the user has created and the results should be saved to a
> directory the user specifies, e.g. the workflow parameters have to be
> set accordingly.
>
>
> What is, in your opinion, the best way to achieve this, e.g. launch the
> workflow in org.xtext.metapig.f9.eimer.generator\src\workflow\F9Generato
> r.mwe2 from eclipse instance 2?
> How can we set the dependencies?
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
---
Get professional support from the Xtext committers at www.typefox.io
|
|
| | |
Re: HELP! run a xtext-generator workflow (located in the parent workspace) programmatically [message #651417 is a reply to message #651100] |
Sun, 30 January 2011 17:16 |
Christian Hecht Messages: 4 Registered: January 2011 |
Junior Member |
|
|
Thank you for pointing that out Christian, finally some success !
Our code now looks like this:
public void build(String absoluteModelPath) {
try {
ResourceSet resourceSet = new ResourceSetImpl();
Resource resource = resourceSet.createResource(URI.createURI("file:///"+absoluteModelPath)); //
resource.load(null);
resourceSet.getResources().add(resource);
Object format = resource.getContents().get(0);
OutputImpl output = new OutputImpl();
Outlet outlet = new Outlet("C:\\src-gen");
output.addOutlet(outlet);
XpandExecutionContextImpl execCtx = new XpandExecutionContextImpl(output, null);
execCtx.registerMetaModel(new JavaBeansMetaModel());
XpandFacade facade = XpandFacade.create(execCtx);
facade.evaluate("templates::Template::main", format);
}
catch (Exception e) {
e.printStackTrace();
}
}
This generates the classfiles correctly!
But there is still one issue:
As described in the post above, the metamodel and the workflow in the first instance are modified, then the workflow is started, then we run
Job.getJobManager().join(
ResourcesPlugin.FAMILY_AUTO_REFRESH, null);
Job.getJobManager().join(
ResourcesPlugin.FAMILY_AUTO_BUILD, null);
to wait till the refresh of the workspace is finished.
after that, the second instance is started, the user writes his code there and starts the code generation.
for the generation to work, we need to close the second workspace and start it again. If not, we get
EvaluationException : No Definition templates::Template::main for org::xtext::metapig::f9::eimer::f9e::impl::ModelImpl could be found!
Internal error : element was null
at org.eclipse.xpand2.XpandFacade.evaluate2(XpandFacade.java:59)
at org.eclipse.xpand2.XpandFacade.evaluate(XpandFacade.java:43)
at de.tu_berlin.swt.metapig.launchers.ModelToCodeLauncher2.build(ModelToCodeLauncher2.java:48)
Presumably this is caused by the modified metamodel not being registered.
Any advise on this one ?
|
|
|
Goto Forum:
Current Time: Tue Sep 24 09:03:12 GMT 2024
Powered by FUDForum. Page generated in 0.05380 seconds
|