|
Re: Remove double instances from set [message #503270 is a reply to message #502483] |
Fri, 11 December 2009 16:17 |
|
--=-TqDII4WGBFRbdKVltM9r
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
Hi, Bjorn,
One of the characteristics of OCL that I appreciate is that it is a
"small" language: there aren't many constructs to learn in the syntax
and the library is small. Sometimes, however, the smallness of the
library makes life difficult and you are forced into long-winded
expressions like your example.
I think I can make this case more succinct but probably slightly less
efficient:
let keys : Set(String) = myTasks().name->asSet() in
keys.collect(k | myTasks()->any(name = k))
The result of this is not a Set (I think it's a Bag) but can be
converted to a set using ->asSet().
Cheers,
Christian
On Tue, 2009-12-08 at 09:04 -0500, Bjorn wrote:
> Hello,
>
> Please consider the following case: I have a query that results in a set of class-instances of the following class:
>
> Class Task {
> name: String
> id : Integer
> }
> The set of instances are not unique in the sense that it contains instances with identical name-attribute values. I want to generate a (sub)set of this set that is unique with respect to the 'name' attribute.
>
> I implemented this with the following query, but doubt if there isn't a more simple (or more efficient) way to do so. If so, I like to know how...
>
> In OCL:
> myTasks()->iterate(
> task : Task ;
> resultSet : Set(Task) = Set{} |
>
> if resultSet->exists(t|t.name=task.name) then
> resultSet
> else
> resultSet->including(task)
> endif
> )
> In words:
> My input is a set of tasks. I use the iterate operations to construct a new set of tasks from the input. I start with a new empty task list and iterate through the input tasks. If the output set does not contain an instance of the task with the same name as the current (iterator)task, it is added. Otherwise, nothing is done (the ResultSet isunchanged).
>
> Thanks in advance for you reaction(s),
>
> Bjorn
>
>
--=-TqDII4WGBFRbdKVltM9r
Content-Type: text/html; charset="utf-8"
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=UTF-8">
<META NAME="GENERATOR" CONTENT="GtkHTML/3.24.1.1">
</HEAD>
<BODY>
Hi, Bjorn,<BR>
<BR>
One of the characteristics of OCL that I appreciate is that it is a "small" language: there aren't many constructs to learn in the syntax and the library is small. Sometimes, however, the smallness of the library makes life difficult and you are forced into long-winded expressions like your example.<BR>
<BR>
I think I can make this case more succinct but probably slightly less efficient:<BR>
<BR>
let keys : Set(String) = myTasks().name->asSet() in<BR>
keys.collect(k | myTasks()->any(name = k))<BR>
<BR>
The result of this is not a Set (I think it's a Bag) but can be converted to a set using ->asSet().<BR>
<BR>
Cheers,<BR>
<BR>
Christian<BR>
<BR>
<BR>
On Tue, 2009-12-08 at 09:04 -0500, Bjorn wrote:
<BLOCKQUOTE TYPE=CITE>
<PRE>
Hello,
Please consider the following case: I have a query that results in a set of class-instances of the following class:
Class Task {
name: String
id : Integer
}
The set of instances are not unique in the sense that it contains instances with identical name-attribute values. I want to generate a (sub)set of this set that is unique with respect to the 'name' attribute.
I implemented this with the following query, but doubt if there isn't a more simple (or more efficient) way to do so. If so, I like to know how...
In OCL:
myTasks()->iterate(
task : Task ;
resultSet : Set(Task) = Set{} |
if resultSet->exists(t|t.name=task.name) then
resultSet
else
resultSet->including(task)
endif
)
In words:
My input is a set of tasks. I use the iterate operations to construct a new set of tasks from the input. I start with a new empty task list and iterate through the input tasks. If the output set does not contain an instance of the task with the same name as the current (iterator)task, it is added. Otherwise, nothing is done (the ResultSet isunchanged).
Thanks in advance for you reaction(s),
Bjorn
</PRE>
</BLOCKQUOTE>
</BODY>
</HTML>
--=-TqDII4WGBFRbdKVltM9r--
|
|
|
Powered by
FUDForum. Page generated in 0.02472 seconds