1 #ifndef _MADARA_KNOWLEDGE_BASE_INL_ 2 #define _MADARA_KNOWLEDGE_BASE_INL_ 6 #include "ace/streams.h" 43 typename std::enable_if<std::is_integral<T>::value,
66 typename std::enable_if<std::is_integral<T>::value,
89 typename std::enable_if<std::is_floating_point<T>::value,
101 result =
impl_->set (key, (
double)value, settings);
105 result =
context_->
set (key, (
double)value, settings);
112 typename std::enable_if<std::is_floating_point<T>::value,
124 result =
impl_->set (variable, (
double)value, settings);
128 result =
context_->
set (variable, (
double)value, settings);
175 result =
impl_->get_log_level ();
190 return impl_->get_logger ();
204 impl_->attach_logger (logger);
217 impl_->set_log_level (level);
253 impl_->copy (*source.
impl_.get (), copy_set, clean_copy);
275 result =
impl_->apply_modified (settings);
290 impl_->close_transport ();
302 result =
impl_->get (key, settings);
320 var =
impl_->get_ref (key, settings);
339 result =
impl_->get (variable, settings);
358 result =
impl_->retrieve_index (key, index, settings);
377 result =
impl_->retrieve_index (variable, index, settings);
398 result =
impl_->read_file (knowledge_key, filename, settings);
418 result =
impl_->read_file (variable, filename, settings);
431 const unsigned char * value,
size_t size,
440 result =
impl_->set_file (
441 impl_->get_ref (key, settings), value, size, settings);
456 const unsigned char * value,
size_t size,
463 result =
impl_->set_file (variable, value, size, settings);
476 const unsigned char * value,
size_t size,
485 result =
impl_->set_jpeg (
486 impl_->get_ref (key, settings), value, size, settings);
491 impl_->get_ref (key, settings), value, size, settings);
501 const unsigned char * value,
size_t size,
508 result =
impl_->set_jpeg (variable, value, size, settings);
527 result =
impl_->read_policy (knowledge_key, filename);
541 result =
impl_->write_file (knowledge_key, filename);
556 result =
impl_->expand_statement (statement);
573 impl_->mark_modified (variable);
588 impl_->mark_modified (name, settings);
606 result =
impl_->set (variable, std::move(value), settings);
610 result =
context_->
set (variable, std::move(value), settings);
626 result =
impl_->set (key, value, settings);
646 result =
impl_->set (key, std::move(value), settings);
650 result =
context_->
set (key, std::move(value), settings);
666 result =
impl_->set (variable, value, settings);
670 result =
context_->
set (variable, value, settings);
680 size_t index, T&& value,
687 result =
impl_->set_index (key, index, std::forward<T>(value), settings);
701 size_t index, T&& value,
708 result =
impl_->set_index (variable, index, std::forward<T>(value), settings);
712 result =
context_->
set_index (variable, index, std::forward<T>(value), settings);
729 result =
impl_->set (key, value, size, settings);
733 result =
context_->
set (key, value, size, settings);
750 result =
impl_->set (variable, value, size, settings);
754 result =
context_->
set (variable, value, size, settings);
763 const std::vector <KnowledgeRecord::Integer> & value,
770 result =
impl_->set (key, value, settings);
783 const std::vector <KnowledgeRecord::Integer> & value,
790 result =
impl_->set (variable, value, settings);
794 result =
context_->
set (variable, value, settings);
803 std::vector <KnowledgeRecord::Integer> && value,
810 result =
impl_->set (key, std::move(value), settings);
814 result =
context_->
set (key, std::move(value), settings);
823 std::vector <KnowledgeRecord::Integer> && value,
830 result =
impl_->set (variable, std::move(value), settings);
834 result =
context_->
set (variable, std::move(value), settings);
843 const double * value,
851 result =
impl_->set (key, value, size, settings);
855 result =
context_->
set (key, value, size, settings);
864 const double * value,
872 result =
impl_->set (variable, value, size, settings);
876 result =
context_->
set (variable, value, size, settings);
885 const std::vector <double> & value,
892 result =
impl_->set (key, value, settings);
905 std::vector <double> && value,
912 result =
impl_->set (key, std::move(value), settings);
916 result =
context_->
set (key, std::move(value), settings);
925 const std::vector <double> & value,
932 result =
impl_->set (variable, value, settings);
936 result =
context_->
set (variable, value, settings);
945 std::vector <double> && value,
952 result =
impl_->set (variable, std::move(value), settings);
956 result =
context_->
set (variable, std::move(value), settings);
971 result =
impl_->set (key, value, settings);
991 result =
impl_->set (variable, value, settings);
995 result =
context_->
set (variable, value, settings);
1010 result =
impl_->set (key, std::move(value), settings);
1014 result =
context_->
set (key, std::move(value), settings);
1030 result =
impl_->set (variable, std::move(value), settings);
1034 result =
context_->
set (variable, std::move(value), settings);
1049 impl_->set_quality (key, quality, settings);
1063 bool result =
false;
1067 result =
impl_->exists (key, settings);
1082 bool result =
false;
1086 result =
impl_->exists (variable, settings);
1099 KnowledgeBase::print_all_redeployment_results (
1100 std::ostream & output)
1102 return impl_->print_all_redeployment_results (output);
1106 KnowledgeBase::run_all (
void)
1108 return impl_->run_all ();
1112 KnowledgeBase::start_latency (
void)
1114 return impl_->start_latency ();
1118 KnowledgeBase::print_my_latencies (
1119 std::ostream & output)
1121 impl_->print_my_latencies (output);
1125 KnowledgeBase::print_all_latencies (
1126 std::ostream & output)
1128 impl_->print_all_latencies (output);
1132 KnowledgeBase::print_all_summations (
1133 std::ostream & output)
1135 impl_->print_all_summations (output);
1139 KnowledgeBase::vote (
void)
1141 return impl_->vote ();
1148 return impl_->transport_settings ();
1153 unsigned int level)
const 1157 impl_->print (level);
1167 unsigned int level)
const 1171 impl_->print (level);
1188 impl_->to_string (target,
1189 array_delimiter, record_delimiter, key_val_delimiter);
1194 array_delimiter, record_delimiter, key_val_delimiter);
1201 const std::string & statement,
unsigned int level)
const 1205 impl_->print (statement, level);
1217 bool result (
false);
1221 result =
impl_->clear (key, settings);
1236 impl_->clear (erase);
1249 impl_->clear_map ();
1283 #ifndef _MADARA_NO_KARL_ 1293 result =
impl_->compile (expression);
1309 result =
impl_->evaluate (expression, settings);
1330 result =
impl_->evaluate (expression, settings);
1350 result =
impl_->evaluate (root, settings);
1369 impl_->define_function (name, func);
1385 impl_->define_function (name, func);
1393 #ifdef _MADARA_JAVA_ 1401 impl_->define_function (name, func);
1410 #ifdef _MADARA_PYTHON_CALLBACKS_ 1415 const std::string & name, boost::python::object callable)
1419 impl_->define_function (name, callable);
1440 impl_->define_function (name, expression);
1459 impl_->define_function (name, expression);
1477 result = this->
wait (ce, settings);
1479 else if (
impl_.get ())
1481 result =
impl_->wait (expression, settings);
1487 #endif // _MADARA_NO_KARL_ 1493 impl_->activate_transport ();
1505 result =
impl_->attach_transport (transport);
1518 result =
impl_->get_num_transports ();
1532 result =
impl_->attach_transport (
id, settings);
1546 result =
impl_->remove_transport (index);
1561 else if (
impl_.get ())
1563 result = &(
impl_->get_context ());
1576 else if (
impl_.get ())
1578 impl_->clear_modifieds ();
1589 else if (
impl_.get ())
1591 impl_->add_modifieds (modifieds);
1604 else if (
impl_.get ())
1606 return impl_->save_modifieds ();
1609 return default_result;
1621 result =
impl_->send_modifieds (prefix, settings);
1636 else if (
impl_.get ())
1638 result =
impl_->debug_modifieds ();
1655 result =
impl_->get_id ();
1680 std::vector <KnowledgeRecord> & target)
1688 else if (
impl_.get ())
1690 result =
impl_->to_vector (subject, start, end, target);
1704 else if (
impl_.get ())
1706 impl_->get_matches (prefix, suffix, matches);
1720 std::map <std::string, KnowledgeRecord> & target)
1728 else if (
impl_.get ())
1730 result =
impl_->to_map (expression, target);
1741 std::vector <std::string> & next_keys,
1742 std::map <std::string, KnowledgeRecord> & result,
1745 size_t result_size = 0;
1750 prefix, delimiter, suffix, next_keys, result, just_keys);
1752 else if (
impl_.get ())
1754 result_size =
impl_->to_map (
1755 prefix, delimiter, suffix, next_keys, result, just_keys);
1769 else if (
impl_.get ())
1771 return impl_->to_map (prefix);
1785 else if (
impl_.get ())
1787 return impl_->to_map_stripped (prefix);
1803 else if (
impl_.get ())
1805 result =
impl_->save_context (filename);
1821 else if (
impl_.get ())
1823 result =
impl_->save_context (settings);
1840 else if (
impl_.get ())
1842 result =
impl_->save_as_json (filename);
1858 else if (
impl_.get ())
1860 result =
impl_->save_as_json (settings);
1876 else if (
impl_.get ())
1878 result =
impl_->save_as_karl (filename);
1894 else if (
impl_.get ())
1896 result =
impl_->save_as_karl (settings);
1906 bool reset_modifieds)
1914 else if (
impl_.get ())
1916 result =
impl_->save_checkpoint (filename, reset_modifieds);
1932 else if (
impl_.get ())
1934 result =
impl_->save_checkpoint (settings);
1953 else if (
impl_.get ())
1955 result =
impl_->load_context (filename, use_id, settings);
1974 else if (
impl_.get ())
1976 result =
impl_->load_context (filename, meta, use_id, settings);
1993 else if (
impl_.get ())
1995 result =
impl_->load_context (checkpoint_settings, update_settings);
2008 else if (
impl_.get ())
2010 impl_->wait_for_change ();
2022 result =
impl_->setup_unique_hostport (host);
2036 #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.
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 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.
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...
madara::knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically returns the value of a variable.
knowledge::KnowledgeMap to_map_stripped(const std::string &prefix) const
Creates a map with Knowledge Records that begin with the given prefix.
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.
VariableReferences save_modifieds(void) const
Saves the list of modified records to use later for resending.
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.
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)
Returns a non-const reference to the Transport Settings.
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.
int64_t save_checkpoint(const std::string &filename, const std::string &id="") const
Saves a checkpoint of a list of changes to a file.
knowledge::KnowledgeRecord evaluate(CompiledExpression expression, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Evaluate a compiled expression.
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
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.
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.
CompiledExpression compile(const std::string &expression)
Compiles a KaRL expression into an expression tree.
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.
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.
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Atomically returns a reference to the 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.
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 ...
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_policy(const std::string &policy_key, const std::string &policy_file)
Read a policy into the knowledge base.
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.