Home » Modeling » OCL » Is it possible to modify condition collections of cycle?
|
Re: Is it possible to modify condition collections of cycle? [message #786253 is a reply to message #786027] |
Mon, 30 January 2012 07:09 |
Ed Willink Messages: 7655 Registered: July 2009 |
Senior Member |
|
|
Hi
This is a FAQ. For which I've added
http://wiki.eclipse.org/MDT/OCL/FAQ#How_do_I_modify_an_Object_in_OCL.3F.
An approximate equivalent to your example might be possible in OCL using
iterate to continually create new 'Stack's. (The new OCL uses
accumulators behind the scenes for iterate so old collections are reused.)
A replica of your code is possible with recursion. (Optimisation to
reuse of stale collections awaits implementation).
package Espada : pfx = 'platform:/resource/Activity/Espada.ecore' {
class Node {
property value : Integer;
property left : Node;
property right : Node;
operation find(target : Integer) : Integer
{
body: recurse(Sequence{self}, target);
}
operation recurse(stack : Node[0..*]{ordered,!unique}, target :
Integer) : Integer
{
body:
if stack->notEmpty()
then 0
else
let peek : Node = stack->last() in
if peek.value = target
then stack->size()
else
recurse(stack->excluding(peek)->append(peek.left)->append(peek.right),
target)
endif
endif;
}
}
}
Regards
Ed Willink
On 29/01/2012 22:42, Patricia Espada wrote:
> Hello,
>
> I'm trying to understand some property of cycles in OCL. I want to
> change a variable that is subject of a cycle condition, in that same
> cycle. Maybe with and example it's better to understand. I want to
> determine the depth of a tree, doing something similar to this:
>
> Stack s = new Stack();
>
> s.push(head);
>
> while(s.count > 0)
> {
> Node temp = s.pop();
>
> if (temp != NULL)
> {
> if (s.Value == val)
> return s.Depth;
> else
> {
> s.push(temp.Left);
> s.push(temp.Right);
> }
> }
>
> }
>
> it is possible to do this in OCL? and if it is, is it possible to use
> this in OCLInEcore?
|
|
| | | | | | | | | | | | |
Re: Is it possible to modify condition collections of cycle? [message #787214 is a reply to message #787082] |
Tue, 31 January 2012 09:35 |
Patricia Espada Messages: 47 Registered: November 2011 |
Member |
|
|
Edward Willink wrote on Tue, 31 January 2012 00:59Hi
On 31/01/2012 00:03, Patricia Espada wrote:
> Hi again
>
> Thank you once more Edward. I really didn't get that.
An ambiguous statement.
What is "that"? My 'explanation' or 'the need to specify the problem'.
Not the explanation, what we wanted when you ask me to specify the problem. My problem was specified, was only the question that you already response, that it's impossible to modify a collection while iterating that same collection.
Edward Willink wrote on Tue, 31 January 2012 00:59Hi
>
> What got me into this question was that I want to determine a graph
> depth, knowing the root and that each node has ? number of childs.
> I have a recursive solution, described here
> http://www.eclipse.org/forums/index.php/t/281497/, but it's not
> working. And I was wondering if it was possible to do these in a
> non-recursive way.
Again ambiguous. A restatement of your 'solution' or an explanation of
your thinking.
What response are you looking for?
Regards
Ed Willink
Then again, this was the motive for my first question. The only solution I have is a recursive one. I was hopping to find a non-recursive solution if my first question was possible. So, now i have to thing in another way to resolve the problem.
Thanks anyway for your help
|
|
|
Goto Forum:
Current Time: Fri Apr 19 19:43:55 GMT 2024
Powered by FUDForum. Page generated in 0.04052 seconds
|