adevs
|
00001 /*************** 00002 Copyright (C) 2000-2010 by James Nutaro 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Lesser General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Lesser General Public License for more details. 00013 00014 You should have received a copy of the GNU Lesser General Public 00015 License along with this library; if not, write to the Free Software 00016 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 00018 Bugs, comments, and questions can be sent to nutaro@gmail.com 00019 ***************/ 00020 #ifndef __adevs_models_h_ 00021 #define __adevs_models_h_ 00022 #include "adevs_time.h" 00023 #include "adevs_bag.h" 00024 #include "adevs_set.h" 00025 #include "adevs_exception.h" 00026 #include <cstdlib> 00027 00028 namespace adevs 00029 { 00030 00031 /* 00032 * Declare network and atomic model so types can be used as the type of 00033 * parent in the basic Devs model and for type ID functions. 00034 */ 00035 template <class X, class T> class Network; 00036 template <class X, class T> class Atomic; 00037 template <class X, class T> class Schedule; 00038 template <class X, class T> class Simulator; 00039 00040 /* 00041 * Constant indicating no processor assignment for the model. This is used by the 00042 * parallel simulator 00043 */ 00044 #define ADEVS_NOT_ASSIGNED_TO_PROCESSOR -1 00045 00052 template <class X, class T = double> class Devs 00053 { 00054 public: 00056 Devs(): 00057 parent(NULL), 00058 proc(ADEVS_NOT_ASSIGNED_TO_PROCESSOR) 00059 { 00060 } 00062 virtual ~Devs() 00063 { 00064 } 00070 virtual Network<X,T>* typeIsNetwork() { return NULL; } 00072 virtual Atomic<X,T>* typeIsAtomic() { return NULL; } 00077 const Network<X,T>* getParent() const { return parent; } 00078 Network<X,T>* getParent() { return parent; } 00085 void setParent(Network<X,T>* parent) { this->parent = parent; } 00098 virtual bool model_transition() { return false; } 00106 virtual T lookahead() { return adevs_zero<T>(); } 00113 void setProc(int proc) { this->proc = proc; } 00118 int getProc() { return proc; } 00119 00120 private: 00121 Network<X,T>* parent; 00122 int proc; 00123 }; 00124 00130 template <class X, class T = double> class Event 00131 { 00132 public: 00134 Event(): 00135 model(NULL), 00136 value() 00137 { 00138 } 00146 Event(Devs<X,T>* model, const X& value): 00147 model(model), 00148 value(value) 00149 { 00150 } 00152 Event(const Event<X,T>& src): 00153 model(src.model), 00154 value(src.value) 00155 { 00156 } 00158 const Event<X,T>& operator=(const Event<X,T>& src) 00159 { 00160 model = src.model; 00161 value = src.value; 00162 return *this; 00163 } 00165 Devs<X,T>* model; 00167 X value; 00169 ~Event() 00170 { 00171 } 00172 }; 00173 00177 template <class X, class T = double> class Atomic: public Devs<X,T> 00178 { 00179 public: 00181 Atomic(): 00182 Devs<X,T>() 00183 { 00184 tL = adevs_zero<T>(); 00185 tL_cp = adevs_sentinel<T>(); 00186 x = y = NULL; 00187 q_index = 0; // The Schedule requires this to be zero 00188 active = false; 00189 } 00191 virtual void delta_int() = 0; 00197 virtual void delta_ext(T e, const Bag<X>& xb) = 0; 00202 virtual void delta_conf(const Bag<X>& xb) = 0; 00207 virtual void output_func(Bag<X>& yb) = 0; 00212 virtual T ta() = 0; 00219 virtual void gc_output(Bag<X>& g) = 0; 00229 virtual void beginLookahead() 00230 { 00231 method_not_supported_exception ns("beginLookahead",this); 00232 throw ns; 00233 } 00240 virtual void endLookahead(){} 00242 virtual ~Atomic(){} 00244 Atomic<X,T>* typeIsAtomic() { return this; } 00245 protected: 00252 T getLastEventTime() const { return tL; } 00253 00254 private: 00255 00256 friend class Simulator<X,T>; 00257 friend class Schedule<X,T>; 00258 00259 // Time of last event 00260 T tL; 00261 // Index in the priority queue 00262 unsigned int q_index; 00263 // Input and output event bags 00264 Bag<X> *x, *y; 00265 // Has this model been activated? 00266 bool active; 00267 // When did the model start checkpointing? 00268 T tL_cp; 00269 }; 00270 00274 template <class X, class T = double> class Network: public Devs<X,T> 00275 { 00276 public: 00278 Network(): 00279 Devs<X,T>() 00280 { 00281 } 00288 virtual void getComponents(Set<Devs<X,T>*>& c) = 0; 00300 virtual void route(const X& value, Devs<X,T>* model, Bag<Event<X,T> >& r) = 0; 00305 virtual ~Network() 00306 { 00307 } 00309 Network<X,T>* typeIsNetwork() { return this; } 00310 }; 00311 00312 } // end of namespace 00313 00314 #endif