Microsimulation API

ssim.h
Go to the documentation of this file.
1 // -*-C++-*-
2 //
3 // This file is part of SSim, a simple discrete-event simulator.
4 // See http://www.inf.usi.ch/carzaniga/ssim/
5 //
6 // Copyright (C) 1998-2004 University of Colorado
7 // Copyright (C) 2012 Antonio Carzaniga
8 //
9 // Authors: Antonio Carzaniga <firstname.lastname@usi.ch>
10 // See AUTHORS for full details.
11 //
12 // SSim is free software: you can redistribute it and/or modify it under
13 // the terms of the GNU General Public License as published by the Free
14 // Software Foundation, either version 3 of the License, or (at your
15 // option) any later version.
16 //
17 // SSim is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU General Public License
23 // along with SSim. If not, see <http://www.gnu.org/licenses/>.
24 //
25 #ifndef _ssim_h
26 #define _ssim_h
27
28 #include <functional>
29 #include <string>
30
51 namespace ssim {
52
55 typedef int ProcessId;
56
59 const ProcessId NULL_PROCESSID = -1;
60
75 typedef double Time;
76
79 const Time INIT_TIME = 0;
80
106 class Event {
107 public:
108 Event(): refcount(0) {};
109 virtual ~Event() {};
110 virtual std::string str() const { return "(event)"; };
111
112 private:
113 mutable unsigned refcount;
114 friend class SimImpl; // this is an opaque implementation class
115 friend class Sim; // these need to be friends to manage refcount
116 };
117
118 typedef std::function<bool (const Event *)> EventPredicate;
119
125 class Process {
126 public:
127 virtual ~Process() {};
128
138 virtual void initialize(void) {};
139
194 virtual void process_event(const Event * msg) {};
195
203 virtual void stop(void) {};
204 };
205
212 class ProcessWithPId : public Process {
213 public:
225 ProcessId activate() throw();
226
233 ProcessId pid() const throw();
234
235 ProcessWithPId() throw();
236
237 private:
238 ProcessId process_id;
239 };
240
251 class SimErrorHandler {
252 public:
253 virtual ~SimErrorHandler() {}
254
263 virtual void clear() throw() {}
264
282 virtual void handle_busy(ProcessId p, const Event * e) throw() {}
283
301 virtual void handle_terminated(ProcessId p, const Event * e) throw() {}
302 };
303
326 class Sim {
327 public:
340 static ProcessId create_process(Process *) throw();
341
343 static int stop_process(ProcessId) throw();
345 static void stop_process() throw();
346
357 static void clear() throw();
358
373 static void self_signal_event(const Event * e) throw();
374
391 static void self_signal_event(const Event * e, Time delay) throw();
392
412 static void signal_event(ProcessId p, const Event * e) throw();
413
435 static void signal_event(ProcessId p, const Event * e, Time d) throw();
436
488 static void advance_delay(Time) throw();
489
510 static ProcessId this_process() throw();
511
529 static Time clock() throw();
530
532 static void run_simulation();
534 static void stop_simulation() throw();
535
548 static void set_stop_time(Time t = INIT_TIME) throw();
549
557 static void set_error_handler(SimErrorHandler *) throw();
558 static void ignore_event(EventPredicate pred) throw();
559 };
560 void Rprint_actions();
561
562 } // end namespace ssim
563
564 #endif /* _ssim_h */
565
ssim::INIT_TIME
const Time INIT_TIME
beginning of time
Definition: ssim.h:79
ssim::Process::initialize
virtual void initialize(void)
action executed when the process is initialized.
Definition: ssim.h:138
ssim::Sim::this_process
static ProcessId this_process()
returns the current process
Definition: ssim.cc:260
ssim::Sim::signal_event
static void signal_event(ProcessId p, const Event *e)
signal an event to the given process immediately
Definition: ssim.cc:276
ssim::EventPredicate
std::function< bool(const Event *)> EventPredicate
Definition: ssim.h:118
ssim::Sim::create_process
static ProcessId create_process(Process *)
creates a new process
Definition: ssim.cc:108
ssim::Sim::set_error_handler
static void set_error_handler(SimErrorHandler *)
registers a handler for simulation errors.
Definition: ssim.cc:284
ssim::ProcessWithPId::pid
ProcessId pid() const
process id of this process.
Definition: ssim.cc:298
ssim::Sim::stop_simulation
static void stop_simulation()
stops execution of the simulation
Definition: ssim.cc:251
ssim::Process
Virtual class (interface) representing processes running within the simulator.
Definition: ssim.h:125
ssim::SimImpl
Definition: ssim.cc:90
ssim::SimErrorHandler::~SimErrorHandler
virtual ~SimErrorHandler()
Definition: ssim.h:253
ssim::ProcessId
int ProcessId
process identifier type
Definition: ssim.h:55
ssim::Process::stop
virtual void stop(void)
executed when the process is explicitly stopped.
Definition: ssim.h:203
ssim::SimErrorHandler
an error handler for simulation errors.
Definition: ssim.h:251
ssim::Sim::advance_delay
static void advance_delay(Time)
advance the execution time of the current process.
Definition: ssim.cc:255
ssim::Sim::ignore_event
static void ignore_event(EventPredicate pred)
Definition: ssim.cc:131
ssim::Event
basic event in the simulation.
Definition: ssim.h:106
ssim::Sim::set_stop_time
static void set_stop_time(Time t=INIT_TIME)
stops the execution of the simulation at the given time
Definition: ssim.cc:237
ssim::SimErrorHandler::clear
virtual void clear()
handles a clear operation.
Definition: ssim.h:263
ssim::Event::Event
Event()
Definition: ssim.h:108
ssim::ProcessWithPId
utility Process class providing a utility interface with the simulator.
Definition: ssim.h:212
ssim::Event::~Event
virtual ~Event()
Definition: ssim.h:109
ssim::ProcessWithPId::activate
ProcessId activate()
activates this process within the simulator.
Definition: ssim.cc:288
ssim::Event::refcount
unsigned refcount
Definition: ssim.h:110
ssim::SimErrorHandler::handle_terminated
virtual void handle_terminated(ProcessId p, const Event *e)
handles terminated-process conditions.
Definition: ssim.h:301
ssim::Sim::clear
static void clear()
clears out internal data structures
Definition: ssim.cc:115
ssim::ProcessWithPId::ProcessWithPId
ProcessWithPId()
Definition: ssim.cc:296
ssim::Sim::self_signal_event
static void self_signal_event(const Event *e)
signal an event to the current process immediately
Definition: ssim.cc:268
ssim::Sim::run_simulation
static void run_simulation()
starts execution of the simulation
Definition: ssim.cc:148
ssim::Sim::clock
static Time clock()
returns the current virtual time for the current process
Definition: ssim.cc:264
ssim::SimErrorHandler::handle_busy
virtual void handle_busy(ProcessId p, const Event *e)
handles busy-process conditions.
Definition: ssim.h:282
ssim::Event::str
virtual std::string str() const
Definition: ssim.h:110
ssim::Time
double Time
virtual time type
Definition: ssim.h:75
ssim::Process::process_event
virtual void process_event(const Event *msg)
action executed in response to an event signaled to this process.
Definition: ssim.h:194
ssim::Sim::stop_process
static void stop_process()
stops the execution of the current process
Definition: ssim.cc:241
ssim
name space for the Siena simulator.
Definition: microsimulation.cc:3
ssim::Rprint_actions
void Rprint_actions()
Definition: ssim.cc:81
ssim::Sim
a generic discrete-event sequential simulator
Definition: ssim.h:326
ssim::NULL_PROCESSID
const ProcessId NULL_PROCESSID
no process will be identified by NULL_PROCESSID
Definition: ssim.h:59
ssim::Process::~Process
virtual ~Process()
Definition: ssim.h:127
ssim::ProcessWithPId::process_id
ProcessId process_id
Definition: ssim.h:238