1 #ifndef _MADARA_KNOWLEDGE_BASE_INL_ 2 #define _MADARA_KNOWLEDGE_BASE_INL_ 41 typename std::enable_if<std::is_integral<T>::value,
64 typename std::enable_if<std::is_integral<T>::value,
87 typename std::enable_if<std::is_floating_point<T>::value,
99 result =
impl_->set (key, (
double)value, settings);
103 result =
context_->
set (key, (
double)value, settings);
110 typename std::enable_if<std::is_floating_point<T>::value,
122 result =
impl_->set (variable, (
double)value, settings);
126 result =
context_->
set (variable, (
double)value, settings);
173 result =
impl_->get_log_level ();
188 return impl_->get_logger ();
202 impl_->attach_logger (logger);
215 impl_->set_log_level (level);
251 impl_->copy (*source.
impl_.get (), copy_set, clean_copy);
273 result =
impl_->apply_modified (settings);
288 impl_->close_transport ();
300 result =
impl_->get (key, settings);
318 var =
impl_->get_ref (key, settings);
337 result =
impl_->get (variable, settings);
356 result =
impl_->retrieve_index (key, index, settings);
375 result =
impl_->retrieve_index (variable, index, settings);
396 result =
impl_->read_file (knowledge_key, filename, settings);
416 result =
impl_->read_file (variable, filename, settings);
429 const unsigned char * value,
size_t size,
438 result =
impl_->set_file (
439 impl_->get_ref (key, settings), value, size, settings);
454 const unsigned char * value,
size_t size,
461 result =
impl_->set_file (variable, value, size, settings);
474 const unsigned char * value,
size_t size,
483 result =
impl_->set_jpeg (
484 impl_->get_ref (key, settings), value, size, settings);
489 impl_->get_ref (key, settings), value, size, settings);
499 const unsigned char * value,
size_t size,
506 result =
impl_->set_jpeg (variable, value, size, settings);
524 result =
impl_->write_file (knowledge_key, filename);
539 result =
impl_->expand_statement (statement);
556 impl_->mark_modified (variable);
571 impl_->mark_modified (name, settings);
589 result =
impl_->set (variable, std::move(value), settings);
593 result =
context_->
set (variable, std::move(value), settings);
609 result =
impl_->set (key, value, settings);
629 result =
impl_->set (key, std::move(value), settings);
633 result =
context_->
set (key, std::move(value), settings);
649 result =
impl_->set (variable, value, settings);
653 result =
context_->
set (variable, value, settings);
663 size_t index, T&& value,
670 result =
impl_->set_index (key, index, std::forward<T>(value), settings);
684 size_t index, T&& value,
691 result =
impl_->set_index (variable, index, std::forward<T>(value), settings);
695 result =
context_->
set_index (variable, index, std::forward<T>(value), settings);
712 result =
impl_->set (key, value, size, settings);
716 result =
context_->
set (key, value, size, settings);
733 result =
impl_->set (variable, value, size, settings);
737 result =
context_->
set (variable, value, size, settings);
746 const std::vector <KnowledgeRecord::Integer> & value,
753 result =
impl_->set (key, value, settings);
766 const std::vector <KnowledgeRecord::Integer> & value,
773 result =
impl_->set (variable, value, settings);
777 result =
context_->
set (variable, value, settings);
786 std::vector <KnowledgeRecord::Integer> && value,
793 result =
impl_->set (key, std::move(value), settings);
797 result =
context_->
set (key, std::move(value), settings);
806 std::vector <KnowledgeRecord::Integer> && value,
813 result =
impl_->set (variable, std::move(value), settings);
817 result =
context_->
set (variable, std::move(value), settings);
826 const double * value,
834 result =
impl_->set (key, value, size, settings);
838 result =
context_->
set (key, value, size, settings);
847 const double * value,
855 result =
impl_->set (variable, value, size, settings);
859 result =
context_->
set (variable, value, size, settings);
868 const std::vector <double> & value,
875 result =
impl_->set (key, value, settings);
888 std::vector <double> && value,
895 result =
impl_->set (key, std::move(value), settings);
899 result =
context_->
set (key, std::move(value), settings);
908 const std::vector <double> & value,
915 result =
impl_->set (variable, value, settings);
919 result =
context_->
set (variable, value, settings);
928 std::vector <double> && value,
935 result =
impl_->set (variable, std::move(value), settings);
939 result =
context_->
set (variable, std::move(value), settings);
954 result =
impl_->set (key, value, settings);
974 result =
impl_->set (variable, value, settings);
978 result =
context_->
set (variable, value, settings);
993 result =
impl_->set (key, std::move(value), settings);
997 result =
context_->
set (key, std::move(value), settings);
1013 result =
impl_->set (variable, std::move(value), settings);
1017 result =
context_->
set (variable, std::move(value), settings);
1032 impl_->set_quality (key, quality, settings);
1046 bool result =
false;
1050 result =
impl_->exists (key, settings);
1065 bool result =
false;
1069 result =
impl_->exists (variable, settings);
1082 return impl_->transport_settings ();
1087 unsigned int level)
const 1091 impl_->print (level);
1101 unsigned int level)
const 1105 impl_->print (level);
1122 impl_->to_string (target,
1123 array_delimiter, record_delimiter, key_val_delimiter);
1128 array_delimiter, record_delimiter, key_val_delimiter);
1135 const std::string & statement,
unsigned int level)
const 1139 impl_->print (statement, level);
1151 bool result (
false);
1155 result =
impl_->clear (key, settings);
1170 impl_->clear (erase);
1183 impl_->clear_map ();
1217 #ifndef _MADARA_NO_KARL_ 1227 result =
impl_->compile (expression);
1243 result =
impl_->evaluate (expression, settings);
1264 result =
impl_->evaluate (expression, settings);
1284 result =
impl_->evaluate (root, settings);
1303 impl_->define_function (name, func);
1319 impl_->define_function (name, func);
1327 #ifdef _MADARA_JAVA_ 1335 impl_->define_function (name, func);
1344 #ifdef _MADARA_PYTHON_CALLBACKS_ 1349 const std::string & name, boost::python::object callable)
1353 impl_->define_function (name, callable);
1374 impl_->define_function (name, expression);
1393 impl_->define_function (name, expression);
1411 result = this->
wait (ce, settings);
1413 else if (
impl_.get ())
1415 result =
impl_->wait (expression, settings);
1421 #endif // _MADARA_NO_KARL_ 1427 impl_->activate_transport ();
1439 result =
impl_->attach_transport (transport);
1452 result =
impl_->get_num_transports ();
1466 result =
impl_->attach_transport (
id, settings);
1480 result =
impl_->remove_transport (index);
1495 else if (
impl_.get ())
1497 result = &(
impl_->get_context ());
1510 else if (
impl_.get ())
1512 impl_->clear_modifieds ();
1523 else if (
impl_.get ())
1525 impl_->add_modifieds (modifieds);
1538 else if (
impl_.get ())
1540 return impl_->save_modifieds ();
1543 return default_result;
1555 result =
impl_->send_modifieds (prefix, settings);
1570 else if (
impl_.get ())
1572 result =
impl_->debug_modifieds ();
1589 result =
impl_->get_id ();
1614 std::vector <KnowledgeRecord> & target)
1622 else if (
impl_.get ())
1624 result =
impl_->to_vector (subject, start, end, target);
1638 else if (
impl_.get ())
1640 impl_->get_matches (prefix, suffix, matches);
1654 std::map <std::string, KnowledgeRecord> & target)
1662 else if (
impl_.get ())
1664 result =
impl_->to_map (expression, target);
1675 std::vector <std::string> & next_keys,
1676 std::map <std::string, KnowledgeRecord> & result,
1679 size_t result_size = 0;
1684 prefix, delimiter, suffix, next_keys, result, just_keys);
1686 else if (
impl_.get ())
1688 result_size =
impl_->to_map (
1689 prefix, delimiter, suffix, next_keys, result, just_keys);
1703 else if (
impl_.get ())
1705 return impl_->to_map (prefix);
1719 else if (
impl_.get ())
1721 return impl_->to_map_stripped (prefix);
1737 else if (
impl_.get ())
1739 result =
impl_->save_context (filename);
1755 else if (
impl_.get ())
1757 result =
impl_->save_context (settings);
1774 else if (
impl_.get ())
1776 result =
impl_->save_as_json (filename);
1792 else if (
impl_.get ())
1794 result =
impl_->save_as_json (settings);
1810 else if (
impl_.get ())
1812 result =
impl_->save_as_karl (filename);
1828 else if (
impl_.get ())
1830 result =
impl_->save_as_karl (settings);
1840 bool reset_modifieds)
1848 else if (
impl_.get ())
1850 result =
impl_->save_checkpoint (filename, reset_modifieds);
1866 else if (
impl_.get ())
1868 result =
impl_->save_checkpoint (settings);
1887 else if (
impl_.get ())
1889 result =
impl_->load_context (filename, use_id, settings);
1908 else if (
impl_.get ())
1910 result =
impl_->load_context (filename, meta, use_id, settings);
1927 else if (
impl_.get ())
1929 result =
impl_->load_context (checkpoint_settings, update_settings);
1942 else if (
impl_.get ())
1944 impl_->wait_for_change ();
1956 result =
impl_->setup_unique_hostport (host);
1970 #endif // _MADARA_KNOWLEDGE_BASE_INL_ This class encapsulates an entry in a KnowledgeBase.
std::map< std::string, bool > CopySet
Typedef for set of copyable keys.
int set(const std::string &key, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to the specific record.
This class provides a distributed knowledge base implementation.
int read_file(const std::string &knowledge_key, const std::string &filename, const EvalSettings &settings=EvalSettings(false, false, true, false, false))
Read a file into the knowledge base.
int apply_modified(const EvalSettings &settings=EvalSettings())
Applies current time and modified to all global variables and tries to send them. ...
size_t remove_transport(size_t index)
Removes a transport.
int64_t save_context(const std::string &filename) const
Saves the context to a file.
void get_matches(const std::string &prefix, const std::string &suffix, VariableReferences &matches)
Creates an iteration of VariableReferences to all keys matching the prefix and suffix.
helper type for specifying template type parameters using a function argument instead of inside expli...
size_t to_vector(const std::string &subject, unsigned int start, unsigned int end, std::vector< KnowledgeRecord > &target)
Fills a vector with Knowledge Records that begin with a common subject and have a finite range of int...
int64_t save_context(const std::string &filename, const std::string &id="") const
Saves the context to a file.
size_t to_map(const std::string &subject, std::map< std::string, knowledge::KnowledgeRecord > &target)
Fills a variable map with Knowledge Records that match an expression.
VariableReferences save_modifieds(void) const
Saves the list of modified records to use later for resending.
int set_file(const std::string &key, const unsigned char *value, size_t size, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to an arbitrary string.
void unlock(void)
Unlocks the context to allow updates over the network (is only necessary if the context has been expl...
int set_jpeg(const std::string &key, const unsigned char *value, size_t size, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to a JPEG image.
size_t get_num_transports(void)
Gets the number of transports.
void copy(const ThreadSafeContext &source, const KnowledgeRequirements &settings)
Copies variables and values from source to this context.
std::string debug_modifieds(void) const
Retrieves a stringified list of all modified variables that are ready to send over transport on next ...
int set_index(const VariableReference &variable, size_t index, T &&value, const EvalSettings &settings=EvalSettings(false, false, true, false, false))
Atomically sets the value of an array index to a value.
void lock(void) const
Locks the mutex on this context.
int send_modifieds(const std::string &prefix="KnowledgeBase::send_modifieds", const EvalSettings &settings=EvalSettings())
Sends all modified variables through the attached transports.
This class stores variables and their values for use by any entity needing state information in a thr...
int64_t load_context(const std::string &filename, std::string &id, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings(true, true, true, false))
Loads the context from a file.
std::string get_id(void)
Returns the unique host and ephemeral binding for this Knowlede Base.
void print(unsigned int level) const
Atomically prints all variables and values in the context.
int get_log_level(void)
Gets the log level.
VariableReferences save_modifieds(void) const
Saves the list of modified records to use later for resending.
Holds settings for checkpoints to load or save.
void attach_logger(logger::Logger &logger) const
Attaches a logger to be used for printing.
int64_t save_as_karl(const std::string &filename) const
Saves the context to a file as karl assignments, rather than binary.
std::string setup_unique_hostport(const std::string &host="")
Binds to an ephemeral port for unique tie breakers in global ordering.
Provides knowledge logging services to files and terminals.
Holds basic transport settings.
int get_log_level(void)
Gets the log level.
Optimized reference to a variable within the knowledge base.
Compiled, optimized KaRL logic.
std::vector< KnowledgeRecord > FunctionArguments
void lock(void)
Locks the context to prevent updates over the network.
int set_index(const std::string &key, size_t index, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of an array index to a value.
void define_function(const std::string &name, knowledge::KnowledgeRecord(*func)(FunctionArguments &, Variables &), const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Defines an external function.
KnowledgeBase()
Constructor.
ssize_t write_file(const std::string &knowledge_key, const std::string &filename)
Write a file from the knowledge base to a specified location.
madara::knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Retrieves a knowledge value.
std::shared_ptr< KnowledgeBaseImpl > impl_
Pointer to actual implementation, i.e., the "bridge", which is reference counted to automate memory m...
logger::Logger & get_logger(void) const
Gets the logger used for information printing.
void add_modifieds(const VariableReferences &modifieds) const
Adds a list of VariableReferences to the current modified list.
madara::transport::TransportSettings & transport_settings(void)
bool exists(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically checks to see if a variable already exists.
bool clear(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Clears a variable.
A multi-threaded logger for logging to one or more destinations.
void apply_modified(void)
Changes all global variables to modified at current clock.
knowledge::KnowledgeMap to_map_stripped(const std::string &prefix) const
Creates a map with Knowledge Records that begin with the given prefix.
void print_knowledge(unsigned int level=0) const
Deprecated alias for.
void define_function(const std::string &name, knowledge::KnowledgeRecord(*func)(FunctionArguments &, Variables &))
Defines a function.
knowledge::KnowledgeRecord evaluate(CompiledExpression expression, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Evaluate a compiled expression.
int64_t save_checkpoint(const std::string &filename, const std::string &id="") const
Saves a checkpoint of a list of changes to a file.
void attach_logger(logger::Logger &logger) const
Attaches a logger to be used for printing.
ThreadSafeContext * context_
A knowledge base can also be a facade for another knowledge base.
void to_string(std::string &target, const std::string &array_delimiter=",", const std::string &record_delimiter=";\n", const std::string &key_val_delimiter="=") const
Saves all keys and values into a string, using the underlying knowledge::KnowledgeRecord::to_string f...
madara::knowledge::KnowledgeRecord wait(const std::string &expression, const WaitSettings &settings=WaitSettings())
Waits for an expression to be non-zero.
void facade_for(ThreadSafeContext &target)
Change the knowledge base to become a facade for another context.
void clear_modifieds(void)
Clear all modifications to the knowledge base.
Holds settings requirements for knowledge, usually in copying.
This class provides a distributed knowledge base to users.
void unlock(void) const
Unlocks the mutex on this context.
int64_t save_checkpoint(const std::string &filename, bool reset_modifieds=true)
Saves a checkpoint of a list of changes to a file.
::std::map< std::string, KnowledgeRecord > KnowledgeMap
CompiledExpression compile(const std::string &expression)
Compiles a KaRL expression into an expression tree.
int64_t save_as_karl(const std::string &filename) const
Saves the context to a file as karl assignments, rather than binary.
An abstract base class defines a simple abstract implementation of an expression tree node...
size_t to_map(const std::string &subject, std::map< std::string, knowledge::KnowledgeRecord > &target)
Fills a variable map with Knowledge Records that match an expression.
std::string expand_statement(const std::string &statement)
Expands a statement using variable expansion.
int modify(const EvalSettings &settings=EvalSettings())
Alias for apply_modified.
int set_jpeg(const std::string &key, const unsigned char *value, size_t size, const EvalSettings &settings=EvalSettings(false, false, true, false, false))
Atomically sets the value of a variable to a JPEG image.
void get_matches(const std::string &prefix, const std::string &suffix, VariableReferences &matches)
Creates an iteration of VariableReferences to all keys matching the prefix and suffix.
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Atomically returns a reference to the variable.
int set_file(const std::string &key, const unsigned char *value, size_t size, const EvalSettings &settings=EvalSettings(false, false, true, false, false))
Atomically sets the value of a variable to an arbitrary string.
void wait_for_change(bool extra_release=false)
Wait for a change to happen to the context.
size_t to_vector(const std::string &subject, unsigned int start, unsigned int end, std::vector< KnowledgeRecord > &target)
Fills a vector with Knowledge Records that begin with a common subject and have a finite range of int...
void copy(const KnowledgeBase &source, const KnowledgeRequirements &settings)
Copies variables and values from source to this context.
std::vector< VariableReference > VariableReferences
a vector of variable references
void clear_map(void)
Clears the knowledge base.
void close_transport(void)
Closes the transport mechanism so no dissemination is possible.
void set_quality(const std::string &key, uint32_t quality, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Sets the quality of writing to a certain variable from this entity.
std::string expand_statement(const std::string &statement) const
Expands a string with variable expansion.
ThreadSafeContext & get_context(void)
Returns the ThreadSafeContext associated with this Knowledge Base.
Encapsulates settings for an evaluation statement.
int set(const VariableReference &variable, const std::string &value, const EvalSettings &settings=EvalSettings(false, false, true, false, false))
Atomically sets the value of a variable to a string.
Provides functions and classes for the distributed knowledge base.
madara::knowledge::KnowledgeRecord retrieve_index(const std::string &key, size_t index, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Retrieves a value at a specified index within a knowledge array.
bool exists(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false)) const
Checks if a knowledge location exists in the context.
madara::knowledge::KnowledgeRecord evaluate(const std::string &expression, const EvalSettings &settings=EvalSettings())
Evaluates an expression.
knowledge::KnowledgeMap to_map_stripped(const std::string &prefix) const
Creates a map with Knowledge Records that begin with the given prefix.
void add_modifieds(const VariableReferences &modifieds) const
Adds a list of VariableReferences to the current modified list.
bool clear(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Clears a variable.
Settings for applying knowledge updates.
void to_string(std::string &target, const std::string &array_delimiter=",", const std::string &record_delimiter=";\n", const std::string &key_val_delimiter="=") const
Saves all keys and values into a string, using the underlying knowledge::KnowledgeRecord::to_string f...
int64_t load_context(const std::string &filename, bool use_id=true, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings(true, true, true, false))
Loads the context from a file.
madara::knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically returns the value of a variable.
Copyright (c) 2015 Carnegie Mellon University.
int64_t save_as_json(const std::string &filename) const
Saves the context to a file as JSON.
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.
void mark_modified(const VariableReference &variable, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Marks the variable reference as updated.
logger::Logger & get_logger(void) const
Gets the logger used for information printing.
std::string debug_modifieds(void) const
Retrieves a stringified list of all modified variables that are ready to send over transport on next ...
KnowledgeRecord retrieve_index(const std::string &key, size_t index, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Retrieves a value at a specified index within a knowledge array.
void activate_transport(void)
Starts the transport mechanism for dissemination if it is closed.
Encapsulates settings for a wait statement.
size_t attach_transport(madara::transport::Base *transport)
Attaches a transport to the Knowledge Engine.
Settings for applying knowledge updates.
Base class from which all transports must be derived.
ThreadSafeContext & get_context(void)
Returns the ThreadSafeContext associated with this Knowledge Base.
CompiledExpression compile(const std::string &expression)
Compiles a KaRL expression into an expression tree.
Provides an interface for external functions into the MADARA KaRL variable settings.
int read_file(const std::string &key, const std::string &filename, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically reads a file into a variable.
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Atomically returns a reference to the variable.
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...
void acquire(void)
Acquires the recursive lock on the knowledge base.
void set_log_level(int level)
Sets the log level.
int64_t save_as_json(const std::string &filename) const
Saves the context to a file as JSON.
void print(unsigned int level=0) const
Prints all knowledge variables and values in the context.
void set_log_level(int level)
Sets the log level.
void use(ThreadSafeContext &original)
Refer to and use another knowledge base's context.
void wait_for_change(void)
Wait for a change to happen to the context (e.g., from transports)
void release(void)
Releases a recursive lock on the knowledge base.
void reset_modified(void)
Reset all variables to be unmodified.