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_simpledigraph_h_ 00021 #define __adevs_simpledigraph_h_ 00022 #include "adevs.h" 00023 #include <map> 00024 #include <set> 00025 #include <cstdlib> 00026 00027 namespace adevs 00028 { 00029 00035 template <class VALUE, class T = double> class SimpleDigraph: 00036 public Network<VALUE,T> 00037 { 00038 public: 00040 typedef Devs<VALUE,T> Component; 00041 00043 SimpleDigraph(): 00044 Network<VALUE,T>() 00045 { 00046 } 00048 void add(Component* model); 00050 void couple(Component* src, Component* dst); 00052 void getComponents(Set<Component*>& c); 00054 void route(const VALUE& x, Component* model, 00055 Bag<Event<VALUE,T> >& r); 00057 ~SimpleDigraph(); 00058 00059 private: 00060 // The set of components 00061 Set<Component*> models; 00062 // Coupling information 00063 std::map<Component*,Bag<Component*> > graph; 00064 }; 00065 00066 template <class VALUE, class T> 00067 void SimpleDigraph<VALUE,T>::add(Component* model) 00068 { 00069 assert(model != this); 00070 models.insert(model); 00071 model->setParent(this); 00072 } 00073 00074 template <class VALUE, class T> 00075 void SimpleDigraph<VALUE,T>::couple(Component* src, Component* dst) 00076 { 00077 if (src != this) add(src); 00078 if (dst != this) add(dst); 00079 graph[src].insert(dst); 00080 } 00081 00082 template <class VALUE, class T> 00083 void SimpleDigraph<VALUE,T>::getComponents(Set<Component*>& c) 00084 { 00085 c = models; 00086 } 00087 00088 template <class VALUE, class T> 00089 void SimpleDigraph<VALUE,T>:: 00090 route(const VALUE& x, Component* model, 00091 Bag<Event<VALUE,T> >& r) 00092 { 00093 // Find the list of target models and ports 00094 typename std::map<Component*,Bag<Component*> >::iterator graph_iter; 00095 graph_iter = graph.find(model); 00096 // If no target, just return 00097 if (graph_iter == graph.end()) return; 00098 // Otherwise, add the targets to the event bag 00099 Event<VALUE,T> event; 00100 typename Bag<Component*>::iterator node_iter; 00101 for (node_iter = (*graph_iter).second.begin(); 00102 node_iter != (*graph_iter).second.end(); node_iter++) 00103 { 00104 event.model = *node_iter; 00105 event.value = x; 00106 r.insert(event); 00107 } 00108 } 00109 00110 template <class VALUE, class T> 00111 SimpleDigraph<VALUE,T>::~SimpleDigraph() 00112 { 00113 typename Set<Component*>::iterator i; 00114 for (i = models.begin(); i != models.end(); i++) 00115 { 00116 delete *i; 00117 } 00118 } 00119 00120 } // end of namespace 00121 00122 #endif