Skip to main content



      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 17:49 Go to next message
Eclipse UserFriend
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 18:47 Go to previous messageGo to next message
Eclipse UserFriend
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] Mon, 10 September 2012 20:03 Go to previous messageGo to next message
Eclipse UserFriend
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 03:54 Go to previous messageGo to next message
Eclipse UserFriend
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 13:18 Go to previous message
Eclipse UserFriend
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 Jul 23 21:50:52 EDT 2025

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

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

Back to the top