Line data Source code
1 : #ifndef ERROR_HANDLER_MA_RULE_ENGINE_H
2 : #define ERROR_HANDLER_MA_RULE_ENGINE_H
3 :
4 : #include "ErrorHandler/MessageAnalyzer/ma_condition.h"
5 : #include "ErrorHandler/MessageAnalyzer/ma_participants.h"
6 : #include "ErrorHandler/MessageAnalyzer/ma_rule.h"
7 : #include "ErrorHandler/MessageAnalyzer/ma_types.h"
8 :
9 : #include <boost/thread.hpp>
10 : #include "ErrorHandler/MessageAnalyzer/ma_timing_event.h"
11 :
12 : namespace fhicl {
13 : class ParameterSet;
14 : }
15 :
16 : namespace novadaq {
17 : namespace errorhandler {
18 :
19 : class ma_rule_engine
20 : {
21 : public:
22 : // c'tor
23 : ma_rule_engine(fhicl::ParameterSet const& pset, alarm_fn_t alarm, cond_match_fn_t cond_match);
24 :
25 : // public method, call to run the rule engine
26 : void feed(qt_mf_msg const& msg);
27 :
28 : // public accessor for cond map and rule map
29 0 : size_t cond_size() const { return cmap.size(); }
30 0 : size_t rule_size() const { return rmap.size(); }
31 :
32 0 : const std::vector<std::string>& cond_names() const { return cnames; }
33 0 : const std::vector<std::string>& rule_names() const { return rnames; }
34 :
35 : bool is_EHS() const { return EHS; }
36 :
37 : // get the raw configuration ParameterSet object
38 : fhicl::ParameterSet
39 : get_configuration() const
40 : {
41 : return pset;
42 : }
43 :
44 : // get condition fields
45 : const std::string&
46 0 : cond_description(std::string const& name) const
47 : {
48 0 : return find_cond_by_name(name).description();
49 : }
50 :
51 : const std::string&
52 0 : cond_sources(std::string const& name) const
53 : {
54 0 : return find_cond_by_name(name).sources_str();
55 : }
56 :
57 : const std::string&
58 0 : cond_regex(std::string const& name) const
59 : {
60 0 : return find_cond_by_name(name).regex();
61 : }
62 :
63 : int
64 0 : cond_msg_count(std::string const& name) const
65 : {
66 0 : return find_cond_by_name(name).get_msg_count();
67 : }
68 :
69 : // get rule fields
70 : const std::string&
71 0 : rule_description(std::string const& name) const
72 : {
73 0 : return find_rule_by_name(name).description();
74 : }
75 :
76 : const std::string&
77 0 : rule_expr(std::string const& name) const
78 : {
79 0 : return find_rule_by_name(name).cond_expr();
80 : }
81 :
82 : const std::vector<std::string>&
83 0 : rule_cond_names(std::string const& name) const
84 : {
85 0 : return find_rule_by_name(name).cond_names();
86 : }
87 :
88 : int
89 0 : rule_alarm_count(std::string const& name) const
90 : {
91 0 : return find_rule_by_name(name).get_alarm_count();
92 : }
93 :
94 : // set rule enable/disable status
95 0 : void enable_rule(std::string const& name, bool flag)
96 : {
97 0 : find_rule_by_name(name).enable(flag);
98 0 : }
99 :
100 : // enable/disable EHS
101 : void enable_EHS(bool flag)
102 : {
103 : EHS = flag;
104 : }
105 :
106 : // reset a rule to its ground state (reset alarms and domains)
107 0 : void reset_rule(std::string const& name)
108 : {
109 0 : find_rule_by_name(name).reset();
110 0 : }
111 :
112 0 : void reset_rules()
113 : {
114 0 : for (rule_map_t::iterator it = rmap.begin(); it != rmap.end(); ++it)
115 0 : it->second.reset();
116 0 : }
117 :
118 : // reset conditions
119 : void reset_cond(std::string const& name)
120 : {
121 : find_cond_by_name(name).reset();
122 : }
123 :
124 0 : void reset_conds()
125 : {
126 0 : for (cond_map_t::iterator it = cmap.begin(); it != cmap.end(); ++it)
127 0 : it->second.reset();
128 0 : }
129 :
130 : // reset all
131 0 : void reset()
132 : {
133 0 : reset_conds();
134 0 : reset_rules();
135 0 : }
136 :
137 : // participants
138 : void add_participant_group(std::string const& group)
139 : {
140 : ma_participants::instance().add_group(group);
141 : }
142 :
143 : void add_participant_group(std::string const& group, size_t size)
144 : {
145 : ma_participants::instance().add_group(group, size);
146 : }
147 :
148 : void add_participant(std::string const& group, std::string const& app)
149 : {
150 : ma_participants::instance().add_participant(group, app);
151 : }
152 :
153 : void add_participant(std::string const& app)
154 : {
155 : ma_participants::instance().add_participant(app);
156 : }
157 :
158 : size_t get_group_participant_count(std::string const& group) const
159 : {
160 : return ma_participants::instance().get_group_participant_count(group);
161 : }
162 :
163 : size_t get_participant_count() const
164 : {
165 : return ma_participants::instance().get_participant_count();
166 : }
167 :
168 : private:
169 : // initialize the rule engine with configuration file
170 : void init_engine();
171 :
172 : // event worker
173 : void event_worker();
174 :
175 : // merge notification list from conditions
176 : void merge_notify_list(notify_list_t& n_list, conds_t const& c_list, notify_t type);
177 :
178 : // evaluates the domain / status of all rules in the notification list
179 : void evaluate_rules_domain(notify_list_t& notify_domain);
180 : void evaluate_rules(notify_list_t& notify_status);
181 :
182 : // find condition/rule with given name
183 : const ma_condition& find_cond_by_name(std::string const& name) const;
184 : ma_condition& find_cond_by_name(std::string const& name);
185 :
186 : const ma_rule& find_rule_by_name(std::string const& name) const;
187 : ma_rule& find_rule_by_name(std::string const& name);
188 :
189 : private:
190 : // configuration
191 : fhicl::ParameterSet pset;
192 :
193 : // map of conditions
194 : cond_map_t cmap;
195 : std::vector<std::string> cnames;
196 :
197 : // map of rules
198 : rule_map_t rmap;
199 : std::vector<std::string> rnames;
200 :
201 : // callbacks
202 : alarm_fn_t alarm_fn;
203 : cond_match_fn_t cond_match_fn;
204 :
205 : // a list of scheduled events
206 : ma_timing_events events;
207 :
208 : // event thread
209 : boost::thread event_worker_t;
210 :
211 : // whether this engine is an Error Handler Supervisor
212 : bool EHS;
213 : };
214 :
215 : } // end of namespace errorhandler
216 : } // end of namespace novadaq
217 :
218 : #endif
|