Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » the order of the element in the collection does not maintain(the sorted collection returned from the java service does not preserve the order in the for loop)
the order of the element in the collection does not maintain [message #1067193] Sun, 07 July 2013 12:58 Go to next message
ibrahim kara is currently offline ibrahim karaFriend
Messages: 10
Registered: May 2012
Junior Member
Hello,

I have a java service to sort Transition elements according to their priorities.
I return an LinkedList from my java service, but when acceleo run the for loop it does not maintain the order. (different every time)

the template created from the java class contains this prototype of the invoke method:
it is normal that the retrun type is an OrderedSet while the invoke argument is a Sequence?

I always print the result of the sortTransitions() in my java class and it keeps always the right order, the order change only when entering the for loop in Acceleo although I added asOrderedSet operation which is supposed to maintain order.

Could you please tell me how to preserve the order of the elements returning sorted from the Java service?
I would really appreciate the help asap.
Thanks in advance.

(I cannot use the sortedBy method since I can not directly reach the priorities of the elements)

Ibrahim
Re: the order of the element in the collection does not maintain [message #1067231 is a reply to message #1067193] Sun, 07 July 2013 19:05 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

Acceleo/OCL preserves order where ordered should be preserved.

If you want us to find your bug for you, you need to share your code
with us.

Regards

Ed Willink

On 07/07/2013 13:59, ibrahim kara wrote:
> Hello,
> I have a java service to sort Transition elements according to their
> priorities. I return an LinkedList from my java service, but when
> acceleo run the for loop it does not maintain the order. (different
> every time)
>
> the template created from the java class contains this prototype of
> the invoke method:
> it is normal that the retrun type is an OrderedSet while the invoke
> argument is a Sequence?
> I always print the result of the sortTransitions() in my java class
> and it keeps always the right order, the order change only when
> entering the for loop in Acceleo although I added asOrderedSet
> operation which is supposed to maintain order.
>
> Could you please tell me how to preserve the order of the elements
> returning sorted from the Java service?
> I would really appreciate the help asap.
> Thanks in advance.
>
> (I cannot use the sortedBy method since I can not directly reach the
> priorities of the elements)
>
> Ibrahim
Re: the order of the element in the collection does not maintain [message #1067234 is a reply to message #1067231] Sun, 07 July 2013 19:47 Go to previous messageGo to next message
ibrahim kara is currently offline ibrahim karaFriend
Messages: 10
Registered: May 2012
Junior Member
Hi there,

Here is the for loop I call the Java service:
[template public transitionCode(aState : State, args0 : OclAny, args1 : OclAny) post(trim())]

[for (aTransition : Transition | outgoing->filter(Transition)->asSequence()->oclAsSet().filterJoinTargetsOut()->filter(Transition)->asSequence()->oclAsSet().sortTransitions()->asSequence()->first())]
if [if (source.container.qualifiedName = target.container.qualifiedName)]
[if (source.eClass().name = 'State' and (target.eClass().name = 'State' or target.eClass().name = 'FinalState'))]
[simpleTransitionCode(args0)/]
[else]
[pseudostateTargetTransitionCode(args0, args1->filter(State))/]
[/if]
[else]
<%-- /*start interlevel transition <%args(0)%>*/ --%>[/comment]
[if (source.eClass().name = 'State' and (target.eClass().name = 'State' or target.eClass().name = 'FinalState'))]
[interLevelTransition(args0)/]
[else]
[pseudostateTargetTransitionCode(args0, args1->filter(State))/]
[/if]
/*end interlevel transition*/
[/if]
[/for]
......
[/template]

Here is the Java code:
public LinkedList<Transition> sortTransitions(List<Transition> list)
//throws ENodeCastException, ClassCastException
{
//EList<Transition> list = aVertex.getIncomings();
//EList<Transition> filterJoinTargetsOut = (EList<Transition>)filterJoinTargetsOut((State)aVertex);
//EList<Transition> list = filterJoinTargetsOut;
//EList<Transition> list = aVertex.getOutgoings();

//EList<Transition> list = new BasicEList<Transition>();
//if(listobject instanceof EList)
//list = (EList<Transition>) listobject;

Set<Transition> unpriorizedSet = new LinkedHashSet<Transition>();
TreeMap<Integer, Transition> tm = new TreeMap<Integer, Transition>(
new TransitionComparator());

UmlServices umlservice = new UmlServices();
LinkedList<Transition> sortedList = new LinkedList<Transition>();
Set<Entry<Integer, Transition>> set = null;
// if list is empty return empty list
if (list == null || list.size() == 0) {
return sortedList;
}
for (Iterator<Transition> iterator = list.iterator(); iterator.hasNext()Wink {// Iterator<.......> FIX
Transition transition = (Transition) iterator.next();
Stereotype stereotype = umlservice.getStereotype(transition,
"ExecutionOrderPriority");

if (stereotype == null) {
unpriorizedSet.add(transition);
} else {
Integer transitionPriority = (Integer) transition.getValue(
stereotype, "priority");
if (transitionPriority != null) {
if (!tm.containsKey(transitionPriority)) {
tm.put(transitionPriority, transition);
} else {
unpriorizedSet.add(transition);
}
} else {
unpriorizedSet.add(transition);
}
}

// }
}

set = tm.entrySet();
for (Entry<Integer, Transition> entry : set) {
sortedList.add(entry.getValue());
}
for (Transition entry : unpriorizedSet) {
sortedList.add(entry);
}

/* TO TEST THE SORTING
int base=0;
for (Iterator<Transition> iterator = sortedList.iterator(); iterator.hasNext()Wink {
Transition transition = (Transition) iterator.next();
Stereotype stereotype = umlservice.getStereotype(transition,
"ExecutionOrderPriority");
if((Integer) transition.getValue(stereotype, "priority") > base){
}
else{ return null;}
base= (Integer)transition.getValue(stereotype, "priority");
}
*/

return sortedList;
}

and here is the Java service wrapper:

[query public sortTransitions(arg0 : Sequence(Transition)) : OrderedSet(Transition)
= invoke('org.openmodelica.modelicaml.generate.modelica.services.ModelicaMLServices', 'sortTransitions(java.util.List)', Sequence{arg0}) /]


The java code does sort the transitions according to their priorities but when the list is returned to the for loop it has different order every run.
Thanks for the help.

Re: the order of the element in the collection does not maintain [message #1067235 is a reply to message #1067234] Sun, 07 July 2013 20:04 Go to previous messageGo to next message
ibrahim kara is currently offline ibrahim karaFriend
Messages: 10
Registered: May 2012
Junior Member
I believe the problem I have is exactly the same as Asma's in this post:
http://www.acceleo.org/forum/read.php?8,3862
Re: the order of the element in the collection does not maintain [message #1067249 is a reply to message #1067234] Mon, 08 July 2013 05:52 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

You only provide an uncheckable snippet of your code so it's still
difficult to give you a proper answer however, each time you call
->oclAsSet() you allow the tooling to create a random ordering in
excahnge for uniqueness.

Regards

Ed Willink



On 07/07/2013 20:47, ibrahim kara wrote:
> Hi there,
>
> Here is the for loop I call the Java service:
> [template public transitionCode(aState : State, args0 : OclAny, args1
> : OclAny) post(trim())]
> [for (aTransition : Transition |
> outgoing->filter(Transition)->asSequence()->oclAsSet().filterJoinTargetsOut()->filter(Transition)->asSequence()->oclAsSet().sortTransitions()->asSequence()->first())]
> if [if (source.container.qualifiedName =
> target.container.qualifiedName)]
> [if (source.eClass().name = 'State' and (target.eClass().name
> = 'State' or target.eClass().name = 'FinalState'))]
> [simpleTransitionCode(args0)/]
> [else]
> [pseudostateTargetTransitionCode(args0, args1->filter(State))/]
> [/if]
> [else]
> <%-- /*start interlevel transition <%args(0)%>*/ --%>[/comment]
> [if (source.eClass().name = 'State' and (target.eClass().name
> = 'State' or target.eClass().name = 'FinalState'))]
> [interLevelTransition(args0)/]
> [else]
> [pseudostateTargetTransitionCode(args0, args1->filter(State))/]
> [/if]
> /*end interlevel transition*/
> [/if]
> [/for]
> .....
> [/template]
>
> Here is the Java code:
> public LinkedList<Transition> sortTransitions(List<Transition> list)
> //throws ENodeCastException, ClassCastException {
> //EList<Transition> list = aVertex.getIncomings();
> //EList<Transition> filterJoinTargetsOut =
> (EList<Transition>)filterJoinTargetsOut((State)aVertex);
> //EList<Transition> list = filterJoinTargetsOut;
> //EList<Transition> list = aVertex.getOutgoings();
>
> //EList<Transition> list = new BasicEList<Transition>();
> //if(listobject instanceof EList)
> //list = (EList<Transition>) listobject;
> Set<Transition> unpriorizedSet = new LinkedHashSet<Transition>();
> TreeMap<Integer, Transition> tm = new TreeMap<Integer,
> Transition>(
> new TransitionComparator());
>
> UmlServices umlservice = new UmlServices();
> LinkedList<Transition> sortedList = new LinkedList<Transition>();
> Set<Entry<Integer, Transition>> set = null;
> // if list is empty return empty list
> if (list == null || list.size() == 0) {
> return sortedList;
> }
> for (Iterator<Transition> iterator = list.iterator();
> iterator.hasNext();) {// Iterator<.......> FIX
> Transition transition = (Transition) iterator.next();
> Stereotype stereotype = umlservice.getStereotype(transition,
> "ExecutionOrderPriority");
>
> if (stereotype == null) {
> unpriorizedSet.add(transition);
> } else {
> Integer transitionPriority = (Integer)
> transition.getValue(
> stereotype, "priority");
> if (transitionPriority != null) {
> if (!tm.containsKey(transitionPriority)) {
> tm.put(transitionPriority, transition);
> } else {
> unpriorizedSet.add(transition);
> }
> } else {
> unpriorizedSet.add(transition);
> }
> }
>
> // }
> }
>
> set = tm.entrySet();
> for (Entry<Integer, Transition> entry : set) {
> sortedList.add(entry.getValue());
> }
> for (Transition entry : unpriorizedSet) {
> sortedList.add(entry);
> }
>
> /* TO TEST THE SORTING
> int base=0;
> for (Iterator<Transition> iterator = sortedList.iterator();
> iterator.hasNext();) {
> Transition transition = (Transition) iterator.next();
> Stereotype stereotype = umlservice.getStereotype(transition,
> "ExecutionOrderPriority");
> if((Integer) transition.getValue(stereotype, "priority") >
> base){
> }
> else{ return null;}
> base= (Integer)transition.getValue(stereotype, "priority");
> }
> */
>
> return sortedList;
> }
>
> and here is the Java service wrapper:
>
> [query public sortTransitions(arg0 : Sequence(Transition)) :
> OrderedSet(Transition)
> =
> invoke('org.openmodelica.modelicaml.generate.modelica.services.ModelicaMLServices',
> 'sortTransitions(java.util.List)', Sequence{arg0}) /]
>
>
> The java code does sort the transitions according to their priorities
> but when the list is returned to the for loop it has different order
> every run.
> Thanks for the help.
>
>
Re: the order of the element in the collection does not maintain [message #1067250 is a reply to message #1067235] Mon, 08 July 2013 05:54 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

No. In that post the user was disappointed that the getAllAttributes()
was not specified with the ordering behaviour that they wanted. You
appear to create your own disappointment with oclAsSet.

Regards

Ed Willink

On 07/07/2013 21:04, ibrahim kara wrote:
> I believe the problem I have is exactly the same as Asma's in this post:
> http://www.acceleo.org/forum/read.php?8,3862
Re: the order of the element in the collection does not maintain [message #1067441 is a reply to message #1067250] Mon, 08 July 2013 17:48 Go to previous messageGo to next message
ibrahim kara is currently offline ibrahim karaFriend
Messages: 10
Registered: May 2012
Junior Member
Hi Ed;
I call it with oclAsSet() cause if I call it with sequence or other collection types I reveive the error "Cannot find operation (sortTransitions()) for the type (Transition)"

even though It should look for sortTransitions for the type Sequence(Transition)

[for (aTransition : Transition | outgoing->filter(Transition)->asSequence()->oclAsSet().filterJoinTargetsOut()->filter(Transition)->asSequence().sortTransitions()......

cause the Java service is called through the query that is called from a Sequence(Transition):

> [query public sortTransitions(arg0 : Sequence(Transition)) :
> OrderedSet(Transition)
> =
> invoke('org.openmodelica.modelicaml.generate.modelica.services.ModelicaMLServices',
> 'sortTransitions(java.util.List)', Sequence{arg0}) /]

Do you see the problem?

(I unfortunately cannot share the whole code but would really appreciate the help)

Thanks
Re: the order of the element in the collection does not maintain [message #1067449 is a reply to message #1067441] Mon, 08 July 2013 18:36 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

Error messages are not always easy to understand, but just choosing
random things to make them go away is a sure way to compound your problems.

oclAsWSrt is completely inappropriate for you.

Why didn't you just call asOrderedSet()?

If you want help you should create a small example that demonstrates
your problem that you are prepared to share.

Regards

Ed Willink


On 08/07/2013 18:48, ibrahim kara wrote:
> Hi Ed;
> I call it with oclAsSet() cause if I call it with sequence or other
> collection types I reveive the error "Cannot find operation
> (sortTransitions()) for the type (Transition)"
>
> even though It should look for sortTransitions for the type
> Sequence(Transition)
>
> [for (aTransition : Transition |
> outgoing->filter(Transition)->asSequence()->oclAsSet().filterJoinTargetsOut()->filter(Transition)->asSequence().sortTransitions()......
>
> cause the Java service is called through the query that is called from
> a Sequence(Transition):
>
>> [query public sortTransitions(arg0 : Sequence(Transition)) :
>> OrderedSet(Transition)
>> =
>> invoke('org.openmodelica.modelicaml.generate.modelica.services.ModelicaMLServices',
>> 'sortTransitions(java.util.List)', Sequence{arg0}) /]
>
> Do you see the problem?
>
> (I unfortunately cannot share the whole code but would really
> appreciate the help)
>
> Thanks
Re: the order of the element in the collection does not maintain [message #1067456 is a reply to message #1067449] Mon, 08 July 2013 19:20 Go to previous message
ibrahim kara is currently offline ibrahim karaFriend
Messages: 10
Registered: May 2012
Junior Member
Hi,
asOrderedSet gives the same error as well.
"Cannot find operation (sortTransitions()) for the type (Transition)"

I will try to create a small example but I really do not know what the real problem is so can not make sure if I can represent the same problem with a small example and test it :S

Do you have any example where a java service (called in a for loop statement) receives and returns a collection keeping the order same?

Thanks in advance.
Previous Topic:[XPAND] How to run the sample project?
Next Topic:[Acceleo] Getting started example doesn't work
Goto Forum:
  


Current Time: Tue Apr 16 13:56:24 GMT 2024

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

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

Back to the top