Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » OCL » Reverse sort for sortedBy()
Reverse sort for sortedBy() [message #939654] Thu, 11 October 2012 00:14 Go to next message
Warwick Burrows is currently offline Warwick BurrowsFriend
Messages: 132
Registered: July 2009
Location: Austin, TX
Senior Member
Hi,

Is there a way to perform a reverse sort in an OCL query? Looking on the web the only place I found a reference to reverse OCL sorting indicated that putting a "-" in front of the sort field would work e.g. sortedBy(e|-e.name). But I get an exception:

Caused by: org.eclipse.ocl.SemanticException: Cannot find operation (-()) for the type (String)

I also tried sortedBy(-e|e.name) but got this error instead:

Caused by: org.eclipse.ocl.SyntaxException: 2:374:2:374 "::" expected instead of "|"

Is that the correct syntax? Is there another way to reverse sort?

Thanks,
Warwick

[Updated on: Thu, 11 October 2012 00:21]

Report message to a moderator

Re: Reverse sort for sortedBy() [message #940117 is a reply to message #939654] Thu, 11 October 2012 10:22 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

I suspect that the "-" prefix was for sorting by numeric keys for which
negation will reverse the order.

There is no counterpart for characters, unless you want to use
Characters() to take a String apart and then use a custom codepoint()
routine to get integers.

Much simpler is to just invoke ->sortedBy(....)->reverse().

Regards

Ed Willink

On 11/10/2012 01:14, Warwick Burrows wrote:
> Hi,
>
> Is there a way to perform a reverse sort in an OCL query? Looking on
> the web the only place I found a reference to reverse OCL sorting
> indicated that putting a "-" in front of the sort field would work
> e.g. sortedBy(-e.name). But I get an exception:
>
> Caused by: org.eclipse.ocl.SemanticException: Cannot find operation
> (-()) for the type (String)
>
> Is that the correct syntax? Is there another way to reverse sort?
>
> Thanks,
> Warwick
>
>
>
Re: Reverse sort for sortedBy() [message #940577 is a reply to message #940117] Thu, 11 October 2012 19:34 Go to previous messageGo to next message
Warwick Burrows is currently offline Warwick BurrowsFriend
Messages: 132
Registered: July 2009
Location: Austin, TX
Senior Member
Hey Ed,

Thanks for your reply. I am definitely happy that there is a simple way to do a reverse sort. I tried as you suggested but get this exception now:

Caused by: org.eclipse.ocl.SemanticException: Cannot find operation (reverse()) for the type (OrderedSet(Map))
at org.eclipse.ocl.util.OCLUtil.checkForErrors(OCLUtil.java:358)
at org.eclipse.ocl.util.OCLUtil.checkForErrors(OCLUtil.java:328)
at org.eclipse.ocl.internal.helper.HelperUtil.checkForErrors(HelperUtil.java:518)
at org.eclipse.ocl.internal.helper.HelperUtil.parseQuery(HelperUtil.java:165)
at org.eclipse.ocl.internal.helper.OCLHelperImpl.createQuery(OCLHelperImpl.java:176)
at org.eclipse.emf.cdo.server.ocl.OCLQueryHandler.executeQuery(OCLQueryHandler.java:141)


Is the reverse() operation implemented in a particular version of OCL? So that you know I'm using OCL queries to CDO 4.1 (last release candidate before the 4.1 release) and I can see the following OCL plugins in the CDO server plugins directory:

org.eclipse.emf.cdo.server.ocl_4.0.0.201207031627.jar
org.eclipse.ocl_3.2.0.v20120611-1210.jar
org.eclipse.ocl.common_1.0.0.v20120516-1543.jar
org.eclipse.ocl.ecore_3.2.0.v20120522-1637.jar

Thanks,
Warwick
Re: Reverse sort for sortedBy() [message #941007 is a reply to message #940577] Fri, 12 October 2012 06:16 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

In April I retrofitted a variety of functions such as characters(),
selectByKind() and oclAsSet() from the new experimental pivot-based OCL
to the traditional Ecore-based OCL to avoid exactly this problem.
Unfortunately, I missed out reverse().

The experimental functionality is used within the Xtext editors and can
be accessed from the Java API, but while it remains experimental, the
migration of the CDO interface and general revision is still to be done.
The new code already executes 3 to 30 times quicker so the simple CDO
migration looks very likely for Kepler.

For now you can try something like

let backwards : Sequence(...) = ...->sortedBy(name) in
let size: Integer = backwards->size() in
Sequence{0..size-1}->collect(i : Integer | backwards->at(size - i))

Regards

Ed Willink



On 11/10/2012 20:34, Warwick Burrows wrote:
> Hey Ed,
>
> Thanks for your reply. I am definitely happy that there is a simple
> way to do a reverse sort. I tried as you suggested but get this
> exception now:
>
> Caused by: org.eclipse.ocl.SemanticException: Cannot find operation
> (reverse()) for the type (OrderedSet(Map))
> at org.eclipse.ocl.util.OCLUtil.checkForErrors(OCLUtil.java:358)
> at org.eclipse.ocl.util.OCLUtil.checkForErrors(OCLUtil.java:328)
> at
> org.eclipse.ocl.internal.helper.HelperUtil.checkForErrors(HelperUtil.java:518)
> at
> org.eclipse.ocl.internal.helper.HelperUtil.parseQuery(HelperUtil.java:165)
> at
> org.eclipse.ocl.internal.helper.OCLHelperImpl.createQuery(OCLHelperImpl.java:176)
> at
> org.eclipse.emf.cdo.server.ocl.OCLQueryHandler.executeQuery(OCLQueryHandler.java:141)
>
>
> Is the reverse() operation implemented in a particular version of
> OCL? So that you know I'm using OCL queries to CDO 4.1 (last release
> candidate before the 4.1 release) and I can see the following OCL
> plugins in the CDO server plugins directory:
>
> org.eclipse.emf.cdo.server.ocl_4.0.0.201207031627.jar
> org.eclipse.ocl_3.2.0.v20120611-1210.jar
> org.eclipse.ocl.common_1.0.0.v20120516-1543.jar
> org.eclipse.ocl.ecore_3.2.0.v20120522-1637.jar
>
> Thanks,
> Warwick
>
Re: Reverse sort for sortedBy() [message #941074 is a reply to message #941007] Fri, 12 October 2012 07:39 Go to previous messageGo to next message
Warwick Burrows is currently offline Warwick BurrowsFriend
Messages: 132
Registered: July 2009
Location: Austin, TX
Senior Member
Hi Ed,

Thanks for your reply and for implementing and improving ocl for eclipse and cdo. Unfortunately I'm not clear on how to apply the ocl code snippet you've suggested. For example if I have a very simple case like the following OCLQuery to cdo to sort all maps by name, ascending:

MyMap.allInstances()->sortedBy(name)

How would I incorporate your example with that query so that it sorts the result set in reverse order? I've not been able to find a lot of doco on ocl syntax. Is it just a matter of replacing the "..." with my desired result set? e.g.

let backwards : Sequence(MyMap.allInstances()) = MyMap.allInstances()->sortedBy(name) in
let size: Integer = backwards->size() in
Sequence{0..size-1}->collect(i : Integer | backwards->at(size - i))

Is there anything else I should be aware of with the sort logic when applying it to other more complex queries?

Thanks,
Warwick
Re: Reverse sort for sortedBy() [message #941117 is a reply to message #941074] Fri, 12 October 2012 08:15 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

You nearly got it right; the first ... was your type, the second your
expression so try

let backwards : Sequence(MyMap) = MyMap.allInstances()->sortedBy(name) in
let size: Integer = backwards->size() in
Sequence{0..size-1}->collect(i : Integer | backwards->at(size - i))

allInstances() is not very efficient so you may want to look to an
appropriate container.

sortedBy is very simple, the body expression just provides a per-element
sorting key.

Regards

Ed Willink


On 12/10/2012 08:39, Warwick Burrows wrote:
> Hi Ed,
>
> Thanks for your reply and for implementing and improving ocl for
> eclipse and cdo. Unfortunately I'm not clear on how to apply the ocl
> code snippet you've suggested. For example if I have a very simple
> case like the following OCLQuery to cdo to sort all maps by name,
> ascending:
>
> MyMap.allInstances()->sortedBy(name)
>
> How would I incorporate your example with that query so that it sorts
> the result set in reverse order? I've not been able to find a lot of
> doco on ocl syntax. Is it just a matter of replacing the "..." with my
> desired result set? e.g.
>
> let backwards : Sequence(MyMap.allInstances()) =
> MyMap.allInstances()->sortedBy(name) in
> let size: Integer = backwards->size() in
> Sequence{0..size-1}->collect(i : Integer | backwards->at(size - i))
>
> Is there anything else I should be aware of with the sort logic when
> applying it to other more complex queries?
>
> Thanks,
> Warwick
>
Re: Reverse sort for sortedBy() [message #941694 is a reply to message #941117] Fri, 12 October 2012 19:28 Go to previous messageGo to next message
Warwick Burrows is currently offline Warwick BurrowsFriend
Messages: 132
Registered: July 2009
Location: Austin, TX
Senior Member
Ed Willink wrote on Fri, 12 October 2012 04:15
Hi
You nearly got it right; the first ... was your type, the second your
expression so try

let backwards : Sequence(MyMap) = MyMap.allInstances()->sortedBy(name) in
let size: Integer = backwards->size() in
Sequence{0..size-1}->collect(i : Integer | backwards->at(size - i))

allInstances() is not very efficient so you may want to look to an
appropriate container.

sortedBy is very simple, the body expression just provides a per-element
sorting key.


Great thanks, I'll try this! BTW, is there a site where I can learn about ocl syntax with examples? I need to understand what you mean by "container" in the context of your suggestion above. So far I've not been able to find a site that lays it all out which is why I didn't even know there was a reverse() operation Smile

Thanks,
Warwick
Re: Reverse sort for sortedBy() [message #941762 is a reply to message #941694] Fri, 12 October 2012 21:00 Go to previous messageGo to next message
Warwick Burrows is currently offline Warwick BurrowsFriend
Messages: 132
Registered: July 2009
Location: Austin, TX
Senior Member
That works! I just needed one adjustment to get around the issue where the defined type of the "backwards" variable didn't match the results returned by the query. I found someone with a similar problem and just added the oclAsType(MyMap) to the sorted results.

let backwards : Sequence(MyMap) = MyMap.allInstances()->sortedBy(name).oclAsType(MyMap) in
let size: Integer = backwards->size() in
Sequence{0..size-1}->collect(i : Integer | backwards->at(size - i))


Thanks,
Warwick
Re: Reverse sort for sortedBy() [message #942603 is a reply to message #941694] Sat, 13 October 2012 17:38 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
Hi<br>
<br>
'container' is a standard modeling term; very little to do with OCL.<br>
<h1 class="parseasinTitle"> <span id="btAsinTitle">The Object
Constraint Language: Getting Your Models Ready for MDA
(Addison-Wesley Object Technology) <span style="text-transform:
capitalize; font-size: 16px;">[Paperback]</span></span> </h1>
<a
href="http://www.amazon.co.uk/s/ref=ntt_athr_dp_sr_1?_encoding=UTF8&amp;field-author=Jos%20Warmer&amp;search-alias=books-uk">Jos
Warmer</a> <span class="byLinePipe">(Author)</span>, <a
href="http://www.amazon.co.uk/s/ref=ntt_athr_dp_sr_2?_encoding=UTF8&amp;field-author=Anneke%20Kleppe&amp;search-alias=books-uk">Anneke
Kleppe</a> <span class="byLinePipe">(Author)</span>  <br>
<br>
is a must for learning OCL.<br>
<br>
    Regards<br>
<br>
        Ed Willink<br>
<br>
On 12/10/2012 20:28, Warwick Burrows wrote:
<blockquote cite="mid:k59r19$jrp$1@xxxxxxxxe.org" type="cite">Ed
Willink wrote on Fri, 12 October 2012 04:15
<br>
<blockquote type="cite">Hi
<br>
You nearly got it right; the first ... was your type, the second
your expression so try
<br>
<br>
let backwards : Sequence(MyMap) =
MyMap.allInstances()-&gt;sortedBy(name) in
<br>
let size: Integer = backwards-&gt;size() in
<br>
Sequence{0..size-1}-&gt;collect(i : Integer |
backwards-&gt;at(size - i))
<br>
<br>
allInstances() is not very efficient so you may want to look to
an appropriate container.
<br>
<br>
sortedBy is very simple, the body expression just provides a
per-element sorting key.
<br>
</blockquote>
<br>
<br>
Great thanks, I'll try this!  BTW, is there a site where I can
learn about ocl syntax with examples? I need to understand what
you mean by "container" in the context of your suggestion above.
So far I've not been able to find a site that lays it all out
which is why I didn't even know there was a reverse() operation :)
<br>
<br>
Thanks,
<br>
Warwick
<br>
<br>
</blockquote>
<br>
</body>
</html>
Re: Reverse sort for sortedBy() [message #942607 is a reply to message #941762] Sat, 13 October 2012 17:40 Go to previous message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

oclAsType() should not be necessary, and it isn't with the new
pivot-based OCL that has fully modeled library declarations exploiting
templated types to avoid loss of static type precision.

Regards

Ed Willink

On 12/10/2012 22:00, Warwick Burrows wrote:
> That works! I just needed one adjustment to get around the issue
> where the defined type of the "backwards" variable didn't match the
> results returned by the query. I found someone with a similar problem
> and just added the oclAsType(MyMap) to the sorted results.
>
>
> let backwards : Sequence(MyMap) =
> MyMap.allInstances()->sortedBy(name).oclAsType(MyMap) in
> let size: Integer = backwards->size() in
> Sequence{0..size-1}->collect(i : Integer | backwards->at(size - i))
>
>
> Thanks,
> Warwick
>
Previous Topic:OCL to Java
Next Topic:Validate OCL constraints on XMI model
Goto Forum:
  


Current Time: Tue Apr 23 13:40:00 GMT 2024

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

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

Back to the top