Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to iterate in a collection using Xtend?
How to iterate in a collection using Xtend? [message #911044] Mon, 10 September 2012 21:49 Go to next message
Cristiano Gavião is currently offline Cristiano GaviãoFriend
Messages: 279
Registered: July 2009
Senior Member
Hi,

I have this model:
Examples:
	'Examples:'
	table=Table
;
Table hidden(WS, SL_COMMENT):
	{Table}
	(rows+=TableRow (NEWLINE rows+=TableRow)*)
	NEWLINE?
;
TableRow:
	{TableRow}
	('|' (cells += TableCell ('|' cells += TableCell)*) '|')
;
TableCell:
	name = Content
;


and this text:
Examples:
|h1|h2|h3|
|cell11|cell12|cell13|
|cell21|cell22|cell23|


I need to to assert that each cell has the corrected value.

Could someone point me how to do this with Xtend?

I tried the docs and know that exists IterableExtensions but couldn't find any example how to use it properly.

thanks in advance.

Cristiano
Re: How to iterate in a collection using Xtend? [message #911065 is a reply to message #911044] Mon, 10 September 2012 22:47 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3108
Registered: July 2009
Senior Member
Am 10.09.12 23:49, schrieb Cristiano Gaviao:
> Hi,
> I have this model:
>
> Examples:
> 'Examples:'
> table=Table
> ;
> Table hidden(WS, SL_COMMENT):
> {Table}
> (rows+=TableRow (NEWLINE rows+=TableRow)*)
> NEWLINE?
> ;
> TableRow:
> {TableRow}
> ('|' (cells += TableCell ('|' cells += TableCell)*) '|')
> ;
> TableCell:
> name = Content
> ;
>
>
> and this text:
>
> Examples:
> |h1|h2|h3|
> |cell11|cell12|cell13|
> |cell21|cell22|cell23|
>
>
> I need to to assert that each cell has the corrected value.
>
> Could someone point me how to do this with Xtend?
>
> I tried the docs and know that exists IterableExtensions but couldn't
> find any example how to use it properly.
>
> thanks in advance.
>
> Cristiano
>

You can of course use a for loop, e.g.

for(row: table.rows) {
for(cell: row.cells) {
..
}
}

There are other means like

table.rows.forEach [ ]
or
table.rows.map[cells].flatten.forEach [ ]

but the for-loop should already do the trick.

Regards,
Sebastian
--
Looking for professional support for Xtext, Xtend or Eclipse Modeling?
Go visit: http://xtext.itemis.com
Re: How to iterate in a collection using Xtend? [message #911083 is a reply to message #911065] Tue, 11 September 2012 00:03 Go to previous messageGo to next message
Cristiano Gavião is currently offline Cristiano GaviãoFriend
Messages: 279
Registered: July 2009
Senior Member
Hi Sebastian,

Thanks for the tip.
I've found an interesting example here.

And I'm trying this but can find a way to increment the counter.
val values = "h1,h2,h3&cell11,cell12,cell13&cell21,cell22,cell23".split("&")
		if (!tableRows.nullOrEmpty) {
			val i = 0
			for(row: tableRows)
			{
				val rowCellValues = row.cells.map[name.toString].join(",")
				assertEquals(values.get(i), rowCellValues)
				i=i+1
			}
        }


There is a error being indicated in the "i=i+1" sentence.

can't I create local variable with xtend? how should I do that ?

thanks,

Cristiano
Re: How to iterate in a collection using Xtend? [message #911224 is a reply to message #911083] Tue, 11 September 2012 07:54 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3108
Registered: July 2009
Senior Member
Cristiano,

The val keyword indicated final locals where you would use 'var' for
variable.
Please refer to the docs for details.

Best regards,
Sebastian
--
Looking for professional support for Xtext, Xtend or Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 11.09.12 02:03, schrieb Cristiano Gaviao:
> Hi Sebastian,
>
> Thanks for the tip. I've found an interesting example
> http://www.slideshare.net/meysholdt/testdriven-development-of-xtext-dsls.
>
> And I'm trying this but can find a way to increment the counter.
> val values =
> "h1,h2,h3&cell11,cell12,cell13&cell21,cell22,cell23".split("&")
> if (!tableRows.nullOrEmpty) {
> val i = 0
> for(row: tableRows)
> {
> val rowCellValues = row.cells.map[name.toString].join(",")
> assertEquals(values.get(i), rowCellValues)
> i=i+1
> }
> }
>
> There is a error being indicated in the "i=i+1" sentence.
>
> can't I create local variable with xtend? how should I do that ?
>
> thanks,
>
> Cristiano
Re: How to iterate in a collection using Xtend? [message #911473 is a reply to message #911224] Tue, 11 September 2012 17:18 Go to previous message
Cristiano Gavião is currently offline Cristiano GaviãoFriend
Messages: 279
Registered: July 2009
Senior Member
Hi Sebastian,

I didn't note the difference between val and var.

thanks,

Cristiano
Previous Topic:Use of ITypeProvider in the JvmModelInferrer
Next Topic:Need assistance with formatting...
Goto Forum:
  


Current Time: Wed Dec 02 22:30:44 GMT 2020

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

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

Back to the top