Object Constraint Language - PowerPoint PPT Presentation

1 / 47
About This Presentation
Title:

Object Constraint Language

Description:

Product of the Object Management Group (OMG) ... E Elms. 143. 44. 49. Red. D Dolly. 86. 50. 18. Green. C Cross. 93. 23. 35. Red. B Barry. 80. 32. 24 ... – PowerPoint PPT presentation

Number of Views:80
Avg rating:3.0/5.0
Slides: 48
Provided by: danm65
Category:

less

Transcript and Presenter's Notes

Title: Object Constraint Language


1
Object Constraint Language
  • Design by Contract and Queries
  • Dan Massey
  • YL Consulting

2
OCL Topics
  • Object Constraint Language (OCL)
  • OCL for Design by Contract
  • OCL for Queries
  • Another OCL Example

3
OCL Topics
  • Object Constraint Language (OCL)
  • OCL for Design by Contract
  • OCL for Queries
  • Another OCL Example

4
Object Constraint Language
  • OCL 2.0
  • Product of the Object Management Group (OMG)
  • Add-on to the Unified Modeling Language (UML)
    2.0 specification
  • UML combined with OCL forms a solid base for
    Model Driven Architecture (MDA)

5
Model Driven Architecture
  • Work with Platform Independent Model (PIM)
  • Transform PIM into Platform Specific Model (PSM)
  • Transform PSM into implementation

6
Model Constraints
  • Originally viewed as a way to constrain or to
    restrict values in a model
  • In addition OCL supports
  • Query expressions
  • Derived values
  • Conditions
  • Business rules

7
Reasons to use OCL
  • Express concepts that are not supported by UML
    diagrams
  • Make models more precise
  • Support transformation tools and code generation
    (MDA)

8
Approach
  • Were going to explore OCL by example to give you
    ideas as to where it might apply in your work.
  • At the end Ill point you toward sources of more
    complete information.

9
OCL Topics
  • Object Constraint Language (OCL)
  • OCL for Design by Contract
  • OCL for Queries
  • Another OCL Example

10
Design by Contract
  • Why do we use contracts?
  • We know the rules. We know what happens if we
    break them.
  • Design by Contract does the same for software.
  • Provide a formal specification of behavior.

11
More Design by Contract
  • winningBid()Bid is a signature, not a
    specification. Its like giving someone a phone
    without providing the format for a correct phone
    number.
  • Are there any conditions that must be met for a
    call to winningBid() to succeed?

12
And Design by Contract
  • Specify preconditions, postconditions, and
    invariants. Defensive programming?
  • Well cover each of these in turn.
  • Bertrand Meyer is the father of DbC
  • http//www2.inf.ethz.ch/personal/meyer/

13
Auction Domain
  • A partial auction domain to get us started.
    Includes Auction, Bid, and Money.
  • We have the structure, but what are the rules?

14
OCL Expression Context
  • Every OCL expression takes place in a context
  • context Auction
  • context AuctionwinningBid()Bid

15
Preconditions
  • Preconditions are the conditions that must be
    true prior to successfully invoking the method in
    question.
  • Often preconditions that fail result in
    exceptions.

16
Example Preconditions
  • context AuctionplaceBid(bidderId,
    bidMoney)
  • pre not bidder.oclIsUndefined()
  • pre not bid.oclIsUndefined()
  • pre bid.currency
  • self.bids-gtfirst().amount.currency
  • pre bid gt self.currentBid().money

17
Simple Preconditions
  • context AuctionplaceBid(bidderId,
    bidMoney)
  • pre not bidder.oclIsUndefined()
  • pre not bid.oclIsUndefined()
  • This operation requires a bidder identifier and a
    bid.

18
Implementing Preconditions
  • public void placeBid(Id bidder,
  • Money bid)
  • if (bidder null)
  • throw new NullPointerException(
  • bidder may not be null)

19
More Involved Preconditions
  • context AuctionplaceBid(bidderId,
    bidMoney)
  • pre bid.currency
  • self.bids-gtfirst().amount.currency
  • pre bid gt self.currentBid().money
  • The bid must be in the same currency as the
    starting bid. The bid must be higher than the
    current high bid.

20
Implementing Preconditions
  • if (bid.getAmount().getCurrency() !
  • ((Bid) bids.first()).getAmount()
  • .getCurrency())
  • throw new IllegalArgumentException(
  • currencies must match)

21
Assertion Temptation
  • // Dont do this. Anyone who runs your app
  • // can turn off assertions.
  • assert bidder ! null
  • bidder may not be null
  • assert bid.getAmount().getCurrency()
  • ((Bid) bids.first()).getAmount()
  • .getCurrency()
  • currencies must match

22
OCL Comments
  • context AuctionplaceBid(bidderId,
    bidMoney)
  • -- currencies must match
  • pre bid.currency
  • self.bids-gtfirst().amount.currency
  • pre alwaysTrue 1 1
  • -- and // are valid comment forms in OCL

23
More Preconditions
  • Preconditions are not limited to checking
    parameters. They can specify any condition that
    must be true prior to executing the operation.
  • What are some other possible preconditions?

24
More Preconditions
  • context AuctionwinningBid()Bid
  • -- auction must end to have a winner
  • pre TimeStampnow() gt self.expires()
  • context Bidgt(bidBid)Boolean
  • -- _gt is our name for gt
  • pre not bid.oclIsUndefined()

25
Invariants
  • Invariants are conditions that are always true.
  • They are always true. Think conservation of
    energy, not the color of the sky is blue.

26
Invariants
  • If an invariant is violated something is wrong
    with the implementation of your system.
  • What are some invariants in our domain?

27
Example Invariants
  • context Auction
  • inv self.expires() gt self.start
  • context Bid
  • inv self.amount.amount gt 0.0
  • / Note this could be specified in
  • the Auction context, but dont. /

28
Postconditions
  • Postconditions are the conditions that your
    operation guarantees will be true when it has
    completed.
  • The contract You meet the preconditions and Ill
    meet the postconditions.

29
PostConditions
  • context AuctionplaceBid(bidderId,
  • bidMoney)
  • post self.currentBid().amount bid
  • Notice that the postcondition assumes serial or
    single-threaded execution. This postcondition
    isnt necessarily enforceable in an unprotected
    multi-threaded context.

30
PostConditions
  • context AuctionplaceBid(bidderId,
  • bidMoney)
  • post self.currentBid() gt
  • self.currentBid_at_pre()
  • Postconditions can reach back in time.
  • _at_pre references the value before the operation.
  • Another way to state a precondition

31
Implementing PostConditions
  • public void placeBid(Id bidder,
  • Money bid)
  • Bid preCurrentBid this.currentBid()
  • if (this.currentBid() lt preCurrentBid)

32
OCL Contracts
  • Add more formal specifications to your models.
  • Specify what will happen, without the how.
  • Start at component boundaries.
  • You can start out with simple contracts.

33
OCL Topics
  • Object Constraint Language (OCL)
  • OCL for Design by Contract
  • OCL for Queries
  • Another OCL Example

34
OCL as Query Language
  • You can think of OCL as equivalent to SQL when it
    comes to querying object models.
  • Borlands ECO framework uses OCL for querying (as
    well as constraints, derived values, etc.)
  • Other query alternatives include OQL and Xquery

35
Test Results Mini Domain
  • Below is a simple domain for describing test
    results from test sessions at a competition for
    teams of logicians.
  • This is our domain for examining OCL queries.

36
Example Test Data Set
  • Heres some example test result data from a
    logician competition.

37
Derived Values
  • context TestResultcompositeScore Integer
  • derive (2 logicScore) illogicScore

38
OCL Collection Operations
  • context TestSessionnumberOfResults
    Integer
  • derive results-gtsize()

39
OCL Collection Choices
  • Set no duplicates, not ordered,result of
    navigating on association
  • OrderedSet not sorted, just ordered
  • Bag may contain duplicates, not ordered,
  • usually the result of navigating multiple
    associations
  • Sequence an ordered Bag

40
OCL Collections Classification
41
OCL select() Query
  • context TestSessionnumberOfScoresOverTarget(
    target Integer) Integer
  • body results-gtselect( compositeScore gt
    target)-gtsize()

42
Translating OCL Queries
  • Lets look at what a transformation tool might do
    with one of our queries in SQL.
  • SELECT COUNT() FROM test_results WHERE
    composite_score gt target

43
Returning a Collection
  • context TestSessiontestResultsForTeam( testTeam
    Team) Set(TestResult)
  • body results-gtselect(team testTeam)

44
Building Up Queries
  • context TestSessionaverageScoreForTeam( testTea
    m Team) Real
  • body testResultsForTeam(testTeam).compositeScore
  • -gtsum() / testResultsForTeam(testTeam)-gtsize()

45
Which Logicians Won?
  • context TestSessionbestResults()
    Set(TestResult)
  • body results-gtselect(compositeScore
  • results-gtsortedBy(compositeScore) -gtlast().compo
    siteScore)

46
OCL Resources
  • The Object Constraint Language, 2edJos Warmer
    and Anneke Kleppe
  • Klasse Objectens OCL pagehttp//www.klasse.nl/oc
    l/
  • OCL 2.0 Specification at OMGhttp//www.omg.org/cg
    i-bin/doc?ptc/2003-10-14

47
Questions and Answers
Write a Comment
User Comments (0)
About PowerShow.com