12.2 Lotto example sketch – OLD II

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 and betSize, which defines the number of Player objects and the size of a Hand in a Bet.
  • Class Hand which represents a hand of seven numbers.
  • Class Bet which represents a bet. It has parameters thePlayer, which is the Player that has submitted the Bet and theHand, which is the Hand of the Bet.
  • Class Player which represents a player.
  • The Monitor-object Lotto, which represents the Lotto.
  • A MonitorProcess-object controller 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 a Bet by invoking Lotto.submit.
  • The argument of submit is the object Bet(this(Player), Hand).
  • The first argument of Bet is a reference to the Player submitting the Bet.
  • The second argument of Bet is a Hand which randomly generates 7 numbers.
  • After submitting a Bet, it executes doSomethingElse 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 submit as an entry method. 

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-methods submit, clearBets and findWinningBets where submit is invoked by the Player-objects, while and clearBets and findWinningBets are invoked by the controller object.
  • The Bets being submitted are stored in bets which is a Set.
  • 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 of Monitor is used in submitBet to check if it is possible to submit a Bet.
  • The statement wait(possibleToSubmit) delays the invoker until possibleToSubmit has the value true.
  • When the Lotto object is generated, possibleToSubmit is assigned the value True.
  • When findWinningBets is invoked, it assigns false to possibleToSubmit.
  • Finally when clearBets is invoked it assigns true to possibleToSubmit 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 hypothetical Player called “Winner” and a Hand with seven random numbers as arguments
  • It then scans the submitted bets in bets and checks whether or not the current Bet is equal to the winningBet
  • If a winner is found, the Player of this Bet 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 array numbers that contains the seven random numbers of the Hand.
  • It uses a for:to:repeat to generate and store seven random numbers in numbers.
  • Class Bet has an equal 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.