ocl tutorial unleash

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

Author: shriniwas-yadav

Post on 26-Aug-2014

186 views

Category:

Documents


0 download

Embed Size (px)

TRANSCRIPT

Object Constraint Languagein 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 TopicsOCL BasicsThe LiquidLemons Example OCL in Together

Team Modeling Exercise

OCL TopicsOCL BasicsThe LiquidLemons Example OCL in Together

Team Modeling Exercise

OCL isStrongly typed declarative languageSmall set of built-in types, including collections User-defined types and operators

OCL Basic TypesBoolean true or false Integer theoretically unlimited natural numbers, subtype of Real Real mathematical Real, no specific implementation implied String sequence of characters

Boolean Operationsa : Booleana and b not a a implies b a or b a=b

b : Booleana xor b a b

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

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

/

subtraction division

Every Type in OCL is an Objecta : Integera.mod(b) a.abs() a.min(b) a.floor()

b:Integera.div(b) a.max(b) a.round()

Operators are just infix notation operations.

String Operationsa : Stringa=b a.concat(b) a.toLower() a.subString(1, 3)

b : Stringa b a.size() a.toUpper()

Operations return new Strings.

OCL ConditionalOCL provides one conditional construct:if then else endif No elseif or switch/case

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

User-Defined TypesClasses, Interfaces, etc. defined in the modelUsers may overload infix and unary operators: Multiply Money by a Real Add two Distance values

OCL CollectionsRefreshing our memories, we have: Collection Bag Sequence Set OrderedSetOCL allows you to treat any instance like a collection. You can ask if a single attribute is empty.

Collections must by TypedSet(Bid) Bag(String) Like generics in Java or templates in C++.

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.

Basic Collections Operationsa : Set(String)a=c

b : Stringa 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 +

More Collections OperationsOCL 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

FlattenRecursively 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 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)

Other OCL TypesOclAny 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 TopicsOCL BasicsThe LiquidLemons Example OCL in Together

Team Modeling Exercise

Liquid Lemons Domain

LL Invariantscontext 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 Pre and Postcontext LiquidLemons::addLemonade(flavor : Flavor, size : DrinkSize) pre activeSale : currentSale->size() = 1 pre mustHaveFlavor : not flavor.oclIsUndefined() pre mustSpecifySize : not size.oclIsUndefined() post newLemonade : currentLemonade->size() = 1

LL Query

Collections Queriescontext ItemAnalyzer::averagePrice() : Money body: items.basePrice()->sum() / items.basePrice()>size() context ItemAnalyzer::uniqueModDescriptions() : Set(String) body: items.modDescriptions() ->flatten()->asSet()

Composite QueriesUse 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

Composite Queries Cont.context Sale::subtotal() : Money body: items.price()->sum() context LineItem::price() : Money body: basePrice() + modsPrice() context LineItem::modsPrice() : Money body: modifiers.price()->sum()

Dont Do Thiscontext 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 PolymorphismLittle 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 TopicsOCL BasicsThe 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 TopicsOCL BasicsThe LiquidLemons Example OCL in Together

Team Modeling Exercise

Domain OptionsTic-Tac-Toe Ants Electronic Voting Poem Golf Turtle Logo Dog Kennel