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 Predeclare 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; } 00076 void setParent(Network<X>* parent) { this->parent = parent; } 00089 virtual bool model_transition() { return false; } 00094 virtual double lookahead() { return 0.0; } 00101 void setProc(int proc) { this->proc = proc; } 00106 int getProc() { return proc; } 00107 00108 private: 00109 Network<X>* parent; 00110 int proc; 00111 }; 00112 00117 template <class X> class Event 00118 { 00119 public: 00121 Event(): 00122 model(NULL), 00123 value() 00124 { 00125 } 00127 Event(Devs<X>* model, const X& value): 00128 model(model), 00129 value(value) 00130 { 00131 } 00133 Event(const Event<X>& src): 00134 model(src.model), 00135 value(src.value) 00136 { 00137 } 00139 const Event<X>& operator=(const Event<X>& src) 00140 { 00141 model = src.model; 00142 value = src.value; 00143 return *this; 00144 } 00146 Devs<X>* model; 00148 X value; 00150 ~Event() 00151 { 00152 } 00153 }; 00154 00158 template <class X> class Atomic: public Devs<X> 00159 { 00160 public: 00164 Atomic(): 00165 Devs<X>() 00166 { 00167 tL = 0.0; 00168 x = y = NULL; 00169 q_index = 0; // The Schedule requires this to be zero 00170 active = false; 00171 } 00173 virtual void delta_int() = 0; 00175 virtual void delta_ext(double e, const Bag<X>& xb) = 0; 00177 virtual void delta_conf(const Bag<X>& xb) = 0; 00179 virtual void output_func(Bag<X>& yb) = 0; 00181 virtual double ta() = 0; 00188 virtual void gc_output(Bag<X>& g) = 0; 00190 virtual ~Atomic(){} 00192 Atomic<X>* typeIsAtomic() { return this; } 00193 protected: 00200 double getLastEventTime() const { return tL; } 00201 00202 private: 00203 00204 friend class Simulator<X>; 00205 friend class Schedule<X>; 00206 00207 // Time of last event 00208 double tL; 00209 // Index in the priority queue 00210 unsigned int q_index; 00211 // Input and output event bags 00212 Bag<X> *x, *y; 00213 // Has this model been actived? 00214 bool active; 00215 }; 00216 00220 template <class X> class Network: public Devs<X> 00221 { 00222 public: 00224 Network(): 00225 Devs<X>() 00226 { 00227 } 00233 virtual void getComponents(Set<Devs<X>*>& c) = 0; 00239 virtual void route(const X& value, Devs<X>* model, Bag<Event<X> >& r) = 0; 00244 virtual ~Network() 00245 { 00246 } 00248 Network<X>* typeIsNetwork() { return this; } 00249 }; 00250 00251 } // end of namespace 00252 00253 #endif