MADARA  3.4.1
madara::knowledge::Variables Class Reference

Provides an interface for external functions into the MADARA KaRL variable settings. More...

#include <Variables.h>

Public Member Functions

 Variables (const Variables &rhs)
 Constructor. More...
 
 Variables (ThreadSafeContext *new_context=0)
 Constructor. More...
 
void apply_modified (const KnowledgeUpdateSettings &settings=knowledge::KnowledgeUpdateSettings())
 Applies modified to all global variables. More...
 
CompiledExpression compile (const std::string &expression)
 Compiles a KaRL expression into an expression tree. More...
 
knowledge::KnowledgeRecord dec (const std::string &key, const KnowledgeUpdateSettings &settings=knowledge::KnowledgeUpdateSettings(false, true, false, false))
 Decrements the value of the variable. More...
 
knowledge::KnowledgeRecord dec (const VariableReference &variable, const KnowledgeUpdateSettings &settings=knowledge::KnowledgeUpdateSettings(false, true, false, false))
 Decrements the value of the variable. More...
 
void define_function (const std::string &name, boost::python::object callable)
 Defines a named python function. More...
 
void define_function (const std::string &name, const CompiledExpression &expression)
 Defines a MADARA KaRL function. More...
 
void define_function (const std::string &name, const std::string &expression)
 Defines a MADARA KaRL function. More...
 
void define_function (const std::string &name, jobject callable)
 Defines a named java function. More...
 
void define_function (const std::string &name, knowledge::KnowledgeRecord(*func)(const char *, FunctionArguments &, Variables &))
 Defines a named function that can distinguish the name it was called with in MADARA. More...
 
void define_function (const std::string &name, knowledge::KnowledgeRecord(*func)(FunctionArguments &, Variables &))
 Defines a function. More...
 
madara::knowledge::KnowledgeRecord evaluate (CompiledExpression &expression, const KnowledgeUpdateSettings &settings=knowledge::KnowledgeUpdateSettings())
 Evaluates an expression. More...
 
madara::knowledge::KnowledgeRecord evaluate (const std::string &expression, const KnowledgeUpdateSettings &settings=knowledge::KnowledgeUpdateSettings())
 Evaluates an expression (USE ONLY FOR PROTOTYPING; DO NOT USE IN PRODUCTION SYSTEMS). More...
 
bool exists (const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false)) const
 Checks if a knowledge location exists in the context. More...
 
bool exists (const VariableReference &variable, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false)) const
 Checks if a knowledge variable exists in the context. More...
 
std::string expand_statement (const std::string &statement) const
 Expands a string with variable expansion. More...
 
knowledge::KnowledgeRecord get (const std::string &key, const KnowledgeReferenceSettings &settings=knowledge::KnowledgeReferenceSettings(false))
 Retrieves the value of a variable. More...
 
knowledge::KnowledgeRecord get (const VariableReference &variable, const KnowledgeReferenceSettings &settings=knowledge::KnowledgeReferenceSettings(false))
 Retrieves the value of a variable. More...
 
ThreadSafeContextget_context (void)
 Returns the ThreadSafeContext associated with this Variables facade. More...
 
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. More...
 
VariableReference get_ref (const std::string &key, const KnowledgeReferenceSettings &settings=knowledge::KnowledgeReferenceSettings(false))
 Retrieves the value of a variable. More...
 
knowledge::KnowledgeRecord inc (const std::string &key, const KnowledgeUpdateSettings &settings=knowledge::KnowledgeUpdateSettings(false, true, false, false))
 Atomically increments the value of the variable. More...
 
knowledge::KnowledgeRecord inc (const VariableReference &variable, const KnowledgeUpdateSettings &settings=knowledge::KnowledgeUpdateSettings(false, true, false, false))
 Atomically increments the value of the variable. More...
 
int64_t load_context (const std::string &filename, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings(true, true, true, false))
 Loads the context from a file. More...
 
void operator= (Variables &rhs)
 Assignment operator. More...
 
void print (const std::string &statement, unsigned int level=0) const
 Print a statement, similar to printf (variable expansions allowed) e.g., input = "MyVar{.id} = {MyVar{.id}}\n";. More...
 
void print (unsigned int level=0) const
 Prints all variables and values in the context. More...
 
madara::knowledge::KnowledgeRecord retrieve_index (const std::string &key, size_t index, const KnowledgeReferenceSettings &settings=knowledge::KnowledgeReferenceSettings(false))
 Retrieves a value at a specified index within a knowledge array. More...
 
madara::knowledge::KnowledgeRecord retrieve_index (const VariableReference &variable, size_t index, const KnowledgeReferenceSettings &settings=knowledge::KnowledgeReferenceSettings(false))
 Retrieves a value at a specified index within a knowledge array. More...
 
int64_t save_as_karl (const std::string &filename) const
 Saves the context to a file as karl assignments, rather than binary. More...
 
int64_t save_checkpoint (const std::string &filename, bool reset_modifieds=true)
 Saves a checkpoint of a list of changes to a file. More...
 
int64_t save_context (const std::string &filename)
 Saves the context to a file. More...
 
int set (const std::string &key, const double *value, uint32_t size, const KnowledgeUpdateSettings &settings=knowledge::KnowledgeUpdateSettings(false, true, false, false))
 Atomically sets the value of a variable to a double array. More...
 
int set (const std::string &key, const madara::knowledge::KnowledgeRecord &value, const KnowledgeUpdateSettings &settings=knowledge::KnowledgeUpdateSettings(false, true, false, false))
 Sets the value of a variable. More...
 
int set (const std::string &key, const madara::knowledge::KnowledgeRecord::Integer *value, uint32_t size, const KnowledgeUpdateSettings &settings=knowledge::KnowledgeUpdateSettings(false, true, false, false))
 Atomically sets the value of a variable to an integer array. More...
 
int set (const std::string &key, const std::string &value, const KnowledgeUpdateSettings &settings=knowledge::KnowledgeUpdateSettings(false, true, false, false))
 Sets the value of a variable. More...
 
int set (const std::string &key, const std::vector< double > &value, const KnowledgeUpdateSettings &settings=knowledge::KnowledgeUpdateSettings(false, true, false, false))
 Atomically sets the value of a variable to a double array. More...
 
int set (const std::string &key, const std::vector< KnowledgeRecord::Integer > &value, const KnowledgeUpdateSettings &settings=knowledge::KnowledgeUpdateSettings(false, true, false, false))
 Atomically sets the value of a variable to an integer array. More...
 
int set (const std::string &key, double value, const KnowledgeUpdateSettings &settings=knowledge::KnowledgeUpdateSettings(false, true, false, false))
 Sets the value of a variable. More...
 
int set (const std::string &key, madara::knowledge::KnowledgeRecord::Integer value, const KnowledgeUpdateSettings &settings=knowledge::KnowledgeUpdateSettings(false, true, false, false))
 Sets the value of a variable. More...
 
int set (const VariableReference &variable, const double *value, uint32_t size, const KnowledgeUpdateSettings &settings=knowledge::KnowledgeUpdateSettings(false, true, false, false))
 Atomically sets the value of a variable to a double array. More...
 
int set (const VariableReference &variable, const madara::knowledge::KnowledgeRecord &value, const KnowledgeUpdateSettings &settings=knowledge::KnowledgeUpdateSettings(false, true, false, false))
 Sets the value of a variable. More...
 
int set (const VariableReference &variable, const madara::knowledge::KnowledgeRecord::Integer *value, uint32_t size, const KnowledgeUpdateSettings &settings=knowledge::KnowledgeUpdateSettings(false, true, false, false))
 Atomically sets the value of a variable to an integer array. More...
 
int set (const VariableReference &variable, const std::string &value, const KnowledgeUpdateSettings &settings=knowledge::KnowledgeUpdateSettings(false, true, false, false))
 Sets the value of a variable. More...
 
int set (const VariableReference &variable, const std::vector< double > &value, const KnowledgeUpdateSettings &settings=knowledge::KnowledgeUpdateSettings(false, true, false, false))
 Atomically sets the value of a variable to a double array. More...
 
int set (const VariableReference &variable, const std::vector< KnowledgeRecord::Integer > &value, const KnowledgeUpdateSettings &settings=knowledge::KnowledgeUpdateSettings(false, true, false, false))
 Atomically sets the value of a variable to an integer array. More...
 
int set (const VariableReference &variable, double value, const KnowledgeUpdateSettings &settings=knowledge::KnowledgeUpdateSettings(false, true, false, false))
 Sets the value of a variable. More...
 
int set (const VariableReference &variable, madara::knowledge::KnowledgeRecord::Integer value, const KnowledgeUpdateSettings &settings=knowledge::KnowledgeUpdateSettings(false, true, false, false))
 Sets the value of a variable. More...
 
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. More...
 
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 function. More...
 
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 integer values. More...
 
ssize_t write_file (const std::string &knowledge_key, const std::string &filename)
 Write a file from the context to a specified location. More...
 

Private Attributes

ThreadSafeContextcontext_
 Variables context that is directly used by the KaRL engine. More...
 

Friends

class madara::expression::CompositeFunctionNode
 
class madara::knowledge::KnowledgeRecordFilters
 

Detailed Description

Provides an interface for external functions into the MADARA KaRL variable settings.

Definition at line 52 of file Variables.h.

Constructor & Destructor Documentation

◆ Variables() [1/2]

madara::knowledge::Variables::Variables ( ThreadSafeContext new_context = 0)
inline

Constructor.

Definition at line 61 of file Variables.h.

◆ Variables() [2/2]

madara::knowledge::Variables::Variables ( const Variables rhs)
inline

Constructor.

Definition at line 66 of file Variables.h.

Member Function Documentation

◆ apply_modified()

void madara::knowledge::Variables::apply_modified ( const KnowledgeUpdateSettings settings = knowledge::KnowledgeUpdateSettings())

Applies modified to all global variables.

Parameters
settingsSettings for evaluating and printing

Definition at line 97 of file Variables.cpp.

◆ compile()

madara::knowledge::CompiledExpression madara::knowledge::Variables::compile ( const std::string &  expression)

Compiles a KaRL expression into an expression tree.

Always do this before calling evaluate because it puts the expression into an optimized format. Best practice is to save the CompiledExpression in a global variable or in some kind of persistent store. Pair with expand_statement if you know that variable expansion is used but the variable values that are expanded never change (e.g. an id that is set through the command line and thus stays the same after it is initially set).

Parameters
expressionexpression to compile
Returns
compiled, optimized expression tree
Exceptions
exceptions::KarlExceptionfailure during compile/evaluate

Definition at line 650 of file Variables.cpp.

◆ dec() [1/2]

madara::knowledge::KnowledgeRecord madara::knowledge::Variables::dec ( const std::string &  key,
const KnowledgeUpdateSettings settings = knowledge::KnowledgeUpdateSettings(false, true, false, false) 
)

Decrements the value of the variable.

Parameters
keyunique identifier of the variable
settingsSettings for updating knowledge
Returns
new value of variable

Definition at line 467 of file Variables.cpp.

◆ dec() [2/2]

madara::knowledge::KnowledgeRecord madara::knowledge::Variables::dec ( const VariableReference variable,
const KnowledgeUpdateSettings settings = knowledge::KnowledgeUpdateSettings(false, true, false, false) 
)

Decrements the value of the variable.

Parameters
variablereference to a variable (
See also
get_ref)
Parameters
settingsSettings for updating knowledge
Returns
new value of variable

Definition at line 484 of file Variables.cpp.

◆ define_function() [1/6]

void madara::knowledge::Variables::define_function ( const std::string &  name,
boost::python::object  callable 
)

Defines a named python function.

Parameters
namename of the function
callableexternal python function to call with this name

Definition at line 596 of file Variables.cpp.

◆ define_function() [2/6]

void madara::knowledge::Variables::define_function ( const std::string &  name,
const CompiledExpression expression 
)

Defines a MADARA KaRL function.

Parameters
namename of the function
expressionKaRL function body

Definition at line 636 of file Variables.cpp.

◆ define_function() [3/6]

void madara::knowledge::Variables::define_function ( const std::string &  name,
const std::string &  expression 
)

Defines a MADARA KaRL function.

Parameters
namename of the function
expressionKaRL function body

Definition at line 617 of file Variables.cpp.

◆ define_function() [4/6]

void madara::knowledge::Variables::define_function ( const std::string &  name,
jobject  callable 
)

Defines a named java function.

Parameters
namename of the function
callableexternal java object to call with this name

Definition at line 578 of file Variables.cpp.

◆ define_function() [5/6]

void madara::knowledge::Variables::define_function ( const std::string &  name,
knowledge::KnowledgeRecord(*)(const char *, FunctionArguments &, Variables &)  func 
)

Defines a named function that can distinguish the name it was called with in MADARA.

Parameters
namename of the function
funcexternal function to call with this name

◆ define_function() [6/6]

void madara::knowledge::Variables::define_function ( const std::string &  name,
knowledge::KnowledgeRecord(*)(FunctionArguments &, Variables &)  func 
)

Defines a function.

Parameters
namename of the function
funcexternal function to call with this name

Definition at line 562 of file Variables.cpp.

◆ evaluate() [1/2]

madara::knowledge::KnowledgeRecord madara::knowledge::Variables::evaluate ( CompiledExpression expression,
const KnowledgeUpdateSettings settings = knowledge::KnowledgeUpdateSettings() 
)

Evaluates an expression.

Recommended best practices are to compile the expression into a global variable or persistent store outside of the function call and use a reference to this CompiledExpression from within the external function that you create.

Parameters
expressionKaRL expression to evaluate (result of compile)
settingsSettings for updating knowledge
Returns
value of expression
Exceptions
exceptions::KarlExceptionfailure during compile/evaluate

Definition at line 683 of file Variables.cpp.

◆ evaluate() [2/2]

madara::knowledge::KnowledgeRecord madara::knowledge::Variables::evaluate ( const std::string &  expression,
const KnowledgeUpdateSettings settings = knowledge::KnowledgeUpdateSettings() 
)

Evaluates an expression (USE ONLY FOR PROTOTYPING; DO NOT USE IN PRODUCTION SYSTEMS).

Consider compiling the expression first with a one-time compile call during an initialization phase for your program, and then using the evaluate CompiledExpression call in any function that must be called frequently or periodically. The difference in overhead between this function and the compiled version is orders of magnitude (generally nanoseconds versus microseconds every call).

Parameters
expressionKaRL expression to evaluate
settingsSettings for evaluating and printing
Returns
value of expression
Exceptions
exceptions::KarlExceptionfailure during compile/evaluate

Definition at line 666 of file Variables.cpp.

◆ exists() [1/2]

bool madara::knowledge::Variables::exists ( const std::string &  key,
const KnowledgeReferenceSettings settings = KnowledgeReferenceSettings(          false) 
) const

Checks if a knowledge location exists in the context.

Parameters
keyknowledge variable location
settingssettings for referring to knowledge variables
Returns
true if location has been set

Definition at line 61 of file Variables.cpp.

◆ exists() [2/2]

bool madara::knowledge::Variables::exists ( const VariableReference variable,
const KnowledgeReferenceSettings settings = KnowledgeReferenceSettings(          false) 
) const

Checks if a knowledge variable exists in the context.

Parameters
variableknowledge variable reference
settingssettings for referring to knowledge variables
Returns
true if location has been set

Definition at line 79 of file Variables.cpp.

◆ expand_statement()

std::string madara::knowledge::Variables::expand_statement ( const std::string &  statement) const

Expands a string with variable expansion.

For instance, if .id == 5, and a statement of "MyVar{.id} = {.id} * 30" then then expanded statement would be "MyVar5 = 5 * 30".

Parameters
statementstatement to expand. Useful for printing.
Returns
variable expanded statement

Definition at line 543 of file Variables.cpp.

◆ get() [1/2]

madara::knowledge::KnowledgeRecord madara::knowledge::Variables::get ( const std::string &  key,
const KnowledgeReferenceSettings settings = knowledge::KnowledgeReferenceSettings(false) 
)

Retrieves the value of a variable.

Parameters
keyunique identifier of the variable
settingssettings when referring to variables
Returns
the value of the keyed variable

Definition at line 23 of file Variables.cpp.

◆ get() [2/2]

madara::knowledge::KnowledgeRecord madara::knowledge::Variables::get ( const VariableReference variable,
const KnowledgeReferenceSettings settings = knowledge::KnowledgeReferenceSettings(false) 
)

Retrieves the value of a variable.

Parameters
variablereference to a variable (
See also
get_ref)
Parameters
settingssettings when referring to variables
Returns
the value of the keyed variable

Definition at line 42 of file Variables.cpp.

◆ get_context()

madara::knowledge::ThreadSafeContext * madara::knowledge::Variables::get_context ( void  )

Returns the ThreadSafeContext associated with this Variables facade.

This is necessary for creating custom transports.

Returns
the context used by the knowledge base

Definition at line 17 of file Variables.cpp.

◆ get_matches()

void madara::knowledge::Variables::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.

Parameters
prefixstring that the key must start with
suffixstring that the key must end with
matchesall VariableReferences matching the prefix and suffix

Definition at line 719 of file Variables.cpp.

◆ get_ref()

madara::knowledge::VariableReference madara::knowledge::Variables::get_ref ( const std::string &  key,
const KnowledgeReferenceSettings settings = knowledge::KnowledgeReferenceSettings(false) 
)

Retrieves the value of a variable.

Parameters
keyunique identifier of the variable
settingssettings when referring to variables
Returns
a variable reference to the key (efficient accessor)

Definition at line 146 of file Variables.cpp.

◆ inc() [1/2]

madara::knowledge::KnowledgeRecord madara::knowledge::Variables::inc ( const std::string &  key,
const KnowledgeUpdateSettings settings = knowledge::KnowledgeUpdateSettings(false, true, false, false) 
)

Atomically increments the value of the variable.

Parameters
keyunique identifier of the variable
settingsSettings for updating knowledge
Returns
new value of variable

Definition at line 434 of file Variables.cpp.

◆ inc() [2/2]

madara::knowledge::KnowledgeRecord madara::knowledge::Variables::inc ( const VariableReference variable,
const KnowledgeUpdateSettings settings = knowledge::KnowledgeUpdateSettings(false, true, false, false) 
)

Atomically increments the value of the variable.

Parameters
variablereference to a variable (
See also
get_ref)
Parameters
settingsSettings for updating knowledge
Returns
new value of variable

Definition at line 451 of file Variables.cpp.

◆ load_context()

int64_t madara::knowledge::Variables::load_context ( const std::string &  filename,
const KnowledgeUpdateSettings settings = KnowledgeUpdateSettings(          true, true, true, false) 
)

Loads the context from a file.

Parameters
filenamename of the file to open
settingssettings to use when applying updates to context
Returns
total bytes read

Definition at line 807 of file Variables.cpp.

◆ operator=()

void madara::knowledge::Variables::operator= ( Variables rhs)

Assignment operator.

Definition at line 9 of file Variables.cpp.

◆ print() [1/2]

void madara::knowledge::Variables::print ( const std::string &  statement,
unsigned int  level = 0 
) const

Print a statement, similar to printf (variable expansions allowed) e.g., input = "MyVar{.id} = {MyVar{.id}}\n";.

Parameters
statementtemplated statement to print from
levellog level.
See also
LogMacros.h

Definition at line 529 of file Variables.cpp.

◆ print() [2/2]

void madara::knowledge::Variables::print ( unsigned int  level = 0) const

Prints all variables and values in the context.

Parameters
levellog level.
See also
LogMacros.h

Definition at line 516 of file Variables.cpp.

◆ retrieve_index() [1/2]

madara::knowledge::KnowledgeRecord madara::knowledge::Variables::retrieve_index ( const std::string &  key,
size_t  index,
const KnowledgeReferenceSettings settings = knowledge::KnowledgeReferenceSettings(false) 
)

Retrieves a value at a specified index within a knowledge array.

Parameters
keyvariable name of the array
indexindex within the array
settingssettings for referring to knowledge variables
Returns
value at knowledge location

Definition at line 111 of file Variables.cpp.

◆ retrieve_index() [2/2]

madara::knowledge::KnowledgeRecord madara::knowledge::Variables::retrieve_index ( const VariableReference variable,
size_t  index,
const KnowledgeReferenceSettings settings = knowledge::KnowledgeReferenceSettings(false) 
)

Retrieves a value at a specified index within a knowledge array.

Parameters
variablereference to a variable (
See also
get_ref)
Parameters
indexindex within the array
settingssettings for referring to knowledge variables
Returns
value at knowledge location

Definition at line 129 of file Variables.cpp.

◆ save_as_karl()

int64_t madara::knowledge::Variables::save_as_karl ( const std::string &  filename) const

Saves the context to a file as karl assignments, rather than binary.

Parameters
filenamename of the file to save to
Returns
total bytes written

Definition at line 768 of file Variables.cpp.

◆ save_checkpoint()

int64_t madara::knowledge::Variables::save_checkpoint ( const std::string &  filename,
bool  reset_modifieds = true 
)

Saves a checkpoint of a list of changes to a file.

Parameters
filenamename of the file to open
reset_modifiedsif true, resets the modified list to empty.
Returns
total bytes written

Definition at line 786 of file Variables.cpp.

◆ save_context()

int64_t madara::knowledge::Variables::save_context ( const std::string &  filename)

Saves the context to a file.

Parameters
filenamename of the file to open
Returns
total bytes written

Definition at line 751 of file Variables.cpp.

◆ set() [1/16]

int madara::knowledge::Variables::set ( const std::string &  key,
const double *  value,
uint32_t  size,
const KnowledgeUpdateSettings settings = knowledge::KnowledgeUpdateSettings(false, true, false, false) 
)

Atomically sets the value of a variable to a double array.

Parameters
keyunique identifier of the variable
valuean array of doubles
sizenumber of elements in the array
settingssettings for applying the update
Returns
0 if the value was set. -1 if null key

Definition at line 335 of file Variables.cpp.

◆ set() [2/16]

int madara::knowledge::Variables::set ( const std::string &  key,
const madara::knowledge::KnowledgeRecord value,
const KnowledgeUpdateSettings settings = knowledge::KnowledgeUpdateSettings(false, true, false, false) 
)

Sets the value of a variable.

Parameters
keyunique identifier of the variable
valuenew value of the variable
settingsSettings for updating knowledge
Returns
the new value of the keyed variable

Definition at line 267 of file Variables.cpp.

◆ set() [3/16]

int madara::knowledge::Variables::set ( const std::string &  key,
const madara::knowledge::KnowledgeRecord::Integer value,
uint32_t  size,
const KnowledgeUpdateSettings settings = knowledge::KnowledgeUpdateSettings(false, true, false, false) 
)

Atomically sets the value of a variable to an integer array.

Parameters
keyunique identifier of the variable
valuean array of Integers
sizenumber of elements in the array
settingssettings for applying the update
Returns
0 if the value was set. -1 if null key

Definition at line 197 of file Variables.cpp.

◆ set() [4/16]

int madara::knowledge::Variables::set ( const std::string &  key,
const std::string &  value,
const KnowledgeUpdateSettings settings = knowledge::KnowledgeUpdateSettings(false, true, false, false) 
)

Sets the value of a variable.

Parameters
keyunique identifier of the variable
valuenew value of the variable
settingsSettings for updating knowledge
Returns
the new value of the keyed variable

Definition at line 401 of file Variables.cpp.

◆ set() [5/16]

int madara::knowledge::Variables::set ( const std::string &  key,
const std::vector< double > &  value,
const KnowledgeUpdateSettings settings = knowledge::KnowledgeUpdateSettings(false, true, false, false) 
)

Atomically sets the value of a variable to a double array.

Parameters
keyunique identifier of the variable
valuea STL vector of doubles
settingssettings for applying the update
Returns
0 if the value was set. -1 if null key

Definition at line 368 of file Variables.cpp.

◆ set() [6/16]

int madara::knowledge::Variables::set ( const std::string &  key,
const std::vector< KnowledgeRecord::Integer > &  value,
const KnowledgeUpdateSettings settings = knowledge::KnowledgeUpdateSettings(false, true, false, false) 
)

Atomically sets the value of a variable to an integer array.

Parameters
keyunique identifier of the variable
valuea STL vector of Integers
settingssettings for applying the update
Returns
0 if the value was set. -1 if null key

Definition at line 232 of file Variables.cpp.

◆ set() [7/16]

int madara::knowledge::Variables::set ( const std::string &  key,
double  value,
const KnowledgeUpdateSettings settings = knowledge::KnowledgeUpdateSettings(false, true, false, false) 
)

Sets the value of a variable.

Parameters
keyunique identifier of the variable
valuenew value of the variable
settingsSettings for updating knowledge
Returns
the new value of the keyed variable

Definition at line 302 of file Variables.cpp.

◆ set() [8/16]

int madara::knowledge::Variables::set ( const std::string &  key,
madara::knowledge::KnowledgeRecord::Integer  value,
const KnowledgeUpdateSettings settings = knowledge::KnowledgeUpdateSettings(false, true, false, false) 
)

Sets the value of a variable.

Parameters
keyunique identifier of the variable
valuenew value of the variable
settingsSettings for updating knowledge
Returns
the new value of the keyed variable

Definition at line 162 of file Variables.cpp.

◆ set() [9/16]

int madara::knowledge::Variables::set ( const VariableReference variable,
const double *  value,
uint32_t  size,
const KnowledgeUpdateSettings settings = knowledge::KnowledgeUpdateSettings(false, true, false, false) 
)

Atomically sets the value of a variable to a double array.

Parameters
variablereference to a variable (
See also
get_ref)
Parameters
valuean array of doubles
sizenumber of elements in the array
settingssettings for applying the update
Returns
0 if the value was set. -1 if null key

Definition at line 352 of file Variables.cpp.

◆ set() [10/16]

int madara::knowledge::Variables::set ( const VariableReference variable,
const madara::knowledge::KnowledgeRecord value,
const KnowledgeUpdateSettings settings = knowledge::KnowledgeUpdateSettings(false, true, false, false) 
)

Sets the value of a variable.

Parameters
variablereference to a variable (
See also
get_ref)
Parameters
valuenew value of the variable
settingsSettings for updating knowledge
Returns
the new value of the keyed variable

Definition at line 285 of file Variables.cpp.

◆ set() [11/16]

int madara::knowledge::Variables::set ( const VariableReference variable,
const madara::knowledge::KnowledgeRecord::Integer value,
uint32_t  size,
const KnowledgeUpdateSettings settings = knowledge::KnowledgeUpdateSettings(false, true, false, false) 
)

Atomically sets the value of a variable to an integer array.

Parameters
variablereference to a variable (
See also
get_ref)
Parameters
valuean array of Integers
sizenumber of elements in the array
settingssettings for applying the update
Returns
0 if the value was set. -1 if null key

Definition at line 215 of file Variables.cpp.

◆ set() [12/16]

int madara::knowledge::Variables::set ( const VariableReference variable,
const std::string &  value,
const KnowledgeUpdateSettings settings = knowledge::KnowledgeUpdateSettings(false, true, false, false) 
)

Sets the value of a variable.

Parameters
variablereference to a variable (
See also
get_ref)
Parameters
valuenew value of the variable
settingsSettings for updating knowledge
Returns
the new value of the keyed variable

Definition at line 418 of file Variables.cpp.

◆ set() [13/16]

int madara::knowledge::Variables::set ( const VariableReference variable,
const std::vector< double > &  value,
const KnowledgeUpdateSettings settings = knowledge::KnowledgeUpdateSettings(false, true, false, false) 
)

Atomically sets the value of a variable to a double array.

Parameters
variablereference to a variable (
See also
get_ref)
Parameters
valuea STL vector of doubles
settingssettings for applying the update
Returns
0 if the value was set. -1 if null key

Definition at line 385 of file Variables.cpp.

◆ set() [14/16]

int madara::knowledge::Variables::set ( const VariableReference variable,
const std::vector< KnowledgeRecord::Integer > &  value,
const KnowledgeUpdateSettings settings = knowledge::KnowledgeUpdateSettings(false, true, false, false) 
)

Atomically sets the value of a variable to an integer array.

Parameters
variablereference to a variable (
See also
get_ref)
Parameters
valuea STL vector of Integers
settingssettings for applying the update
Returns
0 if the value was set. -1 if null key

Definition at line 250 of file Variables.cpp.

◆ set() [15/16]

int madara::knowledge::Variables::set ( const VariableReference variable,
double  value,
const KnowledgeUpdateSettings settings = knowledge::KnowledgeUpdateSettings(false, true, false, false) 
)

Sets the value of a variable.

Parameters
variablereference to a variable (
See also
get_ref)
Parameters
valuenew value of the variable
settingsSettings for updating knowledge
Returns
the new value of the keyed variable

Definition at line 319 of file Variables.cpp.

◆ set() [16/16]

int madara::knowledge::Variables::set ( const VariableReference variable,
madara::knowledge::KnowledgeRecord::Integer  value,
const KnowledgeUpdateSettings settings = knowledge::KnowledgeUpdateSettings(false, true, false, false) 
)

Sets the value of a variable.

Parameters
variablereference to a variable (
See also
get_ref)
Parameters
valuenew value of the variable
settingsSettings for updating knowledge
Returns
the new value of the keyed variable

Definition at line 180 of file Variables.cpp.

◆ to_map()

size_t madara::knowledge::Variables::to_map ( const std::string &  subject,
std::map< std::string, knowledge::KnowledgeRecord > &  target 
)

Fills a variable map with Knowledge Records that match an expression.

At the moment, this expression must be of the form "subject*"

Parameters
subjectAn expression that matches the variable names that are of interest. Wildcards may only be at the end.
targetThe map that will be filled with variable names and the Knowledge Records they correspond to
Returns
entries in the resulting map

Definition at line 733 of file Variables.cpp.

◆ to_string()

void madara::knowledge::Variables::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 function.

This is an optimized version that allows the specification of a target string to avoid string copying (which can be expensive with longer strings across multiple function boundaries). This function differs from knowledge::KnowledgeRecord to_string in that it is intended to save the database in a format that can be easily parseable. Consequently, strings are delineated in this function by being included in single quotes. Arrays are delineated with array indices [].

This is not appropriate for saving the context if it has binary data inside of it, as only the size of the data entry would be saved.

Parameters
targetwhere to store the resulting string operation.
array_delimiterfor any arrays, the characters in between data elements.
record_delimiterthe characters to place between individual key/value pairs
key_val_delimiterthe characters to place between keys and values.

Definition at line 500 of file Variables.cpp.

◆ to_vector()

size_t madara::knowledge::Variables::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 integer values.

Parameters
subjectThe common subject of the variable names. For instance, if we are looking for a range of vars like "var0", "var1", "var2", then the common subject would be "var".
startAn inclusive start index
endAn inclusive end index
targetThe vector that will be filled with Knowledge Record instances within the subject range.
Returns
entries in the resulting vector

Definition at line 701 of file Variables.cpp.

◆ write_file()

ssize_t madara::knowledge::Variables::write_file ( const std::string &  knowledge_key,
const std::string &  filename 
)

Write a file from the context to a specified location.

Parameters
filenamefile to write to
knowledge_keykey to read the file from
Returns
-1 if unsuccessful, 0 otherwise

Definition at line 826 of file Variables.cpp.

Friends And Related Function Documentation

◆ madara::expression::CompositeFunctionNode

Definition at line 55 of file Variables.h.

◆ madara::knowledge::KnowledgeRecordFilters

Definition at line 56 of file Variables.h.

Member Data Documentation

◆ context_

ThreadSafeContext* madara::knowledge::Variables::context_
private

Variables context that is directly used by the KaRL engine.

Definition at line 633 of file Variables.h.


The documentation for this class was generated from the following files: