Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Generating code from attributes in CSV
Generating code from attributes in CSV [message #1822231] Mon, 02 March 2020 08:46 Go to next message
Mohan Radhakrishnan is currently offline Mohan RadhakrishnanFriend
Messages: 19
Registered: July 2009
Junior Member
Hi,

I've looked at XText and XTend sample DSL's and executed in eclipse. Now I want to work with some CSV files that contain names of methods that generated code should use.

So, for example, this is the sample CSV.

Capital,,Capital,,,,,,,,,,,
Equity 1,,,equity1,,,,,,,,,,
Iinstruments,,,,capital,,,,,Currency,,,,
Premium,,,,premium,,,,,Currency,,,,
Earnings,,,,earnings,,,,,Currency,,,,

Can I create a DSL that generated code based on this ?

(e.g) Capital c = new Capital();
System.out.println( c.equity1().capital() );
System.out.println( c.equity1().premium() );
System.out.println( c.equity1().earnings() );

I can further enhance using using XTend if needed. I think. But XText is the part that deals with the DSL to handle this. Right ?

Thanks,
Mohan
Re: Generating code from attributes in CSV [message #1822233 is a reply to message #1822231] Mon, 02 March 2020 09:11 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13628
Registered: July 2009
Senior Member
i am not sure if you need xtext at all for a loose format like csv or if just writing a code generator would be better

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Generating code from attributes in CSV [message #1822235 is a reply to message #1822233] Mon, 02 March 2020 09:21 Go to previous messageGo to next message
Mohan Radhakrishnan is currently offline Mohan RadhakrishnanFriend
Messages: 19
Registered: July 2009
Junior Member
Do you mean that XTend can directly do that ? If so then I would like some advice.

But even though I have shown very simple code the type of data handled by each method would need validators to be written later.

Thanks.
Re: Generating code from attributes in CSV [message #1822237 is a reply to message #1822231] Mon, 02 March 2020 09:36 Go to previous messageGo to next message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 739
Registered: July 2009
Location: Dortmund, Germany
Senior Member

Xtend is "just" a programming language ideal to write code generators. You don't need Xtext models as input, it can be also an object structure that you create with a CSV parser.

You may get pointers from this talk: https://de.slideshare.net/kthoms/recipes-to-build-code-generators-for-nonxtext-models-with-xtend
It is also available as record from our EclipseCon presentation: https://www.youtube.com/watch?v=teOULtQ81-U
Re: Generating code from attributes in CSV [message #1822296 is a reply to message #1822237] Tue, 03 March 2020 12:02 Go to previous messageGo to next message
Mohan Radhakrishnan is currently offline Mohan RadhakrishnanFriend
Messages: 19
Registered: July 2009
Junior Member
Thanks. I am able to generate some code now. But I still need to read the .csv file and use the tokens as class and method names.

I am using an XTend project. No XText here.

Mohan

[Updated on: Tue, 03 March 2020 12:02]

Report message to a moderator

Re: Generating code from attributes in CSV [message #1822308 is a reply to message #1822296] Tue, 03 March 2020 17:33 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13628
Registered: July 2009
Senior Member
What is your exact question

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Generating code from attributes in CSV [message #1822321 is a reply to message #1822308] Wed, 04 March 2020 02:45 Go to previous messageGo to next message
Mohan Radhakrishnan is currently offline Mohan RadhakrishnanFriend
Messages: 19
Registered: July 2009
Junior Member
Hello Christian,

This was my original question.

So, for example, this is the sample CSV. I am led to believe that this doesn't need a DSL atleast at this time. These are the
steps I envisage. I am asking about Step 1 here. But if you have ideas about Step 2 you could share. I have the XTend domain
modeling book too.

Step 1 :
1. Create an XTend project.
2. Read CSV and use the tokens to create a template and generate code.

Step 2 :
1. If I need to really create a DSL from the CSV so that I can have some more semantics like
Hiberate validator code generated using XText and XTend I have to use both XText and XTend

Capital,,Capital,,,,,,,,,,,
Equity 1,,,equity1,,,,,,,,,,
Iinstruments,,,,capital,,,,,Currency,,,,
Premium,,,,premium,,,,,Currency,,,,
Earnings,,,,earnings,,,,,Currency,,,,

I want to generate code based on this.

(e.g) Capital c = new Capital();
System.out.println( c.equity1().capital() );
System.out.println( c.equity1().premium() );
System.out.println( c.equity1().earnings() );
Re: Generating code from attributes in CSV [message #1822326 is a reply to message #1822321] Wed, 04 March 2020 04:56 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13628
Registered: July 2009
Senior Member
I don't see the rules for your Csv and generation yet

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Generating code from attributes in CSV [message #1822343 is a reply to message #1822326] Wed, 04 March 2020 13:56 Go to previous messageGo to next message
Mohan Radhakrishnan is currently offline Mohan RadhakrishnanFriend
Messages: 19
Registered: July 2009
Junior Member
Capital,,Capital,,,,,,,,,,,
Equity 1,,,equity1,,,,,,,,,,
Iinstruments,,,,capital,,,,,Currency,,,,
Premium,,,,premium,,,,,Currency,,,,
Earnings,,,,earnings,,,,,Currency,,,,

The first line is the root POJO. Second line is the nested POJO. All other lines are 'getters' and 'setters'. Each such properly
returns a data type - int,long, double. Once I know how to use this I can add mocks and other testing techniques. That is what I think.
There is another CSV that contains the messages returned when a wrong data type is used. These two CSV's will be used.

I thought in the next step if I have a DSL based on this CSV I could work directly with it.

Re: Generating code from attributes in CSV [message #1822346 is a reply to message #1822343] Wed, 04 March 2020 14:44 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13628
Registered: July 2009
Senior Member
this might be possible if you can distinguish the different things by the empty columns and if they are different.
but you want get great editing experience


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Generating code from attributes in CSV [message #1822459 is a reply to message #1822346] Sat, 07 March 2020 12:10 Go to previous messageGo to next message
Mohan Radhakrishnan is currently offline Mohan RadhakrishnanFriend
Messages: 19
Registered: July 2009
Junior Member
Ok. Can I look at an example for direct XTend code generation from a text file like CSV then ?

Thanks.

[Updated on: Sat, 07 March 2020 12:11]

Report message to a moderator

Re: Generating code from attributes in CSV [message #1822465 is a reply to message #1822459] Sat, 07 March 2020 16:30 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13628
Registered: July 2009
Senior Member
Imdont know any

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Generating code from attributes in CSV [message #1822489 is a reply to message #1822465] Sun, 08 March 2020 09:50 Go to previous message
Tamas Miklossy is currently offline Tamas MiklossyFriend
Messages: 114
Registered: February 2016
Senior Member
Hi Mohan,

you may get some inspiration from the following projects:
- https://github.com/kthoms/xtend-codegen-examples
- https://github.com/itemis/itemis-blog/tree/combining-emf-models-with-xtext-dsls

Hope that helps,
Tamás
Previous Topic:xtext grammar
Next Topic:Xtext Embedded editor not shown in wizard
Goto Forum:
  


Current Time: Fri Oct 23 03:18:15 GMT 2020

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

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

Back to the top