Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Xtend2 - sorting by a function
icon5.gif  Xtend2 - sorting by a function [message #668016] Tue, 03 May 2011 12:09 Go to next message
Andrew Clark is currently offline Andrew Clark
Messages: 18
Registered: June 2010
Junior Member
Using the MyDsl example in Xtext 2, I have written the following Xtend

class MyDslGenerator implements IGenerator {
	
	override void doGenerate(Resource resource, IFileSystemAccess fsa) {
		for(model: resource.allContentsIterable.filter(typeof(Model))) {
			fsa.generateFile("example.txt", model.hello)
		}
	}
	
	hello (Model m) {
		''' 
		Longest is «m.longest»
		'''
	}

	longest(Model m) {
		m.greetings.sortBy(e|e.length).last.name
	}

	longest2(Model m) {
		m.greetings.map(e|e.length).sort.last
	}
	
	int length(Greeting g) {
		g.name.length
	}
	
}


When this runs I get

Exception in thread "main" java.lang.IllegalArgumentException: The 'no duplicates' constraint is violated
	at org.eclipse.emf.common.util.AbstractEList.set(AbstractEList.java:268)
	at org.eclipse.emf.common.util.AbstractEList$EListIterator.doSet(AbstractEList.java:963)
	at org.eclipse.emf.common.util.AbstractEList$EListIterator.set(AbstractEList.java:941)
	at java.util.Collections.sort(Unknown Source)
	at org.eclipse.xtext.xbase.lib.ListExtensions.sortBy(ListExtensions.java:85)
	at org.xtext.example.mydsl.generator.MyDslGenerator.longest(MyDslGenerator.java:46)
	at org.xtext.example.mydsl.generator.MyDslGenerator.hello(MyDslGenerator.java:32)
	at org.xtext.example.mydsl.generator.MyDslGenerator.doGenerate(MyDslGenerator.java:24)
	at org.xtext.example.mydsl.generator.Main.runGenerator(Main.java:63)
	at org.xtext.example.mydsl.generator.Main.main(Main.java:32)



alternatively I can try the longest2 function and get

Exception in thread "main" java.lang.UnsupportedOperationException
	at java.util.AbstractList.set(Unknown Source)
	at java.util.AbstractList$ListItr.set(Unknown Source)
	at java.util.Collections.sort(Unknown Source)
	at org.eclipse.xtext.xbase.lib.ListExtensions.sort(ListExtensions.java:44)
	at org.xtext.example.mydsl.generator.MyDslGenerator.longest2(MyDslGenerator.java:61)
	at org.xtext.example.mydsl.generator.MyDslGenerator.hello(MyDslGenerator.java:32)
	at org.xtext.example.mydsl.generator.MyDslGenerator.doGenerate(MyDslGenerator.java:24)
	at org.xtext.example.mydsl.generator.Main.runGenerator(Main.java:63)
	at org.xtext.example.mydsl.generator.Main.main(Main.java:32)


Is this a bug (this worked in Xtend 1)?
Re: Xtend2 - sorting by a function [message #668036 is a reply to message #668016] Tue, 03 May 2011 13:28 Go to previous messageGo to next message
Sven Efftinge is currently offline Sven Efftinge
Messages: 1769
Registered: July 2009
Senior Member
It's because (currently) the sort functions do modify the passed list.
EMF puts certain constraints on the list which are validated during the
sort operation. I think we should change that and return a new list
instead and leave the old one unmodified.
Please open a bugzilla.

Thanks,
Sven


Am 5/3/11 2:09 PM, schrieb Andrew Clark:
> Using the MyDsl example in Xtext 2, I have written the following Xtend
>
> class MyDslGenerator implements IGenerator {
>
> override void doGenerate(Resource resource, IFileSystemAccess fsa) {
> for(model: resource.allContentsIterable.filter(typeof(Model))) {
> fsa.generateFile("example.txt", model.hello)
> }
> }
>
> hello (Model m) {
> ''' Longest is «m.longest»
> '''
> }
>
> longest(Model m) {
> m.greetings.sortBy(e|e.length).last.name
> }
>
> longest2(Model m) {
> m.greetings.map(e|e.length).sort.last
> }
>
> int length(Greeting g) {
> g.name.length
> }
>
> }
>
>
> When this runs I get
>
>
> Exception in thread "main" java.lang.IllegalArgumentException: The 'no
> duplicates' constraint is violated
> at
> org.eclipse.emf.common.util.AbstractEList.set(AbstractEList. java:268)
> at
> org.eclipse.emf.common.util.AbstractEList$EListIterator.doSe t(AbstractEList.java:963)
>
> at
> org.eclipse.emf.common.util.AbstractEList$EListIterator.set( AbstractEList.java:941)
>
> at java.util.Collections.sort(Unknown Source)
> at
> org.eclipse.xtext.xbase.lib.ListExtensions.sortBy(ListExtens ions.java:85)
> at
> org.xtext.example.mydsl.generator.MyDslGenerator.longest(MyD slGenerator.java:46)
>
> at
> org.xtext.example.mydsl.generator.MyDslGenerator.hello(MyDsl Generator.java:32)
>
> at
> org.xtext.example.mydsl.generator.MyDslGenerator.doGenerate( MyDslGenerator.java:24)
>
> at org.xtext.example.mydsl.generator.Main.runGenerator(Main.jav a:63)
> at org.xtext.example.mydsl.generator.Main.main(Main.java:32)
>
>
>
> alternatively I can try the longest2 function and get
>
>
> Exception in thread "main" java.lang.UnsupportedOperationException
> at java.util.AbstractList.set(Unknown Source)
> at java.util.AbstractList$ListItr.set(Unknown Source)
> at java.util.Collections.sort(Unknown Source)
> at
> org.eclipse.xtext.xbase.lib.ListExtensions.sort(ListExtensio ns.java:44)
> at
> org.xtext.example.mydsl.generator.MyDslGenerator.longest2(My DslGenerator.java:61)
>
> at
> org.xtext.example.mydsl.generator.MyDslGenerator.hello(MyDsl Generator.java:32)
>
> at
> org.xtext.example.mydsl.generator.MyDslGenerator.doGenerate( MyDslGenerator.java:24)
>
> at org.xtext.example.mydsl.generator.Main.runGenerator(Main.jav a:63)
> at org.xtext.example.mydsl.generator.Main.main(Main.java:32)
>
>
> Is this a bug (this worked in Xtend 1)?
>


--
Need professional support for Xtext or other Eclipse Modeling technologies?
Go to: http://xtext.itemis.com
Twitter : @svenefftinge
Blog : http://blog.efftinge.de


--
Need professional support on Xtext or Xtend?
Mail to: xtext (at) itemis.com
Twitter : @svenefftinge
Blog : blog.efftinge.de
Re: Xtend2 - sorting by a function [message #668038 is a reply to message #668036] Tue, 03 May 2011 13:36 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian Zarnekow
Messages: 2899
Registered: July 2009
Senior Member
Hi Sven, hi Andrew,

since it's well documented (since M7) which functions modify the passed
list and which create new copies of the input, I'm inclined to not
change the behavior of sort. Otherwise it's hardly possible to sort an
existing list in a convenient way with Xtend.

Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 03.05.11 15:28, schrieb Sven Efftinge:
> It's because (currently) the sort functions do modify the passed list.
> EMF puts certain constraints on the list which are validated during the
> sort operation. I think we should change that and return a new list
> instead and leave the old one unmodified.
> Please open a bugzilla.
>
> Thanks,
> Sven
>
>
> Am 5/3/11 2:09 PM, schrieb Andrew Clark:
>> Using the MyDsl example in Xtext 2, I have written the following Xtend
>>
>> class MyDslGenerator implements IGenerator {
>>
>> override void doGenerate(Resource resource, IFileSystemAccess fsa) {
>> for(model: resource.allContentsIterable.filter(typeof(Model))) {
>> fsa.generateFile("example.txt", model.hello)
>> }
>> }
>>
>> hello (Model m) {
>> ''' Longest is «m.longest»
>> '''
>> }
>>
>> longest(Model m) {
>> m.greetings.sortBy(e|e.length).last.name
>> }
>>
>> longest2(Model m) {
>> m.greetings.map(e|e.length).sort.last
>> }
>>
>> int length(Greeting g) {
>> g.name.length
>> }
>>
>> }
>>
>>
>> When this runs I get
>>
>>
>> Exception in thread "main" java.lang.IllegalArgumentException: The 'no
>> duplicates' constraint is violated
>> at
>> org.eclipse.emf.common.util.AbstractEList.set(AbstractEList. java:268)
>> at
>> org.eclipse.emf.common.util.AbstractEList$EListIterator.doSe t(AbstractEList.java:963)
>>
>> at
>> org.eclipse.emf.common.util.AbstractEList$EListIterator.set( AbstractEList.java:941)
>>
>> at java.util.Collections.sort(Unknown Source)
>> at
>> org.eclipse.xtext.xbase.lib.ListExtensions.sortBy(ListExtens ions.java:85)
>> at
>> org.xtext.example.mydsl.generator.MyDslGenerator.longest(MyD slGenerator.java:46)
>>
>> at
>> org.xtext.example.mydsl.generator.MyDslGenerator.hello(MyDsl Generator.java:32)
>>
>> at
>> org.xtext.example.mydsl.generator.MyDslGenerator.doGenerate( MyDslGenerator.java:24)
>>
>> at org.xtext.example.mydsl.generator.Main.runGenerator(Main.jav a:63)
>> at org.xtext.example.mydsl.generator.Main.main(Main.java:32)
>>
>>
>>
>> alternatively I can try the longest2 function and get
>>
>>
>> Exception in thread "main" java.lang.UnsupportedOperationException
>> at java.util.AbstractList.set(Unknown Source)
>> at java.util.AbstractList$ListItr.set(Unknown Source)
>> at java.util.Collections.sort(Unknown Source)
>> at
>> org.eclipse.xtext.xbase.lib.ListExtensions.sort(ListExtensio ns.java:44)
>> at
>> org.xtext.example.mydsl.generator.MyDslGenerator.longest2(My DslGenerator.java:61)
>>
>> at
>> org.xtext.example.mydsl.generator.MyDslGenerator.hello(MyDsl Generator.java:32)
>>
>> at
>> org.xtext.example.mydsl.generator.MyDslGenerator.doGenerate( MyDslGenerator.java:24)
>>
>> at org.xtext.example.mydsl.generator.Main.runGenerator(Main.jav a:63)
>> at org.xtext.example.mydsl.generator.Main.main(Main.java:32)
>>
>>
>> Is this a bug (this worked in Xtend 1)?
>>
>
>
Re: Xtend2 - sorting by a function [message #668047 is a reply to message #668038] Tue, 03 May 2011 14:10 Go to previous messageGo to next message
Andrew Clark is currently offline Andrew Clark
Messages: 18
Registered: June 2010
Junior Member
Hi Sebastian,

If the sort functionality is not changed then how should I write the Xtend code in my example ?

BTW: Which documentation are you talking about, the Java doc in ListExtensions ?

regards,

Andrew


Re: Xtend2 - sorting by a function [message #668052 is a reply to message #668047] Tue, 03 May 2011 14:32 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian Zarnekow
Messages: 2899
Registered: July 2009
Senior Member
Hi Andrew,

we discussed this offline and I filed this ticket to describe the results:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=344584

Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 03.05.11 16:10, schrieb Andrew Clark:
> Hi Sebastian,
> If the sort functionality is not changed then how should I write the
> Xtend code in my example ?
> BTW: Which documentation are you talking about, the Java doc in
> ListExtensions ?
>
> regards,
>
> Andrew
>
>
>
Re: Xtend2 - sorting by a function [message #668061 is a reply to message #668052] Tue, 03 May 2011 15:02 Go to previous message
Andrew Clark is currently offline Andrew Clark
Messages: 18
Registered: June 2010
Junior Member
Wow that was quick, thanks to you both. I am curious to know the details of the offline discussion Razz
Previous Topic:OCL Execution Environment for Xtext?
Next Topic:assertion error in MergingHighlightedPositionAcceptor
Goto Forum:
  


Current Time: Mon Oct 20 08:25:24 GMT 2014

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

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