A Boolean attribute stores a true/false decision in your MDriven model and is for anyone writing OCL expressions, constraints, or derived values.
Boolean types
A Boolean has two possible values: true and false. In OCL, you can declare it as either non-nullable or nullable.
| Declaration | Meaning | Use it when | Example |
|---|---|---|---|
Boolean
|
A non-nullable Boolean. Its value is always true or false.
|
The attribute represents a normal yes/no decision. | IsActive : Boolean
|
Boolean?
|
A nullable Boolean. Its value can be true, false, or null.
|
The absence of an answer has a meaning that is different from both true and false. | ApprovalReceived : Boolean?
|
Choose non-nullable Boolean attributes for stored decisions
For an attribute on a class that is persisted to the database, use Boolean in almost all cases.
A nullable attribute defaults to null. This adds a third state that expressions and user-interface logic must handle. It is easy to treat false and null as the same value by mistake.
For example, model whether an order is shipped as:
IsShipped : BooleanUse a nullable Boolean only when all three states are required. For example, an optional approval can mean:
ApprovalReceived
|
Meaning |
|---|---|
true
|
Approval was received. |
false
|
Approval was declined. |
null
|
No approval decision has been recorded. |
Use Boolean expressions directly
When an expression already evaluates to a Boolean, use that expression directly. Do not compare it to true or false.
For an IsActive Boolean attribute, write:
customer.IsActive
not customer.IsActiveDo not write the following when you only need to test the Boolean value:
customer.IsActive = true
customer.IsActive = falseThe equality operator, =, compares two values. Use it when you are actually comparing values, such as checking whether a status has a specific value. See Documentation:Mathematical symbols for comparison operators and Documentation:OCL Boolean Operators for logical operators.
Do not test a Boolean with collection operations
A Boolean is a single value, not a collection. Do not use collection operations such as notEmpty to determine whether it is true.
The following expression is incorrect:
customer.IsActive->notEmptyIt always returns true: the Boolean is converted to a collection containing one Boolean value, and that collection is not empty. It does not test whether IsActive is true.
Write the Boolean expression itself instead:
customer.IsActiveTo test the opposite condition, use:
not customer.IsActiveExample: deriving a decision
Assume an Order has a non-nullable IsPaid : Boolean attribute and an IsShipped : Boolean attribute. An expression that identifies paid orders that are not shipped can be written as:
self.IsPaid and not self.IsShippedEach attribute is used as a Boolean expression. The and operator combines the two conditions.
See also
[[Category:Attributes and data types [Beginner]]]
