1 #ifndef _MADARA_KNOWLEDGE_BASE_IMPL_INL_
2 #define _MADARA_KNOWLEDGE_BASE_IMPL_INL_
65 return map_.
get(t_key, settings);
71 return map_.
get(variable, settings);
174 const unsigned char* value,
size_t size,
const EvalSettings& settings)
176 int result =
map_.
set_file(variable, value, size, settings);
185 const unsigned char* value,
size_t size,
const EvalSettings& settings)
187 int result =
map_.
set_jpeg(variable, value, size, settings);
216 "KnowledgeBaseImpl::activate_transport:"
217 " transport already activated. If you need"
218 " a new type, close transport first\n");
283 map_.
to_string(target, array_delimiter, record_delimiter, key_val_delimiter);
287 const std::string& statement,
unsigned int level)
const
321 #ifndef _MADARA_NO_KARL_
345 #ifdef _MADARA_PYTHON_CALLBACKS_
348 const std::string& name, boost::python::object callable)
374 return wait(compiled, settings);
382 return evaluate(compiled, settings);
404 std::shared_ptr<transport::Base> transport;
467 unsigned int start,
unsigned int end, std::vector<KnowledgeRecord>& target)
479 std::map<std::string, KnowledgeRecord>& target)
486 std::vector<std::string>& next_keys,
487 std::map<std::string, KnowledgeRecord>& result,
bool just_keys)
489 return map_.
to_map(prefix, delimiter, suffix, next_keys, result, just_keys);
547 map_.reset_checkpoint();
558 return total_written;
666 #ifndef _MADARA_NO_KARL_
672 return evaluate(compiled, settings);
#define madara_logger_log(loggering, level,...)
Fast version of the madara::logger::log method.
VariableReferenceMap map_
Holds settings for checkpoints to load or save.
std::string originator
the originator id of the checkpoint
Compiled, optimized KaRL logic.
This class provides a distributed knowledge base implementation.
madara::knowledge::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.
std::string expand_statement(const std::string &statement) const
Expands a statement using variable expansion.
ThreadSafeContext & get_context(void)
Returns the ThreadSafeContext associated with this Knowledge Base.
madara::transport::TransportSettings & transport_settings(void)
Returns a non-const reference to the Transport Settings.
void clear_map(void)
Clears the knowledge base.
void set_log_level(int level)
Sets the log level.
~KnowledgeBaseImpl()
Destructor.
int get_log_level(void)
Gets the log level.
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 release(void)
Releases a recursive lock on the knowledge base.
int64_t save_as_json(const std::string &filename) const
Saves the context to a file as JSON.
std::string get_id(void)
Returns the unique host and ephemeral binding for this Knowlede Base.
madara::knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Retrieves a knowledge value.
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 print(unsigned int level) const
Prints all knowledge variables and values in the context.
int read_file(const std::string &knowledge_key, const std::string &filename, const EvalSettings &settings=EvalSettings())
Read a file into the knowledge base.
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_quality(const std::string &key, uint32_t quality, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Sets the quality of writing to a certain variable from this entity.
std::string setup_unique_hostport(std::string host="")
Creates a random UUID for unique tie breakers in global ordering.
MADARA_LOCK_TYPE transport_mutex_
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 attach_transport(madara::transport::Base *transport)
Attaches a transport to the Knowledge Engine.
void activate_transport(void)
Starts the transport mechanism for dissemination if it is closed.
VariableReferences save_modifieds(void) const
Saves the list of modified records to use later for resending.
KnowledgeBaseImpl()
Constructor.
int apply_modified(const EvalSettings &settings=EvalSettings())
Applies current time and modified to all global variables and tries to send them.
void acquire(void)
Acquires the recursive lock on the knowledge base.
void close_transport(void)
Closes the transport mechanism so no dissemination is possible.
int64_t save_context(const std::string &filename) const
Saves the context to a file.
void add_modifieds(const VariableReferences &modifieds) const
Adds a list of VariableReferences to the current modified list.
void mark_modified(const VariableReference &variable, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Marks the variable reference as updated.
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 clear_modifieds(void)
Clear all modifications to the knowledge base.
bool clear(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Clears a variable.
size_t remove_transport(size_t index)
Removes a transport.
std::string file_to_string(CheckpointSettings &checkpoint_settings)
Loads and returns a karl script from a file with encode/decode.
void attach_logger(logger::Logger &logger) const
Attaches a logger to be used for printing.
transport::QoSTransportSettings settings_
size_t get_num_transports(void)
Gets the number of transports.
madara::knowledge::KnowledgeRecord evaluate(const std::string &expression)
Evaluates an expression.
void copy(const KnowledgeBaseImpl &source, const KnowledgeRequirements &reqs, const EvalSettings &settings=EvalSettings())
Copies variables and values from source to this context.
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Atomically returns a reference to the variable.
void unlock(void)
Unlocks the context to allow updates over the network (is only necessary if the context has been expl...
CompiledExpression compile(const std::string &expression)
Compiles a KaRL expression into an expression tree.
void define_function(const std::string &name, knowledge::KnowledgeRecord(*func)(FunctionArguments &, Variables &))
Defines a function.
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 lock(void)
Locks the context to prevent updates over the network.
MADARA_EXPORT int send_modifieds(const std::string &prefix, const EvalSettings &settings=EvalSettings::SEND)
Sends all modified variables through the attached transports.
int set_jpeg(const VariableReference &variable, const unsigned char *value, size_t size, const EvalSettings &settings=EvalSettings())
Atomically sets the value of a variable to a JPEG image.
int64_t load_context(const std::string &filename, bool use_id, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings(true, true, true, false))
Loads the context from a file.
knowledge::KnowledgeMap to_map_stripped(const std::string &prefix) const
Creates a map with Knowledge Records that begin with the given prefix.
madara::knowledge::KnowledgeRecord wait(const std::string &expression)
Waits for an expression to be non-zero.
void reset_checkpoint(void) const
Resets the local changed map, which tracks checkpointing modifieds.
int set_file(const VariableReference &variable, const unsigned char *value, size_t size, const EvalSettings &settings=EvalSettings())
Atomically sets the value of a variable to the contents of a file.
int64_t save_as_karl(const std::string &filename) const
Saves the context to a file as karl assignments, rather than binary.
void wait_for_change(void)
Wait for a change to happen to the context (e.g., from transports)
bool exists(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Checks if a knowledge location exists in the context.
std::vector< std::shared_ptr< transport::Base > > transports_
int64_t save_checkpoint(const std::string &filename, bool reset_modifieds=true)
Saves a checkpoint of a list of changes to a file.
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...
This class encapsulates an entry in a KnowledgeBase.
ssize_t to_file(const std::string &filename) const
writes the value to a file
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...
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...
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.
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 * get_record(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Retrieves a knowledge record from the key.
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.
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.
void set_write_quality(const std::string &key, uint32_t quality, const KnowledgeReferenceSettings &settings)
Atomically sets write quality of this process for a variable.
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.
bool delay_launch
Delay launching transports until explicit activate call.
uint32_t type
Type of transport. See madara::transport::Types for options.
std::string write_domain
All class members are accessible to users for easy setup.
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.