The Gaudi Framework  v29r0 (ff2e7097)
ControlFlowGraph.h
Go to the documentation of this file.
1 #ifndef GAUDIHIVE_CONTROLFLOWGRAPH_H
2 #define GAUDIHIVE_CONTROLFLOWGRAPH_H
3 
4 // std includes
5 #include <sstream>
6 #include <unordered_map>
7 #include <vector>
8 
9 // local includes
10 #include "AlgsExecutionStates.h"
11 
12 // Framework includes
13 #include "GaudiKernel/Algorithm.h"
15 
16 namespace concurrency
17 {
18 
19  namespace recursive_CF
20  {
21 
23  class ControlFlowGraph;
24 
26  {
27  public:
29  ControlFlowNode( ControlFlowGraph& graph, unsigned int nodeIndex, const std::string& name )
30  : m_graph( &graph ), m_nodeIndex( nodeIndex ), m_nodeName( name )
31  {
32  }
34  virtual ~ControlFlowNode() {}
36  virtual void initialize( const std::unordered_map<std::string, unsigned int>& algname_index_map ) = 0;
38  virtual int updateState( AlgsExecutionStates& states, std::vector<int>& node_decisions ) const = 0;
41  const std::vector<int>& node_decisions, const unsigned int& recursionLevel ) const = 0;
43  const unsigned int& getNodeIndex() const { return m_nodeIndex; }
45  const std::string& getNodeName() const { return m_nodeName; }
46 
47  public:
49 
50  protected:
52  std::string stateToString( const int& stateId ) const;
53  unsigned int m_nodeIndex;
55  };
56 
58  {
59  public:
61  DecisionNode( ControlFlowGraph& graph, unsigned int nodeIndex, const std::string& name, bool modeConcurrent,
62  bool modePromptDecision, bool modeOR, bool allPass )
63  : ControlFlowNode( graph, nodeIndex, name )
64  , m_modeConcurrent( modeConcurrent )
65  , m_modePromptDecision( modePromptDecision )
66  , m_modeOR( modeOR )
67  , m_allPass( allPass )
68  , m_children()
69  {
70  }
72  ~DecisionNode() override;
74  void initialize( const std::unordered_map<std::string, unsigned int>& algname_index_map ) override;
76  int updateState( AlgsExecutionStates& states, std::vector<int>& node_decisions ) const override;
78  void addDaughterNode( ControlFlowNode* node );
80  const std::vector<ControlFlowNode*>& getDaughters() const { return m_children; }
82  void printState( std::stringstream& output, AlgsExecutionStates& states, const std::vector<int>& node_decisions,
83  const unsigned int& recursionLevel ) const override;
84 
85  public:
92  bool m_modeOR;
94  bool m_allPass;
95 
96  private:
99  };
100 
102  {
103  public:
105  AlgorithmNode( ControlFlowGraph& graph, Algorithm* algoPtr, unsigned int nodeIndex, unsigned int algoIndex,
106  bool inverted, bool allPass )
107  : ControlFlowNode( graph, nodeIndex, algoPtr->name() )
108  , m_algoIndex( algoIndex )
109  , m_algoName( algoPtr->name() )
110  , m_inverted( inverted )
111  , m_allPass( allPass )
112  , m_algorithm( algoPtr ){};
113 
115  void initialize( const std::unordered_map<std::string, unsigned int>& algname_index_map ) override;
117  Algorithm* getAlgorithm() const { return m_algorithm; }
119  int updateState( AlgsExecutionStates& states, std::vector<int>& node_decisions ) const override;
121  void printState( std::stringstream& output, AlgsExecutionStates& states, const std::vector<int>& node_decisions,
122  const unsigned int& recursionLevel ) const override;
123 
124  private:
126  unsigned int m_algoIndex;
132  bool m_allPass;
135  };
136 
139 
141  virtual ~IControlFlowGraph() = default;
142  };
143 
144  class ControlFlowGraph : public CommonMessaging<IControlFlowGraph>
145  {
146  public:
148  ControlFlowGraph( const std::string& name, SmartIF<ISvcLocator> svc ) : m_svcLocator( svc ), m_name( name ) {}
150  ~ControlFlowGraph() override
151  {
152  if ( m_headNode != 0 ) delete m_headNode;
153  }
154 
156  void initialize( const std::unordered_map<std::string, unsigned int>& algname_index_map );
158  void addHeadNode( const std::string& headName, bool modeConcurrent, bool modePromptDecision, bool modeOR,
159  bool allPass );
161  DecisionNode* getHeadNode() const { return m_headNode; };
163  StatusCode addAlgorithmNode( Algorithm* daughterAlgo, const std::string& parentName, bool inverted,
164  bool allPass );
166  StatusCode addDecisionHubNode( Algorithm* daughterAlgo, const std::string& parentName, bool modeConcurrent,
167  bool modePromptDecision, bool modeOR, bool allPass );
169  unsigned int getControlFlowNodeCounter() const { return m_nodeCounter; }
171  void updateEventState( AlgsExecutionStates& states, std::vector<int>& node_decisions ) const;
174  const unsigned int& recursionLevel ) const
175  {
176  m_headNode->printState( output, states, node_decisions, recursionLevel );
177  }
179  const std::string& name() const override { return m_name; }
181  SmartIF<ISvcLocator>& serviceLocator() const override { return m_svcLocator; }
183  std::string dumpControlFlow() const;
185  void dumpControlFlow( std::ostringstream&, ControlFlowNode*, const int& ) const;
186 
187  private:
189  DecisionNode* m_headNode{nullptr};
195  unsigned int m_nodeCounter{0};
197  unsigned int m_algoCounter{0};
201  };
202 
203  } // namespace recursive_CF
204 } // namespace concurrency
205 
206 #endif
DecisionNode(ControlFlowGraph &graph, unsigned int nodeIndex, const std::string &name, bool modeConcurrent, bool modePromptDecision, bool modeOR, bool allPass)
Constructor.
virtual void printState(std::stringstream &output, AlgsExecutionStates &states, const std::vector< int > &node_decisions, const unsigned int &recursionLevel) const =0
Print a string representing the control flow state.
std::unordered_map< std::string, DecisionNode * > DecisionHubsMap
std::vector< ControlFlowNode * > m_children
All direct daughter nodes in the tree.
Algorithm * m_algorithm
Algorithm representative behind the AlgorithmNode.
const std::string & getNodeName() const
Get node name.
std::string stateToString(const int &stateId) const
Translation between state id and name.
const unsigned int & getNodeIndex() const
Get node index.
bool m_modePromptDecision
Whether to evaluate the hub decision ASA its child decisions allow to do that.
unsigned int getControlFlowNodeCounter() const
Get total number of graph nodes.
bool m_allPass
Whether always passing regardless of daughter results.
Algorithm * getAlgorithm() const
get Algorithm representatives
AlgsExecutionStates::State State
virtual int updateState(AlgsExecutionStates &states, std::vector< int > &node_decisions) const =0
Method to set algos to CONTROLREADY, if possible.
std::string m_algoName
The name of the algorithm.
STL class.
const std::string & name() const override
Retrieve name of the service.
The AlgsExecutionStates encodes the state machine for the execution of algorithms within a single eve...
AlgorithmNode(ControlFlowGraph &graph, Algorithm *algoPtr, unsigned int nodeIndex, unsigned int algoIndex, bool inverted, bool allPass)
Constructor.
const std::vector< ControlFlowNode * > & getDaughters() const
ControlFlowNode(ControlFlowGraph &graph, unsigned int nodeIndex, const std::string &name)
Constructor.
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:28
virtual void initialize(const std::unordered_map< std::string, unsigned int > &algname_index_map)=0
Initialize.
bool m_allPass
Whether the selection result is relevant or always "pass".
bool m_inverted
Whether the selection result is negated or not.
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:78
DecisionNode * getHeadNode() const
Get head node.
void printState(std::stringstream &output, AlgsExecutionStates &states, const std::vector< int > &node_decisions, const unsigned int &recursionLevel) const
Print a string representing the control flow state.
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
AlgoNodesMap m_algoNameToAlgoNodeMap
Index: map of algorithm&#39;s name to AlgorithmNode.
DecisionHubsMap m_decisionNameToDecisionHubMap
Index: map of decision&#39;s name to DecisionHub.
unsigned int m_algoIndex
The index of the algorithm.
ControlFlowGraph(const std::string &name, SmartIF< ISvcLocator > svc)
Constructor.
State
Execution states of the algorithms.
std::unordered_map< std::string, AlgorithmNode * > AlgoNodesMap
bool m_modeOR
Whether acting as "and" (false) or "or" node (true)
bool m_modeConcurrent
Whether all daughters will be evaluated concurrently or sequentially.
SmartIF< ISvcLocator > m_svcLocator
Service locator (needed to access the MessageSvc)