Skip to main content



      Home
Home » Modeling » EMF » how do I deal with multiple instances of the same EClass object?
how do I deal with multiple instances of the same EClass object? [message #406235] Fri, 05 January 2007 17:51 Go to next message
Eclipse UserFriend
I have a GenClass object, and I want to see if the corresponding EClass
object is of a certain type.

I tried the following:
if
(ModelsPackage.eINSTANCE.getNode().isSuperTypeOf(genClass.ge tEcoreClass()))
....

This was returning false, when I expected it to return true. After debugging
it, I found that the GenClass's ecore class DOES have a super type of type
Node, but it's a different instance of the EClass. They also have slightly
different properties as well, though I know they represent the same actual
class.

How do I tell if my ecore class subclasses a particular class, if I get it
from a GenClass? Or, how do I get the above statement to return true instead
of false?

Liam
Re: how do I deal with multiple instances of the same EClass object? [message #406236 is a reply to message #406235] Sat, 06 January 2007 00:35 Go to previous messageGo to next message
Eclipse UserFriend
Hi Liam,

The ecore model you are accessing from the Editor's genmodel was loaded
directly from the .ecore file, without any interference from the
generated code. That's why you are seeing different instances of the
metadata.

I would recommend you to stick with the metadata "available in the
editor". You could do something like this:

EPackage myEPackage = genPackage.getEcorePackage();
EClass nodeEClass = (EClass)myEPackage.getClassifier("Node");
nodeEClass.isSuperTypeOf(genClass.getEcoreClass());

I wrote this right here so forgive me if there is any typo or missing
semicolon ;-)

Cheers,
Marcelo

Liam Morley wrote:
> I have a GenClass object, and I want to see if the corresponding EClass
> object is of a certain type.
>
> I tried the following:
> if
> (ModelsPackage.eINSTANCE.getNode().isSuperTypeOf(genClass.ge tEcoreClass()))
> ....
>
> This was returning false, when I expected it to return true. After debugging
> it, I found that the GenClass's ecore class DOES have a super type of type
> Node, but it's a different instance of the EClass. They also have slightly
> different properties as well, though I know they represent the same actual
> class.
>
> How do I tell if my ecore class subclasses a particular class, if I get it
> from a GenClass? Or, how do I get the above statement to return true instead
> of false?
>
> Liam
>
>
Re: how do I deal with multiple instances of the same EClass object? [message #406257 is a reply to message #406236] Mon, 08 January 2007 15:35 Go to previous messageGo to next message
Eclipse UserFriend
Marcelo,
thanks- unfortunately, "Node" isn't necessarily in the same package (or even
the same file). I ended up choosing the following approach, though I'd be
curious if there's something better.

private boolean isNode(EClass subclass) {
if (nodeClass == null) {
List<EClass> supertypes = subclass.getESuperTypes();
for (EClass eClass : supertypes) {
if ("Node".equals(eClass.getName())
&&
"http//my/uri".equals(eClass.getEPackage().getNsURI()))
nodeClass = eClass;
}
}
return nodeClass != null && nodeClass.isSuperTypeOf(subclass);
}

if (isNode(genClass.getECoreClass()) ...


- liam


"Marcelo Paternostro" <marcelop@ca.ibm.com> wrote in message
news:enncfs$vrf$1@utils.eclipse.org...
> Hi Liam,
>
> The ecore model you are accessing from the Editor's genmodel was loaded
> directly from the .ecore file, without any interference from the generated
> code. That's why you are seeing different instances of the metadata.
>
> I would recommend you to stick with the metadata "available in the
> editor". You could do something like this:
>
> EPackage myEPackage = genPackage.getEcorePackage();
> EClass nodeEClass = (EClass)myEPackage.getClassifier("Node");
> nodeEClass.isSuperTypeOf(genClass.getEcoreClass());
>
> I wrote this right here so forgive me if there is any typo or missing
> semicolon ;-)
>
> Cheers,
> Marcelo
>
> Liam Morley wrote:
>> I have a GenClass object, and I want to see if the corresponding EClass
>> object is of a certain type.
>>
>> I tried the following:
>> if
>> (ModelsPackage.eINSTANCE.getNode().isSuperTypeOf(genClass.ge tEcoreClass()))
>> ....
>>
>> This was returning false, when I expected it to return true. After
>> debugging it, I found that the GenClass's ecore class DOES have a super
>> type of type Node, but it's a different instance of the EClass. They also
>> have slightly different properties as well, though I know they represent
>> the same actual class.
>>
>> How do I tell if my ecore class subclasses a particular class, if I get
>> it from a GenClass? Or, how do I get the above statement to return true
>> instead of false?
>>
>> Liam
Re: how do I deal with multiple instances of the same EClass object? [message #406261 is a reply to message #406257] Tue, 09 January 2007 01:56 Go to previous message
Eclipse UserFriend
Hi Liam,

I think the format of the final code really depends on your application.
One thing I noticed on the code below is that it doesn't check whether
'SubSubNode' is a 'Node' (SubSubNode -> SubNode -> Node). If this is
indeed a problem, you could use subclass.getEAllSuperTypes(), which
returns a collection with the entire super type hierarchy.

There are other alternatives, though. For example you could use
genModel.getAllGenAndUsedGenPackagesWithClassifiers() to retrieve all
GenPackages that are "related" to the genModel and use this collection
to initialize 'nodeClass'.

Cheers,
Marcelo

Liam Morley wrote:
> Marcelo,
> thanks- unfortunately, "Node" isn't necessarily in the same package (or even
> the same file). I ended up choosing the following approach, though I'd be
> curious if there's something better.
>
> private boolean isNode(EClass subclass) {
> if (nodeClass == null) {
> List<EClass> supertypes = subclass.getESuperTypes();
> for (EClass eClass : supertypes) {
> if ("Node".equals(eClass.getName())
> &&
> "http//my/uri".equals(eClass.getEPackage().getNsURI()))
> nodeClass = eClass;
> }
> }
> return nodeClass != null && nodeClass.isSuperTypeOf(subclass);
> }
>
> if (isNode(genClass.getECoreClass()) ...
>
>
> - liam
>
>
> "Marcelo Paternostro" <marcelop@ca.ibm.com> wrote in message
> news:enncfs$vrf$1@utils.eclipse.org...
>> Hi Liam,
>>
>> The ecore model you are accessing from the Editor's genmodel was loaded
>> directly from the .ecore file, without any interference from the generated
>> code. That's why you are seeing different instances of the metadata.
>>
>> I would recommend you to stick with the metadata "available in the
>> editor". You could do something like this:
>>
>> EPackage myEPackage = genPackage.getEcorePackage();
>> EClass nodeEClass = (EClass)myEPackage.getClassifier("Node");
>> nodeEClass.isSuperTypeOf(genClass.getEcoreClass());
>>
>> I wrote this right here so forgive me if there is any typo or missing
>> semicolon ;-)
>>
>> Cheers,
>> Marcelo
>>
>> Liam Morley wrote:
>>> I have a GenClass object, and I want to see if the corresponding EClass
>>> object is of a certain type.
>>>
>>> I tried the following:
>>> if
>>> (ModelsPackage.eINSTANCE.getNode().isSuperTypeOf(genClass.ge tEcoreClass()))
>>> ....
>>>
>>> This was returning false, when I expected it to return true. After
>>> debugging it, I found that the GenClass's ecore class DOES have a super
>>> type of type Node, but it's a different instance of the EClass. They also
>>> have slightly different properties as well, though I know they represent
>>> the same actual class.
>>>
>>> How do I tell if my ecore class subclasses a particular class, if I get
>>> it from a GenClass? Or, how do I get the above statement to return true
>>> instead of false?
>>>
>>> Liam
>
>
Previous Topic:Multiple Label Features
Next Topic:Changing a Static EMF Model Dynamically
Goto Forum:
  


Current Time: Tue Oct 28 18:02:57 EDT 2025

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

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

Back to the top