Home » Modeling » EMF "Technology" (Ecore Tools, EMFatic, etc) » [Texo] Generating model code programmatically
|
Re: [Texo] Generating model code programmatically [message #536595 is a reply to message #536519] |
Fri, 28 May 2010 20:07 |
Martin Taal Messages: 5468 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 #537098 is a reply to message #536519] |
Tue, 01 June 2010 10:39 |
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 11:45 |
Martin Taal Messages: 5468 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 12:04 |
Martin Taal Messages: 5468 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 #537471 is a reply to message #537464] |
Wed, 02 June 2010 14:51 |
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 #622954 is a reply to message #536519] |
Fri, 28 May 2010 20:07 |
Martin Taal Messages: 5468 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 #622963 is a reply to message #536519] |
Tue, 01 June 2010 10:39 |
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 11:45 |
Martin Taal Messages: 5468 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 12:04 |
Martin Taal Messages: 5468 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 14:02 |
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 #622971 is a reply to message #537464] |
Wed, 02 June 2010 14:51 |
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
|
|
| | | | |
Goto Forum:
Current Time: Tue Sep 24 15:47:58 GMT 2024
Powered by FUDForum. Page generated in 0.10119 seconds
|