0% found this document useful (0 votes)
176 views20 pages

OCL Tutorial: Feb 16, 2007 Prepared by Mazeiar Salehie

The answer is no, we cannot simplify the expression as written because it would not be valid OCL. The expression "players.tournaments.matches->includes(self)" is trying to navigate across multiple associations in one step, which is not allowed in OCL. OCL only allows navigating one association at a time. The original expression "p.tournaments->exists(t| t.matches->includes(self))" navigates each association (players to tournaments, tournaments to matches) separately, which is valid OCL.

Uploaded by

Neha Kale
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
176 views20 pages

OCL Tutorial: Feb 16, 2007 Prepared by Mazeiar Salehie

The answer is no, we cannot simplify the expression as written because it would not be valid OCL. The expression "players.tournaments.matches->includes(self)" is trying to navigate across multiple associations in one step, which is not allowed in OCL. OCL only allows navigating one association at a time. The original expression "p.tournaments->exists(t| t.matches->includes(self))" navigates each association (players to tournaments, tournaments to matches) separately, which is valid OCL.

Uploaded by

Neha Kale
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

OCL Tutorial

Feb 16, 2007


Prepared by Mazeiar Salehie
Resource
• This tutorial is based on the Arena management system
discussed in the following book:
– B. Bruegge, A. H. Dutoit, “Object-oriented Software Engineering
- Using UML, Patterns, and Java,” Prentice Hall, 2nd edition,
2004
• The example is in Chapter 9 – Object design: specifying
interfaces
• Some modifications have been applied to OCL
expressions in the book to comply with OCL 2.0
specification.
• The examples are about constraints on classes, but the
same expressions are valid for components except for
some slight modifications for provided and required
interfaces.

Winter 2007 ECE 493 T5 -CBSE 2


First Example

Tournament
- maxNumPlayers: int
+ getMaxNumPlayers():int
+ getPlayers(): List
+ acceptPlayer(p:Player)
+ removePlayer(p:Player)
+ isPlayerAccepted(p:Player):boolean

Winter 2007 ECE 493 T5 -CBSE 3


Interface Annotations
public class Tournament {

/** The maximum number of players


* is positive at all times.
* @invariant maxNumPlayers > 0
*/
private int maxNumPlayers;

/** The players List contains


* references to Players who
* are registered with the
* Tournament. */
private List players;

/** Returns the current number of


* players in the tournament. */
public int getNumPlayers() {…}

/** Returns the maximum number of


* players in the tournament. */
public int getMaxNumPlayers() {…}

Winter 2007 ECE 493 T5 -CBSE 4


OCL Expressions 1
• context Tournament::acceptPlayer(p)
pre: not isPlayerAccepted(p)

• context Tournament::acceptPlayer(p)
pre: getNumPlayers() < getMaxNumPlayers()

• context Tournament::acceptPlayer(p)
post: isPlayerAccepted(p)

• context Tournament::acceptPlayer(p)
post: getNumPlayers() = getNumPlayers()@pre + 1

Winter 2007 ECE 493 T5 -CBSE 5


OCL Expressions 2
• context Tournament::removePlayer(p)
pre: isPlayerAccepted(p)

• context Tournament::removePlayer(p)
post: not isPlayerAccepted(p)

• context Tournament::removePlayer(p)
post: getNumPlayers() = getNumPlayers()@pre - 1

Winter 2007 ECE 493 T5 -CBSE 6


Second Example
League
*
+start:Date
+end:Date
+getActivePlayers()

{ordered}
* tournaments
Tournament
+start:Date
+end:Date
+acceptPlayer(p:Player)
* tournaments

* players
players
Player
*
+name:String
+email:String
Winter 2007 ECE 493 T5 -CBSE 7
Informal Constraints
• A Tournament’s planned duration must be
under one week.
• Players can be accepted in a Tournament
only if they are already registered with the
corresponding League.
• The number of active Players in a League
are those that have taken part in at least
one Tournament of the League.

Winter 2007 ECE 493 T5 -CBSE 8


Three Basic Types of Navigations

1. Local attribute 2. Directly related class 3. Indirectly related class


Tournament League League
start:Date *
end:Date
* *
Player Tournament
*

*
• Any OCL constraints can be Player
built using a combination of
these types
Winter 2007 ECE 493 T5 -CBSE 9
An Instance Problem Space
tttExpert:League chessNovice:League

winter:Tournament xmas:Tournament
start=Dec 21 start=Dec 23
end=Dec 22 end=Dec 25

alice:Player

bob:Player

marc:Player

joe:Player

zoe:Player
2 Leagues, 2 Tournaments and 5 Players
Winter 2007 ECE 493 T5 -CBSE 10
OCL Expressions
• Local attribute navigation
context Tournament
inv: end - start <= Calendar.WEEK
• Directly related class navigation
context Tournament::acceptPlayer(p)
pre: league.players->includes(p)
• Indirectly related class navigation
context League::getActivePlayers
post: result =
tournaments.players->asSet()
Winter 2007 ECE 493 T5 -CBSE 11
Third Example
TournamentForm
1
1
+applyForTournament() TournamentControl

* +selectSponsors(advertisers):List *
+advertizeTournament()
+acceptPlayer(p)
+announceTournament()
+isPlayerOverbooked():boolean
1

1
Tournament
+name:String
* +start:Date *
* * players +end:Date sponsors * *
Player +acceptPlayer(p) Advertiser
+removePlayer(p)
* +schedule()
matches *
matches
Match
*
+start:Date
+status:MatchStatus
+playMove(p,m)
+getScore():Map
Winter 2007 ECE 493 T5 -CBSE 12
OCL Expressions 1
• OCL forall quantifier
/* All Matches in a Tournament occur
within the Tournament’s time frame
*/

context Tournament
inv:
matches->forAll(m:Match |
m.start.after(t.start) and
m.end.before(t.end))
Winter 2007 ECE 493 T5 -CBSE 13
OCL Expressions 2
• OCL exists quantifier
/* Each Tournament conducts at
least one Match on the first
day of the Tournament */

context Tournament
inv:
matches->exists(m:Match |
m.start.equals(start))
Winter 2007 ECE 493 T5 -CBSE 14
OCL Expressions 3
• context
TournamentControl::selectSponsors(advertisers)
pre: interestedSponsors->notEmpty and
tournament.sponsors->isEmpty

• context
TournamentControl::selectSponsors(advertisers)
post: tournament.sponsors.equals(advertisers)

Winter 2007 ECE 493 T5 -CBSE 15


OCL Expressions 4
• context TournamentControl::advertiseTournament()
pre: tournament.sponsors->isEmpty and not
tournament.advertised

• context TournamentControl::advertiseTournament()
post: tournament.advertised

• context TournamentControl::acceptPlayer(p)
pre: tournament.advertised and
interestedPlayers->includes(p) and
not isPlayerOverbooked(p)

• context TournamentControl::acceptPlayer(p)
post: tournament.players->includes(p)
Winter 2007 ECE 493 T5 -CBSE 16
OCL Expressions 5
• All Matches of in a Tournament must
occur within the time frame of the
Tournament

context Tournament
inv: matches->forAll(m|
m.start.after(start) and
m.start.before(end))

Winter 2007 ECE 493 T5 -CBSE 17


Forth Example
players *
Player Tournament
* tournaments

players *
matches
Match
*

• Dealing with a loop in class diagram

Winter 2007 ECE 493 T5 -CBSE 18


OCL Expressions 1
• No Player can take part in two or
more Tournaments that overlap

context TournamentControl
inv: tournament.players->forAll(p|
p.tournaments->forAll(t|
t <> tournament implies
not t.overlap(tournament)))

Winter 2007 ECE 493 T5 -CBSE 19


OCL Expressions 2
• A match can only involve players who are
accepted in the tournament
context Match
inv: players->forAll(p|
p.tournaments->exists(t|
t.matches->includes(self)))
• Can we simplify the above expression as:
context Match
inv:players.tournaments.matches->
includes(self)
• It seems so. But, the answer is no! Can you tell
why?
Winter 2007 ECE 493 T5 -CBSE 20

You might also like