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