adevs
adevs_wrapper.h
1 
31 #ifndef __adevs_wrapper_h_
32 #define __adevs_wrapper_h_
33 #include "adevs_models.h"
34 
35 namespace adevs
36 {
54  template <typename ExternalType, typename InternalType, class T = double> class ModelWrapper:
55  public Atomic<ExternalType,T>,
56  public EventListener<InternalType,T>
57  {
58  public:
73  virtual void translateInput(const Bag<ExternalType>& external_input,
74  Bag<Event<InternalType,T> >& internal_input) = 0;
83  virtual void translateOutput(const Bag<Event<InternalType,T> >& internal_output,
84  Bag<ExternalType>& external_output) = 0;
91  virtual void gc_input(Bag<Event<InternalType,T> >& g) = 0;
95  void delta_int();
97  void delta_ext(T e, const Bag<ExternalType>& xb);
99  void delta_conf(const Bag<ExternalType>& xb);
101  void output_func(Bag<ExternalType>& yb);
103  T ta();
105  void outputEvent(Event<InternalType,T> y, T t);
107  ~ModelWrapper();
108  private:
109  ModelWrapper(){}
110  ModelWrapper(const ModelWrapper&){}
111  void operator=(const ModelWrapper&){}
112  // Bag of events created by the input translation method
113  Bag<Event<InternalType,T> > input;
114  // Output from the wrapped model
115  Bag<Event<InternalType,T> > output;
116  // The wrapped model
117  Devs<InternalType,T>* model;
118  // Simulator for driving the wrapped model
119  Simulator<InternalType,T>* sim;
120  // Last event time
121  T tL;
122  };
123 
124 template <typename ExternalType, typename InternalType, class T>
126  Atomic<ExternalType,T>(),
127  EventListener<InternalType,T>(),
128  model(model),
129  tL(adevs_zero<T>())
130 {
131  sim = new Simulator<InternalType,T>(model);
132  sim->addEventListener(this);
133 }
134 
135 template <typename ExternalType, typename InternalType, class T>
137 {
138  // Update the internal clock
139  tL = sim->nextEventTime();
140  // Execute the next autonomous event for the wrapped model
141  sim->execNextEvent();
142 }
143 
144 template <typename ExternalType, typename InternalType, class T>
146 {
147  // Update the internal clock
148  tL += e;
149  // Convert the external inputs to internal inputs
150  translateInput(xb,input);
151  // Apply the input
152  sim->computeNextState(input,tL);
153  // Clean up
154  gc_input(input);
155  input.clear();
156 }
157 
158 template <typename ExternalType, typename InternalType, class T>
160 {
161  // Update the internal clock
162  tL = sim->nextEventTime();
163  // Convert the external inputs to internal inputs
164  translateInput(xb,input);
165  // Apply the input
166  sim->computeNextState(input,tL);
167  // Clean up
168  gc_input(input);
169  input.clear();
170 }
171 
172 template <typename ExternalType, typename InternalType, class T>
174 {
175  if (sim->nextEventTime() < adevs_inf<T>()) return sim->nextEventTime()-tL;
176  else return adevs_inf<T>();
177 }
178 
179 template <typename ExternalType, typename InternalType, class T>
181 {
182  // Compute the model's output events; this causes the outputEvent method to be called
183  sim->computeNextOutput();
184  // Translate the output events to external output events
185  translateOutput(output,yb);
186  // Clean up; the contents of the output bag are deleted by the wrapped model's
187  // gc_output method
188  output.clear();
189 }
190 
191 template <typename ExternalType, typename InternalType, class T>
193 {
194  // Just save the events for processing by the output_func
195  output.insert(y);
196 }
197 
198 template <typename ExternalType, typename InternalType, class T>
200 {
201  delete sim;
202  delete model;
203 }
204 
205 } // end of namespace
206 
207 #endif
Definition: adevs_event_listener.h:43
Definition: adevs_wrapper.h:54
virtual void gc_input(Bag< Event< InternalType, T > > &g)=0
void delta_ext(T e, const Bag< ExternalType > &xb)
Atomic external transition function.
Definition: adevs_wrapper.h:145
void delta_conf(const Bag< ExternalType > &xb)
Atomic confluent transition function.
Definition: adevs_wrapper.h:159
virtual void translateInput(const Bag< ExternalType > &external_input, Bag< Event< InternalType, T > > &internal_input)=0
void outputEvent(Event< InternalType, T > y, T t)
EventListener outputEvent method.
Definition: adevs_wrapper.h:192
void output_func(Bag< ExternalType > &yb)
Atomic output function.
Definition: adevs_wrapper.h:180
virtual void translateOutput(const Bag< Event< InternalType, T > > &internal_output, Bag< ExternalType > &external_output)=0
T ta()
Atomic time advance function.
Definition: adevs_wrapper.h:173
void delta_int()
Atomic internal transition function.
Definition: adevs_wrapper.h:136
void addEventListener(EventListener< X, T > *l)
Definition: adevs_abstract_simulator.h:53
Definition: adevs_models.h:47
Devs< InternalType, T > * getWrappedModel()
Get the model that is wrapped by this object.
Definition: adevs_wrapper.h:93
~ModelWrapper()
Destructor. This destroys the wrapped model too.
Definition: adevs_wrapper.h:199