Graph.h
Go to the documentation of this file.
1 /** A simple DFP engine meant for single-threaded execution.
2 
3  A node is constructed with zero or more ports.
4 
5  A port mediates between a node and an edge.
6 
7  A port is either of type input or output.
8 
9  In the context of a node, a port has a name and an index into an
10  ordered list of other ports of a given type.
11 
12  An edge is a queue passing data objects in one direction from its
13  tail (input) end to its head (output) end.
14 
15  Each end of an edge is exclusively "plugged" into one node through
16  one port.
17 
18  A valid graph consists of nodes with all ports plugged to edges.
19 
20  */
21 
22 #ifndef WIRECELL_PGRAPH_GRAPH
23 #define WIRECELL_PGRAPH_GRAPH
24 
25 #include "WireCellPgraph/Node.h"
26 #include "WireCellUtil/Logging.h"
27 
28 #include <vector>
29 #include <unordered_set>
30 #include <unordered_map>
31 
32 namespace WireCell {
33  namespace Pgraph {
34 
35  class Graph {
36  public:
37  Graph();
38 
39  // Add a node to the graph.
40  void add_node(Node* node);
41 
42  // Connect two nodes by their given ports. Return false
43  // if they are incompatible. new nodes will be implicitly
44  // added to the graph.
45  bool connect(Node* tail, Node* head,
46  size_t tpind=0, size_t hpind=0);
47 
48  // return a topological sort of the graph as per Kahn algorithm.
49  std::vector<Node*> sort_kahn();
50 
51  // Excute the graph until nodes stop delivering
52  bool execute();
53 
54  // Excute parents of node or if any parent is not ready,
55  // recursively call this method on parent. Return number
56  // of nodes executed.
57  int execute_upstream(Node* node);
58 
59  // All internal calling of nodes goes through here.
60  bool call_node(Node* node);
61 
62  // Return false if any node is not connected.
63  bool connected();
64 
65  private:
66  std::vector<std::pair<Node*,Node*> > m_edges;
67  std::unordered_set<Node*> m_nodes;
68  std::unordered_map< Node*, std::vector<Node*> > m_edges_forward,
71  };
72 }
73 }
74 #endif
std::unordered_set< Node * > m_nodes
Definition: Graph.h:67
void add_node(Node *node)
Definition: Graph.cxx:16
bool call_node(Node *node)
Definition: Graph.cxx:134
std::vector< Node * > sort_kahn()
Definition: Graph.cxx:55
std::vector< std::pair< Node *, Node * > > m_edges
Definition: Graph.h:66
bool connect(Node *tail, Node *head, size_t tpind=0, size_t hpind=0)
Definition: Graph.cxx:21
std::unordered_map< Node *, std::vector< Node * > > m_edges_backward
Definition: Graph.h:68
Log::logptr_t l
Definition: Graph.h:70
std::shared_ptr< spdlog::logger > logptr_t
Definition: Logging.h:24
Definition: Main.h:22
int execute_upstream(Node *node)
Definition: Graph.cxx:88
std::unordered_map< Node *, std::vector< Node * > > m_edges_forward
Definition: Graph.h:68