MADARA  3.2.3
KnowledgeBaseImpl.h
Go to the documentation of this file.
1 #ifndef _MADARA_KNOWLEDGE_BASE_IMPL_H
2 #define _MADARA_KNOWLEDGE_BASE_IMPL_H
3 
11 #include <string>
12 #include <map>
13 
14 #include <ostream>
15 #include <vector>
16 
23 #include "madara/MadaraExport.h"
27 
28 namespace madara
29 {
30 
31  namespace knowledge
32  {
33 
39  {
40  public:
45 
51  KnowledgeBaseImpl (const std::string & host, int transport);
52 
59  KnowledgeBaseImpl (const std::string & host, int transport,
60  const std::string & domain);
61 
67  KnowledgeBaseImpl (const std::string & host,
69 
74 
78  void lock (void);
79 
84  void unlock (void);
85 
89  void activate_transport (void);
90 
95  size_t get_num_transports (void);
96 
100  void close_transport (void);
101 
111  void copy (const KnowledgeBaseImpl & source,
112  const KnowledgeRequirements & reqs);
113 
133  void copy (const KnowledgeBaseImpl & source,
134  const CopySet & copy_set = CopySet (),
135  bool clean_copy = false);
136 
144  const KnowledgeReferenceSettings & settings =
146 
154  get (const VariableReference & variable,
155  const KnowledgeReferenceSettings & settings =
157 
167  get_ref (const std::string & key,
168  const KnowledgeReferenceSettings & settings =
170 
175  template<typename K>
176  std::shared_ptr<std::string> share_string(K && key,
177  const KnowledgeReferenceSettings & settings =
179  {
180  return map_.share_string(std::forward<K>(key), settings);
181  }
182 
187  template<typename K>
188  std::shared_ptr<std::string> take_string(K && key,
189  const KnowledgeReferenceSettings & settings =
191  {
192  return map_.share_string(std::forward<K>(key), settings);
193  }
194 
199  template<typename K>
200  std::shared_ptr<std::vector<KnowledgeRecord::Integer>> share_integers(K && key,
201  const KnowledgeReferenceSettings & settings =
203  {
204  return map_.share_integers(std::forward<K>(key), settings);
205  }
206 
211  template<typename K>
212  std::shared_ptr<std::vector<KnowledgeRecord::Integer>> take_integers(K && key,
213  const KnowledgeReferenceSettings & settings =
215  {
216  return map_.share_integers(std::forward<K>(key), settings);
217  }
218 
223  template<typename K>
224  std::shared_ptr<std::vector<double>> share_doubles(K && key,
225  const KnowledgeReferenceSettings & settings =
227  {
228  return map_.share_doubles(std::forward<K>(key), settings);
229  }
230 
235  template<typename K>
236  std::shared_ptr<std::vector<double>> take_doubles(K && key,
237  const KnowledgeReferenceSettings & settings =
239  {
240  return map_.share_doubles(std::forward<K>(key), settings);
241  }
242 
247  template<typename K>
248  std::shared_ptr<std::vector<unsigned char>> share_binary(K && key,
249  const KnowledgeReferenceSettings & settings =
251  {
252  return map_.share_binary(std::forward<K>(key), settings);
253  }
254 
259  template<typename K>
260  std::shared_ptr<std::vector<unsigned char>> take_binary(K && key,
261  const KnowledgeReferenceSettings & settings =
263  {
264  return map_.share_binary(std::forward<K>(key), settings);
265  }
266 
272  void mark_modified (const VariableReference & variable,
273  const KnowledgeUpdateSettings & settings = KnowledgeUpdateSettings());
274 
280  void mark_modified (const std::string & name,
281  const KnowledgeUpdateSettings & settings = KnowledgeUpdateSettings());
282 
291  size_t index,
292  const KnowledgeReferenceSettings & settings =
294 
303  const VariableReference & variable,
304  size_t index,
305  const KnowledgeReferenceSettings & settings =
307 
316  std::string expand_statement (const std::string & statement) const;
317 
323  std::string debug_modifieds (void) const;
324 
329  int get_log_level (void);
330 
335  void attach_logger (logger::Logger & logger) const;
336 
341  logger::Logger & get_logger (void) const;
342 
347  void set_log_level (int level);
348 
349 #ifndef _MADARA_NO_KARL_
350 
359  compile (const std::string & expression);
360 
369 
379  const std::string & expression,
380  const EvalSettings & settings);
381 
391  CompiledExpression & expression,
392  const EvalSettings & settings);
393 
404  const EvalSettings & settings =
405  EvalSettings ());
406 
416 
428  const WaitSettings & settings);
429 
441  const WaitSettings & settings);
442 
448  void define_function (const std::string & name,
450 
457  void define_function (const std::string & name,
458  knowledge::KnowledgeRecord (*func) (const char *, FunctionArguments &, Variables &));
459 
460 #ifdef _MADARA_JAVA_
461 
466  void define_function (const std::string & name, jobject callable);
467 #endif
468 
469 #ifdef _MADARA_PYTHON_CALLBACKS_
470 
475  void define_function (const std::string & name, boost::python::object callable);
476 #endif
477 
483  void define_function (const std::string & name,
484  const std::string & expression);
485 
491  void define_function (const std::string & name,
492  const CompiledExpression & expression);
493 
494 #endif // _MADARA_NO_KARL_
495 
503  int read_file (const std::string & knowledge_key,
504  const std::string & filename,
505  const EvalSettings & settings =
506  EvalSettings ());
507 
514  ssize_t write_file (const std::string & knowledge_key,
515  const std::string & filename);
516 
517  template <typename K, typename V>
518  int set (K&& key, V&& val, const EvalSettings & settings) {
519  int result = map_.set (std::forward<K>(key),
520  std::forward<V>(val), settings);
521 
522  send_modifieds ("KnowledgeBaseImpl:set", settings);
523 
524  return result;
525  }
526 
527  template <typename K, typename V>
528  int set_index (K&& key, size_t i, V&& val,
529  const EvalSettings & settings) {
530  int result = map_.set_index (
531  std::forward<K>(key), i,
532  std::forward<V>(val), settings);
533 
534  send_modifieds ("KnowledgeBaseImpl:set_index", settings);
535 
536  return result;
537  }
538 
539  template <typename K, typename V>
540  int set (K&& key, const V * arr, size_t size,
541  const EvalSettings & settings) {
542  int result = map_.set (std::forward<K>(key), arr, (uint32_t)size, settings);
543 
544  send_modifieds ("KnowledgeBaseImpl:set", settings);
545 
546  return result;
547  }
548 
556  int
557  read_file (
558  const VariableReference & variable,
559  const std::string & filename,
560  const EvalSettings & settings =
561  EvalSettings ());
562 
571  int set_text (const VariableReference & variable,
572  const char * value, size_t size,
573  const EvalSettings & settings =
574  EvalSettings ());
575 
584  int set_file (const VariableReference & variable,
585  const unsigned char * value, size_t size,
586  const EvalSettings & settings =
587  EvalSettings ());
588 
597  int set_jpeg (const VariableReference & variable,
598  const unsigned char * value, size_t size,
599  const EvalSettings & settings =
600  EvalSettings ());
601 
610  int set_xml (const VariableReference & variable,
611  const char * value, size_t size,
612  const EvalSettings & settings =
613  EvalSettings ());
614 
620 
627  int apply_modified (const EvalSettings & settings =
628  EvalSettings ());
629 
637  bool exists (const std::string & key,
638  const KnowledgeReferenceSettings & settings =
639  KnowledgeReferenceSettings ()) const;
640 
648  bool exists (const VariableReference & variable,
649  const KnowledgeReferenceSettings & settings =
650  KnowledgeReferenceSettings (false)) const;
651 
659  void set_quality (const std::string & key, uint32_t quality,
660  const KnowledgeReferenceSettings & settings =
662 
667  void print (unsigned int level) const;
668 
677  void print (const std::string & statement, unsigned int level) const;
678 
701  void to_string (std::string & target,
702  const std::string & array_delimiter = ",",
703  const std::string & record_delimiter = ";\n",
704  const std::string & key_val_delimiter = "=") const;
705 
715  bool clear (const std::string & key,
716  const KnowledgeReferenceSettings & settings =
718 
732  void clear (bool erase = false);
733 
737  void clear_map (void);
738 
742  void clear_rules (void);
743 
749  void acquire (void);
750 
756  void release (void);
757 
768  size_t attach_transport (madara::transport::Base * transport);
769 
777  size_t attach_transport (const std::string & id,
778  transport::TransportSettings & settings);
779 
786  size_t remove_transport (size_t index);
787 
795 
800  std::string get_id (void);
801 
816  size_t to_vector (const std::string & subject,
817  unsigned int start,
818  unsigned int end,
819  std::vector <KnowledgeRecord> & target);
820 
828  void get_matches (
829  const std::string & prefix,
830  const std::string & suffix,
831  VariableReferences & matches);
832 
843  size_t to_map (const std::string & subject,
844  std::map <std::string, knowledge::KnowledgeRecord> & target);
845 
862  size_t to_map (const std::string & prefix,
863  const std::string & delimiter,
864  const std::string & suffix,
865  std::vector <std::string> & next_keys,
866  std::map <std::string, knowledge::KnowledgeRecord> & result,
867  bool just_keys);
868 
877  knowledge::KnowledgeMap to_map (const std::string & prefix) const;
878 
891 
897  int64_t save_context (const std::string & filename) const;
898 
906  int64_t save_context (CheckpointSettings & settings) const;
907 
913  int64_t save_as_json (const std::string & filename) const;
914 
920  int64_t save_as_json (const CheckpointSettings & settings) const;
921 
927  int64_t save_as_karl (const std::string & filename) const;
928 
934  int64_t save_as_karl (const CheckpointSettings & settings) const;
935 
943  int64_t save_checkpoint (const std::string & filename,
944  bool reset_modifieds = true);
945 
954  int64_t save_checkpoint (
955  CheckpointSettings & settings) const;
956 
966  int64_t load_context (const std::string & filename,
967  bool use_id,
968  const KnowledgeUpdateSettings & settings =
969  KnowledgeUpdateSettings (true, true, true, false));
970 
984  int64_t load_context (const std::string & filename,
985  FileHeader & meta,
986  bool use_id = true,
987  const KnowledgeUpdateSettings & settings =
988  KnowledgeUpdateSettings (true, true, true, false));
989 
998  int64_t load_context (CheckpointSettings & checkpoint_settings,
999  const KnowledgeUpdateSettings & update_settings =
1000  KnowledgeUpdateSettings (true, true, true, false));
1001 
1009  void clear_modifieds (void);
1010 
1014  void reset_checkpoint (void) const;
1015 
1020  void add_modifieds (const VariableReferences & modifieds) const;
1021 
1030  VariableReferences save_modifieds (void) const;
1031 
1039  MADARA_EXPORT int send_modifieds (
1040  const std::string & prefix,
1041  const EvalSettings & settings =
1042  EvalSettings ());
1043 
1047  void wait_for_change (void);
1048 
1058 
1059  private:
1063 
1064  std::vector<std::unique_ptr<transport::Base>> transports_;
1065  };
1066  }
1067 }
1068 
1069 // include the inline functions
1070 #include "KnowledgeBaseImpl.inl"
1071 
1072 #endif // _MADARA_KNOWLEDGE_BASE_IMPL_H
This class encapsulates an entry in a KnowledgeBase.
int64_t save_context(const std::string &filename) const
Saves the context 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...
std::map< std::string, bool > CopySet
Typedef for set of copyable keys.
size_t remove_transport(size_t index)
Removes a transport.
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())
Read a file into the knowledge base.
Defines a file header which is the default for KaRL checkpointing.
Definition: FileHeader.h:36
void print(unsigned int level) const
Prints all knowledge variables and values in the context.
ssize_t write_file(const std::string &knowledge_key, const std::string &filename)
Write a file from the knowledge base to a specified location.
std::shared_ptr< std::vector< double > > share_doubles(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
int apply_modified(const EvalSettings &settings=EvalSettings())
Applies current time and modified to all global variables and tries to send them. ...
size_t get_num_transports(void)
Gets the number of transports.
std::string expand_statement(const std::string &statement) const
Expands a statement using variable expansion.
int set_text(const VariableReference &variable, const char *value, size_t size, const EvalSettings &settings=EvalSettings())
Atomically sets the value of a variable to a text file&#39;s contents.
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.
logger::Logger & get_logger(void) const
Gets the logger used for information printing.
std::shared_ptr< std::string > take_string(K &&key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Returns a shared_ptr, while resetting this record to empty.
std::shared_ptr< std::vector< unsigned char > > take_binary(K &&key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Returns a shared_ptr, while resetting this record to empty.
std::shared_ptr< std::vector< unsigned char > > share_binary(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
void set_log_level(int level)
Sets the log level.
std::string get_id(void)
Returns the unique host and ephemeral binding for this Knowlede Base.
This class stores variables and their values for use by any entity needing state information in a thr...
void lock(void)
Locks the context to prevent updates over the network.
std::shared_ptr< std::vector< KnowledgeRecord::Integer > > share_integers(K &&key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Returns a shared_ptr, sharing with the internal one.
void wait_for_change(void)
Wait for a change to happen to the context (e.g., from transports)
Holds settings for checkpoints to load or save.
void copy(const KnowledgeBaseImpl &source, const KnowledgeRequirements &reqs)
Copies variables and values from source to this context.
void close_transport(void)
Closes the transport mechanism so no dissemination is possible.
Provides knowledge logging services to files and terminals.
Definition: GlobalLogger.h:11
VariableReferences save_modifieds(void) const
Saves the list of modified records to use later for resending.
Holds basic transport settings.
Optimized reference to a variable within the knowledge base.
Compiled, optimized KaRL logic.
std::vector< KnowledgeRecord > FunctionArguments
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...
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 &))
Defines a function.
size_t attach_transport(madara::transport::Base *transport)
Attaches a transport to the Knowledge Engine.
std::string debug_modifieds(void) const
Retrieves a stringified list of all modified variables that are ready to send over transport on next ...
A multi-threaded logger for logging to one or more destinations.
Definition: Logger.h:88
madara::knowledge::KnowledgeRecord wait(const std::string &expression)
Waits for an expression to be non-zero.
std::shared_ptr< std::string > share_string(K &&key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Returns a shared_ptr, sharing with the internal one.
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.
void clear_map(void)
Clears the knowledge base.
static struct madara::knowledge::tags::string_t string
std::shared_ptr< std::vector< double > > share_doubles(K &&key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Returns a shared_ptr, sharing with the internal one.
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.
std::vector< std::unique_ptr< transport::Base > > transports_
int64_t save_as_json(const std::string &filename) const
Saves the context to a file as JSON.
Holds settings requirements for knowledge, usually in copying.
void release(void)
Releases a recursive lock on the knowledge base.
::std::map< std::string, KnowledgeRecord > KnowledgeMap
std::shared_ptr< std::vector< unsigned char > > share_binary(K &&key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Returns a shared_ptr, sharing with the internal one.
std::shared_ptr< std::vector< KnowledgeRecord::Integer > > share_integers(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
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.
An abstract base class defines a simple abstract implementation of an expression tree node...
Definition: ComponentNode.h:36
bool exists(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Checks if a knowledge location exists in the context.
int get_log_level(void)
Gets the log level.
bool clear(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Clears a variable.
std::shared_ptr< std::vector< KnowledgeRecord::Integer > > take_integers(K &&key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Returns a shared_ptr, while resetting this record to empty.
void mark_modified(const VariableReference &variable, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Marks the variable reference as updated.
CompiledExpression compile(const std::string &expression)
Compiles a KaRL expression into an expression tree.
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 setup_unique_hostport(std::string host="")
Creates a random UUID for unique tie breakers in global ordering.
int set_index(K &&key, size_t i, V &&val, const EvalSettings &settings)
std::vector< VariableReference > VariableReferences
a vector of variable references
void reset_checkpoint(void) const
Resets the local changed map, which tracks checkpointing modifieds.
Container for quality-of-service settings.
knowledge::KnowledgeMap to_map_stripped(const std::string &prefix) const
Creates a map with Knowledge Records that begin with the given prefix.
Encapsulates settings for an evaluation statement.
Definition: EvalSettings.h:27
void clear_rules(void)
Clears the permanent knowledge rules (unimplemented)
int set_xml(const VariableReference &variable, const char *value, size_t size, const EvalSettings &settings=EvalSettings())
Atomically sets the value of a variable to an XML string.
void acquire(void)
Acquires the recursive lock on the knowledge base.
madara::transport::TransportSettings & transport_settings(void)
Returns a non-const reference to the Transport Settings.
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.
madara::knowledge::KnowledgeRecord evaluate(const std::string &expression)
Evaluates an expression.
Provides functions and classes for the distributed knowledge base.
void activate_transport(void)
Starts the transport mechanism for dissemination if it is closed.
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.
int64_t save_as_karl(const std::string &filename) const
Saves the context to a file as karl assignments, rather than binary.
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Atomically returns a reference to the variable.
Settings for applying knowledge updates.
MADARA_EXPORT int send_modifieds(const std::string &prefix, const EvalSettings &settings=EvalSettings())
Sends all modified variables through the attached transports.
Copyright (c) 2015 Carnegie Mellon University.
std::shared_ptr< std::string > share_string(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Encapsulates settings for a wait statement.
Definition: WaitSettings.h:23
Settings for applying knowledge updates.
Base class from which all transports must be derived.
Definition: Transport.h:45
void attach_logger(logger::Logger &logger) const
Attaches a logger to be used for printing.
int64_t save_checkpoint(const std::string &filename, bool reset_modifieds=true)
Saves a checkpoint of a list of changes to a file.
ThreadSafeContext & get_context(void)
Returns the ThreadSafeContext associated with this Knowledge Base.
std::shared_ptr< std::vector< double > > take_doubles(K &&key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Returns a shared_ptr, while resetting this record to empty.
Provides an interface for external functions into the MADARA KaRL variable settings.
transport::QoSTransportSettings settings_
void clear_modifieds(void)
Clear all modifications to the knowledge base.
void unlock(void)
Unlocks the context to allow updates over the network (is only necessary if the context has been expl...
void add_modifieds(const VariableReferences &modifieds) const
Adds a list of VariableReferences to the current modified list.