MADARA  3.1.8
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/MADARA_export.h"
25 #include "madara/knowledge/Files.h"
28 #include "ace/SOCK_Dgram.h"
29 
30 // declare ACE class so MADARA user will not have to directly link to ACE
31 class ACE_SOCK_Acceptor;
32 
33 namespace madara
34 {
35 
36  namespace knowledge
37  {
38 
44  {
45  public:
50 
56  KnowledgeBaseImpl (const std::string & host, int transport);
57 
64  KnowledgeBaseImpl (const std::string & host, int transport,
65  const std::string & domain);
66 
72  KnowledgeBaseImpl (const std::string & host,
74 
79 
83  void lock (void);
84 
89  void unlock (void);
90 
94  void activate_transport (void);
95 
100  size_t get_num_transports (void);
101 
105  void close_transport (void);
106 
116  void copy (const KnowledgeBaseImpl & source,
117  const KnowledgeRequirements & reqs);
118 
138  void copy (const KnowledgeBaseImpl & source,
139  const CopySet & copy_set = CopySet (),
140  bool clean_copy = false);
141 
149  const KnowledgeReferenceSettings & settings =
151 
159  get (const VariableReference & variable,
160  const KnowledgeReferenceSettings & settings =
162 
172  get_ref (const std::string & key,
173  const KnowledgeReferenceSettings & settings =
175 
180  template<typename K>
181  std::shared_ptr<std::string> share_string(K && key,
182  const KnowledgeReferenceSettings & settings =
184  {
185  return map_.share_string(std::forward<K>(key), settings);
186  }
187 
192  template<typename K>
193  std::shared_ptr<std::string> take_string(K && key,
194  const KnowledgeReferenceSettings & settings =
196  {
197  return map_.share_string(std::forward<K>(key), settings);
198  }
199 
204  template<typename K>
205  std::shared_ptr<std::vector<KnowledgeRecord::Integer>> share_integers(K && key,
206  const KnowledgeReferenceSettings & settings =
208  {
209  return map_.share_integers(std::forward<K>(key), settings);
210  }
211 
216  template<typename K>
217  std::shared_ptr<std::vector<KnowledgeRecord::Integer>> take_integers(K && key,
218  const KnowledgeReferenceSettings & settings =
220  {
221  return map_.share_integers(std::forward<K>(key), settings);
222  }
223 
228  template<typename K>
229  std::shared_ptr<std::vector<double>> share_doubles(K && key,
230  const KnowledgeReferenceSettings & settings =
232  {
233  return map_.share_doubles(std::forward<K>(key), settings);
234  }
235 
240  template<typename K>
241  std::shared_ptr<std::vector<double>> take_doubles(K && key,
242  const KnowledgeReferenceSettings & settings =
244  {
245  return map_.share_doubles(std::forward<K>(key), settings);
246  }
247 
252  template<typename K>
253  std::shared_ptr<std::vector<unsigned char>> share_binary(K && key,
254  const KnowledgeReferenceSettings & settings =
256  {
257  return map_.share_binary(std::forward<K>(key), settings);
258  }
259 
264  template<typename K>
265  std::shared_ptr<std::vector<unsigned char>> take_binary(K && key,
266  const KnowledgeReferenceSettings & settings =
268  {
269  return map_.share_binary(std::forward<K>(key), settings);
270  }
271 
277  void mark_modified (const VariableReference & variable,
278  const KnowledgeUpdateSettings & settings = KnowledgeUpdateSettings());
279 
285  void mark_modified (const std::string & name,
286  const KnowledgeUpdateSettings & settings = KnowledgeUpdateSettings());
287 
296  size_t index,
297  const KnowledgeReferenceSettings & settings =
299 
308  const VariableReference & variable,
309  size_t index,
310  const KnowledgeReferenceSettings & settings =
312 
321  std::string expand_statement (const std::string & statement) const;
322 
328  std::string debug_modifieds (void) const;
329 
334  int get_log_level (void);
335 
340  void attach_logger (logger::Logger & logger) const;
341 
346  logger::Logger & get_logger (void) const;
347 
352  void set_log_level (int level);
353 
354 #ifndef _MADARA_NO_KARL_
355 
363  compile (const std::string & expression);
364 
372 
381  const std::string & expression,
382  const EvalSettings & settings);
383 
392  CompiledExpression & expression,
393  const EvalSettings & settings);
394 
404  const EvalSettings & settings =
405  EvalSettings ());
406 
415 
426  const WaitSettings & settings);
427 
438  const WaitSettings & settings);
439 
445  void define_function (const std::string & name,
447 
454  void define_function (const std::string & name,
455  knowledge::KnowledgeRecord (*func) (const char *, FunctionArguments &, Variables &));
456 
457 #ifdef _MADARA_JAVA_
458 
463  void define_function (const std::string & name, jobject callable);
464 #endif
465 
466 #ifdef _MADARA_PYTHON_CALLBACKS_
467 
472  void define_function (const std::string & name, boost::python::object callable);
473 #endif
474 
480  void define_function (const std::string & name,
481  const std::string & expression);
482 
488  void define_function (const std::string & name,
489  const CompiledExpression & expression);
490 
491 #endif // _MADARA_NO_KARL_
492 
500  int read_file (const std::string & knowledge_key,
501  const std::string & filename,
502  const EvalSettings & settings =
503  EvalSettings ());
504 
511  ssize_t write_file (const std::string & knowledge_key,
512  const std::string & filename);
513 
519  int read_policy (const std::string & policy_key,
520  const std::string & policy_file);
521 #ifdef _USE_CID_
522 
526  void print_all_redeployment_results (std::ostream & output);
527 
532  void run_all (void);
533 
538  long start_latency (void);
539 
544  void print_my_latencies (std::ostream & output);
545 
550  void print_all_latencies (std::ostream & output);
551 
556  void print_all_summations (std::ostream & output);
557 
564  long vote (void);
565 
566 #endif // _USE_CID_
567 
568  template <typename K, typename V>
569  int set (K&& key, V&& val, const EvalSettings & settings) {
570  int result = map_.set (std::forward<K>(key),
571  std::forward<V>(val), settings);
572 
573  send_modifieds ("KnowledgeBaseImpl:set", settings);
574 
575  return result;
576  }
577 
578  template <typename K, typename V>
579  int set_index (K&& key, size_t i, V&& val,
580  const EvalSettings & settings) {
581  int result = map_.set_index (
582  std::forward<K>(key), i,
583  std::forward<V>(val), settings);
584 
585  send_modifieds ("KnowledgeBaseImpl:set_index", settings);
586 
587  return result;
588  }
589 
590  template <typename K, typename V>
591  int set (K&& key, const V * arr, size_t size,
592  const EvalSettings & settings) {
593  int result = map_.set (std::forward<K>(key), arr, size, settings);
594 
595  send_modifieds ("KnowledgeBaseImpl:set", settings);
596 
597  return result;
598  }
599 
607  int
608  read_file (
609  const VariableReference & variable,
610  const std::string & filename,
611  const EvalSettings & settings =
612  EvalSettings ());
613 
622  int set_text (const VariableReference & variable,
623  const char * value, size_t size,
624  const EvalSettings & settings =
625  EvalSettings ());
626 
635  int set_file (const VariableReference & variable,
636  const unsigned char * value, size_t size,
637  const EvalSettings & settings =
638  EvalSettings ());
639 
648  int set_jpeg (const VariableReference & variable,
649  const unsigned char * value, size_t size,
650  const EvalSettings & settings =
651  EvalSettings ());
652 
661  int set_xml (const VariableReference & variable,
662  const char * value, size_t size,
663  const EvalSettings & settings =
664  EvalSettings ());
665 
671 
678  int apply_modified (const EvalSettings & settings =
679  EvalSettings ());
680 
688  bool exists (const std::string & key,
689  const KnowledgeReferenceSettings & settings =
690  KnowledgeReferenceSettings ()) const;
691 
699  bool exists (const VariableReference & variable,
700  const KnowledgeReferenceSettings & settings =
701  KnowledgeReferenceSettings (false)) const;
702 
710  void set_quality (const std::string & key, uint32_t quality,
711  const KnowledgeReferenceSettings & settings =
713 
718  void print (unsigned int level) const;
719 
728  void print (const std::string & statement, unsigned int level) const;
729 
752  void to_string (std::string & target,
753  const std::string & array_delimiter = ",",
754  const std::string & record_delimiter = ";\n",
755  const std::string & key_val_delimiter = "=") const;
756 
766  bool clear (const std::string & key,
767  const KnowledgeReferenceSettings & settings =
769 
783  void clear (bool erase = false);
784 
788  void clear_map (void);
789 
793  void clear_rules (void);
794 
800  void acquire (void);
801 
807  void release (void);
808 
816  size_t attach_transport (madara::transport::Base * transport);
817 
825  size_t attach_transport (const std::string & id,
826  transport::TransportSettings & settings);
827 
834  size_t remove_transport (size_t index);
835 
843 
848  std::string get_id (void);
849 
864  size_t to_vector (const std::string & subject,
865  unsigned int start,
866  unsigned int end,
867  std::vector <KnowledgeRecord> & target);
868 
876  void get_matches (
877  const std::string & prefix,
878  const std::string & suffix,
879  VariableReferences & matches);
880 
891  size_t to_map (const std::string & subject,
892  std::map <std::string, knowledge::KnowledgeRecord> & target);
893 
910  size_t to_map (const std::string & prefix,
911  const std::string & delimiter,
912  const std::string & suffix,
913  std::vector <std::string> & next_keys,
914  std::map <std::string, knowledge::KnowledgeRecord> & result,
915  bool just_keys);
916 
925  knowledge::KnowledgeMap to_map (const std::string & prefix) const;
926 
939 
945  int64_t save_context (const std::string & filename) const;
946 
954  int64_t save_context (CheckpointSettings & settings) const;
955 
961  int64_t save_as_json (const std::string & filename) const;
962 
968  int64_t save_as_json (const CheckpointSettings & settings) const;
969 
975  int64_t save_as_karl (const std::string & filename) const;
976 
982  int64_t save_as_karl (const CheckpointSettings & settings) const;
983 
991  int64_t save_checkpoint (const std::string & filename,
992  bool reset_modifieds = true);
993 
1003  int64_t save_checkpoint (
1004  CheckpointSettings & settings) const;
1005 
1015  int64_t load_context (const std::string & filename,
1016  bool use_id,
1017  const KnowledgeUpdateSettings & settings =
1018  KnowledgeUpdateSettings (true, true, true, false));
1019 
1033  int64_t load_context (const std::string & filename,
1034  FileHeader & meta,
1035  bool use_id = true,
1036  const KnowledgeUpdateSettings & settings =
1037  KnowledgeUpdateSettings (true, true, true, false));
1038 
1047  int64_t load_context (CheckpointSettings & checkpoint_settings,
1048  const KnowledgeUpdateSettings & update_settings =
1049  KnowledgeUpdateSettings (true, true, true, false));
1050 
1058  void clear_modifieds (void);
1059 
1063  void reset_checkpoint (void) const;
1064 
1069  void add_modifieds (const VariableReferences & modifieds) const;
1070 
1079  VariableReferences save_modifieds (void) const;
1080 
1088  int send_modifieds (
1089  const std::string & prefix,
1090  const EvalSettings & settings =
1091  EvalSettings ());
1092 
1096  void wait_for_change (void);
1097 
1107  std::string setup_unique_hostport (const std::string & host = "");
1108 
1109  private:
1111  ACE_SOCK_Dgram unique_bind_;
1115 
1117  };
1118  }
1119 }
1120 
1121 // include the inline functions
1122 #include "KnowledgeBaseImpl.inl"
1123 
1124 #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:35
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
Returns a shared_ptr, sharing with the internal one.
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.
std::string setup_unique_hostport(const std::string &host="")
Binds to an ephemeral port for unique tie breakers in global ordering.
This class provides file repo capabilities to the knowledge base.
Definition: Files.h:24
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
Returns a shared_ptr, sharing with the internal one.
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.
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.
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
Returns a shared_ptr, sharing with the internal one.
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:35
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.
static constexpr struct madara::knowledge::tags::string_t string
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.
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:26
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.
int read_policy(const std::string &policy_key, const std::string &policy_file)
Read a policy into the knowledge base.
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
Returns a shared_ptr, sharing with the internal one.
Encapsulates settings for a wait statement.
Definition: WaitSettings.h:23
madara::transport::Transports transports_
Settings for applying knowledge updates.
Base class from which all transports must be derived.
Definition: Transport.h:62
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.