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

« back to all changes in this revision

Viewing changes to src/jcsp-demos/doc-ex/jcsp/lang/Regulate.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
  *                                                                        *
 
4
  *  JCSP ("CSP for Java") libraries                                       *
 
5
  *  Copyright (C) 1996-2008 Peter Welch and Paul Austin.                  *
 
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 version.    *
 
11
  *                                                                        *
 
12
  *  This library is distributed in the hope that it will be useful,       *
 
13
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
14
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 
15
  *  Lesser General Public License for more details.                       *
 
16
  *                                                                        *
 
17
  *  You should have received a copy of the GNU Lesser General Public      *
 
18
  *  License along with this library; if not, write to the Free Software   *
 
19
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,  *
 
20
  *  USA.                                                                  *
 
21
  *                                                                        *
 
22
  *  Author contact: P.H.Welch@ukc.ac.uk                                   *
 
23
  *                                                                        *
 
24
  *************************************************************************/
 
25
 
 
26
// package org.jcsp.plugNplay;
 
27
//
 
28
// This class *is* in the above package.  It is reproduced here because
 
29
// this source is quoted in the documentation of org.jcsp.lang.Alternative.
 
30
// The above package declaration is commented out since, otherwise,
 
31
// compiling the *java files in this directory would fail (because of
 
32
// an attempt to duplicate Regulate in org.jcsp.plugNplay).
 
33
 
 
34
//{{{  javadoc
 
35
/**
 
36
 * This process controls the flow of traffic from its in to out channels.
 
37
 * <H2>Description</H2>
 
38
 * <tt>Regulate</tt> produces a constant rate of output flow, regardless of
 
39
 * the rate of its input. At the end of each timeslice defined by the required
 
40
 * output rate, it outputs the last object input during that timeslice.
 
41
 * If nothing has come in during a timeslice, the previous output will be repeated
 
42
 * (note: this will be a null if nothing has ever arrived).
 
43
 * If the input flow is greater than the required output flow, data will be discarded.
 
44
 * <P>
 
45
 * The interval (in msecs) defining the output flow rate is given by a constructor
 
46
 * argument; but this can be changed at any time by sending a new interval (as a <tt>Long</tt>)
 
47
 * down its <tt>reset</tt> channel.
 
48
 * <H2>Description</H2>
 
49
 * See {@link Alternative#STFR here}.
 
50
 *
 
51
 * @see org.jcsp.plugNplay.FixedDelay
 
52
 * @see org.jcsp.plugNplay.Regular
 
53
 *
 
54
 * @author P.H.Welch
 
55
 *
 
56
 */
 
57
//}}}
 
58
 
 
59
import org.jcsp.lang.*;
 
60
 
 
61
public class Regulate implements CSProcess {
 
62
 
 
63
  private final AltingChannelInput in, reset;
 
64
  private final ChannelOutput out;
 
65
  private final long initialInterval;
 
66
 
 
67
  /**
 
68
    * Construct the process.
 
69
    * 
 
70
    * @param in the input channel
 
71
    * @param out the output channel
 
72
    * @param initialInterval the initial interval between outputs (in milliseconds)
 
73
    * @param reset send a <tt>Long</tt> down this to change the interval between outputs (in milliseconds)
 
74
    */
 
75
  public Regulate (final AltingChannelInput in, final AltingChannelInput reset,
 
76
                   final ChannelOutput out, final long initialInterval) {
 
77
    this.in = in;
 
78
    this.reset = reset;
 
79
    this.out = out;
 
80
    this.initialInterval = initialInterval;
 
81
  }
 
82
 
 
83
  /**
 
84
    * The main body of this process.
 
85
    */
 
86
  public void run () {
 
87
 
 
88
    final CSTimer tim = new CSTimer ();
 
89
 
 
90
    final Guard[] guards = {reset, tim, in};              // prioritised order
 
91
    final int RESET = 0;                                  // index into guards
 
92
    final int TIM = 1;                                    // index into guards
 
93
    final int IN = 2;                                     // index into guards
 
94
 
 
95
    final Alternative alt = new Alternative (guards);
 
96
 
 
97
    Object x = null;                                      // holding object
 
98
 
 
99
    long interval = initialInterval;
 
100
 
 
101
    long timeout = tim.read () + interval;
 
102
    tim.setAlarm (timeout);
 
103
 
 
104
    while (true) {
 
105
      switch (alt.priSelect ()) {
 
106
        case RESET:
 
107
          interval = ((Long) reset.read ()).longValue ();
 
108
          timeout = tim.read ();                          // fall through
 
109
        case TIM:
 
110
          out.write (x);
 
111
          timeout += interval;
 
112
          tim.setAlarm (timeout);
 
113
        break;
 
114
        case IN:
 
115
          x = in.read ();
 
116
        break;
 
117
      }
 
118
    }
 
119
 
 
120
  }
 
121
 
 
122
}