simbad.sim
Class Simulator

java.lang.Object
  extended by simbad.sim.Simulator

public class Simulator
extends java.lang.Object

The Simulator class. It manages the list of agents and performs the simulation steps. For each agent a simulation step is is as follow:

  • update sensors
  • call agent performBehavior
  • update position
  • In normal operation the steps are triggered by a timer event.
    The Simulator class also provides a Background Mode with limited rendering. This mode is mainly useful for batch simulation ( ie genetic algorithms). See Also Simbatch class. Cooperates with: World, PhysicalEngine


    Nested Class Summary
    private  class Simulator.SimulatorThread
               
     
    Field Summary
    private  java.util.ArrayList agents
              The list of all agents.
    private  javax.swing.JComponent applicationComponent
              Keeps track of application's main component -- can be null
    private  long counter
              Count simulation steps.
    (package private)  FrameMeter fps
               
    private  int framesPerSecond
              number of frames per virtual seconds - 20 is a good value
    private  Lock lock
              Used for mutual exclusion.
    private  java.util.ArrayList objects
              The list of all objects in the world.
    (package private)  PhysicalEngine physicalEngine
              Handles all algorithms and resources related to agent interactions.
    private  Simulator.SimulatorThread simulatorThread
              Thread for the background mode
    private  java.util.Timer timer
               
    private  boolean usePhysics
              Control the usage of physical law in the simulation
    private  float virtualTimeFactor
               
    (package private)  World world
               
     
    Constructor Summary
    Simulator(javax.swing.JComponent applicationComponent, World world, EnvironmentDescription ed)
              Constructs the simulator object
     
    Method Summary
    private  void addMobileAndStaticObjects(EnvironmentDescription ed)
              Add all agents and objects.
    private  void createAgentsUI()
              Creates the UI that may be associated to each agent.
     void dispose()
              Dispose all resources.
     java.util.ArrayList getAgentList()
               
    protected  int getFramesPerSecond()
               
    protected  boolean getUsePhysics()
              Gets use physics indicator.
     float getVirtualTimeFactor()
               
     void initBehaviors()
              initialize the behavior of all agents.
    private  void initialize(EnvironmentDescription ed)
              Initialize the simulator - only called once.
     void lock()
              Obtain simulator critical resources.
     void performSimulationStep()
              Perform a single step of simulation
     void resetSimulation()
              Reset the simulation.
     void restartSimulation()
              Simulator control.
     void setApplicationComponent(javax.swing.JComponent component)
               
    protected  void setFramesPerSecond(int fps)
               
    protected  void setUsePhysics(boolean usePhysics)
              Sets use physics indicator.
     void setVirtualTimeFactor(float fact)
              Set the time factor.
     void simulateOneStep()
              The main simulator method.
     void startBackgroundMode()
              Starts special background mode
     void startSimulation()
              Starts the simulator loop.
     void stopBackgroundMode()
              Stops special background mode
     void stopSimulation()
              Stop (or pause) the simulator loop.
     void unlock()
              Release simulator critical resources.
     
    Methods inherited from class java.lang.Object
    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
     

    Field Detail

    lock

    private Lock lock
    Used for mutual exclusion. all methods accessing agents shoud use this lock.


    applicationComponent

    private javax.swing.JComponent applicationComponent
    Keeps track of application's main component -- can be null


    world

    World world

    fps

    FrameMeter fps

    timer

    private java.util.Timer timer

    objects

    private java.util.ArrayList objects
    The list of all objects in the world. (static objects and agents).


    agents

    private java.util.ArrayList agents
    The list of all agents.


    framesPerSecond

    private int framesPerSecond
    number of frames per virtual seconds - 20 is a good value


    virtualTimeFactor

    private float virtualTimeFactor

    simulatorThread

    private Simulator.SimulatorThread simulatorThread
    Thread for the background mode


    usePhysics

    private boolean usePhysics
    Control the usage of physical law in the simulation


    physicalEngine

    PhysicalEngine physicalEngine
    Handles all algorithms and resources related to agent interactions.


    counter

    private long counter
    Count simulation steps.

    Constructor Detail

    Simulator

    public Simulator(javax.swing.JComponent applicationComponent,
                     World world,
                     EnvironmentDescription ed)
    Constructs the simulator object

    Parameters:
    applicationComponent - - A reference to the main Application container.
    world - - The 3d world object.
    ed - - the Environment description.
    Method Detail

    initialize

    private void initialize(EnvironmentDescription ed)
    Initialize the simulator - only called once.


    dispose

    public void dispose()
    Dispose all resources. only called once.


    addMobileAndStaticObjects

    private void addMobileAndStaticObjects(EnvironmentDescription ed)
    Add all agents and objects. Only called once.


    createAgentsUI

    private void createAgentsUI()
    Creates the UI that may be associated to each agent. If the agent has set a Panel with setUIPanel a window is created containing the panel. Only called once.


    simulateOneStep

    public void simulateOneStep()
    The main simulator method. It is called cyclicaly or step by step. (see startSimulation).


    initBehaviors

    public void initBehaviors()
    initialize the behavior of all agents.


    startSimulation

    public void startSimulation()
    Starts the simulator loop.


    stopSimulation

    public void stopSimulation()
    Stop (or pause) the simulator loop.


    restartSimulation

    public void restartSimulation()
    Simulator control.


    resetSimulation

    public void resetSimulation()
    Reset the simulation. Resets any living agents.


    performSimulationStep

    public void performSimulationStep()
    Perform a single step of simulation


    getAgentList

    public java.util.ArrayList getAgentList()

    setUsePhysics

    protected void setUsePhysics(boolean usePhysics)
    Sets use physics indicator.


    getUsePhysics

    protected boolean getUsePhysics()
    Gets use physics indicator.


    setFramesPerSecond

    protected void setFramesPerSecond(int fps)

    getFramesPerSecond

    protected int getFramesPerSecond()

    setVirtualTimeFactor

    public void setVirtualTimeFactor(float fact)
    Set the time factor. Used to increase or decrease the simulation rate.

    Parameters:
    factor - : typical value 1.0 (default) , 2.0 or 0.5

    getVirtualTimeFactor

    public float getVirtualTimeFactor()

    setApplicationComponent

    public void setApplicationComponent(javax.swing.JComponent component)

    lock

    public void lock()
    Obtain simulator critical resources.


    unlock

    public void unlock()
    Release simulator critical resources.


    startBackgroundMode

    public void startBackgroundMode()
    Starts special background mode


    stopBackgroundMode

    public void stopBackgroundMode()
    Stops special background mode