2007年7月17日 星期二

Basic OCL Elements

Expressions, Types, and Values

Types in OCL are divided into the following groups:


  • Predefined types, as defined in the standard library, including the following:

  • Basic types: Integer, Real, String, and Boolean

  • Collection types: Collection, Set, Bag, OrderedSet, and Sequence


  • User-defined types

  • Defined by the user in the UML diagrams
  • Every instantiable model element in a UML diagram is automatically a type in OCL


  • E.g., each class, interface, component, or datatype


  • Basic Types and Operators


    The Boolean Type

    Example: The result of the following sample expression is true if for every service it can be said that when it offers bonus points it never burns bonus points

    context Service
    inv: self.pointsEarned > 0 implies not (self.pointsBurned = 0)

    Another interesting operation on the Boolean type is the if-then-else
    if ‹boolean OCL expression›
    then ‹OCL expression›
    else ‹OCL expression›
    endif


    The Integer and Real Types

    All these examples are expressions of the Boolean type, which result in true:

    2654 * 4.3 + 101 = 11513.2
    (3.2).floor() / 3 = 1
    1.175 * (-8.9).abs() - 10 = 0.4575
    12 > 22.7 = false
    12.max(33) = 33
    33.max(12) = 33
    13.mod(2) = 1
    13.div(2) = 6
    33.7.min(12) = 12.0
    -24.abs() = 24
    (-2.4).floor() = -3



    The String Types
    All these examples are expressions of the Boolean type, and result in true:

    'Anneke'.size() = 6
    ('Anneke' = 'Jos') = false
    'Anneke '.concat('and Jos') = 'Anneke and Jos'
    'Anneke'.toUpper() = 'ANNEKE'
    'Anneke'.toLower() = 'anneke'
    'Anneke and Jos'.substring(12, 14) = 'Jos'



    Precedence Rules


    Comments
    An OCL line comment begins with two hyphens. All text from the hyphens to the end of the line is considered to be a comment. Comments longer than one line may be enclosed between /* and */.

    -- the expression 20 * 5 + 4 should be evaluated here
    20 * 5 + 4 -- this is a comment
    /* this is a very long comment that does not enlighten the reader one bit about what the expression is really about */
    20 * -- this is a comment 5 + 4 (invalid OCL expression)

    Related Articles

    0 意見:

    技術提供:Blogger.