adevs
adevs_digraph.h
1 
31 #ifndef __adevs_digraph_h_
32 #define __adevs_digraph_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 PORT=int> class PortValue
47 {
48  public:
51  port(),
52  value()
53  {
54  }
56  PortValue(const PortValue& src):
57  port(src.port),
58  value(src.value)
59  {
60  }
62  PortValue(PORT port, const VALUE& value):
63  port(port),
64  value(value)
65  {
66  }
69  {
70  port = src.port;
71  value = src.value;
72  return *this;
73  }
76  {
77  }
79  PORT port;
81  VALUE value;
82 };
83 
88 template <class VALUE, class PORT=int, class T = double> class Digraph:
89 public Network<PortValue<VALUE,PORT>,T>
90 {
91  public:
96 
99  Network<IO_Type,T>()
100  {
101  }
103  void add(Component* model);
105  void couple(Component* src, PORT srcPort,
106  Component* dst, PORT dstPort);
110  void route(const IO_Type& x, Component* model,
111  Bag<Event<IO_Type,T> >& r);
113  ~Digraph();
114 
115  private:
116  // A node in the coupling graph
117  struct node
118  {
119  node():
120  model(NULL),
121  port()
122  {
123  }
124  node(Component* model, PORT port):
125  model(model),
126  port(port)
127  {
128  }
129  const node& operator=(const node& src)
130  {
131  model = src.model;
132  port = src.port;
133  return *this;
134  }
135  Component* model;
136  PORT port;
137 
138  // Comparison for STL map
139  bool operator<(const node& other) const
140  {
141  if (model == other.model) return port < other.port;
142  return model < other.model;
143  }
144  };
145  // Component model set
146  Set<Component*> models;
147  // Coupling information
148  std::map<node,Bag<node> > graph;
149 };
150 
151 template <class VALUE, class PORT, class T>
153 {
154  assert(model != this);
155  models.insert(model);
156  model->setParent(this);
157 }
158 
159 template <class VALUE, class PORT, class T>
160 void Digraph<VALUE,PORT,T>::couple(Component* src, PORT srcPort,
161 Component* dst, PORT dstPort)
162 {
163  if (src != this) add(src);
164  if (dst != this) add(dst);
165  node src_node(src,srcPort);
166  node dst_node(dst,dstPort);
167  graph[src_node].insert(dst_node);
168 }
169 
170 template <class VALUE, class PORT, class T>
172 {
173  c = models;
174 }
175 
176 template <class VALUE, class PORT, class T>
178 route(const IO_Type& x, Component* model,
179 Bag<Event<IO_Type,T> >& r)
180 {
181  // Find the list of target models and ports
182  node src_node(model,x.port);
183  typename std::map<node,Bag<node> >::iterator graph_iter;
184  graph_iter = graph.find(src_node);
185  // If no target, just return
186  if (graph_iter == graph.end()) return;
187  // Otherwise, add the targets to the event bag
188  Event<IO_Type,T> event;
189  typename Bag<node>::iterator node_iter;
190  for (node_iter = (*graph_iter).second.begin();
191  node_iter != (*graph_iter).second.end(); node_iter++)
192  {
193  event.model = (*node_iter).model;
194  event.value.port = (*node_iter).port;
195  event.value.value = x.value;
196  r.insert(event);
197  }
198 }
199 template <class VALUE, class PORT, class T>
201 {
202  typename Set<Component*>::iterator i;
203  for (i = models.begin(); i != models.end(); i++)
204  {
205  delete *i;
206  }
207 }
208 
209 } // end of namespace
210 
211 #endif
Definition: adevs_set.h:42
const PortValue< VALUE, PORT > & operator=(const PortValue< VALUE, PORT > &src)
Assignment operator.
Definition: adevs_digraph.h:68
Digraph()
Construct a network with no components.
Definition: adevs_digraph.h:98
Definition: adevs_digraph.h:46
PortValue(const PortValue &src)
Copy constructor.
Definition: adevs_digraph.h:56
PortValue()
Constructor.
Definition: adevs_digraph.h:50
void route(const IO_Type &x, Component *model, Bag< Event< IO_Type, T > > &r)
Route an event based on the coupling information.
Definition: adevs_digraph.h:178
Definition: adevs_models.h:46
PORT port
The port on which the value appears.
Definition: adevs_digraph.h:79
void setParent(Network< X, T > *parent)
Definition: adevs_models.h:96
Devs< IO_Type, T > Component
A component of the Digraph model.
Definition: adevs_digraph.h:95
~PortValue()
Destructor.
Definition: adevs_digraph.h:75
void couple(Component *src, PORT srcPort, Component *dst, PORT dstPort)
Couple the source model to the destination model.
Definition: adevs_digraph.h:160
PortValue< VALUE, PORT > IO_Type
An input or output to a component model.
Definition: adevs_digraph.h:93
A bidirectional iterator for the Bag.
Definition: adevs_bag.h:49
PortValue(PORT port, const VALUE &value)
Create an object with the specified port and value.
Definition: adevs_digraph.h:62
~Digraph()
Destructor. Destroys all of the component models.
Definition: adevs_digraph.h:200
VALUE value
The value appearing on the port.
Definition: adevs_digraph.h:81
Definition: adevs_models.h:63
Definition: adevs_digraph.h:88
Definition: adevs_models.h:142
void getComponents(Set< Component * > &c)
Puts the network's components into to c.
Definition: adevs_digraph.h:171
void add(Component *model)
Add a model to the network.
Definition: adevs_digraph.h:152
Definition: adevs_bag.h:45