Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Model loading - Casting
Model loading - Casting [message #431166] Wed, 01 July 2009 09:27 Go to next message
Patrick is currently offline PatrickFriend
Messages: 6
Registered: July 2009
Junior Member
Hi!

I have tried to find the answer to this typical question using google, the
newsgroup and the book, but could not find a solution. Maybe I am
searching with the wrong keywords. Therefore, I would be glad to get a
little hint ;)

PRBOLEM:

I have loaded a model resource form an xmi file. This works as always. For
one xmi with another metamodel everything works well. But changing the
metamodel and creating a new xmi file brings up the problem that I cannot
cast the contents to any object (ClassCastException).

For example I can read the name of the model content "Net" which was the
Class called Net before. The casting does not work.
Or trying to cast the EPackage to the own Package of MxExamplePackage does
not work (ClassCastExcecption).

So, there is the question, do I have to register the some XMI Elements
(uri, ..) with classes and how can I do that? (If there exists already a
tutorial, example or so this would be the best for me)

All pieces of information were imported. The metamodel is external, the
genmodel ist external (prev. version?). Only the XMI is self created by
my application..

<?xml version="1.0" encoding="ASCII"?>

<xmi:XMI xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:MyExample="MyExample"
xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore">

<ecore:EPackage name="MyExample" nsURI="MyExample" nsPrefix="MyExample">
<eClassifiers xsi:type="ecore:EClass" name="Net">
...

(I have thought about the plugin.xml, but I could make it run.. )

I am using Eclipse 3.4 with EMF 2.4

Would be glad for any hint..

Thanks a lot!

br,
Patrick
Re: Model loading - Casting [message #431168 is a reply to message #431166] Wed, 01 July 2009 10:04 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 30692
Registered: July 2009
Senior Member
Patrick,

Comments below.

Patrick wrote:
> Hi!
>
> I have tried to find the answer to this typical question using google,
> the newsgroup and the book, but could not find a solution. Maybe I am
> searching with the wrong keywords. Therefore, I would be glad to get a
> little hint ;)
>
> PRBOLEM:
>
> I have loaded a model resource form an xmi file. This works as always.
> For one xmi with another metamodel everything works well. But changing
> the metamodel and creating a new xmi file brings up the problem that I
> cannot cast the contents to any object (ClassCastException).
Changing the metamodel? Not sure what that means.
> For example I can read the name of the model content "Net" which was
> the Class called Net before.
Before what?
> The casting does not work.
What casting?
> Or trying to cast the EPackage to the own Package of MxExamplePackage
> does not work (ClassCastExcecption).
I can't imagine why you'd ever need or want to cast down to a specific
generated package. It could only the same instance as
MxExamplePackage.eINSTANCE...
>
> So, there is the question, do I have to register the some XMI Elements
> (uri, ..) with classes and how can I do that? (If there exists already
> a tutorial, example or so this would be the best for me)
For the root element of an XMI document the combination of namespace,
Foo, and element name, Bar, effectively uses
(EClass)EPackage.Registry.INSTANCE.getEPackage("Foo").getEClassifier( "Bar")
to locate the class.
>
> All pieces of information were imported. The metamodel is external,
> the genmodel ist external (prev. version?). Only the XMI is self
> created by my application..
> <?xml version="1.0" encoding="ASCII"?>
>
> <xmi:XMI xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xmlns:MyExample="MyExample"
> xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore">
>
> <ecore:EPackage name="MyExample" nsURI="MyExample" nsPrefix="MyExample">
> <eClassifiers xsi:type="ecore:EClass" name="Net">
> ..
>
> (I have thought about the plugin.xml, but I could make it run.. )
>
> I am using Eclipse 3.4 with EMF 2.4
>
> Would be glad for any hint..
Unfortunately I don't actually understand your question. :-(
>
> Thanks a lot!
>
> br,
> Patrick
>
Re: Model loading - Casting [message #431170 is a reply to message #431168] Wed, 01 July 2009 10:36 Go to previous messageGo to next message
Patrick is currently offline PatrickFriend
Messages: 6
Registered: July 2009
Junior Member
Hi!

Thx for your answer. I will try to clarify my issue.

I have used a tool that uses a certain metamodel and produces a certain
model instance (xmi) based on this metamodel. I can write out and read in
the model elements (xmi) and cast them to their original Classes.

My problem, however, is:
I have added another metamodel to my project and would like to do the same
with a model conforming to it.
-> Casting does not work..

->Rest of the comments below.

Ed Merks wrote:

> Patrick,

> Comments below.

> Patrick wrote:
>> Hi!
>>
>> I have tried to find the answer to this typical question using google,
>> the newsgroup and the book, but could not find a solution. Maybe I am
>> searching with the wrong keywords. Therefore, I would be glad to get a
>> little hint ;)
>>
>> PRBOLEM:
>>
>> I have loaded a model resource form an xmi file. This works as always.
>> For one xmi with another metamodel everything works well. But changing
>> the metamodel and creating a new xmi file brings up the problem that I
>> cannot cast the contents to any object (ClassCastException).
> Changing the metamodel? Not sure what that means.
>> For example I can read the name of the model content "Net" which was
>> the Class called Net before.
> Before what?

I have a class called Net (or another). I write this out as Root Object to
an xmi file. Then, I want to read it in as Resource and cast this certain
content (of the Net) to this original Net Class.

However, I can only read the contents in and see their names etc., but the
casting does not work for this particular case. (I have only added a
metamodel, an xmi file and some code classes for reading, writing, .. but
nothing else)

>> The casting does not work.
> What casting?
>> Or trying to cast the EPackage to the own Package of MxExamplePackage
>> does not work (ClassCastExcecption).
> I can't imagine why you'd ever need or want to cast down to a specific
> generated package. It could only the same instance as
> MxExamplePackage.eINSTANCE...

Well, you are right. I am only interested in the root object and not in
the Package. I have only tried that out, if can somehow come from this
"side". (sry for this)

>>
>> So, there is the question, do I have to register the some XMI Elements
>> (uri, ..) with classes and how can I do that? (If there exists already
>> a tutorial, example or so this would be the best for me)
> For the root element of an XMI document the combination of namespace,
> Foo, and element name, Bar, effectively uses
> (EClass)EPackage.Registry.INSTANCE.getEPackage("Foo").getEClassifier( "Bar")
> to locate the class.

This gives me back an EClass, but how can I get the 'real' class from it.
Is that possible? But your tip is very great, because I have searched
through the contents manually ;) .. (I am new to those EMF features. I
have more experience with the modeling itself .. So I am sry for newbie
questions)

>>
>> All pieces of information were imported. The metamodel is external,
>> the genmodel ist external (prev. version?). Only the XMI is self
>> created by my application..
>> <?xml version="1.0" encoding="ASCII"?>
>>
>> <xmi:XMI xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI"
>> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>> xmlns:MyExample="MyExample"
>> xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore">
>>
>> <ecore:EPackage name="MyExample" nsURI="MyExample" nsPrefix="MyExample">
>> <eClassifiers xsi:type="ecore:EClass" name="Net">
>> ..
>>
>> (I have thought about the plugin.xml, but I could make it run.. )
>>
>> I am using Eclipse 3.4 with EMF 2.4
>>
>> Would be glad for any hint..
> Unfortunately I don't actually understand your question. :-(

Thank you for your extremely fast answer. I hope it could make my issue
more understandable with this posting.

And thanks for your help at all!

>>
>> Thanks a lot!
>>
>> br,
>> Patrick
>>
Re: Model loading - Casting [message #431171 is a reply to message #431170] Wed, 01 July 2009 10:55 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 30692
Registered: July 2009
Senior Member
Patrick,

Comments below.

Patrick wrote:
> Hi!
>
> Thx for your answer. I will try to clarify my issue.
>
> I have used a tool that uses a certain metamodel and produces a
> certain model instance (xmi) based on this metamodel. I can write out
> and read in the model elements (xmi) and cast them to their original
> Classes.
>
> My problem, however, is:
> I have added another metamodel to my project and would like to do the
> same with a model conforming to it.
And the two are unrelated? You added them to the same exact project
folder? Keep in mind that plugin.xml doesn't regenerate so the
registration of the model's nsURI might well be missing.
> -> Casting does not work..
> ->Rest of the comments below.
>
> Ed Merks wrote:
>
>> Patrick,
>
>> Comments below.
>
>> Patrick wrote:
>>> Hi!
>>>
>>> I have tried to find the answer to this typical question using
>>> google, the newsgroup and the book, but could not find a solution.
>>> Maybe I am searching with the wrong keywords. Therefore, I would be
>>> glad to get a little hint ;)
>>>
>>> PRBOLEM:
>>>
>>> I have loaded a model resource form an xmi file. This works as
>>> always. For one xmi with another metamodel everything works well.
>>> But changing the metamodel and creating a new xmi file brings up the
>>> problem that I cannot cast the contents to any object
>>> (ClassCastException).
>> Changing the metamodel? Not sure what that means.
>>> For example I can read the name of the model content "Net" which was
>>> the Class called Net before.
>> Before what?
>
> I have a class called Net (or another). I write this out as Root
> Object to an xmi file. Then, I want to read it in as Resource and cast
> this certain content (of the Net) to this original Net Class.
So what steps have you taken so that it's actually handled as an
instance of this other model rather than simply an instance of the model
for which it was serialized?
> However, I can only read the contents in and see their names etc., but
> the casting does not work for this particular case. (I have only added
> a metamodel, an xmi file and some code classes for reading, writing,
> .. but nothing else)
Of course it will always be interpreted as an instance of the original
model unless you take steps make it behave otherwise...
>
>>> The casting does not work.
>> What casting?
>>> Or trying to cast the EPackage to the own Package of
>>> MxExamplePackage does not work (ClassCastExcecption).
>> I can't imagine why you'd ever need or want to cast down to a
>> specific generated package. It could only the same instance as
>> MxExamplePackage.eINSTANCE...
>
> Well, you are right. I am only interested in the root object and not
> in the Package. I have only tried that out, if can somehow come from
> this "side". (sry for this)
>
>>>
>>> So, there is the question, do I have to register the some XMI
>>> Elements (uri, ..) with classes and how can I do that? (If there
>>> exists already a tutorial, example or so this would be the best for me)
>> For the root element of an XMI document the combination of namespace,
>> Foo, and element name, Bar, effectively uses
>> (EClass)EPackage.Registry.INSTANCE.getEPackage("Foo").getEClassifier( "Bar")
>> to locate the class.
>
> This gives me back an EClass, but how can I get the 'real' class from
> it. Is that possible?
EClass.getInstanceClass()
> But your tip is very great, because I have searched through the
> contents manually ;) .. (I am new to those EMF features. I have more
> experience with the modeling itself .. So I am sry for newbie questions)
>
>>>
>>> All pieces of information were imported. The metamodel is external,
>>> the genmodel ist external (prev. version?). Only the XMI is self
>>> created by my application..
>>> <?xml version="1.0" encoding="ASCII"?>
>>>
>>> <xmi:XMI xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI"
>>> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>>> xmlns:MyExample="MyExample"
>>> xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore">
>>>
>>> <ecore:EPackage name="MyExample" nsURI="MyExample"
>>> nsPrefix="MyExample">
>>> <eClassifiers xsi:type="ecore:EClass" name="Net">
>>> ..
>>>
>>> (I have thought about the plugin.xml, but I could make it run.. )
>>>
>>> I am using Eclipse 3.4 with EMF 2.4
>>>
>>> Would be glad for any hint..
>> Unfortunately I don't actually understand your question. :-(
>
> Thank you for your extremely fast answer. I hope it could make my
> issue more understandable with this posting.
>
> And thanks for your help at all!
One approach I can suggest, if I understand your question properly, is
to use OPTION_EXTENDED_META_DATA during loading, and providing a
specialized implementation of BasicExtendedMetaData which specializes
methods so that something like getEPackage("oldNSURI") actuall returns
the EPackage for the new target model. In this way, you can map the
namespaces and names in the instance document onto a model different
from what you'd otherwise get...
>>>
>>> Thanks a lot!
>>>
>>> br,
>>> Patrick
>>>
>
Re: Model loading - Casting [message #431172 is a reply to message #431171] Wed, 01 July 2009 11:13 Go to previous messageGo to next message
Patrick is currently offline PatrickFriend
Messages: 6
Registered: July 2009
Junior Member
Hi Ed,

Thank you again.

Comments below as well.

Ed Merks wrote:

> Patrick,

> Comments below.

> Patrick wrote:
>> Hi!
>>
>> Thx for your answer. I will try to clarify my issue.
>>
>> I have used a tool that uses a certain metamodel and produces a
>> certain model instance (xmi) based on this metamodel. I can write out
>> and read in the model elements (xmi) and cast them to their original
>> Classes.
>>
>> My problem, however, is:
>> I have added another metamodel to my project and would like to do the
>> same with a model conforming to it.
> And the two are unrelated? You added them to the same exact project
> folder? Keep in mind that plugin.xml doesn't regenerate so the
> registration of the model's nsURI might well be missing.

That seems to be exactly my problem. Is there a possibility to generate
these plugin.xml lines?

Because what I have tried seems to be problematic. For me it would be nice
only to map the Net - a certain content - class or do I have to map the
Package?

I have tried something like:

<extension point="org.eclipse.emf.ecore.generated_package">

<package

uri = "MyExample"

class = "MyExample.MyExamplePackage"

genModel = "src/model/MyExample.genmodel" />

</extension>

or

<extension point="org.eclipse.emf.ecore.generated_package">

<package

uri = "MyExample" //or MyExample.Net or MyExample:Net

class = "MyExample.Net"

genModel = "src/model/MyExample.genmodel" />

</extension>

However, the URI seems to be wrong or so .. (or the genmodel ist not valid
anymore .. )

If you have any idea, I would be very glad. Otherwise I will continue to
test it out.. Or maybe I will try to import it to another new EMF project
and hopefully see the correct values ;)

Thanks a lot.. ;)

>> -> Casting does not work..
>> ->Rest of the comments below.
>>
>> Ed Merks wrote:
>>
>>> Patrick,
>>
>>> Comments below.
>>
>>> Patrick wrote:
>>>> Hi!
>>>>
>>>> I have tried to find the answer to this typical question using
>>>> google, the newsgroup and the book, but could not find a solution.
>>>> Maybe I am searching with the wrong keywords. Therefore, I would be
>>>> glad to get a little hint ;)
>>>>
>>>> PRBOLEM:
>>>>
>>>> I have loaded a model resource form an xmi file. This works as
>>>> always. For one xmi with another metamodel everything works well.
>>>> But changing the metamodel and creating a new xmi file brings up the
>>>> problem that I cannot cast the contents to any object
>>>> (ClassCastException).
>>> Changing the metamodel? Not sure what that means.
>>>> For example I can read the name of the model content "Net" which was
>>>> the Class called Net before.
>>> Before what?
>>
>> I have a class called Net (or another). I write this out as Root
>> Object to an xmi file. Then, I want to read it in as Resource and cast
>> this certain content (of the Net) to this original Net Class.
> So what steps have you taken so that it's actually handled as an
> instance of this other model rather than simply an instance of the model
> for which it was serialized?
>> However, I can only read the contents in and see their names etc., but
>> the casting does not work for this particular case. (I have only added
>> a metamodel, an xmi file and some code classes for reading, writing,
>> .. but nothing else)
> Of course it will always be interpreted as an instance of the original
> model unless you take steps make it behave otherwise...
>>
>>>> The casting does not work.
>>> What casting?
>>>> Or trying to cast the EPackage to the own Package of
>>>> MxExamplePackage does not work (ClassCastExcecption).
>>> I can't imagine why you'd ever need or want to cast down to a
>>> specific generated package. It could only the same instance as
>>> MxExamplePackage.eINSTANCE...
>>
>> Well, you are right. I am only interested in the root object and not
>> in the Package. I have only tried that out, if can somehow come from
>> this "side". (sry for this)
>>
>>>>
>>>> So, there is the question, do I have to register the some XMI
>>>> Elements (uri, ..) with classes and how can I do that? (If there
>>>> exists already a tutorial, example or so this would be the best for me)
>>> For the root element of an XMI document the combination of namespace,
>>> Foo, and element name, Bar, effectively uses
>>>
(EClass)EPackage.Registry.INSTANCE.getEPackage("Foo").getEClassifier( "Bar")
>>> to locate the class.
>>
>> This gives me back an EClass, but how can I get the 'real' class from
>> it. Is that possible?
> EClass.getInstanceClass()

And if that does not work, I can probably expect my plugin.xml to be
wrong..

>> But your tip is very great, because I have searched through the
>> contents manually ;) .. (I am new to those EMF features. I have more
>> experience with the modeling itself .. So I am sry for newbie questions)
>>
>>>>
>>>> All pieces of information were imported. The metamodel is external,
>>>> the genmodel ist external (prev. version?). Only the XMI is self
>>>> created by my application..
>>>> <?xml version="1.0" encoding="ASCII"?>
>>>>
>>>> <xmi:XMI xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI"
>>>> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>>>> xmlns:MyExample="MyExample"
>>>> xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore">
>>>>
>>>> <ecore:EPackage name="MyExample" nsURI="MyExample"
>>>> nsPrefix="MyExample">
>>>> <eClassifiers xsi:type="ecore:EClass" name="Net">
>>>> ..
>>>>
>>>> (I have thought about the plugin.xml, but I could make it run.. )
>>>>
>>>> I am using Eclipse 3.4 with EMF 2.4
>>>>
>>>> Would be glad for any hint..
>>> Unfortunately I don't actually understand your question. :-(
>>
>> Thank you for your extremely fast answer. I hope it could make my
>> issue more understandable with this posting.
>>
>> And thanks for your help at all!
> One approach I can suggest, if I understand your question properly, is
> to use OPTION_EXTENDED_META_DATA during loading, and providing a
> specialized implementation of BasicExtendedMetaData which specializes
> methods so that something like getEPackage("oldNSURI") actuall returns
> the EPackage for the new target model. In this way, you can map the
> namespaces and names in the instance document onto a model different
> from what you'd otherwise get...

I do fully understand that, but I will look it up and try it out.
Thank you!

>>>>
>>>> Thanks a lot!
>>>>
>>>> br,
>>>> Patrick
>>>>
>>
Re: Model loading - Casting [message #431173 is a reply to message #431172] Wed, 01 July 2009 11:25 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 30692
Registered: July 2009
Senior Member
Patrick,

Comments below.

Patrick wrote:
> Hi Ed,
> Thank you again.
>
> Comments below as well.
>
> Ed Merks wrote:
>
>> Patrick,
>
>> Comments below.
>
>> Patrick wrote:
>>> Hi!
>>>
>>> Thx for your answer. I will try to clarify my issue.
>>>
>>> I have used a tool that uses a certain metamodel and produces a
>>> certain model instance (xmi) based on this metamodel. I can write
>>> out and read in the model elements (xmi) and cast them to their
>>> original Classes.
>>>
>>> My problem, however, is:
>>> I have added another metamodel to my project and would like to do
>>> the same with a model conforming to it.
>> And the two are unrelated? You added them to the same exact project
>> folder? Keep in mind that plugin.xml doesn't regenerate so the
>> registration of the model's nsURI might well be missing.
>
> That seems to be exactly my problem. Is there a possibility to
> generate these plugin.xml lines?
Delete the plugin.xml and regenerate it.
>
> Because what I have tried seems to be problematic. For me it would be
> nice only to map the Net - a certain content - class or do I have to
> map the Package?
>
> I have tried something like:
>
> <extension point="org.eclipse.emf.ecore.generated_package">
>
> <package
>
> uri = "MyExample"
>
> class = "MyExample.MyExamplePackage"
>
> genModel = "src/model/MyExample.genmodel" />
>
> </extension>
>
> or
>
> <extension point="org.eclipse.emf.ecore.generated_package">
>
> <package
>
> uri = "MyExample" //or MyExample.Net or MyExample:Net
>
> class = "MyExample.Net"
>
> genModel = "src/model/MyExample.genmodel" />
>
> </extension>
>
> However, the URI seems to be wrong or so .. (or the genmodel ist not
> valid anymore .. )
>
> If you have any idea, I would be very glad. Otherwise I will continue
> to test it out.. Or maybe I will try to import it to another new EMF
> project and hopefully see the correct values ;)
>
> Thanks a lot.. ;)
>
>>> -> Casting does not work..
>>> ->Rest of the comments below.
>>>
>>> Ed Merks wrote:
>>>
>>>> Patrick,
>>>
>>>> Comments below.
>>>
>>>> Patrick wrote:
>>>>> Hi!
>>>>>
>>>>> I have tried to find the answer to this typical question using
>>>>> google, the newsgroup and the book, but could not find a solution.
>>>>> Maybe I am searching with the wrong keywords. Therefore, I would
>>>>> be glad to get a little hint ;)
>>>>>
>>>>> PRBOLEM:
>>>>>
>>>>> I have loaded a model resource form an xmi file. This works as
>>>>> always. For one xmi with another metamodel everything works well.
>>>>> But changing the metamodel and creating a new xmi file brings up
>>>>> the problem that I cannot cast the contents to any object
>>>>> (ClassCastException).
>>>> Changing the metamodel? Not sure what that means.
>>>>> For example I can read the name of the model content "Net" which
>>>>> was the Class called Net before.
>>>> Before what?
>>>
>>> I have a class called Net (or another). I write this out as Root
>>> Object to an xmi file. Then, I want to read it in as Resource and
>>> cast this certain content (of the Net) to this original Net Class.
>> So what steps have you taken so that it's actually handled as an
>> instance of this other model rather than simply an instance of the
>> model for which it was serialized?
>>> However, I can only read the contents in and see their names etc.,
>>> but the casting does not work for this particular case. (I have only
>>> added a metamodel, an xmi file and some code classes for reading,
>>> writing, .. but nothing else)
>> Of course it will always be interpreted as an instance of the
>> original model unless you take steps make it behave otherwise...
>>>
>>>>> The casting does not work.
>>>> What casting?
>>>>> Or trying to cast the EPackage to the own Package of
>>>>> MxExamplePackage does not work (ClassCastExcecption).
>>>> I can't imagine why you'd ever need or want to cast down to a
>>>> specific generated package. It could only the same instance as
>>>> MxExamplePackage.eINSTANCE...
>>>
>>> Well, you are right. I am only interested in the root object and not
>>> in the Package. I have only tried that out, if can somehow come from
>>> this "side". (sry for this)
>>>
>>>>>
>>>>> So, there is the question, do I have to register the some XMI
>>>>> Elements (uri, ..) with classes and how can I do that? (If there
>>>>> exists already a tutorial, example or so this would be the best
>>>>> for me)
>>>> For the root element of an XMI document the combination of
>>>> namespace, Foo, and element name, Bar, effectively uses
> (EClass)EPackage.Registry.INSTANCE.getEPackage("Foo").getEClassifier( "Bar")
>
>>>> to locate the class.
>>>
>>> This gives me back an EClass, but how can I get the 'real' class
>>> from it. Is that possible?
>> EClass.getInstanceClass()
>
> And if that does not work, I can probably expect my plugin.xml to be
> wrong..
>>> But your tip is very great, because I have searched through the
>>> contents manually ;) .. (I am new to those EMF features. I have more
>>> experience with the modeling itself .. So I am sry for newbie
>>> questions)
>>>
>>>>>
>>>>> All pieces of information were imported. The metamodel is
>>>>> external, the genmodel ist external (prev. version?). Only the XMI
>>>>> is self created by my application..
>>>>> <?xml version="1.0" encoding="ASCII"?>
>>>>>
>>>>> <xmi:XMI xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI"
>>>>> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>>>>> xmlns:MyExample="MyExample"
>>>>> xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore">
>>>>>
>>>>> <ecore:EPackage name="MyExample" nsURI="MyExample"
>>>>> nsPrefix="MyExample">
>>>>> <eClassifiers xsi:type="ecore:EClass" name="Net">
>>>>> ..
>>>>>
>>>>> (I have thought about the plugin.xml, but I could make it run.. )
>>>>>
>>>>> I am using Eclipse 3.4 with EMF 2.4
>>>>>
>>>>> Would be glad for any hint..
>>>> Unfortunately I don't actually understand your question. :-(
>>>
>>> Thank you for your extremely fast answer. I hope it could make my
>>> issue more understandable with this posting.
>>>
>>> And thanks for your help at all!
>> One approach I can suggest, if I understand your question properly,
>> is to use OPTION_EXTENDED_META_DATA during loading, and providing a
>> specialized implementation of BasicExtendedMetaData which specializes
>> methods so that something like getEPackage("oldNSURI") actuall
>> returns the EPackage for the new target model. In this way, you can
>> map the namespaces and names in the instance document onto a model
>> different from what you'd otherwise get...
>
> I do fully understand that, but I will look it up and try it out.
> Thank you!
>
>>>>>
>>>>> Thanks a lot!
>>>>>
>>>>> br,
>>>>> Patrick
>>>>>
>>>
>
Re: Model loading - Casting [message #431174 is a reply to message #431173] Wed, 01 July 2009 12:15 Go to previous message
Patrick is currently offline PatrickFriend
Messages: 6
Registered: July 2009
Junior Member
Thank you Ed for your kind advice!

Regenerating does it.. It is very simple and I just have not thought about
this. I have always tried to do it by hand.

kind regards,

Patrick

Ed Merks wrote:

> Patrick,

> Comments below.

> Patrick wrote:
>> Hi Ed,
>> Thank you again.
>>
>> Comments below as well.
>>
>> Ed Merks wrote:
>>
>>> Patrick,
>>
>>> Comments below.
>>
>>> Patrick wrote:
>>>> Hi!
>>>>
>>>> Thx for your answer. I will try to clarify my issue.
>>>>
>>>> I have used a tool that uses a certain metamodel and produces a
>>>> certain model instance (xmi) based on this metamodel. I can write
>>>> out and read in the model elements (xmi) and cast them to their
>>>> original Classes.
>>>>
>>>> My problem, however, is:
>>>> I have added another metamodel to my project and would like to do
>>>> the same with a model conforming to it.
>>> And the two are unrelated? You added them to the same exact project
>>> folder? Keep in mind that plugin.xml doesn't regenerate so the
>>> registration of the model's nsURI might well be missing.
>>
>> That seems to be exactly my problem. Is there a possibility to
>> generate these plugin.xml lines?
> Delete the plugin.xml and regenerate it.
>>
>> Because what I have tried seems to be problematic. For me it would be
>> nice only to map the Net - a certain content - class or do I have to
>> map the Package?
>>
>> I have tried something like:
>>
>> <extension point="org.eclipse.emf.ecore.generated_package">
>>
>> <package
>>
>> uri = "MyExample"
>>
>> class = "MyExample.MyExamplePackage"
>>
>> genModel = "src/model/MyExample.genmodel" />
>>
>> </extension>
>>
>> or
>>
>> <extension point="org.eclipse.emf.ecore.generated_package">
>>
>> <package
>>
>> uri = "MyExample" //or MyExample.Net or MyExample:Net
>>
>> class = "MyExample.Net"
>>
>> genModel = "src/model/MyExample.genmodel" />
>>
>> </extension>
>>
>> However, the URI seems to be wrong or so .. (or the genmodel ist not
>> valid anymore .. )
>>
>> If you have any idea, I would be very glad. Otherwise I will continue
>> to test it out.. Or maybe I will try to import it to another new EMF
>> project and hopefully see the correct values ;)
>>
>> Thanks a lot.. ;)
>>
>>>> -> Casting does not work..
>>>> ->Rest of the comments below.
>>>>
>>>> Ed Merks wrote:
>>>>
>>>>> Patrick,
>>>>
>>>>> Comments below.
>>>>
>>>>> Patrick wrote:
>>>>>> Hi!
>>>>>>
>>>>>> I have tried to find the answer to this typical question using
>>>>>> google, the newsgroup and the book, but could not find a solution.
>>>>>> Maybe I am searching with the wrong keywords. Therefore, I would
>>>>>> be glad to get a little hint ;)
>>>>>>
>>>>>> PRBOLEM:
>>>>>>
>>>>>> I have loaded a model resource form an xmi file. This works as
>>>>>> always. For one xmi with another metamodel everything works well.
>>>>>> But changing the metamodel and creating a new xmi file brings up
>>>>>> the problem that I cannot cast the contents to any object
>>>>>> (ClassCastException).
>>>>> Changing the metamodel? Not sure what that means.
>>>>>> For example I can read the name of the model content "Net" which
>>>>>> was the Class called Net before.
>>>>> Before what?
>>>>
>>>> I have a class called Net (or another). I write this out as Root
>>>> Object to an xmi file. Then, I want to read it in as Resource and
>>>> cast this certain content (of the Net) to this original Net Class.
>>> So what steps have you taken so that it's actually handled as an
>>> instance of this other model rather than simply an instance of the
>>> model for which it was serialized?
>>>> However, I can only read the contents in and see their names etc.,
>>>> but the casting does not work for this particular case. (I have only
>>>> added a metamodel, an xmi file and some code classes for reading,
>>>> writing, .. but nothing else)
>>> Of course it will always be interpreted as an instance of the
>>> original model unless you take steps make it behave otherwise...
>>>>
>>>>>> The casting does not work.
>>>>> What casting?
>>>>>> Or trying to cast the EPackage to the own Package of
>>>>>> MxExamplePackage does not work (ClassCastExcecption).
>>>>> I can't imagine why you'd ever need or want to cast down to a
>>>>> specific generated package. It could only the same instance as
>>>>> MxExamplePackage.eINSTANCE...
>>>>
>>>> Well, you are right. I am only interested in the root object and not
>>>> in the Package. I have only tried that out, if can somehow come from
>>>> this "side". (sry for this)
>>>>
>>>>>>
>>>>>> So, there is the question, do I have to register the some XMI
>>>>>> Elements (uri, ..) with classes and how can I do that? (If there
>>>>>> exists already a tutorial, example or so this would be the best
>>>>>> for me)
>>>>> For the root element of an XMI document the combination of
>>>>> namespace, Foo, and element name, Bar, effectively uses
>> (EClass)EPackage.Registry.INSTANCE.getEPackage("Foo").getEClassifier( "Bar")
>>
>>>>> to locate the class.
>>>>
>>>> This gives me back an EClass, but how can I get the 'real' class
>>>> from it. Is that possible?
>>> EClass.getInstanceClass()
>>
>> And if that does not work, I can probably expect my plugin.xml to be
>> wrong..
>>>> But your tip is very great, because I have searched through the
>>>> contents manually ;) .. (I am new to those EMF features. I have more
>>>> experience with the modeling itself .. So I am sry for newbie
>>>> questions)
>>>>
>>>>>>
>>>>>> All pieces of information were imported. The metamodel is
>>>>>> external, the genmodel ist external (prev. version?). Only the XMI
>>>>>> is self created by my application..
>>>>>> <?xml version="1.0" encoding="ASCII"?>
>>>>>>
>>>>>> <xmi:XMI xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI"
>>>>>> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>>>>>> xmlns:MyExample="MyExample"
>>>>>> xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore">
>>>>>>
>>>>>> <ecore:EPackage name="MyExample" nsURI="MyExample"
>>>>>> nsPrefix="MyExample">
>>>>>> <eClassifiers xsi:type="ecore:EClass" name="Net">
>>>>>> ..
>>>>>>
>>>>>> (I have thought about the plugin.xml, but I could make it run.. )
>>>>>>
>>>>>> I am using Eclipse 3.4 with EMF 2.4
>>>>>>
>>>>>> Would be glad for any hint..
>>>>> Unfortunately I don't actually understand your question. :-(
>>>>
>>>> Thank you for your extremely fast answer. I hope it could make my
>>>> issue more understandable with this posting.
>>>>
>>>> And thanks for your help at all!
>>> One approach I can suggest, if I understand your question properly,
>>> is to use OPTION_EXTENDED_META_DATA during loading, and providing a
>>> specialized implementation of BasicExtendedMetaData which specializes
>>> methods so that something like getEPackage("oldNSURI") actuall
>>> returns the EPackage for the new target model. In this way, you can
>>> map the namespaces and names in the instance document onto a model
>>> different from what you'd otherwise get...
>>
>> I do fully understand that, but I will look it up and try it out.
>> Thank you!
>>
>>>>>>
>>>>>> Thanks a lot!
>>>>>>
>>>>>> br,
>>>>>> Patrick
>>>>>>
>>>>
>>
Previous Topic:Copying EMF Mixed CData
Next Topic:Use ECore to model XSD and IDL
Goto Forum:
  


Current Time: Sat Dec 14 09:51:38 GMT 2019

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

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

Back to the top