home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-02-20 | 4.7 KB | 142 lines |
-
- package simula.simset.simulation;
-
- import simula.SimulaException;
- import simula.simset.Link;
-
- /**
- * Accordingly to a process-oriented simulation, the <code>Process</code>
- * objects implement the interacting sequential processes that model the
- * behaviour of a system.
- * A user-defines class can be turned into a process by extending this class.
- * At any point in time, a process can be in one, and only one, of the
- * following states:
- * <br><br><code>Active</code> -- The process is at the head of the event list
- * and its actions are being executed. Only one process can be active at any one
- * time.
- * <br><br><code>Suspended</code> -- The process is on the event list, scheduled
- * to become active at a specified time in the future. The event list is ordered
- * according to these 'event times'. The event time of a suspended process may
- * be equal to that of the active process, in which case its actions will be
- * executed before the clock is advanced.
- * <br><br><code>Passive</code> -- The process is not on the event list. Unless
- * another process brings it back on to the list by means of an activation
- * statement, its further actions will not be executed.
- * <br><br><code>Terminated</code> -- The process is not on the event list and
- * has * no further actions to execute.
- * <br><br>A process that is either active or suspended is said to be
- * 'scheduled'.
- * The Process object is currently implemented like a Java Thread.
- *
- * @see Link
- * @see Runnable
- */
- public abstract class Process extends Link implements Runnable
- {
- /**
- * Active status constant.
- */
- protected static final int ACTIVE = 2;
- /**
- * Suspended status constant.
- */
- protected static final int SUSPENDED = 4;
- /**
- * Passive status constant.
- */
- protected static final int PASSIVE = 8;
- /**
- * Terminated status constant.
- */
- protected static final int TERMINATED = 16;
-
- // Attributes
- /**
- * The status of this process.
- */
- protected int status;
-
- /**
- * The time when the process will be acive.
- */
- protected double evTime;
-
- /**
- * A reference to the <code>Thread</code> associated with this process.
- */
- protected EventThread processThread;
-
- /**
- * A reference to the simulation context.
- */
- protected Simulation sim;
-
-
- // Constructors
- /**
- * Create a new process and insert it in the given simulation context.
- */
- public Process(Simulation _simulation) {
- status = Process.PASSIVE;
- evTime = 0;
-
- sim = _simulation;
-
- if (processThread == null) {
- processThread = new EventThread(this,_simulation);
- }
- }
- /**
- * If this process is scheduled, returns the time when will become active.
- * An attempt to invoke this method from within a passive process results
- * in an exception being thrown.
- *
- * @exception SimulaException The process is either passive or terminated.
- */
- public double evtime() throws SimulaException {
- if (this.status == Process.PASSIVE) throw new SimulaException(SimulaException.PASSIVE_PROCESS);
- if (this.status == Process.TERMINATED) throw new SimulaException(SimulaException.TERMINATED_PROCESS);
- return evTime;
- }
- /**
- * Sets the time when the process will be active.
- */
- protected void evtime(double _time) {
- evTime = _time;
- }
- /**
- * Returns <code>True</code> if the process is passive or terminated,
- * <code>False</code> if it is scheduled.
- * @return <code>True</code> if the process is passive or terminated,
- * <code>False</code> if it is scheduled.
- */
- public boolean idle() { return ((status == Process.PASSIVE) || (status == Process.TERMINATED)); }
- // Methods
-
- /**
- * If the process is scheduled and is not the last process on the event list,
- * this method returns a reference to the next process on the list (i.e. the
- * one that will become active after this); otherwise returns <code>Null</code>.
- */
- public Process nextev() { return (Process) this.suc(); }
- // Accessors
- /**
- * Returns the <code>Thread</code> associated with this process.
- */
- protected EventThread processThread() { return processThread; }
- /**
- * This is the main body of the process.
- */
- public abstract void run();
- /**
- * Sets the new state of this process.
- */
- protected void setStatus(int newStatus) { status = newStatus; }
- /**
- * Returns <code>True</code> if the process is terminated, <code>False</code>
- * if it is scheduled or passive.
- * @return <code>True</code> if the process is terminated, <code>False</code>
- * if it is scheduled or passive.
- */
- public boolean terminated() { return (status == Process.TERMINATED); }
- } // end of class Process