# Re: Single element of set

Alan Cameron Wills (alan@trireme.com)
Fri, 14 Apr 2000 13:46:31 +0100

```
The Catalysis version of the 'set flattening' is more powerful than the OCL one,
and I commend it to OCL semanticists. It solves this problem. In the Catalysis
expression language, which is not quite OCL, you'd write

Let h = hotel->select(x | "some test on x" ) in
h.frontDesk.....

Let h= hotel->select(x | "some test on x" )->asSequence->first in
h.frontDesk....

The only danger in the Catalysis approach is that h might turn out to be a set
with a size <> 1; in which case h.frontDesk is a set of front desks, rather than a
single one. Nevertheless, it does have a meaning.

The Catalysis version works thus:

* Multiple associations by default represent Flat Sets (of the target type).
(You can annotate an association end with <<set>> or <<seq>> if you want some
other interpretation.)
* FlatSet is a type distinct from Set and other collections.
* FlatSet has all the axioms of Set; together with the extra rule
x == FS{x}
where FS{a,b,c} is the flat set containing a, b, and c.
* When applied to a flat set, the operator "." yields a flat set whose results
include all the results of applying to the inputs. This is like OCL,  except
that in OCL the result is a bag. We have found the flat set result more
useful in most of the examples we've tried. (You can coerce the input to a
Bag if you really want a Bag result:  xx->asBag.doStuff )

You might like to check that the extra axiom has the following effects:

* FS{a,{b,c}, d} == FS{a,b,c,d}
* Of course it works both ways -- hence no need to extract an element
explicitly.
* It doesn't of course disturb the meaning of ordinary sets.

Alan

> At 07:31 PM 4/13/00 +0100, John Daniels wrote:
> >Dear list,
> >
> >I keep finding myself writing OCL like this:
> >
> >----------------
> >Let h = hotel->select(x | "some test on x" )->asSequence->first in
> >----------------
> >
> >where I have already asserted that the hotel set includes a single
> >element that matches the select condition.
> >
> >Is there a simpler way to access the single element of a set?
> >
> >Thanks,
> >--John
> >
> >=========================================================================
> >John Daniels
> >Tel/fax: +44 (0)20 8777 6007
> >
> >

--
Alan Cameron Wills
TriReme International Ltd      http://www.trireme.com
Tel: +44 161 225 3240          Fax: +44 161 257 3292
Catalysis: Objects, Components and Frameworks in UML
```