Generated on Wed Apr 29 2015 11:51:40 for GGL-4.1.2 by doxygen 1.8.3.1
VF2_MatchingHandler.hh
Go to the documentation of this file.
1 
2 #ifndef SGM_VF2_MATCHINGHANDLER_HH_
3 #define SGM_VF2_MATCHINGHANDLER_HH_
4 
5 
6 #include "sgm/HashMap.hh"
7 #if HAVE_UNORDERED_MAP > 0
8  #include <unordered_map>
9 #elif HAVE_TR1_UNORDERED_MAP > 0
10  #include <tr1/unordered_map>
11 #elif HAVE_GNU_HASH_MAP > 0
12  #include <ext/hash_map>
13 #else
14  #include <map>
15 #endif
16 
17 #include <set>
18 #include <vector>
19 
20 #include <vf2/argraph.h>
21 #include <vf2/argedit.h>
22 
23 #include "sgm/Graph_Interface.hh"
24 #include "sgm/Pattern.hh"
25 #include "sgm/Match.hh"
26 #include "sgm/Match_Reporter.hh"
27 
28 #include "sgm/MC_Node.hh"
29 
30 
31 namespace sgm {
32 
33  /*! @brief General VF2-matching functionalities
34  *
35  * The general interface necessary to enable the use of the VF2 library
36  * for graph and sub graph matching. It covers the needed data structures
37  * and routines.
38  *
39  * @author Martin Mann (c) 2011 - http://www.bioinf.uni-freiburg.de/~mmann/
40  */
42 
43  protected:
44 
45  //! Data type that defines the the search mode of findMatches(..)
46  //! function
48 
49 
50  //! Label class used in the internal VF-2 data structures that just
51  //! indexes the known labels
52  typedef int Label;
53 
54  //! The data type that allows for the reuse of Label objects to be
55  //! more memory efficient.
56  typedef
57 #if HAVE_UNORDERED_MAP > 0
58  std::unordered_map< std::string, Label>
59 #elif HAVE_TR1_UNORDERED_MAP > 0
60  std::tr1::unordered_map< std::string, Label>
61 #elif HAVE_GNU_HASH_MAP > 0
62  __gnu_cxx::hash_map< std::string, Label, sgm::hash_string>
63 #else
64  std::map< std::string, Label>
65 #endif
67 
68 
69  //! Data handler for node information used for node comparison
70  class NodeData {
71  public:
72 
73  //! container that holds node constraints to be verified
74  typedef std::vector< const MC_Node* > NodeConstraints;
75 
76  //! the node index of the node represented
77  size_t id;
78  //! the label of the node
80  //! the out degree of the node
81  size_t outDegree;
82  //! the number of self loops of the node
83  size_t selfloops;
84  //! the node constraints to be checked
86  //! Default construction
87  NodeData();
88  //! Construction
89  //! @param id the node index of the node represented
90  //! @param label the node label
91  //! @param outDegree the out degree of the node
92  //! @param selfloops the number of self loops of the node
93  NodeData ( const size_t id
94  , const Label label
95  , const size_t outDegree
96  , const size_t selfloops );
97  //! Copy construction
98  //! @param toCopy the object to make this a copy of
99  NodeData( const VF2_MatchingHandler::NodeData & toCopy );
100  //! Destruction
101  ~NodeData();
102  };
103 
104  //! Comparator class for the node label used in the internal VF-2
105  //! data structures
106  class LabelComparator: public vf2::AttrComparator {
107  public:
108  //! the label defining the wildcard to use
110  //! construction
111  //! @param pWildcard access to the wildcard label
113  //! comparison of the two pointers.
114  //! @param pa first label pointer
115  //! @param pb second label pointer
116  //! @return true if pa and pb are both != NULL and pa
117  //! equals pb
118  virtual bool compatible(void *pa, void *pb) = 0;
119  //! comparison of the two Label objects.
120  //! @param a first label
121  //! @param b second label
122  //! @return true if a and b are both Label != 0 and either
123  //! of them is a wildcard or both are equal
124  virtual bool compatibleLabel(const Label & a, const Label & b);
125  };
126 
127  //! Storage to enable fast memory clean up of NodeLabels.
128  typedef std::vector< NodeData > NodeDataVec;
129 
130  //! collector for parallel edge handling, since VF2 does not support
131  //! multiple edges between two nodes. Therefore, all labels of
132  //! parallel edges are compiled into one edge.
133  typedef std::multiset<Label> EdgeLabel;
134 
135  //! Storage to enable fast memory clean up of EdgeLabels.
136  typedef std::vector< EdgeLabel* > EdgeLabelVec;
137 
138 
139  //////////////////// VF2 MATCH HANDLING ///////////////////////////////
140 
141  /*! @brief Handles match reporting from VF2-engine
142  *
143  * This class is needed to forward matches found by the VF-2 subgraph
144  * matching engine to a Match_Reporter instance.
145  *
146  */
148  public:
149  //! The global Match_Reporter in use to write the found matchings to
151 
152  //! The pattern reference used to write the found matchings
153  //! to matchReporter
155 
156  //! The target reference used to write the found matchings
157  //! to matchReporter
159 
160  //! The maximal number of hits to find
161  const size_t maxHitsToFind;
162 
163  //! the number of matches for the current target to be matched
164  size_t numOfMatches;
165 
166  /*!
167  * Constructs a match handler for vf2::match(..) usage.
168  * @param matchReporter all matches will be reported to this
169  * instance
170  * @param pattern the pattern graph needed to forward the report
171  * to matchReporter
172  * @param target the target graph needed to forward the report
173  * to matchReporter
174  * @param maxHitsToFind the maximal number of hits to find
175  */
177  , const Pattern_Interface& pattern
178  , const Graph_Interface& target
179  , const size_t maxHitsToFind
180  );
181 
182  //! A static match reporting function used for the vf2-lib match(..)
183  //! call. It is reporting to the matchReporter member variable
184  //! access via x. The usage of the function will report
185  //! maxHitsToFind matches of the pattern in the target graph.
186  //! @param n length of the arrays q and t and so equal to the number
187  //! of nodes of the pattern/query graph
188  //! @param q the matched indices from the pattern graph
189  //! @param t the node indices from the target graph where the indices
190  //! from the pattern graph (q[i]) were matched to
191  //! @param x this will hold a pointer to a VF2_match_handler
192  //! instance to access the reporter data
193  //! @return whether or not the search algorithm should search for
194  //! another solution based on the number of matches seen
195  //! (numOfMatches) and the targeted number (maxHitsToFind)
196  static
197  bool
198  report_matches(int n, vf2::node_id *q, vf2::node_id *t, void *x);
199 
200  };
201 
202 
203  public:
204 
206  virtual ~VF2_MatchingHandler();
207 
208  protected:
209 
210  //! container of patterns to match
211  typedef std::vector< const Pattern_Interface* > PatternVec;
212  //! container of match reporters to report matches to
213  typedef std::vector< Match_Reporter* > ReporterVec;
214 
215  /*!
216  * Performs exact sub-graph matching to find all occurences of the
217  * pattern graph within the target graph. Each hit is reported to
218  * the Match_Reporter object.
219  * @param pattern the container of pattern graphs to search for
220  * @param target the graph to search the pattern within
221  * @param output container of match reporters, one for each pattern
222  * to match, all hits of each pattern are reported to the
223  * according output object
224  * @param maxHits the maximal number of matches to find
225  * @return the number of exact matches found
226  */
227  template< class VF2STATE, class NODECOMPARE, class EDGECOMPARE >
228  static
229  size_t
230  findMatches ( const PatternVec & pattern,
231  const Graph_Interface & target,
232  ReporterVec & output,
233  const size_t maxHits );
234 
235  /*!
236  * Static function that is used by findMatches to fill the internally
237  * used ARGEdit objects.
238  * @param graph the graph to be 'filled' into the loader
239  * @param loader the loader to fill
240  * @param patternLabel the already known labels from the graph
241  * @param nodeData the container that will hold the node data.
242  * @param edgeLabels the container that will hold all edge labels.
243  * @param isPattern If true, than graph will be used to fill
244  * patternLabel and all nodes and edges in the loader will get
245  * a label. If false, only known label from the pattern graph
246  * are set in the loader, all other are set to NULL.
247  * @return false if isPattern == false and at least one label in the
248  * graph is not present in patternLabel, true otherwise
249  */
250  static
251  bool
252  fillLoader( const Graph_Interface& graph,
253  vf2::ARGEdit & loader,
254  PatternMap & patternLabel,
255  NodeDataVec & nodeData,
256  EdgeLabelVec & edgeLabels,
257  bool isPattern );
258 
259  /*!
260  * Checks whether or not a given pattern match fulfills the additional
261  * matching constraints requested by the pattern.
262  *
263  * @param pattern the pattern to be found
264  * @param target the graph the left side was found within, i.e. in
265  * that the rule should be applied
266  * @param match contains the indices of the matched left side nodes in
267  * the target graph. match[i] corresponds to the mapping of the i-th
268  * vertex in the leftSide graph.
269  */
270  static
271  bool
272  isApplicable ( const Pattern_Interface& pattern,
273  const Graph_Interface & target,
274  const Match & match );
275 
276  /*!
277  * checks whether or not the pattern is compatible with the target
278  * based on the out degree information of the nodes.
279  *
280  * @param patternNodes the node data of the pattern
281  * @param targetNodes the node data of the target
282  * @return true, if the pattern node degree distribution is compatible
283  * with the distribution of the target; false otherwise
284  */
285  static
286  bool
287  areDegreeCompatible( const NodeDataVec & patternNodes
288  , const NodeDataVec & targetNodes );
289 
290  };
291 
292 }
293 
294 #include "sgm/VF2_MatchingHandler.icc"
295 
296 #endif /* SGM_VF2_MATCHINGHANDLER_HH_ */