Code style / Performance / Best practice question [message #899259] |
Tue, 31 July 2012 08:57 |
Eclipse User |
|
|
|
Hello,
I convert from an input model which has a lot of abstract base classes
and references to those base classes. During the transformation I need
to figure out which concrete derived class is actually referenced. I
found two ways to do that:
Method A:
---------
mapping INMODEL::AbstractBase::theMapping() : OUTMODEL::SomeClass
{
switch {
case (self.oclIsKindOf(INMODEL::ClassA))
attrA := self.oclAsType(INMODEL::ClassA).map someClass();
case (self.oclIsKindOf(INMODEL::ClassB))
attrB := self.oclAsType(INMODEL::ClassB).map otherClass();
}
}
Method B:
---------
mapping INMODEL::AbstractBase::theMapping() : OUTMODEL::SomeClass
{
attrA := self[INMODEL::ClassA]->any(true).map someClass();
attrB := self[INMODEL::ClassB]->any(true).map otherClass()
}
It seems to me that the two methods behave identical. The first one
needs a cast and looks bloatier. The second one seems to be more concise
and does not need a cast. However, the "any(true)" seems a bit awkward.
So my questions really are:
1) which would be preferable in terms of "good QVT coding practice"?
2) which would be preferable in terms of performance?
3) if the answer to 1) is "neither": what would be a better way? Is
there maybe a way that QVT can dynamically choose the the right mapping?
BTW, the output is XML based on XSD. The above code maps to XSD elements
containing an xs:choice, of which there are a lot.
Thanks for any insights!
Marius
|
|
|
|
Re: Code style / Performance / Best practice question [message #899724 is a reply to message #899301] |
Thu, 02 August 2012 06:24 |
Eclipse User |
|
|
|
Hi Sebastian,
sorry for the late reply.
On 31.07.2012 14:14, Sebastian Lehrig wrote:
> Have you checked "Disjunction of Mapping Operations" as described in
> Section "8.1.13" of the QVT 1.1 Specification?
>
> What you will get is something like (beware, untested code ;) ):
> mapping INMODEL::AbstractBase::theMapping() : OUTMODEL::SomeClass
> disjuncts someClass(), otherClass() {}
> mapping INMODEL::ClassA::someClass() : JAVA::Field { ... }
> mapping INMODEL::ClassB::otherClass() : JAVA::Field { ... }
>
> Wouldn't this do the job for you?
Totally! I'll try that as soon as I can and will report back!
Thanks,
Marius
|
|
|
Powered by
FUDForum. Page generated in 1.01291 seconds