Skip to main content



      Home
Home » Modeling » TMF (Xtext) » OCL: Iterate over code lines
OCL: Iterate over code lines [message #894885] Tue, 10 July 2012 18:29 Go to next message
Eclipse UserFriend
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 01:34 Go to previous messageGo to next message
Eclipse UserFriend
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 15:24 Go to previous message
Eclipse UserFriend
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 15:25] by 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 Jul 12 12:50:10 EDT 2025

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

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

Back to the top