MADARA  3.4.1
VariableMapFilter.h
Go to the documentation of this file.
1 
2 
3 #ifndef _MADARA_FILTERS_VARIABLEMAP_H_
4 #define _MADARA_FILTERS_VARIABLEMAP_H_
5 
13 #include <string>
14 #include <vector>
15 #include <map>
16 #include <sstream>
17 
18 #include "AggregateFilter.h"
20 #include "madara/utility/Utility.h"
23 
24 namespace madara
25 {
26 namespace filters
27 {
33 {
34 public:
40  inline void process_config(const std::string& config)
41  {
42  initialized_ = false;
43 
44  std::vector<std::string> splitters, tokens, pivots;
45 
46  splitters.push_back("->");
47  splitters.push_back("\n");
48 
49  utility::tokenizer(config, splitters, tokens, pivots);
50 
51  if (tokens.size() % 2 == 0)
52  {
53  // valid configuration file
54  for (size_t i = 0; i + 1 < tokens.size(); i += 2)
55  {
56  utility::strip_white_space(tokens[i]);
57  utility::strip_white_space(tokens[i + 1]);
58 
59  if (tokens[i] == "" || tokens[i + 1] == "")
60  {
61  // invalid configuration file
64  "VariableMapFilter::process_config: ERROR: "
65  "configuration mappings has an empty map endpoint. "
66  "Unable to process config.\n")
67 
69  "VariableMapFilter::process_config: ERROR: "
70  "configuration mappings has an empty map endpoint. "
71  "Unable to process config.");
72  }
73  else
74  {
76  // std::pair <std::string, knowledge::VariableReference> entry;
77  // entry.first = tokens[i + 1];
78  // entry.second = ref;
79 
80  map_[tokens[i]] = {tokens[i + 1], ref};
81  }
82  }
83  }
84  else
85  {
86  // invalid configuration file
89  "VariableMapFilter::process_config: ERROR: "
90  "configuration mappings are incorrect. tokens.size=%d."
91  "Mappings must be in 'kb_var -> new_var' format, one each line. "
92  "Unable to process config.\n",
93  (int)tokens.size())
94 
96  "VariableMapFilter::process_config: ERROR: "
97  "configuration mappings are incorrect. Unable to process "
98  "config.");
99  }
100  }
101 
107  inline void read_config(const std::string& filename)
108  {
109  if (filename != "")
110  {
112  }
113  }
114 
119  VariableMapFilter(const std::string& filename = "") : initialized_(false)
120  {
121  process_config(filename);
122  }
123 
127  virtual ~VariableMapFilter() = default;
128 
134  inline virtual void filter(knowledge::KnowledgeMap& records,
136  {
137  // if we haven't setup valid VariableReferences yet, do so
138  if (!initialized_)
139  {
140  std::map<std::string,
141  std::pair<std::string, knowledge::VariableReference>>::iterator i =
142  map_.begin();
143 
144  for (; i != map_.end(); ++i)
145  {
146  // set the ref for the kb local variable
147  i->second.second = vars.get_ref(i->first);
148  }
149  }
150 
151  // iterate through the map and add the local variables to records
152  for (auto entry : map_)
153  {
154  records[entry.second.first] = vars.get(entry.second.second);
155  }
156  } // end filter function
157 
158 protected:
160 
165  std::map<std::string, std::pair<std::string, knowledge::VariableReference>>
167 };
168 }
169 }
170 
171 #endif // _MADARA_FILTERS_VARIABLEMAP_H_
#define madara_logger_ptr_log(loggering, level,...)
Fast version of the madara::logger::log method for Logger pointers.
Definition: Logger.h:41
An exception for attempting to access an invalid context1.
Abstract base class for implementing aggregate record filters via a functor interface.
Filter for mapping variables in the KB into a filter's outgoing or incoming messages.
VariableMapFilter(const std::string &filename="")
Constructor.
void read_config(const std::string &filename)
Reads a config file and processes the contents.
virtual void filter(knowledge::KnowledgeMap &records, const transport::TransportContext &, knowledge::Variables &vars)
Adds to the counter.
void process_config(const std::string &config)
Processes mappings from a config file.
virtual ~VariableMapFilter()=default
Destructor.
std::map< std::string, std::pair< std::string, knowledge::VariableReference > > map_
Map of local KB variable to a variable to add and a VariableReference to the local KB variable.
Optimized reference to a variable within the knowledge base.
Provides an interface for external functions into the MADARA KaRL variable settings.
Definition: Variables.h:53
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=knowledge::KnowledgeReferenceSettings(false))
Retrieves the value of a variable.
Definition: Variables.cpp:146
knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=knowledge::KnowledgeReferenceSettings(false))
Retrieves the value of a variable.
Definition: Variables.cpp:23
Provides context about the transport.
constexpr string_t string
T get(const KnowledgeRecord &kr)
Get the value of a KnowlegeRecord.
Definition: GetRecord.h:121
::std::map< std::string, KnowledgeRecord > KnowledgeMap
MADARA_EXPORT utility::Refcounter< logger::Logger > global_logger
std::string file_to_string(const std::string &filename)
Reads a file into a string.
Definition: Utility.cpp:324
std::string & strip_white_space(std::string &input)
Strip all whitespace.
Definition: Utility.cpp:124
void tokenizer(const std::string &input, const ::std::vector< std::string > &splitters, ::std::vector< std::string > &tokens, ::std::vector< std::string > &pivots)
Split a string into tokens.
Definition: Utility.cpp:224
Copyright(c) 2020 Galois.