Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » OCL » Remove double instances from set(Remove double instances from set (based on instance attribute value))
Remove double instances from set [message #502483] Tue, 08 December 2009 14:04 Go to next message
Bjorn Mising name is currently offline Bjorn Mising nameFriend
Messages: 3
Registered: October 2009
Junior Member
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

Re: Remove double instances from set [message #503270 is a reply to message #502483] Fri, 11 December 2009 16:17 Go to previous message
Christian Damus is currently offline Christian DamusFriend
Messages: 1270
Registered: July 2009
Location: Canada
Senior Member

--=-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 &quot;small&quot; language:&nbsp; there aren't many constructs to learn in the syntax and the library is small.&nbsp; 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>
&nbsp;&nbsp;&nbsp; let keys : Set(String) = myTasks().name-&gt;asSet() in<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; keys.collect(k | myTasks()-&gt;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 -&gt;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()-&gt;iterate(
task : Task ;
resultSet : Set(Task) = Set{} |

if resultSet-&gt;exists(t|t.name=task.name) then
resultSet
else
resultSet-&gt;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--
Previous Topic:implemented usecases
Next Topic:How to convert Self.description to xsd::DOMElement
Goto Forum:
  


Current Time: Thu Apr 25 20:11:23 GMT 2024

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

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

Back to the top