Information Technology Reference
In-Depth Information
Sequence . These collections manage duplication and ordering
of elements in different ways but somewhat similar to generics
introduced in Java 5. These are generic collections and they
can be nested. OCL provides a set of generic operators to test
and convert collections. Normally,to navigate from a collection,
one should use the -> operator but it is also meaningful in
case of one object only. OCL defines several operations on
collections; they are devoted to computing some information
like the number of elements, to search in the collection, to
extract some subset or to iterate some actions.
As an example, if we want to check that the class names in
a package are not empty, we use the “for all” primitive as in
Listing 3.3. The forAll is a universal quantification.
1 context Package :: checkClassNames()
2 post: result = Self.elements−>forAll(not(name= "" ))
Listing 3.3. OCL quantification example
To find that a class with a given name exists in a package,
we will write an expression with the collect iterator as in
Listing 3.4. Here the collect builds the set of the class names
and includes tests if an element is in the set.
1 context Package :: isIn (String n)
2 post: result = Self.elements−>collect(name)−>includes(n)
Listing 3.4. Collect and test in OCL
To compute the set of all the attributes of classes in a
package, we could use the general iteration mechanism as
illustrated in Listing 3.5. The iterate primitive repeats the
collect action, to collect the class attributes, over the elements
of the collection (Self.elements).
1 context Package :: allAttributes()
2 post: result = Self.elements−>iterate(x:Set;acc:Set = Set {}
3
| acc−>collect(x.attributes))
Listing 3.5. An OCL iteration
 
Search WWH ::




Custom Search