Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » [XPAND] Adding a template to classpath (Xpand not able to find templates in eclipse plugin environment)
[XPAND] Adding a template to classpath [message #989196] Wed, 05 December 2012 02:59 Go to next message
Bhanu Pratap is currently offline Bhanu Pratap
Messages: 13
Registered: April 2010
Junior Member
I have xpand generator plugin, here is the short description of what this plugin is meant to do:

Once this plugin is installed/launched :-
1. User right click's on a .xmi file(could be anywhere in his/her workspace) and selects an action.
2. The above action generates another xpand template in the same location as the above .xmi file.
3. Once this template is generated, another xpand workflow should run this generated template(to generate few other files).

Problem description:

This plugin works fine till step 2, i.e. I am able to successfully run xpand workflow programmatically for the model file on which user right clicks and initiates action. But when it comes to run another workflow based on the generated template(step 3), the xpand workflow fails with error: No definition for <my generated template> found. That is, xpand workflow is not able to load/find the generated template(inside the plugin environment i.e. user's new workspace).

I looked through the entire xpand documentation for it's "generator" component and all I could find is that when we configure generator component in workflow file, the value for the property 'expand' is just in xpand syntax and the workflow should be able to find the template specified if it is on the classpath, for e.g:

<component class="org.eclipse.xpand2.Generator"><metaModel idRef="mm_emf"/><expand value="template::metamodel_expanded::main FOR model"/><outlet path="${src-gen}" fileEncoding="UTF-8"><postprocessor class="org.eclipse.xpand2.output.JavaBeautifier"/></outlet></component>

I have already tried adding the user's workspace directory to the classpath dynamically but it didn't work Sad . Here is my code:

===================================================================
private void addToClasspath(String path){

String classPath = System.getProperty("java.class.path");
String sep = System.getProperty("path.separator");
classPath = path + sep+ classPath;
System.setProperty("java.class.path", classPath);
System.out.println(classPath);

}

private void generate(){
// get project root folder as absolute file system path
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IResource resource = root.findMember(selectedModels.get(0).getFullPath());
//IResource resource = selectedModels.get(0).get
String containerName = resource.getParent().getLocation().toPortableString();
String wfFile = null;
String fileExt = resource.getFileExtension();
addToClasspath(containerName);//+File.separator+"metamodel_expanded.xpt");

Map<String,String> properties = new HashMap<String,String>();
Map<String,String> slotContents = new HashMap<String,String>();
WorkflowRunner runner = new WorkflowRunner();

if(fileExt.equalsIgnoreCase("xmi")){

FileDialog fd = new FileDialog(shell);
fd.setText("Please select the Metamodel file for the Model(xmi) you selected: ");
String metamodelFile = fd.open();
URI metaModelFileURI = null;
try {
File f = new File(metamodelFile);
metaModelFileURI = f.toURI();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//System.out.println(metaModelFileURI);

fd.setFilterPath(containerName);

// first generate the ASTs for metamodel of this model
wfFile = "workflow/M2generator.mwe";
properties.clear(); slotContents.clear();
properties.put("modelFile", metaModelFileURI.toString());
properties.put("src-gen", containerName);
runner.run(wfFile, new NullProgressMonitor(), properties, slotContents) ;

//now generate PEFs for this model
wfFile = "workflow/M1generator.mwe";
String modelFilePath = resource.getRawLocationURI().toString();
properties.clear(); slotContents.clear();
properties.put("modelFile", modelFilePath);
properties.put("src-gen", containerName);
runner.run(wfFile, new NullProgressMonitor(), properties, slotContents) ;

}
else{ // user selected metamodel file, so just generate ASTs
wfFile = "workflow/M2generator.mwe";
String modelFilePath = resource.getRawLocationURI().toString();
properties.clear(); slotContents.clear();
properties.put("modelFile", modelFilePath);
properties.put("src-gen", containerName);
runner.run(wfFile, new NullProgressMonitor(), properties, slotContents) ;
}
===================================================================

So, how can I make this work? Please help ...

Thanks in advance.
Re: [XPAND] Adding a template to classpath [message #989970 is a reply to message #989196] Mon, 10 December 2012 05:14 Go to previous messageGo to next message
Bhanu Pratap is currently offline Bhanu Pratap
Messages: 13
Registered: April 2010
Junior Member
Really disappointed to have no replies so far Sad
Re: [XPAND] Adding a template to classpath [message #990184 is a reply to message #989196] Tue, 11 December 2012 05:21 Go to previous messageGo to next message
Karsten Thoms is currently offline Karsten Thoms
Messages: 353
Registered: July 2009
Location: Dortmund
Senior Member

I was pointed to this thread by a colleague.

Basically I see no reason why this scenarion should not be realizable.

In your plugin, just execute a second workflow that processes the previously generated Xpand file. There is no need to run this in a single workflow, although also here I see no reason why this won't work.

Of course it must be known what the name of the dynamic definition is, to fill the value of "expand" for the second Generator component.

component = Generator {
 ...
 expand = "my::xpand::generator::start" // invoke definition "start" in template "generator.xpt" in package "my/xpand"; 
 // produce generator2.xpt with DEFINE 'start'
}
component = Generator {
 expand = "my::xpand::generator2::start" // the dynamic template
}


Note that for loading resources the ResourceManager is reponsible. This needs to be able to load the dynamic resource afterwards. Maybe you have your problem here...? Set a breakpoint in ResourceManagerDefaultImpl#loadResource() to understand if your dynamic template is loaded and if not, why not. (The why is obvious: Classpath issue)

You may attach a minimal plugin set to state your case more clearly.

Regards,
~Karsten



Need professional support for Xtext, Xpand, EMF?
Go to: http://xtext.itemis.com
Twitter : @kthoms
Blog : www.karsten-thoms.de
Re: [XPAND] Adding a template to classpath [message #990530 is a reply to message #990184] Wed, 12 December 2012 17:17 Go to previous message
Bhanu Pratap is currently offline Bhanu Pratap
Messages: 13
Registered: April 2010
Junior Member
Hi Karsten

Thank you for your reply. Your are right, the problem is related to classpath. I tried to load this generated template using ResouceManagerDefaultImpl and it fails too. I looked further into this classpath related issue and found out that the above way of adding directories to classpath will not work(though it simply modifies the property 'java.classpath', its dynamic update cannot be realized, may be due to some security reasons, to do so one is suppose to implement/extend their own resource loaders).

So, I guess the actual problem here is to add user's working directory(or the directory in which template is generated by user) to resource manager's classpath entries. And I could think of two ways to solve this problem:

1. Obvious one: Programmatically/dynamically add this template to ResourceManager's classpath

2. Construct and provide absolute path of generated template to Xpand's "org.eclipse.xpand2.Generator" component.


So far, I couldn't find any feasible solution for 1st Option.
And for 2nd Option, does XPAND allow us to do so? Like the way we specify "uri" property in case of "org.eclipse.emf.mwe.utils.Reader" component for the model file.

Thanks.

[Updated on: Wed, 12 December 2012 17:20]

Report message to a moderator

Previous Topic:[Xpand] Running Xpand Workflow via Equinox Launcher
Next Topic:JET2 Transform not working
Goto Forum:
  


Current Time: Thu Jul 24 11:26:09 EDT 2014

Powered by FUDForum. Page generated in 0.01922 seconds