jasi.sim.basic
Class AbstractSimKernel

java.lang.Object
  extended by jasi.sim.basic.AbstractSimKernel
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
Simulation

public abstract class AbstractSimKernel
extends java.lang.Object
implements java.lang.Runnable

Basic implementation of a simulation kernel based on the singleton design pattern. This class provides elementary methods to control the execution of a simulation.

A simulation kernel application is created by extension of this class and implementing a run-method like this:

   Time end = ...; 
   Time current = startSimulation(); 
   try { 
        while (isRunning()) { 
                if (current.isGE(end)) { 
                        break; // The requested end time has been reached
                } 
                current = continueSimulation(current, end); 
        } 
   } 
   finally { 
        inishSimulation(); 
   }
 

The simulation thread can be started and finished in the following way:

 sim.start();
 try {
        sim.join();
 } catch (InterruptedException e) {
        System.err.println(e);
 } finally {
        sim.writeArchive();
 }
 


Constructor Summary
protected AbstractSimKernel(SimController c, AbstractSimArchive a)
          Constructor of SimKernel.
 
Method Summary
 AbstractSimArchive archive()
          Get archive of the simulation.
 Calendar calendar()
          Get calendar of the simulation.
protected  Time continueSimulation(Time from, Time to)
          The simulation thread continues its execution.
 SimController controller()
          Get the external controller of this simulation (federation).
 Time currentTime()
          Get current time of the simulation.
 Elements elements()
          Get the element utility class of this simulation.
protected  void finishSimulation()
          The simulation thread finishes.
static AbstractSimKernel getInstance()
          Get the simulation instance.
 AbstractSimKernel init()
          Initialize this simulation instance.
protected static void initialize(AbstractSimKernel kernel)
          Initialize the simulation instance.
 boolean isRunning()
          Ask, if this simulation is running.
 void join()
          Join waiting thread with the simulation thread.
 SimProxy proxy()
          Get the proxy for simulation state change listeners.
 void readArchive(java.lang.String file)
          Read state of this simulation from an archive.
 void start()
          Start the simulation thread.
protected  Time startSimulation()
          The simulation thread is started.
 void stop()
          Stop the simulation thread.
 Tables tables()
          Get the table utility class of this simulation.
 void writeArchive(java.lang.String file)
          Write state of simulation into an archive.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.lang.Runnable
run
 

Constructor Detail

AbstractSimKernel

protected AbstractSimKernel(SimController c,
                            AbstractSimArchive a)
Constructor of SimKernel. It will create a calendar with zero start time.

Parameters:
c - A simulation controller.
a - The simulation archive.
See Also:
instance, Time.ZERO
Method Detail

init

public final AbstractSimKernel init()
Initialize this simulation instance.

Returns:
This instance.

initialize

protected static void initialize(AbstractSimKernel kernel)
Initialize the simulation instance.

Parameters:
kernel - The instance of the kernel.

getInstance

public static AbstractSimKernel getInstance()
Get the simulation instance. An instance of this class must has been assigned previously in extending subclasses (e.g., Simulation):
 sim = new Simulation(...);
 SimKernel.initialize(sim);
 
or
 sim = new Simulation(...).init();
 

Returns:
The instance.

startSimulation

protected final Time startSimulation()
The simulation thread is started. The simulation controller and all listeners will be informed.

Returns:
The current simulation time.
See Also:
SimController.start(jasi.sim.basic.value.Time)

continueSimulation

protected final Time continueSimulation(Time from,
                                        Time to)
The simulation thread continues its execution. It will try to increase the simulation time and pop the next event from the calendar. The following steps will be performed:
  1. Get time of the next event in calendar.
  2. Request from the simulation controller to advance the simulation time to the next event's time.
  3. Check that the granted time is not later than the requested simulation time.
  4. Pop the next event from the calendar and execute it by a simulation model (event listener). If the next event's time is greater than the granted time advance, then only the simulation time will be increased.
The simulation controller and all listeners will be informed. All occurring exceptions will be caught inside this method and the simulation listeners will be informed.

Parameters:
from - The current simulation time.
to - The requested simulation time.
Returns:
The finally reached simulation time.

finishSimulation

protected final void finishSimulation()
The simulation thread finishes. The simulation controller and all listeners will be informed.


readArchive

public final void readArchive(java.lang.String file)
Read state of this simulation from an archive.

Parameters:
file - The archive's file name without extension.

writeArchive

public final void writeArchive(java.lang.String file)
Write state of simulation into an archive.

Parameters:
file - The archive's file name without extension.

isRunning

public final boolean isRunning()
Ask, if this simulation is running.

Returns:
True, when simulation is running.

join

public final void join()
                throws java.lang.InterruptedException
Join waiting thread with the simulation thread.

Throws:
java.lang.InterruptedException - Whenever this thread is interrupted.

start

public final void start()
Start the simulation thread.


stop

public final void stop()
Stop the simulation thread.


currentTime

public final Time currentTime()
Get current time of the simulation. This time can be different to the current time of the simulation's calendar.

Returns:
The current time.

controller

public final SimController controller()
Get the external controller of this simulation (federation).

Returns:
The controller.

proxy

public final SimProxy proxy()
Get the proxy for simulation state change listeners.

Returns:
The proxy.

archive

public final AbstractSimArchive archive()
Get archive of the simulation.

Returns:
The archive

calendar

public final Calendar calendar()
Get calendar of the simulation.

Returns:
The calendar.

elements

public final Elements elements()
Get the element utility class of this simulation.

Returns:
The element utility class.

tables

public final Tables tables()
Get the table utility class of this simulation.

Returns:
The table utility class.