artdaq_mfextensions  v1_05_00
ma_rule.h
1 #ifndef ERROR_HANDLER_MA_RULE_H
2 #define ERROR_HANDLER_MA_RULE_H
3 
4 // from novadaq
5 #include "ErrorHandler/MessageAnalyzer/ma_utils.h"
6 #include "ErrorHandler/MessageAnalyzer/ma_condition.h"
7 #include "ErrorHandler/MessageAnalyzer/ma_domain_expr.h"
8 #include "ErrorHandler/MessageAnalyzer/ma_boolean_expr.h"
9 #include "ErrorHandler/MessageAnalyzer/ma_richmsg.h"
10 #include "ErrorHandler/MessageAnalyzer/ma_action.h"
11 
12 // from ups
13 #include <boost/shared_ptr.hpp>
14 #include <fhiclcpp/ParameterSet.h>
15 
16 // sys headers
17 #include <map>
18 #include <sys/time.h>
19 
20 namespace novadaq {
21 namespace errorhandler {
22 
23 // MsgAnalyzer Rule
24 class ma_rule
25 {
26 public:
27 
28  // c'tor
29  ma_rule( std::string const & name
30  , std::string const & desc
31  , bool repeat
32  , int holdoff_time = 0 );
33 
34  // ----------------------------------------------------------------
35  //
36 
37  // public method, call to initialze the rule
38  void
39  parse( std::string const & cond_expr
40  , std::string const & alarm_message
41  , fhicl::ParameterSet const & actions
42  , cond_map_t * cond_map_ptr );
43 
44  // public method, call to evaluate the domain expression
45  void
46  evaluate_domain( );
47 
48  // public method, call to evaluate the boolean expression
49  bool
50  evaluate( );
51 
52  // carry out actions
53  int
54  act( );
55 
56  // public method, get the alarm
57  ma_domain const &
58  get_alarm( ) const;
59 
60  // public method, get the alarm message
61  std::string
62  get_alarm_message( );
63 
64  // number of alarms. if the repeatable alarm flag is true, it is the
65  // number of total alarms; otherwise it is the number of distinguishable
66  // alarms (one domain only alarms once)
67  int
68  get_alarm_count() const { return alarm_count; }
69 
70  // get fields
71  const std::string & name() const { return name_;}
72  const std::string & description() const { return description_; }
73  const std::string & cond_expr() const { return condition_expr; }
74  const std::string & alarm_message() const { return alarm_msg.plain_message(); }
75 
76  const std::vector<std::string> & cond_names() const { return cond_names_; }
77 
78  // enable/disable the rule
79  void
80  enable( bool flag ) { enabled = flag; }
81 
82  // reset the rule to its ground state ( reset alarms and domains )
83  void
84  reset( );
85 
86  // ----------------------------------------------------------------
87  //
88 
89  // called by the parser to set the boolean expression
90  void set_boolean_expr( ma_boolean_expr const & expr )
91  { boolean_expr = expr; }
92 
93  // called by the parser to set the domain expression
94  void set_domain_expr( ma_domain_expr const & expr )
95  { domain_expr = expr; }
96 
97  // called by the parser to push a cond_ptr to the container
98  cond_idx_t
99  insert_condition_ptr( std::string const & name, bool primitive );
100 
101  // ----------------------------------------------------------------
102  //
103  // get condition index and pointer given a name
104  cond_idx_t
105  get_cond_idx( std::string const & name ) const;
106 
107  // get pointer to the condition
108  ma_condition *
109  get_cond( std::string const & name ) const;
110 
111  // get index to the condition
112  size_t
113  get_idx( std::string const & name ) const;
114 
115  // get the size of condition container
116  size_t
117  get_cond_size() const;
118 
119  // update the "notify_on_source" or "notify_on_target" list
120  // for corresponding conditions
121  void
122  update_notify_list( std::string const & name, arg_t arg );
123 
124 public:
125 
126  cond_vec_t conditions;
127  idx_t conditions_idx;
128  std::vector<bool> primitive_cond;
129 
130 private:
131 
132  // recursive evaluation function
133  // value: specific value set in the given domain
134  // domain: the input domain where values are allowed
135  // n: depth of the recursion
136  // return: true if new alarm found
137  bool
138  recursive_evaluate ( ma_domain & value
139  , ma_domain & alarm
140  , ma_domain const & domain
141  , size_t n );
142 
143  // evaluate the boolean expression with a given set of inputs
144  // value: the input values for each condition
145  bool
146  boolean_evaluate ( ma_domain & value
147  , ma_domain & alarm
148  , ma_domain const & domain );
149 
150  bool
151  parse_alarm_message ( std::string const & s );
152 
153  bool
154  parse_alarm_ref( std::string const & s );
155 
156 private:
157 
158  // a pointer to the condition container containing all conditions in the app
159  // the original container is hold in the ma_rule_engine class
160  cond_map_t * cond_map;
161 
162  std::string name_;
163  std::string description_;
164  std::string condition_expr;
165  int alarm_count;
166 
167  std::vector<std::string> cond_names_; // vector of strings holding the cond name list
168 
169  ma_richmsg alarm_msg;
170 
171  ma_boolean_expr boolean_expr;
172  ma_domain_expr domain_expr;
173 
174  ma_domains domains;
175 
176  std::map<ma_domain, timeval> alarms;
177  std::map<ma_domain, timeval>::const_iterator itor_last_alarm;
178 
179  bool repeat_alarm;
180  int holdoff;
181 
182  bool initialized;
183  bool enabled;
184 
185  ma_actions actions;
186 };
187 
188 typedef boost::shared_ptr<ma_rule> rule_sp;
189 typedef std::map<std::string, ma_rule> rule_map_t;
190 
191 } // end of namespace errorhandler
192 } // end of namespace novadaq
193 
194 #endif