10.8 Type Conformance Rules


OCL is a typed language. When you are constructing a new expression from other expressions, the sub expressions and the operator must "fit." If they don't, the expression is invalid, and any parser will complain that the expression contains a type conformance error. The definition of conformance that is used within OCL is as follows :

Type1 conforms to Type2 if an instance of Type1 can be substituted at each place where an instance of Type2 is expected.

These are the type conformance rules used in OCL expressions:

  1. Type1 conforms to Type2 when they are identical.

  2. Type1 conforms to Type2 when Type1 is a subtype of Type2 .

  3. Each type is a subtype of OclAny .

  4. Type conformance is transitive; that is, if Type1 conforms to Type2 and Type2 conforms to Type3, then Type1 conforms to Type3 . Together with the first rule, this means that a type conforms to any of its predecessors in an inheritance tree.

  5. Integer is a subtype of Real and therefore conforms to Real .

  6. Every type Collection(T) is a subtype of OclAny . The types Set(T), Bag(T), OrderedSet(T), and Sequence(T) are all subtypes of Collection(T) .

  7. Collection(Type1) conforms to Collection(Type2) if Type1 conforms to Type2 .

  8. Set(T) does not conform to OrderedSet(T), Bag(T), or Sequence(T) .

  9. OrderedSet(T) does not conform to Set(T), Bag(T) , or Sequence(T) .

  10. Bag(T) does not conform to Set(T), OrderedSet(T) , or Sequence(T).

  11. Sequence(T) does not conform to Set(T), OrderedSet(T), or Bag(T) .

For example, in Figure 10-2, PeachPie and ApplePie are two separate subtypes of FruitPie . In this example, the following statements are true:

  • Set(ApplePie) conforms to Set(FruitPie) .

  • Set(ApplePie) conforms to Collection(ApplePie) .

  • Set(ApplePie) conforms to Collection(FruitPie) .

  • Set(ApplePie) does not conform to Bag(ApplePie) .

  • Bag(ApplePie) does not conform to Set(ApplePie) .

  • Set(AppePie) does not conform to Set(PeachPie) .

Figure 10-2. Invariants on subtypes

graphics/10fig02.gif



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