Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Generated edit code and Dynamic EMF
Generated edit code and Dynamic EMF [message #663271] Mon, 04 April 2011 13:44 Go to next message
Steve Missing name is currently offline Steve Missing nameFriend
Messages: 6
Registered: April 2011
Junior Member
Hi,

I've searched hi and low but cannot find an answer, I think I understand what is going wrong but don't see how I fix the problem.

I am using an EMF (ecore) model that describes my system. I am using code generation to generate the mode, edit and editor code which is then used inside a set of plugins running in Eclipse.

In my model I have an EReference to an EObject which uses Dynamic EMF to allow my to save arbitrary data into my model. This allows my to have a "schema" that is different in different parts of the model but still ensure typed data. The "schema" is defined and also stored in another part of the model so I can use it for reference and when I generate my EObject.

This all works brilliantly and I can programmatically create my objects and the XML looks correct if I open the file up, but I'm having problems when it comes to the generated edit code. When I open the model to edit I get:

PackageNotFoundException: Package with uri 'null' not found.

And I cannot open the editor. The seems to be due to it not having a EPackage to be able to resolve the persisted EObjects against when it reads the data back in from the .xmi file. But the EPackage is defined in another part of the model. To fix this I guess I will need to make some changes to the edit code so that it can use my previously persisted EPackage during load?

Any pointers as to how I can make the generated edit code work nicely when I have a reference to some dynamically generated model objects?

Thank you.

[Updated on: Mon, 04 April 2011 13:44]

Report message to a moderator

Re: Generated edit code and Dynamic EMF [message #663314 is a reply to message #663271] Mon, 04 April 2011 15:08 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
Comments below.

rather.b.sailing@gmail.com wrote:
> Hi,
>
> I've searched hi and low but cannot find an answer, I think I
> understand what is going wrong but don't see how I fix the problem.
>
> I am using an EMF (ecore) model that describes my system. I am using
> code generation to generate the mode, edit and editor code which is
> then used inside a set of plugins running in Eclipse.
>
> In my model I have an EReference to an EObject which uses Dynamic EMF
> to allow my to save arbitrary data into my model. This allows my to
> have a "schema" that is different in different parts of the model but
> still ensure typed data. The "schema" is defined and also stored in
> another part of the model so I can use it for reference and when I
> generate my EObject.
>
> This all works brilliantly and I can programmatically create my
> objects and the XML looks correct if I open the file up, but I'm
> having problems when it comes to the generated edit code. When I open
> the model to edit I get:
>
> PackageNotFoundException: Package with uri 'null' not found.
Sounds like a problem with loading.
>
> And I cannot open the editor. The seems to be due to it not having a
> EPackage to be able to resolve the persisted EObjects against when it
> reads the data back in from the .xmi file. But the EPackage is defined
> in another part of the model.
Yes, but is it know to the package registry?
> To fix this I guess I will need to make some changes to the edit code
> so that it can use my previously persisted EPackage during load?
It should be registered either in the global package registry
EPackage.Registry.INSTANCE or the local one, ResourceSet.getPackageRegistry.
>
> Any pointers as to how I can make the generated edit code work nicely
> when I have a reference to some dynamically generated model objects?
>
> Thank you.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Generated edit code and Dynamic EMF [message #663462 is a reply to message #663314] Tue, 05 April 2011 08:05 Go to previous messageGo to next message
Steve Missing name is currently offline Steve Missing nameFriend
Messages: 6
Registered: April 2011
Junior Member
On 04/04/2011 16:08, Ed Merks wrote:
> Comments below.
>> And I cannot open the editor. The seems to be due to it not having a
>> EPackage to be able to resolve the persisted EObjects against when it
>> reads the data back in from the .xmi file. But the EPackage is defined
>> in another part of the model.
> Yes, but is it know to the package registry?
>> To fix this I guess I will need to make some changes to the edit code
>> so that it can use my previously persisted EPackage during load?
> It should be registered either in the global package registry
> EPackage.Registry.INSTANCE or the local one,
> ResourceSet.getPackageRegistry.

Thank you for the comments, it's been very helpful. I've now looked at
adding the EPackage into the registry. However I am stuck with a chicken
and egg scenario.

The EPackage that I define is part of the same Model as where I use the
EPackage to create my EObject. Both the EPackage and the EObject are
both persisted to the same .xmi file.

Looking at this code it looks like my only choice would be to persist
the EPackage into a separate .xmi file which can be loaded first and
added to the package registry? I can then load the main model.xmi file
and it will be able to be loaded as the appropriate EPackage will be
available in the registry.

Is there any way to do it and keep everything in the same file? I'm
fairly new to EMF but I guess for that to work the .xmi file would need
to have the right order? Currently the EPackage is referenced before the
XML parser finds the definition. Would doing something to re-order the
persistence help (is that even possible)?

Thank you,

Steve.
Re: Generated edit code and Dynamic EMF [message #663520 is a reply to message #663314] Tue, 05 April 2011 13:29 Go to previous messageGo to next message
Steve Missing name is currently offline Steve Missing nameFriend
Messages: 6
Registered: April 2011
Junior Member
On 04/04/2011 16:08, Ed Merks wrote:
> Comments below.
>> And I cannot open the editor. The seems to be due to it not having a
>> EPackage to be able to resolve the persisted EObjects against when it
>> reads the data back in from the .xmi file. But the EPackage is defined
>> in another part of the model.
> Yes, but is it know to the package registry?
>> To fix this I guess I will need to make some changes to the edit code
>> so that it can use my previously persisted EPackage during load?
> It should be registered either in the global package registry
> EPackage.Registry.INSTANCE or the local one,
> ResourceSet.getPackageRegistry.

To further clarify what I'm trying to achieve I've created a mini-model
and example .xmi file that I am trying to use, which will hopefully be
helpful.

The model allows me to declare multiple schemas and then multiple data
objects who's data structure is defined by one of the schemas. The
schemas can be created by the user dynamically and then the user can
create data that will be a Dynamic EMF instance of that schema and also
add that into the model.

The model will not load with the reflective editor in eclipse with the
previously mentioned "Package with uri 'null' not found." error message.
Although I did create the data part of the .xmi file by hand rather than
writing code but it will hopefully give the idea of what I am trying to
achieve.

Any pointers on how to achieve this sort of system would be greatly
appreciated.

Thank you.


My project.xmi file is:
=======================

<?xml version="1.0" encoding="ASCII"?>
<core:Project xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:core="http://example.com/core"
xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore"
xsi:schemaLocation="http://example.com/core example.ecore
http://www.eclipse.org/emf/2002/Ecore
.../../../plugin/org.eclipse.emf.ecore/model/Ecore.ecore">
<schemas>
<schema name="schema1">
<eClassifiers xsi:type="ecore:EClass" name="fieldList">
<eStructuralFeatures xsi:type="ecore:EAttribute"
name="stringField1">
<eType xsi:type="ecore:EDataType"
href="http://www.eclipse.org/emf/2002/Ecore#//EString"/>
</eStructuralFeatures>
</eClassifiers>
</schema>
</schemas>
<datas>
<customData xsi:type="fieldList" stringField1="someData" />
</datas>
</core:Project>



My model is 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="core"
nsURI="http://example.com/core" nsPrefix="core">
<eClassifiers xsi:type="ecore:EClass" name="schemaContainer">
<eStructuralFeatures xsi:type="ecore:EReference" name="schema"
eType="ecore:EClass
platform:/plugin/org.eclipse.emf.ecore/model/Ecore.ecore#//E Package "
containment="true"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="dataContainer">
<eStructuralFeatures xsi:type="ecore:EReference" name="customData"
eType="ecore:EClass
platform:/plugin/org.eclipse.emf.ecore/model/Ecore.ecore#//E Object "
containment="true"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="Project">
<eStructuralFeatures xsi:type="ecore:EReference" name="schemas"
upperBound="-1"
eType="#//schemaContainer" containment="true"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="datas"
upperBound="-1"
eType="#//dataContainer" containment="true"/>
</eClassifiers>
</ecore:EPackage>
Re: Generated edit code and Dynamic EMF [message #663575 is a reply to message #663462] Tue, 05 April 2011 16:30 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
Steve,

Comments below.

Steve wrote:
> On 04/04/2011 16:08, Ed Merks wrote:
>> Comments below.
>>> And I cannot open the editor. The seems to be due to it not having a
>>> EPackage to be able to resolve the persisted EObjects against when it
>>> reads the data back in from the .xmi file. But the EPackage is defined
>>> in another part of the model.
>> Yes, but is it know to the package registry?
>>> To fix this I guess I will need to make some changes to the edit code
>>> so that it can use my previously persisted EPackage during load?
>> It should be registered either in the global package registry
>> EPackage.Registry.INSTANCE or the local one,
>> ResourceSet.getPackageRegistry.
>
> Thank you for the comments, it's been very helpful. I've now looked at
> adding the EPackage into the registry. However I am stuck with a
> chicken and egg scenario.
>
> The EPackage that I define is part of the same Model as where I use
> the EPackage to create my EObject. Both the EPackage and the EObject
> are both persisted to the same .xmi file.
That's a bit of a problem...
>
> Looking at this code it looks like my only choice would be to persist
> the EPackage into a separate .xmi file which can be loaded first and
> added to the package registry? I can then load the main model.xmi file
> and it will be able to be loaded as the appropriate EPackage will be
> available in the registry.
That's how it normally works. Certainly it would need to appear in the
resource before the instance and you'd need to register the package as
soon as that part of the resource is loaded.
>
> Is there any way to do it and keep everything in the same file? I'm
> fairly new to EMF but I guess for that to work the .xmi file would
> need to have the right order?
It's a bit of work to try to make that work. We had similar situations
with SDO where the data graph could contain packages as well as
instances that conform to those. It was necessary to specialize the
resource implementation to ensure the packages were registered before
the instance data was processed.
> Currently the EPackage is referenced before the XML parser finds the
> definition. Would doing something to re-order the persistence help (is
> that even possible)?
Yes, it must appear first.
>
> Thank you,
>
> Steve.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Generated edit code and Dynamic EMF [message #663576 is a reply to message #663520] Tue, 05 April 2011 16:37 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
Steve,

Comments below.

Steve wrote:
> On 04/04/2011 16:08, Ed Merks wrote:
>> Comments below.
>>> And I cannot open the editor. The seems to be due to it not having a
>>> EPackage to be able to resolve the persisted EObjects against when it
>>> reads the data back in from the .xmi file. But the EPackage is defined
>>> in another part of the model.
>> Yes, but is it know to the package registry?
>>> To fix this I guess I will need to make some changes to the edit code
>>> so that it can use my previously persisted EPackage during load?
>> It should be registered either in the global package registry
>> EPackage.Registry.INSTANCE or the local one,
>> ResourceSet.getPackageRegistry.
>
> To further clarify what I'm trying to achieve I've created a
> mini-model and example .xmi file that I am trying to use, which will
> hopefully be helpful.
>
> The model allows me to declare multiple schemas and then multiple data
> objects who's data structure is defined by one of the schemas. The
> schemas can be created by the user dynamically and then the user can
> create data that will be a Dynamic EMF instance of that schema and
> also add that into the model.
>
> The model will not load with the reflective editor in eclipse with the
> previously mentioned "Package with uri 'null' not found." error message.
You will need a specialized resource implementation to mix your
instances and their models in the same resource. A normal
XMIResourceImpl won't be able to deal with this. (Hmmm, I suspect
though that if the instance included a schema location and the package
was earlier in the resource that it might well work like that.)
> Although I did create the data part of the .xmi file by hand rather
> than writing code but it will hopefully give the idea of what I am
> trying to achieve.
>
> Any pointers on how to achieve this sort of system would be greatly
> appreciated.
Try playing with the xsi:schemaLocation stuff. E.g.,
xsi:schemaLocation="<nsURI> #/<index>" where index is the zero-based
position of the package in the serialization.
>
> Thank you.
>
>
> My project.xmi file is:
> =======================
>
> <?xml version="1.0" encoding="ASCII"?>
> <core:Project xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xmlns:core="http://example.com/core"
> xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore"
> xsi:schemaLocation="http://example.com/core example.ecore
> http://www.eclipse.org/emf/2002/Ecore
> ../../../plugin/org.eclipse.emf.ecore/model/Ecore.ecore">
> <schemas>
> <schema name="schema1">
You'll need a the fragment path to get to this, i.e., #//@schema.0.
Null namespace packages aren't valid and will cause yet more problems.
Try to avoid them.
> <eClassifiers xsi:type="ecore:EClass" name="fieldList">
> <eStructuralFeatures xsi:type="ecore:EAttribute"
> name="stringField1">
> <eType xsi:type="ecore:EDataType"
> href="http://www.eclipse.org/emf/2002/Ecore#//EString"/>
> </eStructuralFeatures>
> </eClassifiers>
> </schema>
> </schemas>
> <datas>
> <customData xsi:type="fieldList" stringField1="someData" />
Classes should have upper case names. Ideally here you'd have
"foo:fieldList" where foo binds to the namespace of the package.
> </datas>
> </core:Project>
>
>
>
> My model is 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="core"
> nsURI="http://example.com/core" nsPrefix="core">
> <eClassifiers xsi:type="ecore:EClass" name="schemaContainer">
It would be best to try to conform to the case conventions, i.e.,
classifiers are upper case and features/operations are lower cased.
> <eStructuralFeatures xsi:type="ecore:EReference" name="schema"
> eType="ecore:EClass
> platform:/plugin/org.eclipse.emf.ecore/model/Ecore.ecore#//E Package "
> containment="true"/>
> </eClassifiers>
> <eClassifiers xsi:type="ecore:EClass" name="dataContainer">
> <eStructuralFeatures xsi:type="ecore:EReference" name="customData"
> eType="ecore:EClass
> platform:/plugin/org.eclipse.emf.ecore/model/Ecore.ecore#//E Object "
> containment="true"/>
> </eClassifiers>
> <eClassifiers xsi:type="ecore:EClass" name="Project">
> <eStructuralFeatures xsi:type="ecore:EReference" name="schemas"
> upperBound="-1"
> eType="#//schemaContainer" containment="true"/>
> <eStructuralFeatures xsi:type="ecore:EReference" name="datas"
> upperBound="-1"
> eType="#//dataContainer" containment="true"/>
> </eClassifiers>
> </ecore:EPackage>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Generated edit code and Dynamic EMF [message #663798 is a reply to message #663576] Wed, 06 April 2011 14:04 Go to previous messageGo to next message
Steve Missing name is currently offline Steve Missing nameFriend
Messages: 6
Registered: April 2011
Junior Member
On 05/04/2011 17:37, Ed Merks wrote:
>> The model will not load with the reflective editor in eclipse with the
>> previously mentioned "Package with uri 'null' not found." error message.
> You will need a specialized resource implementation to mix your
> instances and their models in the same resource. A normal
> XMIResourceImpl won't be able to deal with this. (Hmmm, I suspect though
> that if the instance included a schema location and the package was
> earlier in the resource that it might well work like that.)
>
>> Any pointers on how to achieve this sort of system would be greatly
>> appreciated.
> Try playing with the xsi:schemaLocation stuff. E.g.,
> xsi:schemaLocation="<nsURI> #/<index>" where index is the zero-based
> position of the package in the serialization.

To update on this, thank you very much, I now have it working!

I included the OPTION_SCHEMA_LOCATION option when saving the resource:

Map<Object, Object> options = new HashMap<Object, Object>();
options.put(XMLResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE);
project.eResource().save(options);

and ensured that a NsPrefix and NsURI were set on the schema EPackage
objects. This then ensured that the correct prefixes were used when I
created my EObject instances.

I then manually re-ordered the .xmi file to ensure that the EObjects
that used the schema were after the EPackage declaration in the .xmi
file and bingo!

Loads correctly :).

Now my only problem is how to change the way it is persisted to the .xmi
file to ensure the correct ordering. I'll update again once I've figured
out how to do that so other people have the whole solution
Re: Generated edit code and Dynamic EMF [message #663800 is a reply to message #663798] Wed, 06 April 2011 14:22 Go to previous message
Steve Missing name is currently offline Steve Missing nameFriend
Messages: 6
Registered: April 2011
Junior Member
On 06/04/2011 15:04, Steve wrote:
> Now my only problem is how to change the way it is persisted to the .xmi
> file to ensure the correct ordering. I'll update again once I've figured
> out how to do that so other people have the whole solution

Which can be solved by simply moving the EReference to the EObject
container to the last child reference of the parent. All fixed. Thank
you again for the help.
Previous Topic:Generic EMF Forms Editor?
Next Topic:[Teneo] java.util.GregorianCalendar cannot be cast to java.util.Date
Goto Forum:
  


Current Time: Fri Apr 19 10:26:25 GMT 2024

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

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

Back to the top