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