Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Creating Ecore models programmatically
Creating Ecore models programmatically [message #644827] Tue, 14 December 2010 11:24 Go to next message
ramlaned  is currently offline ramlaned Friend
Messages: 3
Registered: December 2010
Junior Member
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 12:17 Go to previous messageGo to next message
ramlaned  is currently offline ramlaned Friend
Messages: 3
Registered: December 2010
Junior Member
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 12:11 Go to previous messageGo to next message
Jonas Helming is currently offline Jonas HelmingFriend
Messages: 699
Registered: July 2009
Senior Member
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 21:21 Go to previous messageGo to next message
Hallvard Traetteberg is currently offline Hallvard TraettebergFriend
Messages: 673
Registered: July 2009
Location: Trondheim, Norway
Senior Member
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 08:02 Go to previous messageGo to next message
Eike Stepper is currently offline Eike StepperFriend
Messages: 6594
Registered: July 2009
Senior Member
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 11:25 Go to previous messageGo to next message
ramlaned  is currently offline ramlaned Friend
Messages: 3
Registered: December 2010
Junior Member
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 11:25]

Report message to a moderator

Re: Creating Ecore models programmatically [message #1840166 is a reply to message #644839] Thu, 08 April 2021 02:53 Go to previous message
Lesley Lee is currently offline Lesley LeeFriend
Messages: 1
Registered: April 2021
Junior Member
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: Fri Oct 22 13:24:18 GMT 2021

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

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

Back to the top