Inherited by Matrioschka, DefaultSimulation, ShopSim, SimA, and SimB.
Simulation supports Trace.
//---------------------------------------------------------------------------- // 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; }
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 Trace * | getTrace () 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.
| |
Process * | getCurrentProcess () |
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 |
|
Process list identifier.
|
|
Construction.
|
|
Simulation status.
|
|
Stop simulation. This function stops the simulation. |
|
Get currently executed process.
|
|
Get list of processes in state CREATED.
|
|
Get list of processes in state RUNNABLE.
|
|
Get list of processes in state IDLE.
|
|
Get list of processes in state TERMINATED.
|
|
Get current simulation time.
|
|
Get pointer to Trace.
Implements TraceProducer. |
|
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. |