DiaDes  0.1
DIAgnosis of Discrete-Event System
Topology.hh
Go to the documentation of this file.
1 #ifndef __DIADES__AUTOMATA__TOPOLOGY__HH
2 #define __DIADES__AUTOMATA__TOPOLOGY__HH
3 #include <fstream>
4 #include <set>
5 #include <unordered_set>
8 #include <diades/graph/Graph.hh>
10 
11 
12 using std::unordered_set;
13 using std::set;
14 
15 namespace Diades
16 {
17 namespace Automata
18 {
20 
35  class Topology
36  {
37  public:
40  typedef unordered_set<Connection>::const_iterator ConnectionIterator;
41  typedef unordered_set<Node>::const_iterator NodeIterator;
42 
43  private:
46  Graph _graph;
47  unordered_set<Connection> _connections;
48  unordered_set<Node> _nodes;
50 
51  public:
52  static string typeName() { return "AutModel::Topology"; }
54 
55  Node getNodeOfName(const Identifier & name) const
56  {
57  try
58  {
59  NodeIterator nodeIt = nodeBegin();
60  bool found = false;
61  while((!found) && (nodeIt!=nodeEnd()))
62  {
63  found = _labels[*nodeIt] == name;
64  if (!found)
65  {
66  ++nodeIt;
67  }
68  }
69  if(found)
70  {
71  return *nodeIt;
72  }
73  return Node();
74  }
75  catch(exception & e)
76  {
77  throw Exception("getNodeOfName(): the operation did not succeed", e);
78  }
79  return Node();
80  }
81 
82 
83 
84  void setNodeName(Node node,const Identifier & name)
85  {
86  try
87  {
88  require(Exception,node.valid(),"setNodeName(): the node is not valid");
89  require(Exception,&node.owner() == &graph(),"setNodeName(): the node does not belong to the topology");
90  _labels[node]=name;
91  }
92  catch(exception & e)
93  {
94  throw Exception("setNodeName(): the operation did not succeed", e);
95  }
96  }
97 
98  const Identifier & getNodeName(Node node) const
99  {
100  try
101  {
102  require(Exception,node.valid(),"getNodeName(): the node is not valid");
103  require(Exception,&node.owner() == &graph(),"getNodeName(): the node does not belong to the topology");
104  return _labels[node];
105  }
106  catch(exception & e)
107  {
108  throw Exception("getNodeName(): the operation did not succeed", e);
109  }
110  return _labels[node];
111  }
112 
113 
114  const Identifier & getConnectionLabel(Connection connection) const
115  {
116  try
117  {
118  require(Exception,connection.valid(),"getConnectionLabel(): the connection is not valid");
119  require(Exception,&connection.owner() == &graph(),"getConnectionLabel(): the connection does not belong to the topology");
120  return _labels[connection];
121  }
122  catch(exception & e)
123  {
124  throw Exception("setConnectionEventLabel(): the operation did not succeed", e);
125  }
126  return _labels[connection];
127  }
128 
129  void setConnectionLabel(Connection connection,const Identifier & label)
130  {
131  try
132  {
133  require(Exception,connection.valid(),"setConnectionEventLabel(): the connection is not valid");
134  require(Exception,&connection.owner() == &graph(),"setConnectionEventLabel(): the connection does not belong to the topology");
135  _labels[connection]=label;
136  }
137  catch(exception & e)
138  {
139  throw Exception("setConnectionEventLabel(): the operation did not succeed", e);
140  }
141  }
142 
144  {
145  typedef CliqueIterator self;
146  typedef ptrdiff_t difference_type;
147  typedef std::forward_iterator_tag iterator_category;
149  typedef const Diades::Graph::Node* pointer;
152  SupportIterator _it;
153 
154 
155  CliqueIterator():_it(){}
156  explicit CliqueIterator(SupportIterator it):_it(it){}
157  reference operator*() const { return _it->targetRef(); }
158  pointer operator->() const { return &(_it->targetRef()); }
159  self & operator++() { ++_it; return *this; }
160  self operator++(int) { self tmp = *this; ++_it; return tmp; }
161  bool operator==(const self & it) const { return _it == it._it; }
162  bool operator !=(const self & it) const { return !(*this == it); }
163  Edge getSupport() const { return *_it; }
164  };
165 
167 
168  public:
169 
170 
177  unsigned numberOfNodes() const
178  {
179  return _nodes.size();
180  }
181 
188  unsigned numberOfConnections() const
189  {
190  return _connections.size();
191  }
192 
193 
194 
202  bool isSimple(const Connection & connection) const
203  {
204  require(Exception, connection.valid() && (&connection.owner() == &graph()), "isSimple(): the connection is invalid");
205  try
206  {
207  return connection.outDeg() == 2;
208  }
209  catch(exception & e)
210  {
211  throw Exception("isSimple(): the operation did not succeed", e);
212  }
213  return false;
214  }
215 
224  Connection addConnection(Node n1, Node n2);
225 
226 
236  Connection addConnection(Node n1, Node n2,const Identifier & label);
237 
238 
243  unsigned cliqueSize(Connection connection) const
244  {
245  return connection.outDeg();
246  }
247 
248 
249 
258  CliqueIterator cliqueBegin(Connection connection) const
259  {
260  try
261  {
262  require(Exception, connection.valid() && (&connection.owner() == &graph()), "cliqueBegin(): the connection is invalid");
263  return CliqueIterator(connection.outEdgeBegin());
264  }
265  catch(exception & e)
266  {
267  throw Exception("cliqueBegin(): the operation did not succeed", e);
268  }
269  return CliqueIterator(connection.outEdgeBegin());
270  }
279  CliqueIterator cliqueEnd(Connection connection) const
280  {
281  try
282  {
283  require(Exception, connection.valid() && (&connection.owner() == &graph()), "cliqueEnd: the connection is invalid");
284 
285  return CliqueIterator(connection.outEdgeEnd());
286 
287  }
288  catch(exception & e)
289  {
290  throw Exception("cliqueEnd(): the operation did not succeed", e);
291  }
292 
293  return CliqueIterator(connection.outEdgeEnd());
294  }
295 
296 
297 
304  ConnectionIterator connectionBegin() const
305  {
306  return _connections.begin();
307  }
314  ConnectionIterator connectionEnd() const
315  {
316  return _connections.end();
317  }
318 
323  Topology():_graph(),_connections(),_nodes(),_labels()
324  {
325  _labels.init(_graph);
326  }
327 
331  virtual ~Topology()
332  {
333  }
334 
335 
344  unsigned nbConnections(const Node & n1, const Node & n2) const;
345 
346 
355  unsigned connectivity(const Node & n) const;
356 
357 
366  unsigned structuralConnectivity(const Node & n) const;
367 
368 
369 
373  bool isConnected(const Node & n1, const Node & n2) const;
374 
380  void getConnections(const Node & n1, const Node & n2, list<Connection> & connections) const;
381 
382 
390  Connection addClique(const set<Node> & clique);
391 
398  Node addNode()
399  {
400  Node result;
401  try
402  {
403  result = _graph.newNode();
404  _nodes.insert(result);
405  stringstream stream;
406  stream << "n" << result.id();
407  _labels[result] = Identifier(stream.str());
408  ensure(Exception, result.valid() && (&result.owner() == &graph()), "addNode: the returned node is invalid");
409  }
410  catch(exception & e)
411  {
412  throw Exception("addNode: cannot add a new node to the topology");
413  }
414  return result;
415  }
416 
417 
424  Node addNode(const Identifier & name)
425  {
426  Node result;
427  try
428  {
429  result = _graph.newNode();
430  _nodes.insert(result);
431  _labels[result] = name;
432  ensure(Exception, result.valid() && (&result.owner() == &graph()), "addNode: the returned node is invalid");
433  }
434  catch(exception & e)
435  {
436  throw Exception("addNode: cannot add a new node to the topology");
437  }
438  return result;
439  }
440 
447  NodeIterator nodeBegin() const
448  {
449  return _nodes.begin();
450  }
457  NodeIterator nodeEnd() const
458  {
459  return _nodes.end();
460  }
461 
469  NodeConnectionIterator nodeConnectionBegin(Node n) const
470  {
471  try
472  {
473  require(Exception, n.valid() && (&n.owner() == &graph()), "nodeConnectionBegin: the node is invalid");
474  return NodeConnectionIterator(n.outEdgeBegin());
475  }
476  catch(exception & e)
477  {
478  throw Exception("nodeConnectionBegin(): cannot get an iterator about the connection of a node",e);
479  }
480  return NodeConnectionIterator(n.outEdgeBegin());
481  }
489  NodeConnectionIterator nodeConnectionEnd(Node n) const
490  {
491  try
492  {
493  require(Exception, n.valid() && (&n.owner() == &graph()), "nodeConnectionEnd: the node is invalid");
494  return NodeConnectionIterator(n.outEdgeEnd());
495  }
496 catch(exception & e)
497  {
498  throw Exception("nodeConnectionEnd(): cannot get an iterator about the connection of a node",e);
499  }
500  return NodeConnectionIterator(n.outEdgeEnd());
501  }
507  void removeConnection(Connection connection)
508  {
509  require(Exception, connection.valid() && (&connection.owner() == &graph()), "removeConnection: the connection is invalid");
510  _connections.erase(connection);
511  _graph.deleteNode(connection);
512  }
513 
519  void removeNode(Node n);
520 
526  void topology2dot(const string & fileName);
527 
532  void clear();
533 
547  void import(const string & fileName);
548 
549 
562  void save(const string & fileName);
563 
567  bool valid() const
568  {
569  return numberOfNodes() > 0;
570  }
571 
575  bool operator==(const Topology & topology) const
576  {
577  return this == &topology;
578  }
579 
580 
581  bool contains(Node node) const
582  {
583  require(Exception, node.valid(),"contains(): invalid node");
584  try
585  {
586  return &node.owner() == &graph();
587  }
588  catch(exception & e)
589  {
590  throw Exception("contains(): the operation did not succeed",e);
591  }
592  return false;
593  }
594 
595 
602  const Graph & graph() const
603  {
604  return _graph;
605  }
606  };
607 
608 
609  /************************ UTILS ***************************************/
610 
618  Topology::Connection getConnection(const Topology & topology, const set<Topology::Node> & clique,
619  const Identifier & connectionEventLabel);
620 
621 };
622 };
623 
624 #endif
unordered_set< Node >::const_iterator NodeIterator
Definition: Topology.hh:41
Connection addConnection(Node n1, Node n2)
An Identifier is a reference to a string (IdentifierData) that only contains alpha-numeric characters...
Definition: Identifier.hh:133
CliqueIterator cliqueBegin(Connection connection) const
Definition: Topology.hh:258
ConnectionIterator connectionBegin() const
Definition: Topology.hh:304
unsigned structuralConnectivity(const Node &n) const
Node addNode(const Identifier &name)
Definition: Topology.hh:424
NodeIterator nodeEnd() const
Definition: Topology.hh:457
Diades::Graph::Node Connection
Definition: Topology.hh:38
Diades::Graph::Graph Graph
Definition: Topology.hh:44
void init(Graph &g, SizeType capacity=0, ValueType dflt=ValueType())
Definition: NodeMap.hh:315
Diades::Utils::Exception< Topology > Exception
Definition: Topology.hh:53
unsigned cliqueSize(Connection connection) const
Definition: Topology.hh:243
#define ensure(Exception, expr, message)
Definition: Assertion.hh:98
Node getNodeOfName(const Identifier &name) const
Definition: Topology.hh:55
NodeConnectionIterator nodeConnectionEnd(Node n) const
Definition: Topology.hh:489
void topology2dot(const string &fileName)
void removeConnection(Connection connection)
Definition: Topology.hh:507
unordered_set< Connection > _connections
Definition: Topology.hh:47
Diades::Graph::Edge Edge
Definition: Topology.hh:45
void setNodeName(Node node, const Identifier &name)
Definition: Topology.hh:84
const Graph & graph() const
Definition: Topology.hh:602
static string typeName()
Definition: Topology.hh:52
NodeConnectionIterator nodeConnectionBegin(Node n) const
Definition: Topology.hh:469
unordered_set< Connection >::const_iterator ConnectionIterator
Definition: Topology.hh:40
NodeIterator nodeBegin() const
Definition: Topology.hh:447
const Diades::Graph::Node * pointer
Definition: Topology.hh:149
bool operator==(const Topology &topology) const
Definition: Topology.hh:575
bool isSimple(const Connection &connection) const
Definition: Topology.hh:202
#define require(Exception, expr, message)
Definition: Assertion.hh:90
Namespace of the Diades project.
void setConnectionLabel(Connection connection, const Identifier &label)
Definition: Topology.hh:129
Diades::Graph::Node Node
Definition: Topology.hh:39
void save(const string &fileName)
const Identifier & getNodeName(Node node) const
Definition: Topology.hh:98
const Diades::Graph::Node & reference
Definition: Topology.hh:150
list< Edge >::iterator OutEdgeIterator
Definition: Edge.hh:314
unordered_set< Node > _nodes
Definition: Topology.hh:48
Connection addClique(const set< Node > &clique)
const Identifier & getConnectionLabel(Connection connection) const
Definition: Topology.hh:114
bool contains(Node node) const
Definition: Topology.hh:581
CliqueIterator NodeConnectionIterator
Definition: Topology.hh:166
unsigned numberOfConnections() const
Definition: Topology.hh:188
unsigned numberOfNodes() const
Definition: Topology.hh:177
bool operator==(const self &it) const
Definition: Topology.hh:161
Diades::Utils::Identifier Identifier
Definition: Topology.hh:19
Topology::Connection getConnection(const Topology &topology, const set< Topology::Node > &clique, const Identifier &connectionEventLabel)
unsigned nbConnections(const Node &n1, const Node &n2) const
ConnectionIterator connectionEnd() const
Definition: Topology.hh:314
std::forward_iterator_tag iterator_category
Definition: Topology.hh:147
Diades::Graph::NodeMap< Identifier > _labels
Definition: Topology.hh:49
bool operator!=(const self &it) const
Definition: Topology.hh:162
bool isConnected(const Node &n1, const Node &n2) const
void getConnections(const Node &n1, const Node &n2, list< Connection > &connections) const
class Identifier
unsigned connectivity(const Node &n) const
CliqueIterator cliqueEnd(Connection connection) const
Definition: Topology.hh:279
Diades::Graph::OutEdgeIterator SupportIterator
Definition: Topology.hh:151