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