Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Dynamically Loading EMF Metamodels
Dynamically Loading EMF Metamodels [message #467437] Mon, 03 August 2009 03:33 Go to next message
Anirudh  is currently offline Anirudh Friend
Messages: 22
Registered: July 2009
Junior Member
Hi,

I have a backend system which i access via a REST based API and i am
returned a resource. This is an EMF resource (an XMI representation). The
problem is that i cannot, at design time, assume the metamodel for this
resource - as the metamodel in the backend system can change without
notice.

I am wondering if i can, first, read the metamodel (the .ecore file) from
the backend, generate the code and then, read the models. This will,
admittedly, be slower, but i will have to read the metamodel only once per
session and this will not make me commit to a fixed metamodel. Will this
work? Or is there a better way (recommended way) to achieve what i want?

Also, will this work for the UI? Can i generate the editor code at runtime
(EMF.Edit), just before reading the model from the backend and display the
model with the appropriate visualization?

Thanks!
Re: Dynamically Loading EMF Metamodels [message #467928 is a reply to message #467437] Mon, 03 August 2009 12:11 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------080402030307090401060503
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

Anirudh,

Comments below.

Anirudh wrote:
> Hi,
>
> I have a backend system which i access via a REST based API and i am
> returned a resource. This is an EMF resource (an XMI representation).
> The problem is that i cannot, at design time, assume the metamodel for
> this resource - as the metamodel in the backend system can change
> without notice.
Change arbitrarily or in a binary compatible way?
>
> I am wondering if i can, first, read the metamodel (the .ecore file)
> from the backend, generate the code and then, read the models.
I suppose, but it seems not necessary to generate code given that this
is completely dynamic.
> This will, admittedly, be slower, but i will have to read the
> metamodel only once per session and this will not make me commit to a
> fixed metamodel. Will this work? Or is there a better way (recommended
> way) to achieve what i want?
You can just read a .ecore model and then process an instance file based
on that model.
>
> Also, will this work for the UI? Can i generate the editor code at
> runtime (EMF.Edit), just before reading the model from the backend and
> display the model with the appropriate visualization?
Try doing the following. Open the .ecore file in the Sample Ecore
Editor, select one EClass, and then from the context menu invoke Create
Dynamic Instance. It sounds like this is pretty much all you're trying
to accomplish. There are two articles that describe dynamic EMF on the
documentation page:
<http://www.eclipse.org/modeling/emf/docs/>

http://www.eclipse.org/modeling/emf/docs/

>
> Thanks!
>

--------------080402030307090401060503
Content-Type: text/html; charset=ISO-8859-15
Content-Transfer-Encoding: 8bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-15"
http-equiv="Content-Type">
<title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
Anirudh,<br>
<br>
Comments below.<br>
<br>
Anirudh wrote:
<blockquote
cite="mid:09ec95e7860c9c32799e4335f95f1ee1$1@www.eclipse.org"
type="cite">Hi,
<br>
<br>
I have a backend system which i access via a REST based API and i am
returned a resource. This is an EMF resource (an XMI representation).
The problem is that i cannot, at design time, assume the metamodel for
this resource - as the metamodel in the backend system can change
without notice.
<br>
</blockquote>
Change arbitrarily or in a binary compatible way?<br>
<blockquote
cite="mid:09ec95e7860c9c32799e4335f95f1ee1$1@www.eclipse.org"
type="cite"><br>
I am wondering if i can, first, read the metamodel (the .ecore file)
from the backend, generate the code and then, read the models.</blockquote>
I suppose, but it seems not necessary to generate code given that this
is completely dynamic.<br>
<blockquote
cite="mid:09ec95e7860c9c32799e4335f95f1ee1$1@www.eclipse.org"
type="cite"> This will, admittedly, be slower, but i will have to read
the metamodel only once per session and this will not make me commit to
a fixed metamodel. Will this work? Or is there a better way
(recommended way) to achieve what i want?
<br>
</blockquote>
You can just read a .ecore model and then process an instance file
based on that model.<br>
<blockquote
cite="mid:09ec95e7860c9c32799e4335f95f1ee1$1@www.eclipse.org"
type="cite"><br>
Also, will this work for the UI? Can i generate the editor code at
runtime (EMF.Edit), just before reading the model from the backend and
display the model with the appropriate visualization?
<br>
</blockquote>
Try doing the following.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Dynamically Loading EMF Metamodels [message #468000 is a reply to message #467928] Mon, 03 August 2009 14:51 Go to previous messageGo to next message
Anirudh  is currently offline Anirudh Friend
Messages: 22
Registered: July 2009
Junior Member
Hi Ed,

Thanks for that. On re-reading my question, i realized that i was
definitely not clear. I'll try again with an example.

Let us assume i have two 'Library' metamodels both containing 'Book' and
'Author'. In one, a book can have only one author, whereas in another a
book can have multiple authors. Now, depending on the backend i am
connecting to, i receive data adhering to one of the metamodels. I don't
want to hardcode my UI to be dependent on the metamodel (maybe tomorrow, i
have a third backend that now has books with no authors).

I can obtain the metamodel files (the .ecore files) at runtime before i
read the actual models.

My query is whether it is possible at runtime to generate the EClass
Instances and the corresponding editors from this .ecore file, so that
when i read the model, i know exactly how to display it. Will Dynamic EMF
help me here - as far as i know, to use dynamic EMF, i should be aware of
the metamodel at design time (correct me if i am wrong).

Thanks once again.

***
Ed Merks wrote:

> Anirudh,

> Comments below.

> Anirudh wrote:
>> Hi,
>>
>> I have a backend system which i access via a REST based API and i am
>> returned a resource. This is an EMF resource (an XMI representation).
>> The problem is that i cannot, at design time, assume the metamodel for
>> this resource - as the metamodel in the backend system can change
>> without notice.
> Change arbitrarily or in a binary compatible way?
>>
>> I am wondering if i can, first, read the metamodel (the .ecore file)
>> from the backend, generate the code and then, read the models.
> I suppose, but it seems not necessary to generate code given that this
> is completely dynamic.
>> This will, admittedly, be slower, but i will have to read the
>> metamodel only once per session and this will not make me commit to a
>> fixed metamodel. Will this work? Or is there a better way (recommended
>> way) to achieve what i want?
> You can just read a .ecore model and then process an instance file based
> on that model.
>>
>> Also, will this work for the UI? Can i generate the editor code at
>> runtime (EMF.Edit), just before reading the model from the backend and
>> display the model with the appropriate visualization?
> Try doing the following. Open the .ecore file in the Sample Ecore
> Editor, select one EClass, and then from the context menu invoke Create
> Dynamic Instance. It sounds like this is pretty much all you're trying
> to accomplish. There are two articles that describe dynamic EMF on the
> documentation page:
> <http://www.eclipse.org/modeling/emf/docs/>

> http://www.eclipse.org/modeling/emf/docs/

>>
>> Thanks!
Re: Dynamically Loading EMF Metamodels [message #468004 is a reply to message #468000] Mon, 03 August 2009 15:11 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
Anirudh,

Yes, it seems to me that dynamic EMF is a perfect fit. The point is
that all processing is done using reflection on whatever Ecore model is
read in with no a priori knowledge of that model.

Anirudh wrote:
> Hi Ed,
>
> Thanks for that. On re-reading my question, i realized that i was
> definitely not clear. I'll try again with an example.
>
> Let us assume i have two 'Library' metamodels both containing 'Book'
> and 'Author'. In one, a book can have only one author, whereas in
> another a book can have multiple authors. Now, depending on the
> backend i am connecting to, i receive data adhering to one of the
> metamodels. I don't want to hardcode my UI to be dependent on the
> metamodel (maybe tomorrow, i have a third backend that now has books
> with no authors).
>
> I can obtain the metamodel files (the .ecore files) at runtime before
> i read the actual models.
>
> My query is whether it is possible at runtime to generate the EClass
> Instances and the corresponding editors from this .ecore file, so that
> when i read the model, i know exactly how to display it. Will Dynamic
> EMF help me here - as far as i know, to use dynamic EMF, i should be
> aware of the metamodel at design time (correct me if i am wrong).
>
> Thanks once again.
>
> ***
> Ed Merks wrote:
>
>> Anirudh,
>
>> Comments below.
>
>> Anirudh wrote:
>>> Hi,
>>>
>>> I have a backend system which i access via a REST based API and i am
>>> returned a resource. This is an EMF resource (an XMI
>>> representation). The problem is that i cannot, at design time,
>>> assume the metamodel for this resource - as the metamodel in the
>>> backend system can change without notice.
>> Change arbitrarily or in a binary compatible way?
>>>
>>> I am wondering if i can, first, read the metamodel (the .ecore file)
>>> from the backend, generate the code and then, read the models.
>> I suppose, but it seems not necessary to generate code given that
>> this is completely dynamic.
>>> This will, admittedly, be slower, but i will have to read the
>>> metamodel only once per session and this will not make me commit to
>>> a fixed metamodel. Will this work? Or is there a better way
>>> (recommended way) to achieve what i want?
>> You can just read a .ecore model and then process an instance file
>> based on that model.
>>>
>>> Also, will this work for the UI? Can i generate the editor code at
>>> runtime (EMF.Edit), just before reading the model from the backend
>>> and display the model with the appropriate visualization?
>> Try doing the following. Open the .ecore file in the Sample Ecore
>> Editor, select one EClass, and then from the context menu invoke
>> Create Dynamic Instance. It sounds like this is pretty much all
>> you're trying to accomplish. There are two articles that describe
>> dynamic EMF on the documentation page:
>> <http://www.eclipse.org/modeling/emf/docs/>
>
>> http://www.eclipse.org/modeling/emf/docs/
>
>>>
>>> Thanks!
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Dynamically Loading EMF Metamodels [message #468026 is a reply to message #468004] Mon, 03 August 2009 16:54 Go to previous messageGo to next message
Anirudh  is currently offline Anirudh Friend
Messages: 22
Registered: July 2009
Junior Member
Hi Ed,

I am unable to completely get my head around the solution. I'll illustrate
what i am thinking, let me know if it is right.

1. The model (the .xmi file) contains the xsi namespace declaration and
the xsi:schemaLocation attribute.

2. I read the metamodel from the URI provided in the xsi:schemaLocation as
follows:
Resource resource = resourceSet.getResource(URI, true);
EPackage package = (EPackage) resource.getContents().get(0);

3. Create the metamodel and model in memory using the Reflective API and
Dynamic EMF.

Is this okay? Or am i doing it wrong?

***

Ed Merks wrote:

> Anirudh,

> Yes, it seems to me that dynamic EMF is a perfect fit. The point is
> that all processing is done using reflection on whatever Ecore model is
> read in with no a priori knowledge of that model.

> Anirudh wrote:
>> Hi Ed,
>>
>> Thanks for that. On re-reading my question, i realized that i was
>> definitely not clear. I'll try again with an example.
>>
>> Let us assume i have two 'Library' metamodels both containing 'Book'
>> and 'Author'. In one, a book can have only one author, whereas in
>> another a book can have multiple authors. Now, depending on the
>> backend i am connecting to, i receive data adhering to one of the
>> metamodels. I don't want to hardcode my UI to be dependent on the
>> metamodel (maybe tomorrow, i have a third backend that now has books
>> with no authors).
>>
>> I can obtain the metamodel files (the .ecore files) at runtime before
>> i read the actual models.
>>
>> My query is whether it is possible at runtime to generate the EClass
>> Instances and the corresponding editors from this .ecore file, so that
>> when i read the model, i know exactly how to display it. Will Dynamic
>> EMF help me here - as far as i know, to use dynamic EMF, i should be
>> aware of the metamodel at design time (correct me if i am wrong).
>>
>> Thanks once again.
>>
>> ***
>> Ed Merks wrote:
>>
>>> Anirudh,
>>
>>> Comments below.
>>
>>> Anirudh wrote:
>>>> Hi,
>>>>
>>>> I have a backend system which i access via a REST based API and i am
>>>> returned a resource. This is an EMF resource (an XMI
>>>> representation). The problem is that i cannot, at design time,
>>>> assume the metamodel for this resource - as the metamodel in the
>>>> backend system can change without notice.
>>> Change arbitrarily or in a binary compatible way?
>>>>
>>>> I am wondering if i can, first, read the metamodel (the .ecore file)
>>>> from the backend, generate the code and then, read the models.
>>> I suppose, but it seems not necessary to generate code given that
>>> this is completely dynamic.
>>>> This will, admittedly, be slower, but i will have to read the
>>>> metamodel only once per session and this will not make me commit to
>>>> a fixed metamodel. Will this work? Or is there a better way
>>>> (recommended way) to achieve what i want?
>>> You can just read a .ecore model and then process an instance file
>>> based on that model.
>>>>
>>>> Also, will this work for the UI? Can i generate the editor code at
>>>> runtime (EMF.Edit), just before reading the model from the backend
>>>> and display the model with the appropriate visualization?
>>> Try doing the following. Open the .ecore file in the Sample Ecore
>>> Editor, select one EClass, and then from the context menu invoke
>>> Create Dynamic Instance. It sounds like this is pretty much all
>>> you're trying to accomplish. There are two articles that describe
>>> dynamic EMF on the documentation page:
>>> <http://www.eclipse.org/modeling/emf/docs/>
>>
>>> http://www.eclipse.org/modeling/emf/docs/
>>
>>>>
>>>> Thanks!
>>
Re: Dynamically Loading EMF Metamodels [message #468030 is a reply to message #468026] Mon, 03 August 2009 17:23 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
Anirudh,

Comments below.


Anirudh wrote:
> Hi Ed,
>
> I am unable to completely get my head around the solution. I'll
> illustrate what i am thinking, let me know if it is right.
>
> 1. The model (the .xmi file) contains the xsi namespace declaration
> and the xsi:schemaLocation attribute.
Yes, though that's not absolutely necessary. In general, an
xsi:schemaLocation is a hint, at least that's how the XML specifications
describe them. It's often sufficient just to know the namespace and
from that it might well be possible to locate the model (schema) via
some other means, e.g., an XML catalog.
>
> 2. I read the metamodel from the URI provided in the
> xsi:schemaLocation as follows:
> Resource resource = resourceSet.getResource(URI, true);
> EPackage package = (EPackage) resource.getContents().get(0);
Actually, EMF will generally do this automatically. If it can't find
the namespace in the package registry, then it will resort to using the
"hint" about where it can load the model.
>
> 3. Create the metamodel and model in memory using the Reflective API
> and Dynamic EMF.
The Ecore model will simply loaded and that makes it available for
processing the rest of the instance file. Did you try "Create Dynamic
Instance..."? If you look carefully at the generated editor, you'll see
that it shows both the loaded Ecore model as well as the loaded instance...
>
> Is this okay? Or am i doing it wrong?
It sounds like you might be expecting it to be more complex than it
really is...
>
> ***
>
> Ed Merks wrote:
>
>> Anirudh,
>
>> Yes, it seems to me that dynamic EMF is a perfect fit. The point is
>> that all processing is done using reflection on whatever Ecore model
>> is read in with no a priori knowledge of that model.
>
>> Anirudh wrote:
>>> Hi Ed,
>>>
>>> Thanks for that. On re-reading my question, i realized that i was
>>> definitely not clear. I'll try again with an example.
>>>
>>> Let us assume i have two 'Library' metamodels both containing 'Book'
>>> and 'Author'. In one, a book can have only one author, whereas in
>>> another a book can have multiple authors. Now, depending on the
>>> backend i am connecting to, i receive data adhering to one of the
>>> metamodels. I don't want to hardcode my UI to be dependent on the
>>> metamodel (maybe tomorrow, i have a third backend that now has books
>>> with no authors).
>>>
>>> I can obtain the metamodel files (the .ecore files) at runtime
>>> before i read the actual models.
>>>
>>> My query is whether it is possible at runtime to generate the EClass
>>> Instances and the corresponding editors from this .ecore file, so
>>> that when i read the model, i know exactly how to display it. Will
>>> Dynamic EMF help me here - as far as i know, to use dynamic EMF, i
>>> should be aware of the metamodel at design time (correct me if i am
>>> wrong).
>>>
>>> Thanks once again.
>>>
>>> ***
>>> Ed Merks wrote:
>>>
>>>> Anirudh,
>>>
>>>> Comments below.
>>>
>>>> Anirudh wrote:
>>>>> Hi,
>>>>>
>>>>> I have a backend system which i access via a REST based API and i
>>>>> am returned a resource. This is an EMF resource (an XMI
>>>>> representation). The problem is that i cannot, at design time,
>>>>> assume the metamodel for this resource - as the metamodel in the
>>>>> backend system can change without notice.
>>>> Change arbitrarily or in a binary compatible way?
>>>>>
>>>>> I am wondering if i can, first, read the metamodel (the .ecore
>>>>> file) from the backend, generate the code and then, read the models.
>>>> I suppose, but it seems not necessary to generate code given that
>>>> this is completely dynamic.
>>>>> This will, admittedly, be slower, but i will have to read the
>>>>> metamodel only once per session and this will not make me commit
>>>>> to a fixed metamodel. Will this work? Or is there a better way
>>>>> (recommended way) to achieve what i want?
>>>> You can just read a .ecore model and then process an instance file
>>>> based on that model.
>>>>>
>>>>> Also, will this work for the UI? Can i generate the editor code at
>>>>> runtime (EMF.Edit), just before reading the model from the backend
>>>>> and display the model with the appropriate visualization?
>>>> Try doing the following. Open the .ecore file in the Sample Ecore
>>>> Editor, select one EClass, and then from the context menu invoke
>>>> Create Dynamic Instance. It sounds like this is pretty much all
>>>> you're trying to accomplish. There are two articles that describe
>>>> dynamic EMF on the documentation page:
>>>> <http://www.eclipse.org/modeling/emf/docs/>
>>>
>>>> http://www.eclipse.org/modeling/emf/docs/
>>>
>>>>>
>>>>> Thanks!
>>>
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Dynamically Loading EMF Metamodels [message #468332 is a reply to message #468030] Wed, 05 August 2009 06:40 Go to previous messageGo to next message
Anirudh  is currently offline Anirudh Friend
Messages: 22
Registered: July 2009
Junior Member
Hi Ed,

I apologize for being so dense, but i think i am complicating things too
much.
My requirement is simple: i obtain a .ecore file at runtime and i need to
generate the code and editors from this so that i can edit an existing
xmi model.

What is the best way to do this?

From my understanding, for using dynamic EMF, i should know the elements
in the ecore file at design time. In my case, i have no idea about the
metamodel at design time. What does 'Generate Dynamic Instance' do and how
is it related?

Thanks,
Anirudh
Re: Dynamically Loading EMF Metamodels [message #468412 is a reply to message #468332] Wed, 05 August 2009 13:18 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
Anirudh,

COmments below.

Anirudh wrote:
> Hi Ed,
>
> I apologize for being so dense, but i think i am complicating things
> too much.
> My requirement is simple: i obtain a .ecore file at runtime and i need
> to generate the code and editors from this so that i can edit an
> existing xmi model.
That's my point though. You don't need to generate code nor editors to
edit instances.
>
> What is the best way to do this?
Best to avoid it I think.
>
> From my understanding, for using dynamic EMF, i should know the
> elements in the ecore file at design time.
No, the reflective editor works for any Ecore model.
> In my case, i have no idea about the metamodel at design time.
Hence you'll need to rely on reflection.
> What does 'Generate Dynamic Instance' do and how is it related?
Have you tried it? It demonstrates that EMF is capable of editing an
instance of any EClass without generating code...
>
> Thanks,
> Anirudh
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:Re: Metamodeling
Next Topic:EMF.Edit Commands, UI updated challenge.
Goto Forum:
  


Current Time: Fri Mar 29 09:20:29 GMT 2024

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

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

Back to the top