Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Model Migration
Model Migration [message #416099] Thu, 17 January 2008 21:05 Go to next message
Mike Gering is currently offline Mike GeringFriend
Messages: 41
Registered: July 2009
Member
I've figured out how to use the ecore2xml method for migrating a
serialized model from a prior version. I hacked the code into the
generated Editor.createModel() method to get the basic method working.
Now I want to refactor that code so the migration occurs no matter how
the model is loaded.

I think the right approach is to create and register a ResourceFactory.
I created a subclass of XMIResourceFactoryImpl and overrode
createResource(URI uri):

public Resource createResource(URI uri) {
EPackage.Registry ePackageRegistry = new EPackageRegistryImpl(EPackage.Registry.INSTANCE);
ePackageRegistry.put("http://www.ibm.com/vce/1.0.0/rules", RulesPackage.eINSTANCE);
ePackageRegistry.put(" platform:/plugin/com.ibm.adt.vce.rules.model/model/Rules.eco re ", RulesPackage.eINSTANCE);
ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.setPackageRegistry(ePackageRegistry);
Ecore2XMLRegistry ecore2xmlRegistry = new Ecore2XMLRegistryImpl(Ecore2XMLRegistry.INSTANCE);
ecore2xmlRegistry.put("http://www.ibm.com/vce/1.0.0/rules",
EcoreUtil.getObjectByType(
resourceSet.getResource(URI.createURI(" platform:/plugin/com.ibm.adt.vce.rules.model/model/Rules100_ 2_Rules.ecore2xml "),
true).getContents(),
Ecore2XMLPackage.Literals.XML_MAP));
ExtendedMetaData extendedMetaData = new Ecore2XMLExtendedMetaData(EPackage.Registry.INSTANCE, ecore2xmlRegistry);
Map defaultLoadOptions = resourceSet.getLoadOptions();
defaultLoadOptions.put(XMLResource.OPTION_EXTENDED_META_DATA ,
extendedMetaData);
defaultLoadOptions.put(XMLResource.OPTION_RECORD_UNKNOWN_FEA TURE,
Boolean.TRUE);
defaultLoadOptions.put(XMLResource.OPTION_RESOURCE_HANDLER,
new RulesResourceHandler());
Resource resource = super.createResource(uri);

return resource;
}

It isn't working -- my resource handler never gets invoked and none of substitutions are made.

So, what is the best way to package this so the migration occurs whenever the resource is loaded?

Thanks,
Mike Gering
Re: Model Migration [message #416100 is a reply to message #416099] Thu, 17 January 2008 21:38 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33133
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------030101080802040404050102
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Mike,

Comments below.

Mike Gering wrote:
> I've figured out how to use the ecore2xml method for migrating a
> serialized model from a prior version. I hacked the code into the
> generated Editor.createModel() method to get the basic method working.
You deserve a prize!
> Now I want to refactor that code so the migration occurs no matter how
> the model is loaded.
>
> I think the right approach is to create and register a ResourceFactory.
Yes.
> I created a subclass of XMIResourceFactoryImpl and overrode
> createResource(URI uri):
>
> public Resource createResource(URI uri) {
> EPackage.Registry ePackageRegistry = new
> EPackageRegistryImpl(EPackage.Registry.INSTANCE);
> ePackageRegistry.put("http://www.ibm.com/vce/1.0.0/rules",
> RulesPackage.eINSTANCE);
>
> ePackageRegistry.put(" platform:/plugin/com.ibm.adt.vce.rules.model/model/Rules.eco re ",
> RulesPackage.eINSTANCE);
> ResourceSet resourceSet = new ResourceSetImpl();
You could have just used the resourceSet's package registry without
creating one because it's implemented like this:

public EPackage.Registry getPackageRegistry()
{
if (packageRegistry == null)
{
packageRegistry = new
EPackageRegistryImpl(EPackage.Registry.INSTANCE);
}
return packageRegistry;
}

> resourceSet.setPackageRegistry(ePackageRegistry);
> Ecore2XMLRegistry ecore2xmlRegistry = new
> Ecore2XMLRegistryImpl(Ecore2XMLRegistry.INSTANCE);
> ecore2xmlRegistry.put("http://www.ibm.com/vce/1.0.0/rules",
> EcoreUtil.getObjectByType(
>
> resourceSet.getResource(URI.createURI(" platform:/plugin/com.ibm.adt.vce.rules.model/model/Rules100_ 2_Rules.ecore2xml "),
>
> true).getContents(),
> Ecore2XMLPackage.Literals.XML_MAP));
I see. You're loading that resource for the mapping and ensuring that
you've mapped the development time model to the real generated model.
It strikes me though that you should only have to do this once ever, not
once each time a resource is created. So it's better to do this once
and cache the result for reuse with each resource created.
> ExtendedMetaData extendedMetaData = new
> Ecore2XMLExtendedMetaData(EPackage.Registry.INSTANCE, ecore2xmlRegistry);
> Map defaultLoadOptions = resourceSet.getLoadOptions();
> defaultLoadOptions.put(XMLResource.OPTION_EXTENDED_META_DATA ,
> extendedMetaData);
> defaultLoadOptions.put(XMLResource.OPTION_RECORD_UNKNOWN_FEA TURE,
> Boolean.TRUE);
> defaultLoadOptions.put(XMLResource.OPTION_RESOURCE_HANDLER,
> new RulesResourceHandler());
So these are the options you want to set on the resource you create, but
you've set them on the resource set that's not used again beyond this
point in the code. What you'll want to do is create the resource and
put these options into the resource's load options.
> Resource resource = super.createResource(uri);
>
> return resource;
> }
>
> It isn't working -- my resource handler never gets invoked and none of
> substitutions are made.
Nope I don't imagine they would.
>
> So, what is the best way to package this so the migration occurs
> whenever the resource is loaded?
It can be improved a little. :-P

Note that in EMF 2.4 it's possible to register against a content type so
that you could register this resource factory to apply only for
resources that use the old namespace. Unfortunately the docs are
non-existent. The XMI plugin shows how the content type for Ecore
itself is registered so at least there's one working example and
obviously I'll be happy to answer questions about it...
>
> Thanks,
> Mike Gering


--------------030101080802040404050102
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Mike,<br>
<br>
Comments below.<br>
<br>
Mike Gering wrote:
<blockquote cite="mid:fmoftl$or$1@build.eclipse.org" type="cite">I've
figured out how to use the ecore2xml method for migrating a
<br>
serialized model from a prior version. I hacked the code into the
<br>
generated Editor.createModel() method to get the basic method working.
<br>
</blockquote>
You deserve a prize!<br>
<blockquote cite="mid:fmoftl$or$1@build.eclipse.org" type="cite">Now I
want to refactor that code so the migration occurs no matter how
<br>
the model is loaded.
<br>
<br>
I think the right approach is to create and register a ResourceFactory.
<br>
</blockquote>
Yes.<br>
<blockquote cite="mid:fmoftl$or$1@build.eclipse.org" type="cite">I
created a subclass of XMIResourceFactoryImpl and overrode
<br>
createResource(URI uri):
<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;public Resource createResource(URI uri) {
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; EPackage.Registry ePackageRegistry = new
EPackageRegistryImpl(EPackage.Registry.INSTANCE);
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; ePackageRegistry.put(<a class="moz-txt-link-rfc2396E" href="http://www.ibm.com/vce/1.0.0/rules">"http://www.ibm.com/vce/1.0.0/rules"</a>,
RulesPackage.eINSTANCE);
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;
ePackageRegistry.put(" platform:/plugin/com.ibm.adt.vce.rules.model/model/Rules.eco re ",
RulesPackage.eINSTANCE);
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; ResourceSet resourceSet = new ResourceSetImpl();
<br>
</blockquote>
You could have just used the resourceSet's package registry without
creating one because it's implemented like this:<br>
<blockquote><small>&nbsp; public EPackage.Registry getPackageRegistry()</small><br>
<small>&nbsp; {</small><br>
<small>&nbsp;&nbsp;&nbsp; if (packageRegistry == null)</small><br>
<small>&nbsp;&nbsp;&nbsp; {</small><br>
<small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; packageRegistry = new
EPackageRegistryImpl(EPackage.Registry.INSTANCE);</small><br >
<small>&nbsp;&nbsp;&nbsp; }</small><br>
<small>&nbsp;&nbsp;&nbsp; return packageRegistry;</small><br>
<small>&nbsp; }</small><br>
</blockquote>
<blockquote cite="mid:fmoftl$or$1@build.eclipse.org" type="cite"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;
resourceSet.setPackageRegistry(ePackageRegistry);
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; Ecore2XMLRegistry ecore2xmlRegistry = new
Ecore2XMLRegistryImpl(Ecore2XMLRegistry.INSTANCE);
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; ecore2xmlRegistry.put(<a class="moz-txt-link-rfc2396E" href="http://www.ibm.com/vce/1.0.0/rules">"http://www.ibm.com/vce/1.0.0/rules"</a>,
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; EcoreUtil.getObjectByType(
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
resourceSet.getResource(URI.createURI(" platform:/plugin/com.ibm.adt.vce.rules.model/model/Rules100_ 2_Rules.ecore2xml "),
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; true).getContents(),
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; Ecore2XMLPackage.Literals.XML_MAP));
<br>
</blockquote>
I see.&nbsp; You're loading that resource for the mapping and ensuring that
you've mapped the development time model to the real generated model.&nbsp;
It strikes me though that you should only have to do this once ever,
not once each time a resource is created.&nbsp; So it's better to do this
once and cache the result for reuse with each resource created.<br>
<blockquote cite="mid:fmoftl$or$1@build.eclipse.org" type="cite"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;
ExtendedMetaData extendedMetaData = new
Ecore2XMLExtendedMetaData(EPackage.Registry.INSTANCE,
ecore2xmlRegistry);
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; Map defaultLoadOptions = resourceSet.getLoadOptions();
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; defaultLoadOptions.put(XMLResource.OPTION_EXTENDED_META_DATA ,
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; extendedMetaData);
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;
defaultLoadOptions.put(XMLResource.OPTION_RECORD_UNKNOWN_FEA TURE,
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Boolean.TRUE);
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; defaultLoadOptions.put(XMLResource.OPTION_RESOURCE_HANDLER,
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; new RulesResourceHandler());
<br>
</blockquote>
So these are the options you want to set on the resource you create,
but you've set them on the resource set that's not used again beyond
this point in the code.&nbsp; What you'll want to do is create the resource
and put these options into the resource's load options.<br>
<blockquote cite="mid:fmoftl$or$1@build.eclipse.org" type="cite"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;
Resource resource = super.createResource(uri);
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; return resource;
<br>
&nbsp;&nbsp;&nbsp;&nbsp;}
<br>
<br>
It isn't working -- my resource handler never gets invoked and none of
substitutions are made.
<br>
</blockquote>
Nope I don't imagine they would.<br>
<blockquote cite="mid:fmoftl$or$1@build.eclipse.org" type="cite"><br>
So, what is the best way to package this so the migration occurs
whenever the resource is loaded?
<br>
</blockquote>
It can be improved a little.&nbsp; :-P<br>
<br>
Note that in EMF 2.4 it's possible to register against a content type
so that you could register this resource factory to apply only for
resources that use the old namespace.&nbsp; Unfortunately the docs are
non-existent.&nbsp; The XMI plugin shows how the content type for Ecore
itself is registered so at least there's one working example and
obviously I'll be happy to answer questions about it...<br>
<blockquote cite="mid:fmoftl$or$1@build.eclipse.org" type="cite"><br>
Thanks,
<br>
Mike Gering
<br>
</blockquote>
<br>
</body>
</html>

--------------030101080802040404050102--


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Model Migration [message #416127 is a reply to message #416100] Fri, 18 January 2008 14:34 Go to previous messageGo to next message
Mike Gering is currently offline Mike GeringFriend
Messages: 41
Registered: July 2009
Member
Ed,

Thanks for prompt and excellent advice, as always!

Here is a revised code fragment:

public class RulesResourceFactoryImpl extends XMIResourceFactoryImpl {

public static final String RULES_100_NS_URI = "http://www.ibm.com/vce/1.0.0/rules";
public static final String RULES_PLATFORM_URI = " platform:/plugin/com.ibm.adt.vce.rules.model/model/Rules.eco re ";
public static final String RULES_100_PLATFORM_URI = " platform:/plugin/com.ibm.adt.vce.rules.model/model/Rules100_ 2_Rules.ecore2xml ";

private ExtendedMetaData extendedMetaData;

/* (non-Javadoc)
* @see org.eclipse.emf.ecore.resource.impl.ResourceFactoryImpl#crea teResource(org.eclipse.emf.common.util.URI)
*/
public Resource createResource(URI uri) {
XMIResource resource = (XMIResource) super.createResource(uri);

Map defaultLoadOptions = resource.getDefaultLoadOptions();
defaultLoadOptions.put(XMLResource.OPTION_EXTENDED_META_DATA ,
getExtendedMetaData());
defaultLoadOptions.put(XMLResource.OPTION_RECORD_UNKNOWN_FEA TURE,
Boolean.TRUE);
defaultLoadOptions.put(XMLResource.OPTION_RESOURCE_HANDLER,
new RulesResourceHandler());
return resource;
}

private ExtendedMetaData getExtendedMetaData() {
if(extendedMetaData == null) {
ResourceSet resourceSet = new ResourceSetImpl();
EPackage.Registry ePackageRegistry = resourceSet.getPackageRegistry();
ePackageRegistry.put(RULES_100_NS_URI, RulesPackage.eINSTANCE);
ePackageRegistry.put(RULES_PLATFORM_URI, RulesPackage.eINSTANCE);
resourceSet.setPackageRegistry(ePackageRegistry);
Ecore2XMLRegistry ecore2xmlRegistry = new Ecore2XMLRegistryImpl(Ecore2XMLRegistry.INSTANCE);
ecore2xmlRegistry.put(RULES_100_NS_URI,
EcoreUtil.getObjectByType(
resourceSet.getResource(URI.createURI(RULES_100_PLATFORM_URI ),
true).getContents(),
Ecore2XMLPackage.Literals.XML_MAP));
extendedMetaData = new Ecore2XMLExtendedMetaData(EPackage.Registry.INSTANCE, ecore2xmlRegistry);
}
return extendedMetaData;
}
}

If this passes muster, I can consider making a recipe of it. I found it
distressingly hard to get this far by reading the various newsgroup articles and
the EclipseCon 2006 slides.

Thanks again,
Mike
Re: Model Migration [message #416130 is a reply to message #416127] Fri, 18 January 2008 15:08 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33133
Registered: July 2009
Senior Member
Mike,

Please make a recipe! I'll beg and debase myself if I have to. As I
said, you deserve a prize for having figured it all out!


Mike Gering wrote:
> Ed,
>
> Thanks for prompt and excellent advice, as always!
>
> Here is a revised code fragment:
>
> public class RulesResourceFactoryImpl extends XMIResourceFactoryImpl {
>
> public static final String RULES_100_NS_URI =
> "http://www.ibm.com/vce/1.0.0/rules";
> public static final String RULES_PLATFORM_URI =
> " platform:/plugin/com.ibm.adt.vce.rules.model/model/Rules.eco re ";
> public static final String RULES_100_PLATFORM_URI =
> " platform:/plugin/com.ibm.adt.vce.rules.model/model/Rules100_ 2_Rules.ecore2xml ";
>
>
> private ExtendedMetaData extendedMetaData;
>
> /* (non-Javadoc)
> * @see
> org.eclipse.emf.ecore.resource.impl.ResourceFactoryImpl#crea teResource(org.eclipse.emf.common.util.URI)
>
> */
> public Resource createResource(URI uri) {
> XMIResource resource = (XMIResource) super.createResource(uri);
>
> Map defaultLoadOptions = resource.getDefaultLoadOptions();
> defaultLoadOptions.put(XMLResource.OPTION_EXTENDED_META_DATA ,
> getExtendedMetaData());
> defaultLoadOptions.put(XMLResource.OPTION_RECORD_UNKNOWN_FEA TURE,
> Boolean.TRUE);
> defaultLoadOptions.put(XMLResource.OPTION_RESOURCE_HANDLER,
> new RulesResourceHandler());
> return resource;
> }
>
> private ExtendedMetaData getExtendedMetaData() {
> if(extendedMetaData == null) {
> ResourceSet resourceSet = new ResourceSetImpl();
> EPackage.Registry ePackageRegistry =
> resourceSet.getPackageRegistry();
> ePackageRegistry.put(RULES_100_NS_URI,
> RulesPackage.eINSTANCE);
> ePackageRegistry.put(RULES_PLATFORM_URI,
> RulesPackage.eINSTANCE);
> resourceSet.setPackageRegistry(ePackageRegistry);
> Ecore2XMLRegistry ecore2xmlRegistry = new
> Ecore2XMLRegistryImpl(Ecore2XMLRegistry.INSTANCE);
> ecore2xmlRegistry.put(RULES_100_NS_URI,
> EcoreUtil.getObjectByType(
>
> resourceSet.getResource(URI.createURI(RULES_100_PLATFORM_URI ),
> true).getContents(),
> Ecore2XMLPackage.Literals.XML_MAP));
> extendedMetaData = new
> Ecore2XMLExtendedMetaData(EPackage.Registry.INSTANCE,
> ecore2xmlRegistry);
> }
> return extendedMetaData;
> }
> }
>
> If this passes muster, I can consider making a recipe of it. I found it
> distressingly hard to get this far by reading the various newsgroup
> articles and
> the EclipseCon 2006 slides.
>
> Thanks again,
> Mike


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Model Migration [message #416430 is a reply to message #416127] Thu, 31 January 2008 13:46 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: oemer.yildiz.semanticedge.com

Hello,

i've tried to migrate an older version of my model using your recipe,
but without success. After loading the instance of an old model, I
immediately called resource.save(), assuming that EMF would serialize it
using the newer version of my model.

Any hints?

Best regards,
Ömer yildiz

Mike Gering schrieb:
> Ed,
>
> Thanks for prompt and excellent advice, as always!
>
> Here is a revised code fragment:
>
> public class RulesResourceFactoryImpl extends XMIResourceFactoryImpl {
>
> public static final String RULES_100_NS_URI =
> "http://www.ibm.com/vce/1.0.0/rules";
> public static final String RULES_PLATFORM_URI =
> " platform:/plugin/com.ibm.adt.vce.rules.model/model/Rules.eco re ";
> public static final String RULES_100_PLATFORM_URI =
> " platform:/plugin/com.ibm.adt.vce.rules.model/model/Rules100_ 2_Rules.ecore2xml ";
>
>
> private ExtendedMetaData extendedMetaData;
>
> /* (non-Javadoc)
> * @see
> org.eclipse.emf.ecore.resource.impl.ResourceFactoryImpl#crea teResource(org.eclipse.emf.common.util.URI)
>
> */
> public Resource createResource(URI uri) {
> XMIResource resource = (XMIResource) super.createResource(uri);
>
> Map defaultLoadOptions = resource.getDefaultLoadOptions();
> defaultLoadOptions.put(XMLResource.OPTION_EXTENDED_META_DATA ,
> getExtendedMetaData());
> defaultLoadOptions.put(XMLResource.OPTION_RECORD_UNKNOWN_FEA TURE,
> Boolean.TRUE);
> defaultLoadOptions.put(XMLResource.OPTION_RESOURCE_HANDLER,
> new RulesResourceHandler());
> return resource;
> }
>
> private ExtendedMetaData getExtendedMetaData() {
> if(extendedMetaData == null) {
> ResourceSet resourceSet = new ResourceSetImpl();
> EPackage.Registry ePackageRegistry =
> resourceSet.getPackageRegistry();
> ePackageRegistry.put(RULES_100_NS_URI, RulesPackage.eINSTANCE);
> ePackageRegistry.put(RULES_PLATFORM_URI,
> RulesPackage.eINSTANCE);
> resourceSet.setPackageRegistry(ePackageRegistry);
> Ecore2XMLRegistry ecore2xmlRegistry = new
> Ecore2XMLRegistryImpl(Ecore2XMLRegistry.INSTANCE);
> ecore2xmlRegistry.put(RULES_100_NS_URI,
> EcoreUtil.getObjectByType(
>
> resourceSet.getResource(URI.createURI(RULES_100_PLATFORM_URI ),
> true).getContents(),
> Ecore2XMLPackage.Literals.XML_MAP));
> extendedMetaData = new
> Ecore2XMLExtendedMetaData(EPackage.Registry.INSTANCE,
> ecore2xmlRegistry);
> }
> return extendedMetaData;
> }
> }
>
> If this passes muster, I can consider making a recipe of it. I found it
> distressingly hard to get this far by reading the various newsgroup
> articles and
> the EclipseCon 2006 slides.
>
> Thanks again,
> Mike
Re: Model Migration [message #416432 is a reply to message #416430] Thu, 31 January 2008 13:49 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: oemer.yildiz.semanticedge.com

I forgot to mention, that I've already set the same value for
OPTION_EXTENDED_META_DATA in the default save options.


Ömer Yildiz schrieb:
> Hello,
>
> i've tried to migrate an older version of my model using your recipe,
> but without success. After loading the instance of an old model, I
> immediately called resource.save(), assuming that EMF would serialize it
> using the newer version of my model.
>
> Any hints?
>
> Best regards,
> Ömer yildiz
>
> Mike Gering schrieb:
>> Ed,
>>
>> Thanks for prompt and excellent advice, as always!
>>
>> Here is a revised code fragment:
>>
>> public class RulesResourceFactoryImpl extends XMIResourceFactoryImpl {
>>
>> public static final String RULES_100_NS_URI =
>> "http://www.ibm.com/vce/1.0.0/rules";
>> public static final String RULES_PLATFORM_URI =
>> " platform:/plugin/com.ibm.adt.vce.rules.model/model/Rules.eco re ";
>> public static final String RULES_100_PLATFORM_URI =
>> " platform:/plugin/com.ibm.adt.vce.rules.model/model/Rules100_ 2_Rules.ecore2xml ";
>>
>>
>> private ExtendedMetaData extendedMetaData;
>> /* (non-Javadoc)
>> * @see
>> org.eclipse.emf.ecore.resource.impl.ResourceFactoryImpl#crea teResource(org.eclipse.emf.common.util.URI)
>>
>> */
>> public Resource createResource(URI uri) {
>> XMIResource resource = (XMIResource) super.createResource(uri);
>>
>> Map defaultLoadOptions = resource.getDefaultLoadOptions();
>> defaultLoadOptions.put(XMLResource.OPTION_EXTENDED_META_DATA ,
>> getExtendedMetaData());
>> defaultLoadOptions.put(XMLResource.OPTION_RECORD_UNKNOWN_FEA TURE,
>> Boolean.TRUE);
>> defaultLoadOptions.put(XMLResource.OPTION_RESOURCE_HANDLER,
>> new RulesResourceHandler());
>> return resource;
>> }
>>
>> private ExtendedMetaData getExtendedMetaData() {
>> if(extendedMetaData == null) {
>> ResourceSet resourceSet = new ResourceSetImpl();
>> EPackage.Registry ePackageRegistry =
>> resourceSet.getPackageRegistry();
>> ePackageRegistry.put(RULES_100_NS_URI,
>> RulesPackage.eINSTANCE);
>> ePackageRegistry.put(RULES_PLATFORM_URI,
>> RulesPackage.eINSTANCE);
>> resourceSet.setPackageRegistry(ePackageRegistry);
>> Ecore2XMLRegistry ecore2xmlRegistry = new
>> Ecore2XMLRegistryImpl(Ecore2XMLRegistry.INSTANCE);
>> ecore2xmlRegistry.put(RULES_100_NS_URI,
>> EcoreUtil.getObjectByType(
>>
>> resourceSet.getResource(URI.createURI(RULES_100_PLATFORM_URI ),
>> true).getContents(),
>> Ecore2XMLPackage.Literals.XML_MAP));
>> extendedMetaData = new
>> Ecore2XMLExtendedMetaData(EPackage.Registry.INSTANCE,
>> ecore2xmlRegistry); }
>> return extendedMetaData;
>> }
>> }
>>
>> If this passes muster, I can consider making a recipe of it. I found it
>> distressingly hard to get this far by reading the various newsgroup
>> articles and
>> the EclipseCon 2006 slides.
>>
>> Thanks again,
>> Mike
Re: Model Migration [message #416434 is a reply to message #416432] Thu, 31 January 2008 14:12 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33133
Registered: July 2009
Senior Member
Ömer,

It sounds to me like this is liable to do the reverse mapping. What
happens if you don't do that? You can use this option mapped to
Boolean.TRUE for save if you need the option to produce a
schema-conforming serialization...


Ömer Yildiz wrote:
> I forgot to mention, that I've already set the same value for
> OPTION_EXTENDED_META_DATA in the default save options.
>
>
> Ömer Yildiz schrieb:
>> Hello,
>>
>> i've tried to migrate an older version of my model using your recipe,
>> but without success. After loading the instance of an old model, I
>> immediately called resource.save(), assuming that EMF would serialize
>> it using the newer version of my model.
>>
>> Any hints?
>>
>> Best regards,
>> Ömer yildiz
>>
>> Mike Gering schrieb:
>>> Ed,
>>>
>>> Thanks for prompt and excellent advice, as always!
>>>
>>> Here is a revised code fragment:
>>>
>>> public class RulesResourceFactoryImpl extends XMIResourceFactoryImpl {
>>>
>>> public static final String RULES_100_NS_URI =
>>> "http://www.ibm.com/vce/1.0.0/rules";
>>> public static final String RULES_PLATFORM_URI =
>>> " platform:/plugin/com.ibm.adt.vce.rules.model/model/Rules.eco re ";
>>> public static final String RULES_100_PLATFORM_URI =
>>> " platform:/plugin/com.ibm.adt.vce.rules.model/model/Rules100_ 2_Rules.ecore2xml ";
>>>
>>>
>>> private ExtendedMetaData extendedMetaData;
>>> /* (non-Javadoc)
>>> * @see
>>> org.eclipse.emf.ecore.resource.impl.ResourceFactoryImpl#crea teResource(org.eclipse.emf.common.util.URI)
>>>
>>> */
>>> public Resource createResource(URI uri) {
>>> XMIResource resource = (XMIResource) super.createResource(uri);
>>>
>>> Map defaultLoadOptions = resource.getDefaultLoadOptions();
>>> defaultLoadOptions.put(XMLResource.OPTION_EXTENDED_META_DATA ,
>>> getExtendedMetaData());
>>>
>>> defaultLoadOptions.put(XMLResource.OPTION_RECORD_UNKNOWN_FEA TURE,
>>> Boolean.TRUE);
>>> defaultLoadOptions.put(XMLResource.OPTION_RESOURCE_HANDLER,
>>> new RulesResourceHandler());
>>> return resource;
>>> }
>>>
>>> private ExtendedMetaData getExtendedMetaData() {
>>> if(extendedMetaData == null) {
>>> ResourceSet resourceSet = new ResourceSetImpl();
>>> EPackage.Registry ePackageRegistry =
>>> resourceSet.getPackageRegistry();
>>> ePackageRegistry.put(RULES_100_NS_URI,
>>> RulesPackage.eINSTANCE);
>>> ePackageRegistry.put(RULES_PLATFORM_URI,
>>> RulesPackage.eINSTANCE);
>>> resourceSet.setPackageRegistry(ePackageRegistry);
>>> Ecore2XMLRegistry ecore2xmlRegistry = new
>>> Ecore2XMLRegistryImpl(Ecore2XMLRegistry.INSTANCE);
>>> ecore2xmlRegistry.put(RULES_100_NS_URI,
>>> EcoreUtil.getObjectByType(
>>>
>>> resourceSet.getResource(URI.createURI(RULES_100_PLATFORM_URI ),
>>> true).getContents(),
>>>
>>> Ecore2XMLPackage.Literals.XML_MAP));
>>> extendedMetaData = new
>>> Ecore2XMLExtendedMetaData(EPackage.Registry.INSTANCE,
>>> ecore2xmlRegistry); }
>>> return extendedMetaData;
>>> }
>>> }
>>>
>>> If this passes muster, I can consider making a recipe of it. I found it
>>> distressingly hard to get this far by reading the various newsgroup
>>> articles and
>>> the EclipseCon 2006 slides.
>>>
>>> Thanks again,
>>> Mike


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Model Migration [message #416438 is a reply to message #416434] Thu, 31 January 2008 15:37 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: oemer.yildiz.semanticedge.com

Well, no matter what I do, nothing affects the serialization. I stepped
through the code a bit... Ecore2XMLExtendedMetaData is definitely used
to load/save the resource, but the output is always the old model version.

The code is essentially the same as written in the recipe:

"UiPackage" is the *old* model.
"UIModelPackage" is the *new* model.

"TARGET_ECORE" points to the *new* model's .ecore file
"MODEL_MAPPING" points to the .ecore2xml file

public Resource createResource(URI uri) {
...
defaultLoadOptions.put(XMLResource.OPTION_EXTENDED_META_DATA ,
getExtendedMetaData());
...
resource.getDefaultSaveOptions().put(
XMLResource.OPTION_EXTENDED_META_DATA, Boolean.TRUE);
}

private ExtendedMetaData getExtendedMetaData() {
...
rs.getPackageRegistry().put(UiPackage.eNS_URI,
UIModelPackage.eINSTANCE);
rs.getPackageRegistry().put(TARGET_ECORE,
UIModelPackage.eINSTANCE);

...
ecore2xmlRegistry.put(UiPackage.eNS_URI, EcoreUtil
.getObjectByType(rs.getResource(
URI.createURI(MODEL_MAPPING),true).getContents(),
Ecore2XMLPackage.Literals.XML_MAP));

...
}


Ed Merks schrieb:
> Ömer,
>
> It sounds to me like this is liable to do the reverse mapping. What
> happens if you don't do that? You can use this option mapped to
> Boolean.TRUE for save if you need the option to produce a
> schema-conforming serialization...
>
>
> Ömer Yildiz wrote:
>> I forgot to mention, that I've already set the same value for
>> OPTION_EXTENDED_META_DATA in the default save options.
>>
>>
>> Ömer Yildiz schrieb:
>>> Hello,
>>>
>>> i've tried to migrate an older version of my model using your recipe,
>>> but without success. After loading the instance of an old model, I
>>> immediately called resource.save(), assuming that EMF would serialize
>>> it using the newer version of my model.
>>>
>>> Any hints?
>>>
>>> Best regards,
>>> Ömer yildiz
>>>
>>> Mike Gering schrieb:
>>>> Ed,
>>>>
>>>> Thanks for prompt and excellent advice, as always!
>>>>
>>>> Here is a revised code fragment:
>>>>
>>>> public class RulesResourceFactoryImpl extends XMIResourceFactoryImpl {
>>>>
>>>> public static final String RULES_100_NS_URI =
>>>> "http://www.ibm.com/vce/1.0.0/rules";
>>>> public static final String RULES_PLATFORM_URI =
>>>> " platform:/plugin/com.ibm.adt.vce.rules.model/model/Rules.eco re ";
>>>> public static final String RULES_100_PLATFORM_URI =
>>>> " platform:/plugin/com.ibm.adt.vce.rules.model/model/Rules100_ 2_Rules.ecore2xml ";
>>>>
>>>>
>>>> private ExtendedMetaData extendedMetaData;
>>>> /* (non-Javadoc)
>>>> * @see
>>>> org.eclipse.emf.ecore.resource.impl.ResourceFactoryImpl#crea teResource(org.eclipse.emf.common.util.URI)
>>>>
>>>> */
>>>> public Resource createResource(URI uri) {
>>>> XMIResource resource = (XMIResource) super.createResource(uri);
>>>>
>>>> Map defaultLoadOptions = resource.getDefaultLoadOptions();
>>>> defaultLoadOptions.put(XMLResource.OPTION_EXTENDED_META_DATA ,
>>>> getExtendedMetaData());
>>>>
>>>> defaultLoadOptions.put(XMLResource.OPTION_RECORD_UNKNOWN_FEA TURE,
>>>> Boolean.TRUE);
>>>> defaultLoadOptions.put(XMLResource.OPTION_RESOURCE_HANDLER,
>>>> new RulesResourceHandler());
>>>> return resource;
>>>> }
>>>>
>>>> private ExtendedMetaData getExtendedMetaData() {
>>>> if(extendedMetaData == null) {
>>>> ResourceSet resourceSet = new ResourceSetImpl();
>>>> EPackage.Registry ePackageRegistry =
>>>> resourceSet.getPackageRegistry();
>>>> ePackageRegistry.put(RULES_100_NS_URI,
>>>> RulesPackage.eINSTANCE);
>>>> ePackageRegistry.put(RULES_PLATFORM_URI,
>>>> RulesPackage.eINSTANCE);
>>>> resourceSet.setPackageRegistry(ePackageRegistry);
>>>> Ecore2XMLRegistry ecore2xmlRegistry = new
>>>> Ecore2XMLRegistryImpl(Ecore2XMLRegistry.INSTANCE);
>>>> ecore2xmlRegistry.put(RULES_100_NS_URI,
>>>> EcoreUtil.getObjectByType(
>>>>
>>>> resourceSet.getResource(URI.createURI(RULES_100_PLATFORM_URI ),
>>>> true).getContents(),
>>>>
>>>> Ecore2XMLPackage.Literals.XML_MAP));
>>>> extendedMetaData = new
>>>> Ecore2XMLExtendedMetaData(EPackage.Registry.INSTANCE,
>>>> ecore2xmlRegistry); }
>>>> return extendedMetaData;
>>>> }
>>>> }
>>>>
>>>> If this passes muster, I can consider making a recipe of it. I found it
>>>> distressingly hard to get this far by reading the various newsgroup
>>>> articles and
>>>> the EclipseCon 2006 slides.
>>>>
>>>> Thanks again,
>>>> Mike
Re: Model Migration [message #416441 is a reply to message #416438] Thu, 31 January 2008 16:55 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33133
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------090508030706050300050604
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 8bit



Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Model Migration [message #416442 is a reply to message #416441] Thu, 31 January 2008 17:50 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: oemer.yildiz.semanticedge.com

Hi Ed

> Your scenario looks different. You appear to have both models as
> generated models (right?) whereas in the recipe only the final model is
> generated so for the old model in the recipe the dynamic model will be used.

Yes you're right. But now I've tried setting things up like in the
recipe. I've closed the project, that declares the generated_package
extension point for the old model, so there's no way any more mapping
the NS URI used in to-be-migrated files to the old model's package.
Thus, the generated model cannot be contacted and a dynamic model should
be generated with the assistance of the configured ExtendedMetaData
while deserializing, right?

The loading of an old-model-resource still works with this approach,
immediately saving after load does too, but the model is still
serialized in the old format.

I'm close to going crazy :)

> I think you need a mapping like this so that the physical serialized
> source .ecore to which your .ecore2xml refers is redirected to the
> actual generated static instance that's used to load your original
> source model:
>
> ePackageRegistry.put(SOURCE_ECORE, UiPackage.eINSTANCE);

I've tried this before going the dynamic-model way like described above,
but without luck.


>
> Ömer Yildiz wrote:
>> Well, no matter what I do, nothing affects the serialization. I
>> stepped through the code a bit... Ecore2XMLExtendedMetaData is
>> definitely used to load/save the resource, but the output is always
>> the old model version.
>>
>> The code is essentially the same as written in the recipe:
>>
>> "UiPackage" is the *old* model.
>> "UIModelPackage" is the *new* model.
>>
>> "TARGET_ECORE" points to the *new* model's .ecore file
>> "MODEL_MAPPING" points to the .ecore2xml file
>>
>> public Resource createResource(URI uri) {
>> ...
>> defaultLoadOptions.put(XMLResource.OPTION_EXTENDED_META_DATA ,
>> getExtendedMetaData());
>> ...
>> resource.getDefaultSaveOptions().put(
>> XMLResource.OPTION_EXTENDED_META_DATA, Boolean.TRUE);
>> }
>>
>> private ExtendedMetaData getExtendedMetaData() {
>> ...
>> rs.getPackageRegistry().put(UiPackage.eNS_URI,
>> UIModelPackage.eINSTANCE);
>> rs.getPackageRegistry().put(TARGET_ECORE,
>> UIModelPackage.eINSTANCE);
>>
>> ...
>> ecore2xmlRegistry.put(UiPackage.eNS_URI, EcoreUtil
>> .getObjectByType(rs.getResource(
>> URI.createURI(MODEL_MAPPING),true).getContents(),
>> Ecore2XMLPackage.Literals.XML_MAP));
>>
>> ...
>> }
>>
>>
>> Ed Merks schrieb:
>>> Ömer,
>>>
>>> It sounds to me like this is liable to do the reverse mapping. What
>>> happens if you don't do that? You can use this option mapped to
>>> Boolean.TRUE for save if you need the option to produce a
>>> schema-conforming serialization...
>>>
>>>
>>> Ömer Yildiz wrote:
>>>> I forgot to mention, that I've already set the same value for
>>>> OPTION_EXTENDED_META_DATA in the default save options.
>>>>
>>>>
>>>> Ömer Yildiz schrieb:
>>>>> Hello,
>>>>>
>>>>> i've tried to migrate an older version of my model using your
>>>>> recipe, but without success. After loading the instance of an old
>>>>> model, I immediately called resource.save(), assuming that EMF
>>>>> would serialize it using the newer version of my model.
>>>>>
>>>>> Any hints?
>>>>>
>>>>> Best regards,
>>>>> Ömer yildiz
>>>>>
>>>>> Mike Gering schrieb:
>>>>>> Ed,
>>>>>>
>>>>>> Thanks for prompt and excellent advice, as always!
>>>>>>
>>>>>> Here is a revised code fragment:
>>>>>>
>>>>>> public class RulesResourceFactoryImpl extends
>>>>>> XMIResourceFactoryImpl {
>>>>>>
>>>>>> public static final String RULES_100_NS_URI =
>>>>>> "http://www.ibm.com/vce/1.0.0/rules";
>>>>>> public static final String RULES_PLATFORM_URI =
>>>>>> " platform:/plugin/com.ibm.adt.vce.rules.model/model/Rules.eco re ";
>>>>>> public static final String RULES_100_PLATFORM_URI =
>>>>>> " platform:/plugin/com.ibm.adt.vce.rules.model/model/Rules100_ 2_Rules.ecore2xml ";
>>>>>>
>>>>>>
>>>>>> private ExtendedMetaData extendedMetaData;
>>>>>> /* (non-Javadoc)
>>>>>> * @see
>>>>>> org.eclipse.emf.ecore.resource.impl.ResourceFactoryImpl#crea teResource(org.eclipse.emf.common.util.URI)
>>>>>>
>>>>>> */
>>>>>> public Resource createResource(URI uri) {
>>>>>> XMIResource resource = (XMIResource)
>>>>>> super.createResource(uri);
>>>>>>
>>>>>> Map defaultLoadOptions = resource.getDefaultLoadOptions();
>>>>>> defaultLoadOptions.put(XMLResource.OPTION_EXTENDED_META_DATA ,
>>>>>> getExtendedMetaData());
>>>>>>
>>>>>> defaultLoadOptions.put(XMLResource.OPTION_RECORD_UNKNOWN_FEA TURE,
>>>>>> Boolean.TRUE);
>>>>>> defaultLoadOptions.put(XMLResource.OPTION_RESOURCE_HANDLER,
>>>>>> new RulesResourceHandler());
>>>>>> return resource;
>>>>>> }
>>>>>>
>>>>>> private ExtendedMetaData getExtendedMetaData() {
>>>>>> if(extendedMetaData == null) {
>>>>>> ResourceSet resourceSet = new ResourceSetImpl();
>>>>>> EPackage.Registry ePackageRegistry =
>>>>>> resourceSet.getPackageRegistry();
>>>>>> ePackageRegistry.put(RULES_100_NS_URI,
>>>>>> RulesPackage.eINSTANCE);
>>>>>> ePackageRegistry.put(RULES_PLATFORM_URI,
>>>>>> RulesPackage.eINSTANCE);
>>>>>> resourceSet.setPackageRegistry(ePackageRegistry);
>>>>>> Ecore2XMLRegistry ecore2xmlRegistry = new
>>>>>> Ecore2XMLRegistryImpl(Ecore2XMLRegistry.INSTANCE);
>>>>>> ecore2xmlRegistry.put(RULES_100_NS_URI,
>>>>>> EcoreUtil.getObjectByType(
>>>>>>
>>>>>> resourceSet.getResource(URI.createURI(RULES_100_PLATFORM_URI ),
>>>>>> true).getContents(),
>>>>>>
>>>>>> Ecore2XMLPackage.Literals.XML_MAP));
>>>>>> extendedMetaData = new
>>>>>> Ecore2XMLExtendedMetaData(EPackage.Registry.INSTANCE,
>>>>>> ecore2xmlRegistry); }
>>>>>> return extendedMetaData;
>>>>>> }
>>>>>> }
>>>>>>
>>>>>> If this passes muster, I can consider making a recipe of it. I
>>>>>> found it
>>>>>> distressingly hard to get this far by reading the various
>>>>>> newsgroup articles and
>>>>>> the EclipseCon 2006 slides.
>>>>>>
>>>>>> Thanks again,
>>>>>> Mike
>
Re: Model Migration [message #416443 is a reply to message #416442] Thu, 31 January 2008 18:05 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33133
Registered: July 2009
Senior Member
Ömer,

Comments below.


Ömer Yildiz wrote:
> Hi Ed
>
>> Your scenario looks different. You appear to have both models as
>> generated models (right?) whereas in the recipe only the final model
>> is generated so for the old model in the recipe the dynamic model
>> will be used.
>
> Yes you're right. But now I've tried setting things up like in the
> recipe. I've closed the project, that declares the generated_package
> extension point for the old model, so there's no way any more mapping
> the NS URI used in to-be-migrated files to the old model's package.
> Thus, the generated model cannot be contacted and a dynamic model
> should be generated with the assistance of the configured
> ExtendedMetaData while deserializing, right?
In theory. When you load the resource and do
resource.getContents().get(0).eClass() and print it out. Is it an
instance of your new generated model? Is it a dynamic instance?
>
> The loading of an old-model-resource still works with this approach,
> immediately saving after load does too, but the model is still
> serialized in the old format.
You've verified that the resource being used to do the loading is really
the one configured by your factory. (A breakpoint would help verify that.)
>
> I'm close to going crazy :)
>
>> I think you need a mapping like this so that the physical serialized
>> source .ecore to which your .ecore2xml refers is redirected to the
>> actual generated static instance that's used to load your original
>> source model:
>>
>> ePackageRegistry.put(SOURCE_ECORE, UiPackage.eINSTANCE);
>
> I've tried this before going the dynamic-model way like described
> above, but without luck.
A single typo could ruin the result. In the resource set where the
ecore2xml file is being loaded, use EcoreUtil.resolveAll on the resource
set then inspect all the resources in it as well as their URIs. This
will help confirm that a registered package is being used rather than a
loaded .ecore...
>
>
>>
>> Ömer Yildiz wrote:
>>> Well, no matter what I do, nothing affects the serialization. I
>>> stepped through the code a bit... Ecore2XMLExtendedMetaData is
>>> definitely used to load/save the resource, but the output is always
>>> the old model version.
>>>
>>> The code is essentially the same as written in the recipe:
>>>
>>> "UiPackage" is the *old* model.
>>> "UIModelPackage" is the *new* model.
>>>
>>> "TARGET_ECORE" points to the *new* model's .ecore file
>>> "MODEL_MAPPING" points to the .ecore2xml file
>>>
>>> public Resource createResource(URI uri) {
>>> ...
>>> defaultLoadOptions.put(XMLResource.OPTION_EXTENDED_META_DATA ,
>>> getExtendedMetaData());
>>> ...
>>> resource.getDefaultSaveOptions().put(
>>> XMLResource.OPTION_EXTENDED_META_DATA, Boolean.TRUE);
>>> }
>>>
>>> private ExtendedMetaData getExtendedMetaData() {
>>> ...
>>> rs.getPackageRegistry().put(UiPackage.eNS_URI,
>>> UIModelPackage.eINSTANCE);
>>> rs.getPackageRegistry().put(TARGET_ECORE,
>>> UIModelPackage.eINSTANCE);
>>>
>>> ...
>>> ecore2xmlRegistry.put(UiPackage.eNS_URI, EcoreUtil
>>> .getObjectByType(rs.getResource(
>>> URI.createURI(MODEL_MAPPING),true).getContents(),
>>> Ecore2XMLPackage.Literals.XML_MAP));
>>>
>>> ...
>>> }
>>>
>>>
>>> Ed Merks schrieb:
>>>> Ömer,
>>>>
>>>> It sounds to me like this is liable to do the reverse mapping.
>>>> What happens if you don't do that? You can use this option mapped
>>>> to Boolean.TRUE for save if you need the option to produce a
>>>> schema-conforming serialization...
>>>>
>>>>
>>>> Ömer Yildiz wrote:
>>>>> I forgot to mention, that I've already set the same value for
>>>>> OPTION_EXTENDED_META_DATA in the default save options.
>>>>>
>>>>>
>>>>> Ömer Yildiz schrieb:
>>>>>> Hello,
>>>>>>
>>>>>> i've tried to migrate an older version of my model using your
>>>>>> recipe, but without success. After loading the instance of an old
>>>>>> model, I immediately called resource.save(), assuming that EMF
>>>>>> would serialize it using the newer version of my model.
>>>>>>
>>>>>> Any hints?
>>>>>>
>>>>>> Best regards,
>>>>>> Ömer yildiz
>>>>>>
>>>>>> Mike Gering schrieb:
>>>>>>> Ed,
>>>>>>>
>>>>>>> Thanks for prompt and excellent advice, as always!
>>>>>>>
>>>>>>> Here is a revised code fragment:
>>>>>>>
>>>>>>> public class RulesResourceFactoryImpl extends
>>>>>>> XMIResourceFactoryImpl {
>>>>>>>
>>>>>>> public static final String RULES_100_NS_URI =
>>>>>>> "http://www.ibm.com/vce/1.0.0/rules";
>>>>>>> public static final String RULES_PLATFORM_URI =
>>>>>>> " platform:/plugin/com.ibm.adt.vce.rules.model/model/Rules.eco re ";
>>>>>>> public static final String RULES_100_PLATFORM_URI =
>>>>>>> " platform:/plugin/com.ibm.adt.vce.rules.model/model/Rules100_ 2_Rules.ecore2xml ";
>>>>>>>
>>>>>>>
>>>>>>> private ExtendedMetaData extendedMetaData;
>>>>>>> /* (non-Javadoc)
>>>>>>> * @see
>>>>>>> org.eclipse.emf.ecore.resource.impl.ResourceFactoryImpl#crea teResource(org.eclipse.emf.common.util.URI)
>>>>>>>
>>>>>>> */
>>>>>>> public Resource createResource(URI uri) {
>>>>>>> XMIResource resource = (XMIResource)
>>>>>>> super.createResource(uri);
>>>>>>>
>>>>>>> Map defaultLoadOptions = resource.getDefaultLoadOptions();
>>>>>>>
>>>>>>> defaultLoadOptions.put(XMLResource.OPTION_EXTENDED_META_DATA ,
>>>>>>> getExtendedMetaData());
>>>>>>>
>>>>>>> defaultLoadOptions.put(XMLResource.OPTION_RECORD_UNKNOWN_FEA TURE,
>>>>>>> Boolean.TRUE);
>>>>>>> defaultLoadOptions.put(XMLResource.OPTION_RESOURCE_HANDLER,
>>>>>>> new RulesResourceHandler());
>>>>>>> return resource;
>>>>>>> }
>>>>>>>
>>>>>>> private ExtendedMetaData getExtendedMetaData() {
>>>>>>> if(extendedMetaData == null) {
>>>>>>> ResourceSet resourceSet = new ResourceSetImpl();
>>>>>>> EPackage.Registry ePackageRegistry =
>>>>>>> resourceSet.getPackageRegistry();
>>>>>>> ePackageRegistry.put(RULES_100_NS_URI,
>>>>>>> RulesPackage.eINSTANCE);
>>>>>>> ePackageRegistry.put(RULES_PLATFORM_URI,
>>>>>>> RulesPackage.eINSTANCE);
>>>>>>> resourceSet.setPackageRegistry(ePackageRegistry);
>>>>>>> Ecore2XMLRegistry ecore2xmlRegistry = new
>>>>>>> Ecore2XMLRegistryImpl(Ecore2XMLRegistry.INSTANCE);
>>>>>>> ecore2xmlRegistry.put(RULES_100_NS_URI,
>>>>>>> EcoreUtil.getObjectByType(
>>>>>>>
>>>>>>> resourceSet.getResource(URI.createURI(RULES_100_PLATFORM_URI ),
>>>>>>> true).getContents(),
>>>>>>>
>>>>>>> Ecore2XMLPackage.Literals.XML_MAP));
>>>>>>> extendedMetaData = new
>>>>>>> Ecore2XMLExtendedMetaData(EPackage.Registry.INSTANCE,
>>>>>>> ecore2xmlRegistry); }
>>>>>>> return extendedMetaData;
>>>>>>> }
>>>>>>> }
>>>>>>>
>>>>>>> If this passes muster, I can consider making a recipe of it. I
>>>>>>> found it
>>>>>>> distressingly hard to get this far by reading the various
>>>>>>> newsgroup articles and
>>>>>>> the EclipseCon 2006 slides.
>>>>>>>
>>>>>>> Thanks again,
>>>>>>> Mike
>>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Model Migration [message #416450 is a reply to message #416443] Fri, 01 February 2008 11:21 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: oemer.yildiz.semanticedge.com

Hi Ed

>> Thus, the generated model cannot be contacted and a dynamic model
>> should be generated with the assistance of the configured
>> ExtendedMetaData while deserializing, right?
> In theory. When you load the resource and do
> resource.getContents().get(0).eClass() and print it out. Is it an
> instance of your new generated model? Is it a dynamic instance?

It is a dynamic instance (instanceof AnyType)

>> The loading of an old-model-resource still works with this approach,
>> immediately saving after load does too, but the model is still
>> serialized in the old format.
> You've verified that the resource being used to do the loading is really
> the one configured by your factory. (A breakpoint would help verify that.)

Yes, I'm currently using the factory directly.

When saving the resource, I expect, for example,
Ecore2XMLExtendedMetaData.getNamespace(EPackage) to be called when the
Resource is about to write the package's NS URI attribute. It actually
gets called with an instance of EPackageImpl (nsURI="<the old model
version uri>") as parameter. So I expect this method to look it up and
return the new model's NS URI, right?
But that does not happen.

Ecore2XMLExtendedMetaData.java:

public String getNamespace(EPackage ePackage)
{
XMLResource.XMLInfo xmlInfo = getInfo(ePackage);

if (xmlInfo != null)
{
return xmlInfo.getTargetNamespace();
}

return super.getNamespace(ePackage);
}

getInfo returns a null reference, leading the method to return the same
NS URI as passed in by the parameter.
Re: Model Migration [message #416451 is a reply to message #416450] Fri, 01 February 2008 11:38 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33133
Registered: July 2009
Senior Member
Ömer,

Comments below.

Ömer Yildiz wrote:
> Hi Ed
>
>>> Thus, the generated model cannot be contacted and a dynamic model
>>> should be generated with the assistance of the configured
>>> ExtendedMetaData while deserializing, right?
>> In theory. When you load the resource and do
>> resource.getContents().get(0).eClass() and print it out. Is it an
>> instance of your new generated model? Is it a dynamic instance?
>
> It is a dynamic instance (instanceof AnyType)
Hmmm. That doesn't sound good. This suggests to me that if you remove
XMLResource.OPTION_RECORD_UNKNOWN_FEATURE it you'll get an package/class
not found exception which suggests that the namespace in your instance
is not matching the models you are registering.
>
>>> The loading of an old-model-resource still works with this approach,
>>> immediately saving after load does too, but the model is still
>>> serialized in the old format.
>> You've verified that the resource being used to do the loading is
>> really the one configured by your factory. (A breakpoint would help
>> verify that.)
>
> Yes, I'm currently using the factory directly.
>
> When saving the resource, I expect, for example,
> Ecore2XMLExtendedMetaData.getNamespace(EPackage) to be called when the
> Resource is about to write the package's NS URI attribute. It actually
> gets called with an instance of EPackageImpl (nsURI="<the old model
> version uri>") as parameter. So I expect this method to look it up and
> return the new model's NS URI, right?
No, the mapping should be kicking in during loading and you should have
an instance of the new model that you can save as normal. As I
suggested above, I don't think the namespace in your input is actually
matching your old registered model. So look closely at the namespace in
the instance and the namespace against which you are registering the new
package in the package registry.
> But that does not happen.
>
> Ecore2XMLExtendedMetaData.java:
>
> public String getNamespace(EPackage ePackage)
> {
> XMLResource.XMLInfo xmlInfo = getInfo(ePackage);
>
> if (xmlInfo != null)
> {
> return xmlInfo.getTargetNamespace();
> }
>
> return super.getNamespace(ePackage);
> }
>
> getInfo returns a null reference, leading the method to return the
> same NS URI as passed in by the parameter.
Yep, it should have been mapped by now. You need to investigate why
during load the old package is not being found and not being mapped to
the new package. Eliminate XMLResource.OPTION_RECORD_UNKNOWN_FEATURE
for the time being so you don't mask that problem.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Model Migration [message #416452 is a reply to message #416451] Fri, 01 February 2008 12:00 Go to previous message
Eclipse UserFriend
Originally posted by: oemer.yildiz.semanticedge.com

Hello again

> Hmmm. That doesn't sound good. This suggests to me that if you remove
> XMLResource.OPTION_RECORD_UNKNOWN_FEATURE it you'll get an package/class
> not found exception which suggests that the namespace in your instance
> is not matching the models you are registering.

It threw an exception and I've also figured out the problem now. As
shown in the recipe (--> getExtendedMetaData()) I'm putting two entries
into the ResourceSet's package registry.
I wanted to find out why the "package not found" exception occurs, so I
set a breakpoint on EPackageRegistryImpl.getEPackage(..) and inspected
the map contents. None of the two entries were mapped...

I've now modified the getExtendedMetaData() method to put the entries
into EPackage.Registry.INSTANCE and now everything works!

Thank you very much for your help!

Best regards,
Ömer Yildiz
Previous Topic:Adding elements to model
Next Topic:Serialization: additional date formats
Goto Forum:
  


Current Time: Tue Apr 16 22:30:57 GMT 2024

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

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

Back to the top