Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » QVT-OML » Conditional mappings
Conditional mappings [message #1850523] Mon, 07 March 2022 15:03 Go to next message
Francesco Bedini is currently offline Francesco BediniFriend
Messages: 32
Registered: March 2018
Member
I'm trying to convert a class of the source metamodel (let's call it S) in different ways, depending on one of the values of its attributes.

If S.attr = true, generate one single element of class D.
If S.attr = false, generate 3 elements of class D, and other objects
(simplified for clarity, in my case the attribute is not a boolean and has 5 possible values)

I thought about duplicating the mapping and using the when {} guard based on that attribute.

mapping S::transformationName() : D when {self.attr = true} {}

// 1: Generating the same output
mapping S::transformationName() : D when {self.attr = false} {}
// 2: Generating a different output
mapping S::transformationName() : d : List(D), e : List(E) when {self.attr = false} {}


Doing that, I get: "Operation 'transformationName' is already defined for type 'S'" in both cases

I have some notes from the past (I don't know from where) that said that theoretically the first mapping which has a "when" guard which results true should be selected for that transformation, is that wrong?

As, in my case, the different mappings would return a different number of elements and additional objects of other types of destination metamodel elements, is that even possible?
- Or should I just create a single mapping with an if condition which returns all possible outputs?
- Have different mappings with different names, and have an if which calls the right method for each element.

Thanks in advance for the support and let me know if you'd like to have a larger minimal working example, if I wasn't clear.

[Updated on: Mon, 07 March 2022 15:07]

Report message to a moderator

Re: Conditional mappings [message #1850525 is a reply to message #1850523] Mon, 07 March 2022 15:20 Go to previous message
Christopher Gerking is currently offline Christopher GerkingFriend
Messages: 115
Registered: April 2011
Senior Member
Hi

Francesco Bedini wrote on Mon, 07 March 2022 10:03
Doing that, I get: "Operation 'transformationName' is already defined for type 'InputType'" in both cases
Which makes sense because the two mappings have incompatible return types. Thus the QVTo compiler can't guess which of the two mappings you would like to invoke.

Quote:
I have some notes from the past (I don't know from where) that said that theoretically the first mapping which has a "when" guard which results true should be selected for that transformation, is that wrong?
It is wrong in general. Your are probably referring to a disjunctive mapping (using the 'disjuncts' keyword) which delegates the invocation to one of multiple alternatives.

Quote:
As, in my case, the different mappings would return a different number of elements and additional objects of other types of destination metamodel elements, is that even possible?
Disjunction requires conformance with the Liskov substitution principle. Since your mappings create a different number of elements, their signatures do not conform with that principle.


Quote:
Or should I just create a single mapping with an if condition which returns all possible outputs?
Sounds wrong to me because a mapping will always try to instantiate all of its result types.

Quote:
Have different mappings with different names, and have an if which calls the right method for each element.
This approach is certainly more promising.


Kind regards
Christopher
Previous Topic:Qualified name of reference
Next Topic:Wrong output model structure
Goto Forum:
  


Current Time: Wed Apr 24 16:01:15 GMT 2024

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

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

Back to the top