View Javadoc

    1   /*
    2    * @(#) Process.java Jan 19, 2004
    3    *
    4    * Copyright (c) 2003 Delft University of Technology Jaffalaan 5, 2628 BX Delft,
    5    * the Netherlands All rights reserved.
    6    *
    7    * This software is proprietary information of Delft University of Technology
    8    * The code is published under the General Public License
    9    */
    10  package nl.tudelft.simulation.dsol.formalisms.process;
    11
    12  import java.rmi.RemoteException;
    13  import java.util.Stack;
    14
    15  import nl.tudelft.simulation.dsol.SimRuntimeException;
    16  import nl.tudelft.simulation.dsol.formalisms.devs.SimEvent;
    17  import nl.tudelft.simulation.dsol.interpreter.Frame;
    18  import nl.tudelft.simulation.dsol.interpreter.Interpreter;
    19  import nl.tudelft.simulation.dsol.interpreter.InterpreterException;
    20  import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
    21  import nl.tudelft.simulation.language.reflection.ClassUtil;
    22  import nl.tudelft.simulation.logger.Logger;
    23
    24  /***
    25   * A Process <br>
    26   * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
    27   * University of Technology </a>, the Netherlands. <br>
    28   * See for project information <a
    29   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
    30   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
    31   * License (GPL) </a>, no warranty <br>
    32   *
    33   * @version 1.0 Jan 19, 2004 <br>
    34   * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
    35   *         Jacobs </a>
    36   */
    37  public abstract class Process
    38  {
    39  	/*** the processStack of this process */
    40  	private Stack frameStack = new Stack();
    41
    42  	/*** the simulator to schedule on */
    43  	protected DEVSSimulatorInterface simulator = null;
    44
    45  	/***
    46  	 * constructs a new Process
    47  	 *
    48  	 * @param simulator the simulator to schedule on
    49  	 */
    50  	public Process(final DEVSSimulatorInterface simulator)
    51  	{
    52  		super();
    53  		this.simulator = simulator;
    54  		try
    55  		{
    56  			this.frameStack.push(Interpreter.createFrame(this, ClassUtil
    57  					.resolveMethod(this, "process", null), null));
    58  			double simulatorTime = this.simulator.getSimulatorTime();
    59  			if (Double.isNaN(simulatorTime))
    60  			{
    61  				simulatorTime = 0.0;
    62  			}
    63  			SimEvent simEvent = new SimEvent(simulatorTime, this, this,
    64  					"resume", null);
    65  			this.simulator.scheduleEvent(simEvent);
    66  		} catch (Exception exception)
    67  		{
    68  			exception.printStackTrace();
    69  			Logger.severe(this, "<init>", exception);
    70  		}
    71  	}
    72
    73  	/***
    74  	 * processes the process.
    75  	 *
    76  	 * @throws SimRuntimeException on simulation failure
    77  	 * @throws RemoteException on remote network failure
    78  	 */
    79  	public abstract void process() throws SimRuntimeException, RemoteException;
    80
    81  	/***
    82  	 * holds the process for a duration
    83  	 *
    84  	 * @param duration the duration
    85  	 * @throws SimRuntimeException on negative duration
    86  	 * @throws RemoteException on network failure
    87  	 */
    88  	protected void hold(final double duration) throws SimRuntimeException,
    89  			RemoteException
    90  	{
    91  		//First we schedule the resume operation
    92  		this.simulator.scheduleEvent(new SimEvent(this.simulator
    93  				.getSimulatorTime()
    94  				+ duration, this, this, "resume", null));
    95
    96  		//Now we suspend
    97  		this.suspend();
    98  	}
    99
    100 	/***
    101 	 * resumes this process
    102 	 */
    103 	public synchronized void resume()
    104 	{
    105 		if (this.frameStack.isEmpty())
    106 		{
    107 			return;
    108 		}
    109 		try
    110 		{
    111 			((Frame) this.frameStack.peek()).setPaused(false);
    112 			Object result = Interpreter.interpret(this.frameStack);
    113 			if (result instanceof Stack)
    114 			{
    115 				this.frameStack = (Stack) result;
    116 			}
    117 		} catch (InterpreterException exception)
    118 		{
    119 			Logger.warning(this, "resume", exception);
    120 		}
    121 	}
    122
    123 	/***
    124 	 * suspends the process. This method throughs an exception whenever it is
    125 	 * not interpreted but executed directly on the JVM.
    126 	 *
    127 	 * @throws SimRuntimeException on direct invokation.
    128 	 */
    129 	protected void suspend() throws SimRuntimeException
    130 	{
    131 		throw new SimRuntimeException("suspend should be interpreted."
    132 				+ " One may not invoke this method directly");
    133 	}
    134 }