You can use oclIsUndefined() in an OCL expression to test whether a value is either null or invalid.
Syntax
expression.oclIsUndefined() : BooleanThe operation returns true when expression is null or invalid. It returns false for a defined, valid value.
When to use it
Use oclIsUndefined() when your expression must treat both a missing value and an invalid value as unavailable.
For example, this expression returns a fallback text when self.Name is unavailable:
if self.Name.oclIsUndefined() then
'Name is unavailable'
else
self.Name
endifThe conditional-expression form is described in Documentation:OCLOperators if.
Null, invalid, and undefined
| Value state | oclIsUndefined()
|
Meaning |
|---|---|---|
null
|
true
|
The expression has no value. |
invalid
|
true
|
The expression is invalid. |
| A defined, valid value | false
|
The expression can be used as its value type permits. |
Difference from oclIsInvalid()
oclIsUndefined() includes both null and invalid. Use oclIsInvalid() when you need to test only for invalid and must distinguish it from null.
See also
What is invalid?
What is invalid?
You can distinguish a missing value from an invalid result when writing OCL expressions that must handle each state differently.
null means that an expression has no value. invalid is a separate state that oclIsInvalid() tests for. Both states are undefined for the purpose of oclIsUndefined(), so that operation returns true for either state.
| State | Test result | Use when |
|---|---|---|
null
|
expression.oclIsUndefined() returns true; expression.oclIsInvalid() returns false.
|
The value is missing and you want to provide a fallback or handle an optional value. |
invalid
|
expression.oclIsUndefined() returns true; expression.oclIsInvalid() returns true.
|
You must detect invalid separately from null.
|
Example: an expression that returns null
safeCast returns null when the object is not an instance of the target subtype. In this example, self.address.safeCast(USAddress) is null when self.address is not a USAddress:
if self.address.safeCast(USAddress).oclIsUndefined() then
'No US address'
else
self.address.safeCast(USAddress).state
endifChoose the test that matches your rule
Use oclIsUndefined() when null and invalid require the same result. For example, use it before showing fallback text for an unavailable value.
Use oclIsInvalid() when your rule must identify invalid and treat null differently. For example, test oclIsInvalid() before a separate oclIsUndefined() check when your expression needs one outcome for invalid and another for a missing value.
