You are on page 1of 30

CSN505 AFM Presentation

Object Constraint Language

Srinivas T
FT 2011 Batch, Reg. No.: CJB0911008 M. Sc. (Engg.) in Computer Science and Networking

Module Leader: Module Name: Module Code :

Dr Hariharan Ramasangu Applied Formal Methods CSN505

M. S. Ramaiah School of Advanced Studies

Marking
Head Technical Content Grasp and Understanding Maximum 5 5 Score

Delivery Technical and General Aspects Handling Questions


Total

5
5 20

M. S. Ramaiah School of Advanced Studies

Presentation Outline
History Introduction to OCL Why OCL ? Where to use OCL OCL motivation Conclusion References

M. S. Ramaiah School of Advanced Studies

History
Object constraint language is the constraint language of UML. First developed in 1995 as IBEL by IBMs Insurance division for business modelling. It was developed by Jos Warmer and Anneke Klepper, based on Steve Cook and John Danielss syntropy language. OCL was designed in the hope that it would be simpler than languages such as VDM and Z, but it actually ended up more complicated. OCL was used to define further upgrades of UML.
M. S. Ramaiah School of Advanced Studies

Introduction to OCL
OCL notation allows Unified Modeling Language (UML) users more specification precision. OCL is Based on logic and discrete mathematics. OCL is a modeling language not a programming language. Evaluated OCL expressions can not have side effects. OCL provides a way to develop more precise models using UML. Initially OCL is an extension to UML, and now it is a part of UML standard.
M. S. Ramaiah School of Advanced Studies

Why OCL?
A UML diagram, such as a class diagram, is typically not refined enough to provide all the relevant aspects of a specification. There is a need to describe additional constraints about the object in the model. Natural language is inherently unambiguous. Traditional formal languages are difficult for the average business or system modeler to use. OCL is a formal language that remains easy to read and write.
6

M. S. Ramaiah School of Advanced Studies

Where to Use OCL


The OCL language is used to express constraints specifying constant conditions for system being modeled. To specify invariants on classes and types in the class model. To specify type invariant for stereotypes. To describe pre- and post conditions on Operations and methods. To specify constraints on operations. What is a constraint in Object Constraint Language? A constraint is a restriction on one or more values of (part of) an object-oriented model or system.
7

M. S. Ramaiah School of Advanced Studies

Object Constraint Language


Motivation
UML diagrams dont tell everything Q: What does the following class diagram tell?

House security 1 0..* mortgages

0..* 1 Person houses owner 1 borrower Mortgage 0..* mortgages

M. S. Ramaiah School of Advanced Studies

OCL --- Motivation


Is this a valid object diagram?
Whats the problem?

h1: House

p2: Person p1: Person

m1: Mortgage

M. S. Ramaiah School of Advanced Studies

OCL --- Motivation


Solution: Specify constraints explicitly A person have a mortgage on a house only if that house is owned by the person.
House security 0..* houses 1 owner 1 borrower 0..* mortgages 0..* mortgages Person

Mortgage

context Mortgage inv: security.owner = borrower


M. S. Ramaiah School of Advanced Studies

10

OCL --- What Is It?


Standard add-on to UML OCL expressions dependent on types from UML diagrams Language for expressing additional information (e.g., constraints and business rules) about UML models Characteristics Constraint and query languages Math foundation (set and predicate) but no math symbols Strongly typed, declarative, and no side effect High level of abstraction (platform independence)

M. S. Ramaiah School of Advanced Studies

11

Advantages of Constraints
Better documentation Constraints add information about the model elements and their relationships to the visual models used in UML It is a way of documenting the model More precision OCL constraints have formal semantics, hence, can be used to reduce the ambiguity in the UML models Communication without misunderstanding UML models are used to communicate between developers Using OCL constraints modelers can communicate unambiguously
M. S. Ramaiah School of Advanced Studies

12

OCL Constraints
OCL constraints are declarative They specify what must be true not what must be done. OCL constraints have no side effects Evaluating an OCL expression does not change the state of the system.

OCL constraints have formal syntax and semantics their interpretation is unambiguous.
M. S. Ramaiah School of Advanced Studies

13

Different kinds of Constraints


Class invariant
a constraint that must always be met by all instances of the class.

Precondition of an operation
a constraint that must always be true BEFORE the execution of the operation.

Postcondition of an operation
a constraint that must always be true AFTER the execution of the operation.

M. S. Ramaiah School of Advanced Studies

14

Constraints (invariants), Contexts and Self


A constraint (invariant) is a boolean OCL expression evaluates to true/false. Every constraint is bound to a specific type (class, association class, interface) in the UML model its context. The context objects may be denoted within the expression using the keyword self. The context can be specified by: Context <context name> A dashed note line connecting to the context figure in the UML models A constraint might have a name following the keyword invariant.
M. S. Ramaiah School of Advanced Studies

15

Invariants
Using OCL we can specify class invariants such as Customer age >= 18 As a convention we will write the OCL expressions in the following form:
OCLcontext OCLexpression

The class on which the invariant must hold is the invariant context Invariant has to hold for all instances of the class For the above example, the expression age >= 18 is an invariant of the Customer class, i.e. it holds for every instance of that class
M. S. Ramaiah School of Advanced Studies

16

Invariants
We can also write invariants on attributes of associated classes In OCL you can use the rolename to refer to the object on the other end of an association. If the rolename is not present, you can use the classname starting with a lowercase letter

Examples:
Membership card.owner = customer CustomerCard printedName = owner.title.concat( owner.name )
17

M. S. Ramaiah School of Advanced Studies

Choosing a Context
The class on which the invariant must be put is the invariant context

One can write the same invariant property in different contexts


For example Customer age >= 18 LoyaltyProgram customer.forAll( age >= 18 )
M. S. Ramaiah School of Advanced Studies

18

Basics of OCL
Associating OCL expressions to UML models Directly to diagrams as notes Separate accompanying texts, e.g., context Person inv: age >= 0 Specifying invariants State conditions that must be always be met by all instances of context types (classes or interfaces)
M. S. Ramaiah School of Advanced Studies

19

Basics of OCL --- Invariants


context Company inv: self.numberOfEmployees > 50 context c: Company inv: c.numberOfEmployees > 50
self: contextual instance, an instance to which the OCL expression is attached self: contextual instance, an instance to which the OCL expression is attached

context c: Company inv enoughEmployees: c.numberOfEmployees > 50


an optional label

M. S. Ramaiah School of Advanced Studies

20

Invariants
Determine a constraint that must be true for all instances of a type Value of attribute noEmployees in instances of Company must be less than or equal to 50 context Company inv: self.noEmployees <= 50 Equivalent formulation with a c playing the role of self, and a name for the constraint context c: Company inv SME: c.noEmployees <= 50 The stock price of companies is greater than 0 context Company inv: self.stockPrice() > 0
M. S. Ramaiah School of Advanced Studies

21

Pre-conditions, Post-conditions
A pre/post-condition is defined for a method of a class. This methods is the context of the condition.
Context <class name> :: op(x1:t1, , xn:tn) pre: <boolean OCL expression> post: <boolean OCL expression>

M. S. Ramaiah School of Advanced Studies

22

Pre- and Post-conditions


Constraints associated with an operation or other behavioral feature Pre-condition: Constraint assumed to be true before the operation is executed Post-condition: Constraint satised after the operation is executed Pre- and post-conditions associated to operation income in Person context Person::income(): Integer pre: self.age >= 18 post: result < 5000 self is an instance of the type which owns the operation or method result denotes the result of the operation, if any Type of result is the result type of the operation (Integer in the example) A name can be given to the pre- and post-conditions context Person::income(): Integer pre adult: self.age >= 18 post resultOK: result < 5000
M. S. Ramaiah School of Advanced Studies

23

Specifying Pre and Post-conditions


Pre and post-conditions Conditions that must be true at the moment when an operation begins and ends its execution. context Account::deposit(amt: Integer): void pre: amt > 0 pre-value, referring post: balance = balance@pre + amt
to previous value

context Account::deposit(amt: Integer): void pre argumentOk: amt > 0 post balanceIncreased: balance = balance@pre + amt
optional label

M. S. Ramaiah School of Advanced Studies

24

Referring to Pre-value and Result


@pre: denotes the value of a property at the start of an operations result: denotes the result of an operation
context Account::payInterest(rate: Real): void post: balance = balance@pre + calcInterest@pre(rate) context Account::getBalance(): Integer post: result = balance

M. S. Ramaiah School of Advanced Studies

25

Navigating in OCL Expressions


Use dot notation to navigate through associations
Direction and multiplicity matter Use role names or class names
Account 0..* 1 Customer

accounts owner

context Account inv: self.owner -- evaluate to a single Customer self.Customer context Customer inv: self.accounts->size() -- evaluate to a collection self.Account -- of accounts
Arrow notation for collection operations

M. S. Ramaiah School of Advanced Studies

26

Basic Values and Types


Several built-in types and operations
Type
Boolean Integer Real String

Values
false, true -10, 0, 10, -1.5, 3.14, Carmen

Operations
or, and, xor, not, =, <>, implies =, <>, <, >, <=, >=, +, -, *, /, mod(), div(), abs(), max(), min(), round(), floor() =, <>, concat(), size(), toLower(), toUpper(), substring()

M. S. Ramaiah School of Advanced Studies

27

Conclusion
OCL improves the precision of UML. OCL is a formal language that remains easy to read and write. OCL provides a way to develop more precise models using UML. OCL is key for formal methods, UML models as first-class, strategic, reusable artefacts and (future) automated code generation.
M. S. Ramaiah School of Advanced Studies

28

References
Follow CU-Harvard Referencing style

M. S. Ramaiah School of Advanced Studies

29

Thank you

M. S. Ramaiah School of Advanced Studies

30

You might also like