|
|
|
|
Re: Extending Ecore XMI serialization [message #1219862 is a reply to message #1219836] |
Fri, 06 December 2013 10:29 |
ModelGeek Mising name Messages: 550 Registered: June 2011 |
Senior Member |
|
|
May be i am not explaining it well.
I have written an interface extending from ERference and have added new methods.
public interface EReferenceExt extends EReference {
public int getProperty();
}
public class EReferenceImplExt extends EReferenceImpl implements EReferenceExt {
protected int poperty = 1;
public int getProperty() {
return poperty;
}
}
Now i try to create ecore model programmatically:
EPackage pack = EcoreFactory.eINSTANCE.createEPackage();
pack.setName("pack");
pack.setNsURI("http://www.eclipse.org/emf/pack/person");
EClass person = EcoreFactory.eINSTANCE.createEClass();
person.setName("Person");
pack.getEClassifiers().add(person);
EAttribute name = EcoreFactory.eINSTANCE.createEAttribute();
name.setName("name");
name.setEType(EcorePackage.Literals.ESTRING);
person.getEStructuralFeatures().add(name);
EReference children = new EReferenceImplExt();
children.setName("children");
children.setEType(person);
children.setUpperBound(ETypedElement.UNBOUNDED_MULTIPLICITY);
children.setContainment(true);
children.setResolveProxies(true);
person.getEStructuralFeatures().add(children);
EReference spouse = new EReferenceImplExt();
spouse.setName("spouse");
spouse.setEType(person);
spouse.setContainment(true);
spouse.setResolveProxies(true);
person.getEStructuralFeatures().add(spouse);
URI uri1 = URI.createFileURI("c:\\testP.ecore");
ResourceSet resourceSet = new ResourceSetImpl();
//resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("ecore", new EcoreResourceFactoryImpl());
Resource resource = resourceSet.createResource(uri1);
resource.getContents().add(pack);
try {
resource.save(Collections.EMPTY_MAP);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Now when i see the ecore file i see
<eStructuralFeatures xsi:type="ecore:EReference" name="children" upperBound="-1"
eType="#//Person" containment="true"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="spouse" eType="#//Person"
containment="true"/>
it does not say anything about newadded property in EReferenceExt.... if i want to load the ecore model programmatically from generated ecore file, will it give me the instance of EReference or EReferenceExt? i want to get object of EReferenceExt.
do you have any clue?
thanks for your assistance
Cheers
[Updated on: Fri, 06 December 2013 10:32] Report message to a moderator
|
|
|
Re: Extending Ecore XMI serialization [message #1219947 is a reply to message #1219862] |
Sat, 07 December 2013 06:57 |
Ed Merks Messages: 33140 Registered: July 2009 |
Senior Member |
|
|
Comments below.
On 06/12/2013 11:29 AM, ModelGeek Mising name wrote:
> May be i am explaining it well. I have written an interface extending
> from ERference and have added new methods.
> public interface EReferenceExt extends EReference {
> public int getProperty();
> }
That seems like a bad idea.
>
> public class EReferenceImplExt extends EReferenceImpl implements
> EReferenceExt {
> protected int poperty = 1;
>
> public int getProperty() {
> return poperty;
> }
>
> }
>
> Now i try to create ecore model programmatically:
> EPackage pack = EcoreFactory.eINSTANCE.createEPackage();
> pack.setName("pack");
> pack.setNsURI("http://www.eclipse.org/emf/pack/person");
> EClass person = EcoreFactory.eINSTANCE.createEClass();
> person.setName("Person");
> pack.getEClassifiers().add(person);
>
> EAttribute name = EcoreFactory.eINSTANCE.createEAttribute();
> name.setName("name");
> name.setEType(EcorePackage.Literals.ESTRING);
> person.getEStructuralFeatures().add(name);
> EReference children = new EReferenceImplExt();
> children.setName("children");
> children.setEType(person);
> children.setUpperBound(ETypedElement.UNBOUNDED_MULTIPLICITY);
> children.setContainment(true);
> children.setResolveProxies(true);
> //children.setPeriroity(2);
> person.getEStructuralFeatures().add(children);
> EReference spouse = new EReferenceImplExt();
If you have an extension you ought to be using the factory for that
extended model to create the extension but I guess the sense that you
have no such model so that's not going to work at all because
spouse.eClass() is going to return just the EClass for EReference...
> spouse.setName("spouse");
> spouse.setEType(person);
> spouse.setContainment(true);
> spouse.setResolveProxies(true);
> person.getEStructuralFeatures().add(spouse);
>
> URI uri1 = URI.createFileURI("c:\\testP.ecore");
> ResourceSet resourceSet = new ResourceSetImpl();
> //resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("ecore",
> new EcoreResourceFactoryImpl());
>
> Resource resource = resourceSet.createResource(uri1);
> resource.getContents().add(pack); try {
> resource.save(Collections.EMPTY_MAP);
> } catch (IOException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
>
>
> Now when i see the ecore file i see
> <eStructuralFeatures xsi:type="ecore:EReference" name="children"
> upperBound="-1"
> eType="#//Person" containment="true"/>
> <eStructuralFeatures xsi:type="ecore:EReference" name="spouse"
> eType="#//Person"
> containment="true"/>
Well yes, if reflectively it looks like an EReference that what will be
serialized and if reflectively there is no "property" EAttribute it
won't be serialized either.
>
> it does not say anything about newadded property in EReferenceExt....
Because apparently you've not modeled an extension, you've just written
some Java code and by EMF reflection, that's completely invisible.
> if i want to load the ecore model programmatically from generated
> ecore file, will it give me the instance of EReference or
> EReferenceExt? i want to get object of EReferenceExt.
>
> do you have any clue?
Again, it's a bad idea, but if you feel compelled to ignore advice, you
need model your extension, not just hand write some Java code.
>
> Cheers
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.23284 seconds