Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Generating code from attributes in CSV
Generating code from attributes in CSV [message #1822231] Mon, 02 March 2020 03:46 Go to next message
Eclipse UserFriend
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 04:11 Go to previous messageGo to next message
Eclipse UserFriend
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
Re: Generating code from attributes in CSV [message #1822235 is a reply to message #1822233] Mon, 02 March 2020 04:21 Go to previous messageGo to next message
Eclipse UserFriend
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 04:36 Go to previous messageGo to next message
Eclipse UserFriend
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 07:02 Go to previous messageGo to next message
Eclipse UserFriend
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 07:02] by Moderator

Re: Generating code from attributes in CSV [message #1822308 is a reply to message #1822296] Tue, 03 March 2020 12:33 Go to previous messageGo to next message
Eclipse UserFriend
What is your exact question
Re: Generating code from attributes in CSV [message #1822321 is a reply to message #1822308] Tue, 03 March 2020 21:45 Go to previous messageGo to next message
Eclipse UserFriend
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] Tue, 03 March 2020 23:56 Go to previous messageGo to next message
Eclipse UserFriend
I don't see the rules for your Csv and generation yet
Re: Generating code from attributes in CSV [message #1822343 is a reply to message #1822326] Wed, 04 March 2020 08:56 Go to previous messageGo to next message
Eclipse UserFriend
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 09:44 Go to previous messageGo to next message
Eclipse UserFriend
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
Re: Generating code from attributes in CSV [message #1822459 is a reply to message #1822346] Sat, 07 March 2020 07:10 Go to previous messageGo to next message
Eclipse UserFriend
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 07:11] by Moderator

Re: Generating code from attributes in CSV [message #1822465 is a reply to message #1822459] Sat, 07 March 2020 11:30 Go to previous messageGo to next message
Eclipse UserFriend
Imdont know any
Re: Generating code from attributes in CSV [message #1822489 is a reply to message #1822465] Sun, 08 March 2020 05:50 Go to previous message
Eclipse UserFriend
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: Wed Jul 23 16:23:28 EDT 2025

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

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

Back to the top