Formal Methods in Software Engineering 1

Slides:



Advertisements
Similar presentations
1 Mireille Blay-Fornarino – 2007/2008 EPU département SI, Master STIC Survol de Object Constraint Language & IDM À partir du cours Contracts, Patterns.
Advertisements

Formal Methods of Systems Specification Logical Specification of Hard- and Software Dr. Armin Wolf Fraunhofer Institut für Rechnerarchitektur.
Lecture 7 Constraints and Invariants Formal Modeling and Analyzing a Flash File System.
The role of OCL in the Model Driven Architecture Jos Warmer Klasse Objecten
By Karen Richart. The Object Constraint Language (OCL)  Formal specification language that could be used for constraining the model elements that occur.
ICE1341 Programming Languages Spring 2005 Lecture #6 Lecture #6 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
1 The Object Constraint Language: Expressing Constraints in the UML (Most slides created by Robert B. France, Professor Department of Computer Science,
OCL2 April A presentation of OCL 2 Object Constraint Language Christian Hein, Fraunhofer FOKUS April 2006.
Object Constraint Language in Together Dan Massey Y&L Consulting.
Using UML, Patterns, and Java Object-Oriented Software Engineering Chapter 9, Object Design: Object Constraint Language.
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.
1 Specifying Object Interfaces. 2 Major tasks in this stage: --are there any missing attributes or operations? --how can we reduce coupling, make interface.
Databases Illuminated Chapter 7 The Object-Oriented Model.
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
1 Formal Specifications for Complex Systems (236368) Tutorial #7 OCL Object Constraint Language.
SEG4110 – Advanced Software Engineering and Reengineering TOPIC E Object Constraint Language (OCL)
Computer Science 1000 Spreadsheets II Permission to redistribute these slides is strictly prohibited without permission.
1 The Object Constraint Language Jos Warmer and Anneke Kleppe. OCL: The Constraint Language of the UML, Journal of Object-Oriented Programming, 2(2):10-13,
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.
Analyzing the Requirements with Formal Specifications Vienna Development Method Specification Language (VDM-SL) Book: Formal Software Development From.
Agenda Introduction Overview of White-box testing Basis path testing
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.
Practical Object-Oriented Design with UML 2e Slide 1/1 ©The McGraw-Hill Companies, 2004 PRACTICAL OBJECT-ORIENTED DESIGN WITH UML 2e Chapter 9: Interaction.
111 Protocols CS 4311 Wirfs Brock et al., Designing Object-Oriented Software, Prentice Hall, (Chapter 8) Meyer, B., Applying design by contract,
© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 UML Sequence Diagrams.
CS551 - Lecture 8 1 CS551 Modelling with Objects (Chap. 3 of UML) Yugi Lee STB #555 (816)
1 OCL The Role of OCL in UML. 2 רשימת הנושאים  מבוא  מרכיבי השפה  דוגמאות  מקורות.
Fall 2004EE 3563 Digital Systems Design EE 3563 VHDL – Basic Language Elements  Identifiers: –basic identifier: composed of a sequence of one or more.
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.
Object Constraint Language (OCL) OCL provides a way to develop more precise models using UML What is a constraint in Object Constraint Language? A constraint.
Formal Methods in Software Engineering1 Today’s Agenda  Quiz 2 next class  Presentation Schedule  Quick Review  Build Models Using OCL.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Formal Methods in Software Engineering1 Today’s Agenda  Quiz 1 Return  Project Discussion  Quick Review  Finish Introduction to OCL.
1 Assertions. 2 A boolean expression or predicate that evaluates to true or false in every state In a program they express constraints on the state that.
Interpreting the Object Constraint Presented by: Ed Kausmeyer.
An association between class Flight and class Person, indicating that a certain group of persons are the passengers on a flight, will have multiplicity.
Jan Pettersen Nytun, UIA, page 1. Jan Pettersen Nytun, UIA, page 2 HISTORY COLLECTION TYPES AND QUERING IN OCL FORMAL LANGUAGE - STATEMENT EXAMPLES CONSTRAINTS.
Presentation of a paper on “Model-based Software Testing”
The Object Constraint Language
Chapter 16 UML Class Diagrams.
GC211Data Structure Lecture2 Sara Alhajjam.
Representation, Syntax, Paradigms, Types
The Object Constraint Language
JavaScript: Functions.
Chapter 9, Object Design: Object Constraint Language
UML Class Diagrams: Basic Concepts
UML Sequence Diagrams.
Specifying Object Interfaces
Seminar 3 UML Class Diagram.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Functional Programming
ECE 434 Advanced Digital System L10
Representation, Syntax, Paradigms, Types
Chapter 6 Control Statements: Part 2
Defining Classes and Methods
Representation, Syntax, Paradigms, Types
The Object Constraint Language
Protocols CS 4311 Wirfs Brock et al., Designing Object-Oriented Software, Prentice Hall, (Chapter 8) Meyer, B., Applying design by contract, Computer,
Representation, Syntax, Paradigms, Types
Algebraic Specification Software Specification Lecture 34
Classes, Objects and Methods
Object Oriented System Design Class Diagrams
Introduction to UML - Prof. Prasad P. Mahale
Object Constraint Language (OCL)
Introduction to UML - Prof. Prasad P. Mahale
Presentation transcript:

Formal Methods in Software Engineering 1 Today’s Agenda Project Schedule Quick Review OCL Expressions Formal Methods in Software Engineering 1

Formal Methods in Software Engineering 2 Quick Review What types of OCL expressions can be written when the context is a class? Formal Methods in Software Engineering 2

Formal Methods in Software Engineering 3 OCL Expressions Context Basic Elements Collection Types Summary Formal Methods in Software Engineering 3

Formal Methods in Software Engineering 4 What is context? The context of an OCL expression specifies the entity in the UML model for which the expression is defined. Usually, the context is a class, interface, data type, component, or method. A component is an autonomous unit that has one or more interfaces. Components are typically large design units. Formal Methods in Software Engineering 4

Formal Methods in Software Engineering 5 Contextual Type The contextual type is the type of the object for which the expression will be evaluated. If the context itself is a type, the context is equal to the contextual type. If the context is an operation or attribute, the contextual type is the type for which the feature is defined. Formal Methods in Software Engineering 5

Formal Methods in Software Engineering 6 Contextual Instance An OCL expression is always evaluated for a single instance of the contextual type, called the contextual instance. The contextual instance can be referred to by the keyword self, which is often omitted if obvious. context Customer inv: self.name = ‘Edward’ Formal Methods in Software Engineering 6

Formal Methods in Software Engineering 7 Context - Classes When the context is a class (or a classifier), the following types of OCL expressions can be used: Invariants Definitions of new attributes Definitions of new operations Formal Methods in Software Engineering 7

Formal Methods in Software Engineering 8 Invariants An invariant is a constraint that must hold for any instance upon completion of the constructor and completion of every public operation. Note that an invariant does not have to hold during the execution of operations. context Customer inv myInvariant23: self.name = ‘Edward’ Formal Methods in Software Engineering 8

Formal Methods in Software Engineering 9 New attributes New attributes can be added to a class definition using OCL expressions. context Customer def: initial : String = name.substring(1, 1) New attributes can be used to define derived attributes. Formal Methods in Software Engineering 9

Formal Methods in Software Engineering 10 New Operations Similarly, new operations can be added to a class definition. Note that all operations defined by OCL must be query operations. context CustomerCard def: getTotalPoints (d: Date) : Integer = transactions -> select( date.isAfter(d) ).points -> sum () Formal Methods in Software Engineering 10

Formal Methods in Software Engineering 11 Context – Attributes When the context is an attribute, the following expressions can be used: Derivation rules Initial values Formal Methods in Software Engineering 11

Formal Methods in Software Engineering 12 Derivation Rules A derivation rule specifies that the value of the context element should always be equal to the value given by the evaluation of the rule. context LoyaltyAccount::totalPointsEarned : Integer derive: transactions -> select( oclIsTypeOf (Earning) ).points -> sum () Formal Methods in Software Engineering 12

Formal Methods in Software Engineering 13 Initial Values An initial value is the value that the attribute or association end will have at the moment that the contextual instance is created. context CustomerCard::valid : Boolean init: true Formal Methods in Software Engineering 13

Formal Methods in Software Engineering 14 Context - Operations When the context is an operation, the following types of OCL expressions can be used: Pre-/Post- conditions Body of query operations Formal Methods in Software Engineering 14

Pre-/Post-conditions A pre-condition is a boolean expression that must hold at the moment when the operation starts the execution;. A post-condition is a boolean expression that must hold when the operation ends its execution. context LoyaltyProgram::enroll (c: Customer) pre: c.name <> ’’ post: participants -> including (c) Formal Methods in Software Engineering 15

Body of Query Operations Query operations can be fully defined by specifying the result of the operation in a single expression. context CustomerCard::getTransactions ( from : Date, until : Date) : Set (Transaction) body: transactions -> select (date.isAfter(from) and date.isBefore(until)) Formal Methods in Software Engineering 16

Formal Methods in Software Engineering 17 OCL Expressions Context Basic Elements Collection Types Summary Formal Methods in Software Engineering 17

Formal Methods in Software Engineering 18 Types In OCL, each value has a type. Each expression is evaluated to a result value; the type of the expression is the type of the result value. Predefined types Basic Types: Boolean, Integer, Real, and String Collections Types: Collection, Set, Bag, OrderedSet, and Sequence User-defined types Types defined in the UML diagrams, such as LoyaltyProgram, Customers, and so on. Formal Methods in Software Engineering 18

Formal Methods in Software Engineering 19 Boolean Operation Notation Result Type or a or b Boolean and a and b exclusive or a xor b negation not a equals a = b not equals a <> b implies a implies b Formal Methods in Software Engineering 19

Formal Methods in Software Engineering 20 Integer and Real Operation Notation Result Type equals a = b Boolean not equals a <> b less a < b more a > b less or equal a <= b more or equals a >= b minus a + b Integer or Real multiplication a * b division a / b Real modulus a.mod(b) Integer integer division a.div(b) absolute value a.abs() max a.max(b) min a.min(b) round a.round() floor a.floor() Formal Methods in Software Engineering 20

Formal Methods in Software Engineering 21 String Operation Notation Result Type concatenation s1.concat(s2) String size s.size () Integer to lower case s.toLower () to upper case s.toUpper () substring s.substring(i, j) equals s1 = s2 Boolean not equals s1 <> s2 Formal Methods in Software Engineering 21

Formal Methods in Software Engineering 22 User-defined Type A user-defined type is a classifier specified in the UML model. An OCL expression can refer to the features of a user-defined type, including attributes, operations, class attributes, class operations, and association ends. Formal Methods in Software Engineering 22

Attributes and Operations Attributes and query operations of a user-defined type can be used in OCL expressions. They are both referenced using the dot notation. In order to distinguish them, the parentheses after the name of an operation is required. Formal Methods in Software Engineering 23

Class Attributes and Operations A class attribute or operation is referenced by the class name followed by two colons, followed by the attribute or operation name (and parameters). context CustomerCard inv: goodThru.isAfter( Date::now) Formal Methods in Software Engineering 24

Formal Methods in Software Engineering 25 Navigation (1) Association ends, also called navigations, can be used to navigate from one object in the model to another. Navigations are treated as attributes, and are referenced using the dot notation. Note that the name of a navigation is the rolename or the name of the type connected at the end. Formal Methods in Software Engineering 25

Formal Methods in Software Engineering 26 Navigation (2) The type of a navigation is either a user-defined type or a collection of a user-defined types, depending on the multiplicity. LoyaltyProgram 0..* 0..* Customer programs owner 1 context CustomerCard inv: self.owner.dateOfBirth.isBefore( Date::now) cards 0..* context CustomerCard inv: self.owner.programs.size() > 0 CustomerCard Formal Methods in Software Engineering 26

Formal Methods in Software Engineering 27 OCL Expressions Context Basic Elements Collection Types Summary Formal Methods in Software Engineering 27

Formal Methods in Software Engineering 28 Collection Types There are five collection types in OCL: Collection – The abstract super type. Set – No duplication, unordered. OrderedSet – No duplication, ordered. Bag – Duplication allowed, unordered. Sequence – Duplication allowed, ordered. Formal Methods in Software Engineering 28

Formal Methods in Software Engineering 29 Collection Constants Constant sets, ordered sets, sequences, and bags can be specified by enumerating their elements: Set { 1, 2, 5, 88 } OrderedSet { ‘apple’, ‘orange’, ‘strawberry’, ‘pear’ } Sequence { 1, 3, 45, 2, 3 } Bag { 1, 3, 4, 3, 5} Sequence { 1 .. (6 + 4) } Formal Methods in Software Engineering 29

Collection Declaration Occasionally, the type of a model element needs to be explicitly specified, e.g., when defining a new attribute. Set (Customer) Sequence (Set (ProgramPartner)) OrderedSet (ServiceLevel) Bag (Burning) Formal Methods in Software Engineering 30

Collections of Collections In most cases, collections are automatically flattened. Set { Set { 1, 2 }, Set { 3, 4 }, Set { 5, 6 } } Set { 1, 2, 3, 4, 5, 6} Formal Methods in Software Engineering 31

Collection Operations Many standard collection operations are defined in OCL. They are denoted in OCL expressions using an arrow. Important: The user-defined operations are denoted using dots; the standard operations are denoted using arrows. context LoyaltyProgram inv: self.participants -> size () < 10000 Formal Methods in Software Engineering 32

Standard Operations Operation Description count (object) The number of occurrences of the object in the collection excludes (object) True if the object is not an element of the collection excludesAll (collection) True if all elements of the parameter collection are not present in the current collection includes (object) True if the object is an element of the collection includesAll (collection) True if all elements of the parameter collection are present in the current collection isEmpty () True if the collection contains no elements notEmpty () True if the collection contains one or more element size () The number of elements in the collection sum () The addition of all elements in the collection. The elements must be of a type supporting addition (such as Real or Integer) Formal Methods in Software Engineering 33

Formal Methods in Software Engineering 34 Other Operations (1) equals (=) and notEquals (<>) Two sets equal iff all elements are the same. Two ordered sets equal iff all elements are the same, and appear in the same order. Two bags equal iff all elements must appear in both with the same number of times. Two sequences equal iff all elements must appear in both with the same number of times, and in the same order Formal Methods in Software Engineering 34

Formal Methods in Software Engineering 35 Other Operations (2) including(object) including results in a new collection with object being added; For a set or ordered set, object is added only if it is not already present. For a sequence or an ordered set, the element is added in the end. excluding(object) excluding results in a new collect with object being removed. For a bag or sequence, it removes all the occurrences of the given object. Formal Methods in Software Engineering 35

Formal Methods in Software Engineering 36 Other Operations (3) flatten - changes a collection of collections into a collection of single objects. The result of flattening a bag or set is a bag or set, respectively. The result of flattening a sequence or ordered set is a sequence or ordered set, respectively. However, if the subcollections are bags or sets, the order of the elements cannot be determined precisely. Set { Set { 1, 2 }, Set { 2, 3 }, Set { 4, 5, 6 } } Set { 1, 2, 3, 4, 5, 6 } Bag { Set { 1, 2 }, Set { 1, 2 }, Set { 4, 5, 6 } } Bag { 1, 1, 2, 2, 4, 5, 6 } Sequence { Set { 1, 2 }, Set { 2, 3}, Set { 4, 5, 6 } } Sequence { 2, 1, 2, 3, 4, 5, 6 } Formal Methods in Software Engineering 36

Formal Methods in Software Engineering 37 Other Operations (4) asSet, asSequence, asBag, and asOrderedSet These operations transform instances of one collection type to another. Applying asSet on a bag or asOrderedSet on a sequence will remove duplicate elements. Appying asSet on an OrderedSet or asBag on a sequence will lose the ordering info.. Applying asOrderedSet or asSequence on a set or bag will place elements randomly. Formal Methods in Software Engineering 37

Formal Methods in Software Engineering 38 Other Operations (5) union – combines two collections into one Ordered collections can only combined with ordered collections. Combining a set with a bag will result in a bag. intersection – intersects two collections cannot be applied to ordered collection minus – the difference between two collections When applied to an ordered set, the ordering remains. Formal Methods in Software Engineering 38

Formal Methods in Software Engineering 39 Other Operations (6) Operations on ordered collections first/last – returns the first/last element at – returns the element at the given position indexOf – returns the position of the given element insertAt – inserts the given element at specified position subSequence – returns a sub-sequence at specified indices subOrderedSet – returns a sub-ordered set at specified indices append/prepend – add an element as the last or first element Formal Methods in Software Engineering 39

Formal Methods in Software Engineering 40 Iterators Iterators are used to loop over the elements in a collection: They evaluate an expression on each element. context LoyaltyProgram inv: self.Membership.account -> isUnique (acc | acc.number) Formal Methods in Software Engineering 40

Formal Methods in Software Engineering 41 Iterator Operations Operation Description any (expr) Returns a random element of the source collections for which the expression expr is true collect (expr) Returns the collection of objects that result from evaluating expr for each element in the source collection exists (expr) Returns true if at least one element in the source collection for which expr is true. forAll (expr) Returns true if expr is true for all elements in the source collection isUnique (expr) Returns true if expr has a unique value for all elements in the source collection iterate (...) Iterates over all elements in the source collection one (expr) Returns true if there is exactly one element in the source collection for which expr is true reject (expr) Returns a subcollection that contains all elements for which expr is false. select (expr) Returns a subcollection that contains all elements for which expr is true sortedBy (expr) Returns a collection containing all elements of the source collection ordered by expr Formal Methods in Software Engineering 41

Formal Methods in Software Engineering 42 sortedBy This operation takes as input a property, and orders the source collection into a sequence or ordered set. context LoyaltyProgram def: sortedAccounts : Sequence (LoyaltyAccount) = self.Membership.account -> sortedBy ( number) Formal Methods in Software Engineering 42

Formal Methods in Software Engineering 43 select This operation takes as input a boolean expression, and returns a collection that contains all elements for which the boolean expression is true. context CustomerCard inv: self.transactions -> select ( points > 100) -> notEmpty () Formal Methods in Software Engineering 43

Formal Methods in Software Engineering 44 forAll (1) This operation allows to specify that a certain condition must hold for all elements of a collection. context LoyaltyProgram inv: participants -> forAll ( age () <= 70 ) Formal Methods in Software Engineering 44

Formal Methods in Software Engineering 45 forAll (2) In this operation, multiple iterator variables can be declared. The following two operations are equivalent: context LoyaltyProgram inv: participants -> forAll ( c1, c2 | c1 <> c2 implies c1.name <> c2.name) context LoyaltyProgram inv: participants -> forAll ( c1 | participants -> forAll ( c2 | c1 <> c2 implies c1.name <> c2.name)) Formal Methods in Software Engineering 45

Formal Methods in Software Engineering 46 exists This operation allows to specify that a certain condition must hold for at least one element of a collection. context LoyaltyAccount inv: points > 0 implies transactions -> exists ( t | t.points > 0 ) Formal Methods in Software Engineering 46

Formal Methods in Software Engineering 47 collect (1) This operation iterates over a collection, computes a value for each element, and gathers these values into a new collection. context LoyaltyAccount inv: transactions -> collect ( points ) -> exists ( p : Integer | p = 500 ) Formal Methods in Software Engineering 47

Formal Methods in Software Engineering 48 collect (2) Taking a property of a collection using a dot is interpreted as applying the collect operation. context LoyaltyAccount inv: transactions.points -> exists ( p : Integer | p = 500 ) Formal Methods in Software Engineering 48

Formal Methods in Software Engineering 49 iterate (1) This operation is the most generic one, in the sense that all other loop operations can be described as a special case of iterate. collection -> iterate ( element : Type1; result : Type2 = <expression> | <expression-with-element-and-result> ) Formal Methods in Software Engineering 49

Formal Methods in Software Engineering 50 iterate (2) Set { 1, 2, 3 } -> iterate ( i : Integer, sum : Integer = 0 | sum + i) context ProgramPartner def: getBurningTransactions () : Set (Transaction) = deliveredServices.transactions -> iterate ( t : Transaction; resultSet : Set (Transaction) = Set {} | if t.oclIsTypeOf ( Burning ) then resultSet.including ( t ) else resultSet endif Formal Methods in Software Engineering 50

Formal Methods in Software Engineering 51 OCL Expressions Context Basic Elements Collection Types Summary Formal Methods in Software Engineering 51

Formal Methods in Software Engineering 52 Summary The context of an OCL expression can be a class, interface, attribute, or operation. An OCL type can be a predefined type (basic or collection type) or a user-defined type. Navigations are used to navigate from one object in the UML model to another. There are four types of concrete collections: Set, OrderedSet, Bag, and Sequence. Many operations are predefined to manipulate these collection types. Formal Methods in Software Engineering 52