~ubuntu-branches/ubuntu/precise/jcsp/precise

« back to all changes in this revision

Viewing changes to src/jcsp-demos/wotNoChickens/starve/StarveCanteen.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            |_________|    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
 * This Canteen is an active object -- a pure SERVER process for its `service'
 
51
 * and `supply' CALL channels.  The service channel is any-1 since, hopefully,
 
52
 * there will be many customers.  The supply channel may be 1-1 (one chef) or
 
53
 * any-1 (many chefs).
 
54
 *
 
55
 * @author P.H. Welch
 
56
 *
 
57
 */
 
58
 
 
59
class StarveCanteen {
 
60
 
 
61
  // call interfaces and channels
 
62
 
 
63
  public static interface Service {
 
64
    public int takeChicken (String philId);
 
65
  }
 
66
 
 
67
  public static interface Supply {
 
68
    public int freshChickens (String chefId, int value);
 
69
  }
 
70
 
 
71
  // fields and constructors
 
72
 
 
73
  private final Service service;                                // called by the philosophers
 
74
  private final Supply supply;                                  // called by the chefs
 
75
  private final int serviceTime;                // how long a philosopher spends in the canteen
 
76
  private final int supplyTime;                 // how long the chef spends in the canteen
 
77
  private final int maxChickens;                // maximum number of chickens in the canteen
 
78
  private final CSTimer tim;                                    // for delays
 
79
 
 
80
  public StarveCanteen (int serviceTime, int supplyTime, int maxChickens) {
 
81
    this.service = new Service () {
 
82
        public int takeChicken (String philId) {
 
83
                return doTakeChicken (philId);
 
84
        }
 
85
    };
 
86
    this.supply = new Supply () {
 
87
        public int freshChickens (String chefId, int value) {
 
88
                return doFreshChickens (chefId, value);
 
89
        }
 
90
    };
 
91
    this.serviceTime = serviceTime;
 
92
    this.supplyTime = supplyTime;
 
93
    this.maxChickens = maxChickens;
 
94
    this.tim = new CSTimer ();
 
95
  }
 
96
 
 
97
  public Service getService () {
 
98
        return service;
 
99
  }
 
100
 
 
101
  public Supply getSupply () {
 
102
        return supply;
 
103
  }
 
104
 
 
105
  private int nChickens = 0;
 
106
  private int nSupplied = 0;
 
107
 
 
108
  private synchronized int doTakeChicken (String philId) {
 
109
    System.out.println ("   Canteen -> " + philId + " : one chicken ordered ... "
 
110
                                         + nChickens + " left");
 
111
        if (nChickens > 0) {
 
112
        tim.sleep (serviceTime);         // this takes serviceTime to deliver
 
113
        nChickens--;
 
114
        nSupplied++;
 
115
        System.out.println ("   Canteen -> " + philId + " : one chicken coming down ... "
 
116
                                             + nChickens + " left [" + nSupplied + " supplied]");
 
117
            return 1;
 
118
        } else {
 
119
                return 0;
 
120
        }
 
121
  }
 
122
 
 
123
  private synchronized int doFreshChickens (String chefId, int value) {
 
124
    System.out.println ("   Canteen <- " + chefId
 
125
                                         + " : ouch ... make room ... this dish is very hot ...");
 
126
    tim.sleep (supplyTime);          // this takes supplyTime to put down
 
127
    nChickens += value;
 
128
    int sendBack = nChickens - maxChickens;
 
129
    if (sendBack > 0) {
 
130
      nChickens = maxChickens;
 
131
      System.out.println ("   Canteen <- " + chefId
 
132
                                           + " : full up ... sending back " + sendBack);
 
133
    } else {
 
134
      sendBack = 0;
 
135
    }
 
136
    System.out.println ("   Canteen <- " + chefId + " : more chickens ... "
 
137
                                         + nChickens + " now available");
 
138
    return sendBack;
 
139
  }
 
140
 
 
141
}