You can use one in an OCL expression to test whether exactly one member of a collection meets a Boolean condition.
Syntax
self->one(expr : OclExpression) : Boolean
self is the collection being evaluated. expr is a Boolean expression evaluated for each element in that collection.
The result is true only when exactly one element makes the expression true. It returns false when no elements match or when two or more elements match.
How one works
Use one when your rule requires one, and only one, matching object.
| Number of matching elements | Result of one(...)
|
|---|---|
| 0 | false
|
| 1 | true
|
| 2 or more | false
|
For example, assume a collection contains Person objects with an age property:
self->one(age >= 18)
This expression returns true when exactly one person in self is 18 or older. It returns false if every person is younger than 18, or if two or more people are 18 or older.
Use one in a collection rule
Follow these steps when writing a one-match condition:
- Start with the collection that you want to inspect.
- Add
->one(...). - Write a Boolean condition that is evaluated for each collection member.
- Confirm that the intended rule is exactly one match, not at least one match.
For example, if self is a collection of persons and you need to check for exactly one person with a specified gender value:
self->one(gender = 'Male')
The expression is true only if one person has gender equal to 'Male'.
When to use it
Use one for rules such as:
- A collection must contain one object that meets a particular condition.
- A query must confirm that a condition identifies a unique member within the current collection.
- A derived Boolean value must be true only for a single match.
one is a collection operator. OCL is a query language: expressions describe and inspect model data without changing it. For the broader role of OCL expressions in constraints, derivations, and ViewModels, see Documentation:OCL Expressions.
Find and test operators
Use the OCL Editor to inspect the operators available for a collection in your current context. OCL General Operators describes this approach. For a guided introduction to operators and derivation, see Documentation:Part 2 OCL: Operators.
