adevs

/home/rotten/Code/adevs-2.4.3/include/adevs_models.h

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_bag.h"
00023 #include "adevs_set.h"
00024 #include "adevs_exception.h"
00025 #include <cstdlib>
00026 
00027 namespace adevs
00028 {
00029 
00030 /*
00031  * Declare network and atomic model so types can be used as the type of
00032  * parent in the basic Devs model and for type ID functions.  
00033  */
00034 template <class X> class Network;
00035 template <class X> class Atomic;
00036 template <class X> class Schedule;
00037 template <class X> class Simulator;
00038 
00039 /*
00040  * Constant indicating no processor assignment for the model. This is used by the
00041  * parallel simulator
00042  */
00043 #define ADEVS_NOT_ASSIGNED_TO_PROCESSOR -1
00044 
00049 template <class X> class Devs
00050 {
00051         public:
00053                 Devs():
00054                 parent(NULL),
00055                 proc(ADEVS_NOT_ASSIGNED_TO_PROCESSOR)
00056                 {
00057                 }
00059                 virtual ~Devs()
00060                 {
00061                 }
00067                 virtual Network<X>* typeIsNetwork() { return NULL; }
00069                 virtual Atomic<X>* typeIsAtomic() { return NULL; }
00074                 const Network<X>* getParent() const { return parent; }
00075                 Network<X>* getParent() { return parent; }
00082                 void setParent(Network<X>* parent) { this->parent = parent; }
00095                 virtual bool model_transition() { return false; }
00103                 virtual double lookahead() { return 0.0; }
00110                 void setProc(int proc) { this->proc = proc; }
00115                 int getProc() { return proc; }
00116 
00117         private:
00118                 Network<X>* parent;
00119                 int proc;
00120 };
00121 
00127 template <class X> class Event
00128 {
00129         public:
00131                 Event():
00132                 model(NULL),
00133                 value()
00134                 {
00135                 }
00143                 Event(Devs<X>* model, const X& value):
00144                 model(model),
00145                 value(value)
00146                 {
00147                 }
00149                 Event(const Event<X>& src):
00150                 model(src.model),
00151                 value(src.value)
00152                 {
00153                 }
00155                 const Event<X>& operator=(const Event<X>& src)
00156                 {
00157                         model = src.model;
00158                         value = src.value;
00159                         return *this;
00160                 }
00162                 Devs<X>* model;
00164                 X value;
00166                 ~Event()
00167                 {
00168                 }
00169 };
00170 
00174 template <class X> class Atomic: public Devs<X>
00175 {
00176         public:
00178                 Atomic():
00179                 Devs<X>()
00180                 {
00181                         tL = 0.0;
00182                         tL_cp = -1.0;
00183                         x = y = NULL;
00184                         q_index = 0; // The Schedule requires this to be zero
00185                         active = false;
00186                 }
00188                 virtual void delta_int() = 0;
00194                 virtual void delta_ext(double e, const Bag<X>& xb) = 0;
00199                 virtual void delta_conf(const Bag<X>& xb) = 0;
00204                 virtual void output_func(Bag<X>& yb) = 0;
00209                 virtual double ta() = 0;
00216                 virtual void gc_output(Bag<X>& g) = 0;
00226                 virtual void beginLookahead()
00227                 {
00228                         method_not_supported_exception ns("beginLookahead",this);
00229                         throw ns;
00230                 }
00237                 virtual void endLookahead(){}
00239                 virtual ~Atomic(){}
00241                 Atomic<X>* typeIsAtomic() { return this; }
00242         protected:
00249                 double getLastEventTime() const { return tL; }
00250 
00251         private:
00252 
00253                 friend class Simulator<X>;
00254                 friend class Schedule<X>;
00255 
00256                 // Time of last event
00257                 double tL;
00258                 // Index in the priority queue
00259                 unsigned int q_index;
00260                 // Input and output event bags
00261                 Bag<X> *x, *y;
00262                 // Has this model been activated?
00263                 bool active;
00264                 // When did the model start checkpointing?
00265                 double tL_cp;
00266 };
00267 
00271 template <class X> class Network: public Devs<X>
00272 {
00273         public:
00275                 Network():
00276                 Devs<X>()
00277                 {
00278                 }
00285                 virtual void getComponents(Set<Devs<X>*>& c) = 0;
00297                 virtual void route(const X& value, Devs<X>* model, Bag<Event<X> >& r) = 0;
00302                 virtual ~Network()
00303                 {
00304                 }
00306                 Network<X>* typeIsNetwork() { return this; }
00307 };
00308 
00309 } // end of namespace
00310 
00311 #endif