10.10 The OclAny Type


10.10 The OclAny Type

A number of operations are useful for every type of OCL instance. Therefore, the OCL Standard Library has a type called OclAny . When OCL expressions are being evaluated, the type OclAny is considered to be the supertype of all types in the model. All predefined types and all user -defined types inherit the features of OclAny .

To avoid name conflicts between features from the model and features inherited from OclAny, all names of the features of OclAny start with ocl . Although theoretically there may still be name conflicts, you can avoid them by not using the ocl prefix in your user-defined types. The operations defined for all OCL objects are shown in Table 10-2.

Table 10-2. Operations on any OCL instance

Expression

Result Type

object = (object2 : OclAny)

Boolean

object <> (object2 : OclAny)

Boolean

object.oclIsUndefined()

Boolean

object.oclIsKindOf(type : OclType)

Boolean

object.oclIsTypeOf(type : OclType)

Boolean

object.oclIsNew()

Boolean

object.oclInState()

Boolean

object.oclAsType(type : OclType)

type

object.oclInState( str: StateName )

Boolean

type::allInstances()

Set(type)

10.10.1 Operations on OclAny

The equals and notEquals operations are redefined for most types in the Ocl Standard Library, and are explained elsewhere in this book. The oclIsNew operation is explained in Section 10.1.3, the oclAsType operation is explained in Section 10.7, and the oclIsUndefined operation is explained in Section 10.6. Two other operations allow access to the metalevel of your model, which can be useful for advanced modelers.

The oclIsTypeOf operation results in true only if the type of the object is identical to the argument. The oclIsKindOf operation results in true if the type of the object is identical to the argument, or identical to any of the subtypes of the argument. The following examples, which are based on Figure 10-4, show the difference between the oclIsKindOf and oclIsTypeOf operations. For a Transaction, the following invariants are valid:

  context  Transaction  inv  : self.oclIsKindOf(Transaction) = true  inv  : self.oclIsTypeOf(Transaction) = true  inv  : self.oclIsTypeOf(Burning) = false  inv  : self.oclIsKindOf(Burning) = false 
Figure 10-4. Difference between oclIsKindOf and oclIsTypeOf

graphics/10fig04.gif

For the subclass Burning, the following invariants are valid:

  context  Burning  inv  : self.oclIsKindOf(Transaction) = true  inv  : self.oclIsTypeOf(Transaction) = false  inv  : self.oclIsTypeOf(Burning) = true  inv  : self.oclIsKindOf(Burning) = true  inv  : self.oclIsTypeOf(Earning) = false  inv  : self.oclIsKindOf(Earning) = false 

The oclIsKindOf and oclIsTypeOf operations are often used to specify invariants on subclasses. For example, Figure 10-2 shows a general association between FruitPie and PieceOfFruit . For the different subtypes of FruitPie , only specific subtypes of PieceOfFruit are acceptable.

Using oclType or one of the other operations, you can state the invariants for the subtypes of FruitPie: ApplePie and PeachPie :

  context  ApplePie  inv  : self.ingredient->forAll(oclIsKindOf(Apple))  context  PeachPie  inv  : self.ingredient->forAll(oclIsKindOf(Peach)) 
The oclInState Operation

The oclInState operation is an operation that has been defined on the OclAny type; but in fact it is useful only for types that might have a statechart attached. It takes as a parameter a state name, and results in true if the object is in that state. For nested states, the statenames can be combined using the double colon (::). In the example statemachine shown in Figure 10-5, the statenames used can be On, Off , Off::Standby, Off::NoPower . If the classifier of object has the aforementioned associated statemachine, valid OCL expressions are as follows :

 object.oclInState(On) object.oclInState(Off) object.oclInstate(Off::Standby) object.oclInState(Off::NoPower) 
Figure 10-5. Example statechart

graphics/10fig05.gif

If multiple statemachines are attached to the object's classifier, then the statename can be prefixed with the name of the statemachine containing the state and the double colon (::), as with nested states.

10.10.2 The allInstances Operation

The allInstances operation is a class operation that can be applied only to classes. In all other cases, it will result in undefined. For a class, it results in a set of all instances of that class, including all instances of its subclasses. The following expression results in the set of all instances of class Transaction , including all instances of type Burning and Earning :

 Transaction::allInstances() 

The use of allInstances is discouraged (see Section 3.10.3).



Object Constraint Language, The. Getting Your Models Ready for MDA
The Object Constraint Language: Getting Your Models Ready for MDA (2nd Edition)
ISBN: 0321179366
EAN: 2147483647
Year: 2003
Pages: 137

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net