ocl tutorial unleash

37
Object Constraint Language in Together Dan Massey Y&L Consulting

Upload: shriniwas-yadav

Post on 26-Aug-2014

210 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Ocl Tutorial Unleash

Object Constraint Language

in Together

Dan Massey

Y&L Consulting

Page 2: Ocl Tutorial Unleash

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.

Page 3: Ocl Tutorial Unleash

OCL TopicsOCL Basics

The LiquidLemons Example

OCL in Together

Team Modeling Exercise

Page 4: Ocl Tutorial Unleash

OCL TopicsOCL Basics

The LiquidLemons Example

OCL in Together

Team Modeling Exercise

Page 5: Ocl Tutorial Unleash

OCL isStrongly typed declarative language

Small set of built-in types, including collections

User-defined types and operators

Page 6: Ocl Tutorial Unleash

OCL Basic TypesBoolean true or falseInteger theoretically unlimited natural

numbers, subtype of RealReal mathematical Real, no specific

implementation impliedString ‘sequence of characters’

Page 7: Ocl Tutorial Unleash

Boolean Operationsa : Boolean b : Boolean

a and b a or b a xor bnot a a = b a <> ba implies b

implies constraint:if a is true then b must be true

Page 8: Ocl Tutorial Unleash

Numeric OperatorsComparison= equals <> not equals< less > more<= less or equal >= more or equal

Math+ addition - subtraction* multiplication / division

Page 9: Ocl Tutorial Unleash

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.

Page 10: Ocl Tutorial Unleash

String Operationsa : String b : String

a = b a <> ba.concat(b) a.size()a.toLower() a.toUpper()a.subString(1, 3)

Operations return new Strings.

Page 11: Ocl Tutorial Unleash

OCL ConditionalOCL provides one conditional construct:

if <Boolean expression>then <expression>else <expression>endif

No “elseif” or “switch”/“case”

Page 12: Ocl Tutorial Unleash

OCL Operation PrecedenceGrouping ()Path resolution ::Message notation . -> also ^ ^^Unary - notMultiplication/Division * /Addition/Subtraction + -Comparison < > <= >= <> =Logical and or xorLogical implication implies

Page 13: Ocl Tutorial Unleash

User-Defined TypesClasses, Interfaces, etc. defined in the model

Users may overload infix and unary operators:Multiply Money by a RealAdd two Distance values

Page 14: Ocl Tutorial Unleash

OCL CollectionsRefreshing our memories, we have:CollectionBag SequenceSet OrderedSet

OCL allows you to treat any instance like a collection. You can ask if a single attribute is empty.

Page 15: Ocl Tutorial Unleash

Collections must by Typed

Set(Bid)Bag(String)

Like generics in Java or templates in C++.

Page 16: Ocl Tutorial Unleash

Collections LiteralsUse 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.

Page 17: Ocl Tutorial Unleash

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 +

Page 18: Ocl Tutorial Unleash

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

Page 19: Ocl Tutorial Unleash

FlattenRecursively adds the members of nested

collections to a single collection.

context MyTypedef: 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

Page 20: Ocl Tutorial Unleash

Collection LoopsOCL 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)

Page 21: Ocl Tutorial Unleash

Other OCL TypesOclAny the root TypeOclVoidthe “null” Type “undefined”OclType a type, like “Class” in JavaOCL provides operations for type identification.

Tuple group of named valuesTuple {name : String = ‘Dan’,

role : Role = Role::Speaker}

Page 22: Ocl Tutorial Unleash

OCL TopicsOCL Basics

The LiquidLemons Example

OCL in Together

Team Modeling Exercise

Page 23: Ocl Tutorial Unleash

Liquid Lemons Domain

Page 24: Ocl Tutorial Unleash

LL Invariantscontext Sale-- a Sale has one or more itemsinv numberOfSaleItems : items->size() > 1

-- a Sale's subtotal may not be less than zeroinv subtotalGreaterThanZero : subtotal().amount >

0.0 and subtotal().currency = Currency::USDollars

Page 25: Ocl Tutorial Unleash

LL Pre and Postcontext LiquidLemons::addLemonade(flavor :

Flavor, size : DrinkSize)pre activeSale : currentSale->size() = 1pre mustHaveFlavor : not flavor.oclIsUndefined()pre mustSpecifySize : not size.oclIsUndefined()post newLemonade :

currentLemonade->size() = 1

Page 26: Ocl Tutorial Unleash

LL Query

Page 27: Ocl Tutorial Unleash

Collections Queriescontext ItemAnalyzer::averagePrice() : Moneybody: items.basePrice()->sum() /

items.basePrice()->size()

context ItemAnalyzer::uniqueModDescriptions() : Set(String)

body: items.modDescriptions() ->flatten()->asSet()

Page 28: Ocl Tutorial Unleash

Composite QueriesUse small methods with single responsibilities to build up

larger functions.

-- the total we want is subtotal plus taxcontext Sale::total() : Moneybody: subtotal() + tax()

-- tax is the subtotal times the local tax ratecontext Sale::tax() : Moneybody: subtotal() * Tax::localRate

Page 29: Ocl Tutorial Unleash

Composite Queries Cont.context Sale::subtotal() : Moneybody: items.price()->sum()

context LineItem::price() : Moneybody: basePrice() + modsPrice()

context LineItem::modsPrice() : Moneybody: modifiers.price()->sum()

Page 30: Ocl Tutorial Unleash

Don’t Do Thiscontext LineItem::basePrice() : Moneybody: 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

Page 31: Ocl Tutorial Unleash

Use PolymorphismLittle policy classes are an extension of the little operations

idea. Smaller rules are easier to read.

context LargeRegularLemonade::basePrice() : Moneybody: Money::newInstance(2.43, Currency::USDollars)

context SmallFlavoredLemonade::basePrice() : Moneybody: Money::newInstance(1.72, Currency::USDollars)

Page 32: Ocl Tutorial Unleash

OCL TopicsOCL Basics

The LiquidLemons Example

OCL in Together

Team Modeling Exercise

Page 33: Ocl Tutorial Unleash

Where Does the OCL Go?

Use the named property fields and boxes.Shows up in generated docs and XMI export.Not visible in diagrams.

Page 34: Ocl Tutorial Unleash

OCL Notes

Attach notes containing OCL to correct contexts.OCL is visible in the diagrams.Potential to clutter the model.

Page 35: Ocl Tutorial Unleash

Together Designer

Syntax checked OCL in “Constraint Notes” that establish context.

Now in field test.

Page 36: Ocl Tutorial Unleash

OCL TopicsOCL Basics

The LiquidLemons Example

OCL in Together

Team Modeling Exercise

Page 37: Ocl Tutorial Unleash

Domain OptionsTic-Tac-ToeAntsElectronic VotingPoemGolfTurtle LogoDog Kennel