Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » ClassifierId across different but related models
ClassifierId across different but related models [message #1697261] Tue, 02 June 2015 19:42 Go to next message
Ernesto Posse is currently offline Ernesto PosseFriend
Messages: 381
Registered: March 2011
Senior Member
I have two models, M1 and M2, where M2 imports some elements from M1, and I want to do a simple switch (not a visitor) over some elements from M1 and M2. For example, M1 has elements E1 and E2 where E2 has E1 as super-type, and M2 has element E3 which also has E1 as super-type.

I would like to do

void f(E1 element)
{
    switch (element.eClass.getClassifierId())
    {
        case M1Package.E2:
            // do something with E2 element
            break;
        case M2Package.E3: 
            // do something with E2 element
            break;
    }
}


The problem is that in the generated code, the classifierId for M1Package.E2 and M2Package.E3 is the same, resulting in a duplicate case.

How can I make such switch in such a way that it is efficient?

Thanks.
Re: ClassifierId across different but related models [message #1697274 is a reply to message #1697261] Tue, 02 June 2015 21:10 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 30372
Registered: July 2009
Senior Member
Ernesto,

You'd need an if/else first on the package(s) involved. Of course
classifier IDs are unique only within a given package.

On 02/06/2015 9:42 PM, Ernesto Posse wrote:
> I have two models, M1 and M2, where M2 imports some elements from M1,
> and I want to do a simple switch (not a visitor) over some elements
> from M1 and M2. For example, M1 has elements E1 and E2 where E2 has E1
> as super-type, and M2 has element E3 which also has E1 as super-type.
>
> I would like to do
>
>
> void f(E1 element)
> {
> switch (element.eClass.getClassifierId())
> {
> case M1Package.E2:
> // do something with E2 element
> break;
> case M2Package.E3: // do something with E2 element
> break;
> }
> }
>
>
> The problem is that in the generated code, the classifierId for
> M1Package.E2 and M2Package.E3 is the same, resulting in a duplicate case.
>
> How can I make such switch in such a way that it is efficient?
>
> Thanks.
>
Re: ClassifierId across different but related models [message #1697276 is a reply to message #1697274] Tue, 02 June 2015 21:23 Go to previous messageGo to next message
Ernesto Posse is currently offline Ernesto PosseFriend
Messages: 381
Registered: March 2011
Senior Member
Thanks Ed.

So the only way is something as follows?

    static String f(E1 element)
    {
        EClass c = element.eClass();
        if (c == M1Factory.eINSTANCE.getM1Package().getE1())
            return "E1";
        else if (c == M1Factory.eINSTANCE.getM1Package().getE2())
            return "E2";
        else if (c == M2Factory.eINSTANCE.getM2Package().getE3())
            return "E3";
        return "None";
    }

Re: ClassifierId across different but related models [message #1697282 is a reply to message #1697276] Tue, 02 June 2015 21:57 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 30372
Registered: July 2009
Senior Member
Ernesto,

You can test c.ePackage() == MPackage1Package.eINSTANCE first and then
switch on the classifiers of that package. You can of course also test
c == M1Package.Literals.E1 as well. Of course good old fashioned
instanceof tests work as well. You can also look and how an
ComposedAdapterFactory works too. It's not clear your goal. Elegance of
expression? Performance?

On 02/06/2015 11:24 PM, Ernesto Posse wrote:
> Thanks Ed.
>
> So the only way is something as follows?
>
>
> static String f(E1 element)
> {
> EClass c = element.eClass();
> if (c == M1Factory.eINSTANCE.getM1Package().getE1())
> return "E1";
> else if (c == M1Factory.eINSTANCE.getM1Package().getE2())
> return "E2";
> else if (c == M2Factory.eINSTANCE.getM2Package().getE3())
> return "E3";
> return "None";
> }
>
>
Previous Topic:EcoreUtil.generateUUID()
Next Topic:Persisting EAnnotations into an XMI file
Goto Forum:
  


Current Time: Fri Aug 23 22:53:28 GMT 2019

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

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

Back to the top