OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
db_graph.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #ifndef ctrlplane_db_graph_h
6 #define ctrlplane_db_graph_h
7 
8 #include <queue>
9 
10 #include <boost/function.hpp>
11 #include <boost/iterator/iterator_facade.hpp>
12 #include <boost/tuple/tuple.hpp>
13 #include "db/db_graph_base.h"
14 #include "db/db_graph_vertex.h"
15 
16 class DBGraphEdge;
17 
18 class DBGraph : public DBGraphBase {
19 public:
22  typedef boost::function<void(DBGraphVertex *)> VertexVisitor;
23  typedef boost::function<void(DBGraphEdge *)> EdgeVisitor;
24  typedef boost::function<void(DBGraphVertex *)> VertexFinish;
25 
26  struct VisitorFilter {
27  typedef std::set<std::string> AllowedEdgeSet;
28  // bool return value indicates that all edges are allowed except
29  // filterd by EdgeFilter
30  typedef std::pair<bool, AllowedEdgeSet> AllowedEdgeRetVal;
31  virtual ~VisitorFilter() { }
32  virtual bool VertexFilter(const DBGraphVertex *vertex) const {
33  return true;
34  }
35  virtual bool EdgeFilter(const DBGraphVertex *source,
36  const DBGraphVertex *target,
37  const DBGraphEdge *edge) const {
38  return true;
39  }
40  virtual AllowedEdgeRetVal AllowedEdges(const DBGraphVertex *vertex) const {
41  return std::make_pair(false, std::set<std::string>());
42  }
43  };
44 
45  typedef boost::tuple<DBGraphVertex *, DBGraphVertex *, DBGraphEdge *> DBEdgeInfo;
46  class edge_iterator : public boost::iterator_facade<
47  edge_iterator, DBEdgeInfo, boost::forward_traversal_tag, DBEdgeInfo
48  > {
49  public:
50  explicit edge_iterator(DBGraph *graph);
51  private:
53  void increment() {
54  ++iter_;
55  }
56  bool equal(const edge_iterator &rhs) const;
57  DBEdgeInfo dereference() const;
61  };
62 
63  class vertex_iterator : public boost::iterator_facade<
64  vertex_iterator, DBGraphVertex, boost::forward_traversal_tag> {
65  public:
66  explicit vertex_iterator(DBGraph *graph);
67 
68  private:
70  void increment() {
71  ++iter_;
72  }
73  bool equal(const vertex_iterator &rhs) const;
75  return *graph_->vertex_data(*iter_);
76  }
77 
79  graph_t::vertex_iterator iter_;
80  graph_t::vertex_iterator end_;
81  };
82 
83  void AddNode(DBGraphVertex *entry);
84 
85  void RemoveNode(DBGraphVertex *entry);
86 
87  Edge Link(DBGraphVertex *lhs, DBGraphVertex *rhs, DBGraphEdge *link);
88 
89  void Unlink(DBGraphEdge *link);
90 
91  const graph_t *graph() const { return &graph_; }
92 
94  return graph_[vertex].entry;
95  }
96 
97  const std::string edge_name(DBGraph::Edge edge) const {
98  return graph_[edge].name_;
99  }
100 
102  return graph_[edge].edge;
103  }
104 
105  void Visit(DBGraphVertex *start, VertexVisitor vertex_visit_fn,
106  EdgeVisitor edge_visit_fn);
107  void Visit(DBGraphVertex *start, VertexVisitor vertex_visit_fn,
108  EdgeVisitor edge_visit_fn, const VisitorFilter &filter);
109  void Visit(DBGraphVertex *start, VertexVisitor vertex_visit_fn,
110  EdgeVisitor edge_visit_fn, VertexFinish vertex_finish_fn);
111 
114 
115  vertex_iterator vertex_list_begin();
116  vertex_iterator vertex_list_end();
117 
118  void clear();
119 
120  size_t vertex_count() const;
121  size_t edge_count() const;
122 
123 private:
124  typedef std::queue<DBGraphVertex *> VisitQ;
125  struct EdgePredicate;
126  struct VertexPredicate;
127 
128  void IterateEdges(DBGraphVertex *start,
129  OutEdgeIterator &iter_begin, OutEdgeIterator &iter_end,
130  VertexVisitor vertex_visit_fn, EdgeVisitor edge_visit_fn,
131  EdgePredicate &edge_test, VertexPredicate &vertex_test,
132  uint64_t curr_walk, VisitQ &visit_queue,
133  bool match_name=false, const std::string &allowed_edge = "");
134 
135  DBGraphVertex *vertex_target(DBGraphVertex *current_vertex,
136  DBGraphEdge *edge);
137 
139 };
140 
141 #endif
bool equal(const edge_iterator &rhs) const
Definition: db_graph.cc:248
edge_iterator edge_list_begin()
Definition: db_graph.cc:262
boost::adjacency_list< ordered_set_by_nameS, boost::listS, boost::undirectedS, VertexProperties, EdgeProperties > graph_t
Definition: db_graph_base.h:57
boost::graph_traits< graph_t >::edge_iterator edge_iterator
Definition: db_graph_base.h:61
virtual bool VertexFilter(const DBGraphVertex *vertex) const
Definition: db_graph.h:32
const graph_t * graph() const
Definition: db_graph.h:91
void IterateEdges(DBGraphVertex *start, OutEdgeIterator &iter_begin, OutEdgeIterator &iter_end, VertexVisitor vertex_visit_fn, EdgeVisitor edge_visit_fn, EdgePredicate &edge_test, VertexPredicate &vertex_test, uint64_t curr_walk, VisitQ &visit_queue, bool match_name=false, const std::string &allowed_edge="")
Definition: db_graph.cc:164
edge_iterator(DBGraph *graph)
Definition: db_graph.cc:235
virtual bool EdgeFilter(const DBGraphVertex *source, const DBGraphVertex *target, const DBGraphEdge *edge) const
Definition: db_graph.h:35
void Unlink(DBGraphEdge *link)
Definition: db_graph.cc:52
std::queue< DBGraphVertex * > VisitQ
Definition: db_graph.h:124
virtual AllowedEdgeRetVal AllowedEdges(const DBGraphVertex *vertex) const
Definition: db_graph.h:40
DBGraphVertex * vertex_data(DBGraphBase::vertex_descriptor vertex) const
Definition: db_graph.h:93
boost::function< void(DBGraphEdge *)> EdgeVisitor
Definition: db_graph.h:23
DBGraphBase::edge_iterator iter_
Definition: db_graph.h:59
boost::graph_traits< graph_t >::vertex_descriptor vertex_descriptor
Definition: db_graph_base.h:58
size_t vertex_count() const
Definition: db_graph.cc:305
std::set< std::string > AllowedEdgeSet
Definition: db_graph.h:27
vertex_iterator vertex_list_begin()
Definition: db_graph.cc:293
void clear()
Definition: db_graph.cc:301
DBGraphBase::vertex_descriptor Vertex
Definition: db_graph.h:21
vertex_iterator vertex_list_end()
Definition: db_graph.cc:297
edge_iterator edge_list_end()
Definition: db_graph.cc:266
DBEdgeInfo dereference() const
Definition: db_graph.cc:241
virtual ~VisitorFilter()
Definition: db_graph.h:31
void AddNode(DBGraphVertex *entry)
Definition: db_graph.cc:30
graph_t graph_
Definition: db_graph.h:138
friend class boost::iterator_core_access
Definition: db_graph.h:69
void Visit(DBGraphVertex *start, VertexVisitor vertex_visit_fn, EdgeVisitor edge_visit_fn)
Definition: db_graph.cc:111
graph_t::vertex_iterator iter_
Definition: db_graph.h:79
friend class boost::iterator_core_access
Definition: db_graph.h:52
void RemoveNode(DBGraphVertex *entry)
Definition: db_graph.cc:36
DBGraphEdge * edge_data(DBGraph::Edge edge) const
Definition: db_graph.h:101
graph_t::vertex_iterator end_
Definition: db_graph.h:80
std::pair< bool, AllowedEdgeSet > AllowedEdgeRetVal
Definition: db_graph.h:30
DBGraphVertex & dereference() const
Definition: db_graph.h:74
boost::function< void(DBGraphVertex *)> VertexFinish
Definition: db_graph.h:24
boost::graph_traits< graph_t >::edge_descriptor edge_descriptor
Definition: db_graph_base.h:59
DBGraphBase::edge_descriptor Edge
Definition: db_graph.h:20
Edge Link(DBGraphVertex *lhs, DBGraphVertex *rhs, DBGraphEdge *link)
Definition: db_graph.cc:41
vertex_iterator(DBGraph *graph)
Definition: db_graph.cc:270
DBGraphVertex * vertex_target(DBGraphVertex *current_vertex, DBGraphEdge *edge)
Definition: db_graph.cc:154
bool equal(const vertex_iterator &rhs) const
Definition: db_graph.cc:277
size_t edge_count() const
Definition: db_graph.cc:309
DBGraphBase::edge_iterator end_
Definition: db_graph.h:60
OutEdgeListType::iterator OutEdgeIterator
Definition: db_graph_base.h:67
boost::function< void(DBGraphVertex *)> VertexVisitor
Definition: db_graph.h:22
boost::tuple< DBGraphVertex *, DBGraphVertex *, DBGraphEdge * > DBEdgeInfo
Definition: db_graph.h:45
const std::string edge_name(DBGraph::Edge edge) const
Definition: db_graph.h:97