index from sequence [message #734068] |
Thu, 06 October 2011 15:13  |
Eclipse User |
|
|
|
Hi All,
I have a sequence of String like for example
Sequence('ax','ay','az')
I would define an helper that returns the index of an given parameter
for example
Sequence('ax','ay','az').my_helper('ax') --> 1
Sequence('ax','ay','az').my_helper('az') --> 3
please help me
thanks
|
|
|
|
Re: index from sequence [message #734107 is a reply to message #734068] |
Thu, 06 October 2011 17:19   |
Eclipse User |
|
|
|
Hi
If you're trying to use OCL, Sequence literals use {} and Collection
operations use -> so try
Sequence{'ax','ay','az'}->indexOf('ax')
Regards
Ed Willink
On 06/10/2011 16:13, ZALILA Faiez wrote:
> Hi All,
> I have a sequence of String like for example
> Sequence('ax','ay','az')
> I would define an helper that returns the index of an given parameter
> for example
> Sequence('ax','ay','az').my_helper('ax') --> 1
> Sequence('ax','ay','az').my_helper('az') --> 3
>
> please help me
> thanks
|
|
|
Re: index from sequence [message #734302 is a reply to message #734107] |
Fri, 07 October 2011 12:11   |
Eclipse User |
|
|
|
my problem is more hard than this.
So the general problem is that I have 2 classes in my ecore
class Instance :
name: String
ports : 0..* Port
and
class Port :
name : String
and I have to find from a sequence of Instance,
if an given name is included in instance
and what is the position.
for exemple, I have these 3 instances
<instance xsi:type="ModelXtext:Instance">
<name name="Technician"/>
<port name="MainM"/>
<port name="MainN"/>
</instance>
<instance xsi:type="ModelXtext:Instance">
<name name="M1"/>
<port name="MainL"/>
<port name="MainN"/>
</instance>
<instance xsi:type="ModelXtext:Instance">
<name name="M2"/>
<port name="MainL"/>
<port name="MainN"/>
</instance>
I 'd to find the position of a given name in a instance
for example, if I apply this helper get_instance_and_position("MainL") on these instances, I have result like this
=>{ ( /@instance.1 , "0") , ( /@instance.2 , "0" ) }
that means that the port MainL exists in the position 0 in the instance 1
and in the position 0 in the instance 2
I know that this helper return a sequences of tuples composed of Instance and integer that is the position of the port in the instance but I can't how I codes it.
please help me
thanks
|
|
|
Re: index from sequence [message #734331 is a reply to message #734302] |
Fri, 07 October 2011 13:26   |
Eclipse User |
|
|
|
Hi
I recommend using the Interactive OCL Xtext Console.
This allows you to rapidly practice OCL expressions on a model. This is
very useful when you're not entirely confident in what you're doing.
It's also useful when you are confident but trying to use perhaps
over-complex expressions.
Regards
Ed Willink
On 07/10/2011 13:11, ZALILA Faiez wrote:
> my problem is more hard than this.
>
> So the general problem is that I have 2 classes in my ecore
> class Instance :
> name: String ports : 0..* Port
> and
> class Port :
> name : String
>
> and I have to find from a sequence of Instance,
> if an given name is included in instance and what is the position.
> for exemple, I have these 3 instances
>
> <instance xsi:type="ModelXtext:Instance">
> <name name="Technician"/>
> <port name="MainM"/>
> <port name="MainN"/>
> </instance>
> <instance xsi:type="ModelXtext:Instance">
> <name name="M1"/>
> <port name="MainL"/>
> <port name="MainN"/>
> </instance>
> <instance xsi:type="ModelXtext:Instance">
> <name name="M2"/>
> <port name="MainL"/>
> <port name="MainN"/>
> </instance>
>
> I 'd to find the position of a given name in a instance
> for example, if I apply this helper get_instance_and_position("MainL")
> on these instances, I have result like this
> =>{ ( /@instance.1 , "0") , ( /@instance.2 , "0" ) }
> that means that the port MainL exists in the position 0 in the instance 1
> and in the position 0 in the instance 2
>
> I know that this helper return a sequences of tuples composed of
> Instance and integer that is the position of the port in the instance
> but I can't how I codes it.
> please help me
> thanks
|
|
|
Re: index from sequence [message #734373 is a reply to message #734331] |
Fri, 07 October 2011 14:50   |
Eclipse User |
|
|
|
I try to define an helper
helper context Sequence(MetaModel!Instance) def : getInstances(portname : String) : Sequence (TupleType(position: Integer , instances : MetaModel!Instance))=
self->iterate( instance ; tuples : Sequence (TupleType(p: Integer , i : MetaModel!Instance)) = Sequence{} |
let ports: Sequence(MetaModel!Port) = instance.ports.asSequence()->collect(p|p.name.name)
in
if ports->includes(portname)
then
tuples->including(Tuple{p = ports->indexOf(portname) , i = instance})
else
tuples
endif
);
but all the code underlined in red and it shows :
helper 'getInstances': current implementation does not support helpers with collection context
please help me
thanks
|
|
|
Re: index from sequence [message #734408 is a reply to message #734373] |
Fri, 07 October 2011 16:30   |
Eclipse User |
|
|
|
As it says, you can't define helper on collections.
What you can do is :
helper def : getInstances(portname : String, seq : Sequence(MetaModel!Instance))
and then use seq in place of self.
|
|
|
Re: index from sequence [message #734411 is a reply to message #734408] |
Fri, 07 October 2011 16:43  |
Eclipse User |
|
|
|
Thanks Sylvain,
please, I have another problem
that I 'd generate from a Sequence of tuples a String
I try with this code
helper def : getinstancesname (instancesequence:Sequence (TupleType(position: Integer , instances : MetaModel!Instance))) : String =
instancesequence->iterate(t; y : String = 'a' |y+t.position.toString()+t.instance.name)
but that generate an Exception when I run 
please help me
thanks
|
|
|
Powered by
FUDForum. Page generated in 0.04261 seconds