adevs
adevs_models.h
1 
31 #ifndef __adevs_models_h_
32 #define __adevs_models_h_
33 #include "adevs_time.h"
34 #include "adevs_bag.h"
35 #include "adevs_set.h"
36 #include "adevs_exception.h"
37 #include <cstdlib>
38 
39 namespace adevs
40 {
41 
42 /*
43  * Declare network and atomic model so types can be used as the type of
44  * parent in the basic Devs model and for type ID functions.
45  */
46 template <class X, class T> class Network;
47 template <class X, class T> class Atomic;
48 template <class X, class T> class Schedule;
49 template <class X, class T> class Simulator;
50 
51 /*
52  * Constant indicating no processor assignment for the model. This is used by the
53  * parallel simulator
54  */
55 #define ADEVS_NOT_ASSIGNED_TO_PROCESSOR -1
56 
63 template <class X, class T = double> class Devs
64 {
65  public:
67  Devs():
68  parent(NULL),
69  proc(ADEVS_NOT_ASSIGNED_TO_PROCESSOR)
70  {
71  }
73  virtual ~Devs()
74  {
75  }
81  virtual Network<X,T>* typeIsNetwork() { return NULL; }
83  virtual Atomic<X,T>* typeIsAtomic() { return NULL; }
88  const Network<X,T>* getParent() const { return parent; }
89  Network<X,T>* getParent() { return parent; }
96  void setParent(Network<X,T>* parent) { this->parent = parent; }
109  virtual bool model_transition() { return false; }
117  virtual T lookahead() { return adevs_zero<T>(); }
124  void setProc(int proc) { this->proc = proc; }
129  int getProc() { return proc; }
130 
131  private:
132  Network<X,T>* parent;
133  int proc;
134 };
135 
141 template <class X, class T = double> class Event
142 {
143  public:
145  Event():
146  model(NULL),
147  value()
148  {
149  }
157  Event(Devs<X,T>* model, const X& value):
158  model(model),
159  value(value)
160  {
161  }
163  Event(const Event<X,T>& src):
164  model(src.model),
165  value(src.value)
166  {
167  }
169  const Event<X,T>& operator=(const Event<X,T>& src)
170  {
171  model = src.model;
172  value = src.value;
173  return *this;
174  }
178  X value;
181  {
182  }
183 };
184 
188 template <class X, class T = double> class Atomic: public Devs<X,T>
189 {
190  public:
193  Devs<X,T>()
194  {
195  tL = adevs_zero<T>();
196  tL_cp = adevs_sentinel<T>();
197  x = y = NULL;
198  q_index = 0; // The Schedule requires this to be zero
199  }
201  virtual void delta_int() = 0;
207  virtual void delta_ext(T e, const Bag<X>& xb) = 0;
212  virtual void delta_conf(const Bag<X>& xb) = 0;
217  virtual void output_func(Bag<X>& yb) = 0;
222  virtual T ta() = 0;
229  virtual void gc_output(Bag<X>& g) = 0;
239  virtual void beginLookahead()
240  {
241  method_not_supported_exception ns("beginLookahead",this);
242  throw ns;
243  }
250  virtual void endLookahead(){}
252  virtual ~Atomic(){}
254  Atomic<X,T>* typeIsAtomic() { return this; }
255  protected:
262  T getLastEventTime() const { return tL; }
263 
264  private:
265 
266  friend class Simulator<X,T>;
267  friend class Schedule<X,T>;
268 
269  // Time of last event
270  T tL;
271  // Index in the priority queue
272  unsigned int q_index;
273  // Input and output event bags
274  Bag<X> *x, *y;
275  // When did the model start checkpointing?
276  T tL_cp;
277 };
278 
282 template <class X, class T = double> class Network: public Devs<X,T>
283 {
284  public:
287  Devs<X,T>()
288  {
289  }
296  virtual void getComponents(Set<Devs<X,T>*>& c) = 0;
308  virtual void route(const X& value, Devs<X,T>* model, Bag<Event<X,T> >& r) = 0;
313  virtual ~Network()
314  {
315  }
317  Network<X,T>* typeIsNetwork() { return this; }
318 };
319 
320 } // end of namespace
321 
322 #endif