Belle II Software development
ModuleCondition Class Reference

Wraps a condition set on a Module instance. More...

#include <ModuleCondition.h>

Public Types

enum  EConditionOperators {
  c_GT ,
  c_ST ,
  c_GE ,
  c_SE ,
  c_EQ ,
  c_NE
}
 The supported condition operators. More...
 
enum class  EAfterConditionPath {
  c_End ,
  c_Continue
}
 Different options for behaviour after a conditional path was executed. More...
 

Public Member Functions

 ModuleCondition (std::string expression, std::shared_ptr< Path > conditionPath, EAfterConditionPath afterConditionPath)
 initialize from string expression (see class doc).
 
 ~ModuleCondition ()=default
 Destructor, nothing to see here.
 
 ModuleCondition (const ModuleCondition &other)=default
 default copy constructor.
 
ModuleConditionoperator= (const ModuleCondition &other)=default
 and default assignment operator
 
bool evaluate (int value) const
 evaluate the condition using the given value.
 
const std::shared_ptr< Path > & getPath () const
 Returns the path of the condition.
 
int getConditionValue () const
 Returns the value of the condition.
 
EConditionOperators getConditionOperator () const
 Returns the value of the condition.
 
EAfterConditionPath getAfterConditionPath () const
 What to do after a conditional path is finished.
 
std::string getString () const
 A string representation of this condition.
 

Static Public Member Functions

static void exposePythonAPI ()
 Exposes methods of the ModuleCondition class to Python.
 

Private Member Functions

 ModuleCondition ()=delete
 no default constructed objects.
 

Private Attributes

std::shared_ptr< Pathm_conditionPath
 The path which which will be executed if the condition is evaluated to true.
 
EConditionOperators m_conditionOperator
 The operator of the condition (set by parsing the condition expression).
 
int m_conditionValue
 Numeric value used in the condition (set by parsing the condition expression).
 
EAfterConditionPath m_afterConditionPath
 What to do after a conditional path is finished.
 

Detailed Description

Wraps a condition set on a Module instance.

It supports conditions of the form [comparison operator][integer] e.g. ">5", "=7", "!= 0"

Additional spaces in front of the operator, between operator and integer number and after the integer number are allowed.

Supported operators are: "> , < , = , == , >= , <= , !="

Definition at line 29 of file ModuleCondition.h.

Member Enumeration Documentation

◆ EAfterConditionPath

enum class EAfterConditionPath
strong

Different options for behaviour after a conditional path was executed.

Enumerator
c_End 

End current event after the conditional path.

c_Continue 

After the conditional path, resume execution after this module.

Definition at line 42 of file ModuleCondition.h.

42 {
43 c_End,
45 };
@ c_End
End current event after the conditional path.
@ c_Continue
After the conditional path, resume execution after this module.

◆ EConditionOperators

The supported condition operators.

Enumerator
c_GT 

Greater than: ">"

c_ST 

Smaller than: "<"

c_GE 

Greater or equal than: ">=".

c_SE 

Smaller or equal than: "<=".

c_EQ 

Equal: "=" or "=="

c_NE 

Not equal: "!=".

Definition at line 32 of file ModuleCondition.h.

32 {
33 c_GT,
34 c_ST,
35 c_GE,
36 c_SE,
37 c_EQ,
38 c_NE
39 };
@ c_GE
Greater or equal than: ">=".
@ c_SE
Smaller or equal than: "<=".
@ c_GT
Greater than: ">"
@ c_NE
Not equal: "!=".
@ c_EQ
Equal: "=" or "=="
@ c_ST
Smaller than: "<"

Constructor & Destructor Documentation

◆ ModuleCondition()

ModuleCondition ( std::string  expression,
std::shared_ptr< Path conditionPath,
EAfterConditionPath  afterConditionPath 
)

initialize from string expression (see class doc).

Throws runtime_error if expression is invalid.

Maps the operator string to a EConditionOperators value.

Definition at line 23 of file ModuleCondition.cc.

24 :
25 m_conditionPath(std::move(conditionPath)),
26 m_afterConditionPath(afterConditionPath)
27{
29 std::map<std::string, EConditionOperators> m_conditionOperatorMap;
30 m_conditionOperatorMap[">"] = c_GT;
31 m_conditionOperatorMap["<"] = c_ST;
32 m_conditionOperatorMap[">="] = c_GE;
33 m_conditionOperatorMap["<="] = c_SE;
34 m_conditionOperatorMap["="] = c_EQ;
35 m_conditionOperatorMap["=="] = c_EQ;
36 m_conditionOperatorMap["!="] = c_NE;
37
38 //Remove all spaces from the string
39 boost::erase_all(expression, " ");
40
41 //Find the operator
42 unsigned int iOperator = 0;
43 while ((iOperator < expression.length()) && (!isdigit(expression[iOperator]) && (expression[iOperator] != '-'))) iOperator++;
44 if (iOperator == 0) //if iOperator is 0, there is either no operator or the string is empty
45 throw std::runtime_error("Invalid condition: could not parse condition: '" + expression + "'!");
46 std::string opString = expression.substr(0, iOperator);
47
48 //Try to get the operator for the found string from the map
49 auto foundIter = m_conditionOperatorMap.find(opString);
50 if (foundIter == m_conditionOperatorMap.end())
51 throw std::runtime_error("Invalid condition: could not parse condition: '" + expression + "'!");
52
53 m_conditionOperator = foundIter->second;
54 //Try to translate the remaining text to a number
55 m_conditionValue = convertString<int>(expression.substr(iOperator, expression.length() - 1));
56}
std::shared_ptr< Path > m_conditionPath
The path which which will be executed if the condition is evaluated to true.
EConditionOperators m_conditionOperator
The operator of the condition (set by parsing the condition expression).
EAfterConditionPath m_afterConditionPath
What to do after a conditional path is finished.
int m_conditionValue
Numeric value used in the condition (set by parsing the condition expression).

Member Function Documentation

◆ evaluate()

bool evaluate ( int  value) const

evaluate the condition using the given value.

E.g. for a condition ">5", this would return "value>5"

Definition at line 58 of file ModuleCondition.cc.

59{
60 switch (m_conditionOperator) {
61 case c_GT : return value > m_conditionValue;
62 case c_ST : return value < m_conditionValue;
63 case c_GE : return value >= m_conditionValue;
64 case c_SE : return value <= m_conditionValue;
65 case c_EQ : return value == m_conditionValue;
66 case c_NE : return value != m_conditionValue;
67 }
68 return false;
69}

◆ exposePythonAPI()

void exposePythonAPI ( )
static

Exposes methods of the ModuleCondition class to Python.

Definition at line 94 of file ModuleCondition.cc.

95{
96 docstring_options options(true, true, false); //userdef, py sigs, c++ sigs
97
98 //Python class definition
99 class_<ModuleCondition, boost::noncopyable>("ModuleCondition", no_init)
100 .def("__str__", &ModuleCondition::getString)
101 .def("get_value", &ModuleCondition::getConditionValue)
102 .def("get_operator", &ModuleCondition::getConditionOperator)
103 .def("get_after_path", &ModuleCondition::getAfterConditionPath)
104 .def("get_path", &_getPathPython)
105 ;
106}
std::string getString() const
A string representation of this condition.
EConditionOperators getConditionOperator() const
Returns the value of the condition.
int getConditionValue() const
Returns the value of the condition.
EAfterConditionPath getAfterConditionPath() const
What to do after a conditional path is finished.

◆ getAfterConditionPath()

EAfterConditionPath getAfterConditionPath ( ) const
inline

What to do after a conditional path is finished.

Definition at line 69 of file ModuleCondition.h.

69{ return m_afterConditionPath; }

◆ getConditionOperator()

EConditionOperators getConditionOperator ( ) const
inline

Returns the value of the condition.


Definition at line 66 of file ModuleCondition.h.

66{return m_conditionOperator; }

◆ getConditionValue()

int getConditionValue ( ) const
inline

Returns the value of the condition.


Definition at line 63 of file ModuleCondition.h.

63{return m_conditionValue; }

◆ getPath()

const std::shared_ptr< Path > & getPath ( ) const
inline

Returns the path of the condition.


Definition at line 60 of file ModuleCondition.h.

60{return m_conditionPath; }

◆ getString()

std::string getString ( ) const

A string representation of this condition.

Definition at line 71 of file ModuleCondition.cc.

72{
73 std::string output = "(? ";
74 switch (m_conditionOperator) {
75 case c_GT: output += ">"; break;
76 case c_ST: output += "<"; break;
77 case c_GE: output += ">="; break;
78 case c_SE: output += "<="; break;
79 case c_NE: output += "!="; break;
80 case c_EQ: output += "=="; break;
81 default: output += "???";
82 }
83 output += std::to_string(m_conditionValue);
84 output += m_conditionPath->getPathString();
85 output += " )";
86 return output;
87}

Member Data Documentation

◆ m_afterConditionPath

EAfterConditionPath m_afterConditionPath
private

What to do after a conditional path is finished.

Definition at line 86 of file ModuleCondition.h.

◆ m_conditionOperator

EConditionOperators m_conditionOperator
private

The operator of the condition (set by parsing the condition expression).

Definition at line 84 of file ModuleCondition.h.

◆ m_conditionPath

std::shared_ptr<Path> m_conditionPath
private

The path which which will be executed if the condition is evaluated to true.

Definition at line 83 of file ModuleCondition.h.

◆ m_conditionValue

int m_conditionValue
private

Numeric value used in the condition (set by parsing the condition expression).

Definition at line 85 of file ModuleCondition.h.


The documentation for this class was generated from the following files: