|Re: Modify an EMF model programmatically [message #1133556 is a reply to message #1133364]
||Fri, 11 October 2013 23:35
| Mauro Antonaci
Registered: February 2010
There are several ways to do what is stated in the subject of your question. The best choice is often a matter of taste, of different skills about the different emf-related technologies that can be used to accomplish the job, and, last but not least, it depends on the specific problem/context/constraints. |
In a typical scenario you may need to programmatically change the ecore model in the context of a model-to-model transformation workflow: in this case you probably prefer an m2m technology to change the ecore model (xtend, epsilon, ...). Example: the transformation workflow must read some xsd-based models and transform them into a new ecore model.
In another typical scenario you may need to do the job in the context of an interactive wizard (for example you may need to select and pick several xml files and build a new ecore model based on the contents of such files). In this case you probably prefer to work directly at the java/ecore level.
You will notice that each scenario I used in my explanation, includes the same main-job: merge some xml file into a new ecore model. The difference is the context (an interactive wizard vs a transformation workflow) and this difference often lead to a different choice of technology.
And the scenario also determines the way to perform the corollary jobs: how to read an intial non empty target model? how to read the source models? how to store the target model in a resource?, ...
I will suppose that your secenario is the one of the wizard, so I will try to suggest some solution for each subtask.
How to read/create the target model
If you need to create a new target model you must:
- identify the path where you must store the model
- create a ResourceSet
- create a Resource in the choosen location
- create the root element of the target model, using the factory classes provided by the EMFgenerated java code.
- add the root element as a content in the new Resource
If the target model is not new you must:
- identify the path where the source model is stored
- read the resource
How to change the ecore model programmatically
The java classes generated from an ecore metamodel are plain java classes. You must perform the transformation using getters and setters, and you must use the factory to create nel elements
How to store the modified result
In each scenario (new initial model vs non-empty initial model) when the job ends, the target model is associated to a Resource: to store it in the file system you simply have to call the methods of the Resource class.
Powered by FUDForum
. Page generated in 0.02179 seconds