Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » How to programmatic create EClass attribute of non-simple type
How to programmatic create EClass attribute of non-simple type [message #388250] Fri, 01 October 2004 11:39 Go to next message
Eclipse UserFriend
Originally posted by: jla.pine.dk

Hi,

I've found out how to programmatic create ECore classes and attributes
of simple types like this:

EClass personClass = EcoreFactory.eINSTANCE.createEClass();
personClass.setName("Person");
EAttribute personAttr = EcoreFactory.eINSTANCE.createEAttribute();
personAttr.setName("name");
personAttr.setEType(EcorePackage.eINSTANCE.getEString());
personClass.getEStructuralFeatures().add(personAttr);

The above works find, but how do I set a non-simple type (EClass) for an
attribute?

I've tried the following:

// Create Address class
EClass addressClass = EcoreFactory.eINSTANCE.createEClass();
addressClass.setName("Address");
EAttribute eAttr = EcoreFactory.eINSTANCE.createEAttribute();
eAttr.setName("Street");
eAttr.setEType(EcorePackage.eINSTANCE.getEString());
addressClass.getEStructuralFeatures().add(eAttr);

// Create attribute of Address type
EAttribute personAttr = EcoreFactory.eINSTANCE.createEAttribute();
personAttr.setName("Address");
personAttr.setEType(addressClass); <-- wrong?

However, when serializing the SDO datagraph a ClassCastException is
thrown from XMLSaveImpl:

Caused by: java.lang.ClassCastException
at
org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl$Lookup.featureKin d(XMLSaveImpl.java:1849)

Hmmmm... it seems that XMLSaveImpl expects a EDataType or EReference...??

Yes, I have no idea how this ECore stuff works :(

Help greatly appreciated!

Regards
Jesper
Re: How to programmatic create EClass attribute of non-simple type [message #388251 is a reply to message #388250] Fri, 01 October 2004 11:53 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
Jesper,

It looks like you have a good grasp and have only missed a small but
essential point. An EStructuralFeature's type is an EClassifier, but
there are two kinds of EStructuralFeatures, an EAttribute and an
EReference, where the former's EClassifier must be an EDataType (or
EEnum which extends EDataType) and the later's EClassifier must be an
EClass. So depending on whether your feature's type is to be an
EDataType or an EClass, you should create either an EAttribute or an
EReference.


Jesper Ladegaard wrote:

> Hi,
>
> I've found out how to programmatic create ECore classes and attributes
> of simple types like this:
>
> EClass personClass = EcoreFactory.eINSTANCE.createEClass();
> personClass.setName("Person");
> EAttribute personAttr = EcoreFactory.eINSTANCE.createEAttribute();
> personAttr.setName("name");
> personAttr.setEType(EcorePackage.eINSTANCE.getEString());
> personClass.getEStructuralFeatures().add(personAttr);
>
> The above works find, but how do I set a non-simple type (EClass) for
> an attribute?
>
> I've tried the following:
>
> // Create Address class
> EClass addressClass = EcoreFactory.eINSTANCE.createEClass();
> addressClass.setName("Address");
> EAttribute eAttr = EcoreFactory.eINSTANCE.createEAttribute();
> eAttr.setName("Street");
> eAttr.setEType(EcorePackage.eINSTANCE.getEString());
> addressClass.getEStructuralFeatures().add(eAttr);
>
> // Create attribute of Address type
> EAttribute personAttr = EcoreFactory.eINSTANCE.createEAttribute();
> personAttr.setName("Address");
> personAttr.setEType(addressClass); <-- wrong?
>
> However, when serializing the SDO datagraph a ClassCastException is
> thrown from XMLSaveImpl:
>
> Caused by: java.lang.ClassCastException
> at
> org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl$Lookup.featureKin d(XMLSaveImpl.java:1849)
>
>
> Hmmmm... it seems that XMLSaveImpl expects a EDataType or EReference...??
>
> Yes, I have no idea how this ECore stuff works :(
>
> Help greatly appreciated!
>
> Regards
> Jesper


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: How to programmatic create EClass attribute of non-simple type [message #388252 is a reply to message #388251] Fri, 01 October 2004 13:08 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: jla.pine.dk

Thanks a lot!

But now to the next problem... how do I set the DataObject?

I've tried using EDataObject.setDataObject(...) like this:

EDataObject person =
(EDataObject)eFactory.create((EClass)ePackage.getEClassifier ( "Person");
EDataObject address =
(EDataObject)eFactory.create((EClass)ePackage.getEClassifier ( "Address");
person.setDataObject("address", address);

But when I serialize the datagraph the following exception is thrown:

Caused by: org.eclipse.emf.ecore.resource.Resource$IOWrappedException:
The object 'org.eclipse.emf.ecore.sdo.impl.EDataObjectImpl@954549
(eClass: org.eclipse.emf.ecore.impl.EClassImpl@1f7dab1 (name: Address)
(instanceClassName: null) (abstract: false, interface: false))' is not
contained in a resource.
at org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl.save(XMLSaveImpl. java:190)

Is it the object (instance) or the EClass it complains about?

I guess it's the object instance, but how do I include it in my DataGraph?

Regards
Jesper

Ed Merks wrote:

> Jesper,
>
> It looks like you have a good grasp and have only missed a small but
> essential point. An EStructuralFeature's type is an EClassifier, but
> there are two kinds of EStructuralFeatures, an EAttribute and an
> EReference, where the former's EClassifier must be an EDataType (or
> EEnum which extends EDataType) and the later's EClassifier must be an
> EClass. So depending on whether your feature's type is to be an
> EDataType or an EClass, you should create either an EAttribute or an
> EReference.
>
>
> Jesper Ladegaard wrote:
>
>> Hi,
>>
>> I've found out how to programmatic create ECore classes and attributes
>> of simple types like this:
>>
>> EClass personClass = EcoreFactory.eINSTANCE.createEClass();
>> personClass.setName("Person");
>> EAttribute personAttr = EcoreFactory.eINSTANCE.createEAttribute();
>> personAttr.setName("name");
>> personAttr.setEType(EcorePackage.eINSTANCE.getEString());
>> personClass.getEStructuralFeatures().add(personAttr);
>>
>> The above works find, but how do I set a non-simple type (EClass) for
>> an attribute?
>>
>> I've tried the following:
>>
>> // Create Address class
>> EClass addressClass = EcoreFactory.eINSTANCE.createEClass();
>> addressClass.setName("Address");
>> EAttribute eAttr = EcoreFactory.eINSTANCE.createEAttribute();
>> eAttr.setName("Street");
>> eAttr.setEType(EcorePackage.eINSTANCE.getEString());
>> addressClass.getEStructuralFeatures().add(eAttr);
>>
>> // Create attribute of Address type
>> EAttribute personAttr = EcoreFactory.eINSTANCE.createEAttribute();
>> personAttr.setName("Address");
>> personAttr.setEType(addressClass); <-- wrong?
>>
>> However, when serializing the SDO datagraph a ClassCastException is
>> thrown from XMLSaveImpl:
>>
>> Caused by: java.lang.ClassCastException
>> at
>> org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl$Lookup.featureKin d(XMLSaveImpl.java:1849)
>>
>>
>> Hmmmm... it seems that XMLSaveImpl expects a EDataType or EReference...??
>>
>> Yes, I have no idea how this ECore stuff works :(
>>
>> Help greatly appreciated!
>>
>> Regards
>> Jesper
>
>
Re: How to programmatic create EClass attribute of non-simple type [message #388253 is a reply to message #388252] Fri, 01 October 2004 13:14 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
Jesper,

Probably the EReference you created for the "address" feature needs to
have isContainment true, i.e., it needs to be "black diamond". The
default will be isContainment false, which will be a "cross" reference
rather than a containment reference, and any object referenced by such a
cross reference will need to be contained by some other reference in
order to be properly contained by the data graph.


Jesper Ladegaard wrote:

> Thanks a lot!
>
> But now to the next problem... how do I set the DataObject?
>
> I've tried using EDataObject.setDataObject(...) like this:
>
> EDataObject person =
> (EDataObject)eFactory.create((EClass)ePackage.getEClassifier ( "Person");
> EDataObject address =
> (EDataObject)eFactory.create((EClass)ePackage.getEClassifier ( "Address");
> person.setDataObject("address", address);
>
> But when I serialize the datagraph the following exception is thrown:
>
> Caused by: org.eclipse.emf.ecore.resource.Resource$IOWrappedException:
> The object 'org.eclipse.emf.ecore.sdo.impl.EDataObjectImpl@954549
> (eClass: org.eclipse.emf.ecore.impl.EClassImpl@1f7dab1 (name: Address)
> (instanceClassName: null) (abstract: false, interface: false))' is not
> contained in a resource.
> at
> org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl.save(XMLSaveImpl. java:190)
>
> Is it the object (instance) or the EClass it complains about?
>
> I guess it's the object instance, but how do I include it in my
> DataGraph?
>
> Regards
> Jesper
>
> Ed Merks wrote:
>
>> Jesper,
>>
>> It looks like you have a good grasp and have only missed a small but
>> essential point. An EStructuralFeature's type is an EClassifier, but
>> there are two kinds of EStructuralFeatures, an EAttribute and an
>> EReference, where the former's EClassifier must be an EDataType (or
>> EEnum which extends EDataType) and the later's EClassifier must be an
>> EClass. So depending on whether your feature's type is to be an
>> EDataType or an EClass, you should create either an EAttribute or an
>> EReference.
>>
>>
>> Jesper Ladegaard wrote:
>>
>>> Hi,
>>>
>>> I've found out how to programmatic create ECore classes and
>>> attributes of simple types like this:
>>>
>>> EClass personClass = EcoreFactory.eINSTANCE.createEClass();
>>> personClass.setName("Person");
>>> EAttribute personAttr = EcoreFactory.eINSTANCE.createEAttribute();
>>> personAttr.setName("name");
>>> personAttr.setEType(EcorePackage.eINSTANCE.getEString());
>>> personClass.getEStructuralFeatures().add(personAttr);
>>>
>>> The above works find, but how do I set a non-simple type (EClass)
>>> for an attribute?
>>>
>>> I've tried the following:
>>>
>>> // Create Address class
>>> EClass addressClass = EcoreFactory.eINSTANCE.createEClass();
>>> addressClass.setName("Address");
>>> EAttribute eAttr = EcoreFactory.eINSTANCE.createEAttribute();
>>> eAttr.setName("Street");
>>> eAttr.setEType(EcorePackage.eINSTANCE.getEString());
>>> addressClass.getEStructuralFeatures().add(eAttr);
>>>
>>> // Create attribute of Address type
>>> EAttribute personAttr = EcoreFactory.eINSTANCE.createEAttribute();
>>> personAttr.setName("Address");
>>> personAttr.setEType(addressClass); <-- wrong?
>>>
>>> However, when serializing the SDO datagraph a ClassCastException is
>>> thrown from XMLSaveImpl:
>>>
>>> Caused by: java.lang.ClassCastException
>>> at
>>> org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl$Lookup.featureKin d(XMLSaveImpl.java:1849)
>>>
>>>
>>> Hmmmm... it seems that XMLSaveImpl expects a EDataType or
>>> EReference...??
>>>
>>> Yes, I have no idea how this ECore stuff works :(
>>>
>>> Help greatly appreciated!
>>>
>>> Regards
>>> Jesper
>>
>>
>>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: How to programmatic create EClass attribute of non-simple type [message #388254 is a reply to message #388253] Fri, 01 October 2004 13:18 Go to previous message
Eclipse UserFriend
Originally posted by: jla.pine.dk

Makes sense.

And it works too :-)

Thanks a lot!
Jesper

> Jesper,
>
> Probably the EReference you created for the "address" feature needs to
> have isContainment true, i.e., it needs to be "black diamond". The
> default will be isContainment false, which will be a "cross" reference
> rather than a containment reference, and any object referenced by such a
> cross reference will need to be contained by some other reference in
> order to be properly contained by the data graph.
>
>
> Jesper Ladegaard wrote:
>
>> Thanks a lot!
>>
>> But now to the next problem... how do I set the DataObject?
>>
>> I've tried using EDataObject.setDataObject(...) like this:
>>
>> EDataObject person =
>> (EDataObject)eFactory.create((EClass)ePackage.getEClassifier ( "Person");
>> EDataObject address =
>> (EDataObject)eFactory.create((EClass)ePackage.getEClassifier ( "Address");
>> person.setDataObject("address", address);
>>
>> But when I serialize the datagraph the following exception is thrown:
>>
>> Caused by: org.eclipse.emf.ecore.resource.Resource$IOWrappedException:
>> The object 'org.eclipse.emf.ecore.sdo.impl.EDataObjectImpl@954549
>> (eClass: org.eclipse.emf.ecore.impl.EClassImpl@1f7dab1 (name: Address)
>> (instanceClassName: null) (abstract: false, interface: false))' is not
>> contained in a resource.
>> at
>> org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl.save(XMLSaveImpl. java:190)
>> Is it the object (instance) or the EClass it complains about?
>>
>> I guess it's the object instance, but how do I include it in my
>> DataGraph?
>>
>> Regards
>> Jesper
>>
>> Ed Merks wrote:
>>
>>> Jesper,
>>>
>>> It looks like you have a good grasp and have only missed a small but
>>> essential point. An EStructuralFeature's type is an EClassifier, but
>>> there are two kinds of EStructuralFeatures, an EAttribute and an
>>> EReference, where the former's EClassifier must be an EDataType (or
>>> EEnum which extends EDataType) and the later's EClassifier must be an
>>> EClass. So depending on whether your feature's type is to be an
>>> EDataType or an EClass, you should create either an EAttribute or an
>>> EReference.
>>>
>>>
>>> Jesper Ladegaard wrote:
>>>
>>>> Hi,
>>>>
>>>> I've found out how to programmatic create ECore classes and
>>>> attributes of simple types like this:
>>>>
>>>> EClass personClass = EcoreFactory.eINSTANCE.createEClass();
>>>> personClass.setName("Person");
>>>> EAttribute personAttr = EcoreFactory.eINSTANCE.createEAttribute();
>>>> personAttr.setName("name");
>>>> personAttr.setEType(EcorePackage.eINSTANCE.getEString());
>>>> personClass.getEStructuralFeatures().add(personAttr);
>>>>
>>>> The above works find, but how do I set a non-simple type (EClass)
>>>> for an attribute?
>>>>
>>>> I've tried the following:
>>>>
>>>> // Create Address class
>>>> EClass addressClass = EcoreFactory.eINSTANCE.createEClass();
>>>> addressClass.setName("Address");
>>>> EAttribute eAttr = EcoreFactory.eINSTANCE.createEAttribute();
>>>> eAttr.setName("Street");
>>>> eAttr.setEType(EcorePackage.eINSTANCE.getEString());
>>>> addressClass.getEStructuralFeatures().add(eAttr);
>>>>
>>>> // Create attribute of Address type
>>>> EAttribute personAttr = EcoreFactory.eINSTANCE.createEAttribute();
>>>> personAttr.setName("Address");
>>>> personAttr.setEType(addressClass); <-- wrong?
>>>>
>>>> However, when serializing the SDO datagraph a ClassCastException is
>>>> thrown from XMLSaveImpl:
>>>>
>>>> Caused by: java.lang.ClassCastException
>>>> at
>>>> org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl$Lookup.featureKin d(XMLSaveImpl.java:1849)
>>>>
>>>>
>>>> Hmmmm... it seems that XMLSaveImpl expects a EDataType or
>>>> EReference...??
>>>>
>>>> Yes, I have no idea how this ECore stuff works :(
>>>>
>>>> Help greatly appreciated!
>>>>
>>>> Regards
>>>> Jesper
>>>
>>>
>>>
>>>
Previous Topic:Specifying package information in Ecore model
Next Topic:emf, xsd sdo cvs
Goto Forum:
  


Current Time: Fri Apr 19 14:52:13 GMT 2024

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

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

Back to the top