the order of the element in the collection does not maintain [message #1067193] |
Sun, 07 July 2013 08:58  |
Eclipse User |
|
|
|
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 15:47   |
Eclipse User |
|
|
|
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 #1067249 is a reply to message #1067234] |
Mon, 08 July 2013 01:52   |
Eclipse User |
|
|
|
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 #1067456 is a reply to message #1067449] |
Mon, 08 July 2013 15:20  |
Eclipse User |
|
|
|
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.
|
|
|
Powered by
FUDForum. Page generated in 0.05420 seconds