1 #ifndef _KNOWLEDGE_RECORD_CPP_ 2 #define _KNOWLEDGE_RECORD_CPP_ 16 int madara_double_precision (-1);
18 bool madara_use_scientific (
false);
26 return madara_double_precision;
33 "KnowledgeRecord::set_precision:" \
34 " setting precision to %d\n", madara_double_precision);
36 madara_double_precision = new_precision;
43 "KnowledgeRecord::set_fixed:" \
44 " setting output format to std::fixed\n");
46 madara_use_scientific =
false;
53 "KnowledgeRecord::set_scientific:" \
54 " setting output format to std::scientific\n");
56 madara_use_scientific =
true;
61 const std::string & filename, uint32_t read_as_type)
65 bool add_zero_char =
false;
70 std::string::size_type position = filename.rfind (
'.');
72 filename.size () - position);
77 extension ==
".txt" || extension ==
".xml")
87 || extension ==
".txt" || extension ==
".xml")
90 str_value_ = std::make_shared<std::string> ((
char *)buffer, size-1);
94 else if (extension ==
".xml")
101 unsigned char *ucbuf = (
unsigned char *)buffer;
104 if (extension ==
".jpg" || read_as_type ==
IMAGE_JPEG)
137 (
void *)buffer.c_str (), buffer.size ());
153 std::stringstream buffer;
180 std::stringstream buffer;
196 std::vector <KnowledgeRecord::Integer>
205 unsigned int size = (
unsigned int)this->size ();
206 integers.resize (size);
214 const Integer * ptr_temp = &(*int_array_)[0];
216 for (
unsigned int i = 0; i <
size; ++i)
217 integers[i] = ptr_temp[i];
223 const double * ptr_temp = &(*double_array_)[0];
225 for (
unsigned int i = 0; i <
size; ++i)
226 integers[i] =
Integer (ptr_temp[i]);
232 for (
unsigned int i = 0; i <
size; ++i)
233 integers[i] =
Integer (ptr_temp[i]);
237 const unsigned char * ptr_temp = &(*file_value_)[0];
239 for (
unsigned int i = 0; i <
size; ++i)
240 integers[i] =
Integer (ptr_temp[i]);
255 unsigned int size = (
unsigned int)this->size ();
256 doubles.resize (size);
262 const Integer * ptr_temp = &(*int_array_)[0];
264 for (
unsigned int i = 0; i <
size; ++i)
265 doubles[i] =
double (ptr_temp[i]);
271 const double * ptr_temp = &(*double_array_)[0];
273 for (
unsigned int i = 0; i <
size; ++i)
274 doubles[i] = ptr_temp[i];
280 for (
unsigned int i = 0; i <
size; ++i)
281 doubles[i] =
double (ptr_temp[i]);
285 const unsigned char * ptr_temp = &(*file_value_)[0];
287 for (
unsigned int i = 0; i <
size; ++i)
288 doubles[i] =
double (ptr_temp[i]);
305 " type_ is %d\n",
type_);
307 std::stringstream buffer;
313 const Integer * ptr_temp = &(*int_array_)[0];
321 for (uint32_t i = 1; i <
size; ++i, ++ptr_temp)
322 buffer << delimiter << *ptr_temp;
327 if (!madara_use_scientific)
330 "KnowledgeRecord::to_string: using fixed format\n");
332 buffer << std::fixed;
337 "KnowledgeRecord::to_string: using scientific format\n");
339 buffer << std::scientific;
342 if (madara_double_precision >= 0)
345 buffer << std::setprecision (madara_double_precision);
348 " precision set to %d\n", madara_double_precision);
353 " precision set to default\n", madara_double_precision);
361 if (!madara_use_scientific)
364 "KnowledgeRecord::to_string: using fixed format\n");
366 buffer << std::fixed;
371 "KnowledgeRecord::to_string: using scientific format\n");
373 buffer << std::scientific;
376 if (madara_double_precision >= 0)
378 buffer << std::setprecision (madara_double_precision);
381 " precision set to %d\n", madara_double_precision);
386 " precision set to default\n", madara_double_precision);
389 const double * ptr_temp = &(*double_array_)[0];
397 for (uint32_t i = 1; i <
size; ++i, ++ptr_temp)
398 buffer << delimiter << *ptr_temp;
402 buffer <<
"binary:size=";
405 return buffer.str ();
420 buffer =
new char [
size];
426 buffer =
new char [
size];
432 buffer =
new char [
size];
437 size =
sizeof(double);
438 buffer =
new char [
size];
444 buffer =
new char [
size];
450 buffer =
new char [
size];
457 return (
unsigned char *)buffer;
471 last = std::min <unsigned int> (last, size - 1);
474 char * new_buffer =
new char [last - first + 2];
476 memcpy (new_buffer,
str_value_->c_str () + first, last - first + 1);
477 new_buffer[last-first + 1] = 0;
486 last = std::min <unsigned int> (last, (
unsigned int)size - 1);
489 uint32_t bufsize = last - first + 1;
490 unsigned char * new_buffer =
new unsigned char [bufsize];
492 memcpy (new_buffer, &(*
file_value_)[0] + first, last - first + 1);
502 last = std::min <unsigned int> (last, size - 1);
503 uint32_t bufsize = last - first + 1;
506 integers.resize (bufsize);
507 Integer * ptr_temp = &(*int_array_)[0];
509 for (
unsigned int i = first; i <= last; ++i, ++ptr_temp)
510 integers[i] = *ptr_temp;
519 last = std::min <unsigned int> (last, size - 1);
520 uint32_t bufsize = last - first + 1;
523 doubles.resize (bufsize);
524 double * ptr_temp = &(*double_array_)[0];
526 for (
unsigned int i = first; i <= last; ++i, ++ptr_temp)
527 doubles[i] = *ptr_temp;
550 result = lhs < other;
556 result = lhs < other;
579 result = temp < other;
590 result = temp < other;
607 result = lhs < other;
614 result = lhs < other;
636 result = lhs <= other;
642 result = lhs <= other;
665 result = temp <= other;
676 result = temp <= other;
693 result = lhs <= other;
700 result = lhs <= other;
797 result = lhs == other;
805 result = lhs == other;
826 result = lhs > other;
832 result = lhs > other;
856 result = lhs > other;
867 result = lhs > other;
884 result = lhs > other;
892 result = lhs > other;
913 result = lhs >= other;
919 result = lhs >= other;
943 result = lhs >= other;
954 result = lhs >= other;
971 result = lhs >= other;
979 result = lhs >= other;
1027 std::vector<Integer> tmp(index + 1);
1053 std::vector<Integer> tmp(index + 1);
1061 size_t cur_size =
size ();
1063 if (cur_size == new_size) {
1069 if (new_size > cur_size)
1099 const std::string & key,
unsigned int , uint64_t ,
1104 if (key.length () > 0)
1107 " attempting to set %s=%s\n", key.c_str (),
to_string ().c_str ());
1134 " received data[%s]=%s.\n",
1138 else if (result == 0)
1141 " discarded data[%s]=%s as the value was already set.\n",
1144 else if (result == -1)
1147 " discarded data due to null key.\n");
1149 else if (result == -2)
1152 " discarded data[%s]=%s due to lower quality.\n",
1155 else if (result == -3)
1158 " discarded data[%s]=%" PRId64
" due to older timestamp.\n",
1169 " checking if record is non-zero.\n");
1176 return value < 0 || value > 0;
1194 #endif // _KNOWLEDGE_RECORD_CPP_ This class encapsulates an entry in a KnowledgeBase.
int apply(madara::knowledge::ThreadSafeContext &context, const std::string &key, unsigned int quality, uint64_t clock, bool perform_lock)
Apply the knowledge record to a context, given some quality and clock.
bool is_true(void) const
Checks to see if the record is true.
int update_record_from_external(const std::string &key, const knowledge::KnowledgeRecord &rhs, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings(true))
Atomically sets if the variable value meets update conditions.
static void set_fixed(void)
Sets the output format for doubles to std::fixed.
void emplace_integers(Args &&...args)
Construct a vector of integers within this KnowledgeRecord.
madara::knowledge::KnowledgeRecord KnowledgeRecord
void emplace_file(Args &&...args)
Construct a file (vector of unsigned char) within this KnowledgeRecord.
int read_file(const std::string &filename, void *&buffer, size_t &size, bool add_zero_char)
Reads a file into a provided void pointer.
double to_double(void) const
converts the value to a float/double.
KnowledgeRecord inc_index(size_t index)
increments the value at the index to the specified value.
void set_file(const unsigned char *new_value, size_t size)
sets the value to an unknown file type
void lock(void) const
Locks the mutex on this context.
void clear_value(void) noexcept
clears any dynamic values.
bool is_binary_file_type(void) const
returns true if the knowledge record has a binary file type
ssize_t to_file(const std::string &filename) const
writes the value to a file
This class stores variables and their values for use by any entity needing state information in a thr...
KnowledgeRecord fragment(unsigned int first, unsigned int last)
returns a record containing a fragment of the character buffer.
std::shared_ptr< std::vector< Integer > > int_array_
std::string & lower(std::string &input)
Converts the string to lower.
MADARA_EXPORT utility::Refcounter< logger::Logger > global_logger
bool exists(void) const
Checks if record exists (i.e., is not uncreated)
bool is_double_type(void) const
returns if the record is a double type (DOUBLE, DOUBLE_ARRAY)
logger::Logger * logger_
the logger used for any internal debugging information
bool operator>(const KnowledgeRecord &rhs) const
Greater than.
bool operator>=(const KnowledgeRecord &rhs) const
Greater than or equal to.
void set_value(const KnowledgeRecord &new_value)
Sets the value from another KnowledgeRecord, does not copy clock and write_quality.
bool operator==(const KnowledgeRecord &rhs) const
Equal to.
#define madara_logger_ptr_log(logger, level,...)
Fast version of the madara::logger::log method for Logger pointers.
void unlock(void) const
Unlocks the mutex on this context.
std::vector< double > to_doubles(void) const
converts the value to a vector of doubles
static void set_precision(int new_precision)
Sets the double precision of a double record when using to_string ().
std::shared_ptr< std::vector< double > > double_array_
void set_index(size_t index, T value)
sets the value at the index to the specified value.
Integer to_integer(void) const
converts the value to an integer.
static const KnowledgeUpdateSettings GLOBAL_AS_LOCAL_NO_EXPAND
std::shared_ptr< std::vector< unsigned char > > file_value_
void set_changed(void)
Force a change to be registered, waking up anyone waiting on entry.
bool operator<(const KnowledgeRecord &rhs) const
Less than.
static void set_scientific(void)
Sets the output format for doubles to std::scientific.
void unshare(void)
If this record holds a shared_ptr, make a copy of the underlying value so it has an exclusive copy...
std::vector< Integer > to_integers(void) const
converts the value to a vector of integers
Provides functions and classes for the distributed knowledge base.
uint32_t type_
type of variable (INTEGER, DOUBLE, STRING, FILE, IMAGE)
KnowledgeRecord dec_index(size_t index)
decrements the value at the index to the specified value.
bool operator<=(const KnowledgeRecord &rhs) const
Less than or equal to.
Copyright (c) 2015 Carnegie Mellon University.
KnowledgeRecord retrieve_index(size_t index) const
retrieves the value at an array index.
int read_file(const std::string &filename, uint32_t read_as_type=0)
reads a file and sets the type appropriately according to the extension
bool is_integer_type(void) const
returns if the record is a integer type (INTEGER, INTEGER_ARRAY)
bool is_false(void) const
Checks to see if the record is false.
bool is_string_type(void) const
returns true if the record is a string type (STRING, XML, TEXT_FILE)
std::string to_string(const std::string &delimiter=", ") const
converts the value to a string.
void resize(size_t new_size)
resizes an array to a new size
ssize_t write_file(const std::string &filename, void *buffer, size_t size)
Writes a file with provided contents.
uint32_t size(void) const
returns the size of the value
std::shared_ptr< std::string > str_value_
unsigned char * to_unmanaged_buffer(size_t &size) const
returns an unmanaged buffer that the user will have to take care of (this is a copy of the internal v...
static int get_precision(void)
Gets the current double precision for double to string conversion.