adevs
adevs_simpledigraph.h
1 
31 #ifndef __adevs_simpledigraph_h_
32 #define __adevs_simpledigraph_h_
33 #include "adevs.h"
34 #include <map>
35 #include <set>
36 #include <cstdlib>
37 
38 namespace adevs
39 {
40 
46 template <class VALUE, class T = double> class SimpleDigraph:
47 public Network<VALUE,T>
48 {
49  public:
52 
55  Network<VALUE,T>()
56  {
57  }
59  void add(Component* model);
61  void couple(Component* src, Component* dst);
65  void route(const VALUE& x, Component* model,
66  Bag<Event<VALUE,T> >& r);
69 
70  private:
71  // The set of components
72  Set<Component*> models;
73  // Coupling information
74  std::map<Component*,Bag<Component*> > graph;
75 };
76 
77 template <class VALUE, class T>
79 {
80  assert(model != this);
81  models.insert(model);
82  model->setParent(this);
83 }
84 
85 template <class VALUE, class T>
87 {
88  if (src != this) add(src);
89  if (dst != this) add(dst);
90  graph[src].insert(dst);
91 }
92 
93 template <class VALUE, class T>
95 {
96  c = models;
97 }
98 
99 template <class VALUE, class T>
101 route(const VALUE& x, Component* model,
102 Bag<Event<VALUE,T> >& r)
103 {
104  // Find the list of target models and ports
105  typename std::map<Component*,Bag<Component*> >::iterator graph_iter;
106  graph_iter = graph.find(model);
107  // If no target, just return
108  if (graph_iter == graph.end()) return;
109  // Otherwise, add the targets to the event bag
110  Event<VALUE,T> event;
111  typename Bag<Component*>::iterator node_iter;
112  for (node_iter = (*graph_iter).second.begin();
113  node_iter != (*graph_iter).second.end(); node_iter++)
114  {
115  event.model = *node_iter;
116  event.value = x;
117  r.insert(event);
118  }
119 }
120 
121 template <class VALUE, class T>
123 {
124  typename Set<Component*>::iterator i;
125  for (i = models.begin(); i != models.end(); i++)
126  {
127  delete *i;
128  }
129 }
130 
131 } // end of namespace
132 
133 #endif
void getComponents(Set< Component * > &c)
Puts the network's set of components into c.
Definition: adevs_simpledigraph.h:94
void route(const VALUE &x, Component *model, Bag< Event< VALUE, T > > &r)
Route an event according to the network's couplings.
Definition: adevs_simpledigraph.h:101
Definition: adevs_set.h:42
Definition: adevs_simpledigraph.h:46
Devs< VALUE, T > Component
A component of the SimpleDigraph model.
Definition: adevs_simpledigraph.h:51
Definition: adevs_models.h:46
~SimpleDigraph()
Destructor. Destroys all of the component models.
Definition: adevs_simpledigraph.h:122
void setParent(Network< X, T > *parent)
Definition: adevs_models.h:96
A bidirectional iterator for the Bag.
Definition: adevs_bag.h:49
void add(Component *model)
Add a model to the network.
Definition: adevs_simpledigraph.h:78
Definition: adevs_models.h:142
SimpleDigraph()
Construct a network without components.
Definition: adevs_simpledigraph.h:54
void couple(Component *src, Component *dst)
Couple the source model to the destination model.
Definition: adevs_simpledigraph.h:86
Definition: adevs_bag.h:45