The next example is an experiment on playing Lotto. In this simplified version of Lotto you have to guess seven different numbers in the interval from 1 to 34. You may submit one bet each week. At the end of the week the Lotto system chooses randomly seven different winner numbers, and the winning players are those that have submitted a bet with these winner numbers.
We use a MonitorSystem
to represent the players and the Lotto. A player is represented by a MonitorProcess
and the Lotto is represented by a Monitor
. Each Player
may submit one or more bets to the Lotto during a week.
The Lotto system has the following overall structure:
LottoExperiment: obj MonitorSystem
mnoOfPlayers: val 500
betSize: val 7
class Hand:
:::
class Bet(thePlayer: ref Player, theHand: ref Hand):
:::
class Player(inx: var integer): MonitorProcess
:::
Lotto: obj Monitor
:::
controller: obj MonitorProcess("Controller"):
:::
generatePlayers: do
:::
controller.start
LottoExperiment
has the following main attributes:
- Constants
noOfPlayers
andbetSize
, which defines the number ofPlayer
objects and the size of aHand
in aBet
. - Class
Hand
which represents a hand of seven numbers. - Class
Bet
which represents a bet. It has parametersthePlayer
, which is thePlayer
that has submitted theBet
andtheHand
, which is theHand
of theBet
. - Class
Player
which represents a player. - The
Monitor
-objectLotto
, which represents the Lotto. - A
MonitorProcess
-objectcontroller
which controls the system. - An object
generatePlayers
which is an object that generates the players. - A statement that starts the
controller
process.
Class Player
has the following structure:
class Player: MonitorProcess
play: do
cycle
lotto.submit(Bet(this(Player),Hand)
doSomethingElse
- A
Player
repeatedly submits aBet
by invokingLotto.submit
. - The argument of
submit
is the objectBet(this(Player), Hand)
. - The first argument of
Bet
is a reference to thePlayer
submitting theBet
. - The second argument of
Bet
is aHand
which randomly generates 7 numbers. - After submitting a
Bet
, it executesdoSomethingElse
before playing the next time;doSomethingElse
is not specified here.
The Lotto is represented by an object Lotto
. This object keeps the bets being submitted, it has the winning bet, and the deadline for submitting bets. As players in parallel submit bets by calling the method submit
, the Lotto
object is defined as a Monitor
with
as an entry method. submit
The structure of Lotto
is as follows:
Lotto: obj Monitor
submit(B: ref Bet): entry
wait(possibleToSubmit)
bets.insert(B)
clearBets: entry
bets.clear
possibleToSubmit := true
findWinningBets: entry
possibleToSubmit := false
:::
possibleToSubmit: var boolean
bets: obj Set(#Bet)
possibleToSubmit := true
Lotto
has three entry-methodssubmit
,clearBets
andfindWinningBets
wheresubmit
is invoked by thePlayer
-objects, whileandclearBets
andfindWinningBets
are invoked by thecontroller
object.- The
Bets
being submitted are stored inbets
which is aSet
. - It also has a boolean
possibletoSubmit
, which controls whether or not it is possible to submit a Bet.
As said, a Lotto-period is a week. During a week the Players
may submit bets. At the end of a week, the controller
invokes findWinningBets
to find the possible winners of the week. While this goes on, it is not possible to submit bets.
When possible winners have been found, the controller
, invokes clearBets
to remove all elements for bets and open up for a new round of submissions.
Følgende liste beskriver Lotto, men den står som den beskriver controller.
- The
wait
-method ofMonitor
is used insubmitBet
to check if it is possible to submit aBet
. - The statement
wait(possibleToSubmit)
delays the invoker untilpossibleToSubmit
has the value true. - When the
Lotto
object is generated,possibleToSubmit
is assigned the value True. - When
findWinningBets
is invoked, it assigns false topossibleToSubmit
. - Finally when
clearBets
is invoked it assigns true topossibleToSubmit
and thus opens up for another round of bets.
The details of controller
is as follows:
controller: obj MonitorProcess("Controller")
cycle
waitAweek
Lotto.findWinningBets
Lotto.clearBet
As can be seen, controller
executes cycle
where it waitAweek
(not specified here), invokes Lotto.findWinningBets
followed by Lotto.clearBets
.
We now show the details of findWinningBets
:
findWinningBets: entry
possibleToSubmit := false ++++ burde måske stå i controller
winningBet := Bet(Player("Winner"), Hand)
bets.scan
if (current.equal(winningBet)) :then
"Winner: ".print
current.print
newline
- As mentioned, it starts by assigning
possibleToSubmit
to false. - It then generates a
winningBet
with a hypotheticalPlayer
called “Winner” and aHand
with seven random numbers as arguments - It then scans the submitted bets in
bets
and checks whether or not the currentBet
is equal to thewinningBet
- If a winner is found, the
Player
of thisBet
is printed.
Finally we show the structure of class Hand
and class Bet
:
class Hand:
numbers: obj Array(betSize,#integer)
for (1):to(betSize):repeat
numbers.put(random(1,34)):at[inx]
class Bet(thePlayer: ref Player, theHand: ref Hand):
equal(aBet ref Bet) -> B: var boolean:
...
- Class
Hand
has an arraynumbers
that contains the seven random numbers of theHand
. - It uses a
for:to:repeat
to generate and store seven random numbers innumbers
. - Class
Bet
has anequal
method in addition to its two parameters.
After thought. The clever reader may have observed that we may simplify the example by eliminating the use of wait(possbleToSubmit)
. When findingWinners
is executed, it is not possible to invoke submit
since both methods are entry
-methods. A submit
invocation may be executed in between the controller
invokes Lotto.findingWinners
and Lotto.clearBets
, which may be problematic. It is, however, possible to execute bets.clear
as the last statement of findingWinners
, which makes wait(possibleToSubmit)
superfluous.
Another problem with the above solution is that after the controller has waited a week, it executes Lotto.findWinningBets
. This method is an entry method of the Lotto
Monitor
and will thus have to ‘compete’ with the 500 Players
on getting access to Lotto
. This may delay the time when submissions are suspended. In practice this may not be a problem since a Player
probably do not submis bets all the time during a week.
From a modelling point-of-view, the controller
should really be part of the Lotto
system and not a separate method. This means that we should define an object Lotto
containing the current Lotto
Monitor
as well as the controller
. This, will require another parallel abstraction than MonitorSystem
.