Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
GraphNode.hxx
Go to the documentation of this file.
1 // Author: Enrico Guiraud, Danilo Piparo, CERN, Massimo Tumolo Politecnico di Torino 08/2018
2 
3 /*************************************************************************
4  * Copyright (C) 1995-2018, Rene Brun and Fons Rademakers. *
5  * All rights reserved. *
6  * *
7  * For the licensing terms see $ROOTSYS/LICENSE. *
8  * For the list of contributors see $ROOTSYS/README/CREDITS. *
9  *************************************************************************/
10 
11 #ifndef ROOT_RDF_GRAPHNODE
12 #define ROOT_RDF_GRAPHNODE
13 
14 #include <string>
15 #include <memory>
16 #include <vector>
17 #include "TString.h"
18 
19 #include <iostream>
20 
21 namespace ROOT {
22 namespace Internal {
23 namespace RDF {
24 namespace GraphDrawing {
25 
26 class GraphCreatorHelper;
27 
28 // clang-format off
29 /**
30 \class ROOT::Internal::RDF::GraphNode
31 \ingroup dataframe
32 \brief Class used to create the operation graph to be printed in the dot representation
33 
34  This represent a single node of the overall graph. Each node maps the real RNode keeping just
35  the name and the columns defined up to that point.
36 */
37 // clang-format on
38 class GraphNode {
39  friend class GraphCreatorHelper;
40 
41 private:
42  unsigned int fCounter; ///< Nodes may share the same name (e.g. Filter). To manage this situation in dot, each node
43  ///< is represented by an unique id.
44  std::string fName, fColor, fShape;
45  std::vector<std::string>
46  fDefinedColumns; ///< Columns defined up to this node. By checking the defined columns between two consecutive
47  ///< nodes, it is possible to know if there was some Define in between.
48  std::shared_ptr<GraphNode> fPrevNode;
49 
50  bool fIsExplored = false; ///< When the graph is reconstructed, the first time this node has been explored this flag
51  ///< is set and it won't be explored anymore
52  bool fIsNew = true; ///< A just created node. This means that in no other exploration the node was already created
53  ///< (this is needed because branches may share some common node).
54 
55  ////////////////////////////////////////////////////////////////////////////
56  /// \brief Returns a static variable to allow each node to retrieve its counter
57  static unsigned int &GetStaticGlobalCounter()
58  {
59  static unsigned int sGlobalCounter = 1;
60  return sGlobalCounter;
61  }
62 
63 public:
64  ////////////////////////////////////////////////////////////////////////////
65  /// \brief Creates a node with a name and a counter
66  GraphNode(const std::string_view &name) : fName(name) { fCounter = GetStaticGlobalCounter()++; }
67 
68  ////////////////////////////////////////////////////////////////////////////
69  /// \brief Resets the counter.
70  /// This is not strictly needed but guarantees that two consecutive request to the graph return the same result.
71  static void ClearCounter() { GraphNode::GetStaticGlobalCounter() = 1; }
72 
73  ////////////////////////////////////////////////////////////////////////////
74  /// \brief Appends a node on the head of the current node
75  void SetPrevNode(const std::shared_ptr<GraphNode> &node) { fPrevNode = node; }
76 
77  ////////////////////////////////////////////////////////////////////////////
78  /// \brief Adds the column defined up to the node
79  void AddDefinedColumns(const std::vector<std::string> &columns) { fDefinedColumns = columns; }
80 
81  ////////////////////////////////////////////////////////////////////////////
82  /// \brief Gets the column defined up to the node
83  std::vector<std::string> GetDefinedColumns() { return fDefinedColumns; }
84 
85  ////////////////////////////////////////////////////////////////////////////
86  /// \brief Manually sets the counter to a node.
87  /// It is used by the root node to set its counter to zero.
88  void SetCounter(unsigned int counter) { fCounter = counter; }
89 
90  ////////////////////////////////////////////////////////////////////////////
91  /// \brief Allows to stop the graph traversal when an explored node is encountered
92  void SetIsExplored(bool isExplored) { fIsExplored = isExplored; }
93 
94  ////////////////////////////////////////////////////////////////////////////
95  /// \brief The node is considered just created
96  void SetIsNew(bool isNew) { fIsNew = isNew; }
97 
98  bool GetIsNew() { return fIsNew; }
99 
100  ////////////////////////////////////////////////////////////////////////////
101  /// \brief Gives a different shape based on the node type
102  void SetRoot()
103  {
104  fColor = "#e8f8fc";
105  fShape = "oval";
106  }
107 
108  ////////////////////////////////////////////////////////////////////////////
109  /// \brief Gives a different shape based on the node type
110  void SetFilter()
111  {
112  fColor = "#c4cfd4";
113  fShape = "diamond";
114  }
115 
116  ////////////////////////////////////////////////////////////////////////////
117  /// \brief Gives a different shape based on the node type
118  void SetDefine()
119  {
120  fColor = "#60aef3";
121  fShape = "oval";
122  }
123 
124  ////////////////////////////////////////////////////////////////////////////
125  /// \brief Gives a different shape based on the node type
126  void SetRange()
127  {
128  fColor = "#6F4D8F";
129  fShape = "diamond";
130  }
131 
132  ////////////////////////////////////////////////////////////////////////////
133  /// \brief Gives a different shape based on the node type
134  void SetAction(bool hasRun)
135  {
136  if (hasRun) {
137  fColor = "#baf1e5";
138  } else {
139  fColor = "#9cbbe5";
140  }
141  fShape = "box";
142  }
143 };
144 
145 } // namespace GraphDrawing
146 } // namespace RDF
147 } // namespace Internal
148 } // namespace ROOT
149 
150 #endif