Home » Modeling » M2T (model-to-text transformation) » Getting the name of an element which is itself an attribute value
|
Re: Getting the name of an element which is itself an attribute value [message #44706 is a reply to message #44675] |
Sat, 26 April 2008 15:19 |
Paul Elder Messages: 849 Registered: July 2009 |
Senior Member |
|
|
Georges:
The GMF editor is producing a file that has an EMF meta-model. Provided that
meta model is registered correctly (via the
org.eclipse.emf.ecore.generated_package extension point in the EMF project
defining the model), JET should be able to read that model.
Note that if JET recognizes a file as being produced by EMF, JET DOES NOT
parse it as an XML document. Instead, it uses EMF to load the model, and
then uses EMF's reflective APIs to navigate the document. When using this
API, XPath steps will match elements in the model as follows:
* an initial / corresonds to the EMF Resource containing the model.
* a child-step (syntax /foo or /child::foo) will try to find objects in two
ways:
* First, it will look for an EReference feature on the current context
object with name 'foo'. If found, it will match all the objects refered to
by the feature.
* Seond, it will look in the eContents collection of the current object
for an object whose eClass().getName() equals foo (case sensitive).
* an attribute step (syntax /@name or /attribute::name) is matched as
follows:
* JET will look for an EAttribute feature on the current context object
with name 'name'. If found, it returns that object.
So, some examples form you model (I'm assuming is serialized in XMI, so
though, I'm not quite sure...
* To get the flowName of the root object:
<c:get select="/Flow/@flowName"/>
* To traverse all the nodes in Flow:
<c:iterate select="/Flow/nodes" var="node">
<c:get select="$node/@nodeName"/>
</c:iterate>
* To find all the nodes output linked from each node...
<c:iterate select="/Flow/nodes" var="node">
Node: <c:get select="$node/@nodeName"/>
<c:iterate select="$node/outputLinks" var="link">
</c:iterate>
<%-- I'm assuming SubFlow has a reference feature
outputLinks (type OutputLinks) and the targetNode is a reference feature,
too --%>
<c:get select="$link/targetNode/@nodeName"/>
</c:iterate>
So, assuming my guesses about the EMF model are correct, the JET template
for the XML document would look like this:
<%-- templates/outputFile.xml.jet --%>
<?xml version="1.0" encoding="utf-8"?>
<tns:flow name="<c:get select=/Flow/@flowName/>" >
<bin_table><c:get select="/Flow/BinTable/@name"/></bin_table>
<c:iterate select="/Flow/nodes" var="node">
<c:if test="$node/@inputLabel">
<input_label><c:get select="$node/@inputLabel"/></input_label>
</c:if>
<node name="<c:get select="$node/@nodeName"/>">
<c:iterate select="$node/outputLinks" var="link">
<output_label_<c:get select="$link/@ordinal"/>><c:get
select="$link/@inputLabel"/></output_label_<c:get select="$link/@ordinal"/>>
</c:iterate>
</node>
</c:iterate>
</tns:flow>
You may have noticed that I used an attribute (@ordinal) which is not
defined on your GMF-produced model.. It's created in main.jet:
<%-- templates/main.jet --%>
<c:iterate select="/Flow/nodes" var="node">
<c:setVariable var="nextOrdinal" select="1"/>
<c:iterate select="$node/outputLinks" var="link">
<c:set select="$link" name="ordinal"><c:get
select="$nextOrdinal"/></c:set>
<c:setVariable var="nextOrdinal" select="$nextOrdinal + 1"/>
</c:iterate>
</c:iterate>
<%-- Don't know what method you will use to get the name of the output file.
Here, I'm assuming
that it is set in a JET variable. You could set it a number of ways,
depending on how the transformation is invoked --%>
<ws:file template="templates/outputfile.xml.jet" path="{$outputFilePath}" />
Paul
|
|
| | |
Re: Getting the name of an element which is itself an attribute value [message #44832 is a reply to message #44775] |
Wed, 30 April 2008 06:00 |
Eclipse User |
|
|
|
Originally posted by: georges.bachelier.atmel.com
Hello Paul,
thank you very much for your time and teaching; I have learned a lot about
JET in a short time and our transformation works fine now.
I have one more question though: the transformation works only if it is
performed from a second instance of Eclipse on which my plug-ins are
loaded. The transformation is launched as follows from our GMF-based
editor on diagram saving :
../..
IDiagramDocument diagramDocument = ( IDiagramDocument ) document;
TransactionalEditingDomain domain =
diagramDocument.getEditingDomain( );
XMIResourceImpl resource = ( XMIResourceImpl )
domain.getResourceSet( ).getResources( ).get( 1 );
Map< String, String > variables = new HashMap< String, String >( );
variables.put( "org.eclipse.jet.resource.project.name",
resource.getURI( ).segments( )[ 1 ] );
IStatus status = JET2Platform.runTransformOnObject(
"com.xxx.yyy.testfloweditor.jet", resource, variables, new
NullProgressMonitor( ) );
../..
No problem with this. But, if I run the transformation in Eclipse through
a "JET transformation" run configuration, I get the following error
message:
templates/flow_template.jet(48,52): <c:get
select="$link/targetNode/@inputLabel">
Error: XPath expression returned no result
It seems that the 'path'to the 'inputLabel' attribute cannot be resolved.
Although I am happy with the current JET results, it would save a lot of
time if we could perform a successful transformation from Eclipse.
What do you think about this? Is there anything I can tune to make this
work?
Best regards,
Georges
|
|
|
Re: Getting the name of an element which is itself an attribute value [message #44870 is a reply to message #44832] |
Wed, 30 April 2008 15:07 |
Paul Elder Messages: 849 Registered: July 2009 |
Senior Member |
|
|
Georges:
In order to read you model, JET requires information on the ECore package
describing your model. I'm guessing that the plug-in defining Flow Model is
located in your development workspace.
When JET attempts to load a file from disk, it looks for an registered ECore
package for the files extension. But, as your EMF plug-in is in the
workspace the association with the extension has not happen. In this case,
JET falls back to loading the document as XML. This doesn't work in your
case becuase your documents are serialized as XMI. (XML and XMI make this
all very confusing. Bottom line is that the XPath expressions are not the
same if the model is loaded as a pure XML document rather than interpreted
as XMI.)
But, all is not lost! Do the following to let JET know that it should load
your JET transformations input using EMF.
a) Open plugin.xml
b) Swtich to the Extensions tab
c) Expand org.eclipse.jet.transform
d) Click (transform)
e) In the modelLoader field, enter org.eclipse.jet.emf
f) Save and close the plug-in editor.
Works like a charm (in my tests, anyhow)!
Paul
"Georges" <georges.bachelier@atmel.com> wrote in message
news:fe9d169e22cac7f7c37c6ff2a40368b4$1@www.eclipse.org...
> Hello Paul,
>
> thank you very much for your time and teaching; I have learned a lot about
> JET in a short time and our transformation works fine now.
> I have one more question though: the transformation works only if it is
> performed from a second instance of Eclipse on which my plug-ins are
> loaded. The transformation is launched as follows from our GMF-based
> editor on diagram saving :
>
> ./..
> IDiagramDocument diagramDocument = ( IDiagramDocument ) document;
> TransactionalEditingDomain domain =
> diagramDocument.getEditingDomain( );
> XMIResourceImpl resource = ( XMIResourceImpl )
> domain.getResourceSet( ).getResources( ).get( 1 );
> Map< String, String > variables = new HashMap< String, String >( );
> variables.put( "org.eclipse.jet.resource.project.name",
> resource.getURI( ).segments( )[ 1 ] );
> IStatus status = JET2Platform.runTransformOnObject(
> "com.xxx.yyy.testfloweditor.jet", resource, variables, new
> NullProgressMonitor( ) );
> ./..
>
> No problem with this. But, if I run the transformation in Eclipse through
> a "JET transformation" run configuration, I get the following error
> message:
>
> templates/flow_template.jet(48,52): <c:get
> select="$link/targetNode/@inputLabel">
> Error: XPath expression returned no result
>
> It seems that the 'path'to the 'inputLabel' attribute cannot be resolved.
> Although I am happy with the current JET results, it would save a lot of
> time if we could perform a successful transformation from Eclipse.
> What do you think about this? Is there anything I can tune to make this
> work?
>
> Best regards,
>
> Georges
>
>
>
|
|
| | | | |
Goto Forum:
Current Time: Fri Sep 20 19:22:48 GMT 2024
Powered by FUDForum. Page generated in 0.05122 seconds
|