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