How to invoke Xpand template into a Java-code???? [message #637685] |
Mon, 08 November 2010 10:55  |
Eclipse User |
|
|
|
I created a plug-in (I work with the Yakindu Workbench).
This Plug-in should generate Java code for state machine diagram (statemachine-File).
So i have a custom action (ActionGenerateJavaCode.java) that should invoke the Xpand template.
When I run the action, then I get the following error message:
EvaluationException : No Definition templates::Statechart::Root for statemachine::Statechart could be found!
Internal error: element was null
the custom action looks like this:
import org.eclipse.core.resources.IFile;
import org.eclipse.emf.common.util.URI;
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.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.xpand2.XpandExecutionContextImpl;
import org.eclipse.xpand2.XpandFacade;
import org.eclipse.xpand2.output.Outlet;
import org.eclipse.xpand2.output.Output;
import org.eclipse.xpand2.output.OutputImpl;
import org.eclipse.xtend.typesystem.emf.EmfMetaModel;
import statemachine.impl.StatechartImpl;
public class ActionGenerateJavaCode implements IObjectActionDelegate{
private ISelection selection;
@Override
public void run(IAction action) {
try{
IFile file = getFile();
URI fileURI = null;
java.net.URI uri = file.getLocationURI();
fileURI = URI.createURI(uri.toString(), true);
ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.getPackageRegistry().put(
statemachine.StatemachinePackage.eINSTANCE.getNsURI(),
statemachine.StatemachinePackage.eINSTANCE);
Resource resource = resourceSet.createResource(fileURI);
try {
resource.load(null);
}
catch (Exception e) {
e.printStackTrace();
}
resourceSet.getResources().add(resource);
StatechartImpl statechart = (StatechartImpl) resource.getContents().get(0);
//Xpand
Output out = new OutputImpl();
out.addOutlet(new Outlet("src-gen"));
XpandExecutionContextImpl executionContext = new XpandExecutionContextImpl(out, null);
//Configure the metamodels
EmfMetaModel emfMetaModel = new EmfMetaModel();
emfMetaModel.setMetaModelPackage(statemachine.StatemachinePa ckage.class.getName());
emfMetaModel.setMetaModelPackage(statechartexpressions.State chartexpressionsPackage.class.getName());
executionContext.registerMetaModel(emfMetaModel);
XpandFacade xpandFacade = XpandFacade.create(executionContext);
Object [] params =null;
xpandFacade.evaluate("templates::Statechart::Root", statechart, params);
System.out.println("End generate code");
}
catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void selectionChanged(IAction action, ISelection selection) {
this.selection = selection;
}
@Override
public void setActivePart(IAction action, IWorkbenchPart targetPart) {
}
public IFile getFile(){
return (IFile)((IStructuredSelection)selection).getFirstElement();
}
}
|
|
|
Re: How to invoke Xpand template into a Java-code???? [message #638169 is a reply to message #637685] |
Wed, 10 November 2010 09:50   |
Eclipse User |
|
|
|
Hello IdaEss,
the code look good. Please check if the Templatefile Statechart containing the Define Root is accessible by your code. Do you run the code from within a plugin? Is the template accessible within the plugin?
IdaEss wrote on Mon, 08 November 2010 11:55 |
EvaluationException : No Definition templates::Statechart::Root for statemachine::Statechart could be found!
Internal error: element was null
|
Regards
Darius
|
|
|
Re: How to invoke Xpand template into a Java-code???? [message #638246 is a reply to message #638169] |
Wed, 10 November 2010 16:12  |
Eclipse User |
|
|
|
Hi,
How to know that the templateFile is accessible by the code
My template file looks like this:
«IMPORT statemachine»
«IMPORT statechartexpressions»
«EXTENSION com::yakindu::statechart::codegenerator::java::extensions::E xtensions»
«IMPORT com::yakindu::statechart::codegenerator::java::templates::ge neric»
«DEFINE Root FOR Statechart»
«FILE getImplPackagePath(this) + getStatechartImplClassName(this) + "TestCase.java"»
«REM» »package «getImplPackage(this)»;«ENDREM»
«EXPAND Imports»
«EXPAND BasicImports»
«EXPAND MessinaImports»
«EXPAND ExceptionsImports»
public class «getStatechartImplClassName(this)»TestCase extends AllSignals
{
«getStatechartImplClassName(this)» statechart;
public int run() throws IOException, TestFailedException,
InterruptedException {
...
...
...
}
«ENDDEFINE»
When i execute my Plug-in (Right click on the project -> Run As -> Eclipse Application) then a second Yakindu Workbench (Yakindu instance) starts.
I created a new Java project in this second workbench and ein added a state machine diagramm (MyStatemachine). The context menu of the statemachine file (MyStatemachine.statemachine) contains a the entry "Generate Java-code". If i click on this entry then the java-code of the statemachine should be created but it doesn't work . Instead of i get the error message on the first workbench
error message:
EvaluationException : No Definition templates::Statechart::Root for statemachine::Statechart could be found!
Internal error: element was null
Best regards
Ida
|
|
|
Powered by
FUDForum. Page generated in 0.03831 seconds