Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Data Structures   File List   Namespace Members   Data Fields   Globals   Related Pages   Examples  

Simulation Class Reference
[Base]

Inherits ExecutionList, CoroutineContext, DistContext, LabelScope, Trace, TraceProducer, StatisticManager, and Observable< SimulationObserver >.

Inherited by Matrioschka, DefaultSimulation, ShopSim, SimA, and SimB.


Detailed Description

Simulation is the base class for all user simulations.

Author:
Ralf Gerstenberger

Note:
Simulation supports Observation.

Simulation supports Trace.

In a user simulation the method initSimulation() must be implemented to set-up the simulation. run(), step() or runUntil() can be used to compute the simulation. run() returns after the simulation has finished. step() returns after each process switch in the simulation. runUntil() returns after simulation time exceeds the given time or the simulation is finished. Simulation also manages lists of all Process objects in the states CREATED, RUNNABLE, IDLE and TERMINATED.

Example:
//----------------------------------------------------------------------------
//      Copyright (C) 2002, 2003 Humboldt-Universitaet zu Berlin
//
//      This library is free software; you can redistribute it and/or
//      modify it under the terms of the GNU Lesser General Public
//      License as published by the Free Software Foundation; either
//      version 2.1 of the License, or (at your option) any later version.
//
//      This library is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//      Lesser General Public License for more details.
//
//      You should have received a copy of the GNU Lesser General Public
//      License along with this library; if not, write to the Free Software
//      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//----------------------------------------------------------------------------
#include <odemx/base/Process.h>

#include <iostream>

using namespace std;
using namespace odemx;

//
// In this example we demonstrate the possibility to cascade simulations in a recursive 
// simulation called Matrioschka. Like the Russian puppet inside a puppet inside a puppet 
// inside ...
// 
// A user defined simulation is a class derived from Simulation. Every process or model
// component requires a pointer to its simulation. The user defined simulation can provide
// its processes with additional data. Furthermore, a user defined simulation must implement
// the 'initSimulation()' function to start its processes and to do specific initialisations.
//
class Matrioschka : public Simulation {
        //
        // The simulation contains only the process Proc.
        // This process expects a Matrioschka-simulation as
        // environment. Defining the process as an inside 
        // class of Matrioschka is of course not required.
        //
        class Proc : public Process {
        public:
                Proc(Matrioschka* sim, ProcessObserver* o = 0)
                        : Process(sim, "Proc", o) {}

        protected:
                //
                // In its 'main()' function the process gets a pointer to
                // its simulation. After a cast (the dynanmic_cast could 
                // be spared) the process can access the data provided by 
                // its simulation.
                //
                virtual int main() {
                        int i;
                        Matrioschka* sim=dynamic_cast<Matrioschka*>(getSimulation());
                        int level=sim->getLevel();

                        for (i=level; i>0; --i)
                                cout << ' ';

                        if (level%2>0)
                                cout << "Di " << level << endl;
                        else
                                cout << "Da " << level << endl;

                        //
                        // After some outputs the process realises the recursion.
                        // 
                        if (level>0) {
                                Matrioschka m(sim->getLevel()-1);
                                m.run();
                        }

                        for (i=level; i>0; --i)
                                cout << ' ';

                        if (level%2>0)
                                cout << "Di " << level << endl;
                        else
                                cout << "Da " << level << endl;

                        return 0;
                }
        };

        Proc* p;
        unsigned int mLevel;

public:
        //
        // Because of the recursion Matrioschka needs the additional
// parameter 'level'.
        //
        Matrioschka(unsigned int level, SimulationObserver* o=0)
                : Simulation("Matrioschka", o), mLevel(level) {}

        ~Matrioschka() {
                delete p;
        }
        
        //
        // 'level' is the recursion parameter of the Matrioschka simulation.
        //
        unsigned int getLevel() {return mLevel;}

protected:
        //
        // If you are defining your own simulation class you have to provide 'initSimulation'
        // to initialise the simulation. This function is called when the simulation is started
        // for the first time.
        //
        virtual void initSimulation() {
                p = new Proc(this);
                p->activate();
        }       
};

//
// The 'main' function looks a bit deserted in this example. Almost everything is done in
// the simulation. The advantage is, you can 'reuse' the simulation as it is.
//
int main(int argc, char* argv[]) {
        Matrioschka m(10);
        m.run();

        return 0;
}

Since:
1.0


Public Types

enum  List { CREATED, RUNNABLE, IDLE, TERMINATED }
 Process list identifier. More...


Public Member Functions

 Simulation (Label l="Simulation", SimulationObserver *o=0)
 Construction.

 ~Simulation ()
 Destruction.

virtual SimTime getTime () const
 Get current simulation time.

virtual TracegetTrace () const
 Get pointer to Trace.

Simulation computing
The computation of a simulation is started with one of the following functions. A simulation is finished when there is no process left to be executed or the function exitSimulation() was called.

void run ()
 Run simulation until it's finished.

void step ()
 Execute one process and return.

void runUntil (SimTime t)
 Run until the current time is greater than t.

bool isFinished ()
 Simulation status.

virtual void exitSimulation ()
 Stop simulation.

Process management
Simulation remembers all process objects. The following functions can be used to get the objects in different process states.

See also:
Process::ProcessState


ProcessgetCurrentProcess ()
 Get currently executed process.

std::list< Process * > & getCreatedProcesses ()
 Get list of processes in state CREATED.

std::list< Process * > & getRunnableProcesses ()
 Get list of processes in state RUNNABLE.

std::list< Process * > & getIdleProcesses ()
 Get list of processes in state IDLE.

std::list< Process * > & getTerminatedProcesses ()
 Get list of processes in state TERMINATED.


Static Public Attributes

Trace MarkTypes
These MarkTypes are used to trace Simulation events. A TraceConsumer can use these constants to identify trace events send by Simulation.

const MarkTypeId baseMarkId = 1000
const MarkType markInit = MarkType("init", baseMarkId+1, typeid(Simulation))
const MarkType markExitCall = MarkType("exit call", baseMarkId+2, typeid(Simulation))
const MarkType markRun = MarkType("run", baseMarkId+3, typeid(Simulation))
const MarkType markStep = MarkType("step", baseMarkId+4, typeid(Simulation))
const MarkType markRunUntil = MarkType("run until", baseMarkId+5, typeid(Simulation))
const MarkType markExecProc = MarkType("execute process", baseMarkId+6, typeid(Simulation))
const MarkType markCurrProc = MarkType("current process", baseMarkId+7, typeid(Simulation))
const MarkType markTime = MarkType("time", baseMarkId+8, typeid(Simulation))

Protected Member Functions

virtual void initSimulation ()=0
 Initialisation of a simulation.


Friends

class Process


Member Enumeration Documentation

enum List
 

Process list identifier.

Enumeration values:
CREATED  Created processes.
RUNNABLE  Runnable processes.
IDLE  Idle processes.
TERMINATED  Terminated processes.


Constructor & Destructor Documentation

Simulation Label  l = "Simulation",
SimulationObserver o = 0
 

Construction.

Parameters:
l label of this object
o initial observer


Member Function Documentation

bool isFinished  ) 
 

Simulation status.

Returns:
true if simulation is finished
This function returns true if the simulation is finished.
Examples:
Parallel.cpp.

void exitSimulation  )  [virtual]
 

Stop simulation.

This function stops the simulation.

Process * getCurrentProcess  ) 
 

Get currently executed process.

Returns:
pointer to executed process object
This function returns a pointer to the momentarily executed process (the process in state CURRENT).

std::list< Process * > & getCreatedProcesses  ) 
 

Get list of processes in state CREATED.

Returns:
list of processes in state CREATED
This function returns a list of all processes in state CREATED.

std::list< Process * > & getRunnableProcesses  ) 
 

Get list of processes in state RUNNABLE.

Returns:
list of processes in state RUNNABLE
This function returns a list of all processes in state RUNNABLE.

std::list< Process * > & getIdleProcesses  ) 
 

Get list of processes in state IDLE.

Returns:
list of processes in state IDLE
This function returns a list of all processes in state IDLE.

std::list< Process * > & getTerminatedProcesses  ) 
 

Get list of processes in state TERMINATED.

Returns:
list of processes in state TERMINATED
This function returns a list of all processes in state TERMINATED.

virtual SimTime getTime  )  const [inline, virtual]
 

Get current simulation time.

Returns:
time now
This function returns the current simulation time. The current time is determined by the current or last executed process. If a process is executed its execution time becomes the new simulation time.

virtual Trace* getTrace  )  const [inline, virtual]
 

Get pointer to Trace.

Returns:
pointer to trace manager
This function returns the pointer to the trace manager.

Implements TraceProducer.

virtual void initSimulation  )  [protected, pure virtual]
 

Initialisation of a simulation.

Implement this method to initialise a simulation. One can create and activate the first processes of a simulation in this function. Inside this function the scheduling operations does not cause an immediate execution of a process. The first process is executed after this function is finished.

Implemented in DefaultSimulation.


Generated on Mon Aug 11 10:36:07 2003 for ODEMx by doxygen1.3