graph_analysis
RegexFilters.hpp
Go to the documentation of this file.
1 #ifndef GRAPH_ANALYSIS_FILTERS_REGEX_FILTER_HPP
2 #define GRAPH_ANALYSIS_FILTERS_REGEX_FILTER_HPP
3 
4 #include <boost/regex.hpp>
5 #include "../Vertex.hpp"
6 #include "../Edge.hpp"
7 #include "CommonFilters.hpp"
8 #include <base-logging/Logging.hpp>
9 
10 namespace graph_analysis {
11 namespace filters {
12 
17 template<typename T>
19 {
20 protected:
21  boost::regex mRegex;
23  bool mInverted;
24 
25 public:
26  RegexFilter(const std::string& regex, Type type, bool invert)
27  : mRegex(regex)
28  , mType(type)
29  , mInverted(invert)
30  {}
31 
32  virtual std::string getName() const { return "graph_analysis::filters::RegexFilter: '" + toString() + "'"; }
33 
34  virtual std::string toString() const
35  {
36  std::string txt = mRegex.str() + "/" + TypeTxt[mType] + "/";
37  if(mInverted)
38  {
39  txt += "inverted";
40  } else {
41  txt += "not-inverted";
42  }
43  return txt;
44  }
45 
46  virtual bool apply(T element) const
47  {
48  bool result = false;
49  switch(mType)
50  {
51  case CONTENT:
52  result = regex_match(element->toString(), mRegex);
53  break;
54  case CLASS:
55  result = regex_match(element->getClassName(), mRegex);
56  break;
57  default:
58  throw std::runtime_error("graph_analysis::filters::RegexFilter unknown filter type provided");
59  }
60 
61  if(mInverted)
62  {
63  return !result;
64  } else {
65  return result;
66  }
67  }
68 };
69 
74 class VertexRegexFilter : public RegexFilter< ::graph_analysis::Vertex::Ptr >
75 {
76 public:
77  VertexRegexFilter(const std::string& regex = std::string(), Type type = CONTENT, bool invert = false);
78 
79  virtual std::string getName() const;
80 };
81 
86 class EdgeRegexFilter : public RegexFilter< ::graph_analysis::Edge::Ptr >
87 {
88 public:
89  EdgeRegexFilter(const std::string& regex = std::string(), Type type = CONTENT, bool invert = false);
90 
91  virtual std::string getName() const;
92 };
93 
98 {
99 public:
100  CombinedEdgeRegexFilter(VertexRegexFilter sourceNodeFilter, EdgeRegexFilter edgeFilter, VertexRegexFilter targetNodeFilter);
101 
102  virtual ~CombinedEdgeRegexFilter();
103 
104  virtual std::string getName() const;
105  virtual std::string toString() const;
106 
107  virtual bool evaluateSource(graph_analysis::Edge::Ptr e) const;
108  virtual bool evaluateTarget(graph_analysis::Edge::Ptr e) const;
109 
110 private:
111  EdgeRegexFilter mEdgeRegexFilter;
112  VertexRegexFilter mSourceNodeFilter;
113  VertexRegexFilter mTargetNodeFilter;
114 };
115 
116 } // end namespace filters
117 } // end namespace graph_analysis
118 #endif // GRAPH_ANALYSIS_FILTERS_REGEX_FILTER_HPP
Filter for a graph.
Definition: Filter.hpp:55
Definition: Filter.hpp:17
RegexFilter(const std::string &regex, Type type, bool invert)
Definition: RegexFilters.hpp:26
std::map< Type, std::string > TypeTxt
Definition: Filter.cpp:7
Type mType
Definition: RegexFilters.hpp:22
Filter to sort out vertices.
Definition: RegexFilters.hpp:74
shared_ptr< Edge > Ptr
Definition: Edge.hpp:21
Definition: Filter.hpp:17
Filter to sort out edges.
Definition: RegexFilters.hpp:86
Definition: EdgeContextFilter.hpp:12
bool mInverted
Definition: RegexFilters.hpp:23
virtual std::string getName() const
Get Name of the filter.
Definition: RegexFilters.hpp:32
Main namespace of the graph analysis library.
Definition: BFS.cpp:3
virtual std::string toString() const
Get string representation of filter.
Definition: RegexFilters.hpp:34
virtual bool apply(T element) const
Apply only the main filter(!) to the target object.
Definition: RegexFilters.hpp:46
Definition: RegexFilters.hpp:18
Type
Filters can operator on content or class information.
Definition: Filter.hpp:17
boost::regex mRegex
Definition: RegexFilters.hpp:21