Jan Pettersen Nytun UIA page 1 Content HISTORY

  • Slides: 35
Download presentation
Jan Pettersen Nytun, UIA, page 1

Jan Pettersen Nytun, UIA, page 1

Content • HISTORY • COLLECTION TYPES AND QUERING IN OCL • FORMAL LANGUAGE -

Content • HISTORY • COLLECTION TYPES AND QUERING IN OCL • FORMAL LANGUAGE - STATEMENT EXAMPLES • CONSTRAINTS AND OCL • EXAMPLES Jan Pettersen Nytun, UIA, page 2

History • Formal Languages like Z, have for a long time (ca. 1970) been

History • Formal Languages like Z, have for a long time (ca. 1970) been used to describe things in a precise and unambiguous way. • OCL was developed by IBM (1995) and is a part of UML. • It emphasize precision and simplicity There is no use of special mathematical symbols. Jan Pettersen Nytun, UIA, page 3

Aligning OCL with the OO-Concepts Level Class Property Ass. OCL 2 (metamodel) the class

Aligning OCL with the OO-Concepts Level Class Property Ass. OCL 2 (metamodel) the class concept the property concept association concept an OCL construct as a concept 1 a specific class a specific property a specific association OCL Formulas 0 an object of a class a slot with value a link between objects a formula instantiated Jan Pettersen Nytun, UIA, page 4

Content • HISTORY • COLLECTION TYPES AND QUERING IN OCL • FORMAL LANGUAGE -

Content • HISTORY • COLLECTION TYPES AND QUERING IN OCL • FORMAL LANGUAGE - STATEMENT EXAMPLES • CONSTRAINTS AND OCL • EXAMPLES Jan Pettersen Nytun, UIA, page 5

Some Collection Types in OCL • A Set is the mathematical set (no duplicate

Some Collection Types in OCL • A Set is the mathematical set (no duplicate elements). • A Bag is like a set, which may contain duplicates (i. e. , the same element may be in a bag twice or more). • A Sequence is like a Bag in which the elements are ordered. Both Bags and Sets have no order defined on them. Jan Pettersen Nytun, UIA, page 6

Self [2]: ”Each OCL expression is written in the context of an instance of

Self [2]: ”Each OCL expression is written in the context of an instance of a specific type. In an OCL expression the name self is used to refer to the contextual instance. ” context Gang inv: self. member… Gang member 1. . * 0. . * Gang. Member When self is A: Gang then self. member is: Set{@M 1, @M 2} : Set(Gang. Member) self. member self M 3: Gang. Member B: Gang model instance (example) M 1: Gang. Member A: Gang M 2: Gang. Member M 4: Gang. Member M 5: Gang. Member

One More Navigation Example Model Quiz * * Question contex Quiz inv: self. question.

One More Navigation Example Model Quiz * * Question contex Quiz inv: self. question. alternative… When the complete constraint is checked, then self is bound to one Quiz object at a time and then the constraint is checked for that bound object. self. question alternative * 1. . * Answer. Alternative incomplete constraint self. question. alternative quiz 2: Quiz q 3: Question a 4: Answer. Alternative self. question. alternative quiz 1: Quiz q 1: Question a 1: Answer. Alternative a 2: Answer. Alternative q 2: Question a 3: Answer. Alternative

The Example continues… Using OCL as a Query Language: Finding all members no matter

The Example continues… Using OCL as a Query Language: Finding all members no matter which Gang they belong to OCL is case sensitive! Using USE as OCL tool.

Alternative way to find all members no matter which Gang they belong to Single

Alternative way to find all members no matter which Gang they belong to Single navigation of an association results in a Set. Combined navigations results in a Bag

Allowing a Member to be Member of Several Gangs belonging to two gangs Jan

Allowing a Member to be Member of Several Gangs belonging to two gangs Jan Pettersen Nytun, UIA, page 12

Allowing a Member to be Member of Several Gangs belonging to two gangs Jan

Allowing a Member to be Member of Several Gangs belonging to two gangs Jan Pettersen Nytun, UIA, page 13

as. Set Jan Pettersen Nytun, UIA, page 14

as. Set Jan Pettersen Nytun, UIA, page 14

Jan Pettersen Nytun, UIA, page 15

Jan Pettersen Nytun, UIA, page 15

Finding all members of the B-gang

Finding all members of the B-gang

Content • HISTORY • COLLECTION TYPES AND QUERING IN OCL • FORMAL LANGUAGE -

Content • HISTORY • COLLECTION TYPES AND QUERING IN OCL • FORMAL LANGUAGE - STATEMENT EXAMPLES • CONSTRAINTS AND OCL • EXAMPLES Jan Pettersen Nytun, UIA, page 17

Logical implication – p implies q p q If p is true then also

Logical implication – p implies q p q If p is true then also q must be true. “p q” is called a predicate A predicate is a statement that may be true or false depending on the values of its variables.

Universal Quantification: In predicate logic, universal quantification formalizes the notion that a logical predicate

Universal Quantification: In predicate logic, universal quantification formalizes the notion that a logical predicate is true for all the values that can be bound to the variable. E. g. : a Integer | (a > 10) (a + 1 > 10) Can be read as: For all a of type Integer the following is true: if a is greater then 10 then a +1 is also greater then 10 Jan Pettersen Nytun, UIA, page 19

Example: Transitivity In logic and mathematics, a binary relation R is transitive if x.

Example: Transitivity In logic and mathematics, a binary relation R is transitive if x. Ry and y. Rz together imply x. Rz In predicate logic: a, b, c X | a R b b R c a R c Less than (<) is a transitive relation: a, b, c Integer | a < b b < c a < c In english: For all Integer a, b, c the following is true if a < b and b < c then also a < c (e. g. , 1<2 2<4 1<4) Is mother of a transitive relation? Jan Pettersen Nytun, UIA, page 20

Content • HISTORY • COLLECTION TYPES AND QUERING IN OCL • FORMAL LANGUAGE -

Content • HISTORY • COLLECTION TYPES AND QUERING IN OCL • FORMAL LANGUAGE - STATEMENT EXAMPLES • CONSTRAINTS AND OCL • EXAMPLES Jan Pettersen Nytun, UIA, page 21

Constraint [1]: … condition or restriction represented as an expression…. can be attached to

Constraint [1]: … condition or restriction represented as an expression…. can be attached to any UML model element…. indicates a restriction that must be enforced by correct design of the system… Jan Pettersen Nytun, UIA, page 22

Why OCL? • The power of the graphical part of UML is limited! •

Why OCL? • The power of the graphical part of UML is limited! • OCL gives you power to improve the documentation in a precise and unambiguous way. • OCL parsers (/ evaluators) ensure that the constraints are meaningful and well formed within the model. • To do MDD the models have to be unambiguous (e. g. , when doing code generation, transformations). Jan Pettersen Nytun, UIA, page 23

Where To Use OCL? ü Invariants on classes and types ü Precondition and postconditions

Where To Use OCL? ü Invariants on classes and types ü Precondition and postconditions on operations (methods) ü Constraints on operations: operation=expression (the return value) ü… Jan Pettersen Nytun, UIA, page 24

Content • HISTORY • COLLECTION TYPES AND QUERING IN OCL • FORMAL LANGUAGE -

Content • HISTORY • COLLECTION TYPES AND QUERING IN OCL • FORMAL LANGUAGE - STATEMENT EXAMPLES • CONSTRAINTS AND OCL • EXAMPLES Jan Pettersen Nytun, UIA, page 25

An Invariant Example: Use of Size An invariant is predicate that is always true

An Invariant Example: Use of Size An invariant is predicate that is always true (i. e. , when the system is at “rest”. ) Gang. Member name : String is. Army : Boolean +member 1. . * 0. . * register(new. Member : Gang. Member) -- The gang is an army if there are more than 100 members. context Gang inv one. Simple. Constraint: self. is. Army = (member->size() > 100) Jan Pettersen Nytun, UIA, page 26

Use of pre –and post-condition Gang. Member +member 1. . * 0. . *

Use of pre –and post-condition Gang. Member +member 1. . * 0. . * name : String is. Army : Boolean register(new. Member : Gang. Member) -- The gang has grown with one when a new member has been added. -- NB! @pre accesses the value before executing the operation context Gang : : register(new. Member : Gang. Member) pre: not member->includes(new. Member) post : member = member@pre->including(new. Member) Jan Pettersen Nytun, UIA, page 27

Comments to Previous Slide collection->includes(object: T): Boolean is True if object is an element

Comments to Previous Slide collection->includes(object: T): Boolean is True if object is an element of collection. E. g. : Set{@m 1, @m 2}->includes(@m 2) = true p@pre refers to the value of p before the operation was executed. E. g. : member@pre is the set member before the operation was executed. collection->including(object: T): Bag(T) is the bag with all elements of collection plus object. E. g. : Set{@m 1, @m 2}->including(@m 3) = Set{@m 1, @m 2, @m 3}. Jan Pettersen Nytun, UIA, page 28

Using OCL in USE What we want: An account has either a person as

Using OCL in USE What we want: An account has either a person as owner or a cooperation as owner context Account inv : person. Owner->size() = 1 implies cooperation. Owner->size() = 0

What is this? This should not be allowed – this is not xor!

What is this? This should not be allowed – this is not xor!

Modify the Constraint: context Account inv: (person. Owner->size() = 1 implies cooperation. Owner->size() =

Modify the Constraint: context Account inv: (person. Owner->size() = 1 implies cooperation. Owner->size() = 0) and (person. Owner->size() = 0 implies cooperation. Owner->size() = 1)

for. All Variations (math. notation: ) Person age : Integer employee * * Company

for. All Variations (math. notation: ) Person age : Integer employee * * Company context Company inv: self. employee->for. All( age <= 70 ) { Equivalent Constraints context Company inv: self. employee->for. All( p | p. age <= 70 ) context Company inv: self. employee->for. All( p : Person | p. age <= 70 ) Jan Pettersen Nytun, UIA, page 34

Example: exists (math. notation: ) collection exists(exp : Ocl. Expression) : Boolean True if

Example: exists (math. notation: ) collection exists(exp : Ocl. Expression) : Boolean True if exp is true for one elements of the collection -- Every Gang must have a member with a car contex Gang inv: member->exists(car->size()>0) Jan Pettersen Nytun, UIA, page 35