Collection
Collection operations allow you to query and constrain collections of objects.
AnyExpression
Returns the first element that validates the given expression.
Signature
any(expr : OclExpression) : TExample
self.collection->any(i < 2)AppendExpression
Appends the given element to the given collection and returns the extended collection.
Signature
append(elem : T) : Collection<T>Example
self.collection->append("string")AsSetExpression
Returns the given collection as a set, containing unique entries.
Signature
asSet() : CollectionExample
self.collection->asSet()AtExpression
Returns the element of the collection at the given index. Index starts at 1.
Signature
at(index : Number) : TExample
self.collection->at(2)CollectExpression
When we want to specify a collection that is derived from some other collection, but which contains different objects from the original collection (i.e., it is not a sub-collection), we can use a collect operation. The collect operation uses the same syntax as the select and reject.
Signature
collect(expr : OclExpression) : CollectionExample
self.children->collect(age)ExistsExpression
Checks whether a collection contains an element satisfying the given expression.
Signature
exists(expr : OclExpression) : BooleanExample
self.collection->exists(i | i < 2)FirstExpression
Returns the first element of the collection.
Signature
collection->first() : TExample
self.collection->first()ForAllExpression
Many times a constraint is needed on all elements of a collection. The forAll operation in OCL allows specifying a Boolean expression, which must hold for all objects in a collection.
Signature
forAll(expr : oclExpression)Example
self.employee->forAll(p | p.age <= 65)IncludesExpression
Tests if the collection contains the given element. Returns true if the element exists in the collection, false otherwise.
Signature
includes(object : T) : BooleanExample
self.employees->collect(id)->includes(self.managerId)Example
context Company inv: self.availableProducts->includes(self.featuredProduct)IncludesAllExpression
Tests if the collection contains all elements from the given collection. Returns true if all elements from the provided collection exist in the source collection, false otherwise.
This operation checks if the source collection is a superset of the provided collection.
Signature
includesAll(c : Collection(T)) : BooleanExample
self.employee.skills->includesAll(self.project.requiredSkills)Example
context Library inv: self.availableBooks->collect(isbn)->includesAll(self.requiredReadingList)ExcludesExpression
Tests if the collection does not contain the given element. Returns true if the element does not exist in the collection, false otherwise.
This is the inverse of the includes operation.
Signature
excludes(object : T) : BooleanExample
self.employees->excludes(terminatedEmployee)Example
context Order inv: self.items->collect(product)->excludes(self.bannedProduct)ExcludesAllExpression
Tests if the collection does not contain any elements from the given collection. Returns true if none of the elements from the provided collection exist in the source collection, false otherwise.
This operation checks if the source collection and the provided collection are disjoint (have no common elements).
Signature
excludesAll(c : Collection(T)) : BooleanExample
self.activeEmployees->excludesAll(self.terminatedEmployees)Example
context Project inv: self.approvedVendors->excludesAll(self.blacklistedVendors)IsEmptyExpression
Returns true if the collection is empty, false otherwise.
Signature
isEmpty() : BooleanExample
self.cars->isEmpty()IsUniqueExpression
Returns true if the given expression evaluates to only different values across the collection.
Signature
isUnique(expr : oclExpression) : booleanExample
self.collection->isUnique(self > 3)LastExpression
Returns the last element of the collection.
Signature
last() : TExample
self.collection->last()NotEmptyExpression
Returns true if the collection is not empty, false otherwise.
Signature
notEmpty() : BooleanExample
self.cars->notEmpty()OneExpression
Returns true if exactly one element matches the given expression.
Signature
one(expr : oclExpression) : booleanExample
self.collection->one(age < 18)RejectExpression
The reject operation specifies a subset of a collection. A reject is an operation on a collection and is specified using the arrow-syntax. This results in a collection that removes all the elements from collection for which the boolean-expression evaluates to true.
Signature
reject(expr : oclExpression) : CollectionExample
self.customer->reject(underage)SelectExpression
The select operation specifies a subset of a collection. A select is an operation on a collection and is specified using the arrow-syntax. This results in a collection that contains all the elements from collection for which the boolean-expression evaluates to true.
Signature
select(expr : oclExpression) : CollectionExample
self.collection->select(item | item.name = "random")SizeExpression
Returns the number of elements in the collection.
Signature
size() : NumberExample
self.collection->size()SumExpression
Returns the sum of all elements in the collection (requires + support).
Signature
sum() : NumberExample
self.jobs.salary->sum()UnionExpression
Returns a collection containing all elements of self and all elements of the passed-in collection.
Signature
union(c : Collection) : CollectionExample
self.collection->union(self.anotherCollection)