Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » Xpand and GMF
Xpand and GMF [message #1738803] Sun, 24 July 2016 21:51 Go to next message
Wendy Wendy is currently offline Wendy WendyFriend
Messages: 21
Registered: July 2016
Junior Member
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 17:32 Go to previous messageGo to next message
Wendy Wendy is currently offline Wendy WendyFriend
Messages: 21
Registered: July 2016
Junior Member
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 #1738919 is a reply to message #1738803] Tue, 26 July 2016 07:04 Go to previous messageGo to next message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 762
Registered: July 2009
Location: Dortmund, Germany
Senior Member

Basically does not look that bad. What's exactly your problem?
Re: Xpand and GMF [message #1738925 is a reply to message #1738919] Tue, 26 July 2016 07:49 Go to previous messageGo to next message
Wendy Wendy is currently offline Wendy WendyFriend
Messages: 21
Registered: July 2016
Junior Member
No file is generated on the workbench Sad
and no exception too.
Re: Xpand and GMF [message #1738926 is a reply to message #1738925] Tue, 26 July 2016 07:55 Go to previous messageGo to next message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 762
Registered: July 2009
Location: Dortmund, Germany
Senior Member

And is the workflow executed? Does the Reader component read files into slot 'model'?
Use the debugger and set a breakpoint in the Reader's load() method.


Need professional support for Xtext, EMF, Eclipse IDE?
Go to: http://devhub.karakun.com
Twitter : @kthoms
Blog : www.karsten-thoms.de
Re: Xpand and GMF [message #1738928 is a reply to message #1738926] Tue, 26 July 2016 08:06 Go to previous messageGo to next message
Wendy Wendy is currently offline Wendy WendyFriend
Messages: 21
Registered: July 2016
Junior Member
Do you mean the workflowrunner?
Re: Xpand and GMF [message #1738929 is a reply to message #1738928] Tue, 26 July 2016 08:09 Go to previous messageGo to next message
Wendy Wendy is currently offline Wendy WendyFriend
Messages: 21
Registered: July 2016
Junior Member
and btw the .run seem to be deprecated is it ok?
Re: Xpand and GMF [message #1738930 is a reply to message #1738928] Tue, 26 July 2016 08:10 Go to previous messageGo to next message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 762
Registered: July 2009
Location: Dortmund, Germany
Senior Member

Yes, you could debug into the WorkflowRunner to see if the workflow was successfully loaded and executed. The could set a breakpoint in WorkflowEngine#executeWorkflow. The process should stop at the breakpoint. Next indicator is Reader#load() to see if your model is actually loaded. If so, it should be placed into the 'model' slot of the WorkflowContext. Is that the case?
Re: Xpand and GMF [message #1738936 is a reply to message #1738930] Tue, 26 July 2016 08:44 Go to previous messageGo to next message
Wendy Wendy is currently offline Wendy WendyFriend
Messages: 21
Registered: July 2016
Junior Member
I couldnt find the Reader part. Im not sure where to look. I find the executeWorkflow tho
Re: Xpand and GMF [message #1738945 is a reply to message #1738936] Tue, 26 July 2016 09:16 Go to previous messageGo to next message
Wendy Wendy is currently offline Wendy WendyFriend
Messages: 21
Registered: July 2016
Junior Member
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 #1738962 is a reply to message #1738945] Tue, 26 July 2016 10:57 Go to previous messageGo to next message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 762
Registered: July 2009
Location: Dortmund, Germany
Senior Member

Without debugging into the process this is hard to tell. Did you enable debugging of the process and set a break point in class WorkflowEngine, method executeWorkflow? Does the debugger stop there?
Re: Xpand and GMF [message #1738966 is a reply to message #1738803] Tue, 26 July 2016 11:10 Go to previous messageGo to next message
Wendy Wendy is currently offline Wendy WendyFriend
Messages: 21
Registered: July 2016
Junior Member
Yes. It run and break there. But the engine return false.

I think its because

0    INFO  WorkflowEngine     - --------------------------------------------------------------------------------------
419  INFO  WorkflowEngine     - EMF Modeling Workflow Engine 1.3.13, Build v201512160556
423  INFO  WorkflowEngine     - (c) 2005-2009 openarchitectureware.org and contributors
428  INFO  WorkflowEngine     - --------------------------------------------------------------------------------------
435  INFO  WorkflowEngine     - running workflow: file:/C:/Users/User/workspace/ontology.diagram/src/templates/Generator.mwe
443  INFO  WorkflowEngine     -
1047 ERROR WorkflowEngine     - [ERROR]: property model not specified. Dereferenced at in file:/C:/Users/User/workspace/ontology.diagram/src/templates/Generator.mwe:10(Element: -UNKNOWN-; Reported by: -UNKNOWN-)
1064 ERROR WorkflowEngine     - Workflow interrupted because of configuration errors.


property model not specified? Isnt it already specified in the property?

[Updated on: Tue, 26 July 2016 12:38]

Report message to a moderator

Re: Xpand and GMF [message #1738978 is a reply to message #1738966] Tue, 26 July 2016 13:27 Go to previous messageGo to next message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 762
Registered: July 2009
Location: Dortmund, Germany
Senior Member

With that message we can do something. It says 'property model not specified'. This property is used in the workflow:
<uri value="${model}" />


You are putting this property in the param map:
properties.put("model", ...)


Please make sure that the 'model' param is passed properly to the engine. Set a breakpoint WorkflowFactory#parseInitAndCreate and see is 'params' contains 'model' key with the proper value.

If that's still a problem set a breakpoint in IssuesImpl and see when addError() is invoked and why.
Re: Xpand and GMF [message #1738981 is a reply to message #1738978] Tue, 26 July 2016 14:04 Go to previous messageGo to next message
Wendy Wendy is currently offline Wendy WendyFriend
Messages: 21
Registered: July 2016
Junior Member
yes the params contains model key. Now im tracking it to the WorkflowFactory, the debug always end up not responding and i have to force close it Sad
Re: Xpand and GMF [message #1739000 is a reply to message #1738981] Tue, 26 July 2016 16:36 Go to previous messageGo to next message
Wendy Wendy is currently offline Wendy WendyFriend
Messages: 21
Registered: July 2016
Junior Member
I think this part might be the reason but not sure why

        <outlet path="${out}">
        	<postprocessor class="org.eclipse.xpand2.output.XMLBeautifier" />
        </outlet>


this part raise issues

[[ERROR]: Class not found: 'org.eclipse.xpand2.output.XMLBeautifier'(Element: bean postprocessor class='org.eclipse.xpand2.output.XMLBeautifier' in /C:/Users/User/eclipse/java-mars2/eclipse/../../../workspace/ontology.diagram/src/templates/Generator.mwe:18; Reported by: -UNKNOWN-)]

but when i remove this beautifier part, it is not responding when i try to debug

ok now this part is corrected by

        	<postprocessor class="org.eclipse.xtend.typesystem.xsd.XMLBeautifier" />


Thx, finally i got it running. I used the old template which use RDFSProperty instead of RDFProperty. thats where i go wrong.

When i debug i found there is this code that took long to run. Any idea what is the problem?

WorkflowEngine:
workflow = factory.parseInitAndCreate(workFlowFile, params, getConverters(), issues);



WorkflowEngine.executeWorkflow:
workflow.invoke(wfContext, monitor, issues);

[Updated on: Tue, 26 July 2016 19:21]

Report message to a moderator

Re: Xpand and GMF [message #1739033 is a reply to message #1739000] Wed, 27 July 2016 06:31 Go to previous messageGo to next message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 762
Registered: July 2009
Location: Dortmund, Germany
Senior Member

parseInitAndCreate() 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.


Need professional support for Xtext, EMF, Eclipse IDE?
Go to: http://devhub.karakun.com
Twitter : @kthoms
Blog : www.karsten-thoms.de
Re: Xpand and GMF [message #1739110 is a reply to message #1739033] Wed, 27 July 2016 14:53 Go to previous messageGo to next message
Wendy Wendy is currently offline Wendy WendyFriend
Messages: 21
Registered: July 2016
Junior Member
Karsten Thoms wrote on Wed, 27 July 2016 06:31
parseInitAndCreate() 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 #1739113 is a reply to message #1739110] Wed, 27 July 2016 15:02 Go to previous messageGo to next message
Wendy Wendy is currently offline Wendy WendyFriend
Messages: 21
Registered: July 2016
Junior Member
Oh just change the "platform:/resource" into "platform:/plugin" ???
Re: Xpand and GMF [message #1740961 is a reply to message #1739113] Sun, 21 August 2016 16:44 Go to previous message
Wendy Wendy is currently offline Wendy WendyFriend
Messages: 21
Registered: July 2016
Junior Member
@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.
Previous Topic:Different behaviors in Eclipse and in Maven
Next Topic:news.eclipse.org is shutting down.
Goto Forum:
  


Current Time: Sat Nov 09 02:04:55 GMT 2024

Powered by FUDForum. Page generated in 0.04811 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top