MADARA  3.1.8
Counter.cpp
Go to the documentation of this file.
1 
2 #ifndef _MADARA_NO_KARL_
3 
4 #include <sstream>
5 
6 #include "Counter.h"
8 
10  const KnowledgeUpdateSettings & settings)
11 : BaseContainer ("", settings), context_ (0), id_ (0), counters_ (1)
12 {
13  init_noharm ();
14 }
15 
17  const std::string & name,
19  const KnowledgeUpdateSettings & settings)
20 : BaseContainer (name, settings), context_ (&(knowledge.get_context ())),
21  id_ (0), counters_ (1)
22 {
23  init_noharm ();
24  build_var ();
26 }
27 
29  const std::string & name,
31  const KnowledgeUpdateSettings & settings)
32 : BaseContainer (name, settings), context_ (knowledge.get_context ()),
33  id_ (0), counters_ (1)
34 {
35  init_noharm ();
36  build_var ();
38 }
39 
41  const std::string & name,
43  int id,
44  int counters,
45  type value,
46  const KnowledgeUpdateSettings & settings)
47 : BaseContainer (name, settings), context_ (&(knowledge.get_context ())),
48  id_ (id), counters_ (counters)
49 {
50  init_noharm ();
51  build_var ();
53  context_->set (variable_, value, settings);
54 }
55 
57  const std::string & name,
59  int id,
60  int counters,
61  type value,
62  const KnowledgeUpdateSettings & settings)
63 : BaseContainer (name, settings), context_ (knowledge.get_context ()),
64  id_ (id), counters_ (counters)
65 {
66  init_noharm ();
67  build_var ();
69  context_->set (variable_, value, settings);
70 }
71 
72 
74 : BaseContainer (rhs), context_ (rhs.context_),
75  variable_ (rhs.variable_),
76  id_ (rhs.id_),
77  counters_ (rhs.counters_),
79 {
80 
81 }
82 
83 
85 {
86 
87 }
88 
89 void
91 {
92  if (this != &rhs)
93  {
94  MADARA_GUARD_TYPE guard (mutex_), guard2 (rhs.mutex_);
95 
96  this->context_ = rhs.context_;
97  this->name_ = rhs.name_;
98  this->id_ = rhs.id_;
99  this->counters_ = rhs.counters_;
100  this->settings_ = rhs.settings_;
101  this->variable_ = rhs.variable_;
103  }
104 }
105 
106 void
108 {
109  if (context_ && name_ != "")
110  {
111  ContextGuard context_guard (*context_);
112  MADARA_GUARD_TYPE guard (mutex_);
113 
114  std::stringstream buffer;
115  if (counters_ > 0)
116  {
117  // add the first counter variable
118  buffer << name_;
119  buffer << ".0";
120 
121  // add all other counter variables
122  for (int i = 1; i < counters_; ++i)
123  {
124  buffer << "+";
125  buffer << name_;
126  buffer << ".";
127  buffer << i;
128  }
129  }
130 
131  aggregate_count_ = context_->compile (buffer.str ());
132  }
133  else if (name_ == "")
134  {
135  context_->print ("ERROR: Container::Counter needs a name.\n", 0);
136  }
137  else if (!context_)
138  {
139  context_->print ("ERROR: Container::Counter needs a context.\n", 0);
140  }
141 }
142 
143 void
145 {
146  if (context_ && name_ != "")
147  {
148  ContextGuard context_guard (*context_);
149  MADARA_GUARD_TYPE guard (mutex_);
150 
151  std::stringstream buffer;
152 
153  buffer << name_;
154  buffer << ".";
155  buffer << id_;
156 
157  variable_ = context_->get_ref (buffer.str (), no_harm);
158  }
159  else if (name_ == "")
160  {
161  context_->print ("ERROR: Container::Counter needs a name.\n", 0);
162  }
163  else if (!context_)
164  {
165  context_->print ("ERROR: Container::Counter needs a context.\n", 0);
166  }
167 }
168 
169 void
171 {
172  no_harm.always_overwrite = false;
174  no_harm.expand_variables = false;
175  no_harm.signal_changes = false;
178 }
179 
180 void
182 {
183  ContextGuard context_guard (*context_);
184  if (context_ && name_ != "")
185  {
187  }
188 }
189 
192 {
193  std::stringstream result;
194 
195  result << "Counter: ";
196 
197  if (context_)
198  {
199  ContextGuard context_guard (*context_);
200  MADARA_GUARD_TYPE guard (mutex_);
201 
202  result << this->name_;
203  result << " = " << context_->get (variable_).to_string ();
204  result << " (total: " << get_count () << ")";
205  }
206 
207  return result.str ();
208 }
209 
210 void
212 {
213  modify ();
214 }
215 
218 {
219  return get_debug_info ();
220 }
221 
224 {
225  return new Counter (*this);
226 }
227 
228 int
230 {
231  MADARA_GUARD_TYPE guard (mutex_);
232  return id_;
233 }
234 
235 int
237 {
238  MADARA_GUARD_TYPE guard (mutex_);
239  return counters_;
240 }
241 
242 void
244  const std::string & var_name,
246 {
247  KnowledgeUpdateSettings keep_local (true);
248  context_ = &(knowledge.get_context ());
249 
250  ContextGuard context_guard (*context_);
251  MADARA_GUARD_TYPE guard (mutex_);
252 
253  name_ = var_name;
254 
255  this->build_var ();
256  this->build_aggregate_count ();
257 }
258 
259 void
261  const std::string & var_name,
263 {
264  KnowledgeUpdateSettings keep_local (true);
265  context_ = knowledge.get_context ();
266 
267  ContextGuard context_guard (*context_);
268  MADARA_GUARD_TYPE guard (mutex_);
269 
270  name_ = var_name;
271 
272  this->build_var ();
273  this->build_aggregate_count ();
274 }
275 
276 void
278 {
279  if (context_)
280  {
281  ContextGuard context_guard (*context_);
282  MADARA_GUARD_TYPE guard (mutex_);
283 
284  id_ = id;
285  counters_ = counters;
286 
287  this->build_var ();
288  this->build_aggregate_count ();
289  }
290 }
291 
294 {
295  if (context_)
296  {
297  ContextGuard context_guard (*context_);
298  MADARA_GUARD_TYPE guard (mutex_);
299  context_->set (variable_, value, settings_);
300  }
301 
302  return value;
303 }
304 
305 bool
307 {
308  if (context_)
309  {
310  ContextGuard context_guard (*context_);
311  MADARA_GUARD_TYPE guard (mutex_);
312  return get_count () == value;
313  }
314 
315  return false;
316 }
317 
318 bool
320 {
321  if (context_)
322  {
323  ContextGuard context_guard (*context_);
324  MADARA_GUARD_TYPE guard (mutex_);
325  return get_count () != value;
326  }
327 
328  return true;
329 }
330 
331 bool
333  const Counter & value) const
334 {
335  if (context_)
336  {
337  ContextGuard context_guard (*context_);
338  MADARA_GUARD_TYPE guard (mutex_);
339  return get_count () == value.get_count ();
340  }
341 
342  return false;
343 }
344 
345 bool
347  const Counter & value) const
348 {
349  if (context_)
350  {
351  ContextGuard context_guard (*context_);
352  MADARA_GUARD_TYPE guard (mutex_);
353  return get_count () != value.get_count ();
354  }
355 
356  return true;
357 }
358 
359 bool
361 {
362  if (context_)
363  {
364  ContextGuard context_guard (*context_);
365  MADARA_GUARD_TYPE guard (mutex_);
366  return get_count () < value;
367  }
368 
369  return false;
370 }
371 
372 bool
374 {
375  if (context_)
376  {
377  ContextGuard context_guard (*context_);
378  MADARA_GUARD_TYPE guard (mutex_);
379  return get_count () <= value;
380  }
381 
382  return false;
383 }
384 
385 bool
387 {
388  if (context_)
389  {
390  ContextGuard context_guard (*context_);
391  MADARA_GUARD_TYPE guard (mutex_);
392  return get_count () > value;
393  }
394 
395  return false;
396 }
397 
398 bool
400 {
401  if (context_)
402  {
403  ContextGuard context_guard (*context_);
404  MADARA_GUARD_TYPE guard (mutex_);
405  return get_count () >= value;
406  }
407 
408  return false;
409 }
410 
413 {
414  return to_integer ();
415 }
416 
419 {
421 
422  if (context_)
423  {
424  ContextGuard context_guard (*context_);
425  MADARA_GUARD_TYPE guard (mutex_);
426  result = get_count_record ();
427  }
428 
429  return result;
430 }
431 
434 {
436 
437  if (context_)
438  {
439  ContextGuard context_guard (*context_);
440  MADARA_GUARD_TYPE guard (mutex_);
441  result = get_count ();
442  }
443 
444  return result;
445 }
446 
447 void
449 {
450 
451  if (context_)
452  {
453  type current (0);
454  ContextGuard context_guard (*context_);
455  MADARA_GUARD_TYPE guard (mutex_);
456 
457  current = context_->get (variable_, settings_).to_integer ();
458  current += value;
459  context_->set (variable_, current, settings_);
460  }
461 }
462 
463 void
465 {
466  if (context_)
467  {
468  type current (0);
469  ContextGuard context_guard (*context_);
470  MADARA_GUARD_TYPE guard (mutex_);
471 
472  current = context_->get (variable_, settings_).to_integer ();
473  current -= value;
474  context_->set (variable_, current, settings_);
475  }
476 }
477 
478 void
480 {
481  if (context_)
482  {
483  ContextGuard context_guard (*context_);
484  MADARA_GUARD_TYPE guard (mutex_);
486  }
487 }
488 
489 void
491 {
492  if (context_)
493  {
494  ContextGuard context_guard (*context_);
495  MADARA_GUARD_TYPE guard (mutex_);
497  }
498 }
499 
500 double
502 {
503  double result (0.0);
504 
505  if (context_)
506  {
507  ContextGuard context_guard (*context_);
508  MADARA_GUARD_TYPE guard (mutex_);
509  result = get_count_double ();
510  }
511 
512  return result;
513 }
514 
517 {
518  std::string result;
519 
520  if (context_)
521  {
522  ContextGuard context_guard (*context_);
523  MADARA_GUARD_TYPE guard (mutex_);
524  result = get_count_string ();
525  }
526 
527  return result;
528 }
529 
530 void
532  uint32_t quality,
533  const KnowledgeReferenceSettings & settings)
534 {
535  if (context_)
536  {
537  ContextGuard context_guard (*context_);
538  MADARA_GUARD_TYPE guard (mutex_);
539  context_->set_quality (name_, quality, true, settings);
540  }
541 }
542 
543 
544 bool
546 {
547  bool result (false);
548 
550  "Counter::is_true: checking local counter for non-zero\n");
551 
552  if (context_)
553  {
554  ContextGuard context_guard (*context_);
555  MADARA_GUARD_TYPE guard (mutex_);
556  result = context_->get (variable_, settings_).is_true ();
557  }
558 
560  "Counter::is_true: final result is %d\n", (int)result);
561 
562  return result;
563 }
564 
565 bool
567 {
568  return !is_true ();
569 }
570 
571 
572 bool
574 {
575  return is_true ();
576 }
577 
578 bool
580 {
581  return is_false ();
582 }
583 
584 
585 #endif // _MADARA_NO_KARL_
This class encapsulates an entry in a KnowledgeBase.
bool expand_variables
Toggle for always attempting to expand variables (true) or never expanding variables (false) ...
madara::knowledge::KnowledgeRecord dec(const std::string &key, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically decrements the value of the variable.
int set(const std::string &key, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to the specific record.
bool is_true(void) const
Checks to see if the record is true.
std::string get_debug_info(void)
Returns the type of the container along with name and any other useful information.
Definition: Counter.cpp:191
knowledge::KnowledgeRecord::Integer to_integer(void) const
Returns the value as an integer (same as *)
Definition: Counter.cpp:433
void operator=(const Counter &rhs)
Assignment operator.
Definition: Counter.cpp:90
void build_var(void)
Builds the variable that is actually incremented.
Definition: Counter.cpp:144
madara::knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically returns the value of a variable.
int counters_
the number of counters in the counter ring
Definition: Counter.h:424
ThreadSafeContext * context_
Variable context that we are modifying.
Definition: Counter.h:409
bool operator<=(type value) const
Checks for less than or equal to relationship.
Definition: Counter.cpp:373
int get_id(void) const
Returns the id of the counter in the counter ring.
Definition: Counter.cpp:229
std::string name_
Prefix of variable.
int id_
id of this counter in the counter ring
Definition: Counter.h:419
bool signal_changes
Toggle whether to signal changes have happened.
void print(unsigned int level) const
Atomically prints all variables and values in the context.
void resize(int id=0, int counters=1)
Resizes the counter, usually when number of counters change.
Definition: Counter.cpp:277
VariableReference variable_
Variable reference.
Definition: Counter.h:414
int get_counters(void) const
Returns the number of counters in the counter ring.
Definition: Counter.cpp:236
MADARA_LOCK_TYPE mutex_
guard for access and changes
CompiledExpression aggregate_count_
Expression for aggregating count in one atomic operation.
Definition: Counter.h:429
#define madara_logger_log(logger, level,...)
Fast version of the madara::logger::log method.
Definition: Logger.h:20
void operator-=(type value)
Decrements by a value.
Definition: Counter.cpp:464
bool operator==(const Counter &value) const
Checks for equality.
Definition: Counter.cpp:332
double to_double(void) const
Returns the value as a double.
Definition: Counter.cpp:501
void set_name(const std::string &var_name, KnowledgeBase &knowledge)
Sets the variable name that this refers to.
Definition: Counter.cpp:243
bool operator>(type value) const
Checks for greater than relationship.
Definition: Counter.cpp:386
A thread-safe guard for a context or knowledge base.
Definition: ContextGuard.h:23
virtual bool is_true_(void) const
Polymorphic is true method which can be used to determine if all values in the container are true...
Definition: Counter.cpp:573
void operator++(void)
Increments the value of the variable and returns the result.
Definition: Counter.cpp:479
Counter(const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Default constructor.
Definition: Counter.cpp:9
void operator+=(type value)
Increments by a value.
Definition: Counter.cpp:448
knowledge::KnowledgeRecord::Integer type
trait that describes the value type
Definition: Counter.h:37
This class provides a distributed knowledge base to users.
Definition: KnowledgeBase.h:44
knowledge::KnowledgeRecord get_count_record(void) const
Counts all counter variables.
Definition: Counter.h:401
bool operator<(type value) const
Checks for less than relationship.
Definition: Counter.cpp:360
void modify(void)
Mark the value as modified.
Definition: Counter.cpp:181
madara::knowledge::KnowledgeRecord inc(const std::string &key, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically increments the value of the variable.
virtual std::string get_debug_info_(void)
Returns the type of the container along with name and any other useful information.
Definition: Counter.cpp:217
bool is_true(void) const
Determines if the local count is not zero.
Definition: Counter.cpp:545
bool operator!=(const Counter &value) const
Checks for inequality.
Definition: Counter.cpp:346
virtual void modify_(void)
Polymorphic modify method used by collection containers.
Definition: Counter.cpp:211
virtual bool is_false_(void) const
Polymorphic is false method which can be used to determine if at least one value in the container is ...
Definition: Counter.cpp:579
Integer to_integer(void) const
converts the value to an integer
static constexpr struct madara::knowledge::tags::string_t string
bool always_overwrite
Toggle for always overwriting records, regardless of quality, clock values, etc.
void build_aggregate_count(void)
Builds the aggregate counter logic.
Definition: Counter.cpp:107
CompiledExpression compile(const std::string &expression)
Compiles a KaRL expression into an expression tree.
ThreadSafeContext & get_context(void)
Returns the ThreadSafeContext associated with this Knowledge Base.
Provides functions and classes for the distributed knowledge base.
KnowledgeUpdateSettings settings_
Settings for modifications.
void operator--(void)
Decrements the value of the variable and returns the result.
Definition: Counter.cpp:490
virtual BaseContainer * clone(void) const
Clones this container.
Definition: Counter.cpp:223
knowledge::KnowledgeRecord to_record(void) const
Returns the value as a knowledge::KnowledgeRecord.
Definition: Counter.cpp:418
bool is_false(void) const
Determines if the local count is zero.
Definition: Counter.cpp:566
void init_noharm(void)
Initialize the no harm eval settings.
Definition: Counter.cpp:170
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Atomically returns a reference to the variable.
Settings for applying knowledge updates.
bool treat_globals_as_locals
Toggle whether updates to global variables are treated as local variables and not marked as modified ...
uint32_t set_quality(const std::string &key, uint32_t quality, bool force_update, const KnowledgeReferenceSettings &settings)
Atomically sets quality of this process for a variable.
logger::Logger & get_logger(void) const
Gets the logger used for information printing.
bool delay_sending_modifieds
Toggle for sending modifieds in a single update event after each evaluation.
Definition: EvalSettings.h:114
This class stores an integer within a variable context.
Definition: Counter.h:33
bool track_local_changes
Toggle for checkpointing support.
std::string get_count_string(void) const
Counts all counter variables.
Definition: Counter.h:383
type operator*(void) const
Returns the value of the variable.
Definition: Counter.cpp:412
Settings for applying knowledge updates.
EvalSettings no_harm
Settings we&#39;ll use for all evaluations.
Definition: Counter.h:434
std::string to_string(const std::string &delimiter=", ") const
converts the value to a string.
double get_count_double(void) const
Counts all counter variables.
Definition: Counter.h:392
Provides an interface for external functions into the MADARA KaRL variable settings.
This class is an abstract base class for all containers.
Definition: BaseContainer.h:33
void set_quality(uint32_t quality, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Sets the quality of writing to the counter variables.
Definition: Counter.cpp:531
type get_count(void) const
Counts all counter variables.
Definition: Counter.h:374
void mark_modified(const VariableReference &variable, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Marks the variable reference as updated for the purposes of sending or checkpointing knowledge (for g...
ThreadSafeContext * get_context(void)
Returns the ThreadSafeContext associated with this Variables facade.
bool operator>=(type value) const
Checks for greater than or equal to relationship.
Definition: Counter.cpp:399
std::string to_string(void) const
Returns the value as a string.
Definition: Counter.cpp:516