00001 /*************** 00002 Copyright (C) 2000-2006 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_bag.h" 00023 #include "adevs_set.h" 00024 #include <cstdlib> 00025 00026 namespace adevs 00027 { 00028 00029 /* 00030 * Declare network and atomic model so types can be used as the type of 00031 * parent in the basic Devs model and for type ID functions. 00032 */ 00033 template <class X> class Network; 00034 template <class X> class Atomic; 00035 template <class X> class Schedule; 00036 template <class X> class Simulator; 00037 00039 #define ADEVS_NOT_ASSIGNED_TO_PROCESSOR -1 00040 00045 template <class X> class Devs 00046 { 00047 public: 00049 Devs(): 00050 parent(NULL), 00051 proc(ADEVS_NOT_ASSIGNED_TO_PROCESSOR) 00052 { 00053 } 00055 virtual ~Devs() 00056 { 00057 } 00063 virtual Network<X>* typeIsNetwork() { return NULL; } 00065 virtual Atomic<X>* typeIsAtomic() { return NULL; } 00070 const Network<X>* getParent() const { return parent; } 00071 Network<X>* getParent() { return parent; } 00078 void setParent(Network<X>* parent) { this->parent = parent; } 00091 virtual bool model_transition() { return false; } 00098 virtual double lookahead() { return 0.0; } 00105 void setProc(int proc) { this->proc = proc; } 00110 int getProc() { return proc; } 00111 00112 private: 00113 Network<X>* parent; 00114 int proc; 00115 }; 00116 00121 template <class X> class Event 00122 { 00123 public: 00125 Event(): 00126 model(NULL), 00127 value() 00128 { 00129 } 00131 Event(Devs<X>* model, const X& value): 00132 model(model), 00133 value(value) 00134 { 00135 } 00137 Event(const Event<X>& src): 00138 model(src.model), 00139 value(src.value) 00140 { 00141 } 00143 const Event<X>& operator=(const Event<X>& src) 00144 { 00145 model = src.model; 00146 value = src.value; 00147 return *this; 00148 } 00150 Devs<X>* model; 00152 X value; 00154 ~Event() 00155 { 00156 } 00157 }; 00158 00162 template <class X> class Atomic: public Devs<X> 00163 { 00164 public: 00166 Atomic(): 00167 Devs<X>() 00168 { 00169 tL = 0.0; 00170 x = y = NULL; 00171 q_index = 0; // The Schedule requires this to be zero 00172 active = false; 00173 } 00175 virtual void delta_int() = 0; 00177 virtual void delta_ext(double e, const Bag<X>& xb) = 0; 00179 virtual void delta_conf(const Bag<X>& xb) = 0; 00181 virtual void output_func(Bag<X>& yb) = 0; 00183 virtual double ta() = 0; 00190 virtual void gc_output(Bag<X>& g) = 0; 00192 virtual ~Atomic(){} 00194 Atomic<X>* typeIsAtomic() { return this; } 00195 protected: 00202 double getLastEventTime() const { return tL; } 00203 00204 private: 00205 00206 friend class Simulator<X>; 00207 friend class Schedule<X>; 00208 00209 // Time of last event 00210 double tL; 00211 // Index in the priority queue 00212 unsigned int q_index; 00213 // Input and output event bags 00214 Bag<X> *x, *y; 00215 // Has this model been activated? 00216 bool active; 00217 }; 00218 00222 template <class X> class Network: public Devs<X> 00223 { 00224 public: 00226 Network(): 00227 Devs<X>() 00228 { 00229 } 00235 virtual void getComponents(Set<Devs<X>*>& c) = 0; 00241 virtual void route(const X& value, Devs<X>* model, Bag<Event<X> >& r) = 0; 00246 virtual ~Network() 00247 { 00248 } 00250 Network<X>* typeIsNetwork() { return this; } 00251 }; 00252 00253 } // end of namespace 00254 00255 #endif