artdaq_mfextensions  v1_06_02
ma_boolean_cond.cpp
1 
2 #include "ErrorHandler/MessageAnalyzer/ma_boolean_cond.h"
3 #include "ErrorHandler/MessageAnalyzer/ma_boolean_expr.h"
4 
5 using namespace novadaq::errorhandler;
6 
7 template<typename T>
8 bool compare(compare_op_t op, T v, T rhv)
9 {
10  switch (op)
11  {
12  case CO_L:
13  return (v < rhv);
14  case CO_LE:
15  return (v <= rhv);
16  case CO_E:
17  return (v == rhv);
18  case CO_NE:
19  return (v != rhv);
20  case CO_GE:
21  return (v >= rhv);
22  case CO_G:
23  return (v > rhv);
24  default:
25  return false;
26  }
27 }
28 
29 typedef std::vector<boost::any> anys_t;
30 
31 void ma_boolean_cond::insert_ext_func(cond_idx_t ci, arg_t arg, anys_t const& func_args, std::string const& function)
32 {
33  cond_arg.first = ci;
34  cond_arg.second = arg;
35  cond_type = FUNCTION;
36  ext_func.reset(ma_function_factory::create_instance(function));
37 
38  if (!ext_func->parse_arguments(func_args))
39  throw std::runtime_error("arguments rejected by " + function);
40 }
41 
42 void ma_boolean_cond::reset()
43 {
44  if (cond_type == EXPR)
45  {
46  // expression must not be null
47  assert(expr.get() != NULL);
48  return expr->reset();
49  }
50 
51  if (cond_type == COND)
52  {
53  return;
54  }
55 
56  if (cond_type >= FUNCTION)
57  {
58  // custom function must not be null
59  assert(ext_func.get() != NULL);
60 
61  return ext_func->reset();
62  }
63 
64  throw std::runtime_error("ma_boolean_cond::reset(): unknow cond_type");
65 }
66 
67 bool ma_boolean_cond::evaluate(ma_domain& value, ma_domain& alarm, ma_domain const& domain) const
68 {
69  if (cond_type == EXPR)
70  {
71  // expression must not be null
72  assert(expr.get() != NULL);
73 
74  // evaluate from the expr
75  return expr->evaluate(value, alarm, domain);
76  }
77 
78  if (cond_type == COND)
79  {
80  cond_idx_t cond_idx = cond_arg.first;
81 
82  // condition ptr must not be null
83  assert(cond_idx.first != NULL);
84 
85  // update alarm
86  if (domain_is_null(alarm[cond_idx.second]))
87  alarm[cond_idx.second] = value[cond_idx.second];
88 
89  // get status from hitmap of the condition
90  return cond_idx.first->get_status(value[cond_idx.second]);
91  }
92 
93  if (cond_type >= FUNCTION)
94  {
95  cond_idx_t cond_idx = cond_arg.first;
96 
97  // condition ptr must not be null
98  assert(cond_idx.first != NULL);
99 
100  // custom function must not be null
101  assert(ext_func.get() != NULL);
102 
103  // update alarm
104  if (ext_func->grouped_alarm())
105  {
106  alarm[cond_idx.second] = domain[cond_idx.second];
107  }
108  else
109  {
110  alarm[cond_idx.second] =
111  ma_cond_domain(cond_idx.first->find_source(cond_idx.first->get_msg_source()), cond_idx.first->find_target(cond_idx.first->get_qt_mf_msgarget()));
112  }
113 
114  // evaluate
115  boost::any v = ext_func->evaluate(*(cond_idx.first), domain[cond_idx.second]);
116  double d;
117  bool b;
118  std::string s;
119 
120  switch (cond_type)
121  {
122  case FUNCTION:
123  b = boost::any_cast<bool>(v);
124  return b;
125 
126  case FUNCTION_BOOL:
127  b = boost::any_cast<bool>(v);
128  return compare(op, b, rhv_b);
129 
130  case FUNCTION_STRING:
131  s = boost::any_cast<std::string>(v);
132  return compare(op, s, rhv_s);
133 
134  case FUNCTION_DOUBLE:
135  if (v.type() == typeid(int))
136  d = boost::any_cast<int>(v);
137  else
138  d = boost::any_cast<double>(v);
139  return compare(op, d, rhv_d);
140 
141  default:
142  break;
143  }
144  }
145 
146  throw std::runtime_error("ma_boolean_cond::evaluate(): unknow cond_type");
147 }
148 
149 void ma_boolean_cond::insert_expr(ma_boolean_expr const& b_expr)
150 {
151  expr.reset(new ma_boolean_expr(b_expr));
152  cond_type = EXPR;
153 }