DiaDes  0.1
DIAgnosis of Discrete-Event System
GraphIntOld.hh
Go to the documentation of this file.
1 #ifndef __DIADES__GRAPH__GRAPHINT__HH
2 #define __DIADES__GRAPH__GRAPHINT__HH
3 
4 #include <vector>
5 #include <unordered_set>
6 #include <unordered_map>
7 #include <boost/archive/text_oarchive.hpp>
8 #include <boost/archive/text_iarchive.hpp>
9 #include <boost/serialization/vector.hpp>
10 #include <boost/serialization/list.hpp>
12 #include <diades/graph/GraphGen.hh>
13 #include <diades/graph/EdgeData.hh>
14 #include <diades/graph/NodeData.hh>
15 #include <diades/graph/Edge.hh>
16 #include <diades/graph/Node.hh>
17 #include <diades/graph/Graph.hh.hh>
18 
19 
20 
21 namespace Diades
22 {
23  namespace Graph
24  {
35  class Graph {
36 
37  private:
38  GraphData * _data;
39 
40  public:
41  typedef vector<Node>::size_type SizeType;
42 
43 
44 
45 
46  public :
47 
50 
55  Graph():_data(nullptr){
56  std::cout << "GDefault: data = " << _data << std::endl;
57  }
58 
63  Graph(GraphData * data):_data(data)
64  {
65  std::cout << "GGraphData: data = " << _data << std::endl;
66  }
67 
72  Graph(const Graph & g):_data(g._data)
73  {
74  std::cout << "Gcopy: data = " << _data << std::endl;
75  }
76 
80  const GraphData * data() const
81  {
82  std::cout << "GconstData: data = " << _data << std::endl;
83  return _data;
84  }
85 
89  GraphData * data()
90  {
91  std::cout << "GnonconstData: data = " << _data << std::endl;
92  return _data;
93  }
94 
98  unsigned id() const
99  {
100  if(valid())
101  {
102  return _data->id();
103  }
104  return 0;
105  }
106 
110  void init()
111  {
112  if(valid())
113  {
114  std::cout << "Ginit(valid): b data = " << _data << std::endl;
115  _data->clear();
116  std::cout << "Ginit(valid): a data = " << _data << std::endl;
117  }
118  else
119  {
120  std::cout << "Ginit(not valid): b data = " << _data << std::endl;
121  _data = new GraphData();
122  std::cout << "Ginit(not valid): a data = " << _data << std::endl;
123  }
124  }
125 
129  void destroy()
130  {
131  std::cout << "Gdestroy: b data = " << _data << std::endl;
132  if(valid())
133  {
134  std::cout << "Gdestroy (valid): b data = " << _data << std::endl;
135  delete _data;
136  std::cout << "Gdestroy (valid): after delet data = " << _data << std::endl;
137  _data = nullptr;
138  std::cout << "Gdestroy (valid): finally data = " << _data << std::endl;
139  }
140  }
141 
142 
151  virtual ~Graph(){
152  std::cout << "Gdestructor data = " << _data << std::endl;
153  }
154 
156 
157 
158 
161 
162 
163 
172  bool succeeds(const Node & n1, const Node & n2) const
173  {
174  return _data->succeeds(n1,n2);
175  }
176 
177 
186  bool preceeds(const Node & n1, const Node & n2) const
187  {
188  return succeeds(n2,n1);
189  }
190 
191 
192 
193 
194 
195 
196 
197 
198  //****************** Accessors *********************************
199 
205  bool operator==(const Graph & g) const
206  {
207  return _data == g._data;
208  }
209 
210 
216  bool operator!=(const Graph & g) const
217  {
218  return !( (*this) == g );
219  }
220 
226  bool operator<(const Graph & g) const
227  {
228  if(!g.valid())
229  {
230  return false;
231  }
232  else
233  {
234  if(!valid())
235  {
236  return true;
237  }
238  else
239  {
240  return id() < g.id();
241  }
242  }
243  }
244 
250  SizeType numberOfNodes() const
251  {
252  require(GraphInvalid, valid(),"Graph::numberOfNodes, invalid graph");
253  return _data->numberOfNodes();
254  }
255 
261  SizeType numberOfEdges() const
262  {
263  require(GraphInvalid, valid(),"Graph::numberOfEdges, invalid graph");
264  return _data->numberOfEdges();
265  }
266 
272  bool empty() const
273  {
274  require(GraphInvalid, valid(),"Graph::empty(), invalid graph");
275  return numberOfNodes()==0;
276  }
277 
278 
284  Node target(const Edge & edge) const;
285 
286 
292  Node source(const Edge & edge) const;
293 
294 
295 
302  const Node & targetRef(const Edge & edge) const;
303 
304 
311  const Node & sourceRef(const Edge & edge) const;
312 
313 
314 
315 
316 
317 
323  Node getNode(unsigned id) const;
324 
330  Edge getEdge(unsigned id) const;
331 
340  void transitiveClosure(Edge edge);
341 
342 
350  bool cycleDetection(const Node & n) const
351  {
352  require(GraphInvalid, valid(),"Graph::cycleDetection(), invalid graph");
353  return _data->cycleDetection(n);
354  }
355 
356 
358 
361 
366  Node newNode();
367 
368 
377  Edge newEdge(Node source, Node target)
378  {
379  require(GraphInvalid, valid(),"Graph::newEdge(), invalid graph");
380  return _data->newEdge(source,target);
381  }
382 
384 
395  virtual void deleteNode(Node & node)
396  {
397  require(GraphInvalid, valid(),"Graph::deleteNode(), invalid graph");
398  return _data->deleteNode(node);
399  }
400 
401 
402 
403 
412  {
413  require(GraphInvalid, valid(),"Graph::deleteNode(), invalid graph");
414  return _data->deleteNode(start,end);
415  }
416 
417 
418 
419 
427  virtual void deleteEdge(Edge & edge)
428  {
429  require(GraphInvalid, valid(),"Graph::deleteEdge(), invalid graph");
430  return _data->deleteEdge(edge);
431  }
432 
433 
434 
442  {
443  require(GraphInvalid, valid(),"Graph::deleteEdge(), invalid graph");
444  return _data->deleteEdge(start,end);
445  }
446 
448  {
449  require(GraphInvalid, valid(),"Graph::deleteEdge(), invalid graph");
450  return _data->deleteEdge(start,end);
451  }
452 
453 
456  virtual void deleteAllEdges()
457  {
458  require(GraphInvalid, valid(),"Graph::deleteAllEdges(), invalid graph");
459  return _data->deleteAllEdges();
460  }
461 
462 
463  // /** Elimination of edge paths
464  // * @param s a Node
465  // * @pre s.valid() && s.owner()==this
466  // * @post s.outDeg()==0
467  // *
468  // * Elimination of transition paths from the state s in the current graph.
469  // */
470  // virtual void eliminatePathsFrom(Node s);
471 
478  void clear()
479  {
480  require(GraphInvalid, valid(),"Graph::clear(), invalid graph");
481  _data->clear();
482  }
483 
485 
489 
496  require(GraphInvalid, valid(),"Graph::nodeBegin(), invalid graph");
497  return NodeIterator(&_data->_vNodes,0);
498  }
499 
506  require(GraphInvalid, valid(),"Graph::nodeEnd(), invalid graph");
507  return NodeIterator(&_data->_vNodes,_data->_vNodes.size());
508  }
509 
510 
511 
518  require(GraphInvalid, valid(),"Graph::nodeBegin(), invalid graph");
519  return NodeIterator(&_data->_vNodes,0);
520  }
521 
528  require(GraphInvalid, valid(),"Graph::nodeEnd(), invalid graph");
529  return NodeIterator(&_data->_vNodes,_data->_vNodes.size());
530  }
531 
532 
533 
534 
535 
542  {
543  require(GraphInvalid, valid(),"Graph::edgeBegin(), invalid graph");
544  return EdgeIterator(&_data->_vEdges,0);
545  }
546 
553  {
554  require(GraphInvalid, valid(),"Graph::edgeEnd(), invalid graph");
555  return EdgeIterator(&_data->_vEdges,_data->_vEdges.size());
556  }
557 
558 
565  {
566  require(GraphInvalid, valid(),"Graph::edgeBegin(), invalid graph");
567  return EdgeIterator(&_data->_vEdges,0);
568  }
569 
576  {
577  require(GraphInvalid, valid(),"Graph::edgeEnd(), invalid graph");
578  return EdgeIterator(&_data->_vEdges,_data->_vEdges.size());
579  }
580 
581 
587  OutEdgeIterator outEdgeBegin(const Node & node) const;
588 
594  OutEdgeIterator outEdgeEnd(const Node & node) const;
595 
601  InEdgeIterator inEdgeBegin(const Node & node) const;
602 
608  InEdgeIterator inEdgeEnd(const Node & node) const;
609 
610 
611 
613  //@name Others
615 
619  Graph & operator=(const Graph &g)
620  {
621  if(this != &g)
622  {
623  _data = g._data;
624  }
625  return *this;
626  }
627 
628  // /** Size of a graph
629  // * @return the memory usage of a graph
630  // **/
631  // SizeType memoryUsage() const;
632 
633  // /** Size of a sub-graph
634  // * @param transitions to measure
635  // * @return the memory usage of a sub-graph
636  // **/
637  // SizeType memoryUsage(const unordered_set<Edge> & transitions) const;
638 
639 
640  // /** Size of a graph (nodes)
641  // * @return the memory usage of the graph nodes
642  // **/
643  // SizeType nodeMemoryUsage() const;
644 
645  // /** Size of a graph (edges)
646  // * @return the memory usage of the graph edges
647  // **/
648  // SizeType edgeMemoryUsage() const;
649 
650 
651 
657  SizeType edgeCapacity() const {
658  require(GraphInvalid,valid(),"Graph::edgeCapacity(), invalid graph");
659  return _data->edgeCapacity();
660  }
661 
662 
668  SizeType nodeCapacity() const {
669  require(GraphInvalid,valid(),"Graph::nodeCapacity(), invalid graph");
670  return _data->nodeCapacity();
671  }
673 
674 
676  bool sanityCheck(string & log) const
677  {
678  if(!valid()) { log+="The graph is invalid"; return false; }
679  return _data->sanityCheck(log);
680  }
681 
682 
683  bool valid() const
684  {
685  return _data != nullptr;
686  }
687 
688  public:
689 
691  ostream & toDot(ostream & os);
692 
693 
694 
695 
696 
697  public:
698  template<class Archive>
699  void serialize(Archive & ar, const unsigned int version)
700  {
701  ar & _data;
702  }
703 
704 
705  friend ostream & operator << (ostream & os, const Graph & g)
706  {
707  return os << "[GRAPH: " << g._data << "]";
708  }
709  };
710 
711  inline Node Graph::getNode(unsigned id) const
712  {
713  if(valid())
714  {
715  return _data->getNode(id);
716  }
717  return Node();
718  }
719 
720  inline Edge Graph::getEdge(unsigned id) const
721  {
722  if(valid())
723  {
724  return _data->getEdge(id);
725  }
726  return Edge();
727  }
728 
730  {
731  require(GraphInvalid, valid(),"Graph::newNode(), invalid graph, please apply the init() method to the graph before!!");
732  return _data->newNode();
733  }
734 
735 
736  inline Node Graph::target(const Edge & edge) const
737  {
738  require(GraphInvalid, valid(),"Graph::target(), invalid graph");
739  require(GraphInvalid, edge.valid() && (edge.owner() == *this),"Graph::target, invalid edge");
740  return _data->target(edge);
741  }
742 
743 
744  inline Node Graph::source(const Edge & edge) const
745  {
746  require(GraphInvalid, valid(),"Graph::source(), invalid graph");
747  require(GraphInvalid, edge.valid() && (edge.owner() == *this),"Graph::source, invalid edge");
748  return _data->source(edge);
749  }
750 
751 
752  inline Node GraphData::target(const Edge & edge) const
753  {
754  require(GraphInvalid, edge.valid() && (edge.owner().data() == this),"GraphData::target, invalid edge");
755  return _vNodes[edge._eData->_target->identifier()];
756  }
757 
758 
759  inline Node GraphData::source(const Edge & edge) const
760  {
761  require(GraphInvalid, edge.valid() && (edge.owner().data() == this),"GraphData::source, invalid edge");
762  return _vNodes[edge._eData->_source->identifier()];
763  }
764 
765 
766 
767  inline const Node & Graph::targetRef(const Edge & edge) const
768  {
769  require(GraphInvalid, valid(),"Graph::target(), invalid graph");
770  require(GraphInvalid, edge.valid() && (edge.owner() == *this),"Graph::target, invalid edge");
771  return _data->targetRef(edge);
772  }
773 
774 
775  inline const Node & Graph::sourceRef(const Edge & edge) const
776  {
777  require(GraphInvalid, valid(),"Graph::source(), invalid graph");
778  require(GraphInvalid, edge.valid() && (edge.owner() == *this),"Graph::source, invalid edge");
779  return _data->sourceRef(edge);
780  }
781 
782 
783  inline const Node & GraphData::targetRef(const Edge & edge) const
784  {
785  require(GraphInvalid, edge.valid() && (edge.owner().data() == this),"GraphData::target, invalid edge");
786  return _vNodes[edge._eData->_target->identifier()];
787  }
788 
789 
790  inline const Node & GraphData::sourceRef(const Edge & edge) const
791  {
792  require(GraphInvalid, edge.valid() && (edge.owner().data() == this),"GraphData::source, invalid edge");
793  return _vNodes[edge._eData->_source->identifier()];
794  }
795 
796 
797 
798 
799 
800 
801 
802 
803 
804 
805 
806 
807 
808 
809 
810  inline OutEdgeIterator Graph::outEdgeBegin(const Node & node) const {
811  require(GraphInvalid, node.valid() && (node.owner() == *this),"Graph::outEdgeBegin, invalid node");
812  return node.outEdgeBegin();
813  }
814 
815 
816  inline OutEdgeIterator Graph::outEdgeEnd(const Node & node) const {
817  require(GraphInvalid,node.valid() && (node.owner() == *this),"Graph::outEdgeEnd, invalid state");
818  return node.outEdgeEnd();
819  }
820 
821 
822  inline InEdgeIterator Graph::inEdgeBegin(const Node & node) const {
823  require(GraphInvalid, node.valid() && (node.owner() == *this),"Graph::inEdgeBegin, invalid node");
824  return node.inEdgeBegin();
825  }
826 
827 
828  inline InEdgeIterator Graph::inEdgeEnd(const Node & node) const {
829  require(GraphInvalid,node.valid() && (node.owner() == *this),"Graph::inEdgeEnd, invalid state");
830  return node.inEdgeEnd();
831  }
832 
833 
834 
835 
840  template<typename InputIterator>
841  inline void deleteEdge(Graph & g, InputIterator first, InputIterator last)
842  {
843  for(; first!= last; ++first)
844  {
845  g.deleteEdge(*first);
846  }
847  }
848  template<typename InputIterator>
849  inline void deleteEdge(GraphData & g, InputIterator first, InputIterator last)
850  {
851  for(; first!= last; ++first)
852  {
853  g.deleteEdge(*first);
854  }
855  }
856 
857  template<typename InputIterator>
858  inline void deleteNode(Graph & g, InputIterator first, InputIterator last)
859  {
860  for(; first!= last; ++first)
861  {
862  g.deleteNode(*first);
863  }
864  }
865 
866 
867  template<typename InputIterator>
868  inline void deleteNode(GraphData & g, InputIterator first, InputIterator last)
869  {
870  for(; first!= last; ++first)
871  {
872  g.deleteNode(*first);
873  }
874  }
875  template<typename InputIterator,typename Predicate>
876  inline void deleteNode(Graph & g, InputIterator first, InputIterator last, Predicate pred)
877  {
878  for(; first!= last; ++first)
879  {
880  if(pred()(*first))
881  {
882  g.deleteNode(*first);
883  }
884  }
885  }
886 
887 
888 
889 
890 
891  };
892 };
893 
894 
895 namespace boost {
896  namespace serialization {
897 
898 
899  template<class Archive>
900  void save(Archive & ar, const ::Diades::Graph::GraphData & g, const unsigned int version)
901  {
902  unsigned nbNodes = g.numberOfNodes();
903  ar & nbNodes;
904  unsigned nbEdges = g.numberOfEdges();
905  ar & nbEdges;
906  ar & g.listEdgeId();
907  ar & g.listNodeId();
908  unsigned size = g.vNodes().size();
909  ar & size;
910  for(::Diades::Graph::SizeType i = 0; i < size; ++i)
911  {
912  if(g.vNodes().operator[](i).valid())
913  {
914  int id = g.vNodes().operator[](i).id();
915  ar & id; // == i?
916  }
917  }
918  size = g.vEdges().size();
919  ar & size;
920  for(::Diades::Graph::SizeType i = 0; i < size; ++i)
921  {
922  if(g.vEdges().operator[](i).valid())
923  {
924  int id = g.vEdges().operator[](i).id();
925  ar & id;
926  id = g.vEdges().operator[](i).source().id();
927  ar & id;
928  id = g.vEdges().operator[](i).target().id();
929  ar & id;
930  }
931  }
932 
933  }
934 
935 
936 
937  template<class Archive>
938  void load(Archive & ar, ::Diades::Graph::GraphData & g, const unsigned int version)
939  {
940  unsigned nbNodes;
941  ar & nbNodes;
942  unsigned nbEdges;
943  ar & nbEdges;
944  ar & g.listEdgeId();
945  ar & g.listNodeId();
947  ar & size;
948  g.vNodes().resize(size);
949  for(::Diades::Graph::SizeType i = 0; i < nbNodes; ++i)
950  {
951  ::Diades::Graph::SizeType validNodeIndex;
952  ar & validNodeIndex;
953  g.newNode(validNodeIndex);
954  }
955  ar & size;
956  g.vEdges().resize(size);
957  for(::Diades::Graph::SizeType i = 0; i < nbEdges; ++i)
958  {
959  ::Diades::Graph::SizeType validEdgeIndex;
960  ar & validEdgeIndex;
961  ::Diades::Graph::SizeType sourceNodeId;
962  ar & sourceNodeId;
963  ::Diades::Graph::SizeType targetNodeId;
964  ar & targetNodeId;
965  g.newEdge(validEdgeIndex, g.vNodes().operator[](sourceNodeId),g.vNodes().operator[](targetNodeId));
966  }
967  }
968  };
969 
970 };
971 
972 BOOST_SERIALIZATION_SPLIT_FREE(::Diades::Graph::GraphData)
973 
974 
975 
976 #endif
NodeData * _source
identifier of the Edge
Definition: EdgeData.hh:106
friend ostream & operator<<(ostream &os, const Graph &g)
Definition: GraphIntOld.hh:705
OutEdgeIterator outEdgeBegin(const Node &node) const
Definition: GraphIntOld.hh:810
NodeData * _target
pointer to the NodeData source of the Edge
Definition: EdgeData.hh:108
GraphIterator< Edge > EdgeIterator
Definition: GraphInt.hh:143
SizeType edgeCapacity() const
Definition: GraphIntOld.hh:657
Edge newEdge(Node source, Node target)
Definition: GraphIntOld.hh:377
vector< Node >::size_type SizeType
Definition: GraphIntOld.hh:41
SizeType nodeCapacity() const
Definition: GraphIntOld.hh:668
const Graph & owner() const
Definition: EdgeImpl.hh:85
bool valid() const
Definition: NodeImpl.hh:23
Definition: Run.cc:88
NodeIterator nodeEnd()
Definition: GraphIntOld.hh:505
InEdgeIterator inEdgeBegin() const
Definition: Node.hh:178
void save(Archive &ar, const ::Diades::Graph::GraphData &g, const unsigned int version)
Definition: GraphIntOld.hh:900
const Graph & owner() const
Definition: NodeImpl.hh:46
EdgeIterator edgeEnd()
Definition: GraphIntOld.hh:552
bool valid() const
Definition: GraphIntOld.hh:683
NodeId identifier() const
Definition: NodeData.hh:155
const Node & targetRef(const Edge &edge) const
Definition: GraphIntOld.hh:767
virtual void deleteEdge(EdgeIterator start, EdgeIterator end)
Definition: GraphIntOld.hh:441
InEdgeIterator inEdgeEnd() const
Definition: Node.hh:188
EdgeIterator edgeBegin()
Definition: GraphIntOld.hh:541
const GraphData * data() const
Definition: GraphIntOld.hh:80
OutEdgeIterator outEdgeBegin() const
Definition: Node.hh:158
EdgeIterator edgeBegin() const
Definition: GraphIntOld.hh:564
GraphIterator< Node > NodeIterator
Definition: GraphInt.hh:139
virtual void deleteEdge(LocalEdgeIterator start, LocalEdgeIterator end)
Definition: GraphIntOld.hh:447
Node source(const Edge &edge) const
Definition: GraphIntOld.hh:744
bool operator<(const Graph &g) const
Definition: GraphIntOld.hh:226
void serialize(Archive &ar, const unsigned int version)
Definition: GraphIntOld.hh:699
EdgeData * _eData
Definition: Edge.hh:204
NodeIterator nodeEnd() const
Definition: GraphIntOld.hh:527
ostream & toDot(ostream &os)
#define require(Exception, expr, message)
Definition: Assertion.hh:90
Namespace of the Diades project.
Edge getEdge(EdgeSizeType index) const
bool operator!=(const Graph &g) const
Definition: GraphIntOld.hh:216
SizeType numberOfNodes() const
Definition: GraphIntOld.hh:250
virtual void deleteNode(NodeIterator start, NodeIterator end)
Definition: GraphIntOld.hh:411
Graph(const Graph &g)
Definition: GraphIntOld.hh:72
list< Edge >::iterator LocalEdgeIterator
Definition: Edge.hh:315
InEdgeIterator inEdgeBegin(const Node &node) const
Definition: GraphIntOld.hh:822
bool sanityCheck(string &log) const
Definition: GraphIntOld.hh:676
bool succeeds(const Node &n1, const Node &n2) const
Definition: GraphIntOld.hh:172
list< Edge >::iterator OutEdgeIterator
Definition: Edge.hh:314
GraphData * data()
Definition: GraphIntOld.hh:89
virtual void deleteNode(Node &node)
Definition: GraphIntOld.hh:395
NodeId id() const
Definition: Node.hh:248
Node getNode(NodeSizeType index) const
void load(Archive &ar, ::Diades::Graph::GraphData &g, const unsigned int version)
Definition: GraphIntOld.hh:938
boost::adjacency_list Graph
Definition: ScaleFree.cc:9
virtual void deleteEdge(Edge &edge)
Definition: GraphIntOld.hh:427
OutEdgeIterator outEdgeEnd(const Node &node) const
Definition: GraphIntOld.hh:816
void transitiveClosure(Edge edge)
bool operator==(const Graph &g) const
Definition: GraphIntOld.hh:205
OutEdgeIterator outEdgeEnd() const
Definition: Node.hh:168
SizeType numberOfEdges() const
Definition: GraphIntOld.hh:261
Graph(GraphData *data)
Definition: GraphIntOld.hh:63
bool empty() const
Definition: GraphIntOld.hh:272
Log log(Logger logger, const string &msg)
Definition: Log.hh:124
bool cycleDetection(const Node &n) const
Definition: GraphIntOld.hh:350
unsigned id() const
Definition: GraphIntOld.hh:98
list< Edge >::iterator InEdgeIterator
Definition: Edge.hh:313
bool valid() const
Definition: EdgeImpl.hh:72
NodeIterator nodeBegin()
Definition: GraphIntOld.hh:495
vector< Node > _vNodes
Definition: GraphInt.hh:192
Node target(const Edge &edge) const
Definition: GraphIntOld.hh:736
EdgeIterator edgeEnd() const
Definition: GraphIntOld.hh:575
virtual void deleteAllEdges()
Definition: GraphIntOld.hh:456
NodeIterator nodeBegin() const
Definition: GraphIntOld.hh:517
Graph & operator=(const Graph &g)
Definition: GraphIntOld.hh:619
NodeSizeType numberOfNodes() const
Definition: GraphInt.hh:314
bool preceeds(const Node &n1, const Node &n2) const
Definition: GraphIntOld.hh:186
InEdgeIterator inEdgeEnd(const Node &node) const
Definition: GraphIntOld.hh:828
const Node & sourceRef(const Edge &edge) const
Definition: GraphIntOld.hh:775