The Object Constraint Language

Slides:



Advertisements
Similar presentations
Formal Methods of Systems Specification Logical Specification of Hard- and Software Dr. Armin Wolf Fraunhofer Institut für Rechnerarchitektur.
Advertisements

1.3 Predicates and Quantifiers
The role of OCL in the Model Driven Architecture Jos Warmer Klasse Objecten
OCL2 April A presentation of OCL 2 Object Constraint Language Christian Hein, Fraunhofer FOKUS April 2006.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Axiomatic Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
CS 355 – Programming Languages
Feb 2003 R McFadyen1 Contracts (Ch 13) Used to help understand requirements more completely based on assertions; assertions are applicable to any.
Jan 23, Ron McFadyen1 SSD for a samplePOS Use Case Figure 13.1 Input Events invoke a system operation of the same name same idea as in object-oriented.
Formal Methods of Systems Specification Logical Specification of Hard- and Software Prof. Dr. Holger Schlingloff Institut für Informatik der.
Lecturer: Sebastian Coope Ashton Building, Room G.18 COMP 201 web-page: Lecture.
These courseware materials are to be used in conjunction with Software Engineering: A Practitioner’s Approach, 6/e and are provided with permission by.
1 Specifying Object Interfaces. 2 Major tasks in this stage: --are there any missing attributes or operations? --how can we reduce coupling, make interface.
CS 330 Programming Languages 09 / 18 / 2007 Instructor: Michael Eckmann.
Describing Syntax and Semantics
SEG4110 – Advanced Software Engineering and Reengineering TOPIC E Object Constraint Language (OCL)
Predicates & Quantifiers Goal: Introduce predicate logic, including existential & universal quantification Introduce translation between English sentences.
A presentation of OCL 2 Object Constraint Language Fraunhofer FOKUS.
1 COSC 4406 Software Engineering COSC 4406 Software Engineering Haibin Zhu, Ph.D. Dept. of Computer Science and mathematics, Nipissing University, 100.
111 Writing Protocols in OCL CS 4311 Jos B. Warmer and Anneke G. Kleppe, OCL: The Constraint Language of the UML, JOOP, May Jos B. Warmer and Anneke.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
These courseware materials are to be used in conjunction with Software Engineering: A Practitioner’s Approach, 6/e and are provided with permission by.
1 OCL The Role of OCL in UML. 2 רשימת הנושאים  מבוא  מרכיבי השפה  דוגמאות  מקורות.
These courseware materials are to be used in conjunction with Software Engineering: A Practitioner’s Approach, 6/e and are provided with permission by.
IM NTU Software Development Methods, Fall2006 Software Development Methods, Fall 2006 OCL 2006/12/ Object Constraint Language (OCL) Yih-Kuen Tsay.
Chapter 3 Part II Describing Syntax and Semantics.
Object Constraint Language
1 Kyung Hee University Constraints Spring Kyung Hee University Graphical Notations  Graphical notations are well suited for displaying structural.
Software Engineering 2 -Prakash Shrestha.
Copyright © Peter Cappello 2011 Predicates & Quantifiers.
UML Profile BY RAEF MOUSHEIMISH. Background Model is a description of system or part of a system using well- defined language. Model is a description.
CSC3315 (Spring 2009)1 CSC 3315 Languages & Compilers Hamid Harroud School of Science and Engineering, Akhawayn University
Interpreting the Object Constraint Presented by: Ed Kausmeyer.
Object Design More Design Patterns Object Constraint Language Object Design Specifying Interfaces Review Exam 2 CEN 4010 Class 18 – 11/03.
An association between class Flight and class Person, indicating that a certain group of persons are the passengers on a flight, will have multiplicity.
Cliquez pour modifier le style du titre Cliquez pour modifier les styles du texte du masque Deuxième niveau Troisième niveau Quatrième niveau Cinquième.
Jan Pettersen Nytun, UIA, page 1. Jan Pettersen Nytun, UIA, page 2 HISTORY COLLECTION TYPES AND QUERING IN OCL FORMAL LANGUAGE - STATEMENT EXAMPLES CONSTRAINTS.
Chapter 2 Sets and Functions.
Design by Contract Jim Fawcett CSE784 – Software Studio
Design by Contract Jim Fawcett CSE784 – Software Studio
Course Outcomes of Object Oriented Modeling Design (17630,C604)
Used to help understand requirements more completely
Extending UML.
GC211Data Structure Lecture2 Sara Alhajjam.
Topics: jGRASP editor ideosyncrasies assert debugger.
Predicates & Quantifiers
Introduction to Scripting
The Object Constraint Language
Chapter 9, Object Design: Object Constraint Language
Component-Level Design
Specifying Object Interfaces
Component-Level Design
Use of Mathematics using Technology (Maltlab)
Programming Languages 2nd edition Tucker and Noonan
Slides by Steve Armstrong LeTourneau University Longview, TX
LECTURE 11: Specifying Systems – State Diag’s & OCL
Defining Classes and Methods
MA/CSSE 474 More Math Review Theory of Computation
Protocols CS 4311 Wirfs Brock et al., Designing Object-Oriented Software, Prentice Hall, (Chapter 8) Meyer, B., Applying design by contract, Computer,
Tutorial 10 Programming with JavaScript
Predicates and Quantifiers
Binary Search and Loop invariants
ISWIM For Testing – A Model Driven Approach
Java Modeling Language (JML)
Object Oriented System Design Class Diagrams
Object Constraint Language (OCL)
Formal Methods in Software Engineering 1
Programming Languages 2nd edition Tucker and Noonan
Generics, Lambdas and Reflection
Software Architecture & Design
Presentation transcript:

The Object Constraint Language OCL The Object Constraint Language

Content HISTORY COLLECTION TYPES AND QUERING IN OCL FORMAL LANGUAGE - STATEMENT EXAMPLES CONSTRAINTS AND OCL EXAMPLES

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.

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

Content HISTORY COLLECTION TYPES AND QUERING IN OCL FORMAL LANGUAGE - STATEMENT EXAMPLES CONSTRAINTS AND OCL EXAMPLES

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.

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{@M1,@M2} :Set(GangMember) self.member self M1:GangMember A:Gang M3:GangMember M2:GangMember B:Gang M4:GangMember M5:GangMember

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 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

Example… Defining classes – content of file gang1.use: model Gangs class Gang attributes name : String isArmy : Boolean end class GangMember -- associations association Membership between Gang[1] role gang GangMember[1..*] role member After opening of specification (gang1.use) in the tool and having created a class diagram view:

content of command file gang1_objects.cmd: The Example continues… We need to define some objects to “navigate” – content of command file gang1_objects.cmd: -- Create objects: -- Gang A !create a:Gang !set a.name := 'A' !set a.isArmy := false -- Member m1 !create m1: GangMember !set m1.name := 'M1' -- Member m2 !create m2: GangMember !set m2.name := 'M2' -- Membership !insert (a,m1) into Membership !insert (a,m2) into Membership -- Gang B !create b:Gang !set b.name := 'B' !set b.isArmy := false -- Member m3 !create m3: GangMember !set m3.name := 'M3' -- Member m4 !create m4: GangMember !set m4.name := 'M4' -- Member m5 !create m5: GangMember !set m5.name := 'M5' !insert (b,m3) into Membership !insert (b,m4) into Membership !insert (b,m5) into Membership After running command file (gang1_objects.cmd) and the creation of an object diagram view:

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! To display the OCL Evaluate Window: State/OCL Evaluate OCL expression.

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

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

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

asSet

Finding all members of the B-gang

Content HISTORY COLLECTION TYPES AND QUERING IN OCL FORMAL LANGUAGE - STATEMENT EXAMPLES CONSTRAINTS AND OCL EXAMPLES

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 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

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.

Content HISTORY COLLECTION TYPES AND QUERING IN OCL FORMAL LANGUAGE - STATEMENT EXAMPLES CONSTRAINTS AND OCL EXAMPLES

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.

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).

Where To Use OCL? Invariants on classes and types Precondition and postconditions on operations (methods) Constraints on operations: operation=expression (the return value) …

Content HISTORY COLLECTION TYPES AND QUERING IN OCL FORMAL LANGUAGE - STATEMENT EXAMPLES CONSTRAINTS AND OCL EXAMPLES

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)

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. -- NB! @pre accesses the value before executing the operation context Gang :: register(newMember : GangMember) pre: not member->includes(newMember) post : member = member@pre->including(newMember)

Comments to Previous Slide collection->includes(object: T): Boolean is True if object is an element of collection. E.g.: Set{@m1,@m2}->includes(@m2) = 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{@m1,@m2}->including(@m3) = Set{@m1,@m2,@m3}.

model xor class Account end class Person class Corporation association personOwner between Account[0..*] role account Person[0..1] role personOwner association cooperationOwner between Corporation[0..1] role cooperationOwner constraints context Account inv : personOwner->size() = 1 implies cooperationOwner->size() = 0 Using OCL in USE What we want: An account has either a person as owner or a cooperation as owner { The Constraint is part of the specification

Create Some Objects -- Create objects: !create a2801: Account !create Harald: Person !create UiA: Corporation -- Create link: !insert (a2801, Harald) into personOwner

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

Modify the Constraint: context Account inv: (personOwner->size() = 1 implies cooperationOwner->size() = 0) and (personOwner->size() = 0 implies cooperationOwner->size() = 1)

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

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)

It is possible to use the Evaluate OCL Expression dialog box to express invariants But you must transform it to an expression…