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