TuttleOFX
1
|
00001 #ifndef _TUTTLE_HOST_VISITORS_HPP_ 00002 #define _TUTTLE_HOST_VISITORS_HPP_ 00003 00004 #include <iostream> 00005 #include <vector> 00006 #include <boost/graph/depth_first_search.hpp> 00007 #include <boost/graph/breadth_first_search.hpp> 00008 #include <boost/graph/properties.hpp> 00009 #include <boost/graph/visitors.hpp> 00010 00011 namespace tuttle { 00012 namespace host { 00013 namespace graph { 00014 namespace visitor { 00015 00016 /** 00017 * @brief detect if there is a cycle inside a directed graph 00018 * or if we can garantee that it's a DAG, Directed Acyclic Graph. 00019 */ 00020 class CycleDetector : public boost::default_dfs_visitor 00021 { 00022 public: 00023 CycleDetector( bool& hasCycle ) 00024 : _hasCycle( hasCycle ) 00025 { 00026 _hasCycle = false; 00027 } 00028 00029 public: 00030 template<class EdgeDescriptor, class Graph> 00031 void back_edge( EdgeDescriptor, const Graph& ) 00032 { 00033 _hasCycle = true; 00034 } 00035 00036 public: 00037 bool& _hasCycle; 00038 }; 00039 00040 template<class TGraph> 00041 class MarkUsed : public boost::default_dfs_visitor 00042 { 00043 public: 00044 typedef typename TGraph::GraphContainer GraphContainer; 00045 typedef typename TGraph::Vertex Vertex; 00046 typedef typename TGraph::Edge ProcessEdge; 00047 00048 MarkUsed( TGraph& graph ) 00049 : _graph( graph ) 00050 {} 00051 00052 /** 00053 * Set all vertex with unused default value. 00054 */ 00055 template <class VertexDescriptor, class Graph> 00056 void initialize_vertex( VertexDescriptor v, const Graph& g ) 00057 { 00058 Vertex& vertex = _graph.instance( v ); 00059 00060 //TUTTLE_TLOG( TUTTLE_INFO, "MarkUsed &&&&& init" << vertex.getName() ); 00061 vertex.setUsed( false ); 00062 } 00063 00064 /** 00065 * Set visited vertex used. 00066 */ 00067 template <class VertexDescriptor, class Graph> 00068 void discover_vertex( VertexDescriptor v, const Graph& g ) 00069 { 00070 Vertex& vertex = _graph.instance( v ); 00071 00072 //TUTTLE_LOG_VAR( TUTTLE_TRACE, vertex.getName() ); 00073 vertex.setUsed(); 00074 } 00075 00076 private: 00077 TGraph& _graph; 00078 }; 00079 00080 template<class TGraph> 00081 class Test_dfs : public boost::default_dfs_visitor 00082 { 00083 public: 00084 typedef typename TGraph::GraphContainer GraphContainer; 00085 typedef typename TGraph::Vertex Vertex; 00086 typedef typename TGraph::Edge ProcessEdge; 00087 00088 Test_dfs( TGraph& graph ) 00089 : _graph( graph ) 00090 { 00091 TUTTLE_TLOG( TUTTLE_TRACE, "Test_dfs" ); 00092 } 00093 00094 ~Test_dfs() 00095 { 00096 TUTTLE_TLOG( TUTTLE_TRACE, "~Test_dfs" ); 00097 } 00098 00099 template <class VertexDescriptor, class Graph> 00100 void initialize_vertex( VertexDescriptor v, const Graph& g ) 00101 { 00102 Vertex& vertex = _graph.instance( v ); 00103 00104 TUTTLE_TLOG( TUTTLE_TRACE, "initialize_vertex: " << vertex ); 00105 } 00106 00107 template <class VertexDescriptor, class Graph> 00108 void start_vertex( VertexDescriptor v, const Graph& g ) 00109 { 00110 Vertex& vertex = _graph.instance( v ); 00111 00112 TUTTLE_TLOG( TUTTLE_TRACE, "start_vertex: " << vertex ); 00113 } 00114 00115 template <class VertexDescriptor, class Graph> 00116 void discover_vertex( VertexDescriptor v, const Graph& g ) 00117 { 00118 Vertex& vertex = _graph.instance( v ); 00119 00120 TUTTLE_TLOG( TUTTLE_TRACE, "discover_vertex: " << vertex ); 00121 } 00122 00123 template <class VertexDescriptor, class Graph> 00124 void finish_vertex( VertexDescriptor v, const Graph& g ) 00125 { 00126 Vertex& vertex = _graph.instance( v ); 00127 00128 TUTTLE_TLOG( TUTTLE_TRACE, "finish_vertex: " << vertex ); 00129 } 00130 00131 template<class EdgeDescriptor, class Graph> 00132 void examine_edge( EdgeDescriptor e, Graph& g ) 00133 { 00134 ProcessEdge& edge = _graph.instance( e ); 00135 00136 // Vertex& vertexSource = _graph.sourceInstance(e); 00137 // Vertex& vertexDest = _graph.targetInstance(e); 00138 00139 TUTTLE_TLOG( TUTTLE_TRACE, "examine_edge: " << edge ); 00140 } 00141 00142 template <class EdgeDescriptor, class Graph> 00143 void tree_edge( EdgeDescriptor e, const Graph& g ) 00144 { 00145 ProcessEdge& edge = _graph.instance( e ); 00146 00147 TUTTLE_TLOG( TUTTLE_TRACE, "tree_edge: " << edge ); 00148 } 00149 00150 template <class EdgeDescriptor, class Graph> 00151 void back_edge( EdgeDescriptor e, const Graph& g ) 00152 { 00153 ProcessEdge& edge = _graph.instance( e ); 00154 00155 TUTTLE_TLOG( TUTTLE_TRACE, "back_edge: " << edge ); 00156 } 00157 00158 template <class EdgeDescriptor, class Graph> 00159 void forward_or_cross_edge( EdgeDescriptor e, const Graph& g ) 00160 { 00161 ProcessEdge& edge = _graph.instance( e ); 00162 00163 TUTTLE_TLOG( TUTTLE_TRACE, "forward_or_cross_edge: " << edge ); 00164 } 00165 00166 private: 00167 TGraph& _graph; 00168 }; 00169 00170 class Test_bfs : public boost::default_bfs_visitor 00171 { 00172 public: 00173 Test_bfs() {} 00174 00175 template<class VertexDescriptor, class Graph> 00176 void initialize_vertex( VertexDescriptor v, Graph& g ) 00177 { 00178 TUTTLE_TLOG( TUTTLE_TRACE, "initialize_vertex " << g[v] ); 00179 } 00180 00181 template<class VertexDescriptor, class Graph> 00182 void discover_vertex( VertexDescriptor v, Graph& g ) 00183 { 00184 TUTTLE_TLOG( TUTTLE_TRACE, "discover_vertex " << g[v] << " outedges: " << out_degree( v, g ) ); 00185 } 00186 00187 template<class VertexDescriptor, class Graph> 00188 void examine_vertex( VertexDescriptor v, Graph& g ) 00189 { 00190 TUTTLE_TLOG( TUTTLE_TRACE, "examine_vertex " << g[v] ); 00191 } 00192 00193 template<class VertexDescriptor, class Graph> 00194 void finish_vertex( VertexDescriptor v, Graph& g ) 00195 { 00196 TUTTLE_TLOG( TUTTLE_TRACE, "finish_vertex " << g[v] ); 00197 } 00198 00199 template<class EdgeDescriptor, class Graph> 00200 void examine_edge( EdgeDescriptor e, Graph& g ) 00201 { 00202 TUTTLE_TLOG( TUTTLE_TRACE, "examine_edge " << g[e] ); 00203 } 00204 00205 template<class EdgeDescriptor, class Graph> 00206 void tree_edge( EdgeDescriptor e, Graph& g ) 00207 { 00208 TUTTLE_TLOG( TUTTLE_TRACE, "tree_edge " << g[e] ); 00209 } 00210 00211 template<class EdgeDescriptor, class Graph> 00212 void non_tree_edge( EdgeDescriptor e, Graph& g ) 00213 { 00214 TUTTLE_TLOG( TUTTLE_TRACE, "non_tree_edge " << g[e] ); 00215 } 00216 00217 template<class EdgeDescriptor, class Graph> 00218 void gray_target( EdgeDescriptor e, Graph& g ) 00219 { 00220 TUTTLE_TLOG( TUTTLE_TRACE, "gray_target " << g[e] ); 00221 } 00222 00223 template<class EdgeDescriptor, class Graph> 00224 void black_target( EdgeDescriptor e, Graph& g ) 00225 { 00226 TUTTLE_TLOG( TUTTLE_TRACE, "black_target " << g[e] ); 00227 } 00228 00229 }; 00230 00231 } 00232 } 00233 } 00234 } 00235 00236 #endif 00237