Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Adding feature to dynamic instance model(Trying to access an EReference which is added to a dynamic instance model cuases a stack overflow. )
Adding feature to dynamic instance model [message #1818447] Mon, 16 December 2019 12:49 Go to next message
Stefan John is currently offline Stefan JohnFriend
Messages: 7
Registered: March 2018
Junior Member
Hi all,

I am currently trying to encapsulate a model element TypeA of a dynamic instance model within a wrapper extending EObjectImpl. I am trying to do that without extending the underlying metamodel.

So far, I managed to add the wrapper object to the model by substituting TypeA with EcoreUtil.replace(), effectively, injecting it into the containment hierarchy.
Now I would like TypeA to be contained in the wrapper.

// given an instance aInstance of TypeA and a wrapperInstance
EObject container = aInstance.eContainer();
if (container != null) {
	EStructuralFeature containment = aInstance.eContainmentFeature();
	EcoreUtil.replace(container, containment, aInstance, wrapperInstance);

	EcoreFactory eFactory = EcoreFactory.eINSTANCE;
	EReference referenceToWrapped = eFactory.createEReference();
	referenceToWrapped.setEType(aInstance.eClass());
	referenceToWrapped.setContainment(true);
	referenceToWrapped.setName("wrappedObject");

	wrapperInstance.eClass().getEStructuralFeatures().add(referenceToWrapped);
	wrapperInstance.eSet(referenceToWrapped, aInstance);
}


Unfortunately, trying to eSet() or eGet() the new reference results in an infinite loop in the implementation which is trying to deal with resolving the featureID.
Exception in thread "main" java.lang.StackOverflowError
	at org.eclipse.emf.ecore.impl.BasicEObjectImpl.eDerivedStructuralFeatureID(BasicEObjectImpl.java:1497)
	at org.eclipse.emf.ecore.impl.BasicEObjectImpl.eSet(BasicEObjectImpl.java:1068)
	at org.eclipse.emf.ecore.impl.BasicEObjectImpl.eSet(BasicEObjectImpl.java:1095)
	at org.eclipse.emf.ecore.impl.BasicEObjectImpl.eSet(BasicEObjectImpl.java:1071)
	...


Switching to eDynamicSet() seems to get closer to what I try to achieve. However, I was not able to find documentation of eDynamicSet() and eOpenSet() so I know nothing about the semantics or preconditions.

Exception in thread "main" java.lang.IllegalArgumentException: The feature 'wrappedObject' is not a valid changeable feature 
at org.eclipse.emf.ecore.impl.BasicEObjectImpl.eOpenSet(BasicEObjectImpl.java:1141)
	at org.eclipse.emf.ecore.impl.BasicEObjectImpl.eDynamicSet(BasicEObjectImpl.java:1119)
	at org.eclipse.emf.ecore.impl.BasicEObjectImpl.eDynamicSet(BasicEObjectImpl.java:1107)
	at vectoroptimization.dynamicemf.DynamicRunner.wrapSolutionObjects(DynamicRunner.java:176)
	at vectoroptimization.dynamicemf.DynamicRunner.loadModels(DynamicRunner.java:78)
	at vectoroptimization.dynamicemf.DynamicRunner.main(DynamicRunner.java:42)


I found a similar thread which is, however, about generated models.
https://www.eclipse.org/forums/index.php/t/152894/

I would really appreciate someone helping me to understand how or if extending a dynamic instance model is supported (without extending the underlying metamodel).
I got the EMF book at hand if that helps. Maybe I just didn't find the relevant parts in it.

Best wishes,
Stefan
Re: Adding feature to dynamic instance model [message #1818450 is a reply to message #1818447] Mon, 16 December 2019 14:17 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 30897
Registered: July 2009
Senior Member
No, you cannot (should not, must not) modify the Ecore model of an instance. Even if your Ecore model is one you synthesize dynamically, once you create an instance you must not change the model. The only way to achieve an instance that has more features than some other instance is if that instance is an instance of a different EClass, You can dynamically create a new EClass in a new EPackage and it may derive from some other EClass (such the the instance is compatible) and can of course that class can have additional features. But if you're going to persist the instance, you will need to persist the Ecore model too.
Re: Adding feature to dynamic instance model [message #1818451 is a reply to message #1818450] Mon, 16 December 2019 14:41 Go to previous messageGo to next message
Stefan John is currently offline Stefan JohnFriend
Messages: 7
Registered: March 2018
Junior Member
Thanks Ed, for that fast and definitive answer. I already feared that I am walking thin ice there.
Re: Adding feature to dynamic instance model [message #1818453 is a reply to message #1818451] Mon, 16 December 2019 14:58 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 6590
Registered: July 2009
Senior Member
Hi

As Ed M points out, Ecore does not support open classes. Ditto UML 2.x. UML 1.x had a Package merge capability deprecated in UML 2. Complete OCL requires open classes.

If you really want open classes you might want to look at EMF Facet that is on the point of being terminated. Grab its GIT quick. I've not studied it at all but my guess is that it revises the class model to elaborate a class as a merge of partial classes (facets). Doing this with the Ecore metamodel is hard since it conflicts with the EMF design intent.

Alternatively you might look at the Pivot OCL support for Complete OCL whereby a CompleteClass is a merge of one or more ordinary Classes with the same hierarchical nsURI. Doing this with the Pivot metamodel is tractable since it is part of the design intent.

Regards

Ed Willink

Re: Adding feature to dynamic instance model [message #1818457 is a reply to message #1818453] Mon, 16 December 2019 16:43 Go to previous message
Stefan John is currently offline Stefan JohnFriend
Messages: 7
Registered: March 2018
Junior Member
Also thanks @ theotherEd ;)

I will keep that in mind if I really need to go down that path. For now, I still see alternatives for my project plan which might be less of a hustle.
Previous Topic:History of EMF
Next Topic:Behaviors for proxy contained in containment feature after
Goto Forum:
  


Current Time: Mon Feb 24 00:39:14 GMT 2020

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

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

Back to the top