object constraint language n.
Skip this Video
Download Presentation
Object Constraint Language

Loading in 2 Seconds...

play fullscreen
1 / 47

Object Constraint Language - PowerPoint PPT Presentation

  • Uploaded on

Object Constraint Language. Design by Contract and Queries Dan Massey Y&L Consulting. OCL Topics. Object Constraint Language (OCL) OCL for Design by Contract OCL for Queries Another OCL Example. OCL Topics. Object Constraint Language (OCL) OCL for Design by Contract OCL for Queries

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'Object Constraint Language' - clark

Download Now An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
object constraint language

Object Constraint Language

Design by Contract and Queries

Dan Massey

Y&L Consulting

ocl topics
OCL Topics
  • Object Constraint Language (OCL)
  • OCL for Design by Contract
  • OCL for Queries
  • Another OCL Example
ocl topics1
OCL Topics
  • Object Constraint Language (OCL)
  • OCL for Design by Contract
  • OCL for Queries
  • Another OCL Example
object constraint language1
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®)
model driven architecture
Model Driven Architecture
  • Work with Platform Independent Model (PIM)
  • Transform PIM into Platform Specific Model (PSM)
  • Transform PSM into implementation
model constraints
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
reasons to use ocl
Reasons to use OCL
  • Express concepts that are not supported by UML diagrams
  • Make models more precise
  • Support transformation tools and code generation (MDA)
  • We’re going to explore OCL by example to give you ideas as to where it might apply in your work.
  • At the end I’ll point you toward sources of more complete information.
ocl topics2
OCL Topics
  • Object Constraint Language (OCL)
  • OCL for Design by Contract
  • OCL for Queries
  • Another OCL Example
design by contract
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.
more design by contract
More Design by Contract
  • winningBid():Bid is a signature, not a specification. It’s 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?
and design by contract
And… Design by Contract
  • Specify preconditions, postconditions, and invariants. Defensive programming?
  • We’ll cover each of these in turn.
  • Bertrand Meyer is the father of DbC
  • http://www2.inf.ethz.ch/personal/meyer/
auction domain
Auction Domain
  • A partial auction domain to get us started. Includes “Auction,” “Bid,” and “Money.”
  • We have the structure, but what are the rules?
ocl expression context
OCL Expression Context
  • Every OCL expression takes place in a context:
  • contextAuction
  • contextAuction::winningBid():Bid
  • Preconditions are the conditions that must be true prior to successfully invoking the method in question.
  • Often preconditions that fail result in exceptions.
example preconditions
Example Preconditions
  • contextAuction::placeBid(bidder:Id,bid:Money)
  • pre:notbidder.oclIsUndefined()
  • pre:notbid.oclIsUndefined()
  • pre:bid.currency=
  • self.bids->first().amount.currency
  • pre:bid>self.currentBid().money
simple preconditions
Simple Preconditions
  • contextAuction::placeBid(bidder:Id,bid:Money)
  • pre:notbidder.oclIsUndefined()
  • pre:notbid.oclIsUndefined()
  • This operation requires a bidder identifier and a bid.
implementing preconditions
Implementing Preconditions
  • publicvoid placeBid(Id bidder,
  • Money bid) {
  • if (bidder == null) {
  • thrownew NullPointerException(
  • “bidder may not be null”);
  • }
more involved preconditions
More Involved Preconditions
  • contextAuction::placeBid(bidder:Id,bid:Money)
  • pre:bid.currency=
  • self.bids->first().amount.currency
  • pre:bid>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.
implementing preconditions1
Implementing Preconditions
  • if (bid.getAmount().getCurrency() !=
  • ((Bid) bids.first()).getAmount()
  • .getCurrency()) {
  • thrownew IllegalArgumentException(
  • “currencies must match”);
  • }
assertion temptation
Assertion Temptation
  • // Don’t 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”;
ocl comments
OCL Comments
  • contextAuction::placeBid(bidder:Id,bid:Money)
  • -- currencies must match
  • pre:bid.currency=
  • self.bids->first().amount.currency
  • prealwaysTrue:1=1
  • -- and /**/ are valid comment forms in OCL
more preconditions
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?
more preconditions1
More Preconditions
  • contextAuction::winningBid():Bid
  • -- auction must end to have a winner
  • pre:TimeStamp::now()>=self.expires()
  • contextBid::>(bid:Bid):Boolean
  • -- _gt is our name for “>”
  • pre:notbid.oclIsUndefined()
  • Invariants are conditions that are always true.
  • They are always true. Think conservation of energy, not “the color of the sky is blue.”
  • If an invariant is violated something is wrong with the implementation of your system.
  • What are some invariants in our domain?
example invariants
Example Invariants
  • contextAuction
  • inv:self.expires()>self.start
  • contextBid
  • inv:self.amount.amount>0.0
  • /* Note this could be specified in
  • the Auction context, but don’t. */
  • Postconditions are the conditions that your operation guarantees will be true when it has completed.
  • The contract: You meet the preconditions and I’ll meet the postconditions.
  • contextAuction::placeBid(bidder:Id,
  • bid:Money)
  • post: self.currentBid().amount=bid
  • Notice that the postcondition assumes serial or single-threaded execution. This postcondition isn’t necessarily enforceable in an unprotected multi-threaded context.
  • contextAuction::placeBid(bidder:Id,
  • bid:Money)
  • post:self.currentBid()>
  • self.currentBid@pre()
  • Postconditions can reach back in time.
  • @pre references the value before the operation.
  • Another way to state a precondition…
implementing postconditions
Implementing PostConditions
  • publicvoid placeBid(Id bidder,
  • Money bid) {
  • Bid preCurrentBid = this.currentBid();
  • if (this.currentBid() <= preCurrentBid) {
ocl contracts
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.
ocl topics3
OCL Topics
  • Object Constraint Language (OCL)
  • OCL for Design by Contract
  • OCL for Queries
  • Another OCL Example
ocl as query language
OCL as Query Language
  • You can think of OCL as equivalent to SQL when it comes to querying object models.
  • Borland’s ECO framework uses OCL for querying (as well as constraints, derived values, etc.)
  • Other query alternatives include OQL and Xquery
test results mini domain
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.
example test data set
Example Test Data Set
  • Here’s some example test result data from a logician competition.
derived values
Derived Values
  • contextTestResult::compositeScore:Integer
  • derive:(2*logicScore)+illogicScore
ocl collection operations
OCL Collection Operations
  • contextTestSession::numberOfResults:Integer
  • derive:results->size()
ocl collection choices
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
ocl select query
OCL select() Query
  • contextTestSession::numberOfScoresOverTarget( target:Integer):Integer
  • body:results->select( compositeScore>target)->size()
translating ocl queries
Translating OCL Queries
  • Let’s look at what a transformation tool might do with one of our queries in SQL.
  • SELECT COUNT(*) FROM test_resultsWHERE composite_score > :target;
returning a collection
Returning a Collection
  • contextTestSession::testResultsForTeam( testTeam:Team):Set(TestResult)
  • body:results->select(team=testTeam)
building up queries
Building Up Queries
  • contextTestSession::averageScoreForTeam( testTeam:Team):Real
  • body:testResultsForTeam(testTeam).compositeScore
  • ->sum()/testResultsForTeam(testTeam)->size()
which logicians won
Which Logicians Won?
  • contextTestSession::bestResults():Set(TestResult)
  • body:results->select(compositeScore=
  • results->sortedBy(compositeScore) ->last().compositeScore)
ocl resources
OCL Resources
  • The Object Constraint Language, 2edJos Warmer and Anneke Kleppe
  • Klasse Objecten’s OCL pagehttp://www.klasse.nl/ocl/
  • OCL 2.0 Specification at OMGhttp://www.omg.org/cgi-bin/doc?ptc/2003-10-14