Creating Ecore models programmatically [message #644827] |
Tue, 14 December 2010 06:24  |
Eclipse User |
|
|
|
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 #645004 is a reply to message #644943] |
Wed, 15 December 2010 03:02   |
Eclipse User |
|
|
|
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 #1840166 is a reply to message #644839] |
Wed, 07 April 2021 22:53  |
Eclipse User |
|
|
|
Jonas Helming wrote on Tue, 14 December 2010 12:11Hi,
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);
|
|
|
Powered by
FUDForum. Page generated in 0.10211 seconds