Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Reference elements from other Ecore models
Reference elements from other Ecore models [message #1844837] Mon, 27 September 2021 15:54 Go to next message
John Henbergs is currently offline John HenbergsFriend
Messages: 239
Registered: October 2020
Senior Member
Hi all,

I have two similar Ecore metamodels, and I have created two models respectively. Now I am creating a third meta model that can reference elements from these two metamodels. This third metamodels looks like this.

index.php/fa/41030/0/

Now when I create the model for this third meta model, I need to be able to reference an element from the first model for Start and reference an element from the second model as End.

I created this third model and I used Load Resource in order to load the two models from which I want to reference elements, but I am not quite sure how to proceed, or if the third meta model is ok for what I am trying to achieve.

So in the following screenshot
index.php/fa/41032/0/
I need to have another option where I can look at all the elements from the first model (for Start) and second model (for End ) and choose the element that I want. Just like we can do here for example:
index.php/fa/41031/0/

Thank you!

[Updated on: Mon, 27 September 2021 15:55]

Report message to a moderator

Re: Reference elements from other Ecore models [message #1844838 is a reply to message #1844837] Mon, 27 September 2021 17:38 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33264
Registered: July 2009
Senior Member
There isn't actually a question in your post. I.e., you are trying to achieve something but you've not described that is... I'm not sure why you need so many different, though similar, models....

Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Reference elements from other Ecore models [message #1844958 is a reply to message #1844838] Thu, 30 September 2021 19:22 Go to previous messageGo to next message
John Henbergs is currently offline John HenbergsFriend
Messages: 239
Registered: October 2020
Senior Member
Hi Ed,

You are right, I will Try to explain as briefly what my issue is.

In the Ecore2Ecore mapping for example when you create a model to map the elements of two other metamodels, you get to choose the elements by choosing the Package like in the following screenshot:

index.php/fa/41054/0/

So my first question is: How can I get the same thing? So being able to choose what metamodel I want to use as input and output? Is there an option when you create the mapping metamodel, or you need to write some code?

Afterwards when you create a mapping rule you get to choose what elements you will map, where you get a list of the elements that are part of the source and target metamodels that are loaded in the first step as follows:

index.php/fa/41055/0/


How can this be achieved then?
Re: Reference elements from other Ecore models [message #1844963 is a reply to message #1844958] Fri, 01 October 2021 05:01 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33264
Registered: July 2009
Senior Member
Using Load Resource... you can load more resources into the resource set and the contents of these will be available as choices for properties. You can look at org.eclipse.emf.edit.provider.ItemPropertyDescriptor.getComboBoxObjects(Object) to see how that's done. You can also implement/specialize it yourself:

https://wiki.eclipse.org/EMF/FAQ#How_can_I_control_which_instances_are_available_as_choices_when_editing_a_property_in_the_properties_view.3F


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Reference elements from other Ecore models [message #1844970 is a reply to message #1844963] Fri, 01 October 2021 09:59 Go to previous messageGo to next message
John Henbergs is currently offline John HenbergsFriend
Messages: 239
Registered: October 2020
Senior Member
Hi Ed,

Yes, I think this is exactly what I was looking for.
Just a few more questions to see if I understand this correctly.

1- To Load Resources I should open the model file, right click on platform:/resource/... and click on Load Resource. There I could for example browse registered packages and choose the package from which I want to choose contents in the properties view. Is that correct?

2- When I read the article you suggested, it says "You can modify the generated item provider". I am not quite sure where I can find the generated item provider, if it is even generated automatically or I have to do it manually, and if so how? For now in my modelling project I only have the following:

index.php/fa/41057/0/

And after I find it I should just override it with the suggested code?

Thank you in advance!
Re: Reference elements from other Ecore models [message #1844973 is a reply to message #1844970] Fri, 01 October 2021 11:17 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33264
Registered: July 2009
Senior Member
1. Yes.
2. I suggest reading https://eclipsesource.com/blogs/tutorials/emf-tutorial/


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Reference elements from other Ecore models [message #1844974 is a reply to message #1844973] Fri, 01 October 2021 11:41 Go to previous messageGo to next message
John Henbergs is currently offline John HenbergsFriend
Messages: 239
Registered: October 2020
Senior Member
Hi Ed,

Thanks! So what I did is the following:

I opened the Genmodel file -> Right click on the root -> Generate Edit Code.

Then I open the java file generated for the element where the property is located, so if I have the following class in the metamodel.

index.php/fa/41059/0/

I open UniversityItemProvider.java

and I make the changes suggested in the article to addLocationPropertyDescriptor(Object object).

I save it and then create a model, and when I try to click on the properties pane in the location, I do not get any values, I have to write a string myself.

Am I doing something wrong in this process?

Thank you in advance!
Re: Reference elements from other Ecore models [message #1844976 is a reply to message #1844974] Fri, 01 October 2021 12:43 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33264
Registered: July 2009
Senior Member
Did you generate an editor? If not, what editor did you open? I assume you launched a new IDE from the IDE in which you generated code, right?

Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Reference elements from other Ecore models [message #1844981 is a reply to message #1844976] Fri, 01 October 2021 13:45 Go to previous messageGo to next message
John Henbergs is currently offline John HenbergsFriend
Messages: 239
Registered: October 2020
Senior Member
Ok I have create a very simple metamodel as follows:

index.php/fa/41062/0/

and after I save it, from the genmodel -> Generate All

In ModelRootItemProvider.java I modify location property by the following snippet of code:

	protected void addLocationPropertyDescriptor(Object object) {
		itemPropertyDescriptors
				.add(new ItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(),
						getResourceLocator(), getString("_UI_ModelRoot_location_feature"),
						getString("_UI_PropertyDescriptor_description", "_UI_ModelRoot_location_feature",
								"_UI_ModelRoot_type"),
						MappingV1Package.Literals.MODEL_ROOT__LOCATION, true, false, true,
						null, null, null)
				   {
			        @Override
			        public Collection<?> getChoiceOfValues(Object object)
			        {
			          List<Object> choiceOfValues = new ArrayList<Object>(super.getChoiceOfValues(object));
			          // Filter the choices before returning them.
			          return choiceOfValues;
			        }
			      });
			  }


I save everything and I run a new instance of Eclipse.

In the new instance of eclipse I create a test.mappingv1 model.

Right click on the root of the model -> Load Resource -> Browse from Workspace -> and I choose the metamodel city.ecore.

I try to click on the location property of ModelRoot, but now I cannot write anything myself there, but I also do not get anything else. This is a screenshot of it.

index.php/fa/41061/0/
Re: Reference elements from other Ecore models [message #1844983 is a reply to message #1844981] Fri, 01 October 2021 16:03 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33264
Registered: July 2009
Senior Member
Your location property is an EAttribute of type EString. What choices could it possibly provide other than all possible strings? Choices are only generated for things like an EReference where the types of object that conform to the reference's EClass are shown.

Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Reference elements from other Ecore models [message #1844990 is a reply to message #1844983] Fri, 01 October 2021 22:37 Go to previous messageGo to next message
John Henbergs is currently offline John HenbergsFriend
Messages: 239
Registered: October 2020
Senior Member
Thank you! I am now working on a solution, but I am having this issue where I try to load org.eclipse.emf.ecore and when I generate code I get some errors saying "cannot convert from ecore.EObject to org.eclipse.emf.ecore.EObject". Did I load something wrong or...?
Re: Reference elements from other Ecore models [message #1844993 is a reply to message #1844990] Sat, 02 October 2021 05:14 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7681
Registered: July 2009
Senior Member
Hi

Yes. This looks just like metamodel schizophrenia; you have loaded the same metamodel twice using different access paths in this case probably once by a reference to Ecore.ecore and again by a reference to http://www.eclipse.org/emf/2002/Ecore. You can Google Willink metamodel schizophrenia for some of my too-many posts on the topic. More on Bugzilla.

It is particularly common with genmodel where usedpackages requires a file reference but type references particularly data types are often nsURIs. However it doesn't usually occur for Ecore since EMF has special casing to hide some inconsistencies.

The simplest solution is to ensure that all your transitive metamodel references use only one style of reference. This can work for simple cases and with many tools.

When that fails, you can add known EPackage registry entries to 'misload' the nsURI as the *.ecore file, or vice-versa and a URI mapping to 'misload' the *.ecore as the nsURI.

For the general case where the entries are not conveniently known, as with general OCL tooling, the org.eclipse.ocl.pivot.internal.resource.StandaloneProjectMap can configure your ResourceSet to 'misload' static first / dynamic-first / first-referenced first and the problem goes away for the invoking program.

Regards

Ed Willink
Re: Reference elements from other Ecore models [message #1844994 is a reply to message #1844993] Sat, 02 October 2021 05:39 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33264
Registered: July 2009
Senior Member
Why did you need to load Ecore? To use EObject? Did you ensure that Ecore is a usedGenPackage such that you didn't end up generating a copy of Ecore?

Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Reference elements from other Ecore models [message #1844995 is a reply to message #1844994] Sat, 02 October 2021 09:51 Go to previous messageGo to next message
John Henbergs is currently offline John HenbergsFriend
Messages: 239
Registered: October 2020
Senior Member
Hello,

So I am not quite sure where to check the usedGenPackage (any help with that?), or how to solve it, but I want to load Ecore because I want to reference EObject and Epackage.

When I generate all I only have issues with EObject. Moreover, when I create the metamodel, I see that I can reference EObject even without loading ecore as in the following screenshot, but nevertheless I want to load it cause I also want to reference EPackge:

index.php/fa/41067/0/
Re: Reference elements from other Ecore models [message #1845000 is a reply to message #1844995] Sat, 02 October 2021 13:01 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33264
Registered: July 2009
Senior Member
Look in the *.genmodel with a text editor. Yes, EObject is always available, which is why I ask why you need to load something. But EPackage will not be available unless you load it. You should load from the Target Platform. When you generate you say you have issues, but you're not giving much information about that leaving only guess work. I can find no string with "cannot convert" in it so guessing comes to and end.

Look at the Change.ecore and Change.genmodel here:

https://git.eclipse.org/c/emf/org.eclipse.emf.git/tree/plugins/org.eclipse.emf.ecore.change/model

That is what your *.ecore and *.genmodel should look like.

Minimally like this:
<?xml version="1.0" encoding="UTF-8"?>
<ecore:EPackage xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="test" nsURI="http://test" nsPrefix="test">
  <eClassifiers xsi:type="ecore:EClass" name="Test">
    <eStructuralFeatures xsi:type="ecore:EReference" name="package" eType="ecore:EClass ../../org.eclipse.emf.ecore/model/Ecore.ecore#//EPackage"/>
  </eClassifiers>
</ecore:EPackage>

and this
<?xml version="1.0" encoding="UTF-8"?>
<genmodel:GenModel xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore"
    xmlns:genmodel="http://www.eclipse.org/emf/2002/GenModel" modelDirectory="/org.example.test/src" modelPluginID="org.example.test"
    modelName="My" rootExtendsClass="org.eclipse.emf.ecore.impl.MinimalEObjectImpl$Container"
    importerID="org.eclipse.emf.importer.ecore" complianceLevel="5.0" copyrightFields="false"
    usedGenPackages="../../org.eclipse.emf.ecore/model/Ecore.genmodel#//ecore" operationReflection="true"
    importOrganizing="true">
  <foreignModel>My.ecore</foreignModel>
  <genPackages prefix="Test" basePackage="org.example" disposableProviderFactory="true"
      ecorePackage="My.ecore#/">
    <genClasses ecoreClass="My.ecore#//Test">
      <genFeatures notify="false" createChild="false" propertySortChoices="true" ecoreFeature="ecore:EReference My.ecore#//Test/package"/>
    </genClasses>
  </genPackages>
</genmodel:GenModel>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Reference elements from other Ecore models [message #1845040 is a reply to message #1845000] Mon, 04 October 2021 11:30 Go to previous messageGo to next message
John Henbergs is currently offline John HenbergsFriend
Messages: 239
Registered: October 2020
Senior Member
Many thanks, problem resolved.

An issue I am having now it being able to have the path of objects in the available choices.

So, if I have a class as follows:

index.php/fa/41089/0/

On the available choices for input and output I will get all objects from all the loaded resources. However, being that they might have same names, besides the object name I also want to be able to see the path of each object.

I am trying to customise the ItemProvider for that and I want to go through all the Objects I the choiceOfValues List of Objects, and use a method like getPath(); but that is not available here. Is there another way to do it or is there something wrong with what I have here so far?

index.php/fa/41090/0/

Best,
John
Re: Reference elements from other Ecore models [message #1845041 is a reply to message #1845040] Mon, 04 October 2021 12:32 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7681
Registered: July 2009
Senior Member
Hi

A common practice is to offer qualified names or simple names followed by qualified names as the JDT completion assist does.

Regards

Ed Willink
Re: Reference elements from other Ecore models [message #1845042 is a reply to message #1845041] Mon, 04 October 2021 12:46 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33264
Registered: July 2009
Senior Member
The method you are specializing is for the choice of available EObjects. Nothing you do here affects the label of those objects. You'd need to specialize org.eclipse.emf.edit.provider.ItemPropertyDescriptor.getLabelProvider(Object) if you want something different from simply allowing each object to display the label as implemented by its own corresponding model's item providers. Given you've not restricted the type of object that can be referenced, you can't assume anything common about those objects...

Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Reference elements from other Ecore models [message #1848012 is a reply to message #1845042] Wed, 17 November 2021 12:09 Go to previous messageGo to next message
Jan Crkvic is currently offline Jan CrkvicFriend
Messages: 18
Registered: November 2021
Junior Member
Hi, I am having a similar issue. I create an Ecore Modeling Project, create an EClass and then and EReference as its child. I want to make the Etype of the Ereference as EPackage. For that reason, I Load Resource -> Browse Target Platform Packages -> ahttp://www.eclipse.org/emf/2002/Ecore . After doing this I get Epackage as alternative.

When I check my .genmodel I do not have usedGenPackages and I get errors in:
ecore.impl
(EcoreFactoryImpl.java (Type mismatch: cannot convert from ecore.EObject to org.eclipse.emf.ecore.EObject) and
EObjectImpl.java (The type EObjectImpl must implement the inherited abstract method EObject.eAllContents()))

and ecore.util
(EcoreValidator.java (The method validate_EveryDefaultConstraint(org.eclipse.emf.ecore.EObject, org.eclipse.emf.common.util.DiagnosticChain, java.util.Map<java.lang.Object,java.lang.
Object>) in the type EObjectValidator is not applicable for the arguments (ecore.EObject, org.eclipse.emf.common.util.DiagnosticChain, java.util.Map<java.lang.Object, java.lang.Object>)))

Should I use another way to load the resource?
Re: Reference elements from other Ecore models [message #1848021 is a reply to message #1848012] Wed, 17 November 2021 14:48 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33264
Registered: July 2009
Senior Member
You should Reload... the *.genmodel and on the Package Selection page, be sure that you check mark the org.eclipse.emf.ecore package in the "Referenced generator models" section in the lower half.



Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Reference elements from other Ecore models [message #1848024 is a reply to message #1848021] Wed, 17 November 2021 14:57 Go to previous message
Jan Crkvic is currently offline Jan CrkvicFriend
Messages: 18
Registered: November 2021
Junior Member
Works perfectly well, thank you :)
Previous Topic:General question about connection between model and "real world"
Next Topic:UML/XMI resource loader connects to www.eclipse.org
Goto Forum:
  


Current Time: Thu Dec 26 11:48:43 GMT 2024

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

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

Back to the top