Belle II Software light-2406-ragdoll
ModuleCondition.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#include <framework/core/ModuleCondition.h>
9#include <framework/core/Path.h>
10#include <framework/utilities/Conversion.h>
11
12#include <boost/algorithm/string/erase.hpp>
13
14#include <boost/python/class.hpp>
15#include <boost/python/docstring_options.hpp>
16
17#include <map>
18#include <utility>
19
20using namespace Belle2;
21using namespace boost::python;
22
23ModuleCondition::ModuleCondition(std::string expression, PathPtr conditionPath, EAfterConditionPath afterConditionPath):
24 m_conditionPath(std::move(conditionPath)),
25 m_afterConditionPath(afterConditionPath)
26{
28 std::map<std::string, EConditionOperators> m_conditionOperatorMap;
29 m_conditionOperatorMap[">"] = c_GT;
30 m_conditionOperatorMap["<"] = c_ST;
31 m_conditionOperatorMap[">="] = c_GE;
32 m_conditionOperatorMap["<="] = c_SE;
33 m_conditionOperatorMap["="] = c_EQ;
34 m_conditionOperatorMap["=="] = c_EQ;
35 m_conditionOperatorMap["!="] = c_NE;
36
37 //Remove all spaces from the string
38 boost::erase_all(expression, " ");
39
40 //Find the operator
41 unsigned int iOperator = 0;
42 while ((iOperator < expression.length()) && (!isdigit(expression[iOperator]) && (expression[iOperator] != '-'))) iOperator++;
43 if (iOperator == 0) //if iOperator is 0, there is either no operator or the string is empty
44 throw std::runtime_error("Invalid condition: could not parse condition: '" + expression + "'!");
45 std::string opString = expression.substr(0, iOperator);
46
47 //Try to get the operator for the found string from the map
48 auto foundIter = m_conditionOperatorMap.find(opString);
49 if (foundIter == m_conditionOperatorMap.end())
50 throw std::runtime_error("Invalid condition: could not parse condition: '" + expression + "'!");
51
52 m_conditionOperator = foundIter->second;
53 //Try to translate the remaining text to a number
54 m_conditionValue = convertString<int>(expression.substr(iOperator, expression.length() - 1));
55}
56
57bool ModuleCondition::evaluate(int value) const
58{
59 switch (m_conditionOperator) {
60 case c_GT : return value > m_conditionValue;
61 case c_ST : return value < m_conditionValue;
62 case c_GE : return value >= m_conditionValue;
63 case c_SE : return value <= m_conditionValue;
64 case c_EQ : return value == m_conditionValue;
65 case c_NE : return value != m_conditionValue;
66 }
67 return false;
68}
69
70std::string ModuleCondition::getString() const
71{
72 std::string output = "(? ";
73 switch (m_conditionOperator) {
74 case c_GT: output += ">"; break;
75 case c_ST: output += "<"; break;
76 case c_GE: output += ">="; break;
77 case c_SE: output += "<="; break;
78 case c_NE: output += "!="; break;
79 case c_EQ: output += "=="; break;
80 default: output += "???";
81 }
82 output += std::to_string(m_conditionValue);
83 output += m_conditionPath->getPathString();
84 output += " )";
85 return output;
86}
87
88namespace {
90 std::shared_ptr<Path> _getPathPython(ModuleCondition* m) {return m->getPath(); };
91}
92
94{
95 docstring_options options(true, true, false); //userdef, py sigs, c++ sigs
96
97 //Python class definition
98 class_<ModuleCondition, boost::noncopyable>("ModuleCondition", no_init)
99 .def("__str__", &ModuleCondition::getString)
100 .def("get_value", &ModuleCondition::getConditionValue)
101 .def("get_operator", &ModuleCondition::getConditionOperator)
102 .def("get_after_path", &ModuleCondition::getAfterConditionPath)
103 .def("get_path", &_getPathPython)
104 ;
105}
Wraps a condition set on a Module instance.
bool evaluate(int value) const
evaluate the condition using the given value.
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).
@ 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: "<"
std::string getString() const
A string representation of this condition.
EConditionOperators getConditionOperator() const
Returns the value of the condition.
int m_conditionValue
Numeric value used in the condition (set by parsing the condition expression).
int getConditionValue() const
Returns the value of the condition.
EAfterConditionPath getAfterConditionPath() const
What to do after a conditional path is finished.
static void exposePythonAPI()
Exposes methods of the ModuleCondition class to Python.
ModuleCondition()=delete
no default constructed objects.
std::shared_ptr< Path > PathPtr
Defines a pointer to a path object as a boost shared pointer.
Definition: Path.h:35
Abstract base class for different kinds of events.
Definition: ClusterUtils.h:24
STL namespace.