Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » OCL: Iterate over code lines
OCL: Iterate over code lines [message #894885] Tue, 10 July 2012 22:29 Go to next message
Wouter Schaekers is currently offline Wouter SchaekersFriend
Messages: 17
Registered: July 2012
Junior Member
Hey

Example:

num a = 1
num b = 2
num c = 3
num d = 4
num e = 5

a.plus(b)
c.plus(d)
a.plus(c)
a.plus(e) <- ERROR

I want to iterate over all lines (eg the grammar lines that define the 'plus') and make sure that the first argument is never higher than the second argument. So:
a.plus(b) -> a becomes 3
c.plus(d) -> c becomes 7
a.plus(d) -> a becomes 10
a.plus(e) -> 10 > 5 -> ERROR

Ofcourse it isn't difficult if you store the solution of every operation in a new variable, but that's not what I want. I want to put the sum in the first argument.
I know how to iterate over a collection in OCL. (eg self.rules->iterate(n : Name; sum : Integer = 0 | sum + n.int1) < 15) But in here, I have to use some sort of index or use a next() or previous() command if that exists.
Is it possible to do such a thing in OCL?

Thanks.
Re: OCL: Iterate over code lines [message #894913 is a reply to message #894885] Wed, 11 July 2012 05:34 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

There is an OCL newsgroup for OCL discussions.

Your discussion seems to suggest that you are performing run-time
evaluation at compile-time, which is of course possible but a bit
peculiar. For the simple expressions you provide, it may work, but it
will collapse as soon as your expression language elaborates to
incorporate a variable/parameter.

I suggest rethinking what you are really trying to do.

In OCL you would need to iterate a Set(Tuple(name:String,value:Integer))
to maintain the accumulated state. (Imperative OCL provides a Dict,
which is mutable)

Regards

Ed Willink

On 10/07/2012 23:29, Wouter Schaekers wrote:
> Hey
>
> Example:
>
> num a = 1
> num b = 2
> num c = 3
> num d = 4
> num e = 5
>
> a.plus(b)
> c.plus(d)
> a.plus(c)
> a.plus(e) <- ERROR
>
> I want to iterate over all lines (eg the grammar lines that define the
> 'plus') and make sure that the first argument is never higher than the
> second argument. So:
> a.plus(b) -> a becomes 3
> c.plus(d) -> c becomes 7
> a.plus(d) -> a becomes 10
> a.plus(e) -> 10 > 5 -> ERROR
>
> Ofcourse it isn't difficult if you store the solution of every
> operation in a new variable, but that's not what I want. I want to put
> the sum in the first argument.
> I know how to iterate over a collection in OCL. (eg
> self.rules->iterate(n : Name; sum : Integer = 0 | sum + n.int1) < 15)
> But in here, I have to use some sort of index or use a next() or
> previous() command if that exists.
> Is it possible to do such a thing in OCL?
>
> Thanks.
Re: OCL: Iterate over code lines [message #895138 is a reply to message #894913] Wed, 11 July 2012 19:24 Go to previous message
Wouter Schaekers is currently offline Wouter SchaekersFriend
Messages: 17
Registered: July 2012
Junior Member
Ed Willink wrote on Wed, 11 July 2012 01:34
Hi

There is an OCL newsgroup for OCL discussions.

Thanks for pointing this out! I'll post the same question there.

Ed Willink wrote on Wed, 11 July 2012 01:34
Your discussion seems to suggest that you are performing run-time
evaluation at compile-time, which is of course possible but a bit
peculiar. For the simple expressions you provide, it may work, but it
will collapse as soon as your expression language elaborates to
incorporate a variable/parameter.

This is just a simplified example of what I actually want to accomplish. What a really want to do is reference checking (just to see whether two objects are the same). That's all, but again with the same possibility to store the result in the first argument. The variable/parameter problem will never occur.

Ed Willink wrote on Wed, 11 July 2012 01:34
In OCL you would need to iterate a Set(Tuple(name:String,value:Integer))
to maintain the accumulated state. (Imperative OCL provides a Dict,
which is mutable)

Can you be a bit more specific (eg give an example)? That would help a lot!

Thanks.

[Updated on: Wed, 11 July 2012 19:25]

Report message to a moderator

Previous Topic:Cross References are working only in a specific parts of the model - why?
Next Topic:xtext, xtend and source generation
Goto Forum:
  


Current Time: Sat Apr 20 11:59:17 GMT 2024

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

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

Back to the top