Skip to main content



      Home
Home » Modeling » EMF » Creating Ecore models programmatically
Creating Ecore models programmatically [message #644827] Tue, 14 December 2010 06:24 Go to next message
Eclipse UserFriend
Hi,

I've searched the web for this, but couldn't find anything.

I wonder if it is possible to create Ecore models (.ecore) programmatically in Java?

For instance something like this (pseudo):

EClass c = new EClass( "SomeClass" );
c.addAttribute(...);
c.addReference(...);

model.addClass( c );
model.writeToDisk( "platform/resource/..../model.ecore");

Also, is this possible for model instances (.xmi)?

If not, what packages/classes in the EMF library could be useful to investigate further?

Take care!
Re: Creating Ecore models programmatically [message #644838 is a reply to message #644827] Tue, 14 December 2010 07:17 Go to previous messageGo to next message
Eclipse UserFriend
I searched some more and found an answer to first question regarding .ecore files. Smile Any additional info is of course welcome.

( http://wiki.eclipse.org/EMF/Recipes#Recipe:_Generating_Your_ Own_Ecore_Model_using_a_stand-alone_Java_App)

Re: Creating Ecore models programmatically [message #644839 is a reply to message #644827] Tue, 14 December 2010 07:11 Go to previous messageGo to next message
Eclipse UserFriend
Hi,
of course this is possible. Ecore itself is defined in Ecore, so, there
should actually be no big difference between creating an Ecore or a
model instance. You can use the default EMF API for this.
Create EObject:
EcoreFactory.eINSTANCE.createEClass()
Modify References:
eClass.getEAttributes().add(yourEAttribute);
and so on.
For your instance you call your factory to create a new object:

{PackageName}Factory.eINSTANCE.create{yourModelelement}()

Cheers
Jonas


On 14.12.10 12:24, ramlaned wrote:
> Hi,
>
> I've searched the web for this, but couldn't find anything.
>
> I wonder if it is possible to create Ecore models (.ecore)
> programmatically in Java?
> For instance something like this (pseudo):
>
> EClass c = new EClass( "SomeClass" );
> c.addAttribute(...);
> c.addReference(...);
>
> model.addClass( c );
> model.writeToDisk( "platform/resource/..../model.ecore");
>
> Also, is this possible for model instances (.xmi)?
>
> If not, what packages/classes in the EMF library could be useful to
> investigate further?
>
> Take care!
Re: Creating Ecore models programmatically [message #644943 is a reply to message #644839] Tue, 14 December 2010 16:21 Go to previous messageGo to next message
Eclipse UserFriend
On 14.12.10 13.11, Jonas wrote:
> Hi,
> of course this is possible. Ecore itself is defined in Ecore, so, there
> should actually be no big difference between creating an Ecore or a
> model instance. You can use the default EMF API for this.
> Create EObject:
> EcoreFactory.eINSTANCE.createEClass()
> Modify References:
> eClass.getEAttributes().add(yourEAttribute);
> and so on.
> For your instance you call your factory to create a new object:
>
> {PackageName}Factory.eINSTANCE.create{yourModelelement}()

Note that you can also create the instances of this model, without
generating code, by using creating a factory with
EcoreFactory.eINSTANCE.createEFactory() and using this factory's
create(EClass) method:

EPackage pack = EcoreFactory.eINSTANCE.createEPackage();
EClass personClass = EcoreFactory.eINSTANCE.createEClass();
EAttribute nameAttribute = EcoreFactory.eINSTANCE.createEAttribute();
nameAttribute.setName("name");
nameAttribute.setEType(EcorePackage.eINSTANCE.getEString());
personClass.getEAttributes().add(nameAttribute);
pack.getEClassifiers().add(personClass);
EFactory factory = EcoreFactory.eINSTANCE.createEFactory();
EObject person = factory.create(personClass);
person.eSet(nameAttribute, "Jonas");

Thus, both models and instances of these models can be created reflectively.

Hallvard
Re: Creating Ecore models programmatically [message #645004 is a reply to message #644943] Wed, 15 December 2010 03:02 Go to previous messageGo to next message
Eclipse UserFriend
This is a multi-part message in MIME format.
--------------000401010102070005010001
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit

Am 14.12.2010 22:21, schrieb Hallvard Trætteberg:
> On 14.12.10 13.11, Jonas wrote:
>> Hi,
>> of course this is possible. Ecore itself is defined in Ecore, so, there
>> should actually be no big difference between creating an Ecore or a
>> model instance. You can use the default EMF API for this.
>> Create EObject:
>> EcoreFactory.eINSTANCE.createEClass()
>> Modify References:
>> eClass.getEAttributes().add(yourEAttribute);
>> and so on.
>> For your instance you call your factory to create a new object:
>>
>> {PackageName}Factory.eINSTANCE.create{yourModelelement}()
>
> Note that you can also create the instances of this model, without generating code, by using creating a factory with EcoreFactory.eINSTANCE.createEFactory() and using this factory's create(EClass) method:
That's not entirely correct. You can create a factory with EcoreFactory.eINSTANCE.createEFactory() but that factory can not be used to create instances of classes outside the Ecore package itself. Here is an example that *runs* without warnings or exceptions:

|EAttribute nameAttribute = EcoreFactory.eINSTANCE.createEAttribute();
nameAttribute.setName("name");
nameAttribute.setEType(EcorePackage.eINSTANCE.getEString());

EClass personClass = EcoreFactory.eINSTANCE.createEClass();
personClass.*_setName_**_("Person")_*;
personClass.*_getEStructuralFeatures_**_()_*.add(nameAttribu te);

EPackage pack = EcoreFactory.eINSTANCE.createEPackage();
pack.setName("pack");
pack.getEClassifiers().add(personClass);

EFactory factory = *_pack.getEFactoryInstance_**_()_*;
EObject person = factory.create(personClass);
person.eSet(nameAttribute, "Jonas");|


Cheers
/Eike

----
http://www.esc-net.de
http://thegordian.blogspot.com
http://twitter.com/eikestepper



>
> EPackage pack = EcoreFactory.eINSTANCE.createEPackage();
> EClass personClass = EcoreFactory.eINSTANCE.createEClass();
> EAttribute nameAttribute = EcoreFactory.eINSTANCE.createEAttribute();
> nameAttribute.setName("name");
> nameAttribute.setEType(EcorePackage.eINSTANCE.getEString());
> personClass.getEAttributes().add(nameAttribute);
> pack.getEClassifiers().add(personClass);
> EFactory factory = EcoreFactory.eINSTANCE.createEFactory();
> EObject person = factory.create(personClass);
> person.eSet(nameAttribute, "Jonas");
>
> Thus, both models and instances of these models can be created reflectively.
>
> Hallvard

--------------000401010102070005010001
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Am 14.12.2010 22:21, schrieb Hallvard Trætteberg:
<blockquote cite="mid:ie8mr2$ba7$1@news.eclipse.org" type="cite">On
14.12.10 13.11, Jonas wrote:
<br>
<blockquote type="cite">Hi,
<br>
of course this is possible. Ecore itself is defined in Ecore,
so, there
<br>
should actually be no big difference between creating an Ecore
or a
<br>
model instance. You can use the default EMF API for this.
<br>
Create EObject:
<br>
EcoreFactory.eINSTANCE.createEClass()
<br>
Modify References:
<br>
eClass.getEAttributes().add(yourEAttribute);
<br>
and so on.
<br>
For your instance you call your factory to create a new object:
<br>
<br>
{PackageName}Factory.eINSTANCE.create{yourModelelement}()
<br>
</blockquote>
<br>
Note that you can also create the instances of this model, without
generating code, by using creating a factory with
EcoreFactory.eINSTANCE.createEFactory() and using this factory's
create(EClass) method:
<br>
</blockquote>
That's not entirely correct. You can create a factory with
EcoreFactory.eINSTANCE.createEFactory() but that factory can not be
used to create instances of classes outside the Ecore package
itself. Here is an example that *runs* without warnings or
exceptions:<br>
<br>
<title></title>
<style type="text/css">
<!--code { font-family: Courier New, Courier; font-size: 10pt; margin: 0px; }-->
</style>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!-- ======================================================== -->
<!-- = Java Sourcecode to HTML automatically converted code = -->
<!-- = Java2Html Converter 5.0 [2006-02-26] by Markus Gebhard markus@jave.de = -->
<!-- = Further information: http://www.java2html.de = -->
<div class="java" align="left">
<table bgcolor="#ffffff" border="0" cellpadding="3"
cellspacing="0">
<tbody>
<tr>
<!-- start source code --> <td nowrap="nowrap" align="left"
valign="top"> <code>
<font color="#ffffff">    </font><font color="#000000"> EAttribute nameAttribute = EcoreFactory.eINSTANCE.createE Attribute </font><font
color="#000000">()</font><font color="#000000">;</font><br>
<font color="#ffffff">    </font><font color="#000000">nameAttribute.setName</font><font
color="#000000">(</font><font color="#2a00ff">"name"</font><font
color="#000000">)</font><font color="#000000">;</font><br>
<font color="#ffffff">    </font><font color="#000000">nameAttribute.setEType</font><font
color="#000000">(</font><font color="#000000">EcorePackage.eINSTANCE.getEString</font><font
color="#000000">())</font><font color="#000000">;</font><br>
<font color="#ffffff"></font><br>
<font color="#ffffff">    </font><font color="#000000">EClass personClass = EcoreFactory.eINSTANCE.createEClass </font><font
color="#000000">()</font><font color="#000000">;</font><br>
<font color="#ffffff">    </font><font color="#000000">personClass.<font
color="#ff0000"><b><u>setName</u></b></font></font><font
color="#ff0000"><b><u>("Person")</u></b></font><font
color="#000000">;</font><br>
<font color="#ffffff">    </font><font color="#000000">personClass.<font
color="#ff0000"><b><u>getEStructuralFeatures</u></b></font ></font><font
color="#ff0000"><b><u>()</u></b></font><font
color="#000000">.add</font><font color="#000000">(</font><font
color="#000000">nameAttribute</font><font
color="#000000">)</font><font color="#000000">;</font><br>
<font color="#ffffff"></font><br>
<font color="#ffffff">    </font><font color="#000000">EPackage pack = EcoreFactory.eINSTANCE.createEPackage </font><font
color="#000000">()</font><font color="#000000">;</font><br>
<font color="#ffffff">    </font><font color="#000000">pack.setName</font><font
color="#000000">(</font><font color="#2a00ff">"pack"</font><font
color="#000000">)</font><font color="#000000">;</font><br>
<font color="#ffffff">    </font><font color="#000000">pack.getEClassifiers</font><font
color="#000000">()</font><font color="#000000">.add</font><font
color="#000000">(</font><font color="#000000">personClass</font><font
color="#000000">)</font><font color="#000000">;</font><br>
<font color="#ffffff"></font><br>
<font color="#ffffff">    </font><font color="#000000">EFactory factory = <font
color="#ff0000"><b><u>pack.getEFactoryInstance</u></b> </font></font><font
color="#ff0000"><b><u>()</u></b></font><font
color="#000000">;</font><br>
<font color="#ffffff">    </font><font color="#000000">EObject person = factory.create</font> <font
color="#000000">(</font><font color="#000000">personClass</font><font
color="#000000">)</font><font color="#000000">;</font><br>
<font color="#ffffff">    </font><font color="#000000">person.eSet</font><font
color="#000000">(</font><font color="#000000">nameAttribute, </font><font
color="#2a00ff">"Jonas"</font><font color="#000000">)</font><font
color="#000000">;</font></code> </td>
<!-- end source code --> </tr>
</tbody>
</table>
</div>
<!-- = END of automatically generated HTML code = -->
<!-- ======================================================== -->
<br>
Cheers<br>
/Eike<br>
<br>
----<br>
<a class="moz-txt-link-freetext" href="http://www.esc-net.de">http://www.esc-net.de</a><br>
<a class="moz-txt-link-freetext" href="http://thegordian.blogspot.com">http://thegordian.blogspot.com</a><br>
<a class="moz-txt-link-freetext" href="http://twitter.com/eikestepper">http://twitter.com/eikestepper</a><br>
<br>
<br>
<br>
<blockquote cite="mid:ie8mr2$ba7$1@news.eclipse.org" type="cite">
<br>
EPackage pack = EcoreFactory.eINSTANCE.createEPackage();
<br>
EClass personClass = EcoreFactory.eINSTANCE.createEClass();
<br>
EAttribute nameAttribute =
EcoreFactory.eINSTANCE.createEAttribute();
<br>
nameAttribute.setName("name");
<br>
nameAttribute.setEType(EcorePackage.eINSTANCE.getEString());
<br>
personClass.getEAttributes().add(nameAttribute);
<br>
pack.getEClassifiers().add(personClass);
<br>
EFactory factory = EcoreFactory.eINSTANCE.createEFactory();
<br>
EObject person = factory.create(personClass);
<br>
person.eSet(nameAttribute, "Jonas");
<br>
<br>
Thus, both models and instances of these models can be created
reflectively.
<br>
<br>
Hallvard
<br>
</blockquote>
</body>
</html>

--------------000401010102070005010001--
Re: Creating Ecore models programmatically [message #645274 is a reply to message #644827] Thu, 16 December 2010 06:25 Go to previous messageGo to next message
Eclipse UserFriend
Thanks to all that responded to this! I've for the most part worked with instances of Ecore models, thus it's great to see that Ecore models can be created in a similiar manner.

Take care!

[Updated on: Thu, 16 December 2010 06:25] by Moderator

Re: Creating Ecore models programmatically [message #1840166 is a reply to message #644839] Wed, 07 April 2021 22:53 Go to previous message
Eclipse UserFriend
Jonas Helming wrote on Tue, 14 December 2010 12:11
Hi,
of course this is possible. Ecore itself is defined in Ecore, so, there
should actually be no big difference between creating an Ecore or a
model instance. You can use the default EMF API for this.
Create EObject:
EcoreFactory.eINSTANCE.createEClass()
Modify References:
eClass.getEAttributes().add(yourEAttribute);
and so on.
For your instance you call your factory to create a new object:

{PackageName}Factory.eINSTANCE.create{yourModelelement}()

Cheers
Jonas


On 14.12.10 12:24, ramlaned wrote:
> Hi,
>
> I've searched the web for this, but couldn't find anything.
>
> I wonder if it is possible to create Ecore models (.ecore)
> programmatically in Java?
> For instance something like this (pseudo):
>
> EClass c = new EClass( "SomeClass" );
> c.addAttribute(...);
> c.addReference(...);
>
> model.addClass( c );
> model.writeToDisk( "platform/resource/..../model.ecore");
>
> Also, is this possible for model instances (.xmi)?
>
> If not, what packages/classes in the EMF library could be useful to
> investigate further?
>
> Take care!


This is the answer, and the following is my example.
		Person newPerson = CollegeFactory.eINSTANCE.createPerson();
		newPerson.setName("Bob");
		newPerson.setAge(24);
		addressBook.getPersons().add(newPerson);
Previous Topic:[CDO] Websocket transport layer
Next Topic:UML profile and EMF ecore
Goto Forum:
  


Current Time: Thu May 08 08:57:27 EDT 2025

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

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

Back to the top