Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Now I need some advice for code generation(EMF noob)
Now I need some advice for code generation [message #1117465] Thu, 26 September 2013 13:27 Go to next message
Gary Worsham is currently offline Gary WorshamFriend
Messages: 157
Registered: September 2013
Senior Member
All right, I started over with the vogella tutorial:

http://www.vogella.com/articles/EclipseEMF/article.html

and followed it very carefully, and was able to generate a new Eclipse with my model available. Now I want to add some code generation stuff and can't figure out which generated source file to edit to accomplish what I want.

My model at this time consists of a single class, which has two attributes.

The first attribute (an Estring) is the block name. The second one is another Estring which is supposed to be a path to a local file. Right now it is an Estring though eventually I guess I can make it be a java.util.Path attribute.

For my code generation bit, I want to read the file pointed to by the path attribute and parse it to generate Java code. It will be an assembly language file. I have already written the Java code which performs this parsing and translation to a Java representation.

So, my question is:
Which file/method should I modify to do this? The only prerequisite is that the path needs to be set by that time.

Thanks,

GW

[Updated on: Thu, 26 September 2013 13:27]

Report message to a moderator

Re: Now I need some advice for code generation [message #1117545 is a reply to message #1117465] Thu, 26 September 2013 14:58 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 26237
Registered: July 2009
Senior Member
Gary,

I'm not sure you need to modify anything. All such utilities could be
written external to the model. Are you asking how you could invoke what
you've written from the generated editor? Note that you can invoke
Generate Test Code and there will be a generated XyzExample.java that
shows how you can read and write instances of your model in a stand
alone environment (if you want to write some kind of command line
utility)...



On 26/09/2013 3:27 PM, Gary Worsham wrote:
> All right, I started over with the vogella tutorial:
>
> http://www.vogella.com/articles/EclipseEMF/article.html
>
> and followed it very carefully, and was able to generate a new Eclipse
> with my model available. Now I want to add some code generation stuff
> and can't figure out which generated source file to edit to accomplish
> what I want.
>
> My model at this time consists of a single class, which has two
> attributes.
>
> The first attribute (an Estring) is the block name. The second one is
> another Estring which is supposed to be a path to a local file. Right
> now it is an Estring though eventually I guess I can make it be a
> java.util.Path attribute.
>
> For my code generation bit, I want to read the file pointed to by the
> path attribute and parse it to generate Java code. It will be an
> assembly language file. I have already written the Java code which
> performs this parsing and translation to a Java representation.
>
> So, my question is:
> Which file/method should I modify to do this? The only prerequisite
> is that the path needs to be set by that time.
>
> Thanks,
>
> GW
Re: Now I need some advice for code generation [message #1117698 is a reply to message #1117545] Thu, 26 September 2013 18:20 Go to previous messageGo to next message
Gary Worsham is currently offline Gary WorshamFriend
Messages: 157
Registered: September 2013
Senior Member
Ed Merks wrote on Thu, 26 September 2013 10:58
Gary,

I'm not sure you need to modify anything. All such utilities could be
written external to the model. Are you asking how you could invoke what
you've written from the generated editor? Note that you can invoke
Generate Test Code and there will be a generated XyzExample.java that
shows how you can read and write instances of your model in a stand
alone environment (if you want to write some kind of command line
utility)...

Hi Ed,

I'm sure I don't yet know enough about EMF to articulate the issue properly, but I'll try anyway.

The end result that I am going for is a standalone Java application much like I have today (which does not use any modeling). The difference is that, instead of me coding all these classes by hand, I am hoping to have some assistance from the model code generator. The extra twist is that I do not wish to specify all of the attributes of each block in the model. The full understanding of what is needed for any given block is not understood until the assembler code is parsed.

For example, each block can have a variable number of input and output connection "pins". This information is implicit in the assembler code which will be parsed. I already have methods such as addInputPin() and addOutputPin() which I use when initializing my classes the old way. But of course I know how many of them need to be called because I have already translated the assembler to Java equivalent. I don't want to have to do this by hand any more.

So at the first step (since there are other layers of complexity, but I'm trying to just go one layer at a time) I would be looking at something like this:

1) Create basic model definitions and model editor/code generators in Eclipse instance #1.

2) Run Eclipse Application (instance #2) which allows me to now add my block models and specify which assembler file each one is based on.

3) Include the rest of the application code that I'm currently using which gathers these models together into a Swing based GUI application and allows adding/deleting/connecting blocks and then generating the composite DSP output code based on that aggregate model.

4) Build the standalone Java application. Run and/or distribute that application.

Eventually I would like to also distribute the Eclipse application #2 to allow other people to define and add their own functional blocks.

If we go back to the ubiquitous Library example, suppose once we had created a book using the "book" model, which includes a link/path to an external file which was the actual text content of the book, that when we built the library application, the code generators would parse that text and add attributes to the book, such as a list of characters in the story.

Does this help clarify what I'm trying to do? I've looked at a number of EMF examples but I don't think I've come across one yet where the model/class attributes were not completely specified ahead of time.

Thanks,

GW

Re: Now I need some advice for code generation [message #1117779 is a reply to message #1117698] Thu, 26 September 2013 20:06 Go to previous messageGo to next message
Christophe Bouhier is currently offline Christophe BouhierFriend
Messages: 937
Registered: July 2009
Senior Member
Hi Gary,

I stick to the library example :-)

Can't the metamodel be key/value pairs which can be populated when the
text of the book is parsed.
so the attributes can be populated, and the model can grow with an
EReference to Book.

(Sorry, I don't know XCore).
EClass: Book
EAttribute: property / EString
EAttribute: value / EString
EReference: child / Books


You see, building a metamodel on the fly, is like building a model
instance, both are EMF models, so why bother trying to build the meta
model on the fly?

If you really want to build your metamodel dynamicaly, you could use the
EMF reflective api. You can build a EPackage, EClass etc... dynamically.
I can even imagine the code to build the ecore dynamically is generated
by the parser of the text of the book. The text of the book could be a
separate EMF metamodel :P.

Here is an example. (From here:
http://www.ibm.com/developerworks/library/os-eclipse-dynamicemf/).

/*
* Instantiate EcoreFactory
*/
EcoreFactory theCoreFactory = EcoreFactory.eINSTANCE;

/*
* Create EClass instance to model BookStore class
*/
EClass bookStoreEClass = theCoreFactory.createEClass();
bookStoreEClass.setName("BookStore");

/*
* Create EClass instance to model Book class
*/
EClass bookEClass = theCoreFactory.createEClass();
bookEClass.setName("Book");

/*
* Instantiate EPackage and provide unique URI
* to identify this package
*/
EPackage bookStoreEPackage = theCoreFactory.createEPackage();
bookStoreEPackage.setName("BookStorePackage");
bookStoreEPackage.setNsPrefix("bookStore");
bookStoreEPackage.setNsURI("http:///com.ibm.dynamic.example.bookstore.ecore");


hth/ Christophe

On 26-09-13 20:20, Gary Worsham wrote:
> Ed Merks wrote on Thu, 26 September 2013 10:58
>> Gary,
>>
>> I'm not sure you need to modify anything. All such utilities could be
>> written external to the model. Are you asking how you could invoke
>> what you've written from the generated editor? Note that you can
>> invoke Generate Test Code and there will be a generated
>> XyzExample.java that shows how you can read and write instances of
>> your model in a stand alone environment (if you want to write some
>> kind of command line utility)...
>
> Hi Ed,
>
> I'm sure I don't yet know enough about EMF to articulate the issue
> properly, but I'll try anyway.
>
> The end result that I am going for is a standalone Java application much
> like I have today (which does not use any modeling). The difference is
> that, instead of me coding all these classes by hand, I am hoping to
> have some assistance from the model code generator. The extra twist is
> that I do not wish to specify all of the attributes of each block in the
> model. The full understanding of what is needed for any given block is
> not understood until the assembler code is parsed.
>
> For example, each block can have a variable number of input and output
> connection "pins". This information is implicit in the assembler code
> which will be parsed. I already have methods such as addInputPin() and
> addOutputPin() which I use when initializing my classes the old way.
> But of course I know how many of them need to be called because I have
> already translated the assembler to Java equivalent. I don't want to
> have to do this by hand any more.
>
> So at the first step (since there are other layers of complexity, but
> I'm trying to just go one layer at a time) I would be looking at
> something like this:
>
> 1) Create basic model definitions and model editor/code generators in
> Eclipse instance #1.
>
> 2) Run Eclipse Application (instance #2) which allows me to now add my
> block models and specify which assembler file each one is based on.
>
> 3) Include the rest of the application code that I'm currently using
> which gathers these models together into a Swing based GUI application
> and allows adding/deleting/connecting blocks and then generating the
> composite DSP output code based on that aggregate model.
>
> 4) Build the standalone Java application. Run and/or distribute that
> application.
>
> Eventually I would like to also distribute the Eclipse application #2 to
> allow other people to define and add their own functional blocks.
>
> If we go back to the ubiquitous Library example, suppose once we had
> created a book using the "book" model, which includes a link/path to an
> external file which was the actual text content of the book, that when
> we built the library application, the code generators would parse that
> text and add attributes to the book, such as a list of characters in the
> story.
>
> Does this help clarify what I'm trying to do? I've looked at a number
> of EMF examples but I don't think I've come across one yet where the
> model/class attributes were not completely specified ahead of time.
>
> Thanks,
>
> GW
>
>
Re: Now I need some advice for code generation [message #1117790 is a reply to message #1117779] Thu, 26 September 2013 20:19 Go to previous message
Gary Worsham is currently offline Gary WorshamFriend
Messages: 157
Registered: September 2013
Senior Member
Christophe Bouhier wrote on Thu, 26 September 2013 16:06
Hi Gary,

I stick to the library example Smile

Can't the metamodel be key/value pairs which can be populated when the
text of the book is parsed.
so the attributes can be populated, and the model can grow with an
EReference to Book.

(Sorry, I don't know XCore).
EClass: Book
EAttribute: property / EString
EAttribute: value / EString
EReference: child / Books


You see, building a metamodel on the fly, is like building a model
instance, both are EMF models, so why bother trying to build the meta
model on the fly?

If you really want to build your metamodel dynamicaly, you could use the
EMF reflective api. You can build a EPackage, EClass etc... dynamically.
I can even imagine the code to build the ecore dynamically is generated
by the parser of the text of the book. The text of the book could be a
separate EMF metamodel Razz.

Here is an example.

[snip]

hth/ Christophe


Salut et merci Christophe,

I think you have hit it exactly! I don't care if my meta model changes dynamically. The model itself should. I will include EList elements for those things that might be added on the fly. I will review your example and proceed!

A bientot,

GW
Previous Topic:ClassCastException when editing property sheet value
Next Topic:How to edit unknow feature.
Goto Forum:
  


Current Time: Fri Nov 28 20:49:33 GMT 2014

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

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