Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » QVT-OML » Replace conditional statements with queries
Replace conditional statements with queries [message #1850065] Mon, 14 February 2022 14:59 Go to next message
John Henbergs is currently offline John HenbergsFriend
Messages: 239
Registered: October 2020
Senior Member
Hi all,

I was wondering one thing.
If I have an example of a model transformation as follows:

mapping MMA:: EL1 :: EL1toEL2 : MMB: EL2{
if (self.subelement1 -> size() =1){
result.subelement2 := self.subelement1 -> map Subelement1toSubelement2();
}
else {
// do something else
}
}

mapping MMA:: Subelement1 :: Subelement1toSubelement2 : MMB: Subelement2{
//do something
}

Instead of using if and else, how can this be written as query?
Thank you!

Best,
John
Re: Replace conditional statements with queries [message #1850066 is a reply to message #1850065] Mon, 14 February 2022 15:08 Go to previous messageGo to next message
Christopher Gerking is currently offline Christopher GerkingFriend
Messages: 115
Registered: April 2011
Senior Member
Hi John

You could use a disjunction on the basis of a 'when' clause:

mapping MMA:: EL1 :: EL1toEL2 : MMB: EL2 disjuncts MMA::SingleEL1toEL2, MMA::MultipleEL1toEL2;

mapping MMA:: EL1 :: SingleEL1toEL2 : MMB: EL2 when {self.subelement1 -> size() = 1} {
    result.subelement2 := self.subelement1 -> map Subelement1toSubelement2();
}

mapping MMA:: EL1 :: EL1 :: MultipleEL1toEL2  : MMB: EL2 when {not self.subelement1 -> size() = 1} {
    // do something else
}


You don't actually need the second (negated) clause.


Kind regards
Christopher
Re: Replace conditional statements with queries [message #1850116 is a reply to message #1850066] Wed, 16 February 2022 17:37 Go to previous messageGo to next message
John Henbergs is currently offline John HenbergsFriend
Messages: 239
Registered: October 2020
Senior Member
Hi Christopher,

Thank you for your answer. Based on this, I have one additional question.

If you look at my example using if/else, pretend I have another rule

mapping MMA:: X :: X2Y : MMB: Y
disjuncts EL1: : EL1toEL2(), //and some other rules

Now, if instead of using if/else, I do as you suggested (using disjunct), which is the right way to write the X2Y mapping?

mapping MMA:: X :: X2Y : MMB: Y
disjuncts EL1: : EL1toEL2() (where EL12EL2 disjuncts two other rules) // some other rules

or

mapping MMA:: X :: X2Y : MMB: Y
disjuncts EL1 :: SingleEL1toEL2, EL1 :: MultipleEL1toEL2, //some other rules.

Many thanks!
Re: Replace conditional statements with queries [message #1850132 is a reply to message #1850116] Thu, 17 February 2022 08:44 Go to previous messageGo to next message
Christopher Gerking is currently offline Christopher GerkingFriend
Messages: 115
Registered: April 2011
Senior Member
Hi
John Henbergs wrote on Wed, 16 February 2022 12:37

Now, if instead of using if/else, I do as you suggested (using disjunct), which is the right way to write the X2Y mapping?

A tree of disjunctions (i.e., a disjunctive mapping that further disjuncts) is generally possible. So it depends on how much reusability you like or require. If you have a separate EL1: : EL1toEL2 mapping (that is disjunctive itself), you can reuse that mapping from other places. For example, you can invoke or resolve it separately, or use it as part of another disjunction. From your snippet, it's hard to tell whether your transformation could actually benefit from that.

Another criterion is readabilty. I usually like to avoid long lists of disjunctions in favor of a deeper, tree-like nesting (which also ensures maximum reusability). But that's probably a matter of taste, others might find it more intuitive to have a single long list.


Kind regards
Christopher
Re: Replace conditional statements with queries [message #1850133 is a reply to message #1850132] Thu, 17 February 2022 10:28 Go to previous messageGo to next message
John Henbergs is currently offline John HenbergsFriend
Messages: 239
Registered: October 2020
Senior Member
So if I understand correctly, both are possible:

mapping MMA:: X :: X2Y : MMB: Y
disjuncts EL1: : EL1toEL2() (where EL12EL2 disjuncts two other rules) // some other rules

mapping MMA:: X :: X2Y : MMB: Y
disjuncts EL1 :: SingleEL1toEL2, EL1 :: MultipleEL1toEL2, //some other rules.

But you would personally prefer the first one?

Best,
John
Re: Replace conditional statements with queries [message #1850231 is a reply to message #1850133] Tue, 22 February 2022 10:57 Go to previous message
Christopher Gerking is currently offline Christopher GerkingFriend
Messages: 115
Registered: April 2011
Senior Member
John Henbergs wrote on Thu, 17 February 2022 05:28
But you would personally prefer the first one?
My personal preference doesn't play a role.

Are you trying to get a specific transformation to work? If so, both variants should do what you want. Have you tried them out? If you are trying to achieve something other than a specific transformation, please clarify so we can help you in a more targeted way.

Kind regards
Christopher
Previous Topic:Modeltype metamodel
Next Topic:Mono and multi valued attributes
Goto Forum:
  


Current Time: Fri Apr 19 04:27:09 GMT 2024

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

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

Back to the top