~ubuntu-branches/ubuntu/oneiric/jcsp/oneiric

« back to all changes in this revision

Viewing changes to src/jcsp-demos/wotNoChickens/channel/Canteen.java

  • Committer: Bazaar Package Importer
  • Author(s): Miguel Landaeta
  • Date: 2010-06-20 18:12:26 UTC
  • Revision ID: james.westby@ubuntu.com-20100620181226-8yg8d9rjjjiuy7oz
Tags: upstream-1.1-rc4
ImportĀ upstreamĀ versionĀ 1.1-rc4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
    //////////////////////////////////////////////////////////////////////
 
2
    //                                                                  //
 
3
    //  JCSP ("CSP for Java") Libraries                                 //
 
4
    //  Copyright (C) 1996-2008 Peter Welch and Paul Austin.            //
 
5
    //                2001-2004 Quickstone Technologies Limited.        //
 
6
    //                                                                  //
 
7
    //  This library is free software; you can redistribute it and/or   //
 
8
    //  modify it under the terms of the GNU Lesser General Public      //
 
9
    //  License as published by the Free Software Foundation; either    //
 
10
    //  version 2.1 of the License, or (at your option) any later       //
 
11
    //  version.                                                        //
 
12
    //                                                                  //
 
13
    //  This library is distributed in the hope that it will be         //
 
14
    //  useful, but WITHOUT ANY WARRANTY; without even the implied      //
 
15
    //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR         //
 
16
    //  PURPOSE. See the GNU Lesser General Public License for more     //
 
17
    //  details.                                                        //
 
18
    //                                                                  //
 
19
    //  You should have received a copy of the GNU Lesser General       //
 
20
    //  Public License along with this library; if not, write to the    //
 
21
    //  Free Software Foundation, Inc., 59 Temple Place, Suite 330,     //
 
22
    //  Boston, MA 02111-1307, USA.                                     //
 
23
    //                                                                  //
 
24
    //  Author contact: P.H.Welch@kent.ac.uk                             //
 
25
    //                                                                  //
 
26
    //                                                                  //
 
27
    //////////////////////////////////////////////////////////////////////
 
28
 
 
29
 
 
30
//|
 
31
//| This program shows a use of the ALT mechanism.  It is a re-implementation
 
32
//| of the Starving Philosophers example but now has the Canteen programmed
 
33
//| properly as an active process.  Here is the College:
 
34
//|
 
35
//|
 
36
//|      0   1   2   3   4
 
37
//|      :)  :)  :)  :)  :)      ___________             ________
 
38
//|      |   |   |   |   |       |         |             |      |
 
39
//|    ---------------------<->--| Canteen |------<------| Cook |
 
40
//|       service/deliver        |_________|    supply   |______|
 
41
//|
 
42
//|
 
43
//|
 
44
//| This time, although Philsopher 0 is just as greedy, no one starves.
 
45
//|
 
46
 
 
47
import org.jcsp.lang.*;
 
48
 
 
49
/**
 
50
 * @author P.H. Welch
 
51
 */
 
52
class Canteen implements CSProcess {
 
53
 
 
54
  //The Canteen is an active object -- a pure SERVER process for its `supply'
 
55
  //and `service'/`deliver' Channels, giving priority to the former.
 
56
  //
 
57
  //Philosphers eat chickens.  They queue up at the Canteen on its `service'
 
58
  //Channel.  They only get served when chickens are available -- otherwise,
 
59
  //they just have to wait.  Once they have got `service', they are dispensed
 
60
  //a chicken down the `deliver' Channel.
 
61
  //
 
62
  //The Chef cooks chickens.  When a batch ready is ready, he/she queues up at
 
63
  //the Canteen on its `supply' Channel.  Setting down the batch takes around
 
64
  //3 seconds and the Chef is made to hang about this has happened.
 
65
 
 
66
  private final AltingChannelInputInt service;    // shared from all Philosphers (any-1)
 
67
  private final ChannelOutputInt deliver;         // shared to all Philosphers (but only used 1-1)
 
68
  private final AltingChannelInputInt supply;     // from the Chef (1-1)
 
69
 
 
70
  public Canteen (AltingChannelInputInt service, ChannelOutputInt deliver,
 
71
                  AltingChannelInputInt supply) {
 
72
    this.service = service;
 
73
    this.deliver = deliver;
 
74
    this.supply = supply;
 
75
  }
 
76
 
 
77
  public void run () {
 
78
 
 
79
    final Alternative alt = new Alternative (new Guard[] {supply, service});
 
80
    final boolean[] precondition = {true, false};
 
81
    final int SUPPLY = 0;
 
82
    final int SERVICE = 1;
 
83
 
 
84
    final CSTimer tim = new CSTimer ();
 
85
 
 
86
    int nChickens = 0;
 
87
 
 
88
    System.out.println ("            Canteen : starting ... ");
 
89
    while (true) {
 
90
      precondition[SERVICE] = (nChickens > 0);
 
91
      switch (alt.fairSelect (precondition)) {
 
92
        case SUPPLY:
 
93
          int value = supply.read ();        // new batch of chickens from the Chef
 
94
          System.out.println ("            Canteen : ouch ... make room ... this dish is very hot ... ");
 
95
          tim.after (tim.read () + 3000);   // this takes 3 seconds to put down
 
96
          nChickens += value;
 
97
          System.out.println ("            Canteen : more chickens ... " +
 
98
                               nChickens + " now available ... ");
 
99
          supply.read ();                   // let the Chef get back to cooking
 
100
        break;
 
101
        case SERVICE:
 
102
          service.read ();                  // Philosopher wants a chicken
 
103
          System.out.println ("      Canteen : one chicken coming down ... " +
 
104
                               (nChickens - 1) + " left ... ");
 
105
          deliver.write (1);             // serve one chicken
 
106
          nChickens--;
 
107
        break;
 
108
       }
 
109
    }
 
110
  }
 
111
 
 
112
}