Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Set reference dynamically
Set reference dynamically [message #1809564] Thu, 18 July 2019 09:33 Go to next message
Kai S. is currently offline Kai S.Friend
Messages: 20
Registered: June 2019
Junior Member
Hey,

I am trying to set a reference feature after loading in an .ecore-model at runtime. Let's say my ecore-model defines 2 classes "Source" and "Target" and a reference "required" from "Source" to "Target" with the cardinality "0..*". So in the generated model code you have a member variable like this:
protected EList<Target> required;


Now, I create instance-objects dynamically of the classes "Source" and "Target". Lets say, both have an attribute "name" that I also set. So this would look like this:
//res here is the resource I loaded in dynamically from a given .ecore-file
EPackage metapackage = (EPackage) res.getContents().get(0);
EFactory metafactory = metapackage.getEFactoryInstance();

//creating an instance of SourceClass
EClass sourceClass = (EClass) metapackage.getEClassifier("SourceClass");
EAttribute sourceNameAttr = (EAttribute) sourceClass.getEStructuralFeature("name");
EObject sourceClassInstance = metafactory.create(sourceClass);
sourceClassInstance.eSet(sourceNameAttr, "Some beautiful source name...");

//creating an instance of TargetClass
EClass targetClass = (EClass) metapackage.getEClassifier("TargetClass");
EAttribute targetNameAttr = (EAttribute) targetClass.getEStructuralFeature("name");
EObject targetClassInstance = metafactory.create(targetClass);
targetClassInstance.eSet(targetNameAttr, "Some beautiful target name...");


Now, how do I set the "required"-reference from the sourceClassInstance to point to targetClassInstance? When looking at the documentation of EReference I do not find any setter-method to it, so I tried something like the following:
for(EReference r: sourceClass.getEAllReferences()) {
	if(r.getName().contentEquals("required")) {
		sourceClassInstance.eSet(r, targetClassInstance);
	}
}

But this does not seem to work.

Does anyone know how I would go about setting the "required"-reference to point to the targetClassInstance? Since this a 0..*-reference and thus implemented as a list, do I somehow need to retrieve the list and then call add() on it?

Thanks a lot!
Re: Set reference dynamically [message #1809566 is a reply to message #1809564] Thu, 18 July 2019 09:59 Go to previous messageGo to next message
Kai S. is currently offline Kai S.Friend
Messages: 20
Registered: June 2019
Junior Member
I nearly had it: I just realized that I just need to set a List of the required types via the eSet-method since the reference is a multi-value one :)
Re: Set reference dynamically [message #1809574 is a reply to message #1809566] Thu, 18 July 2019 11:18 Go to previous messageGo to next message
Kai S. is currently offline Kai S.Friend
Messages: 20
Registered: June 2019
Junior Member
I have another closely related question though, maybe someone can help me out with that:
How do I dynamically get the type of the reference-target? So how would I get to know that the EList required holds element of type "Target"? I guess I would need some kind of method on the EReference to hand me the target type like EReference.getTarget() or similar.

Is there a way to determine this at runtime?
Re: Set reference dynamically [message #1809590 is a reply to message #1809574] Thu, 18 July 2019 15:08 Go to previous messageGo to next message
Aurélien Mora is currently offline Aurélien MoraFriend
Messages: 37
Registered: July 2014
Member
Hello, I think you are looking for something like eReference.getEReferenceType().getInstanceClass().
Re: Set reference dynamically [message #1809611 is a reply to message #1809590] Thu, 18 July 2019 20:11 Go to previous message
Kai S. is currently offline Kai S.Friend
Messages: 20
Registered: June 2019
Junior Member
Hello,

yes, looks like I can get the info I need via getEReferenceType().getName()!
Thanks a lot for your tip.
Previous Topic:Failed on loading xcore model programmatically
Next Topic:[CDO] How to find the source of stale CDO-Objects
Goto Forum:
  


Current Time: Thu Dec 12 21:00:28 GMT 2019

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

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

Back to the top