MADARA  3.2.3
IntegerVector3D.cpp
Go to the documentation of this file.
1 #include "IntegerVector3D.h"
3 
4 
6  const KnowledgeUpdateSettings & settings,
7  const std::string & delimiter)
8  : BaseContainer ("", settings), context_ (0), delimiter_ (delimiter)
9 {
10 }
11 
13  const std::string & name,
15  const Dimensions & dimensions,
16  bool delete_vars,
17  const KnowledgeUpdateSettings & settings,
18  const std::string & delimiter)
19  : BaseContainer (name, settings), context_ (&(knowledge.get_context ())),
20  delimiter_ (delimiter)
21 {
22  size_ = get_size_ref ();
23  resize (dimensions, delete_vars);
24 }
25 
27  const std::string & name,
29  const Dimensions & dimensions,
30  bool delete_vars,
31  const KnowledgeUpdateSettings & settings,
32  const std::string & delimiter)
33  : BaseContainer (name, settings), context_ (knowledge.get_context ()),
34  delimiter_ (delimiter)
35 {
36  size_ = get_size_ref ();
37  resize (dimensions, delete_vars);
38 }
39 
41  const IntegerVector3D & rhs)
42 : BaseContainer (rhs), context_ (rhs.context_),
43  vector_ (rhs.vector_),
44  size_ (rhs.size_),
46 {
47 }
48 
49 
51 {
52 
53 }
54 
55 void
57 {
58  if (context_ && name_ != "")
59  {
60  ContextGuard context_guard (*context_);
61 
62  Indices dimensions = size ();
63 
64  if (dimensions.x > 0 && dimensions.y > 0 && dimensions.z > 0)
65  {
66  for (size_t i = 0; i < dimensions.x; ++i)
67  {
68  for (size_t j = 0; j < dimensions.y; ++j)
69  {
70  for (size_t k = 0; k < dimensions.z; ++k)
71  {
72  context_->mark_modified (vector_[i][j][k]);
73  }
74  }
75  }
76  }
77 
79  }
80 }
81 
84 {
85  std::stringstream result;
86 
87  result << "IntegerVector3D: ";
88 
89  if (context_)
90  {
91  ContextGuard context_guard (*context_);
92 
93  Indices dimensions = size ();
94 
95  result << this->name_;
96  result << " [" << dimensions.x << "," <<
97  dimensions.y << "," << dimensions.z << "]";
98  result << " = [";
99 
100  if (dimensions.x > 0 && dimensions.y > 0)
101  {
102  for (size_t i = 0; i < dimensions.x; ++i)
103  {
104  for (size_t j = 0; j < dimensions.y; ++j)
105  {
106  result << context_->get (vector_[i][j][0]).to_string ();
107 
108  for (size_t k = 1; k < dimensions.z; ++k)
109  {
110  result << ", " << context_->get (vector_[i][j][k]).to_string ();
111  }
112  }
113  result << "\n";
114  }
115  }
116 
117  result << "]";
118  }
119 
120  return result.str ();
121 }
122 
123 void
125 {
126  modify ();
127 }
128 
131 {
132  return get_debug_info ();
133 }
134 
137 {
138  return new IntegerVector3D (*this);
139 }
140 
141 void
143 {
144  if (context_)
145  {
146  ContextGuard context_guard (*context_);
147  if (index.x < vector_.size () &&
148  index.y < vector_[index.x].size () &&
149  index.z < vector_[index.x][index.y].size ())
150  context_->mark_modified (vector_[index.x][index.y][index.z]);
151  }
152 }
153 
154 void
156  const IntegerVector3D & rhs)
157 {
158  if (this != &rhs)
159  {
160  MADARA_GUARD_TYPE guard (mutex_), guard2 (rhs.mutex_);
161 
162  this->context_ = rhs.context_;
163  this->name_ = rhs.name_;
164  this->settings_ = rhs.settings_;
165  this->size_ = rhs.size_;
166  this->vector_ = rhs.vector_;
167  this->delimiter_ = rhs.delimiter_;
168  }
169 }
170 
173 {
174  VariableReference ref;
175 
176  if (context_ && name_ != "")
177  {
178  KnowledgeUpdateSettings keep_local (true);
179  std::stringstream buffer;
180 
181  ContextGuard context_guard (*context_);
182 
183  buffer << name_;
184  buffer << delimiter_;
185  buffer << "size";
186 
187  ref = context_->get_ref (buffer.str (), keep_local);
188  }
189 
190  return ref;
191 }
192 
193 void
195  const Dimensions & dimensions, bool delete_vars)
196 {
197  if (context_ && name_ != "")
198  {
199  ContextGuard context_guard (*context_);
200 
202  "IntegerVector3D::resize: resizing to [%d,%d]\n",
203  (int)dimensions.x, (int)dimensions.y);
204 
205  bool is_reset = dimensions.x == 0 && dimensions.y == 0;
206 
207  if (!size_.is_valid ())
208  {
209  size_ = get_size_ref ();
210  }
211 
212  // save the old size
213  Dimensions old_size = size ();
214  Dimensions new_size (dimensions);
215 
217  "IntegerVector3D::resize: old size is [%d,%d]\n",
218  (int)old_size.x, (int)old_size.y);
219 
220  if (is_reset)
221  {
223  "IntegerVector3D::resize: new size is being reset to size in KB\n");
224 
225  new_size.x = old_size.x;
226  new_size.y = old_size.y;
227  new_size.z = old_size.z;
228  }
229  else
230  {
231 
233  "IntegerVector3D::resize: using dimensions passed in.\n");
234 
235  // set the new size
236  std::vector <KnowledgeRecord::Integer> update (3);
237  update[0] = dimensions.x;
238  update[1] = dimensions.y;
239  update[2] = dimensions.z;
240 
241  context_->set (size_, update, settings_);
242  }
243 
244  // correct the vector for the new size
245  vector_.resize (new_size.x);
246 
247  for (size_t i = 0; i < new_size.x; ++i)
248  {
250  "IntegerVector3D::resize: resizing vector_[%d] to %d.\n",
251  (int)i, (int)new_size.y);
252 
253  vector_[i].resize (new_size.y);
254 
255  // create any new VariableReference needed, default is end of old cols
256 
257  size_t start = old_size.y;
258 
259 
260  // if you've gained rows and this is a new row, reset start to 0
261  if (is_reset || (old_size.x < new_size.x && i >= old_size.x))
262  {
263  start = 0;
264  }
265 
267  "IntegerVector3D::resize: creating var_refs from %d->%d.\n",
268  (int)start, (int)new_size.y);
269 
270  // create new VariableReferences
271  for (size_t j = start; j < new_size.y; ++j)
272  {
273 
274  vector_[i][j].resize (new_size.z);
275 
276  // create any new VariableReference needed, default is end of old cols
277 
278  size_t start_z = old_size.z;
279 
280  // if you've gained cols and this is a new col, reset start to 0
281  if (is_reset || ((old_size.y < new_size.y && j >= old_size.y) ||
282  (old_size.x < new_size.x && i >= old_size.x)))
283  {
284  start_z = 0;
285  }
286 
287  // create new VariableReferences
288  for (size_t k = start_z; k < new_size.z; ++k)
289  {
290  std::stringstream var_name;
291  var_name << this->name_;
292  var_name << delimiter_;
293  var_name << i;
294  var_name << delimiter_;
295  var_name << j;
296  var_name << delimiter_;
297  var_name << k;
298 
299  vector_[i][j][k] = context_->get_ref (var_name.str (), settings_);
300  }
301  }
302  }
303 
304  // delete if we need to delete
305  if ((new_size.x < old_size.x ||
306  new_size.y < old_size.y || new_size.z < old_size.z) && delete_vars)
307  {
309  "IntegerVector3D::resize: deleting refs: rows: 0->%d.\n",
310  (int)old_size.x);
311 
312  // delete within the old rows
313  for (size_t i = 0; i < old_size.x; ++i)
314  {
315  // by default, delete from new col size to old col size
316  size_t start = new_size.y;
317 
318  // the exception is when we are deleting the entire col
319  if (old_size.x > new_size.x && i >= new_size.x)
320  {
321  start = 0;
322  }
323 
325  "IntegerVector3D::resize: deleting refs: %d:%d->%d.\n",
326  (int)i, (int)start, (int)old_size.x);
327 
328  // delete old columns
329  for (size_t j = start; j < old_size.y; ++j)
330  {
331  // by default, delete from new col size to old col size
332  size_t start_k = new_size.z;
333 
334  // the exception is when we are deleting the entire row
335  if (old_size.y > new_size.y && j >= new_size.y)
336  {
337  start = 0;
338  }
339 
340  // delete old columns
341  for (size_t k = start_k; k < old_size.z; ++k)
342  {
343  std::stringstream var_name;
344  var_name << this->name_;
345  var_name << delimiter_;
346  var_name << i;
347  var_name << delimiter_;
348  var_name << j;
349  var_name << delimiter_;
350  var_name << k;
351 
353  "IntegerVector3D::resize: deleting ref: %s.\n",
354  var_name.str ().c_str ());
355 
356  context_->delete_variable (var_name.str (), settings_);
357  }
358  }
359  }
360  }
361  }
362 }
363 
366 {
367  Indices cur_size;
368 
369  if (context_)
370  {
371  KnowledgeRecord record;
372  // lock the KnowledgeBase during access
373  {
374  ContextGuard context_guard (*context_);
375 
376  record = context_->get (size_);
377  }
378 
379  std::vector <KnowledgeRecord::Integer> sizes (record.to_integers ());
380  cur_size.x = (size_t) (sizes.size () >= 3 ? sizes[0] : 0);
381  cur_size.y = (size_t) (sizes.size () >= 3 ? sizes[1] : 0);
382  cur_size.z = (size_t) (sizes.size () >= 3 ? sizes[2] : 0);
383  }
384 
385  return cur_size;
386 }
387 
388 void
390  const std::string & var_name,
391  KnowledgeBase & knowledge, const Indices & dimensions)
392 {
393  if (context_ != &(knowledge.get_context ()) || name_ != var_name)
394  {
395  context_ = &(knowledge.get_context ());
396 
397  ContextGuard context_guard (*context_);
398 
399  name_ = var_name;
400 
401  vector_.clear ();
402 
403  size_ = get_size_ref ();
404 
405  resize (dimensions);
406  }
407 }
408 
409 void
411  const std::string & var_name,
412  Variables & knowledge, const Indices & dimensions)
413 {
414  if (context_ != knowledge.get_context () || name_ != var_name)
415  {
416  context_ = knowledge.get_context ();
417 
418  ContextGuard context_guard (*context_);
419 
420  name_ = var_name;
421 
422  vector_.clear ();
423  resize (dimensions);
424  }
425 }
426 
427 void
429  const std::string & var_name,
430  ThreadSafeContext & knowledge, const Indices & dimensions)
431 {
432  if (context_ != &knowledge || name_ != var_name)
433  {
434  context_ = &knowledge;
435 
436  ContextGuard context_guard (*context_);
437 
438  name_ = var_name;
439 
440  vector_.clear ();
441  resize (dimensions);
442  }
443 }
444 
445 void
447 const std::string & delimiter)
448 {
449  delimiter_ = delimiter;
450  if (context_)
451  {
452  ContextGuard context_guard (*context_);
453 
454  vector_.clear ();
455  resize ({0,0,0});
456  }
457 }
458 
459 
462 {
463  return delimiter_;
464 }
465 
466 void
468  std::vector<std::vector<std::vector<type> > > & target) const
469 {
470  KnowledgeUpdateSettings keep_local (true);
471 
472  if (context_)
473  {
474  ContextGuard context_guard (*context_);
475 
476  Indices dimensions = size ();
477 
478  target.resize (dimensions.x);
479 
480  for (size_t i = 0; i < dimensions.x; ++i)
481  {
482  target[i].resize (dimensions.y);
483  for (size_t j = 0; j < dimensions.y; ++j)
484  {
485  target[i][j].resize (dimensions.z);
486  for (size_t k = 0; k < dimensions.z; ++k)
487  {
488  target[i][j][k] = context_->get (
489  vector_[i][j][k], keep_local).to_integer ();
490  }
491  }
492  }
493  }
494 }
495 
498  const Indices & index) const
499 {
500  type result (0);
501 
502  KnowledgeUpdateSettings keep_local (true);
503 
505  "IntegerVector3D::[]: retrieving [%d,%d,%d].\n",
506  (int)index.x,(int)index.y,(int)index.z);
507 
508  if (context_)
509  {
510  ContextGuard context_guard (*context_);
511 
512  if (index.x < vector_.size () &&
513  index.y < vector_[index.x].size () &&
514  index.z < vector_[index.x][index.y].size ())
515  {
517  "IntegerVector3D::[]: [%d][%d][%d] is within [%d,%d,%d].\n",
518  (int)index.x,(int)index.y,(int)index.z,
519  (int)vector_.size (),
520  (int)vector_[index.x].size (),
521  (int)vector_[index.x][index.y].size ());
522 
523  result = context_->get (
524  vector_[index.x][index.y][index.z], keep_local).to_integer ();
525 
527  "IntegerVector3D::[]: value is %d.\n",
528  (int)result);
529 
530  }
531  else
532  {
534  "IntegerVector3D::[]: [%d][%d][%d] is not within [%d,%d,%d].\n",
535  (int)index.x,(int)index.y,(int)index.z,
536  (int)vector_.size (),
537  (int)vector_[index.x].size (),
538  (int)vector_[index.x][index.y].size ());
539  }
540  }
541 
542  return result;
543 }
544 
545 bool
547  const Indices & index) const
548 {
549  bool result (false);
550 
551  if (context_)
552  {
553  ContextGuard context_guard (*context_);
554 
555  if (index.x < vector_.size () &&
556  index.y < vector_[index.x].size () &&
557  index.z < vector_[index.x][index.y].size ())
558  {
559  result = context_->exists (vector_[index.x][index.y][index.z]);
560  }
561  }
562 
563  return result;
564 }
565 
566 int
568 const Indices & index,
569 type value)
570 {
571  int result = -1;
572 
573  if (context_)
574  {
575  ContextGuard context_guard (*context_);
576 
577  if (index.x < vector_.size () &&
578  index.y < vector_[index.x].size () &&
579  index.z < vector_[index.x][index.y].size ())
580  {
581  result = context_->set (
582  vector_[index.x][index.y][index.z], value, settings_);
583  }
584  }
585 
586  return result;
587 }
588 
589 
590 int
592 const std::vector<std::vector<std::vector<type> > > & value)
593 {
594  int result = 0;
595 
596  if (context_)
597  {
598  ContextGuard context_guard (*context_);
599 
600  for (size_t i = 0; i < value.size () && i < vector_.size (); ++i)
601  {
602  for (size_t j = 0; j < value[i].size () && j < vector_[i].size (); ++j)
603  {
604  for (size_t k = 0;
605  k < value[i][j].size () && k < vector_[i][j].size (); ++k)
606  {
607  context_->set (vector_[i][j][k], value[i][j][k], settings_);
608  }
609  }
610  }
611  }
612 
613  return result;
614 }
615 
616 int
618  const Indices & index,
619  type value,
620  const KnowledgeUpdateSettings & settings)
621 {
622  int result = -1;
623 
624  if (context_)
625  {
626  ContextGuard context_guard (*context_);
627 
628  if (index.x < vector_.size () &&
629  index.y < vector_[index.x].size () &&
630  index.z < vector_[index.x][index.y].size ())
631  {
632  result = context_->set (vector_[index.x][index.y][index.z], value, settings);
633  }
634  }
635 
636  return result;
637 }
638 
639 
640 int
642  const std::vector<std::vector<std::vector<type> > > & value,
643  const KnowledgeUpdateSettings & settings)
644 {
645  int result = 0;
646 
647  if (context_)
648  {
649  ContextGuard context_guard (*context_);
650 
651  for (size_t i = 0; i < value.size () && i < vector_.size (); ++i)
652  {
653  for (size_t j = 0; j < value[i].size () && j < vector_[i].size (); ++j)
654  {
655  for (size_t k = 0;
656  k < value[i][j].size () && k < vector_[i][j].size (); ++k)
657  {
658  context_->set (vector_[i][j][k], value[i][j][k], settings);
659  }
660  }
661  }
662  }
663 
664  return result;
665 }
666 
667 void
669  const Indices & index,
670  uint32_t quality,
671  const KnowledgeReferenceSettings & settings)
672 {
673  if (context_)
674  {
675  ContextGuard context_guard (*context_);
676 
677  if (index.x < vector_.size () &&
678  index.y < vector_[index.x].size () &&
679  index.z < vector_[index.x][index.y].size ())
680  context_->set_quality (vector_[index.x][index.y][index.z].get_name (),
681  quality, true, settings);
682  }
683 }
684 
685 bool
687 {
688  bool result (false);
689 
691  "IntegerVector3D::is_true: Checking for truth\n");
692 
693  if (context_)
694  {
695  ContextGuard context_guard (*context_);
696 
697  result = true;
698 
700  "IntegerVector3D::is_true: context was not null. Result changed to %d\n",
701  (int)result);
702 
703  for (size_t i = 0; i < vector_.size (); ++i)
704  {
705  for (size_t j = 0; j < vector_[i].size (); ++i)
706  {
707  for (size_t k = 0; k < vector_[i][j].size (); ++k)
708  {
710  "IntegerVector3D::is_true: checking [%d,%d,%d], is_false of %d. \n",
711  (int)i, (int)j, (int)k,
712  (int)context_->get (vector_[i][j][k]).is_false ());
713 
714  if (context_->get (vector_[i][j][k]).is_false ())
715  {
717  "IntegerVector3D::is_true: result is false, breaking\n");
718 
719  result = false;
720  break;
721  }
722  }
723  }
724  }
725 
726  if (vector_.size () == 0)
727  result = false;
728  }
729 
731  "IntegerVector3D::is_true: final result is %d\n", (int)result);
732 
733  return result;
734 }
735 
736 bool
738 {
739  return !is_true ();
740 }
741 
742 
743 bool
745 {
746  return is_true ();
747 }
748 
749 bool
751 {
752  return is_false ();
753 }
754 
This class encapsulates an entry in a KnowledgeBase.
int set(const std::string &key, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to the specific record.
bool exists(const Indices &index) const
Checks to see if the index has ever been assigned a value.
KnowledgeRecord::Integer type
convenience typedef for element type
void modify(void)
Mark the vector as modified.
bool is_false(void) const
Determines if the value of the vector is false.
VariableReference get_size_ref(void)
Returns a reference to the size of vector.
std::vector< std::vector< std::vector< VariableReference > > > vector_
Values of the array.
std::string name_
Prefix of variable.
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 ...
void set_delimiter(const std::string &delimiter)
Sets the delimiter for adding and detecting subvariables.
This class stores variables and their values for use by any entity needing state information in a thr...
void operator=(const IntegerVector3D &rhs)
Assignment operator.
MADARA_LOCK_TYPE mutex_
guard for access and changes
Optimized reference to a variable within the knowledge base.
#define madara_logger_log(logger, level,...)
Fast version of the madara::logger::log method.
Definition: Logger.h:20
bool is_valid(void) const
Checks to see if the variable reference has been initialized.
ThreadSafeContext * context_
Variable context that we are modifying.
bool exists(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically checks to see if a variable already exists.
IntegerVector3D(const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings(), const std::string &delimiter=".")
Default constructor.
virtual std::string get_debug_info_(void)
Returns the type of the container along with name and any other useful information.
void resize(const Dimensions &dimensions, bool delete_vars=true)
Resizes the vector.
void set_quality(const Indices &index, uint32_t quality, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Sets the quality of writing to a certain variable from this entity.
A thread-safe guard for a context or knowledge base.
Definition: ContextGuard.h:23
static struct madara::knowledge::tags::string_t string
std::string delimiter_
Delimiter for the prefix to subvars.
std::string get_delimiter(void)
Gets the delimiter for adding and detecting subvariables.
virtual bool is_true_(void) const
Polymorphic is true method which can be used to determine if all values in the container are true...
virtual void modify_(void)
Polymorphic modify method used by collection containers.
void set_name(const std::string &var_name, KnowledgeBase &knowledge, const Dimensions &dimensions={0, 0, 0})
Sets the variable name that this refers to.
This class provides a distributed knowledge base to users.
Definition: KnowledgeBase.h:45
Manages a 3D array of doubles as a virtual overlay in the KnowledgeBase.
bool is_true(void) const
Determines if all values in the vector are true.
std::string get_debug_info(void)
Returns the type of the container along with name and any other useful information.
virtual BaseContainer * clone(void) const
Clones this container.
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Atomically returns a reference to the variable.
Integer to_integer(void) const
converts the value to an integer.
VariableReference size_
Reference to the size of 2D array.
type operator[](const Indices &index) const
Retrieves an index from the multi-dimensional array.
ThreadSafeContext & get_context(void)
Returns the ThreadSafeContext associated with this Knowledge Base.
bool delete_variable(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Deletes the key.
std::vector< Integer > to_integers(void) const
converts the value to a vector of integers
Provides functions and classes for the distributed knowledge base.
KnowledgeUpdateSettings settings_
Settings for modifications.
Settings for applying knowledge updates.
void copy_to(std::vector< std::vector< std::vector< type > > > &target) const
Copies the vector elements to an STL vector.
madara::knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically returns the value of a variable.
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.
Dimensions size(void) const
Returns the size of the local vector.
bool is_false(void) const
Checks to see if the record is false.
Settings for applying knowledge updates.
std::string to_string(const std::string &delimiter=", ") const
converts the value to a string.
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
int set(const Indices &index, type value)
Sets a knowledge variable to a specified value.
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.