Professional Documents
Culture Documents
Srinivas T
FT 2011 Batch, Reg. No.: CJB0911008 M. Sc. (Engg.) in Computer Science and Networking
Marking
Head Technical Content Grasp and Understanding Maximum 5 5 Score
5
5 20
Presentation Outline
History Introduction to OCL Why OCL ? Where to use OCL OCL motivation Conclusion References
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
h1: House
m1: Mortgage
Mortgage
10
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
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.
14
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
Choosing a Context
The class on which the invariant must be put is the invariant context
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
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>
22
23
context Account::deposit(amt: Integer): void pre argumentOk: amt > 0 post balanceIncreased: balance = balance@pre + amt
optional label
24
25
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
26
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()
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
29
Thank you
30