Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Advanced
Software Engineering
Object Constraint Language
Ramtin Khosravi
OCL Topics
z
OCL Topics
z
OCL 2.0
Product of the Object Management Group
(OMG)
Model Constraints
z
z
z
z
z
OCL Topics
z
Design by Contract
z
z
Auction Domain
Auction Domain
context Auction
context Auction::winningBid():Bid
Preconditions
Example Preconditions
z
z
z
z
z
z
context Auction::placeBid(bidder:Id,
bid:Money)
pre : not bidder.oclIsUndefined()
pre : not bid.oclIsUndefined()
pre : bid.currency =
self.bids->first().amount.currency
pre : bid > self.currentBid().money
Simple Preconditions
z
z
z
context Auction::placeBid(bidder:Id,
bid:Money)
pre : not bidder.oclIsUndefined()
pre : not bid.oclIsUndefined()
Implementing Preconditions
z
z
z
z
z
z
z
z
context Auction::placeBid(bidder:Id,
bid:Money)
pre : bid.currency =
self.bids->first().amount.currency
pre : bid > self.currentBid().money
The bid must be in the same currency as the
starting bid. The bid must be higher than the
current high bid.
Implementing Preconditions
z
z
z
z
z
z
z
z
if (bid.getAmount().getCurrency() !=
((Bid) bids.first()).getAmount()
.getCurrency()) {
throw new IllegalArgumentException(
currencies must match);
}
10
Assertion Temptation
z
z
z
z
z
z
z
z
z
z
OCL Comments
context Auction::placeBid(bidder:Id,
bid:Money)
-- currencies must match
pre : bid.currency =
self.bids->first().amount.currency
pre alwaysTrue : 1 = 1
z
z
z
z
11
More Preconditions
More Preconditions
z
z
z
z
z
z
context Auction::winningBid():Bid
-- auction must end to have a winner
pre : TimeStamp::now() >=
self.expires()
context Bid::>(bid:Bid):Boolean
-- _gt is our name for >
pre : not bid.oclIsUndefined()
12
Invariants
Invariants
13
Example Invariants
z
z
z
z
z
z
context Auction
inv : self.expires() > self.start
context Bid
inv : self.amount.amount > 0.0
/* Note this could be specified in
the Auction context, but dont. */
Postconditions
14
PostConditions
z
context Auction::placeBid(bidder:Id,
bid:Money)
post: self.currentBid().amount = bid
Notice that the postcondition assumes serial or
single-threaded execution. This postcondition
isnt necessarily enforceable in an unprotected
multi-threaded context.
PostConditions
z
z
z
z
z
z
context Auction::placeBid(bidder:Id,
bid:Money)
post : self.currentBid() >
self.currentBid@pre()
Postconditions can reach back in time.
@pre references the value before the operation.
Another way to state a precondition
15
Implementing
PostConditions
z
z
z
z
z
z
z
OCL Contracts
z
z
z
z
16
OCL Topics
z
z
z
17
18
Derived Values
z
z
19
z
z
20
OCL Collections
Classification
Duplicates
Not Ordered
Ordered
No
Duplicates
Bag
Set
Sequence
OrderedSet
context TestSession::numberOfScoresOverTarget(
target : Integer) : Integer
body: results->select(compositeScore > target)->size()
21
team
logic_score
illogic_score
composite_score
name
Blue
24
47
95
A Appell
Blue
24
32
80
B Barry
Red
35
23
93
C Cross
Green
18
50
86
D Dolly
Red
49
44
143
E Elms
Blue
37
42
116
F Fisher
Returning a Collection
z
context TestSession::testResultsForTeam(
testTeam : Team) : Set(TestResult)
body: results->select(team = testTeam)
22
Building Up Queries
z
context TestSession::averageScoreForTeam(
testTeam : Team) : Real
body: testResultsForTeam(testTeam).compositeScore
->sum() / testResultsForTeam(testTeam)->size()
23
Boolean
|
Integer
|
Real
|
true or false
String
|
sequence of characters
Boolean Operations
z
a : Boolean, b : Boolean
a and b
not a
a implies b
z
z
a or b
a=b
a xor b
a <> b
24
Numeric Operators
z
z
z
z
z
z
z
Comparison
=
equals
<
less
<= less or equal
<>
>
>=
not equals
more
more or equal
Math
+ addition
* multiplication
subtraction
division
a : Integer
b:Integer
a.div(b)
a.max(b)
a.round()
a.mod(b)
a.abs()
a.min(b)
a.floor()
z
z
25
String Operations
z
a : String
b : String
a <> b
a.size()
a.toUpper()
a=b
a.concat(b)
a.toLower()
a.subString(1, 3)
z
z
OCL Conditional
z
if <Boolean expression>
then <expression>
else <expression>
endif
z
z
z
26
Grouping
Path resolution
Message notation
Unary
Multiplication/Division
Addition/Subtraction
Comparison
Logical
Logical implication
()
::
. -> also ^ ^^
- not
* /
+ < > <= >= <> =
and or xor
implies
User-Defined Types
z
Money by a Real
| Add two Distance values
27
OCL Collections
z
z
z
z
Set(Bid)
Bag(String)
28
Collections Literals
z
z
z
a : Set(String)
a=c
a <> c
a->size()
a->count(b)
a->excludes(b)
a->excludesAll(c)
a->includes(b)
a->includesAll(c)
a->isEmpty()
a->notEmpty()
a->sum() -- contents are of type supporting +
z
z
z
b : String
c : Set(String)
29
Flatten
z
z
z
context MyType
def: a : Set(Set(Integer)) =
Set {Set{1, 2, 3}, Set{3, 4, 5}, Set{4, 5, 6}}
30
Collection Loops
z
z
z
z
z
z
z
31
LL Invariants
z
z
z
z
z
context Sale
-- a Sale has zero or more items
inv numberOfSaleItems :
not items->size() >= 0
-- a Sale's subtotal may not be less than zero
inv subtotalGreaterThanZero : subtotal().amount > 0.0
and subtotal().currency = Currency::USDollars
32
z
z
z
z
LL Query
33
Collections Queries
z
z
z
z
Composite Queries
z
z
z
z
z
z
34
z
z
z
z
Dont Do This
z
z
z
z
z
z
z
z
z
z
z
z
z
z
z
35
Use Polymorphism
z
z
z
OCL Notes
z
z
z
36
Derivation rules
Initial values
Body of query operations
Invariants
Pre- and post-conditions
Messaging in postconditions
Cycles in class models
Defining derived classes
Dynamic and optional multiplicity
Or constraints
Specifying Operations
37
OCL Resources
z
38