Object Constraint Language - PowerPoint PPT Presentation

1 / 37
About This Presentation
Title:

Object Constraint Language

Description:

... built-in types, including collections. User-defined types and ... Collections ... Together Designer. Syntax checked OCL in 'Constraint Notes' that ... – PowerPoint PPT presentation

Number of Views:133
Avg rating:3.0/5.0
Slides: 38
Provided by: danma85
Category:

less

Transcript and Presenter's Notes

Title: Object Constraint Language


1
Object Constraint Language
  • in Together
  • Dan Massey
  • YL Consulting

2
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.

3
OCL Topics
  • OCL Basics
  • The LiquidLemons Example
  • OCL in Together
  • Team Modeling Exercise

4
OCL Topics
  • OCL Basics
  • The LiquidLemons Example
  • OCL in Together
  • Team Modeling Exercise

5
OCL is
  • Strongly typed declarative language
  • Small set of built-in types, including
    collections
  • User-defined types and operators

6
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

7
Boolean Operations
  • a Boolean b Boolean
  • a and b a or b a xor b
  • not a a b a ltgt b
  • a implies b
  • implies constraint
  • if a is true then b must be true

8
Numeric Operators
  • Comparison
  • equals ltgt not equals
  • lt less gt more
  • lt less or equal gt more or equal
  • Math
  • addition - subtraction
  • multiplication / division

9
Every Type in OCL is an Object
  • a Integer bInteger
  • a.mod(b) a.div(b)
  • a.abs() a.max(b)
  • a.min(b) a.round()
  • a.floor()
  • Operators are just infix notation operations.

10
String Operations
  • a String b String
  • a b a ltgt b
  • a.concat(b) a.size()
  • a.toLower() a.toUpper()
  • a.subString(1, 3)
  • Operations return new Strings.

11
OCL Conditional
  • OCL provides one conditional construct
  • if ltBoolean expressiongt
  • then ltexpressiongt
  • else ltexpressiongt
  • endif
  • No elseif or switch/case

12
OCL Operation Precedence
  • Grouping ()
  • Path resolution
  • Message notation . -gt also
  • Unary - not
  • Multiplication/Division /
  • Addition/Subtraction -
  • Comparison lt gt lt gt ltgt
  • Logical and or xor
  • Logical implication implies

13
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

14
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.

15
Collections must by Typed
  • Set(Bid)
  • Bag(String)
  • Like generics in Java or templates in C.

16
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.

17
Basic Collections Operations
  • a Set(String) b String c Set(String)
  • a c a ltgt c
  • a-gtsize() a-gtcount(b)
  • a-gtexcludes(b) a-gtexcludesAll(c)
  • a-gtincludes(b) a-gtincludesAll(c)
  • a-gtisEmpty() a-gtnotEmpty()
  • a-gtsum() -- contents are of type supporting

18
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

19
Flatten
  • Recursively adds the members of nested
    collections to a single collection.
  • context MyType
  • def a Set(Set(Integer)) Set Set1, 2, 3,
    Set3, 4, 5, Set4, 5, 6
  • a-gtflatten() Set2, 3, 1, 5, 4, 6 -- no order

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

21
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
    RoleSpeaker

22
OCL Topics
  • OCL Basics
  • The LiquidLemons Example
  • OCL in Together
  • Team Modeling Exercise

23
Liquid Lemons Domain
24
LL Invariants
  • context Sale
  • -- a Sale has one or more items
  • inv numberOfSaleItems items-gtsize() gt 1
  • -- a Sale's subtotal may not be less than zero
  • inv subtotalGreaterThanZero subtotal().amount gt
    0.0 and subtotal().currency CurrencyUSDollars

25
LL Pre and Post
  • context LiquidLemonsaddLemonade(flavor
    Flavor, size DrinkSize)
  • pre activeSale currentSale-gtsize() 1
  • pre mustHaveFlavor not flavor.oclIsUndefined()
  • pre mustSpecifySize not size.oclIsUndefined()
  • post newLemonade currentLemonade-gtsize() 1

26
LL Query
27
Collections Queries
  • context ItemAnalyzeraveragePrice() Money
  • body items.basePrice()-gtsum() /
    items.basePrice()-gtsize()
  • context ItemAnalyzeruniqueModDescriptions()
    Set(String)
  • body items.modDescriptions() -gtflatten()-gtasSet()

28
Composite Queries
  • Use small methods with single responsibilities to
    build up larger functions.
  • -- the total we want is subtotal plus tax
  • context Saletotal() Money
  • body subtotal() tax()
  • -- tax is the subtotal times the local tax rate
  • context Saletax() Money
  • body subtotal() TaxlocalRate

29
Composite Queries Cont.
  • context Salesubtotal() Money
  • body items.price()-gtsum()
  • context LineItemprice() Money
  • body basePrice() modsPrice()
  • context LineItemmodsPrice() Money
  • body modifiers.price()-gtsum()

30
Dont Do This
  • context LineItembasePrice() Money
  • body
  • if size DrinkSizeSmall then
  • if flavor FlavorRegular then
  • MoneynewInstance(1.23, CurrencyUSDollars)
  • else
  • MoneynewInstance(1.73, CurrencyUSDollars
    )
  • endif
  • else
  • if flavor FlavorRegular then
  • MoneynewInstance(2.43, CurrencyUSDollars
    )
  • else
  • MoneynewInstance(3.13, CurrencyUSDollars
    )
  • endif
  • endif

31
Use Polymorphism
  • Little policy classes are an extension of the
    little operations idea. Smaller rules are easier
    to read.
  • context LargeRegularLemonadebasePrice() Money
  • body MoneynewInstance(2.43, CurrencyUSDollars
    )
  • context SmallFlavoredLemonadebasePrice()
    Money
  • body MoneynewInstance(1.72, CurrencyUSDollars
    )

32
OCL Topics
  • OCL Basics
  • The LiquidLemons Example
  • OCL in Together
  • Team Modeling Exercise

33
Where Does the OCL Go?
  • Use the named property fields and boxes.
  • Shows up in generated docs and XMI export.
  • Not visible in diagrams.

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

35
Together Designer
  • Syntax checked OCL in Constraint Notes that
    establish context.
  • Now in field test.

36
OCL Topics
  • OCL Basics
  • The LiquidLemons Example
  • OCL in Together
  • Team Modeling Exercise

37
Domain Options
  • Tic-Tac-Toe
  • Ants
  • Electronic Voting
  • Poem
  • Golf
  • Turtle Logo
  • Dog Kennel
Write a Comment
User Comments (0)
About PowerShow.com