Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » M2M (model-to-model transformation) » [QVTO] Mapping a enumeration
[QVTO] Mapping a enumeration [message #523929] Mon, 29 March 2010 21:14 Go to next message
Hugo Melo is currently offline Hugo MeloFriend
Messages: 14
Registered: July 2009
Junior Member
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 16:32 Go to previous messageGo to next message
PBarendrecht is currently offline PBarendrechtFriend
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 Smile

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 Go to previous messageGo to next message
Hugo Melo is currently offline Hugo MeloFriend
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 Go to previous messageGo to next message
Sergey Boyko is currently offline Sergey BoykoFriend
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
Re: [QVTO] Mapping a enumeration [message #524645 is a reply to message #524624] Thu, 01 April 2010 16:35 Go to previous message
Hugo Melo is currently offline Hugo MeloFriend
Messages: 14
Registered: July 2009
Junior Member
Hi Sergey,

sorry for mistakes. Actually i did not copy/paste from my code. I tried to create a simple context and wrote wrong code. Thank you for the 'dot' advice.

Hugo Melo
Previous Topic:[QVTo] Transformation from one to N models
Next Topic:[ATL] problem using injectors in atl.loadModel
Goto Forum:
  


Current Time: Sun Aug 18 03:39:43 GMT 2019

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

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

Back to the top