Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » M2M (model-to-model transformation) » [ATL] Conditional target element creation
[ATL] Conditional target element creation [message #39172] Fri, 18 May 2007 11:12 Go to next message
Joaquin  Cañadas is currently offline Joaquin CañadasFriend
Messages: 25
Registered: July 2009
Junior Member
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 #39203 is a reply to message #39172] Fri, 18 May 2007 11:31 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: rchevrel.sodius.com

This is a multi-part message in MIME format.
--------------020903000108070802000504
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 8bit

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
Re: [ATL] Conditional target element creation [message #39233 is a reply to message #39203] Fri, 18 May 2007 12:36 Go to previous messageGo to next message
Frédéric Jouault is currently offline Frédéric JouaultFriend
Messages: 572
Registered: July 2009
Senior Member
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 18:17 Go to previous messageGo to next message
Joaquin  Cañadas is currently offline Joaquin CañadasFriend
Messages: 25
Registered: July 2009
Junior Member
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 20:29 Go to previous message
Frédéric Jouault is currently offline Frédéric JouaultFriend
Messages: 572
Registered: July 2009
Senior Member
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
Previous Topic:Conditional model element creation in ATL
Next Topic:[ATL] Table2SVGPieChart - Extract to XML?
Goto Forum:
  


Current Time: Thu Jan 21 06:16:55 GMT 2021

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

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

Back to the top