410 likes | 569 Views
Object Constraint Language. in Together Dan Massey Y&L Consulting. Picking up From Logicians. 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
E N D
Object Constraint Language in Together Dan Massey Y&L Consulting
Picking up From Logicians • 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 • Team Modeling Exercise
OCL Topics • OCL Basics • The LiquidLemons Example • OCL in Together • Team Modeling Exercise
OCL is • Strongly typed declarative language • Small set of built-in types, including collections • User-defined types and operators
OCL Basic Types • Boolean true or false • Integer theoretically unlimited natural numbers, subtype of Real • Real mathematical Real, no specific • implementation implied • String ‘sequence of characters’
Boolean Operations • a : Boolean b : Boolean • a and b a or b a xor b • not a a = b a <> b • a implies b • implies constraint: • if a is true then b must be true
Numeric Operators • Comparison • = equals <> not equals • < less > more • <= less or equal >= more or equal • Math • + addition - subtraction • * multiplication / division
Every Type in OCL is an Object • a : Integer b:Integer • a.mod(b) a.div(b) • a.abs() a.max(b) • a.min(b) a.round() • a.floor() • Operators are just infix notation operations.
String Operations • a : String b : String • a = b a <> b • a.concat(b) a.size() • a.toLower() a.toUpper() • a.subString(1, 3) • Operations return new Strings.
OCL Conditional • OCL provides one conditional construct: • if <Boolean expression> • then <expression> • else <expression> • endif • No “elseif” or “switch”/“case”
OCL Operation Precedence • Grouping () • Path resolution :: • Message notation . -> also ^ ^^ • Unary - not • Multiplication/Division * / • Addition/Subtraction + - • Comparison < > <= >= <> = • Logical and or xor • Logical implication implies
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 attributed is empty.
Collections must by Typed • Set(Bid) • Bag(String) • Like generics in Java or templates in C++.
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.
Basic Collections Operations • a : Set(String) b : String c : 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 +
More Collections Operations • OCL supports a wide range of Collection operations that vary by Collection type. • first(), last(), at(i : Integer), etc. for {ordered} collections • union(), -, asSet(), including(), symmetricDifference(collection : Collection) are a sample
Flatten • Recursively adds the members of nested collections to a single collection. • contextMyType • 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)
Other OCL Types • OclAny the root Type • OclVoid the “null” Type “undefined” • OclType a type, like “Class” in Java • OCL provides operations for type identification. • Tuple group of named values • Tuple {name : String = ‘Dan’, role : Role = Role::Speaker}
OCL Topics • OCL Basics • The LiquidLemons Example • OCL in Together • Team Modeling Exercise
LL Invariants • contextSale • -- a Sale has zero or more items • invnumberOfSaleItems:notitems->size()>=0 • -- a Sale's subtotal may not be less than zero • invsubtotalGreaterThanZero:subtotal().amount>0.0andsubtotal().currency= Currency::USDollars
LL Pre and Post • contextLiquidLemons::addLemonade(flavor:Flavor,size:DrinkSize) • preactiveSale:currentSale->size()=1 • premustHaveFlavor:notflavor.oclIsUndefined() • premustSpecifySize:notsize.oclIsUndefined() • postnewLemonade:currentLemonade->size()=1
Collections Queries • contextItemAnalyzer::averagePrice():Money • body:items.basePrice()->sum()/items.basePrice()->size() • contextItemAnalyzer::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 • contextSale::total():Money • body:subtotal()+tax() • -- tax is the subtotal times the local tax rate • contextSale::tax():Money • body:subtotal()*Tax::localRate
Composite Queries Cont. • contextSale::subtotal():Money • body:items.price()->sum() • contextLineItem::price():Money • body:basePrice()+modsPrice() • contextLineItem::modsPrice():Money • body:modifiers.price()->sum()
Don’t Do This • contextLineItem::basePrice():Money • body: • ifsize=DrinkSize::Smallthen • ifflavor=Flavor::Regularthen • Money::newInstance(1.23,Currency::USDollars) • else • Money::newInstance(1.73,Currency::USDollars) • endif • else • ifflavor=Flavor::Regularthen • 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. • contextLargeRegularLemonade::basePrice():Money • body:Money::newInstance(2.43,Currency::USDollars) • contextSmallFlavoredLemonade::basePrice():Money • body:Money::newInstance(1.72,Currency::USDollars)
OCL Topics • OCL Basics • The LiquidLemons Example • OCL in Together • Team Modeling Exercise
Where Does the OCL Go? • 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 • Syntax checked OCL in “Constraint Notes” that establish context. • Now in field test.
OCL Topics • OCL Basics • The LiquidLemons Example • OCL in Together • Team Modeling Exercise
Workshop Model(s) • Scott Ambler addresses writes about model-storming in the October 4th issue of “Software Development” • Short burst of modeling as a team. • We’re model-storming “to analyze requirements”
Domain Options • Tic-Tac-Toe • Ants • Electronic Voting • Poem • Golf • Turtle Logo • Dog Kennel
Regroup • OCL Benefits • OCL Challenges • Domain Modeling
Questions and Answers • See the paper for session 3016 to read more about the LiquidLemons example. The paper also contains a diagram and OCL frojm the Tic-Tac-Toe group modeling activity from the tutorial.