Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » [Xpand] Programmatic tutorial?
[Xpand] Programmatic tutorial? [message #1313513] Thu, 24 April 2014 19:05 Go to next message
Nigel Daniels is currently offline Nigel Daniels
Messages: 28
Registered: May 2011
Junior Member
Hi,

I have got an Xpand template running using a workflow and a test.xmi
file. It is now generating the sort of output I would like, after the
first hurdle was passed, a quick learning curve!

I would now like to run it from an Eclipse UI action and let the
customer select where the file is output to. IS there a tutorial or any
other guidance about on how to achieve this?

As well as understanding what code required I was wondering if there is
an Xpand runtime jar I can bundle with my plugin to save people from
having to grab multiple plugins?

Thanks for any advice on this.

Nigel
Re: [Xpand] Programmatic tutorial? [message #1313826 is a reply to message #1313513] Thu, 24 April 2014 23:17 Go to previous messageGo to next message
Nigel Daniels is currently offline Nigel Daniels
Messages: 28
Registered: May 2011
Junior Member
After much googleing, trial and error I now have the following code:


private void generateJAF() {
// Open a directory dialog to get the output path
DirectoryDialog dialog = new DirectoryDialog (shell);

dialog.setText(JAFEditorPlugin.INSTANCE.getString("_UI_GenerateJAF_dialog_title"));

dialog.setMessage(JAFEditorPlugin.INSTANCE.getString("_UI_GenerateJAF_dialog_message"));
String outputPath = dialog.open();

// Now add the JAF package to a resource set
ResourceSet resourceSet = new ResourceSetImpl();
EPackage JAFPAckage = JafPackage.eINSTANCE;
resourceSet.getPackageRegistry().put(JAFPAckage.getNsURI(),
JAFPAckage);

// Get the current content of the file being worked on
FileEditorInput input =
(FileEditorInput)activeEditorPart.getEditorInput();

// Now set the Xpand output path
URI outputURI = URI.create(outputPath);
Output output = new OutputImpl();
output.addOutlet(new Outlet(outputURI.toString()));
XpandExecutionContextImpl executionContext = new
XpandExecutionContextImpl(output, null);


// Configure the EMF metamodels
EmfMetaModel EMFMetaModel = new EmfMetaModel();
EMFMetaModel.setMetaModelPackage(JafPackage.class.getName());
executionContext.registerMetaModel(EMFMetaModel);

// Now reference the template
URL templateURL =
JAFEditorPlugin.getPlugin().getBundle().getEntry("template/JADTemplate.xpt");
executionContext.registerAdvices(templateURL.getPath());

XpandFacade xpandFacade = XpandFacade.create(executionContext);
Object[] params = null;
//System.out.println(inObjects.get(0));
try {
xpandFacade.evaluate("template::JADTemplate::main",
input.getFile().getContents(), params);
MessageDialog.openInformation(shell, "JAF Generated",
String.format("Your JAF file has been generated in:%n%n", null) +
outputPath);
}
catch (Exception e) {
MessageDialog.openError(shell, "Generation Failed",
String.format("The JAF file generation failed with the message:%n%n",
null) + e.getLocalizedMessage());
}
}

The bit I'm struggling with is the reference to the Template itself? Any
idea how I add the reference to the template in my plugin (I copied it
into the root of my editor plugin into a directory called template) or
even in the Xpand plugin I originally created it in?

Where does the XpandFacade expect it to be?

Thanks for any help.

Nigel
On 24/04/2014 16:05, Nigel Daniels wrote:
> Hi,
>
> I have got an Xpand template running using a workflow and a test.xmi
> file. It is now generating the sort of output I would like, after the
> first hurdle was passed, a quick learning curve!
>
> I would now like to run it from an Eclipse UI action and let the
> customer select where the file is output to. IS there a tutorial or any
> other guidance about on how to achieve this?
>
> As well as understanding what code required I was wondering if there is
> an Xpand runtime jar I can bundle with my plugin to save people from
> having to grab multiple plugins?
>
> Thanks for any advice on this.
>
> Nigel
Re: [Xpand] Programmatic tutorial? [message #1314139 is a reply to message #1313826] Fri, 25 April 2014 03:28 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6125
Registered: July 2009
Senior Member
Hi,

it should be fine if it is on the classpath.
Re: [Xpand] Programmatic tutorial? [message #1314874 is a reply to message #1314139] Fri, 25 April 2014 13:06 Go to previous messageGo to next message
Nigel Daniels is currently offline Nigel Daniels
Messages: 28
Registered: May 2011
Junior Member
Hi Christian,

When I first ran this the template was in it's own plug-in in the
src/template directory and was included on my selected plugins for the
debug environment, but it was not found.

I have included now in the actual editor plugin and it's still not being
found? Is there an example plugin you know of that I can look at to see
what step I am missing as it is still not seen by the XpandFacade, and I
am not sure why.

Afaik the template was on the classpath in both cases before I
attempted to register the advice as to it's location.

Also just to check I have the name correct in the evaluate call this is
the same name I was able to use in my workflow:

<component class="org.eclipse.xpand2.Generator">
<metaModel idRef="mm_emf"/>
<expand value="template::JADTemplate::main FOR jad" />
<outlet path="${src-gen}" />
...

Thanks for helping,

Nigel

On 25/04/2014 00:28, Christian Dietrich wrote:
> Hi,
>
> it should be fine if it is on the classpath.
>
Re: [Xpand] Programmatic tutorial? [message #1315354 is a reply to message #1314874] Fri, 25 April 2014 19:25 Go to previous messageGo to next message
Nigel Daniels is currently offline Nigel Daniels
Messages: 28
Registered: May 2011
Junior Member
Hi, any suggestion on this?

I started with the template in it's own plugin:

com.foo.xpand
|_ src
|_ template
|_ JADTemplate.xpt

I ensured that the template package was exported and that the plugin was
a dependancy for my editor plugin. I've even tried adding the template
package directly to the editor plugin and ensuring it's exported:

com.foo.editor
|_ src
|_ com.foo.presentation [My editor and action are in here]
|_ template
|_ JADTemplate.xpt


Afaik this is on the classpath (in both cases):

The JADTemplate.xpt contains the main:

┬źDEFINE main FOR JAFDocument┬╗

And the code I am running calls it like this :


xpandFacade.evaluate("template::JADTemplate::main",
input.getFile().getContents(), params);

But each time I get:

EvaluationException : No Definition template::JADTemplate::main for
Object could be found!
Internal error : element was null

I've also tried adding the xpt to the evaluation context's resource
manager using a platform URL, file URL and absolute path. I each case I
get the same result.

Any advice on what I am doing wrong is very welcome.

On 25/04/2014 10:06, Nigel Daniels wrote:
> Hi Christian,
>
> When I first ran this the template was in it's own plug-in in the
> src/template directory and was included on my selected plugins for the
> debug environment, but it was not found.
>
> I have included now in the actual editor plugin and it's still not being
> found? Is there an example plugin you know of that I can look at to see
> what step I am missing as it is still not seen by the XpandFacade, and I
> am not sure why.
>
> Afaik the template was on the classpath in both cases before I attempted
> to register the advice as to it's location.
>
> Also just to check I have the name correct in the evaluate call this is
> the same name I was able to use in my workflow:
>
> <component class="org.eclipse.xpand2.Generator">
> <metaModel idRef="mm_emf"/>
> <expand value="template::JADTemplate::main FOR jad" />
> <outlet path="${src-gen}" />
> ...
>
> Thanks for helping,
>
> Nigel
>
> On 25/04/2014 00:28, Christian Dietrich wrote:
>> Hi,
>>
>> it should be fine if it is on the classpath.
>>
>
Re: [Xpand] Programmatic tutorial? [message #1316008 is a reply to message #1315354] Sat, 26 April 2014 04:19 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6125
Registered: July 2009
Senior Member
Hi,

i fear you have to debug into the xpand code.

are you sure the types input.getFile().getContents() and the type in the template match?
Re: [Xpand] Programmatic tutorial? [message #1320668 is a reply to message #1316008] Mon, 28 April 2014 19:14 Go to previous message
Nigel Daniels is currently offline Nigel Daniels
Messages: 28
Registered: May 2011
Junior Member
Hi Christian,

again thanks for the hint about my input type, that turned out to be the
problem here. It's a shame the exception message was not so clear as to
the cause of the problem this had me looking at completely the wrong
problem!

Nigel

On 26/04/2014 01:19, Christian Dietrich wrote:
> Hi,
>
> i fear you have to debug into the xpand code.
>
> are you sure the types input.getFile().getContents() and the type in the
> template match?
Previous Topic:[Xpand] MWE2 Workflow fails with package uri not found
Next Topic:Acceleo not reading metamodels properly
Goto Forum:
  


Current Time: Tue Jul 22 13:46:04 EDT 2014

Powered by FUDForum. Page generated in 0.02115 seconds