o cl by example lecture

Upload: thomasb19747048

Post on 03-Apr-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 o Cl by Example Lecture

    1/60

    Department of Computer Science, Institute for Software and Multimedia Technology

    OCL

    by Example

    .

  • 7/28/2019 o Cl by Example Lecture

    2/60

    In theory, there is no difference

    between theory and practice.

    But, in practice, there is.

    Jan L. A. van de Snepscheut/Yogi Bera

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 2

  • 7/28/2019 o Cl by Example Lecture

    3/60

    Main Goals of the Lecture

    Bridge the gap between practically used softwarespecifications (UML) and formal languages

    , ,

    Learn how to specify semantics using OCL

    Learn what are interestin OCL use cases

    Inform what OCL tools can already be used

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 3

  • 7/28/2019 o Cl by Example Lecture

    4/60

    Foundation: Assertions

    An assertion is a predicate (i.e., a truefalse statement)

    placed in a program to indicate that the developer thinksa e pre ca e s a ways rue a a p ace pe a .

    Usa e in

    Hoare logic [Hoare 1969] Design by contract [Meyer 1986, Eiffel]

    - asser , , , ,)

    During the development cycle (debugging) Static assertions at compile time

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 4

  • 7/28/2019 o Cl by Example Lecture

    5/60

    Object Constraint

    Model-based assertion

    [Warmer and Kleppe] define a constraint as follows:

    of (part of) an object-oriented model or system.

    OCL as specification language for object constraints

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 5

  • 7/28/2019 o Cl by Example Lecture

    6/60

    History of OCL

    Developed at IBM in 1995 originally as a businessengineering language

    Part of the official OMG standard for UML (fromversion 1.1 on)

    Used for precisely defining the well-formedness rules(WFRs) for UML and further OMG-related metamodels

    .

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 6

  • 7/28/2019 o Cl by Example Lecture

    7/60

    OCL (Object Constraint Language)

    Extends the Unified Modeling Language (UML)

    Formal lan ua e for the definition of constraints andqueries on UML models

    Declarative

    Side effect free Add precise semantics to visual (UML-) models

    Meanwhile generally accepted

    Many extensions such as for temporal constraints Core Language of other OMG languages (QVT, PRR)

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 7

  • 7/28/2019 o Cl by Example Lecture

    8/60

    Literature

    1 Warmer J. Kle e A.: The Ob ect Constraint Lan ua e.Precise Modeling with UML. Addison-Wesley, 1999

    [2] Warmer, J., Kleppe, A.: The Object Constraint LanguageSecond Edition.

    Getting Your Models Ready For MDA. Addison-Wesley, 2003[3] OMG UML specification,

    _ _g.htm#UML

    [4] OMG UML 2.0 OCL,. . .

    [5] Heinrich Humann: Formal Specification of SoftwareSystems. Course, 2000, Technische Universitt Dresden

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 8

  • 7/28/2019 o Cl by Example Lecture

    9/60

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 9

  • 7/28/2019 o Cl by Example Lecture

    10/60

    Constraint

    Definition

    A constraint is a restriction on one or morevalues of (part of) an object-oriented model or

    .

    A constraint is formulated on the level of classes, butits semantics is applied on the level of objects.

    originally formulated in the syntactic context of a UML. .

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 10

  • 7/28/2019 o Cl by Example Lecture

    11/60

    Definition

    An invariant is a constraint that should be true for anobject during its complete lifetime.

    Invariants often represent rules that should hold for thereal-life objects after which the software objects aremodeled.

    context

    inv []:

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 11

  • 7/28/2019 o Cl by Example Lecture

    12/60

    OCL/UML By Example

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 12

  • 7/28/2019 o Cl by Example Lecture

    13/60

    -

    context Meeting inv: self.end > self.start

    Equivalent Formulations

    -- "self" always refers to the object identifier from which the

    constraint is evaluated.

    context Meeting inv startEndConstraint:

    self.end > self.start

    -- Names can be given to the constraint

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 13

  • 7/28/2019 o Cl by Example Lecture

    14/60

    r

    operation without stating an algorithm or implementation

    Are attached to an operation in a class diagram

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 14

  • 7/28/2019 o Cl by Example Lecture

    15/60

    Precondition

    Constraint that must be true just prior to the executionof an operation

    Syntax

    pre []:

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 15

  • 7/28/2019 o Cl by Example Lecture

    16/60

    Precondition - Examples

    context Meeting::shift(d:Integer)

    pre: self.isConfirmed = false

    context Meeting::shift(d:Integer)

    context Meeting::shift(d:Integer)pre: self.isConfirmed = false and d>0

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 16

  • 7/28/2019 o Cl by Example Lecture

    17/60

    Definition ons ra n a mus e rue us a er o e execu on

    of an operation

    Postconditions are the way how the actual effect of anoperation is described in OCL.

    Syntax

    context :: < arameters>

    post []:

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 17

  • 7/28/2019 o Cl by Example Lecture

    18/60

    Postcondition - Examples

    context Meeting::duration():Integer

    post: result = self.end self.start

    -- eywor resu re ers o e resu o e opera on

    context Meeting::confirm()

    post: self.isConfirmed = true

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 18

  • 7/28/2019 o Cl by Example Lecture

    19/60

    Postcondition Examples (cont.)

    context Meeting::shift(d:Integer)

    post: start = start@pre +d and end = end@pre + d

    --

    -- which is to be evaluated in the original state-- before execution of the operation

    -- startrefers to the value upon completion of the operation

    --

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 19

  • 7/28/2019 o Cl by Example Lecture

    20/60

    Postcondition Examples (cont.)

    messaging only in postconditions

    hasSent (^) operator

    context Subject::hasChanged()

    post: observer^update(2,4)

    /* standard observer attern:

    results in true if an update message with arguments 2 and 4

    was sent to the observer object during execution of the

    o eration hasChan ed

    */

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 20

  • 7/28/2019 o Cl by Example Lecture

    21/60

    Buildin OCL Ex ressions 1

    Boolean expressions

    Standard library ofprimitive types and associated operations

    Basic types (Boolean, Integer, Real, String)

    Collection

    Set Ordered Set (only OCL2)

    Bag

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 21

  • 7/28/2019 o Cl by Example Lecture

    22/60

    Building OCL Expressions (2)

    User defined types (OCLType)

    ass ype o e ype :

    Classifier in a class diagram (implicitly defined)

    Generalisation among classiefiers leads to Supertypes

    A class has the following Features: Attributes ( s ta r t )

    Class attributes (Dat e: : t oday)

    Class operations Association ends (navigation expressions)

    Enumeration t e Gender Gender : : mal e

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 22

  • 7/28/2019 o Cl by Example Lecture

    23/60

    OCL Type Hierarchy

    T

    T T T T

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 23

  • 7/28/2019 o Cl by Example Lecture

    24/60

    OCL Type Conformance Rules

    OCL is a strongly typed language .

    The parser has to check the conformance:

    Type1 con orms to Type2 i an instance o Type1 can esubstituted at each place where an instance of Type2 isexpected.

    General rules:

    ac ype con orms o eac o s super ypes.

    Type conformance is transitive.

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 24

    .

  • 7/28/2019 o Cl by Example Lecture

    25/60

    OCL Constraints and Inheritance

    Constraints are inherited.

    Wherever an instance of a class is expected, one canalways substitute an instance of any of its subclasses.

    An invariant for a superclass is inherited by its subclass.

    A subclass may strengthen the invariant but cannot weakenit.

    A precondition may be weakened but not strengthened ina redefinition of an operation in a subclass.

    A postcondition may be strengthened but not weakened ina redefinition of an operation in a subclass.

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 25

  • 7/28/2019 o Cl by Example Lecture

    26/60

    Navigation Expressions

    from one object in the model to another object.

    Navigations are treated as attributes (dot-Notation).

    User defined type

    (association end with multiplicity at most 1) Collection

    (association end with multiplicity > 1)

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 26

  • 7/28/2019 o Cl by Example Lecture

    27/60

    av gat on xpress ons - xamp es

    User defined type

    Navigation from Meet i ng to moder at orresults in typeTeammember

    inv: self.moderator.gender = Gender::female

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 27

  • 7/28/2019 o Cl by Example Lecture

    28/60

    Navigation Expressions - Examples

    Collection

    Navigation von Meeting to par t i ci pant s

    context Meeting

    inv: self->collect(participants)->size()>=2

    context Meeting inv: self.participants->size()>=2

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 28

  • 7/28/2019 o Cl by Example Lecture

    29/60

    Collection Operations (1)

    22 operations with variant meaning depending on the

    collection type such as

    equals (=) and not equals operation ()

    Transformations (asBag(), asSet(), asOrderedSet(),asSequence())

    including(object) and excluding(object)

    Set {Bag{1, 2, 2}, Bag{2}} Set {1, 2}

    Typical set operations, , ,

    Operations on ordered collections only (OrderedSet,Sequence) (such as first(), last(), indexOf())

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 29

  • 7/28/2019 o Cl by Example Lecture

    30/60

    Collection Operations (2)

    Loop operations (Iterators) on all collection types

    collect(expr)

    exists(expr)

    forAll(expr)isUnique(expr)

    one(expr)

    select(expr)

    sortedBy(expr)

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 30

  • 7/28/2019 o Cl by Example Lecture

    31/60

    Loo O eration iterate

    Collection->iterate( element : Type1;

    | }

    All other loop operations can be described as a specialcase ofiterate( ) such as in the following simpleexample:

    Set {1,2,3}->sum()

    Set{1,2,3}->

    iterate{i: Integer, sum: Integer=0 | sum + i }

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 31

  • 7/28/2019 o Cl by Example Lecture

    32/60

    Further Examples for Collection Operations (1)

    A teammeetin has to be or anized for a whole team( forAll()) :

    context Teammeeting

    inv: participants->forAll(team=self.for)

    contextMeeting inv: oclIsTypeOf(Teammeeting)

    implies participants->forAll(team=self.for)

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 32

  • 7/28/2019 o Cl by Example Lecture

    33/60

    Further Examples for collection operations (2)

    Postconditions select :

    context Teammember::numMeeting():Integer

    post: result=meetings->size()

    post:

    result=meetin s->select(isConfirmed)->size()

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 33

  • 7/28/2019 o Cl by Example Lecture

    34/60

    Flattening of Collections

    Automatic flattenin rule for all nested collections

    self.participants.meetings

    in the context Meeting

    sel f . par t i ci pant s delivers a Set(Person)

    self. artici ants.meetin s delivers aBag(Set(Person)

    Results in a Bag(Person)

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 34

  • 7/28/2019 o Cl by Example Lecture

    35/60

    Derivation Rule derive OCL2

    context Team::size:Integer

    -

    Derived association (conf l i ct )

    defines a set of meetings that are in conflict with each other

    derive: select(m|mself and self.inConflict(m))

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 35

  • 7/28/2019 o Cl by Example Lecture

    36/60

    Initial Value (init, OCL2)

    Examp es

    context Meeting::isConfirmed : Boolean

    init: false

    init: Set{}

    Note that an initial value must be valid only at the objectcreation time!

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 36

  • 7/28/2019 o Cl by Example Lecture

    37/60

    Query Operation (body, OCL2)

    Operations that do not change the state of the system

    Can be used as a query language

    ower o

    Example

    context

    Teammember:: etMeetin Titles(): Ba (Strin )body: meetings->collect(title)

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 37

  • 7/28/2019 o Cl by Example Lecture

    38/60

    Let Expression (let)

    Interesting for complex expressions

    Define a local variable noConf l i ct that can be usedinstead of a sub-expression

    context Meeting inv:

    let noConflict : Boolean =

    -.

    m.isConfirmed implies not self.inConflict(m))

    in isConfirmed implies noConflict

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 38

  • 7/28/2019 o Cl by Example Lecture

    39/60

    Defining New Attributes and Operations(def, OCL2)

    Adding attributes and query operations to the model

    Helpful for the reuse of OCL expressions in severalconstraints

    context Meetin

    def: noConflict : Boolean =

    participants.meetings->forAll(m|mself and

    m.isConfirmed implies not

    self.inConflict(m))

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 39

  • 7/28/2019 o Cl by Example Lecture

    40/60

    Packaging OCL Expressions

    package MeetingExample

    context Meeting::isConfirmed : Boolean

    context Teammember:meetings : Set(Meetings)

    init: Set{}

    ..

    endpackage

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 40

  • 7/28/2019 o Cl by Example Lecture

    41/60

    No su ort for inconsistenc detection for OCL

    Frame Problem

    Operations are specified by what they change (in post-conditions), with the implicit assumption that everything

    else (the frame) remains unchanged

    Limited recursion

    allInstances() Problem:

    Per son. al l I nst ances( ) allowed

    not allowed for infinite types such asI nt eger . al l I nst ances( )

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 41

  • 7/28/2019 o Cl by Example Lecture

    42/60

    Building complete models with OCL

    Statechart diagram

    Activity diagram Com onent dia ram

    Use case diagram

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 42

  • 7/28/2019 o Cl by Example Lecture

    43/60

    OCL in Statecharts Example (oclInState())

    operation on all objects (Typ Ocl Any)

    oc n a e s: c a e : oo ean

    context Vector::removeElement(d:Data)

    pre: oclInState(notEmpty)

    post: size@pre = 1 implies oclInState(empty)

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 43

  • 7/28/2019 o Cl by Example Lecture

    44/60

    An OCL expression can evaluate to undefined (OclVoid)

    where no value is existent in the respective object

    Strictness Principle

    Whenever a subexpression of an OCL expressionevaluates to undefined then the whole term evaluates toundefined

    Exceptions

    rue or un e ne = rue

    False and undefined = False

    False implies undefined = True

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 44

  • 7/28/2019 o Cl by Example Lecture

    45/60

    The OclVoid Type

    Undefined value is the onl instance Operation for testing if the value of an expression is

    undefined

    oclIsUndefined(): Boolean

    --

    -- otherwise false

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 45

  • 7/28/2019 o Cl by Example Lecture

    46/60

    Some Tips for Writing OCL Expressions

    Constraints should be easy to read and write:

    Avoid complex navigation expressions

    Choose appropriate context

    Avoid allInstances()

    Use the collect shorthand

    Use association end names (role names) instead ofassociation names in modeling

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 46

  • 7/28/2019 o Cl by Example Lecture

    47/60

    Typical Use Cases for OCL

    Model Layer Examples

    Metamodels: {MOF-, Ecore-based} X {UML, CWM, ODM, SBVR, PRR, DSLs}

    M2(Metamodel)

    Specification ofWFRs in OMG standards

    Definition ofModeling Guidelines for DSLs

    Specification ofModel Transformations

    M1 (Model) Model Verification ( CASE-Tool)

    Execution of model transformations

    Specification ofBusiness Rules/Constraints

    Specification ofTest Cases

    M0Ob ects

    Evaluation of Business Rules/Constraints

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 47

  • 7/28/2019 o Cl by Example Lecture

    48/60

    Examples for OCL on Metamodel

    WFR in UML metamodel

    context Classifier inv:

    not self.allParents->includes self

    -- Generalization cycles are not allowed

    context Classifier inv SingleInheritance:

    self.generalization->size()

  • 7/28/2019 o Cl by Example Lecture

    49/60

    Some UML/OCL Tools

    12 OCL tools/libraries (see OCL Portal)

    MagicDraw Enterprise Edition v16.5 Borland Together 2008 (OCL/QVT)

    Eclipse MDT/OCL for EMF Based Models

    ArgoUML

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 49

  • 7/28/2019 o Cl by Example Lecture

    50/60

    Decennial Anniversary of Dresden OCL in 2009

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 50

  • 7/28/2019 o Cl by Example Lecture

    51/60

    Dresden OCL2 for Eclipse

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 51

  • 7/28/2019 o Cl by Example Lecture

    52/60

    Dresden OCL2 for Eclipse

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 52

  • 7/28/2019 o Cl by Example Lecture

    53/60

    XMI Import into Dresden OCL2 for Eclipse

    TopCased (EMF UML2 XMI)

    MagicDraw (EMF UML2 XMI)

    Visual Paradigm (EMF UML2 XMI)

    Eclipse UML2 / UML2 Tools (EMF UML2 XMI)

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 53

  • 7/28/2019 o Cl by Example Lecture

    54/60

    OCL Support in MagicDraw Enterprise Edition

    OCL validation rules(based on Dresden OCL2 Toolkit)

    1. Specification on UML metamodel (M2) /Verification on UML models (M1)

    2. Specification of Stereotypes (M2) /Verification of UML models (M1)

    3. Specification on UML models (M1) /Verification of UML instances (objects)

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 54

  • 7/28/2019 o Cl by Example Lecture

    55/60

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 55

  • 7/28/2019 o Cl by Example Lecture

    56/60

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 56

  • 7/28/2019 o Cl by Example Lecture

    57/60

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 57

  • 7/28/2019 o Cl by Example Lecture

    58/60

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 58

  • 7/28/2019 o Cl by Example Lecture

    59/60

    Acronyms

    OCL

    OMG

    Object Constraint Language

    Object Management Group

    PRR

    e a- ec ac y

    Production Rule Representation

    UML

    WFR

    Unified Modeling Language

    Well-Formedness Rule

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 59

  • 7/28/2019 o Cl by Example Lecture

    60/60

    Thank you

    or your a en on

    Dr. Birgit Demuth MINE Summer School, Nida, 2009 60