Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » M2M (model-to-model transformation) » index from sequence
index from sequence [message #734068] Thu, 06 October 2011 15:13 Go to next message
Fy Za is currently offline Fy ZaFriend
Messages: 245
Registered: March 2010
Senior Member
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 #734097 is a reply to message #734068] Thu, 06 October 2011 16:26 Go to previous messageGo to next message
Sylvain EVEILLARD is currently offline Sylvain EVEILLARDFriend
Messages: 556
Registered: July 2009
Senior Member
See the documentation
indexOf(o : oclAny) returns the rank of first occurrence of o in self;
Re: index from sequence [message #734107 is a reply to message #734068] Thu, 06 October 2011 17:19 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
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 Go to previous messageGo to next message
Fy Za is currently offline Fy ZaFriend
Messages: 245
Registered: March 2010
Senior Member
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 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
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 Go to previous messageGo to next message
Fy Za is currently offline Fy ZaFriend
Messages: 245
Registered: March 2010
Senior Member
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 Go to previous messageGo to next message
Sylvain EVEILLARD is currently offline Sylvain EVEILLARDFriend
Messages: 556
Registered: July 2009
Senior Member
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 Go to previous message
Fy Za is currently offline Fy ZaFriend
Messages: 245
Registered: March 2010
Senior Member
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 Sad

please help me
thanks
Previous Topic: How to call a called rule.[ATL]
Next Topic:Transformation Lists
Goto Forum:
  


Current Time: Thu Apr 25 06:18:28 GMT 2024

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

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

Back to the top