Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » ATL » recursive helper
recursive helper [message #990607] Thu, 13 December 2012 12:09 Go to next message
Florian Wieser is currently offline Florian WieserFriend
Messages: 2
Registered: December 2012
Junior Member
Hello

I am dealing with one problem, i want to define a recursive helper Method

For example if i have a class "Group" and a class called "Entity" an an abstract class "Thing" (Thing is the superclass of Group and Entity)
So a Group can contain Entities and other Groups
this is a possible hierarchy

Entity
Group
|->Entitiy
|->Group
|->Group
|->Entity


Has anyone an idea how i can define a recursive helper with which i can retrieve all the "Entity" elements in this hierarchy, and i just commit a Sequence(MMModel!Thing) which is containing the the two Elements on the root level (in this case the first Entity and the first group from the possible hierarchy)

thank you
Re: recursive helper [message #990761 is a reply to message #990607] Fri, 14 December 2012 07:23 Go to previous messageGo to next message
Andreas Brieg is currently offline Andreas BriegFriend
Messages: 48
Registered: November 2012
Member
This doesn't look difficult at all. You just need to have two variants of the helper. One for Group and one for Entity. The Entity version does return a Sequence only containing self. The Group version collects all from its elements and flattens it.

This might not be strictly correct code, but it should get you the idea:
helper context Entity def: entities(): Sequence(Entity) = self;
helper context Group def: entities(): Sequence(Entity) = self.elements->collect(e|e.entities())->flatten();

This will return the entities in in-order iteration sequence.
Re: recursive helper [message #990987 is a reply to message #990761] Sun, 16 December 2012 12:31 Go to previous messageGo to next message
Florian Wieser is currently offline Florian WieserFriend
Messages: 2
Registered: December 2012
Junior Member
Thx. I have now managed to create an revursive helper

But i have one Problem in the else part. want to store the Sequence elements with the new appended Element in an helper variable.
unfortunately this does not work
helper def: fields: Sequence(FileDefinition!FieldDefinition) =
	Sequence{};

helper context GradingSystem!TaskGroup def : getConcreteTasks() : Sequence(FileDefinition!FieldDefinition ) =
	self.contains->iterate( child ; elements : Sequence(FileDefinition!FieldDefinition ) = 
		Sequence{} |  
		if child.oclIsTypeOf(GradingSystem!TaskGroup) then			
		(
			( elements.union(child.getConcreteTasks()))			
		)-- TaskGroup : recursive call			
		else
		(		  
			thisModule.fields.union((elements.append(thisModule.NewStaticField(self.name+child.name+'Points', 'Integer'))) )
			-- cant store the actual elements sequence in the helper fields, thisModule.field always stays empty
any other possibilities to store this? 


		)  -- ConcreteTask
		endif  
		) -> append(thisModule.NewAgreggField(self.name,'Integer',thisModule.fields )) -- here i would need the thisModule.fields
		
	;


[Updated on: Sun, 16 December 2012 12:32]

Report message to a moderator

Re: recursive helper [message #991077 is a reply to message #990987] Mon, 17 December 2012 10:00 Go to previous message
Andreas Brieg is currently offline Andreas BriegFriend
Messages: 48
Registered: November 2012
Member
union creates a new instance of a sequence and doesn't modify it. So you would need to assign a value to thisModule.fields. However you only could do that in an imperative do-block. You should use the let-expression in that helper to have a variable that you would now use the thisModule.fields is used for. Globals should be avoided in ATL as much as possible.
Previous Topic:Several XML Files into one UML
Next Topic:Setting map value in the target model
Goto Forum:
  


Current Time: Tue Apr 23 14:44:44 GMT 2024

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

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

Back to the top