Home » Modeling » Epsilon » Populate model with values stored written in a file
| | |
Re: Populate model with values stored written in a file [message #1777551 is a reply to message #1777446] |
Thu, 30 November 2017 04:53   |
Eclipse User |
|
|
|
Hi,
I guess something like the following will work.
1. Create a simple EMF meta model using Emfatic[1], and generate the .ecore from it (right click->Generate Ecore Model)
@namespace(
uri="http://example.org/epsilon/emc/csv/people.ecore",
prefix="p")
package top;
class Person {
id attr Integer ~id;
attr String first_name;
attr String last_name;
attr String[*] movies;
}
2. Have some data in CSV format:
id,first_name,last_name,movies
604-78-8459,Ricoriki,Dwyr,Horror
272-41-1349,Norry,Halpin,Drama,Film-Noir,Thriller
844-07-0023,Matteo,Macer,Horror,Mystery,Thriller
429-41-4964,Kattie,Fysh,Comedy
378-90-9530,Link,Proffitt,Drama
811-26-0387,Rafferty,Sobieski,Horror
386-53-1139,Ernestine,Kringe,Drama
850-05-5333,Flossy,Mobberley,Comedy,Romance
605-52-9809,Tull,Ingerithec,Drama
580-79-7291,Derry,Laurisch,Drama,War
676-89-8860,Cosetta,Vlasov,Crime,Film-Noir,Thriller
748-10-2370,Lissa,Stanger,Action,Adventure,Thriller
164-18-3409,Giffie,Boards,Comedy
212-06-7778,Rabbi,Varran,Horror
628-02-3617,Olvan,Alabone,Action,Adventure,Sci-Fi,Thriller
318-48-3006,Constantino,Eyckelbeck,War
122-74-6759,Nickolas,Collard,Drama,Fantasy,Mystery
309-57-3090,Chere,Hurry,Drama
833-32-9040,Mattie,Hamon,Drama
101-82-2564,Hew,Goble,Comedy
3. A simple ETL transformation that copies the fields from CSV to EMF. Here I use the fact that the CSV file has headers, if not you would have to use the "fields" field and access by index (example added in comments):
rule Row2Object
transform r:csv!Row
to p:emf!Person {
p.first_name = r.first_name; // p.first_name = r.fields.at(0);
p.last_name = r.last_name;
p.movies.addAll(r.movies);
}
4. In the launch configuration
a. Add a CSV model (need to tick the "Show all model types" box to see the CSV model type). I have used "csv" as the model name. This should probably be "Read on load" only. Also, check the "Known headers" box if you have headers (and the varargs box for this particular case).
b. Add an EMF model. Select your .model file (or type its path if it does not exist) and then add the .ecore meta model with the "Add file ..." button. I have used "emf" for the model name. This model should probably be "Store on Disposal" only.
5. Run the transformation. Your .model file should be populated.
Voila!
(I will be adding this example to the examples projects in due time if you will like to try it out).
[1] https://www.eclipse.org/emfatic/
|
|
| |
Goto Forum:
Current Time: Wed Jul 23 09:48:42 EDT 2025
Powered by FUDForum. Page generated in 0.40589 seconds
|