Download presentation
Presentation is loading. Please wait.
1
The Object Constraint Language
OCL The Object Constraint Language
2
Content HISTORY COLLECTION TYPES AND QUERING IN OCL
FORMAL LANGUAGE - STATEMENT EXAMPLES CONSTRAINTS AND OCL EXAMPLES
3
History Formal Languages like Z, have for a long time (ca ) 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.
4
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 an object of a class a slot with value a link between objects a formula instantiated
5
Content HISTORY COLLECTION TYPES AND QUERING IN OCL
FORMAL LANGUAGE - STATEMENT EXAMPLES CONSTRAINTS AND OCL EXAMPLES
6
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.
7
model instance (example)
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… member Gang GangMember 1..* 0..* model instance (example) When self is A:Gang then self.member is: :Set(GangMember) self.member self M1:GangMember A:Gang M3:GangMember M2:GangMember B:Gang M4:GangMember M5:GangMember
8
One More Navigation Example
Model alternative Quiz Question AnswerAlternative * * * 1..* contex Quiz inv: self.question.alternative… incomplete constraint self self.question self.question.alternative quiz2:Quiz q3:Question a4:AnswerAlternative 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 self.question self.question.alternative What correspond to self in Java? Example function as quering – not constraint becaouse? quiz1:Quiz q1:Question a1:AnswerAlternative a2:AnswerAlternative q2:Question a3:AnswerAlternative
10
The Example continues… Using OCL as a Query Language: Finding all members no matter which Gang they belong to OCL is case sensitive! Select in menu! Using USE as OCL tool.
11
Single navigation of an association results in a Set.
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
12
Allowing a Member to be Member of Several Gangs
belonging to two gangs
13
Allowing a Member to be Member of Several Gangs
belonging to two gangs
14
asSet
16
Finding all members of the B-gang
17
Content HISTORY COLLECTION TYPES AND QUERING IN OCL
FORMAL LANGUAGE - STATEMENT EXAMPLES CONSTRAINTS AND OCL EXAMPLES
18
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.
19
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 Also: a Integer | a-1 < a
20
Example: Transitivity
In logic and mathematics, a binary relation R is transitive if xRy and yRz together imply xRz 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? quantifier is called the universal quantifier and it stands for “all” or “for all”. is the existential quantifier and it stands for exists.
21
Content HISTORY COLLECTION TYPES AND QUERING IN OCL
FORMAL LANGUAGE - STATEMENT EXAMPLES CONSTRAINTS AND OCL EXAMPLES
22
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… assertion uttales: assersjen A constraint is an assertion, not an executable mechanism.
23
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). OCL give you power to improve the documentation in a precise (OCL is a formal language) and unambiguous way (in contrast to natural language).
24
Where To Use OCL? Invariants on classes and types
Precondition and postconditions on operations (methods) Constraints on operations: operation=expression (the return value) …
25
Content HISTORY COLLECTION TYPES AND QUERING IN OCL
FORMAL LANGUAGE - STATEMENT EXAMPLES CONSTRAINTS AND OCL EXAMPLES
26
An Invariant Example: Use of Size
An invariant is predicate that is always true (i.e., when the system is at “rest”.) Gang name : String GangMember +member isArmy : Boolean 1..* 1..* 0..* 0..* register(newMember : GangMember) -- The gang is an army if there are more than 100 members. context Gang inv oneSimpleConstraint: self.isArmy = (member->size() > 100)
27
Use of pre –and post-condition
Gang name : String GangMember +member isArmy : Boolean 1..* 1..* 0..* register(newMember : GangMember) -- The gang has grown with one when a new member has been added. -- accesses the value before executing the operation context Gang :: register(newMember : GangMember) pre: not member->includes(newMember) post : member =
28
Comments to Previous Slide
collection->includes(object: T): Boolean is True if object is an element of collection. E.g.: = true refers to the value of p before the operation was executed. E.g.: 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.: =
29
Using OCL in USE What we want: An account has either a person as owner or a cooperation as owner
context Account inv : personOwner->size() = 1 implies cooperationOwner->size() = 0
32
What is this? This should not be allowed – this is not xor!
33
Modify the Constraint: context Account inv: (personOwner->size() = 1 implies cooperationOwner->size() = 0) and (personOwner->size() = 0 implies cooperationOwner->size() = 1)
34
forAll Variations (math. notation: )
employee Person Company * * age : Integer { context Company inv: self.employee->forAll( age <= 70 ) self.employee->forAll( p | p.age <= 70 ) self.employee->forAll( p : Person | p.age <= 70 ) Equivalent Constraints
35
Example: exists (math. notation: )
collection exists(exp : OclExpression) : 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)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.