Xpand and GMF [message #1738803] |
Sun, 24 July 2016 17:51  |
Eclipse User |
|
|
|
Hi,
I am a newbie, currently I am developing an eclipse project using GMF and Xpand. Right now I am having a trouble in invoking Xpand template from gmf diagram eclipse workbench.
The idea is to create a graphical editor from ecore model with GMF and then using popup menu on the created model to invoke the Xpand template and generate an output code.
I have tried to google example for this but found none so far. Only a tiny pieces of information available on google. I could use a simple example for GMF with Xpand. Or any tutorial on how to do this.
Thanks.
|
|
|
Re: Xpand and GMF [message #1738889 is a reply to message #1738803] |
Mon, 25 July 2016 13:32   |
Eclipse User |
|
|
|
My action class (src/ont.diagram.handlers/GenerateTurtleAction.java):
package ont.diagram.handlers;
import java.net.URI;
import java.net.URL;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.mwe.core.WorkflowRunner;
import org.eclipse.emf.mwe.core.monitor.NullProgressMonitor;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.xpand2.XpandExecutionContextImpl;
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 org.osgi.framework.Bundle;
import ont.OntPackage;
public class GenerateTurtleAction implements IObjectActionDelegate {
private Shell shell;
private IWorkbenchPart targetPart;
private IFile file;
public void setActivePart(IAction action, IWorkbenchPart targetPart) {
this.targetPart = targetPart;
shell = targetPart.getSite().getShell();
}
public void selectionChanged(IAction action, ISelection selection) {
action.setEnabled(false);
if (selection instanceof IStructuredSelection == false || selection.isEmpty()) {
return;
}
file = (IFile) ((IStructuredSelection) selection).getFirstElement();
action.setEnabled(true);
}
public void run(IAction action) {
try {
EObject source = getInput();
if (source == null) {
String title = "title";
String message = "message";
MessageDialog.openInformation(getShell(), title, NLS.bind(message, file.getFullPath()));
} else {
Bundle bundle = Platform.getBundle("ontology.diagram");
URL url = FileLocator.find(bundle, new Path("src/templates/Generator.mwe"), null);
url = FileLocator.toFileURL(url);
// URL url = FileLocator.toFileURL(new URL("platform:/resource/ontology.diagram/src/templates/Generator.mwe"));
Map<String, String> properties = new HashMap<String, String>();
properties.put("model", URI.createPlatformResourceURI(file.getFullPath().toString(), true).toString());
properties.put("out", Platform.getLocation().toOSString() + file.getParent().getFullPath());
WorkflowRunner runner = new WorkflowRunner();
runner.run(url.getPath(), new NullProgressMonitor(), properties, null);
file.getParent().refreshLocal(IResource.DEPTH_ONE, new org.eclipse.core.runtime.NullProgressMonitor());
MessageDialog.openInformation(
shell,
"Generate Turtle",
"Successful!");
}
} catch (Exception ex) {
handleError(ex);
}
}
private EObject getInput() {
ResourceSetImpl rs = new ResourceSetImpl();
return rs.getEObject(URI.createPlatformResourceURI(file.getFullPath().toString(), true).appendFragment("/"), true);
}
private void handleError(Throwable ex) {
MessageDialog.openError(getShell(), "Transformation failed", MessageFormat.format("{0}: {1}", ex.getClass().getSimpleName(), ex.getMessage() == null ? "no message" : ex.getMessage()));
}
private Shell getShell() {
return targetPart.getSite().getShell();
}
}
My Xpand template (src/templates/TurtleTemplate.xpt):
«IMPORT http:///ont.ecore»
«DEFINE main FOR Ontology-»
«FILE ontologyDescription.ontLabel + ".txt"-»
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix base <«ontologyDescription.ontURI»#> .
#################################################################
#
# Classes
#
#################################################################
«EXPAND rdfclass FOREACH rDFClasses-»
#################################################################
#
# Properties
#
#################################################################
«EXPAND rdfproperty FOREACH rDFProperties-»
«ENDFILE»
«ENDDEFINE»
«DEFINE rdfclass FOR RDFSClass-»
«classLabel» rdf:type rdfs:Class «EXPAND subclass FOREACH subClassOf-».
«ENDDEFINE-»
«DEFINE subclass FOR RDFSClass-»
;
rdfs:subClassOf «classLabel-»
«ENDDEFINE-»
«DEFINE rdfproperty FOR RDFSProperty-»
«propertyLabel» rdf:type rdf:Property «EXPAND subproperty FOREACH subPropertyOf-» «EXPAND dom FOREACH domain-» «EXPAND ran FOREACH range-».
«ENDDEFINE-»
«DEFINE subproperty FOR RDFSProperty-»
;
rdfs:subPropertyOf «propertyLabel-»
«ENDDEFINE-»
«DEFINE dom FOR RDFSClass-»
;
rdfs:domain «classLabel-»
«ENDDEFINE-»
«DEFINE ran FOR RDFSClass-»
;
rdfs:range «classLabel-»
«ENDDEFINE-»
My workflow (Generator.mwe):
<?xml version="1.0"?>
<workflow>
<bean class="org.eclipse.emf.mwe.utils.StandaloneSetup">
<platformUri value="../" />
<RegisterEcoreFile value="http:///ont.ecore"/>
</bean>
<component class="org.eclipse.emf.mwe.utils.Reader">
<uri value="${model}" />
<modelSlot value="model" />
<firstElementOnly value="false" />
</component>
<component class="org.eclipse.xpand2.Generator">
<metaModel id="mm" class="org.eclipse.xtend.typesystem.emf.EmfRegistryMetaModel"/>
<expand value="templates::TurtleTemplate::main FOR model" />
<outlet path="${}">
<postprocessor class="org.eclipse.xpand2.output.XmlBeautifier" />
</outlet>
</component>
</workflow>
when i run it as eclipse application, the action only display the messaedialog. No file is generated. and no exception too.
|
|
|
|
|
|
|
|
|
|
Re: Xpand and GMF [message #1738945 is a reply to message #1738936] |
Tue, 26 July 2016 05:16   |
Eclipse User |
|
|
|
I think this part might be the problem, but not sure how to correct it
Map<String, String> properties = new HashMap<String, String>();
properties.put("model", URI.createPlatformResourceURI(file.getFullPath().toString(), true).toString());
properties.put("out", Platform.getLocation().toOSString() + file.getParent().getFullPath());
WorkflowRunner runner = new WorkflowRunner();
runner.run(url.getPath(), new NullProgressMonitor(), properties, null)
|
|
|
|
|
|
|
|
|
Re: Xpand and GMF [message #1739110 is a reply to message #1739033] |
Wed, 27 July 2016 10:53   |
Eclipse User |
|
|
|
Karsten Thoms wrote on Wed, 27 July 2016 06:31parseInitAndCreate() cannot take long. It parses the Workflow file, and it is fast.
workflow.invoke() can take time, since this is your actual workflow executing.
<platformUri value="../" />
can take time. It goes one level up in the directory and from there it scans recursively for Eclipse project directories with a .project file. In your case "." should be enough. It is convenience to use ".." for multi-projects, e.g. if your Ecore file is in another project.
Also suspicious is that you use a http URL for the RegisterEcoreFile property. I would expect there "platform:/resource/PROJECTNAME_WITH_ECORE/PATH_TO_ECORE/ont.ecore".
Otherwise a HTTP connection is opened and might run into timeout, explaining the delay.
When i debug i still found that it took some second longer than other step in parseInitAndCreate() part. Took few seconds.
The I adjusted mwe as follow
<platformUri value="./" />
<RegisterEcoreFile value="platform:/resource/ontology/model/ont.ecore"/>
now i got this error:
[[ERROR]: Error creating instance of type 'org.eclipse.emf.mwe.utils.StandaloneSetup' : org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1DiagnosticWrappedException: org.eclipse.core.internal.resources.ResourceException: Resource '/ontology/model/ont.ecore' does not exist.(Element: bean bean class='org.eclipse.emf.mwe.utils.StandaloneSetup' in /C:/Users/User/eclipse/java-mars2/eclipse/../../../workspace/ontology.diagram/src/templates/Generator.mwe:4; Reported by: -UNKNOWN-), null, null, null, null, null, null, null, null, null]
I think its because the action run mwe from the eclipse instance while the ecore file is in the main eclipse. Is it? if that is the case, any idea how can i solve it? Thx.
|
|
|
|
Re: Xpand and GMF [message #1740961 is a reply to message #1739113] |
Sun, 21 August 2016 12:44  |
Eclipse User |
|
|
|
@karsten
Hi thx for ur help the other day. I manage to get it working great. tho i need to about new problem i encounter.
I deploy it as a plugin. But when i install n tried to generate i encounter nullpointer
i suspect one of this code might be responsible for that
Bundle bundle = Platform.getBundle("org.eclipse.ontology.diagram");
URL url = FileLocator.find(bundle, new Path("src/template/Generator.mwe"), null);
url = FileLocator.toFileURL(url);
its working okay if i run it as eclipse application. but not if i installed it into my eclipse n try it.
|
|
|
Powered by
FUDForum. Page generated in 0.05670 seconds