You can use oclIsTypeOf in an OCL expression to test whether an object has one exact runtime class; use it when your model contains inheritance and a subtype must not match.
Syntax
object.oclIsTypeOf(TypeName)| Part | Meaning |
|---|---|
object
|
The object whose runtime type you want to test. In a class context, this is often self.
|
TypeName
|
The class to compare with the object's exact runtime class. |
| Result | A Boolean value: true or false.
|
How it works
oclIsTypeOf returns true only when the object is exactly the specified class.
It returns false when the object is an instance of a subclass of the specified class. This is the key difference from oclIsKindOf, which returns true for the specified class and its subclasses.
Example: exact type versus inheritance
Assume this inheritance structure:
Vehicle
└── Car
└── ElectricCar
For an object whose runtime type is Car:
self.oclIsTypeOf(Car) -- true
self.oclIsTypeOf(Vehicle) -- false
self.oclIsTypeOf(ElectricCar) -- falseFor an object whose runtime type is ElectricCar:
self.oclIsTypeOf(ElectricCar) -- true
self.oclIsTypeOf(Car) -- false
self.oclIsTypeOf(Vehicle) -- falseAlthough ElectricCar inherits from Car, it is not exactly a Car for oclIsTypeOf.
Choose the correct type test
| Requirement | Operator | Example for an ElectricCar
|
|---|---|---|
| Match one exact class only | oclIsTypeOf
|
self.oclIsTypeOf(Car) returns false.
|
| Match a class and any of its subclasses | oclIsKindOf
|
self.oclIsKindOf(Car) returns true.
|
Use case
Use oclIsTypeOf when a rule applies only to objects implemented by one specific class. For example, a condition defined on Vehicle can identify objects that are exactly Car and exclude future specializations such as ElectricCar:
self.oclIsTypeOf(Car)If the same rule must also apply to ElectricCar and other subclasses of Car, use self.oclIsKindOf(Car) instead.
