|
Re: [QVTO] Mapping a enumeration [message #524367 is a reply to message #523929] |
Wed, 31 March 2010 16:32 |
PBarendrecht Messages: 36 Registered: November 2009 Location: Eindhoven, Netherlands |
Member |
|
|
Hi Hugo,
I understand your case, however I'm not sure whether I understand your question correctly. You would like to transform your enumeration "Type" (you should consider another name because the word "Type" can be confusing), which contains of three attributes, to three separate objects right?
To start, your mapping should have a source/input object type. You can prefix your mapping with this object type, or use a parameter for this purpose.
Next, you have to define an target/output object type. You chose Interface, but you need three interfaces right?
You could do two things: either create three separate mappings for each attribute (int, bool, string) or return a set of Interfaces.
Finally, one "Type" should be split into three parts, all strings. An Integer can be converted to a String by using the function toString(), the Boolean variable by using repr().
Ok, an example:
--Method 1, use 3 seperate mappings. An example of the bool one:
mapping Type::Type2Bool() : Interface {
name := self.bool.repr();
}
--Method 2, one mapping which returns a set of Interfaces.
mapping Type::Type2Interfaces() : Set(Interface) {
result := Set{ object Interface {name := self.int.toString()}, object Interface {name := self.bool.repr()}, object Interface {name := self.string} };
}
I'm not 100% sure of this last construct, and there have been some problems with mappings returning Sets. If it doesn't work, please let us know
Good luck with QVT!
By the way, maybe you find my tutorial useful: Tutorial.
|
|
|
Re: [QVTO] Mapping a enumeration [message #524456 is a reply to message #524367] |
Thu, 01 April 2010 03:30 |
Hugo Melo Messages: 14 Registered: July 2009 |
Junior Member |
|
|
Hi PBarendrecht, ty for you reply.
I did not use neither of your two methods, but they help me for design a new one.
Causes:
Method 1 - This really works, but gave me a different ideia.
Method 2 - i will need resolve each mapped Interface. Returning a Collection, i cant control the input/output of Types and Interfaces. I implemented a similar solution using a query.
My solution:
Method 3 - create a mapping Type2Interface and call it from each enumeration value. Example:
mapping typeToInterface() : Interface
{
if ( self = Type::STRING ) then
result.name := 'String'
else
if ( self = Type::BOOL ) then
.
.
.
endif;
}
mapping dontmatter()
{
interfaces += Type::STRING-> map typeToInterface();
interfaces += Type::BOOL-> map TypeToInterface();
interfaces += Type::INT-> map TypeToInterface();
}
And later on the code, i can resolve the interface....
blablabla.interface := self.type.resolveoneIn ( type2Interface , Interface );
Actually i switched many simple mapping operations from your solution for one complex if-else-based mapping operation. On method 1, i can use "blablabla.interface := self.type.resolveone ( Interface )", right? Btw, thank you again and sorry for my english.
Hugo Melo
|
|
|
Re: [QVTO] Mapping a enumeration [message #524624 is a reply to message #524456] |
Thu, 01 April 2010 10:54 |
Sergey Boyko Messages: 171 Registered: July 2009 |
Senior Member |
|
|
Hi Hugo ,
Some comments are in-lined below.
Regards,
Sergey
Hugo Melo wrote:
> Hi PBarendrecht, ty for you reply.
>
> I did not use neither of your two methods, but they help me for design a
> new one.
>
> Causes:
>
> Method 1 - This really works, but gave me a different ideia.
>
> Method 2 - i will need resolve each mapped Interface. Returning a
> Collection, i cant control the input/output of Types and Interfaces. I
> implemented a similar solution using a query.
>
>
> My solution:
>
> Method 3 - create a mapping Type2Interface and call it from each
> enumeration value. Example:
>
>
> mapping typeToInterface() : Interface
> {
> if ( self = Type::STRING ) then
> result.name := 'String'
> else
> if ( self = Type::BOOL ) then
> .
> .
> .
> endif;
> }
The problem here is that you can't use 'self' since your mapping
'typeToInterface' doesn't not define context. Such code simply results
in compilation error.
Should be changed to follows:
mapping Type::typeToInterface() : Interface {
name := self.repr();
}
>
>
> mapping dontmatter()
> {
> interfaces += Type::STRING-> map typeToInterface();
> interfaces += Type::BOOL-> map TypeToInterface();
> interfaces += Type::INT-> map TypeToInterface();
> }
No need to use OCL shorthand '->' for calling mappings.
Just use dot accessor:
interfaces += Type::STRING.map typeToInterface();
>
>
> And later on the code, i can resolve the interface....
>
>
> blablabla.interface := self.type.resolveoneIn ( type2Interface ,
> Interface );
Typo here, should be:
blablabla.interface := self.type.resolveoneIn ( typeToInterface,
Interface );
>
>
>
> Actually i switched many simple mapping operations from your solution
> for one complex if-else-based mapping operation. On method 1, i can use
> "blablabla.interface := self.type.resolveone ( Interface )", right? Btw,
> thank you again and sorry for my english.
>
> Hugo Melo
|
|
|
|
Powered by
FUDForum. Page generated in 0.02995 seconds