MADARA  3.4.1
IntegerVector2D.cpp
Go to the documentation of this file.
1 #include "IntegerVector2D.h"
3 
5  const KnowledgeUpdateSettings& settings, const std::string& delimiter)
6  : BaseContainer("", settings), context_(0), delimiter_(delimiter)
7 {
8 }
9 
11  const std::string& name, KnowledgeBase& knowledge,
12  const Dimensions& dimensions, bool delete_vars,
13  const KnowledgeUpdateSettings& settings, const std::string& delimiter)
14  : BaseContainer(name, settings),
15  context_(&(knowledge.get_context())),
16  delimiter_(delimiter)
17 {
18  size_ = get_size_ref();
19  resize(dimensions, delete_vars);
20 }
21 
23  const std::string& name, Variables& knowledge, const Dimensions& dimensions,
24  bool delete_vars, const KnowledgeUpdateSettings& settings,
25  const std::string& delimiter)
26  : BaseContainer(name, settings),
27  context_(knowledge.get_context()),
28  delimiter_(delimiter)
29 {
30  size_ = get_size_ref();
31  resize(dimensions, delete_vars);
32 }
33 
35  const IntegerVector2D& rhs)
36  : BaseContainer(rhs),
37  context_(rhs.context_),
38  vector_(rhs.vector_),
39  size_(rhs.size_),
40  delimiter_(rhs.delimiter_)
41 {
42 }
43 
45 
47 {
48  if (context_ && name_ != "")
49  {
50  ContextGuard context_guard(*context_);
51 
52  Indices dimensions = size();
53 
54  if (dimensions.x > 0 && dimensions.y > 0)
55  {
56  for (size_t i = 0; i < dimensions.x; ++i)
57  {
58  for (size_t j = 0; j < dimensions.y; ++j)
59  {
60  context_->mark_modified(vector_[i][j]);
61  }
62  }
63  }
64 
65  context_->mark_modified(size_);
66  }
67 }
68 
70 {
71  std::stringstream result;
72 
73  result << "IntegerVector2D: ";
74 
75  if (context_)
76  {
77  ContextGuard context_guard(*context_);
78 
79  Indices dimensions = size();
80 
81  result << this->name_;
82  result << " [" << dimensions.x << "," << dimensions.y << "]";
83  result << " = [";
84 
85  if (dimensions.x > 0 && dimensions.y > 0)
86  {
87  for (size_t i = 0; i < dimensions.x; ++i)
88  {
89  result << context_->get(vector_[i][0]).to_string();
90 
91  for (size_t j = 1; j < dimensions.y; ++j)
92  {
93  result << ", " << context_->get(vector_[i][j]).to_string();
94  }
95  result << "\n";
96  }
97  }
98 
99  result << "]";
100  }
101 
102  return result.str();
103 }
104 
106 {
107  modify();
108 }
109 
111  void)
112 {
113  return get_debug_info();
114 }
115 
118 {
119  return new IntegerVector2D(*this);
120 }
121 
123  const Indices& index)
124 {
125  if (context_)
126  {
127  ContextGuard context_guard(*context_);
128  if (index.x < vector_.size() && index.y < vector_[index.x].size())
129  context_->mark_modified(vector_[index.x][index.y]);
130  }
131 }
132 
134  const IntegerVector2D& rhs)
135 {
136  if (this != &rhs)
137  {
138  MADARA_GUARD_TYPE guard(mutex_), guard2(rhs.mutex_);
139 
140  this->context_ = rhs.context_;
141  this->name_ = rhs.name_;
142  this->settings_ = rhs.settings_;
143  this->size_ = rhs.size_;
144  this->vector_ = rhs.vector_;
145  this->delimiter_ = rhs.delimiter_;
146  }
147 }
148 
151 {
152  VariableReference ref;
153 
154  if (context_ && name_ != "")
155  {
156  KnowledgeUpdateSettings keep_local(true);
157  std::stringstream buffer;
158 
159  ContextGuard context_guard(*context_);
160 
161  buffer << name_;
162  buffer << delimiter_;
163  buffer << "size";
164 
165  ref = context_->get_ref(buffer.str(), keep_local);
166  }
167 
168  return ref;
169 }
170 
172  const Dimensions& dimensions, bool delete_vars)
173 {
174  if (context_ && name_ != "")
175  {
176  ContextGuard context_guard(*context_);
177 
179  "IntegerVector2D::resize: resizing to [%d,%d]\n", (int)dimensions.x,
180  (int)dimensions.y);
181 
182  bool is_reset = dimensions.x == 0 && dimensions.y == 0;
183 
184  if (!size_.is_valid())
185  {
186  size_ = get_size_ref();
187  }
188 
189  // save the old size
190  Dimensions old_size = size();
191  Dimensions new_size(dimensions);
192 
194  "IntegerVector2D::resize: old size is [%d,%d]\n", (int)old_size.x,
195  (int)old_size.y);
196 
197  if (is_reset)
198  {
200  "IntegerVector2D::resize: new size is being reset to size in KB\n");
201 
202  new_size.x = old_size.x;
203  new_size.y = old_size.y;
204  }
205  else
206  {
208  "IntegerVector2D::resize: using dimensions passed in.\n");
209 
210  // set the new size
211  std::vector<KnowledgeRecord::Integer> update(2);
212  update[0] = dimensions.x;
213  update[1] = dimensions.y;
214 
215  context_->set(size_, update, settings_);
216  }
217 
218  // correct the vector for the new size
219  vector_.resize(new_size.x);
220 
221  for (size_t i = 0; i < new_size.x; ++i)
222  {
224  "IntegerVector2D::resize: resizing vector_[%d] to %d.\n", (int)i,
225  (int)new_size.y);
226 
227  vector_[i].resize(new_size.y);
228 
229  // create any new VariableReference needed, default is end of old cols
230 
231  size_t start = old_size.y;
232 
233  // if you've gained rows and this is a new row, reset start to 0
234  if (is_reset || (old_size.x < new_size.x && i >= old_size.x))
235  {
236  start = 0;
237  }
238 
240  "IntegerVector2D::resize: creating var_refs from %d->%d.\n",
241  (int)start, (int)new_size.y);
242 
243  // create new VariableReferences
244  for (size_t j = start; j < new_size.y; ++j)
245  {
246  std::stringstream var_name;
247  var_name << this->name_;
248  var_name << delimiter_;
249  var_name << i;
250  var_name << delimiter_;
251  var_name << j;
252 
253  vector_[i][j] = context_->get_ref(var_name.str(), settings_);
254  }
255  }
256 
257  // delete if we need to delete
258  if ((new_size.x < old_size.x || new_size.y < old_size.y) && delete_vars)
259  {
261  "IntegerVector2D::resize: deleting refs: rows: 0->%d.\n",
262  (int)old_size.x);
263 
264  // delete within the old rows
265  for (size_t i = 0; i < old_size.x; ++i)
266  {
267  // by default, delete from new col size to old col size
268  size_t start = new_size.y;
269 
270  // the exception is when we are deleting the entire row
271  if (old_size.x > new_size.x && i >= new_size.x)
272  {
273  start = 0;
274  }
275 
277  "IntegerVector2D::resize: deleting refs: %d:%d->%d.\n", (int)i,
278  (int)start, (int)old_size.x);
279 
280  // delete old columns
281  for (size_t j = start; j < old_size.y; ++j)
282  {
283  std::stringstream var_name;
284  var_name << this->name_;
285  var_name << delimiter_;
286  var_name << i;
287  var_name << delimiter_;
288  var_name << j;
289 
291  "IntegerVector2D::resize: deleting ref: %s.\n",
292  var_name.str().c_str());
293 
294  context_->delete_variable(var_name.str(), settings_);
295  }
296  }
297  }
298  }
299 }
300 
303 {
304  Indices cur_size;
305 
306  if (context_)
307  {
308  KnowledgeRecord record;
309  // lock the KnowledgeBase during access
310  {
311  ContextGuard context_guard(*context_);
312 
313  record = context_->get(size_);
314  }
315 
316  std::vector<KnowledgeRecord::Integer> sizes(record.to_integers());
317  cur_size.x = (size_t)(sizes.size() >= 2 ? sizes[0] : 0);
318  cur_size.y = (size_t)(sizes.size() >= 2 ? sizes[1] : 0);
319  }
320 
321  return cur_size;
322 }
323 
325  const std::string& var_name, KnowledgeBase& knowledge,
326  const Indices& dimensions)
327 {
328  if (context_ != &(knowledge.get_context()) || name_ != var_name)
329  {
330  context_ = &(knowledge.get_context());
331 
332  ContextGuard context_guard(*context_);
333 
334  name_ = var_name;
335 
336  vector_.clear();
337 
338  size_ = get_size_ref();
339 
340  resize(dimensions);
341  }
342 }
343 
345  const std::string& var_name, Variables& knowledge,
346  const Indices& dimensions)
347 {
348  if (context_ != knowledge.get_context() || name_ != var_name)
349  {
350  context_ = knowledge.get_context();
351 
352  ContextGuard context_guard(*context_);
353 
354  name_ = var_name;
355 
356  vector_.clear();
357  resize(dimensions);
358  }
359 }
360 
362  const std::string& var_name, ThreadSafeContext& knowledge,
363  const Indices& dimensions)
364 {
365  if (context_ != &knowledge || name_ != var_name)
366  {
367  context_ = &knowledge;
368 
369  ContextGuard context_guard(*context_);
370 
371  name_ = var_name;
372 
373  vector_.clear();
374  resize(dimensions);
375  }
376 }
377 
379  const std::string& delimiter)
380 {
381  delimiter_ = delimiter;
382  if (context_)
383  {
384  ContextGuard context_guard(*context_);
385 
386  vector_.clear();
387  resize({0, 0});
388  }
389 }
390 
392 {
393  return delimiter_;
394 }
395 
397  std::vector<std::vector<type> >& target) const
398 {
399  KnowledgeUpdateSettings keep_local(true);
400 
401  if (context_)
402  {
403  ContextGuard context_guard(*context_);
404 
405  Indices dimensions = size();
406 
407  target.resize(dimensions.x);
408 
409  for (size_t i = 0; i < dimensions.x; ++i)
410  {
411  target[i].resize(dimensions.y);
412  for (size_t j = 0; j < dimensions.y; ++j)
413  {
414  target[i][j] = context_->get(vector_[i][j], keep_local).to_integer();
415  }
416  }
417  }
418 }
419 
422  const Indices& index) const
423 {
424  type result(0);
425 
426  KnowledgeUpdateSettings keep_local(true);
427 
428  if (context_)
429  {
430  ContextGuard context_guard(*context_);
431 
432  if (index.x < vector_.size() && index.y < vector_[index.x].size())
433  {
434  result =
435  context_->get(vector_[index.x][index.y], keep_local).to_integer();
436  }
437  }
438 
439  return result;
440 }
441 
443  const Indices& index) const
444 {
445  bool result(false);
446 
447  if (context_)
448  {
449  ContextGuard context_guard(*context_);
450 
451  if (index.x < vector_.size() && index.y < vector_[index.x].size())
452  {
453  result = context_->exists(vector_[index.x][index.y]);
454  }
455  }
456 
457  return result;
458 }
459 
461  const Indices& index, type value)
462 {
463  int result = -1;
464 
465  if (context_)
466  {
467  ContextGuard context_guard(*context_);
468 
469  if (index.x < vector_.size() && index.y < vector_[index.x].size())
470  {
471  result = context_->set(vector_[index.x][index.y], value, settings_);
472  }
473  }
474 
475  return result;
476 }
477 
479  const std::vector<std::vector<type> >& value)
480 {
481  int result = 0;
482 
483  if (context_)
484  {
485  ContextGuard context_guard(*context_);
486 
487  for (size_t i = 0; i < value.size() && i < vector_.size(); ++i)
488  {
489  for (size_t j = 0; j < value[i].size() && j < vector_[i].size(); ++j)
490  {
491  context_->set(vector_[i][j], value[i][j], settings_);
492  }
493  }
494  }
495 
496  return result;
497 }
498 
500  const Indices& index, type value, const KnowledgeUpdateSettings& settings)
501 {
502  int result = -1;
503 
504  if (context_)
505  {
506  ContextGuard context_guard(*context_);
507 
508  if (index.x < vector_.size() && index.y < vector_[index.x].size())
509  {
510  result = context_->set(vector_[index.x][index.y], value, settings);
511  }
512  }
513 
514  return result;
515 }
516 
518  const std::vector<std::vector<type> >& value,
519  const KnowledgeUpdateSettings& settings)
520 {
521  int result = 0;
522 
523  if (context_)
524  {
525  ContextGuard context_guard(*context_);
526 
527  for (size_t i = 0; i < value.size() && i < vector_.size(); ++i)
528  {
529  for (size_t j = 0; j < value[i].size() && j < vector_[i].size(); ++j)
530  {
531  context_->set(vector_[i][j], value[i][j], settings);
532  }
533  }
534  }
535 
536  return result;
537 }
538 
540  const Indices& index, uint32_t quality,
541  const KnowledgeReferenceSettings& settings)
542 {
543  if (context_)
544  {
545  ContextGuard context_guard(*context_);
546 
547  if (index.x < vector_.size() && index.y < vector_[index.x].size())
548  context_->set_quality(
549  vector_[index.x][index.y].get_name(), quality, true, settings);
550  }
551 }
552 
554 {
555  bool result(false);
556 
558  "IntegerVector2D::is_true: Checking for truth\n");
559 
560  if (context_)
561  {
562  ContextGuard context_guard(*context_);
563 
564  result = true;
565 
567  "IntegerVector2D::is_true: context was not null. Result changed to "
568  "%d\n",
569  (int)result);
570 
571  for (size_t i = 0; i < vector_.size(); ++i)
572  {
573  for (size_t j = 0; j < vector_[i].size(); ++i)
574  {
576  "IntegerVector2D::is_true: checking [%d,%d], is_false of %d. \n",
577  (int)i, (int)j, (int)context_->get(vector_[i][j]).is_false());
578 
579  if (context_->get(vector_[i][j]).is_false())
580  {
582  "IntegerVector2D::is_true: result is false, breaking\n");
583 
584  result = false;
585  break;
586  }
587  }
588  }
589 
590  if (vector_.size() == 0)
591  result = false;
592  }
593 
595  "IntegerVector2D::is_true: final result is %d\n", (int)result);
596 
597  return result;
598 }
599 
601 {
602  return !is_true();
603 }
604 
606 {
607  return is_true();
608 }
609 
611 {
612  return is_false();
613 }
#define madara_logger_log(loggering, level,...)
Fast version of the madara::logger::log method.
Definition: Logger.h:20
const ThreadSafeContext * context_
A thread-safe guard for a context or knowledge base.
Definition: ContextGuard.h:24
This class provides a distributed knowledge base to users.
Definition: KnowledgeBase.h:45
This class encapsulates an entry in a KnowledgeBase.
std::vector< Integer > to_integers(void) const
converts the value to a vector of integers
Settings for applying knowledge updates.
Settings for applying knowledge updates.
This class stores variables and their values for use by any entity needing state information in a thr...
Optimized reference to a variable within the knowledge base.
Provides an interface for external functions into the MADARA KaRL variable settings.
Definition: Variables.h:53
This class is an abstract base class for all containers.
Definition: BaseContainer.h:34
KnowledgeUpdateSettings settings_
Settings for modifications.
std::string name_
Prefix of variable.
MADARA_LOCK_TYPE mutex_
guard for access and changes
Manages a 2D array of integers as a virtual overlay in the KnowledgeBase.
std::string get_delimiter(void)
Gets the delimiter for adding and detecting subvariables.
VariableReference size_
Reference to the size of 2D array.
bool exists(const Indices &index) const
Checks to see if the index has ever been assigned a value.
std::string get_debug_info(void)
Returns the type of the container along with name and any other useful information.
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.
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.
virtual std::string get_debug_info_(void)
Returns the type of the container along with name and any other useful information.
type operator[](const Indices &index) const
Retrieves an index from the multi-dimensional array.
int set(const Indices &index, type value)
Sets a knowledge variable to a specified value.
void copy_to(std::vector< std::vector< type > > &target) const
Copies the vector elements to an STL vector.
KnowledgeRecord::Integer type
convenience typedef for element type
ThreadSafeContext * context_
Variable context that we are modifying.
std::vector< std::vector< VariableReference > > vector_
Values of the array.
void operator=(const IntegerVector2D &rhs)
Assignment operator.
bool is_true(void) const
Determines if all values in the vector are true.
IntegerVector2D(const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings(), const std::string &delimiter=".")
Default constructor.
virtual BaseContainer * clone(void) const
Clones this container.
void modify(void)
Mark the vector as modified.
void resize(const Dimensions &dimensions, bool delete_vars=true)
Resizes the vector.
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 ...
std::string delimiter_
Delimiter for the prefix to subvars.
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.
Dimensions size(void) const
Returns the size of the local vector.
void set_name(const std::string &var_name, KnowledgeBase &knowledge, const Dimensions &dimensions={0, 0})
Sets the variable name that this refers to.
void set_delimiter(const std::string &delimiter)
Sets the delimiter for adding and detecting subvariables.
constexpr string_t string
Provides functions and classes for the distributed knowledge base.