Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » [Xpand/Xtend] Deleting elements from one List affects the elements on another List(How to delete elements from List in the right way?)
[Xpand/Xtend] Deleting elements from one List affects the elements on another List [message #662411] Wed, 30 March 2011 15:02 Go to next message
mrodrigo Missing name is currently offline mrodrigo Missing name
Messages: 6
Registered: March 2011
Junior Member
Hello all,

I have a problem when trying to delete elements from one List.

Here is my scenario: I have two List, 'ancestors' and 'orderedList'. The first one contains, temporally, elements collected during a loop iteration. The second one is made to contain, permanently, all 'ancestors' values. And when the rule 'removeAllElements()' deletes elements from 'ancestors', the elements from 'orderedList' are, magically, deleted too.

The code of each rule is:
Xpand rule to obtain full hierarchy tree
«DEFINE fullyHierarchyOUList(List unorderedList, List orderedList) FOR gormas_model»
  «LET (List) {} AS ancestors»
  «FOREACH unorderedList AS unorderedNode»
    «REM» Delete values from previous iteration «ENDREM»
    «ancestors.removeAllElements() -> ""»

    «LET (List)unorderedNode AS unorderedNodeAsList»

    «REM» Search for ancestors in recursive way «ENDREM»    
    «searchParent(unorderedNodeAsList, ancestors, unorderedList.reject(e|e==unorderedNodeAsList))»
    «REM» Add ancestors to final List «ENDREM»
    «orderedList.add(ancestors)»
   
    «ENDLET» «REM» End of LET (List)unorderedNode AS unorderedNodeList «ENDREM»
  «ENDFOREACH»
  «ENDLET» «REM» End of LET (List) {} AS ancestors «ENDREM»
«ENDDEFINE»


Xtend function to delete elements from List
List removeAllElements(List objectList) : {
	if objectList.size > 0 then (
		objectList.remove(objectList.last()) ->		
		removeAllElements(objectList)
	)
};


Xtend recursive function to search for ancestors
Void searchParent(List[OrganizationalUnit] lastCheckedNodeAsList, List ancestors, List unorderedList): {
    ancestors.add(((OrganizationalUnit)lastCheckedNodeAsList.first())) ->
    if lastCheckedNodeAsList.size > 1 then 
    searchParent( ((List)(unorderedList.select(e|((List) e).first() == lastCheckedNodeAsList.last()).first())), ancestors, unorderedList.reject(e|e==lastCheckedNodeAsList) )
};


With these entries:
unorderedList = [[University], [Center, University], [Department, University], [Laboratoty, Department]]
orderedList = []


The required results are:
orderedList = [[University], [Center, University], [Department, University], [Laboratoty, Department, University]]

However, the results obtained are:
orderedList = [[Laboratory, Department,University], [Laboratory, Department,University], [Laboratory, Department,University], [Laboratory, Department,University]]	


Somebody understands it? This issue is driving me crazy.

Regards.
Re: [Xpand/Xtend] Deleting elements from one List affects the elements on another List [message #662481 is a reply to message #662411] Wed, 30 March 2011 19:03 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6388
Registered: July 2009
Senior Member
Hi,

i don't know why you make this so complicated
List getAncestors(List lastCheckedNodeAsList, List unorderedList): 
	{lastCheckedNodeAsList.first()}.addAll(lastCheckedNodeAsList.size == 1 ? {} : getAncestors(
	 ((List)(unorderedList.select(e|((List) e).first() == lastCheckedNodeAsList.last()).first())), 
	  unorderedList.reject(e|e==lastCheckedNodeAsList)));
	  
Void doTheStuff(List unorderedList, List orderedList) :
	orderedList.addAll(unorderedList.collect(unorderedNode|getAncestors((List)unorderedNode, unorderedList.reject(e|e==unorderedNode))));	  


may do the same as your template and extensions do.

~Christian
Re: [Xpand/Xtend] Deleting elements from one List affects the elements on another List [message #662569 is a reply to message #662481] Thu, 31 March 2011 07:30 Go to previous message
mrodrigo Missing name is currently offline mrodrigo Missing name
Messages: 6
Registered: March 2011
Junior Member
Hello Christian,

Your code works perfectly! Thank you very much for your assistance!

Regards.
Previous Topic:Workflow
Next Topic:[XPand2] Unknown type 'ecore::EClass'
Goto Forum:
  


Current Time: Sun Oct 26 06:01:56 GMT 2014

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

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