Beruflich Dokumente
Kultur Dokumente
Follows 3242 presentation to go in depth on OCL and using OCL in tools The preconference tutorial used the slides from session 3242 as the first third of the presentation.
OCL Topics
OCL Basics
The LiquidLemons Example OCL in Together
OCL Topics
OCL Basics
The LiquidLemons Example OCL in Together
OCL is
Strongly typed declarative language
Small set of built-in types, including collections User-defined types and operators
Boolean Operations
a : Boolean
a and b not a a implies b a or b a=b
b : Boolean
a xor b a <> b
Numeric Operators
Comparison = equals < less <= less or equal
Math + addition * multiplication <> > >= not equals more more or equal
subtraction division
b:Integer
a.div(b) a.max(b) a.round()
String Operations
a : String
a=b a.concat(b) a.toLower() a.subString(1, 3)
b : String
a <> b a.size() a.toUpper()
OCL Conditional
OCL provides one conditional construct:
if <Boolean expression> then <expression> else <expression> endif No elseif or switch/case
User-Defined Types
Classes, Interfaces, etc. defined in the model
Users may overload infix and unary operators: Multiply Money by a Real Add two Distance values
OCL Collections
Refreshing our memories, we have: Collection Bag Sequence Set OrderedSet
OCL allows you to treat any instance like a collection. You can ask if a single attribute is empty.
Collections Literals
Use curly brackets around comma lists to specify Collection literals.
Bag {sock, sock, shirt, sweater, sock} Sequence {abe, bob, bob, tim, zak} Set {16.0, 2.0, 64.0, 8.0, 128.0, 3.5} OrderedSet {1, 2, 4, 8, 16, 32, 64, 128} You can also specify Collections of Collections.
b : String
a <> c
c : Set(String)
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 +
Flatten
Recursively adds the members of nested collections to a single collection.
context MyType def: a : Set(Set(Integer)) = Set {Set{1, 2, 3}, Set{3, 4, 5}, Set{4, 5, 6}} a->flatten() = Set{2, 3, 1, 5, 4, 6} -- no order
Collection Loops
OCL supports an iterate() operation for Collections. On top of that idea, OCL provides a set of Collection operations that accept expression. Examples: any(expression) exists(expression) collect(expression) one(expression) select(expression) reject(expression)
OCL Topics
OCL Basics
The LiquidLemons Example OCL in Together
LL Invariants
context Sale -- a Sale has one or more items inv numberOfSaleItems : items->size() > 1
-- a Sale's subtotal may not be less than zero inv subtotalGreaterThanZero : subtotal().amount > 0.0 and subtotal().currency = Currency::USDollars
LL Query
Collections Queries
context ItemAnalyzer::averagePrice() : Money body: items.basePrice()->sum() / items.basePrice()>size() context ItemAnalyzer::uniqueModDescriptions() : Set(String) body: items.modDescriptions() ->flatten()->asSet()
Composite Queries
Use small methods with single responsibilities to build up larger functions. -- the total we want is subtotal plus tax context Sale::total() : Money body: subtotal() + tax() -- tax is the subtotal times the local tax rate context Sale::tax() : Money body: subtotal() * Tax::localRate
Dont Do This
context LineItem::basePrice() : Money body: if size = DrinkSize::Small then if flavor = Flavor::Regular then Money::newInstance(1.23, Currency::USDollars) else Money::newInstance(1.73, Currency::USDollars) endif else if flavor = Flavor::Regular then Money::newInstance(2.43, Currency::USDollars) else Money::newInstance(3.13, Currency::USDollars) endif endif
Use Polymorphism
Little policy classes are an extension of the little operations idea. Smaller rules are easier to read.
context LargeRegularLemonade::basePrice() : Money body: Money::newInstance(2.43, Currency::USDollars) context SmallFlavoredLemonade::basePrice() : Money body: Money::newInstance(1.72, Currency::USDollars)
OCL Topics
OCL Basics
The LiquidLemons Example OCL in Together
Use the named property fields and boxes. Shows up in generated docs and XMI export. Not visible in diagrams.
OCL Notes
Attach notes containing OCL to correct contexts. OCL is visible in the diagrams. Potential to clutter the model.
Together Designer
OCL Topics
OCL Basics
The LiquidLemons Example OCL in Together
Domain Options
Tic-Tac-Toe Ants Electronic Voting Poem Golf Turtle Logo Dog Kennel