00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __adevs_wrapper_h_
00021 #define __adevs_wrapper_h_
00022 #include "adevs_models.h"
00023
00024 namespace adevs
00025 {
00043 template <typename ExternalType, typename InternalType> class ModelWrapper:
00044 public Atomic<ExternalType>,
00045 public EventListener<InternalType>
00046 {
00047 public:
00053 ModelWrapper(Devs<InternalType>* model);
00062 virtual void translateInput(const Bag<ExternalType>& external_input,
00063 Bag<Event<InternalType> >& internal_input) = 0;
00072 virtual void translateOutput(const Bag<Event<InternalType> >& internal_output,
00073 Bag<ExternalType>& external_output) = 0;
00080 virtual void gc_input(Bag<Event<InternalType> >& g) = 0;
00082 Devs<InternalType>* getWrappedModel() { return model; }
00084 void delta_int();
00086 void delta_ext(double e, const Bag<ExternalType>& xb);
00088 void delta_conf(const Bag<ExternalType>& xb);
00090 void output_func(Bag<ExternalType>& yb);
00092 double ta();
00094 void outputEvent(Event<InternalType> y, double t);
00096 ~ModelWrapper();
00097 private:
00098 ModelWrapper(){}
00099 ModelWrapper(const ModelWrapper&){}
00100 void operator=(const ModelWrapper&){}
00101
00102 Bag<Event<InternalType> > input;
00103
00104 Bag<Event<InternalType> > output;
00105
00106 Devs<InternalType>* model;
00107
00108 Simulator<InternalType>* sim;
00109
00110 double tL;
00111 };
00112
00113 template <typename ExternalType, typename InternalType>
00114 ModelWrapper<ExternalType,InternalType>::ModelWrapper(Devs<InternalType>* model):
00115 Atomic<ExternalType>(),
00116 EventListener<InternalType>(),
00117 model(model),
00118 tL(0.0)
00119 {
00120 sim = new Simulator<InternalType>(model);
00121 sim->addEventListener(this);
00122 }
00123
00124 template <typename ExternalType, typename InternalType>
00125 void ModelWrapper<ExternalType,InternalType>::delta_int()
00126 {
00127
00128 tL = sim->nextEventTime();
00129
00130 sim->execNextEvent();
00131 }
00132
00133 template <typename ExternalType, typename InternalType>
00134 void ModelWrapper<ExternalType,InternalType>::delta_ext(double e, const Bag<ExternalType>& xb)
00135 {
00136
00137 tL += e;
00138
00139 translateInput(xb,input);
00140
00141 sim->computeNextState(input,tL);
00142
00143 gc_input(input);
00144 input.clear();
00145 }
00146
00147 template <typename ExternalType, typename InternalType>
00148 void ModelWrapper<ExternalType,InternalType>::delta_conf(const Bag<ExternalType>& xb)
00149 {
00150
00151 tL = sim->nextEventTime();
00152
00153 translateInput(xb,input);
00154
00155 sim->computeNextState(input,tL);
00156
00157 gc_input(input);
00158 input.clear();
00159 }
00160
00161 template <typename ExternalType, typename InternalType>
00162 double ModelWrapper<ExternalType,InternalType>::ta()
00163 {
00164 if (sim->nextEventTime() < DBL_MAX) return sim->nextEventTime()-tL;
00165 else return DBL_MAX;
00166 }
00167
00168 template <typename ExternalType, typename InternalType>
00169 void ModelWrapper<ExternalType,InternalType>::output_func(Bag<ExternalType>& yb)
00170 {
00171
00172 sim->computeNextOutput();
00173
00174 translateOutput(output,yb);
00175
00176
00177 output.clear();
00178 }
00179
00180 template <typename ExternalType, typename InternalType>
00181 void ModelWrapper<ExternalType,InternalType>::outputEvent(Event<InternalType> y, double t)
00182 {
00183
00184 output.insert(y);
00185 }
00186
00187 template <typename ExternalType, typename InternalType>
00188 ModelWrapper<ExternalType,InternalType>::~ModelWrapper()
00189 {
00190 delete sim;
00191 delete model;
00192 }
00193
00194 }
00195
00196 #endif