MADARA  3.4.1
DynamicPredicateFilter.h
Go to the documentation of this file.
1 
2 
3 #ifndef _MADARA_FILTERS_DYNAMIC_PREDICATE_FILTER_H_
4 #define _MADARA_FILTERS_DYNAMIC_PREDICATE_FILTER_H_
5 
13 #include "AggregateFilter.h"
15 #include "madara/utility/Utility.h"
16 
17 namespace madara
18 {
19 namespace filters
20 {
25 {
26 public:
33  const std::string& string_vector_name = "predicates.allowed")
34  : initialized_(false), name(string_vector_name), predicates_()
35  {
36  }
37 
41  virtual ~DynamicPredicateFilter() = default;
42 
48  inline virtual void filter(knowledge::KnowledgeMap& records,
50  {
51  // local copy (fast access to predicate vector)
52  std::vector<std::string> predicates;
53 
54  if (!initialized_)
55  {
56  // set the initial vector up
57  predicates_.set_name(name, vars);
58  initialized_ = true;
59  }
60  else
61  {
62  // resize for what the current vector is indicating
64  }
65 
66  // copy the knowledge base predicates to a STL vector for speed
67  predicates_.copy_to(predicates);
68 
69  // by default, the vector is empty and all predicates are accepted
70  if (predicates.size() > 0)
71  {
72  // because of the usage of erase, don't auto inc record in for loop
73  for (auto record = records.begin(); record != records.end();)
74  {
75  // check for valid predicate
76  bool accepted_predicate = false;
77  for (auto predicate : predicates)
78  {
79  if (utility::ends_with(predicate, "*"))
80  {
81  std::string prefix = predicate;
82  prefix.pop_back();
83  if (utility::begins_with(record->first, prefix))
84  {
85  accepted_predicate = true;
86  }
87  }
88  else if (record->first == predicate)
89  {
90  accepted_predicate = true;
91  }
92  }
93 
94  // if not valid, remove the record and update iterator
95  if (!accepted_predicate)
96  {
99  "DynamicPredicateFilter::filter: "
100  "removing variable %s\n",
101  record->first.c_str())
102 
103  record = records.erase(record);
104  } // end not valid predicate
105  else
106  {
107  // valid predicate so keep the record and proceed to next
108  ++record;
109  } // end valid predicate
110  } // end iteration over records
111  } // end if there are predicates to enforce
112  } // end filter method
113 
114 private:
119 
120 public:
125 
126 protected:
131 };
132 }
133 }
134 
135 #endif // _MADARA_FILTERS_DYNAMIC_PREDICATE_FILTER_H_
#define madara_logger_ptr_log(loggering, level,...)
Fast version of the madara::logger::log method for Logger pointers.
Definition: Logger.h:41
Abstract base class for implementing aggregate record filters via a functor interface.
Filter for removing variables with any unwanted predicates.
DynamicPredicateFilter(const std::string &string_vector_name="predicates.allowed")
Constructor.
knowledge::containers::StringVector predicates_
A map of discovered peers.
virtual ~DynamicPredicateFilter()=default
Destructor.
std::string name
The string vector name to bind to inside the knowledge base.
bool initialized_
Tracks if the predicate vector has been initialized.
virtual void filter(knowledge::KnowledgeMap &records, const transport::TransportContext &, knowledge::Variables &vars)
Removes records not in a list of allowed predicates.
Provides an interface for external functions into the MADARA KaRL variable settings.
Definition: Variables.h:53
This class stores a vector of strings inside of KaRL.
Definition: StringVector.h:32
void set_name(const std::string &var_name, KnowledgeBase &knowledge, int size=-1)
Sets the variable name that this refers to.
void copy_to(KnowledgeVector &target) const
Copies the vector elements to an STL vector of Knowledge Records.
void resize(int size=-1, bool delete_vars=true)
Resizes the vector.
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
MADARA_EXPORT bool ends_with(const std::string &input, const std::string &ending)
Check if input contains a pattern at the end.
Definition: Utility.inl:650
MADARA_EXPORT bool begins_with(const std::string &input, const std::string &prefix)
Check if input contains prefix at the beginning.
Definition: Utility.inl:638
Copyright(c) 2020 Galois.