Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF "Technology" (Ecore Tools, EMFatic, etc)  » [Texo] Generating model code programmatically
[Texo] Generating model code programmatically [message #536519] Fri, 28 May 2010 10:46 Go to next message
A. Boughlam
Messages: 14
Registered: May 2010
Junior Member
I've just discovered Texo and i'm wondering how to generate pojo classes from an ecore model programmatically ? If someone can give me a lead on this. Thanks in advance.

Best Regards,
A. Boughlam
Re: [Texo] Generating model code programmatically [message #536595 is a reply to message #536519] Fri, 28 May 2010 16:07 Go to previous messageGo to next message
Martin Taal is currently offline Martin Taal
Messages: 5300
Registered: July 2009
Senior Member
Hi,
I can give you some pointers but the Texo code generation requires some specific parts of the eclipse IDE to do import
resolving and code formatting, in addition Texo requires a java project to generate code into. As part of the test run
Texo generates code using an eclipse headless run and a ready to use workspace. In all it is pretty doable to set this
up, but it is not as easy as calling Texo directly from an ant script.

The Texo model generator test project is available in cvs here:
dev.eclipse.org
/cvsroot/modeling
org.eclipse.emf/org.eclipse.emf.texo/tests/org.eclipse.emf.t exo.modelgenerator.test

The actual test code to generate is fairly simple:

final ModelController modelController = new ModelController();
modelController.setEPackages(ePackages);
modelController.annotate(new ArrayList<ModelAnnotator>());

final WorkFlowStarter wfs = new WorkFlowStarter();
wfs.generateModelCode(TEST_MODEL_PROJECT, ePackages, modelController, "src-test-gen"); //$NON-NLS-1$

where TEST_MODEL_PROJECT is the name of a java project in the workspace and src-test-gen the target folder in that
project. The ePackages is a list of EPackage instances, the modelController is a specific Texo class.

gr. Martin

A. Boughlam wrote:
> I've just discovered Texo and i'm wondering how to generate pojo classes
> from an ecore model programmatically ? If someone can give me a lead on
> this. Thanks in advance.


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Re: [Texo] Generating model code programmatically [message #536839 is a reply to message #536595] Mon, 31 May 2010 04:32 Go to previous messageGo to next message
A. Boughlam
Messages: 14
Registered: May 2010
Junior Member
Thank you Martin, exactly what I needed.

Best Regards,
A. Boughlam
Re: [Texo] Generating model code programmatically [message #536845 is a reply to message #536839] Mon, 31 May 2010 05:12 Go to previous messageGo to next message
Martin Taal is currently offline Martin Taal
Messages: 5300
Registered: July 2009
Senior Member
As an additional remark, the model generator test project contains launch configurations which run the model generator
for a certain workspace. Texo uses Buckminster in its build environment which makes it pretty easy to run launch
configurations.

gr. Martin

A. Boughlam wrote:
> Thank you Martin, exactly what I needed.


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Re: [Texo] Generating model code programmatically [message #537098 is a reply to message #536519] Tue, 01 June 2010 06:39 Go to previous messageGo to next message
A. Boughlam
Messages: 14
Registered: May 2010
Junior Member
Hi again,
I have another question if you don't mind, I need to modify some generation parameters (set the package path, use 'Lists' instead of 'Sets', no ModelFactory/ModelPackage classes ...) and i've seen that we can do this using annotations.
For a given ecore model, I'm using 'AnnotationModelGenerator' to generate programmatically an annotations model , so in the same way how can we annotate it ?
Sorry for the inconvenience and thanks in advance.


Best Regards,
A. Boughlam
Re: [Texo] Generating model code programmatically [message #537122 is a reply to message #537098] Tue, 01 June 2010 07:45 Go to previous messageGo to next message
Martin Taal is currently offline Martin Taal
Messages: 5300
Registered: July 2009
Senior Member
Hi,
No problem at all, just ask what you want :-)

Before doing modelController.annotate(...) (but after setting its EPackages), do something like this:

final ENamedElementAnnotation annotation = modelController.getAnnotation(ePackages.get(0),
ModelcodegeneratorPackage.eNS_URI);

this will give you the code generation annotation on the EPackage (an EPackageModelGenAnnotation instance). You can set
all kinds of information on this which is then used during the code generation.
The same works for all model elements (EClass, EDataType, EStructuralFeature, etc.) which will return an instance of
their resp. annotation (EClassModelGenAnnotation, EAttributeModelGenAnnotation, etc.).

The ModelcodegeneratorPackage.eNS_URI is used to filter which annotation to return (if the model was jaxb/orm annotated
this can be used to get the jaxb resp. orm annotation by passing the nsuri of their respective annotation models).

After your 'manual' settings of the annotation do the modelController.annotate(...) which will complete the annotation
model, while preserving/using your 'manual' changes.

After modelController.annotate(..) (and before calling the WorkFlowStarter) you can also change annotations, but at that
point all annotations have been generated. For example changing the package path in the packagemodelgenannotation (after
calling modelController.annotate(..)) won't change the qualified class name of eclassmodelgenannotations as these
annotations have already been computed.

gr. Martin

A. Boughlam wrote:
> Hi again,
> I have another question if you don't mind, I need to modify some
> generation parameters (set the package path, use 'Lists' instead of
> 'Sets', no ModelFactory/ModelPackage classes ...) and i've seen that we
> can do this using annotations. For a given ecore model, I'm using
> 'AnnotationModelGenerator' to generate programmatically an annotations
> model , so in the same way how can we annotate it ? Sorry for the
> inconvenience and thanks in advance.


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Re: [Texo] Generating model code programmatically [message #537124 is a reply to message #537122] Tue, 01 June 2010 08:04 Go to previous messageGo to next message
Martin Taal is currently offline Martin Taal
Messages: 5300
Registered: July 2009
Senior Member
btw, I noticed that if there are also annotation model files which are read then you can get epackage-not-found
exceptions. I had to make sure that the annotations model were initialized before getting the epackage model gen
annotation. Afaics this only happens if there are annotation model files (which are being read automatically) present.
If you don't have these then I think you won't be bothered by this

I had to add a few lines to get around this exception:

// code to prevent epackage not found exceptions:
ModelcodegeneratorPackage.eINSTANCE.getNsURI();
OrmannotationsPackage.eINSTANCE.getNsURI();
OrmPackage.eINSTANCE.getNsURI();
JaxbmodelannotationsPackage.eINSTANCE.getNsURI();
// get the annotation you want
final List<EPackage> ePackages = GeneratorUtils.readEPackages(uris,
GeneratorUtils.createEPackageRegistry());

final ModelController modelController = new ModelController();
modelController.setEPackages(ePackages);

final ENamedElementAnnotation annotation = modelController
.getAnnotation(ePackages.get(0),
ModelcodegeneratorPackage.eNS_URI);
System.err.println(annotation.getClass());
// do something great with the annotation here

modelController.annotate(new ArrayList<ModelAnnotator>());

final WorkFlowStarter wfs = new WorkFlowStarter();
wfs.generateModelCode(TEST_MODEL_PROJECT, ePackages,
modelController, "src-test-gen"); //$NON-NLS-1$


Martin Taal wrote:
> Hi,
> No problem at all, just ask what you want :-)
>
> Before doing modelController.annotate(...) (but after setting its
> EPackages), do something like this:
>
> final ENamedElementAnnotation annotation =
> modelController.getAnnotation(ePackages.get(0),
> ModelcodegeneratorPackage.eNS_URI);
>
> this will give you the code generation annotation on the EPackage (an
> EPackageModelGenAnnotation instance). You can set all kinds of
> information on this which is then used during the code generation.
> The same works for all model elements (EClass, EDataType,
> EStructuralFeature, etc.) which will return an instance of their resp.
> annotation (EClassModelGenAnnotation, EAttributeModelGenAnnotation, etc.).
>
> The ModelcodegeneratorPackage.eNS_URI is used to filter which annotation
> to return (if the model was jaxb/orm annotated this can be used to get
> the jaxb resp. orm annotation by passing the nsuri of their respective
> annotation models).
>
> After your 'manual' settings of the annotation do the
> modelController.annotate(...) which will complete the annotation model,
> while preserving/using your 'manual' changes.
>
> After modelController.annotate(..) (and before calling the
> WorkFlowStarter) you can also change annotations, but at that point all
> annotations have been generated. For example changing the package path
> in the packagemodelgenannotation (after calling
> modelController.annotate(..)) won't change the qualified class name of
> eclassmodelgenannotations as these annotations have already been computed.
>
> gr. Martin
>
> A. Boughlam wrote:
>> Hi again,
>> I have another question if you don't mind, I need to modify some
>> generation parameters (set the package path, use 'Lists' instead of
>> 'Sets', no ModelFactory/ModelPackage classes ...) and i've seen that
>> we can do this using annotations. For a given ecore model, I'm using
>> 'AnnotationModelGenerator' to generate programmatically an annotations
>> model , so in the same way how can we annotate it ? Sorry for the
>> inconvenience and thanks in advance.
>
>


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Re: [Texo] Generating model code programmatically [message #537454 is a reply to message #537124] Wed, 02 June 2010 10:02 Go to previous messageGo to next message
A. Boughlam
Messages: 14
Registered: May 2010
Junior Member
Thank you for your reply Martin, it's very helpful. It's just that I'm having a 'null' annotation when calling modelController.getAnnotation(ePackages.get(0), ModelcodegeneratorPackage.eNS_URI) . I presume he didn't find any annotation. Did I miss something ?


Best Regards,
A. Boughlam

[Updated on: Wed, 02 June 2010 10:12]

Report message to a moderator

Re: [Texo] Generating model code programmatically [message #537464 is a reply to message #537454] Wed, 02 June 2010 10:17 Go to previous messageGo to next message
Martin Taal is currently offline Martin Taal
Messages: 5300
Registered: July 2009
Senior Member
Hmm, I wonder why that happens, can you post some more code here (the part around/before the getAnnotation call)?

gr. Martin

A. Boughlam wrote:
> Thank you for your reply Martin, it's very helpful. It's just that I'm
> having a 'null' annotation when calling
> modelController.getAnnotation(ePackages.get(0),
> ModelcodegeneratorPackage.eNS_URI) . Do you have any idea on what could
> be the cause ?
>


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Re: [Texo] Generating model code programmatically [message #537471 is a reply to message #537464] Wed, 02 June 2010 10:51 Go to previous messageGo to next message
A. Boughlam
Messages: 14
Registered: May 2010
Junior Member
Here it is :

...
ModelcodegeneratorPackage.eINSTANCE.getNsURI();

List<URI> uris = new ArrayList<URI>();
uris.add(ecorePath.toFile().toURI());

final List<EPackage> ePackages = GeneratorUtils.readEPackages(uris,GeneratorUtils.createEPackageRegistry());

final ModelController modelController = new ModelController();
modelController.setEPackages(ePackages);
	    
final ENamedElementAnnotation annotation = modelController.getAnnotation(ePackages.get(0), ModelcodegeneratorPackage.eNS_URI);
System.err.println(annotation.getClass());

modelController.annotate(new ArrayList<ModelAnnotator>());

WorkFlowStarter wfs = new WorkFlowStarter();
...




Best Regards,
A. Boughlam
Re: [Texo] Generating model code programmatically [message #537479 is a reply to message #537471] Wed, 02 June 2010 11:05 Go to previous messageGo to next message
Martin Taal is currently offline Martin Taal
Messages: 5300
Registered: July 2009
Senior Member
Sorry about that, in my case it worked because I had an annotation model file for the test case.

The one that works always is this one:

final ENamedElementAnnotation annotation = modelController.getAnnotationManager().getAnnotation(
ePackages.get(0),
ModelcodegeneratorPackage.eINSTANCE
.getEPackageModelGenAnnotation());

The api is slightly different the annotationManager.getAnnotation() expects a ENamedElement and the EClass of the
annotation you want to get back.

gr. Martin

A. Boughlam wrote:
> Here it is :
>
> ...
> ModelcodegeneratorPackage.eINSTANCE.getNsURI();
>
> List<URI> uris = new ArrayList<URI>();
> uris.add(ecorePath.toFile().toURI());
>
> final List<EPackage> ePackages =
> GeneratorUtils.readEPackages(uris,GeneratorUtils.createEPack ageRegistry());
>
> final ModelController modelController = new ModelController();
> modelController.setEPackages(ePackages);
> final ENamedElementAnnotation annotation =
> modelController.getAnnotation(ePackages.get(0),
> ModelcodegeneratorPackage.eNS_URI);
> System.err.println(annotation.getClass());
>
> modelController.annotate(new ArrayList<ModelAnnotator>());
>
> WorkFlowStarter wfs = new WorkFlowStarter();
> ...
>
>


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Re: [Texo] Generating model code programmatically [message #537484 is a reply to message #537479] Wed, 02 June 2010 11:15 Go to previous messageGo to next message
Martin Taal is currently offline Martin Taal
Messages: 5300
Registered: July 2009
Senior Member
Hmm, after some more testing it seems you will get a NPE later on. I will change the api a bit to make this easier to
work with.

gr. Martin

Martin Taal wrote:
> Sorry about that, in my case it worked because I had an annotation model
> file for the test case.
>
> The one that works always is this one:
>
> final ENamedElementAnnotation annotation =
> modelController.getAnnotationManager().getAnnotation(
> ePackages.get(0),
> ModelcodegeneratorPackage.eINSTANCE
> .getEPackageModelGenAnnotation());
>
> The api is slightly different the annotationManager.getAnnotation()
> expects a ENamedElement and the EClass of the annotation you want to get
> back.
>
> gr. Martin
>
> A. Boughlam wrote:
>> Here it is :
>>
>> ...
>> ModelcodegeneratorPackage.eINSTANCE.getNsURI();
>>
>> List<URI> uris = new ArrayList<URI>();
>> uris.add(ecorePath.toFile().toURI());
>>
>> final List<EPackage> ePackages =
>> GeneratorUtils.readEPackages(uris,GeneratorUtils.createEPack ageRegistry());
>>
>>
>> final ModelController modelController = new ModelController();
>> modelController.setEPackages(ePackages);
>> final ENamedElementAnnotation annotation =
>> modelController.getAnnotation(ePackages.get(0),
>> ModelcodegeneratorPackage.eNS_URI);
>> System.err.println(annotation.getClass());
>>
>> modelController.annotate(new ArrayList<ModelAnnotator>());
>>
>> WorkFlowStarter wfs = new WorkFlowStarter();
>> ...
>>
>>
>
>


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Re: [Texo] Generating model code programmatically [message #537493 is a reply to message #537484] Wed, 02 June 2010 11:48 Go to previous messageGo to next message
A. Boughlam
Messages: 14
Registered: May 2010
Junior Member
Ok I see, anyway thank you Martin for your time and keep up the good work Thumbs Up

Best Regards,
A. Boughlam

[Updated on: Wed, 02 June 2010 11:50]

Report message to a moderator

Re: [Texo] Generating model code programmatically [message #537505 is a reply to message #537493] Wed, 02 June 2010 12:35 Go to previous message
Martin Taal is currently offline Martin Taal
Messages: 5300
Registered: July 2009
Senior Member
Yaw :-), as Texo is somewhat new I am very interested in user feedback for improvements.

I published a new build a few mins back which solves the issue I encountered. The annotation you get back is empty (none
of its features is set). You can get a fully set annotation back by doing this first:
modelController.getAnnotationManager().addAnnotators(
ModelAnnotator.getCodeGenAnnotator());

before getting the annotation

gr. Martin

A. Boughlam wrote:
> Thank you Martin for your time and good work by the way :thumbup:


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Re: [Texo] Generating model code programmatically [message #622954 is a reply to message #536519] Fri, 28 May 2010 16:07 Go to previous message
Martin Taal is currently offline Martin Taal
Messages: 5300
Registered: July 2009
Senior Member
Hi,
I can give you some pointers but the Texo code generation requires some specific parts of the eclipse IDE to do import
resolving and code formatting, in addition Texo requires a java project to generate code into. As part of the test run
Texo generates code using an eclipse headless run and a ready to use workspace. In all it is pretty doable to set this
up, but it is not as easy as calling Texo directly from an ant script.

The Texo model generator test project is available in cvs here:
dev.eclipse.org
/cvsroot/modeling
org.eclipse.emf/org.eclipse.emf.texo/tests/org.eclipse.emf.t exo.modelgenerator.test

The actual test code to generate is fairly simple:

final ModelController modelController = new ModelController();
modelController.setEPackages(ePackages);
modelController.annotate(new ArrayList<ModelAnnotator>());

final WorkFlowStarter wfs = new WorkFlowStarter();
wfs.generateModelCode(TEST_MODEL_PROJECT, ePackages, modelController, "src-test-gen"); //$NON-NLS-1$

where TEST_MODEL_PROJECT is the name of a java project in the workspace and src-test-gen the target folder in that
project. The ePackages is a list of EPackage instances, the modelController is a specific Texo class.

gr. Martin

A. Boughlam wrote:
> I've just discovered Texo and i'm wondering how to generate pojo classes
> from an ecore model programmatically ? If someone can give me a lead on
> this. Thanks in advance.


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Re: [Texo] Generating model code programmatically [message #622955 is a reply to message #536595] Mon, 31 May 2010 04:32 Go to previous message
A. Boughlam
Messages: 14
Registered: May 2010
Junior Member
Thank you Martin, exactly what I needed.
--
Best Regards,
A. Boughlam


Best Regards,
A. Boughlam
Re: [Texo] Generating model code programmatically [message #622956 is a reply to message #622955] Mon, 31 May 2010 05:12 Go to previous message
Martin Taal is currently offline Martin Taal
Messages: 5300
Registered: July 2009
Senior Member
As an additional remark, the model generator test project contains launch configurations which run the model generator
for a certain workspace. Texo uses Buckminster in its build environment which makes it pretty easy to run launch
configurations.

gr. Martin

A. Boughlam wrote:
> Thank you Martin, exactly what I needed.


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Re: [Texo] Generating model code programmatically [message #622963 is a reply to message #536519] Tue, 01 June 2010 06:39 Go to previous message
A. Boughlam
Messages: 14
Registered: May 2010
Junior Member
Hi again,
I have another question if you don't mind, I need to modify some generation parameters (set the package path, use 'Lists' instead of 'Sets', no ModelFactory/ModelPackage classes ...) and i've seen that we can do this using annotations.
For a given ecore model, I'm using 'AnnotationModelGenerator' to generate programmatically an annotations model , so in the same way how can we annotate it ?
Sorry for the inconvenience and thanks in advance.
--
Best Regards,
A. Boughlam


Best Regards,
A. Boughlam
Re: [Texo] Generating model code programmatically [message #622964 is a reply to message #622963] Tue, 01 June 2010 07:45 Go to previous message
Martin Taal is currently offline Martin Taal
Messages: 5300
Registered: July 2009
Senior Member
Hi,
No problem at all, just ask what you want :-)

Before doing modelController.annotate(...) (but after setting its EPackages), do something like this:

final ENamedElementAnnotation annotation = modelController.getAnnotation(ePackages.get(0),
ModelcodegeneratorPackage.eNS_URI);

this will give you the code generation annotation on the EPackage (an EPackageModelGenAnnotation instance). You can set
all kinds of information on this which is then used during the code generation.
The same works for all model elements (EClass, EDataType, EStructuralFeature, etc.) which will return an instance of
their resp. annotation (EClassModelGenAnnotation, EAttributeModelGenAnnotation, etc.).

The ModelcodegeneratorPackage.eNS_URI is used to filter which annotation to return (if the model was jaxb/orm annotated
this can be used to get the jaxb resp. orm annotation by passing the nsuri of their respective annotation models).

After your 'manual' settings of the annotation do the modelController.annotate(...) which will complete the annotation
model, while preserving/using your 'manual' changes.

After modelController.annotate(..) (and before calling the WorkFlowStarter) you can also change annotations, but at that
point all annotations have been generated. For example changing the package path in the packagemodelgenannotation (after
calling modelController.annotate(..)) won't change the qualified class name of eclassmodelgenannotations as these
annotations have already been computed.

gr. Martin

A. Boughlam wrote:
> Hi again,
> I have another question if you don't mind, I need to modify some
> generation parameters (set the package path, use 'Lists' instead of
> 'Sets', no ModelFactory/ModelPackage classes ...) and i've seen that we
> can do this using annotations. For a given ecore model, I'm using
> 'AnnotationModelGenerator' to generate programmatically an annotations
> model , so in the same way how can we annotate it ? Sorry for the
> inconvenience and thanks in advance.


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Re: [Texo] Generating model code programmatically [message #622965 is a reply to message #537122] Tue, 01 June 2010 08:04 Go to previous message
Martin Taal is currently offline Martin Taal
Messages: 5300
Registered: July 2009
Senior Member
btw, I noticed that if there are also annotation model files which are read then you can get epackage-not-found
exceptions. I had to make sure that the annotations model were initialized before getting the epackage model gen
annotation. Afaics this only happens if there are annotation model files (which are being read automatically) present.
If you don't have these then I think you won't be bothered by this

I had to add a few lines to get around this exception:

// code to prevent epackage not found exceptions:
ModelcodegeneratorPackage.eINSTANCE.getNsURI();
OrmannotationsPackage.eINSTANCE.getNsURI();
OrmPackage.eINSTANCE.getNsURI();
JaxbmodelannotationsPackage.eINSTANCE.getNsURI();
// get the annotation you want
final List<EPackage> ePackages = GeneratorUtils.readEPackages(uris,
GeneratorUtils.createEPackageRegistry());

final ModelController modelController = new ModelController();
modelController.setEPackages(ePackages);

final ENamedElementAnnotation annotation = modelController
.getAnnotation(ePackages.get(0),
ModelcodegeneratorPackage.eNS_URI);
System.err.println(annotation.getClass());
// do something great with the annotation here

modelController.annotate(new ArrayList<ModelAnnotator>());

final WorkFlowStarter wfs = new WorkFlowStarter();
wfs.generateModelCode(TEST_MODEL_PROJECT, ePackages,
modelController, "src-test-gen"); //$NON-NLS-1$


Martin Taal wrote:
> Hi,
> No problem at all, just ask what you want :-)
>
> Before doing modelController.annotate(...) (but after setting its
> EPackages), do something like this:
>
> final ENamedElementAnnotation annotation =
> modelController.getAnnotation(ePackages.get(0),
> ModelcodegeneratorPackage.eNS_URI);
>
> this will give you the code generation annotation on the EPackage (an
> EPackageModelGenAnnotation instance). You can set all kinds of
> information on this which is then used during the code generation.
> The same works for all model elements (EClass, EDataType,
> EStructuralFeature, etc.) which will return an instance of their resp.
> annotation (EClassModelGenAnnotation, EAttributeModelGenAnnotation, etc.).
>
> The ModelcodegeneratorPackage.eNS_URI is used to filter which annotation
> to return (if the model was jaxb/orm annotated this can be used to get
> the jaxb resp. orm annotation by passing the nsuri of their respective
> annotation models).
>
> After your 'manual' settings of the annotation do the
> modelController.annotate(...) which will complete the annotation model,
> while preserving/using your 'manual' changes.
>
> After modelController.annotate(..) (and before calling the
> WorkFlowStarter) you can also change annotations, but at that point all
> annotations have been generated. For example changing the package path
> in the packagemodelgenannotation (after calling
> modelController.annotate(..)) won't change the qualified class name of
> eclassmodelgenannotations as these annotations have already been computed.
>
> gr. Martin
>
> A. Boughlam wrote:
>> Hi again,
>> I have another question if you don't mind, I need to modify some
>> generation parameters (set the package path, use 'Lists' instead of
>> 'Sets', no ModelFactory/ModelPackage classes ...) and i've seen that
>> we can do this using annotations. For a given ecore model, I'm using
>> 'AnnotationModelGenerator' to generate programmatically an annotations
>> model , so in the same way how can we annotate it ? Sorry for the
>> inconvenience and thanks in advance.
>
>


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Re: [Texo] Generating model code programmatically [message #622969 is a reply to message #537124] Wed, 02 June 2010 10:02 Go to previous message
A. Boughlam
Messages: 14
Registered: May 2010
Junior Member
Thank you for your reply Martin, it's very helpful. It's just that I'm having a 'null' annotation when calling modelController.getAnnotation(ePackages.get(0), ModelcodegeneratorPackage.eNS_URI) . Do you have any idea on what could be the cause ?

--
Best Regards,
A. Boughlam


Best Regards,
A. Boughlam
Re: [Texo] Generating model code programmatically [message #622970 is a reply to message #622969] Wed, 02 June 2010 10:17 Go to previous message
Martin Taal is currently offline Martin Taal
Messages: 5300
Registered: July 2009
Senior Member
Hmm, I wonder why that happens, can you post some more code here (the part around/before the getAnnotation call)?

gr. Martin

A. Boughlam wrote:
> Thank you for your reply Martin, it's very helpful. It's just that I'm
> having a 'null' annotation when calling
> modelController.getAnnotation(ePackages.get(0),
> ModelcodegeneratorPackage.eNS_URI) . Do you have any idea on what could
> be the cause ?
>


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Re: [Texo] Generating model code programmatically [message #622971 is a reply to message #537464] Wed, 02 June 2010 10:51 Go to previous message
A. Boughlam
Messages: 14
Registered: May 2010
Junior Member
Here it is :

...
ModelcodegeneratorPackage.eINSTANCE.getNsURI();

List<URI> uris = new ArrayList<URI>();
uris.add(ecorePath.toFile().toURI());

final List<EPackage> ePackages = GeneratorUtils.readEPackages(uris,GeneratorUtils.createEPack ageRegistry());

final ModelController modelController = new ModelController();
modelController.setEPackages(ePackages);

final ENamedElementAnnotation annotation = modelController.getAnnotation(ePackages.get(0), ModelcodegeneratorPackage.eNS_URI);
System.err.println(annotation.getClass());

modelController.annotate(new ArrayList<ModelAnnotator>());

WorkFlowStarter wfs = new WorkFlowStarter();
...


--
Best Regards,
A. Boughlam


Best Regards,
A. Boughlam
Re: [Texo] Generating model code programmatically [message #622972 is a reply to message #622971] Wed, 02 June 2010 11:05 Go to previous message
Martin Taal is currently offline Martin Taal
Messages: 5300
Registered: July 2009
Senior Member
Sorry about that, in my case it worked because I had an annotation model file for the test case.

The one that works always is this one:

final ENamedElementAnnotation annotation = modelController.getAnnotationManager().getAnnotation(
ePackages.get(0),
ModelcodegeneratorPackage.eINSTANCE
.getEPackageModelGenAnnotation());

The api is slightly different the annotationManager.getAnnotation() expects a ENamedElement and the EClass of the
annotation you want to get back.

gr. Martin

A. Boughlam wrote:
> Here it is :
>
> ...
> ModelcodegeneratorPackage.eINSTANCE.getNsURI();
>
> List<URI> uris = new ArrayList<URI>();
> uris.add(ecorePath.toFile().toURI());
>
> final List<EPackage> ePackages =
> GeneratorUtils.readEPackages(uris,GeneratorUtils.createEPack ageRegistry());
>
> final ModelController modelController = new ModelController();
> modelController.setEPackages(ePackages);
> final ENamedElementAnnotation annotation =
> modelController.getAnnotation(ePackages.get(0),
> ModelcodegeneratorPackage.eNS_URI);
> System.err.println(annotation.getClass());
>
> modelController.annotate(new ArrayList<ModelAnnotator>());
>
> WorkFlowStarter wfs = new WorkFlowStarter();
> ...
>
>


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Re: [Texo] Generating model code programmatically [message #622973 is a reply to message #537479] Wed, 02 June 2010 11:15 Go to previous message
Martin Taal is currently offline Martin Taal
Messages: 5300
Registered: July 2009
Senior Member
Hmm, after some more testing it seems you will get a NPE later on. I will change the api a bit to make this easier to
work with.

gr. Martin

Martin Taal wrote:
> Sorry about that, in my case it worked because I had an annotation model
> file for the test case.
>
> The one that works always is this one:
>
> final ENamedElementAnnotation annotation =
> modelController.getAnnotationManager().getAnnotation(
> ePackages.get(0),
> ModelcodegeneratorPackage.eINSTANCE
> .getEPackageModelGenAnnotation());
>
> The api is slightly different the annotationManager.getAnnotation()
> expects a ENamedElement and the EClass of the annotation you want to get
> back.
>
> gr. Martin
>
> A. Boughlam wrote:
>> Here it is :
>>
>> ...
>> ModelcodegeneratorPackage.eINSTANCE.getNsURI();
>>
>> List<URI> uris = new ArrayList<URI>();
>> uris.add(ecorePath.toFile().toURI());
>>
>> final List<EPackage> ePackages =
>> GeneratorUtils.readEPackages(uris,GeneratorUtils.createEPack ageRegistry());
>>
>>
>> final ModelController modelController = new ModelController();
>> modelController.setEPackages(ePackages);
>> final ENamedElementAnnotation annotation =
>> modelController.getAnnotation(ePackages.get(0),
>> ModelcodegeneratorPackage.eNS_URI);
>> System.err.println(annotation.getClass());
>>
>> modelController.annotate(new ArrayList<ModelAnnotator>());
>>
>> WorkFlowStarter wfs = new WorkFlowStarter();
>> ...
>>
>>
>
>


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Re: [Texo] Generating model code programmatically [message #622974 is a reply to message #537484] Wed, 02 June 2010 11:48 Go to previous message
A. Boughlam
Messages: 14
Registered: May 2010
Junior Member
Thank you Martin for your time and good work by the way :thumbup:
--
Best Regards,
A. Boughlam


Best Regards,
A. Boughlam
Re: [Texo] Generating model code programmatically [message #622976 is a reply to message #622974] Wed, 02 June 2010 12:35 Go to previous message
Martin Taal is currently offline Martin Taal
Messages: 5300
Registered: July 2009
Senior Member
Yaw :-), as Texo is somewhat new I am very interested in user feedback for improvements.

I published a new build a few mins back which solves the issue I encountered. The annotation you get back is empty (none
of its features is set). You can get a fully set annotation back by doing this first:
modelController.getAnnotationManager().addAnnotators(
ModelAnnotator.getCodeGenAnnotator());

before getting the annotation

gr. Martin

A. Boughlam wrote:
> Thank you Martin for your time and good work by the way :thumbup:


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Previous Topic:[EEF] //FIXME INVALID CASE INTO template public
Next Topic:EMF Compare NPE
Goto Forum:
  


Current Time: Fri Apr 18 06:36:34 EDT 2014

Powered by FUDForum. Page generated in 0.04495 seconds