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