graph_analysis
Filter.hpp
Go to the documentation of this file.
1 #ifndef GRAPH_ANALYSIS_FILTER_HPP
2 #define GRAPH_ANALYSIS_FILTER_HPP
3 
4 #include <map>
5 #include <vector>
6 #include <stdexcept>
7 #include <assert.h>
8 #include "Edge.hpp"
9 #include "SharedPtr.hpp"
10 
11 namespace graph_analysis {
12 namespace filters {
13 
15 enum Operator { AND = 0, OR };
17 enum Type { CONTENT = 0, CLASS };
18 extern std::map<Type, std::string> TypeTxt;
19 extern std::map<std::string, Type> TxtType;
20 
21 } // end namespace filters
22 
54 template<typename FilterObject>
55 class Filter
56 {
57 public:
59  typedef shared_ptr<FilterType> Ptr;
60 
61  typedef std::vector< FilterType::Ptr > FilterList;
62 
66  virtual ~Filter() {}
67 
72  virtual std::string getName() const { return "graph_analysis::Filter"; }
73 
78  virtual std::string toString() const { return getName(); }
79 
88  int replace(FilterType::Ptr filter, int position = -1)
89  {
90  if(position > -1)
91  {
92  // trigger out of range exception if position does not exist
93  FilterType::Ptr filterAtPosition = mFilters.at(position);
94  removeAt(position);
95  }
96  return add(filter, position);
97  }
98 
102  void clear()
103  {
104  mFilters.clear();
105  }
106 
113  int add(FilterType::Ptr filter, int position = -1)
114  {
115  if(filter)
116  {
117  if(position > -1)
118  {
119  mFilters.insert(mFilters.begin() + position, filter);
120  return position;
121  } else {
122  mFilters.push_back(filter);
123  return mFilters.size() - 1;
124  }
125  } else {
126  throw std::runtime_error("Filter: cannot add a null object as filter");
127  }
128  }
129 
130  void removeAt(int position)
131  {
132  if(position > mFilters.size() -1)
133  {
134  throw std::runtime_error("Filter: cannot remove filter. Position outside of current list");
135  }
136  FilterList list;
137  for(int i = 0; i < mFilters.size(); ++i)
138  {
139  if(i != position)
140  {
141  list.push_back(mFilters[i]);
142  }
143  }
144  mFilters = list;
145  }
146 
156  bool matches(FilterObject o, filters::Operator op = filters::OR) const
157  {
158 
159  bool result = apply(o);
160 
161  typename FilterList::const_iterator cit = mFilters.begin();
162  if(op == filters::OR)
163  {
164  if(result)
165  {
166  return true;
167  }
168 
169  for(; cit != mFilters.end(); ++cit)
170  {
171 
172  FilterType::Ptr filter = *cit;
173  assert(filter);
174  if(filter->matches(o))
175  {
176  return true;
177  }
178  }
179  result = false;
180 
181  } else if(op == filters::AND)
182  {
183  if(!result)
184  {
185  return false;
186  }
187 
188  for(; cit != mFilters.end(); ++cit)
189  {
190  FilterType::Ptr filter = *cit;
191  assert(filter);
192  // filter should be set to true here, otherwise false
193  // will be returned
194  if(!filter->matches(o))
195  {
196  return false;
197  }
198  }
199  }
200  return result;
201  }
202 
208  virtual bool apply(FilterObject o) const { (void)o; return false; }
209 
213  static Ptr Null() { return Ptr(); }
214 
215 protected:
216  FilterList mFilters;
217 };
218 
219 } // end namespace graph_analysis
220 #endif // GRAPH_ANALYSIS_FILTER_HPP
Filter for a graph.
Definition: Filter.hpp:55
int add(FilterType::Ptr filter, int position=-1)
Definition: Filter.hpp:113
Definition: Filter.hpp:17
Filter< FilterObject > FilterType
Definition: Filter.hpp:58
virtual ~Filter()
Definition: Filter.hpp:66
Definition: Filter.hpp:15
std::map< Type, std::string > TypeTxt
Definition: Filter.cpp:7
shared_ptr< FilterType > Ptr
Definition: Filter.hpp:59
FilterList mFilters
Definition: Filter.hpp:216
int replace(FilterType::Ptr filter, int position=-1)
Definition: Filter.hpp:88
virtual bool apply(FilterObject o) const
Apply only the main filter(!) to the target object.
Definition: Filter.hpp:208
void removeAt(int position)
Definition: Filter.hpp:130
Definition: Filter.hpp:17
virtual std::string toString() const
Get string representation of filter.
Definition: Filter.hpp:78
static Ptr Null()
Definition: Filter.hpp:213
Main namespace of the graph analysis library.
Definition: BFS.cpp:3
std::vector< FilterType::Ptr > FilterList
Definition: Filter.hpp:61
Definition: Filter.hpp:15
bool matches(FilterObject o, filters::Operator op=filters::OR) const
Apply all filters, i.e. root filter and children to the filter object, i.e., test whether filter matc...
Definition: Filter.hpp:156
virtual std::string getName() const
Get Name of the filter.
Definition: Filter.hpp:72
Operator
Operator.
Definition: Filter.hpp:15
std::map< std::string, Type > TxtType
Definition: Filter.cpp:11
Type
Filters can operator on content or class information.
Definition: Filter.hpp:17
void clear()
Definition: Filter.hpp:102