Belle II Software development
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, std::shared_ptr<Path> conditionPath,
24 EAfterConditionPath afterConditionPath):
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}
57
58bool ModuleCondition::evaluate(int value) const
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}
70
71std::string ModuleCondition::getString() const
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}
88
89namespace {
91 std::shared_ptr<Path> _getPathPython(ModuleCondition* m) {return m->getPath(); };
92}
93
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}
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.
EAfterConditionPath
Different options for behaviour after a conditional path was executed.
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.
Abstract base class for different kinds of events.
STL namespace.