Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » How to set a reference with ecore reflection?
How to set a reference with ecore reflection? [message #1836518] Wed, 06 January 2021 16:28 Go to next message
Dimg Cim is currently offline Dimg CimFriend
Messages: 59
Registered: December 2015
Member
Hello all,

here I have defned my model with Xcore. Now I want to find out how to programmatically set the referencing of the persons (associatedPerson) to the group via reflection. I tried a lot but maybe this is not the right way.
class Person {
	String firstname
}

class Group {
	refers Person[] associatedPerson
	String name
	contains ExtendedGroup[] extendedgroup
}

class Manager {
	contains Group[] groups
	contains Person[] persons
}

class ExtendedGroup {
}


here is the corresponding test class to that model with trying to set the reference from Person to Group.

	public static void main(String[] args) {
		ComposedAdapterFactory adapterFactory = new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE);
		adapterFactory.addAdapterFactory(new ExampleItemProviderAdapterFactory());
		adapterFactory.addAdapterFactory(new ResourceItemProviderAdapterFactory());
		adapterFactory.addAdapterFactory(new ReflectiveItemProviderAdapterFactory());

		Manager manager = ExampleFactory.eINSTANCE.createManager();

		Group group = ExampleFactory.eINSTANCE.createGroup();
		manager.getGroups().add(group);
		
		Person person1 = ExampleFactory.eINSTANCE.createPerson();
		Person person2 = ExampleFactory.eINSTANCE.createPerson();
		group.getAssociatedPerson().add(person1);
		group.getAssociatedPerson().add(person2);

		ExtendedGroup extendedGroup = ExampleFactory.eINSTANCE.createExtendedGroup();
		
		manager.getGroups().add(group);
		manager.getPersons().add(person1);
		manager.getPersons().add(person2);
		

               // From here on I would like to do the setting of the associated persons via Reflection
		EClass eClass = group.eClass();
		EList<EReference> references = eClass.getEAllReferences();
		for (EReference r : references) {
		if (r.isContainment()) {
                               // Maybe something like this should be done?       
                               // Add person1 and person2 to group               
				r.eSet()
                              // Here the way without Reflections
                               group.getAssicatedPersons().add(person1);
                               group.getAssicatedPersons().add(person2);
		}
	}


best regards
Dimg

[Updated on: Wed, 06 January 2021 16:28]

Report message to a moderator

Re: How to set a reference with ecore reflection? [message #1836528 is a reply to message #1836518] Wed, 06 January 2021 23:10 Go to previous messageGo to next message
Gabe Colburn is currently offline Gabe ColburnFriend
Messages: 28
Registered: December 2012
Junior Member
I'm not familiar with XCore, but your code looks similar enough to my EMF experience that I might be able to help.

If you used a GenModel to generate concrete classes take a look at the implementation for the Group class (GroupImpl.java) to see how to do it reflexively.

If you have an EObject/CDOObject instance, in this case group, it will be something like this:

group.eGet(ExamplePackage.Literals.GROUP__ASSOCIATEDPERSON, true).add(person1);
group.eGet(ExamplePackage.Literals.GROUP__ASSOCIATEDPERSON, true).add(person2);
Re: How to set a reference with ecore reflection? [message #1836536 is a reply to message #1836528] Thu, 07 January 2021 05:59 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
It's of course rather pointless to do something reflectively if you have/know the generated API. I.e., if one uses ExamplePackage.Literals.GROUP__ASSOCIATED_PERSON for eGit then one knows one is dealing with a Group and that it has a getAssociatedPerson() method so it would be better to use that API

If you want to understand how reflection is used in general, in a context/application where the model actually isn't known up front, look in EcoreUtil's methods and especially at the Copier and the EqualityHelper and how they use eGet/eSet/eIsSet/eUnset.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: How to set a reference with ecore reflection? [message #1836563 is a reply to message #1836536] Thu, 07 January 2021 13:55 Go to previous messageGo to next message
Dimg Cim is currently offline Dimg CimFriend
Messages: 59
Registered: December 2015
Member
Thanks for the responds of you two.

@Ed Merks
yes you are right, thats the point, I don't know the generated API. The example is only for demo.

The EcoreUtils helps me a lot.

Thanks again.
Re: How to set a reference with ecore reflection? [message #1836566 is a reply to message #1836563] Thu, 07 January 2021 15:41 Go to previous messageGo to next message
Dimg Cim is currently offline Dimg CimFriend
Messages: 59
Registered: December 2015
Member
Just another question. How can I find out which propertyId is used for the EReference of the object. I tried to implement something like the FeatureEditorDialog logic for References. I find this why to get the chosen values

[code][IItemPropertySource itemPropertySource = ...;
IItemPropertyDescriptor propertyDescriptors = itemPropertySource.getPropertyDescriptor(object, propertyID)
propertyDescriptors.getChoiceOfValues(object);
/code]

but where can I get the property id?
And how can I get all the values of the FeatureEditorDialog to set the choices?

I know that a Group has the reference associatedPerson and in the ExamplePackage I can get the GROUP__ASSOCIATED_PERSON with the index 0, but there should be a way to get the propertyId, aren't there? How do the FeatureEditorDialog do this?

[Updated on: Thu, 07 January 2021 15:44]

Report message to a moderator

Re: How to set a reference with ecore reflection? [message #1836573 is a reply to message #1836566] Thu, 07 January 2021 18:28 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
You can use the feature itself as the propertID.

The org.eclipse.emf.edit.provider.ItemPropertyDescriptor.getComboBoxObjects(Object) already works reflectively to populate all the choices, so I don't know why you're asking this question.

You should use the debugger to try to answer these kinds of questions by looking at how the reflective editor works...


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:[CDO] ClassCastException CDOFeatureDeltaImpl$UnknownValue to InternalEObject
Next Topic:How to get reference and value from diff
Goto Forum:
  


Current Time: Thu Mar 28 12:33:26 GMT 2024

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

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

Back to the top