Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » [solved] Acceleo loop problem(elements recursion because of inheritance)
[solved] Acceleo loop problem [message #1733768] Tue, 31 May 2016 14:45 Go to next message
Viktoria Fink is currently offline Viktoria FinkFriend
Messages: 12
Registered: May 2016
Junior Member
Hallo,

I started with Acceleo to create a tool to generate text from a petrinet-model. My problem is that my whole template loops as much as I have elements (here nodes) in my xmi where I get my Information from. That is if I tell the file to append. The problem for append=false is another one. In this case my file stays empty or at least the text which is not in a loop is written once but anything in the for loop doesn't.

[comment encoding = UTF-8 /]
[module generate('http://pipenet.model')]

[template public generateElement(aPipenet : Pipenet) post(trim())]
[comment @main/]

[file (clearFile('out.txt'), false, 'UTF-8')]
//*****Begin of User Code*****
[for (nodes : Node | aPipenet.nodes)]
[if (nodes.oclIsTypeOf(Transition))]
[ifBLOCK(nodes, aPipenet)/] 

[/if]
[/for]
//*****End of User Code*****
[/file]
[/template]

[template public ifBLOCK(aNode:Node, aPipenet:Pipenet) post(trim())]
[let n : Node = aNode]
IF ([for (nodes : Node | aPipenet.nodes)]
		[if (n.arcs.source = nodes.arcs.target)][nodes.name/] [/if]
	[/for] AND [for (nodes : Node | aPipenet.nodes)]
		[if (n.arcs.target = nodes.arcs.source)] NOT [nodes.name/][/if]
	[/for]) AND [aNode.name/] THEN 
	[for (nodes : Node | aPipenet.nodes)][if (n.arcs.source = nodes.arcs.target)][nodes.name/] = false; [/if][/for]
	[for (nodes : Node | aPipenet.nodes)][if (n.arcs.target = nodes.arcs.source)][nodes.name/] = true; [/if][/for]
END_IF;
[/let]
[/template]


My output for append=false:
//*****Begin of User Code*****

//*****End of User Code*****

My output for append=true:
//*****Begin of User Code*****
something I got from the xmi
//*****End of User Code*****
x-times

I looked for some solution in the forums but my search wasn't successful. I thought I could solve it with a query but that is not going to work because my query would be called from inside the file that means it loops as well. Maybe someone knows the answer to everything and to my problem as well Wink

[Updated on: Mon, 13 June 2016 12:09]

Report message to a moderator

Re: Acceleo loop problem [message #1733861 is a reply to message #1733768] Wed, 01 June 2016 14:05 Go to previous messageGo to next message
Laurent Goubet is currently offline Laurent GoubetFriend
Messages: 1902
Registered: July 2009
Senior Member
Hi,

I have absolutely no idea what you might have as an issue, or what you don't.

[template public generateElement(aPipenet : Pipenet) post(trim())] 

this is your "main" template, so it will be called once for every single "Pipenet" element in your input model. Since you have a file block in there, it means that that file either needs to have a name that changes according to the current Pipenet element, or be in append mode. Otherwise it'll just be overriden for each iteration and the end result will be that of the last Pipenet element; which in your case seems to output nothing.

Please describe how your model looks like and what your are trying to achieve, we migth be able to better help.

Laurent Goubet
Obeo
Re: Acceleo loop problem [message #1734416 is a reply to message #1733861] Wed, 08 June 2016 07:46 Go to previous messageGo to next message
Viktoria Fink is currently offline Viktoria FinkFriend
Messages: 12
Registered: May 2016
Junior Member
Hi,

my xmi-file to my Problem looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<pipenet.model:Pipenet xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:pipenet.model="http://pipenet.diagram.model">
  <nodes xsi:type="pipenet.model:Place" name="S0" presetNodes="//@nodes.7" postsetNodes="//@nodes.1">
    <arcs source="//@nodes.0" target="//@nodes.1"/>
  </nodes>
  <nodes xsi:type="pipenet.model:Transition" name="T1" presetNodes="//@nodes.0" postsetNodes="//@nodes.2">
    <arcs source="//@nodes.1" target="//@nodes.2"/>
  </nodes>
  <nodes xsi:type="pipenet.model:Place" name="S1" presetNodes="//@nodes.1" postsetNodes="//@nodes.3">
    <arcs source="//@nodes.2" target="//@nodes.3"/>
  </nodes>
  <nodes xsi:type="pipenet.model:Transition" name="T2" presetNodes="//@nodes.2" postsetNodes="//@nodes.4">
    <arcs source="//@nodes.3" target="//@nodes.4"/>
  </nodes>
  <nodes xsi:type="pipenet.model:Place" name="S2" presetNodes="//@nodes.3" postsetNodes="//@nodes.5">
    <arcs source="//@nodes.4" target="//@nodes.5"/>
  </nodes>
  <nodes xsi:type="pipenet.model:Transition" name="T3" presetNodes="//@nodes.4" postsetNodes="//@nodes.6">
    <arcs source="//@nodes.5" target="//@nodes.6"/>
  </nodes>
  <nodes xsi:type="pipenet.model:Place" name="S3" presetNodes="//@nodes.5" postsetNodes="//@nodes.7">
    <arcs source="//@nodes.6" target="//@nodes.7"/>
  </nodes>
  <nodes xsi:type="pipenet.model:Transition" name="T4" presetNodes="//@nodes.6" postsetNodes="//@nodes.0">
    <arcs source="//@nodes.7" target="//@nodes.0"/>
  </nodes>
</pipenet.model:Pipenet>


Edit:
I played around a bit and got some result out of it but first what I want to do: I have my model with the nodes which are places or transitions. I want to write the model down as a structured text (SCL). Therefore I have to check how they are connected. I do that with a couple of for-loops and if-blocks.
Now to the result: As long I don't have a for-loop inside the file block it works how expected -> called once for the "Pipenet". If I write a for-loop to check the nodes it's getting chaotic. Everything out of the for-loop is written x-times as per "nodes" elements but the text inside the for-loop is only written once in append-mode. If I don't append then only the text outside the loop is written and nothing more. At the very very beginning where the structure wasn't that complex it was doing fine. I'm not sure what I did wrong on my way to this point.

[Updated on: Wed, 08 June 2016 11:06]

Report message to a moderator

Re: Acceleo loop problem [message #1734643 is a reply to message #1734416] Fri, 10 June 2016 07:19 Go to previous messageGo to next message
Laurent Goubet is currently offline Laurent GoubetFriend
Messages: 1902
Registered: July 2009
Senior Member
Hi,

I cannot say I completely understand what you're trying to accomplish or how it fails here, but the basic thing is : your "file" block is generated multiple times, which means we iterate on multiple different "Pipenet" elements.

You can try to output different things to understand "what" we are iterating over and might give you an idea of how to avoid the issue. For example, the following:

[template public generateElement(aPipenet : Pipenet) post(trim())]
[comment @main/]

[file (clearFile('out.txt'), tru, 'UTF-8')]
Generating for [aPipenet/]

[/file]
[/template]


By the way, what is this "clearFile" you call on the file name?

Laurent Goubet
Obeo
Re: Acceleo loop problem [message #1734808 is a reply to message #1734643] Mon, 13 June 2016 06:31 Go to previous messageGo to next message
Viktoria Fink is currently offline Viktoria FinkFriend
Messages: 12
Registered: May 2016
Junior Member
Hi,

what if there is only one "Pipenet" element? Node inherits from Pipenet. Maybe this is actually my problem. The "file" block is generated 8 times and this is the same as the number of "nodes".
"clearFile" is a method from another class that is called throught a query to check if the file exists and delete it in the beginning. My way to avoid append.

Edit: I followed your advise. At least I can comprehend my problem. The cause to this must be the inheritance. I need to change the metamodel so the file block is generated only once. Makes it easier to work with. Thanks for your help and patience. Wink

[Updated on: Mon, 13 June 2016 08:41]

Report message to a moderator

Re: Acceleo loop problem [message #1734819 is a reply to message #1734808] Mon, 13 June 2016 10:12 Go to previous messageGo to next message
Laurent Goubet is currently offline Laurent GoubetFriend
Messages: 1902
Registered: July 2009
Senior Member
Hi Viktoria,

If you indeed have inheritance, then yes, we'll generate for each element that is an "instanceof" the main template's argument. There is however a way to avoid the recursion if you're sure you only want to generate from the top-level Pipenet.

If you look at the generate java file beside your "main" template, you'll see that "doGenerate" calls "super.doGenerate(monitor)". Change that to "super.generate(monitor, false)" to disable the recursion.

Laurent Goubet
Obeo
Re: Acceleo loop problem [message #1734823 is a reply to message #1734819] Mon, 13 June 2016 10:32 Go to previous messageGo to next message
Viktoria Fink is currently offline Viktoria FinkFriend
Messages: 12
Registered: May 2016
Junior Member
Laurent you are the best!
It works perfectly now. Why not from the start so? Very Happy Kidding.
Re: Acceleo loop problem [message #1734840 is a reply to message #1734823] Mon, 13 June 2016 12:22 Go to previous message
Laurent Goubet is currently offline Laurent GoubetFriend
Messages: 1902
Registered: July 2009
Senior Member
Well I didn't know for sure that recursion was your issue Wink
Re: Acceleo loop problem [message #1734868 is a reply to message #1734643] Mon, 13 June 2016 06:31 Go to previous message
Viktoria Fink is currently offline Viktoria FinkFriend
Messages: 12
Registered: May 2016
Junior Member
Hi,

what if there is only one "Pipenet" element? Node inherits from Pipenet. Maybe this is actually my problem. The "file" block is generated 8 times and this is the same as the number of "nodes".
clearFile is a method from another class that is called throught a query to check if the file exists and delete it in the beginning. My way to avoid append.
Previous Topic:Re: Acceleo loop problem
Next Topic:Re: Acceleo loop problem
Goto Forum:
  


Current Time: Tue Apr 16 23:23:36 GMT 2024

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

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

Back to the top