[QVTO] Mapping a enumeration [message #523929] |
Mon, 29 March 2010 17:14  |
Eclipse User |
|
|
|
Hi everyone,
im trying to map the values of a source enumeration to different objects. A mapping is necessary because i will need resolve the target objects.
Example:
source enum ---> enum Type { int, bool, string };
target object ---> Interface { string name; }
mapping Type2Interface() : Interface
{
name := self.????;
}
The result model should have:
Interface { name='int' }
Interface { name='bool' }
Interface { name='string' }
Later in the transformation....
mapping A2B() : B
{
result.interface := self.type.resolveone(Interface);
}
I hope that is uderstandable. Sorry for my english and organization.
Hugo Melo
|
|
|
Re: [QVTO] Mapping a enumeration [message #524367 is a reply to message #523929] |
Wed, 31 March 2010 12:32   |
Eclipse User |
|
|
|
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] |
Wed, 31 March 2010 23:30   |
Eclipse User |
|
|
|
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 06:54   |
Eclipse User |
|
|
|
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.05757 seconds