Upload
carmel-baker
View
226
Download
0
Embed Size (px)
Citation preview
1
OCL
The Role of OCL in UML
2
רשימת הנושאים מבואמרכיבי השפהדוגמאותמקורות
3
Introduction OCL notation allows Unified Modeling Language (UML) users more
specification precision Based on logic and discrete mathematics It is a formal method…much more formal than other software engineering
methods The OCL language is used to express constraints Constraints specify constant conditions for system being modeled Evaluated OCL expressions can not have side effects OCL is a pure declarative language OCL is a typed language OCL is UML’s formal constraint language
4
Why OCL? UML diagram e.g. class, state, or activity not granular enough to provide all
relevant aspects of a specification Need to provide additional constraints about the objects in the model Such constraints typically described with natural language which can result in
uncertainty Unambiguous constraints need formal languages but they’re often hard to
understand by nonmathematical persons OCL is a formal language for everyone
5
Where to use OCL To specify invariants on classes and types in the class model To describe pre- and post conditions on Operations and Methods To describe Guards To specify constraints on operations
6
Using OCL Choose one or more UML diagrams---class, state, or activity Add OCL expressions stating facts about elements of the diagrams The expressions are called constraints Any implementation derived from the model must remain true Like OOP, OCL expressions operate on objects However, the result of a complete expression must always be Boolean i.e. true
or false
7
OCL Constraint and EA An OCL constraint is an OCL Boolean expression
that evaluates to true or false
8
דוגמא:אילוצים על נישואים
9
EA: 1נישואים אילוץ
10
EA: 2נישואים אילוץ
11
EA: 3נישואים אילוץ
12
EA: 4נישואים אילוץ
13
רשימת הנושאים מבואמרכיבי השפהדוגמאותמקורות
14
Referring to UML Model Info
Many properties expressed in UML models can be referred to from OCL:
attributes operations navigation are derived from associations class attributes and operations states
15
Operations on Real and Integer Types
16
Operations on String Types
17
Operations on Boolean Types
18
The OCL Collection Hierarchy
19
Operations on all Collections
20
Operations on all Collections
21
Specialized Collection Operations
22
Types of OCL expressions
Expression Type
Syntax Applies to Semantics
invariant Inv: Classifier The invariant must be true for all instances of the classifier
precondition Pre: Operation
Behavioral feature
The precondition must be true before the operation executes
postcondition Post: Operation
Behavioral feature
The precondition must be true after the operation executes
The keyword result refers to the result of the operation
23
Types of OCL expressionsExpression Type
Syntax Applies to Semantics
Query operation body
Body: Query operation
Defines the body of a query operation
Initial value Init: Attribute
Association
end
Defines the initial value of the attribute or the association end
define Def: Classifier Adds variables or helper operations to a context classifier
let let: OCL
expression
Adds local variables to OCL expressions
Derived value Derive: Attribute
Association
end
Defines the derivation rule for the derived attribute or association end
24
רשימת הנושאים מבואמרכיבי השפהדוגמאותמקורות
25
דוגמא
26
inv:
1. No account shall be overdrawn by more than 1000$
2. CeckingAccounts have an overdraft facility. The account shall not be overdrawn to an amount greater than its overdraft limit
3. DepositAccounts shall never be overdrawn
4. The owner must also be an operator
27
inv:
1. No account shall be overdrawn by more than 1000$
Context BankAccount
inv balanceValue:
self.balance>-1000
28
inv:1. No account shall be overdrawn by more than 1000$
2. CeckingAccounts have an overdraft facility. The account shall not be overdrawn to an amount greater than its overdraft limit
Context CheckingAccount
inv balanceValue:
self.balance>=(-overdraftLimit)
inv maximumOverdraftLimit:
self.overdraftLimit<=1000
29
inv:
3. DepositAccounts shall never be overdrawn
Context DepositAccount
inv balanceValue:
self.balance>=0.0
30
pre: post and @pre
1. The amount to be deposited shall be greater than zero
2. After the operation executes, the amount shall have been added to the balance
Context BankAccount::deposit(amount:Real) Real
pre :
amount>0
post:
self.balance=self.balance@pre + amount
31
OCL Example: Companies and Employees
32
OCL Example
"No employee may be older than 65 years."
context Company
inv: self.employee->forAll(age <= 65)
Each OCL expression is written in the context of an instance of specific type. In an OCL expression, the reserved word self is used to refer to the contextual instance. For example, if the context is Company, the self refers to an instance of Company.
33
OCL Example
"If a person is employed at a company, it is not unemployed."
context Company
inv: employee->exists(isUnemployed) = false
34
רשימת הנושאים מבואמרכיבי השפהדוגמאותמקורות
35
References Pressman, Roger S. Software Engineering: A Practitioner’s Approach, 6/e. New
York: McGraw-Hill, 2005. Object Constraint Language Specification OMG Available Specification v2.0
May 2006 <http://www.omg.org/cgi-bin/doc?formal/06-05-01.pdf>. http://en.wikipedia.org/wiki/Object_Constraint_Language