Home » Archived » M2M (model-to-model transformation) » [ATL] Conditional target element creation
[ATL] Conditional target element creation [message #39172] |
Fri, 18 May 2007 07:12  |
Eclipse User |
|
|
|
Hello,
it is possible to write a rule where a target element is created
conditionally?. I mean, in the following example:
rule Source2Target {
from
s : MA!SourceClass
to
t1 : MB!TargetClass1(
...
),
t2 : MB!TargetClass2(
attr1 <- s.ref
...
)
}
I would like to create the "t2 : MB!TargetClass2" element only if a
[0-1] reference "ref" is defined (not empty) in the "s : MA!SourceClass"
instance. Currently, when I execute that rule and s.ref does not exist I
get an execution error.
How can I manage that problem?
Thank you in advance
Joaquin
|
|
| |
Re: [ATL] Conditional target element creation [message #39233 is a reply to message #39203] |
Fri, 18 May 2007 08:36   |
Eclipse User |
|
|
|
Hello,
Thank you Régis for your answer.
Let me add that if you use rule inheritance (with ATL 2006), then you
can get rid of the copy-and-paste:
rule Source2Target {
from
s : MA!SourceClass
to
t1 : MB!TargetClass1(
...
)
}
--------------------------------
rule Source2TargetWithT2 extends Source2Target {
from
s : MA!SourceClass (
not s.ref.oclIsUndefined()
)
to
t1 : MB!TargetClass1,
t2 : MB!TargetClass2(
attr1 <- s.ref
...
)
}
Because rule Source2Target is *not* abstract, it will be executed as
such if no subrule matches.
Regards,
Frédéric Jouault
Régis wrote:
> Hi Joaquin,
>
> You can do:
> --------------------------------
> rule Source2TargetWithT2 {
> from
> s : MA!SourceClass (
> not s.ref.oclIsUndefined()
> )
> to
> t1 : MB!TargetClass1(
> ...
> ),
>
> t2 : MB!TargetClass2(
> attr1 <- s.ref
> ...
> )
> }
> --------------------------------
> rule Source2TargetWithoutT2 {
> from
> s : MA!SourceClass (
> s.ref.oclIsUndefined()
> )
> to
> t1 : MB!TargetClass1(
> ...
> )
> }
> --------------------------------
>
> But be sure that a MA!SourceClass can be matched only one time (with
> opposite condition) because an element have to be matched only one time
> by a "normal" rule.
>
> Regards,
>
> *Régis CHEVREL* <mailto:rchevrel@sodius.com>
>
>
> SODIUS*
> *6, rue Cornouaille
> 44319 Nantes - France
>
> Phone: +33 (0)2 28 23 54 34
>
> ***www.mdworkbench.com* <http://www.mdworkbench.com/>* *
> Draw more value from your model
>
>
>> Hello,
>> it is possible to write a rule where a target element is created
>> conditionally?. I mean, in the following example:
>>
>> rule Source2Target {
>> from
>> s : MA!SourceClass
>>
>> to
>> t1 : MB!TargetClass1(
>> ...
>> ),
>>
>> t2 : MB!TargetClass2(
>> attr1 <- s.ref
>> ...
>> )
>> }
>>
>> I would like to create the "t2 : MB!TargetClass2" element only if a
>> [0-1] reference "ref" is defined (not empty) in the "s :
>> MA!SourceClass" instance. Currently, when I execute that rule and
>> s.ref does not exist I get an execution error.
>> How can I manage that problem?
>>
>> Thank you in advance
>> Joaquin
>
|
|
|
Re: [ATL] Conditional target element creation [message #39264 is a reply to message #39233] |
Fri, 18 May 2007 14:17   |
Eclipse User |
|
|
|
Thank you both for your quick answers.
Your solution is quite more elegant and easy to maintenance than mine. I
was trying to create the "problematic" element in a new called rule,
which was conditionally called using an IF sentence in the imperative
part of the first rule, as follows:
rule Source2Target {
from
s : MA!SourceClass
to
t1 : MB!TargetClass1(
...
)
do {
if (not s.ref.oclIsUndefined(s)){
thisModule.CreateClass2(s);
}
}
}
rule CreateClass2(s: MA!SourceClass) {
to
t2 : MB!TargetClass2(
attr1 <- s.ref
)
}
However, my solution does not work completely yet, because the
transformation is more complex than the example.
A new question I have: Are parameters of called rules "in" or "in/out"
values? (maybe I should begin a new thread :S )
Regards
Joaquin
Frédéric Jouault escribió:
> Hello,
>
> Thank you Régis for your answer.
>
> Let me add that if you use rule inheritance (with ATL 2006), then you
> can get rid of the copy-and-paste:
>
> rule Source2Target {
> from
> s : MA!SourceClass
> to
> t1 : MB!TargetClass1(
> ...
> )
> }
> --------------------------------
> rule Source2TargetWithT2 extends Source2Target {
> from
> s : MA!SourceClass (
> not s.ref.oclIsUndefined()
> )
> to
> t1 : MB!TargetClass1,
> t2 : MB!TargetClass2(
> attr1 <- s.ref
> ...
> )
> }
>
>
> Because rule Source2Target is *not* abstract, it will be executed as
> such if no subrule matches.
>
>
> Regards,
>
> Frédéric Jouault
>
>
> Régis wrote:
>> Hi Joaquin,
>>
>> You can do:
>> --------------------------------
>> rule Source2TargetWithT2 {
>> from
>> s : MA!SourceClass (
>> not s.ref.oclIsUndefined()
>> )
>> to
>> t1 : MB!TargetClass1(
>> ...
>> ),
>>
>> t2 : MB!TargetClass2(
>> attr1 <- s.ref
>> ...
>> )
>> }
>> --------------------------------
>> rule Source2TargetWithoutT2 {
>> from
>> s : MA!SourceClass (
>> s.ref.oclIsUndefined()
>> )
>> to
>> t1 : MB!TargetClass1(
>> ...
>> )
>> }
>> --------------------------------
>>
>> But be sure that a MA!SourceClass can be matched only one time (with
>> opposite condition) because an element have to be matched only one
>> time by a "normal" rule.
>>
>> Regards,
>>
>> *Régis CHEVREL* <mailto:rchevrel@sodius.com>
>>
>>
>> SODIUS*
>> *6, rue Cornouaille
>> 44319 Nantes - France
>>
>> Phone: +33 (0)2 28 23 54 34
>>
>> ***www.mdworkbench.com* <http://www.mdworkbench.com/>* *
>> Draw more value from your model
>>
>>
>>> Hello,
>>> it is possible to write a rule where a target element is created
>>> conditionally?. I mean, in the following example:
>>>
>>> rule Source2Target {
>>> from
>>> s : MA!SourceClass
>>>
>>> to
>>> t1 : MB!TargetClass1(
>>> ...
>>> ),
>>>
>>> t2 : MB!TargetClass2(
>>> attr1 <- s.ref
>>> ...
>>> )
>>> }
>>>
>>> I would like to create the "t2 : MB!TargetClass2" element only if a
>>> [0-1] reference "ref" is defined (not empty) in the "s :
>>> MA!SourceClass" instance. Currently, when I execute that rule and
>>> s.ref does not exist I get an execution error.
>>> How can I manage that problem?
>>>
>>> Thank you in advance
>>> Joaquin
>>
|
|
|
Re: [ATL] Conditional target element creation [message #39295 is a reply to message #39264] |
Fri, 18 May 2007 16:29  |
Eclipse User |
|
|
|
Hello,
> Thank you both for your quick answers.
You are welcome.
> Your solution is quite more elegant and easy to maintenance than mine.
Well, I guess it is because we stick to the declarative style ;-).
It is true that you can do everything with the imperative style (e.g.,
your solution looks fine), and also that people are usually more used to
imperative from their experience with General Purpose Languages like
Java, C, etc.
However, it is obvious to me that going declarative is definitely worth
the effort. Moreover, it is definitely not as difficult as some people
probably imagine it is before even trying ;-).
> A new question I have: Are parameters of called rules "in" or "in/out"
> values? (maybe I should begin a new thread :S )
Parameters are "in" for OCL values (String, Integer, Sequence, Tuple, etc.).
Because model elements are passed "by reference", target elements passed
as parameters can be modified in the called rule (so, there are kind of
"in/out").
You can return a value by "putting it on the stack", until I implement
the "return" statement (but somehow, implementing new imperative
constructs is not as appealing as working on the declarative part of
ATL) ;-) .
For instance, if you do the following:
do {
1;
}
you are effectively returning 1.
You can return several values by putting them in a Tuple:
do {
Tuple {v1 = 5, v2 = 3};
}
Best regards,
Frédéric Jouault
|
|
|
Goto Forum:
Current Time: Fri Apr 25 20:58:02 EDT 2025
Powered by FUDForum. Page generated in 0.02664 seconds
|