1 #ifndef _MADARA_KNOWLEDGE_BASE_INL_
2 #define _MADARA_KNOWLEDGE_BASE_INL_
40 typename std::enable_if<std::is_integral<T>::value,
void*>
::type>
59 typename std::enable_if<std::is_integral<T>::value,
void*>
::type>
78 typename std::enable_if<std::is_floating_point<T>::value,
void*>
::type>
86 result =
impl_->set(key, (
double)value, settings);
90 result =
context_->
set(key, (
double)value, settings);
97 typename std::enable_if<std::is_floating_point<T>::value,
void*>::type>
99 const VariableReference& variable, T value,
const EvalSettings& settings)
105 result =
impl_->set(variable, (
double)value, settings);
109 result =
context_->
set(variable, (
double)value, settings);
151 result =
impl_->get_log_level();
165 return impl_->get_logger();
189 impl_->set_log_level(level);
202 impl_->copy(*source.
impl_.get(), reqs, settings);
218 impl_->copy(*source.
impl_.get(), copy_set, clean_copy, settings);
225 context_->
copy(*source_context, copy_set, clean_copy, settings);
237 result =
impl_->apply_modified(settings);
251 impl_->close_transport();
262 result =
impl_->get(key, settings);
279 var =
impl_->get_ref(key, settings);
296 result =
impl_->get(variable, settings);
313 result =
impl_->retrieve_index(key, index, settings);
331 result =
impl_->retrieve_index(variable, index, settings);
348 result =
impl_->read_file(knowledge_key, filename, settings);
365 result =
impl_->read_file(variable, filename, settings);
376 const unsigned char* value,
size_t size,
const EvalSettings& settings)
385 impl_->set_file(
impl_->get_ref(key, settings), value, size, settings);
398 const unsigned char* value,
size_t size,
const EvalSettings& settings)
404 result =
impl_->set_file(variable, value, size, settings);
415 const unsigned char* value,
size_t size,
const EvalSettings& settings)
424 impl_->set_jpeg(
impl_->get_ref(key, settings), value, size, settings);
429 impl_->get_ref(key, settings), value, size, settings);
437 const unsigned char* value,
size_t size,
const EvalSettings& settings)
443 result =
impl_->set_jpeg(variable, value, size, settings);
460 result =
impl_->write_file(knowledge_key, filename);
472 result =
impl_->expand_statement(statement);
487 impl_->mark_modified(variable);
500 impl_->mark_modified(name, settings);
515 result =
impl_->set(variable, std::move(value), settings);
519 result =
context_->
set(variable, std::move(value), settings);
532 result =
impl_->set(key, value, settings);
549 result =
impl_->set(key, std::move(value), settings);
553 result =
context_->
set(key, std::move(value), settings);
566 result =
impl_->set(variable, value, settings);
584 result =
impl_->set_index(key, index, std::forward<T>(value), settings);
603 impl_->set_index(variable, index, std::forward<T>(value), settings);
622 result =
impl_->set(key, value, size, settings);
626 result =
context_->
set(key, value, size, settings);
640 result =
impl_->set(variable, value, size, settings);
644 result =
context_->
set(variable, value, size, settings);
651 const std::vector<KnowledgeRecord::Integer>& value,
658 result =
impl_->set(key, value, settings);
669 const std::vector<KnowledgeRecord::Integer>& value,
676 result =
impl_->set(variable, value, settings);
687 std::vector<KnowledgeRecord::Integer>&& value,
const EvalSettings& settings)
693 result =
impl_->set(key, std::move(value), settings);
697 result =
context_->
set(key, std::move(value), settings);
704 std::vector<KnowledgeRecord::Integer>&& value,
const EvalSettings& settings)
710 result =
impl_->set(variable, std::move(value), settings);
714 result =
context_->
set(variable, std::move(value), settings);
727 result =
impl_->set(key, value, size, settings);
731 result =
context_->
set(key, value, size, settings);
738 const double* value, uint32_t size,
const EvalSettings& settings)
744 result =
impl_->set(variable, value, size, settings);
748 result =
context_->
set(variable, value, size, settings);
755 const std::vector<double>& value,
const EvalSettings& settings)
761 result =
impl_->set(key, value, settings);
772 std::vector<double>&& value,
const EvalSettings& settings)
778 result =
impl_->set(key, std::move(value), settings);
782 result =
context_->
set(key, std::move(value), settings);
789 const std::vector<double>& value,
const EvalSettings& settings)
795 result =
impl_->set(variable, value, settings);
806 std::vector<double>&& value,
const EvalSettings& settings)
812 result =
impl_->set(variable, std::move(value), settings);
816 result =
context_->
set(variable, std::move(value), settings);
829 result =
impl_->set(key, value, settings);
846 result =
impl_->set(variable, value, settings);
863 result =
impl_->set(key, std::move(value), settings);
867 result =
context_->
set(key, std::move(value), settings);
880 result =
impl_->set(variable, std::move(value), settings);
884 result =
context_->
set(variable, std::move(value), settings);
896 impl_->set_quality(key, quality, settings);
911 result =
impl_->exists(key, settings);
928 result =
impl_->exists(variable, settings);
941 return impl_->transport_settings();
975 target, array_delimiter, record_delimiter, key_val_delimiter);
980 target, array_delimiter, record_delimiter, key_val_delimiter);
985 const std::string& statement,
unsigned int level)
const
989 impl_->print(statement, level);
1004 result =
impl_->clear(key, settings);
1018 impl_->clear(erase);
1062 #ifndef _MADARA_NO_KARL_
1070 result =
impl_->compile(expression);
1084 result =
impl_->evaluate(expression, settings);
1103 result =
impl_->evaluate(expression, settings);
1121 result =
impl_->evaluate(root, settings);
1137 impl_->define_function(name, func);
1151 impl_->define_function(name, func);
1159 #ifdef _MADARA_JAVA_
1166 impl_->define_function(name, func);
1175 #ifdef _MADARA_PYTHON_CALLBACKS_
1179 const std::string& name, boost::python::object callable)
1183 impl_->define_function(name, callable);
1203 impl_->define_function(name, expression);
1221 impl_->define_function(name, expression);
1237 result = this->
wait(ce, settings);
1239 else if (
impl_.get())
1241 result =
impl_->wait(expression, settings);
1252 impl_->activate_transport();
1263 result =
impl_->attach_transport(transport);
1275 result =
impl_->get_num_transports();
1288 result =
impl_->attach_transport(
id, settings);
1300 result =
impl_->remove_transport(index);
1314 else if (
impl_.get())
1316 result = &(
impl_->get_context());
1330 else if (
impl_.get())
1332 result = &(
impl_->get_context());
1344 else if (
impl_.get())
1346 impl_->clear_modifieds();
1357 else if (
impl_.get())
1359 impl_->add_modifieds(modifieds);
1371 else if (
impl_.get())
1373 return impl_->save_modifieds();
1376 return default_result;
1386 result =
impl_->send_modifieds(prefix, settings);
1400 else if (
impl_.get())
1402 result =
impl_->debug_modifieds();
1418 result =
impl_->get_id();
1439 unsigned int start,
unsigned int end, std::vector<KnowledgeRecord>& target)
1447 else if (
impl_.get())
1449 result =
impl_->to_vector(subject, start, end, target);
1462 else if (
impl_.get())
1464 impl_->get_matches(prefix, suffix, matches);
1475 std::map<std::string, KnowledgeRecord>& target)
1483 else if (
impl_.get())
1485 result =
impl_->to_map(expression, target);
1493 std::vector<std::string>& next_keys,
1494 std::map<std::string, KnowledgeRecord>& result,
bool just_keys)
1496 size_t result_size = 0;
1501 prefix, delimiter, suffix, next_keys, result, just_keys);
1503 else if (
impl_.get())
1506 impl_->to_map(prefix, delimiter, suffix, next_keys, result, just_keys);
1518 else if (
impl_.get())
1520 return impl_->to_map(prefix);
1533 else if (
impl_.get())
1535 return impl_->to_map_stripped(prefix);
1549 else if (
impl_.get())
1551 result =
impl_->save_context(filename);
1565 else if (
impl_.get())
1567 result =
impl_->save_context(settings);
1581 else if (
impl_.get())
1583 result =
impl_->save_as_json(filename);
1598 else if (
impl_.get())
1600 result =
impl_->save_as_json(settings);
1614 else if (
impl_.get())
1616 result =
impl_->save_as_karl(filename);
1631 else if (
impl_.get())
1633 result =
impl_->save_as_karl(settings);
1640 const std::string& filename,
bool reset_modifieds)
1648 else if (
impl_.get())
1650 result =
impl_->save_checkpoint(filename, reset_modifieds);
1665 else if (
impl_.get())
1667 result =
impl_->save_checkpoint(settings);
1683 else if (
impl_.get())
1685 result =
impl_->load_context(filename, use_id, settings);
1700 else if (
impl_.get())
1702 result =
impl_->load_context(filename, meta, use_id, settings);
1718 else if (
impl_.get())
1720 result =
impl_->load_context(checkpoint_settings, update_settings);
1734 else if (
impl_.get())
1736 return impl_->evaluate_file(checkpoint_settings, update_settings);
1749 else if (
impl_.get())
1751 return impl_->file_to_string(checkpoint_settings);
1763 else if (
impl_.get())
1765 impl_->wait_for_change();
1775 else if (
impl_.get())
1777 impl_->reset_checkpoint();
1787 result =
impl_->setup_unique_hostport(host);
const ThreadSafeContext * context_
madara::knowledge::KnowledgeRecord KnowledgeRecord
An abstract base class defines a simple abstract implementation of an expression tree node.
Holds settings for checkpoints to load or save.
Compiled, optimized KaRL logic.
This class provides a distributed knowledge base implementation.
ThreadSafeContext & get_context(void)
Returns the ThreadSafeContext associated with this Knowledge Base.
This class provides a distributed knowledge base to users.
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.
void print(unsigned int level=0) const
Prints all knowledge variables and values in the context.
void unlock(void)
Unlocks the context to allow updates over the network (is only necessary if the context has been expl...
int64_t save_as_json(const std::string &filename) const
Saves the context to a file as JSON.
ssize_t write_file(const std::string &knowledge_key, const std::string &filename)
Write a file from the knowledge base to a specified location.
void use(ThreadSafeContext &original)
Refer to and use another knowledge base's context.
int send_modifieds(const std::string &prefix="KnowledgeBase::send_modifieds", const EvalSettings &settings=EvalSettings::SEND)
Sends all modified variables through the attached transports.
int64_t save_as_karl(const std::string &filename) const
Saves the context to a file as karl assignments, rather than binary.
madara::knowledge::KnowledgeRecord wait(const std::string &expression, const WaitSettings &settings=WaitSettings())
Waits for an expression to be non-zero.
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Atomically returns a reference to the variable.
int read_file(const std::string &knowledge_key, const std::string &filename, const EvalSettings &settings=EvalSettings(true, false, true, false, false))
Read a file into the knowledge base.
size_t remove_transport(size_t index)
Removes a transport.
std::string setup_unique_hostport(const std::string &host="")
Binds to an ephemeral port for unique tie breakers in global ordering.
void activate_transport(void)
Starts the transport mechanism for dissemination if it is closed.
void attach_logger(logger::Logger &logger) const
Attaches a logger to be used for printing.
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.
ThreadSafeContext * context_
A knowledge base can also be a facade for another knowledge base.
KnowledgeRecord evaluate_file(CheckpointSettings &checkpoint_settings, const KnowledgeUpdateSettings &update_settings=KnowledgeUpdateSettings(true, true, true, false))
Loads and evaluates a karl script from a file.
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...
std::string get_id(void)
Returns the unique host and ephemeral binding for this Knowlede Base.
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.
void reset_checkpoint(void) const
Resets the local changed map, which tracks checkpointing modifieds.
void clear_modifieds(void)
Clear all modifications to the knowledge base.
knowledge::KnowledgeMap to_map_stripped(const std::string &prefix) const
Creates a map with Knowledge Records that begin with the given prefix.
int set_jpeg(const std::string &key, const unsigned char *value, size_t size, const EvalSettings &settings=EvalSettings(true, false, true, false, false))
Atomically sets the value of a variable to a JPEG image.
int modify(const EvalSettings &settings=EvalSettings())
Alias for apply_modified.
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.
void set_log_level(int level)
Sets the log level.
void print_knowledge(unsigned int level=0) const
Deprecated alias for.
void clear_map(void)
Clears the knowledge base.
bool clear(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Clears a variable.
int get_log_level(void)
Gets the log level.
std::string expand_statement(const std::string &statement)
Expands a statement using variable expansion.
madara::transport::TransportSettings & transport_settings(void)
int apply_modified(const EvalSettings &settings=EvalSettings())
Applies current time and modified to all global variables and tries to send them.
int set(const VariableReference &variable, const std::string &value, const EvalSettings &settings=EvalSettings(true, false, true, false, false))
Atomically sets the value of a variable to a string.
void define_function(const std::string &name, knowledge::KnowledgeRecord(*func)(FunctionArguments &, Variables &))
Defines a function.
int set_file(const std::string &key, const unsigned char *value, size_t size, const EvalSettings &settings=EvalSettings(true, false, true, false, false))
Atomically sets the value of a variable to an arbitrary string.
std::string debug_modifieds(void) const
Retrieves a stringified list of all modified variables that are ready to send over transport on next ...
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...
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.
void copy(const KnowledgeBase &source, const KnowledgeRequirements &reqs, const EvalSettings &settings=EvalSettings())
Copies variables and values from source to this context.
void lock(void)
Locks the context to prevent updates over the network.
ThreadSafeContext & get_context(void)
Returns the ThreadSafeContext associated with this Knowledge Base.
void mark_modified(const VariableReference &variable, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Marks the variable reference as updated.
int64_t save_checkpoint(const std::string &filename, bool reset_modifieds=true)
Saves a checkpoint of a list of changes to a file.
size_t attach_transport(madara::transport::Base *transport)
Attaches a transport to the Knowledge Engine.
madara::knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Atomically gets the current value of a variable (without any history).
void release(void)
Releases a recursive lock on the knowledge base.
size_t get_num_transports(void)
Gets the number of transports.
std::string file_to_string(CheckpointSettings &checkpoint_settings)
Loads and returns a karl script from a file with encode/decode.
void facade_for(ThreadSafeContext &target)
Change the knowledge base to become a facade for another context.
void close_transport(void)
Closes the transport mechanism so no dissemination is possible.
CompiledExpression compile(const std::string &expression)
Compiles a KaRL expression into an expression tree.
int set_index(const VariableReference &variable, size_t index, T &&value, const EvalSettings &settings=EvalSettings(true, false, true, false, false))
Atomically sets the value of an array index to a value.
VariableReferences save_modifieds(void) const
Saves the list of modified records to use later for resending.
int64_t save_context(const std::string &filename) const
Saves the context to a file.
logger::Logger & get_logger(void) const
Gets the logger used for information printing.
KnowledgeBase()
Constructor.
void wait_for_change(void)
Wait for a change to happen to the context (e.g., from transports)
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.
void add_modifieds(const VariableReferences &modifieds) const
Adds a list of VariableReferences to the current modified list.
void acquire(void)
Acquires the recursive lock on the knowledge base.
std::shared_ptr< KnowledgeBaseImpl > impl_
Pointer to actual implementation, i.e., the "bridge", which is reference counted to automate memory m...
This class encapsulates an entry in a KnowledgeBase.
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...
CompiledExpression compile(const std::string &expression)
Compiles a KaRL expression into an expression tree.
void unlock(void) const
Unlocks the mutex on this context.
bool exists(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically checks to see if a variable already exists.
void attach_logger(logger::Logger &logger) const
Attaches a logger to be used for printing.
int64_t save_as_json(const std::string &filename) const
Saves the context to a file as JSON.
logger::Logger & get_logger(void) const
Gets the logger used for information printing.
std::string file_to_string(CheckpointSettings &checkpoint_settings)
Loads and returns a karl script from a file with encode/decode.
bool clear(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Clears a variable.
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...
int read_file(const std::string &key, const std::string &filename, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically reads a file into a variable.
madara::knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically returns the current value of a variable.
KnowledgeRecord evaluate_file(CheckpointSettings &checkpoint_settings, const KnowledgeUpdateSettings &update_settings=KnowledgeUpdateSettings(true, true, true, false))
Loads and evaluates a karl script from a file.
void apply_modified(void)
Changes all global variables to modified at current clock.
void add_modifieds(const VariableReferences &modifieds) const
Adds a list of VariableReferences to the current modified list.
void define_function(const std::string &name, knowledge::KnowledgeRecord(*func)(FunctionArguments &, Variables &), const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Defines an external function.
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...
knowledge::KnowledgeRecord evaluate(CompiledExpression expression, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Evaluate a compiled expression.
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.
std::string debug_modifieds(void) const
Retrieves a stringified list of all modified variables that are ready to send over transport on next ...
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(const std::string &key, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to the specific record.
knowledge::KnowledgeMap to_map_stripped(const std::string &prefix) const
Creates a map with Knowledge Records that begin with the given prefix.
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.
int64_t save_checkpoint(const std::string &filename, const std::string &id="") const
Saves a checkpoint of a list of changes to a file.
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 KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to an arbitrary string.
int64_t save_context(const std::string &filename, const std::string &id="") const
Saves the context to a file.
VariableReferences save_modifieds(void) const
Saves the list of modified records to use later for resending.
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.
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) const
Expands a string with variable expansion.
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.
int get_log_level(void)
Gets the log level.
void lock(void) const
Locks the mutex on this 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 set_log_level(int level)
Sets the log level.
void reset_checkpoint(void) const
Reset all checkpoint variables in the modified lists.
void copy(const ThreadSafeContext &source, const KnowledgeRequirements &reqs, const KnowledgeUpdateSettings &settings)
Copies variables and values from source to this context.
void print(unsigned int level) const
Atomically prints all variables and values in the context.
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 reset_modified(void)
Reset all variables to be unmodified.
void wait_for_change(bool extra_release=false)
Wait for a change to happen to the context.
int64_t save_as_karl(const std::string &filename) const
Saves the context to a file as karl assignments, rather than binary.
Optimized reference to a variable within the knowledge base.
Provides an interface for external functions into the MADARA KaRL variable settings.
A multi-threaded logger for logging to one or more destinations.
Base class from which all transports must be derived.
Holds basic transport settings.
Provides functions and classes for the distributed knowledge base.
std::map< std::string, bool > CopySet
Typedef for set of copyable keys.
std::vector< KnowledgeRecord > FunctionArguments
::std::map< std::string, KnowledgeRecord > KnowledgeMap
std::vector< VariableReference > VariableReferences
a vector of variable references
Provides knowledge logging services to files and terminals.
Copyright(c) 2020 Galois.
Encapsulates settings for an evaluation statement.
Holds settings requirements for knowledge, usually in copying.
Encapsulates settings for a wait statement.