home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 21 / IOPROG_21.ISO / SOFT / JSL.ZIP / JSL20 / simula / simset / simulation / Process.java < prev    next >
Encoding:
Java Source  |  1998-02-20  |  4.7 KB  |  142 lines

  1.  
  2. package simula.simset.simulation;
  3.  
  4. import simula.SimulaException;
  5. import simula.simset.Link;
  6.  
  7. /**
  8.  * Accordingly to a process-oriented simulation, the <code>Process</code>
  9.  * objects implement the interacting sequential processes that model the
  10.  * behaviour of a system.
  11.  * A user-defines class can be turned into a process by extending this class.
  12.  * At any point in time, a process can be in one, and only one, of the
  13.  * following states:
  14.  * <br><br><code>Active</code> -- The process is at the head of the event list
  15.  * and its actions are being executed. Only one process can be active at any one
  16.  * time.
  17.  * <br><br><code>Suspended</code> -- The process is on the event list, scheduled
  18.  * to become active at a specified time in the future. The event list is ordered
  19.  * according to these 'event times'. The event time of a suspended process may
  20.  * be equal to that of the active process, in which case its actions will be
  21.  * executed before the clock is advanced.
  22.  * <br><br><code>Passive</code> -- The process is not on the event list. Unless
  23.  * another process brings it back on to the list by means of an activation
  24.  * statement, its further actions will not be executed.
  25.  * <br><br><code>Terminated</code> -- The process is not on the event list and
  26.  * has * no further actions to execute.
  27.  * <br><br>A process that is either active or suspended is said to be
  28.  * 'scheduled'.
  29.  * The Process object is currently implemented like a Java Thread.
  30.  *
  31.  * @see Link
  32.  * @see Runnable
  33.  */
  34. public abstract class Process extends Link implements Runnable
  35. {
  36.    /**
  37.     * Active status constant.
  38.     */
  39.    protected static final int ACTIVE = 2;
  40.    /**
  41.     * Suspended status constant.
  42.     */
  43.    protected static final int SUSPENDED = 4;
  44.    /**
  45.     * Passive status constant.
  46.     */
  47.    protected static final int PASSIVE = 8;
  48.    /**
  49.     * Terminated status constant.
  50.     */
  51.    protected static final int TERMINATED = 16;
  52.  
  53.    // Attributes
  54.    /**
  55.     * The status of this process.
  56.     */
  57.    protected int status;
  58.  
  59.    /**
  60.     * The time when the process will be acive.
  61.     */
  62.    protected double evTime;
  63.  
  64.    /**
  65.     * A reference to the <code>Thread</code> associated with this process.
  66.     */
  67.    protected EventThread processThread;
  68.  
  69.    /**
  70.     * A reference to the simulation context.
  71.     */
  72.    protected Simulation sim;
  73.  
  74.  
  75.    // Constructors
  76.    /**
  77.     * Create a new process and insert it in the given simulation context.
  78.     */
  79.    public Process(Simulation _simulation) {
  80.      status = Process.PASSIVE;
  81.      evTime = 0;
  82.  
  83.      sim = _simulation;
  84.  
  85.      if (processThread == null) {
  86.        processThread = new EventThread(this,_simulation);
  87.      }
  88.    }      
  89.    /**
  90.     * If this process is scheduled, returns the time when will become active.
  91.     * An attempt to invoke this method from within a passive process results
  92.     * in an exception being thrown.
  93.     *
  94.     * @exception SimulaException The process is either passive or terminated.
  95.     */
  96.    public double evtime() throws SimulaException {
  97.      if (this.status == Process.PASSIVE) throw new SimulaException(SimulaException.PASSIVE_PROCESS);
  98.      if (this.status == Process.TERMINATED) throw new SimulaException(SimulaException.TERMINATED_PROCESS);
  99.      return evTime;
  100.    }   
  101.    /**
  102.     * Sets the time when the process will be active.
  103.     */
  104.    protected void evtime(double _time) {
  105.      evTime = _time;
  106.    }   
  107.    /**
  108.     * Returns <code>True</code> if the process is passive or terminated,
  109.     * <code>False</code> if it is scheduled.
  110.     * @return <code>True</code> if the process is passive or terminated,
  111.     * <code>False</code> if it is scheduled.
  112.     */
  113.    public boolean idle() { return ((status == Process.PASSIVE) || (status == Process.TERMINATED)); }   
  114.    // Methods
  115.  
  116.    /**
  117.     * If the process is scheduled and is not the last process on the event list,
  118.     * this method returns a reference to the next process on the list (i.e. the
  119.     * one that will become active after this); otherwise returns <code>Null</code>.
  120.     */
  121.    public Process nextev() { return (Process) this.suc(); }   
  122.    // Accessors
  123.    /**
  124.     * Returns the <code>Thread</code> associated with this process.
  125.     */
  126.    protected EventThread processThread() { return processThread; }   
  127.    /**
  128.     * This is the main body of the process.
  129.     */
  130.    public abstract void run();   
  131.    /**
  132.     * Sets the new state of this process.
  133.     */
  134.    protected void setStatus(int newStatus) { status = newStatus; }   
  135.    /**
  136.     * Returns <code>True</code> if the process is terminated, <code>False</code>
  137.     * if it is scheduled or passive.
  138.     * @return <code>True</code> if the process is terminated, <code>False</code>
  139.     * if it is scheduled or passive.
  140.     */
  141.    public boolean terminated() { return (status == Process.TERMINATED); }   
  142. } // end of class Process