Line data Source code
1 :
2 : #include "ErrorHandler/MessageAnalyzer/ma_hitmap.h"
3 : #include "ErrorHandler/MessageAnalyzer/ma_condition.h"
4 :
5 : using namespace novadaq::errorhandler;
6 :
7 : const std::string ma_hitmap::global_s = "__S_GLOBAL__";
8 : const std::string ma_hitmap::global_t = "__T_GLOBAL__";
9 :
10 : const size_t ma_hitmap::cap_increment = 20;
11 :
12 0 : ma_hitmap::ma_hitmap()
13 0 : : src_idx()
14 0 : , tgt_idx()
15 0 : , src_cap(20)
16 0 : , tgt_cap(20)
17 0 : , cond(0)
18 0 : , hitmap(boost::extents[src_cap][tgt_cap])
19 : {
20 0 : }
21 :
22 0 : void ma_hitmap::reset()
23 : {
24 0 : src_idx.clear();
25 0 : tgt_idx.clear();
26 :
27 0 : for (size_t s = 0; s < src_cap; ++s)
28 0 : for (size_t t = 0; t < tgt_cap; ++t)
29 0 : hitmap[s][t].reset();
30 0 : }
31 :
32 : unsigned int
33 0 : ma_hitmap::capture(qt_mf_msg const& msg, std::string const& src, std::string const& tgt, boost::smatch const& what)
34 : {
35 0 : size_t s_idx = 0;
36 0 : size_t t_idx = 0;
37 :
38 0 : unsigned int result = 0x00;
39 :
40 : // find source index
41 0 : if (!cond->per_source())
42 : {
43 0 : if (src_idx.empty())
44 : {
45 0 : src_idx[global_s] = 0;
46 0 : result |= SOURCE_CHANGE;
47 : }
48 :
49 0 : s_idx = 0;
50 : }
51 : else
52 : {
53 0 : idx_t::const_iterator it = src_idx.find(src);
54 0 : if (it == src_idx.end())
55 : {
56 0 : s_idx = src_idx.size();
57 0 : src_idx.insert(std::make_pair(src, s_idx));
58 0 : result |= SOURCE_CHANGE;
59 : }
60 : else
61 : {
62 0 : s_idx = it->second;
63 : }
64 : }
65 :
66 : // find target index
67 0 : if (!cond->per_target())
68 : {
69 0 : if (tgt_idx.empty())
70 : {
71 0 : tgt_idx[global_t] = 0;
72 0 : result |= TARGET_CHANGE;
73 : }
74 :
75 0 : t_idx = 0;
76 : }
77 : else
78 : {
79 0 : idx_t::const_iterator it = tgt_idx.find(tgt);
80 0 : if (it == tgt_idx.end())
81 : {
82 0 : t_idx = tgt_idx.size();
83 0 : tgt_idx.insert(std::make_pair(tgt, t_idx));
84 0 : result |= TARGET_CHANGE;
85 : }
86 : else
87 : {
88 0 : t_idx = it->second;
89 : }
90 : }
91 :
92 : // resize the array if needed
93 0 : bool resize = false;
94 :
95 0 : if (s_idx >= src_cap)
96 : {
97 0 : src_cap += cap_increment;
98 0 : resize = true;
99 : }
100 :
101 0 : if (t_idx >= tgt_cap)
102 : {
103 0 : tgt_cap += cap_increment;
104 0 : resize = true;
105 : }
106 :
107 0 : if (resize)
108 : {
109 0 : hitmap.resize(boost::extents[src_cap][tgt_cap]);
110 : }
111 :
112 : // knock the cell
113 0 : return hitmap[s_idx][t_idx].hit(msg, what, *cond, s_idx, t_idx)
114 0 : ? (result | STATUS_CHANGE)
115 0 : : (result);
116 : }
117 :
118 0 : bool ma_hitmap::event(size_t src, size_t tgt, time_t t)
119 : {
120 0 : return hitmap[src][tgt].event(t, *cond);
121 : }
122 :
123 0 : int ma_hitmap::find_source(std::string const& src)
124 : {
125 0 : idx_t::const_iterator it = src_idx.find(src);
126 0 : if (it == src_idx.end())
127 0 : return D_NIL;
128 : else
129 0 : return it->second;
130 : }
131 :
132 0 : int ma_hitmap::find_target(std::string const& tgt)
133 : {
134 0 : idx_t::const_iterator it = tgt_idx.find(tgt);
135 0 : if (it == tgt_idx.end())
136 0 : return D_NIL;
137 : else
138 0 : return it->second;
139 : }
140 :
141 : // get src/tgt string from idx
142 : const std::string&
143 0 : ma_hitmap::get_source(ma_cond_domain v) const
144 : {
145 0 : int idx = v.first;
146 0 : assert(!src_idx.empty());
147 :
148 0 : if (idx == D_NIL) throw std::runtime_error("get_source: nil idx");
149 0 : if (idx == D_ANY) return src_idx.begin()->first;
150 :
151 0 : assert((unsigned)idx < src_idx.size());
152 :
153 0 : for (idx_t::const_iterator it = src_idx.begin(); it != src_idx.end(); ++it)
154 0 : if ((unsigned)idx == it->second) return it->first;
155 :
156 0 : throw std::runtime_error("get_source: idx not found");
157 : }
158 :
159 : const std::string&
160 0 : ma_hitmap::get_target(ma_cond_domain v) const
161 : {
162 0 : int idx = v.second;
163 0 : assert(!tgt_idx.empty());
164 :
165 0 : if (idx == D_NIL) throw std::runtime_error("get_target: nil idx");
166 0 : if (idx == D_ANY) return tgt_idx.begin()->first;
167 :
168 0 : assert((unsigned)idx < tgt_idx.size());
169 :
170 0 : for (idx_t::const_iterator it = tgt_idx.begin(); it != tgt_idx.end(); ++it)
171 0 : if ((unsigned)idx == it->second) return it->first;
172 :
173 0 : throw std::runtime_error("get_source: idx not found");
174 : }
175 :
176 : std::string
177 0 : ma_hitmap::get_message(ma_cond_domain v) const
178 : {
179 0 : assert(!src_idx.empty());
180 0 : assert(!tgt_idx.empty());
181 :
182 0 : if (v.first == D_NIL || v.second == D_NIL)
183 0 : throw std::runtime_error("get_message: nil idx");
184 :
185 0 : v.first = (v.first == D_ANY) ? 0 : v.first;
186 0 : v.second = (v.second == D_ANY) ? 0 : v.second;
187 :
188 0 : return hitmap[v.first][v.second].get_latest_message();
189 : }
190 :
191 : std::string
192 0 : ma_hitmap::get_message_group(ma_cond_domain v, size_t g) const
193 : {
194 0 : assert(!src_idx.empty());
195 0 : assert(!tgt_idx.empty());
196 :
197 0 : if (v.first == D_NIL || v.second == D_NIL)
198 0 : throw std::runtime_error("get_message: nil idx");
199 :
200 0 : v.first = (v.first == D_ANY) ? 0 : v.first;
201 0 : v.second = (v.second == D_ANY) ? 0 : v.second;
202 :
203 0 : return hitmap[v.first][v.second].get_message_group(g);
204 : }
205 :
206 : // if the cell has been triggered
207 0 : bool ma_hitmap::get_status(ma_cond_domain v) const
208 : {
209 0 : bool r = hitmap[v.first][v.second].is_on();
210 :
211 0 : TLOG(TLVL_DEBUG) << "hitmap::get_status @ "
212 0 : << v.first << ", " << v.second << " = " << r;
213 0 : return r;
214 : }
215 :
216 0 : int ma_hitmap::get_alarm_count(ma_cond_domain v, arg_t arg) const
217 : {
218 0 : ma_cond_range src, tgt;
219 0 : get_cond_range(v, src, tgt);
220 :
221 0 : int count = 0;
222 : ;
223 0 : if (arg == NONE)
224 : {
225 0 : for (int s = src.first; s <= src.second; ++s)
226 0 : for (int t = tgt.first; t <= tgt.second; ++t)
227 0 : count += hitmap[s][t].get_message_count();
228 : }
229 0 : else if (arg == SOURCE)
230 : {
231 0 : for (int s = src.first; s <= src.second; ++s)
232 0 : for (int t = tgt.first; t <= tgt.second; ++t)
233 0 : if (hitmap[s][t].is_on())
234 : {
235 0 : ++count;
236 0 : break;
237 : }
238 : }
239 : else
240 : {
241 0 : for (int t = tgt.first; t <= tgt.second; ++t)
242 0 : for (int s = src.first; s <= src.second; ++s)
243 0 : if (hitmap[s][t].is_on())
244 : {
245 0 : ++count;
246 0 : break;
247 : }
248 : }
249 :
250 0 : return count;
251 : }
252 :
253 : // get a range of src/target
254 0 : void ma_hitmap::get_cond_range(ma_cond_domain d, ma_cond_range& src, ma_cond_range& tgt) const
255 : {
256 0 : if (domain_is_null(d))
257 0 : throw std::runtime_error("get_cond_range: NIL domain");
258 :
259 0 : if (d.first == D_ANY)
260 0 : src.first = 0, src.second = src_idx.size() - 1;
261 : else
262 0 : src.first = d.first, src.second = d.first;
263 :
264 0 : if (d.second == D_ANY)
265 0 : tgt.first = 0, tgt.second = tgt_idx.size() - 1;
266 : else
267 0 : tgt.first = d.second, tgt.second = d.second;
268 0 : }
269 :
270 : // get a view to the hitmap
271 : const hitmap_view_t
272 0 : ma_hitmap::get_domain_view(ma_cond_domain const& d)
273 : {
274 0 : if (domain_is_null(d))
275 0 : throw std::runtime_error("get_domain_view: null domain");
276 :
277 0 : return hitmap[boost::indices[d.first == D_ANY ? range() : range(d.first)]
278 0 : [d.second == D_ANY ? range() : range(d.second)]];
279 : }
|