Generated on Wed Apr 29 2015 11:51:40 for GGL-4.1.2 by doxygen 1.8.3.1
Rule.hh
Go to the documentation of this file.
1 #ifndef GGL_RULE_HH_
2 #define GGL_RULE_HH_
3 
4 #include "sgm/HashMap.hh"
5 #if HAVE_UNORDERED_MAP > 0
6  #include <unordered_map>
7 #elif HAVE_TR1_UNORDERED_MAP > 0
8  #include <tr1/unordered_map>
9 #elif HAVE_GNU_HASH_MAP > 0
10  #include <ext/hash_map>
11 #else
12  #include <map>
13 #endif
14 
15 #include <boost/graph/properties.hpp>
16 #include <boost/graph/adjacency_list.hpp>
17 
18 #include "sgm/MC_Node.hh"
19 #include "sgm/MC_Edge.hh"
20 
21 namespace ggl {
22 
23 
24 
25 ////////////////////////////////////////////////////////////////////////////////
26 
27  /*! @brief Rule consistency codes
28  *
29  * A helper class that holds constants for consistency or error encodings
30  * that are independent of any template parameter.
31  *
32  * @author Martin Mann (c) 2012 http://www.bioinf.uni-freiburg.de/~mmann/
33  */
34  class Rule_ConCodes {
35  public:
36  //! consistency code : everything fine
37  static const size_t C_Consistent;
38  //! consistency code : no rule ID is present
39  static const size_t C_NoID;
40  //! consistency code : at least one edge shows wrong context usage
41  static const size_t C_WrongEdgeContext;
42 
43  };
44 
45 
46 ////////////////////////////////////////////////////////////////////////////////
47 
48 
49  /*! Graph grammar rule data
50  *
51  * A description of a graph grammar rule with left and righ rule side and
52  * context description.
53  *
54  * @author Martin Mann (c) 2008 http://www.bioinf.uni-freiburg.de/~mmann/
55  *
56  */
57  class Rule : public Rule_ConCodes {
58 
59  public:
60 
61 
62  //! Vector of node indices.
63  typedef std::vector< size_t > NodeVec;
64 
65  //! If a node has outgoing edges, this class contains the indices of the
66  //! ends of these edges.
67 #if HAVE_UNORDERED_MAP > 0
68  typedef std::unordered_map< size_t, NodeVec > OutEdgeMap;
69  typedef std::unordered_map< size_t, size_t > NodeMap;
70 #elif HAVE_TR1_UNORDERED_MAP > 0
71  typedef std::tr1::unordered_map< size_t, NodeVec > OutEdgeMap;
72  typedef std::tr1::unordered_map< size_t, size_t > NodeMap;
73 #elif HAVE_GNU_HASH_MAP > 0
74  typedef __gnu_cxx::hash_map< size_t, NodeVec > OutEdgeMap;
75  typedef __gnu_cxx::hash_map< size_t, size_t > NodeMap;
76 #else
77  typedef std::map< size_t, NodeVec > OutEdgeMap;
78  typedef std::map< size_t, size_t > NodeMap;
79 #endif
80 
81  //! A type definition to encode the different use cases of a node or edge
82  //! of a graph grammar rule.
83  enum RuleContext { RULE_LEFT_SIDE = 2 //!< graph element is ONLY part of the left rule side
84  , RULE_RIGHT_SIDE = 3 //!< graph element is ONLY part of the right rule side
85  , RULE_LABEL_CHANGE = (RULE_LEFT_SIDE * RULE_RIGHT_SIDE) //!< node is part of the both rule sides but the LABEL is CHANGING. NOTE: only valid for nodes!!!
86  , RULE_CONTEXT = 5 * RULE_LABEL_CHANGE //!< graph element is part of the both rule sides = context of the rule
87  };
88 
89  //! This boost graph property is used to determine the index of a given
90  //! node along the iterator order.
91  typedef boost::vertex_index_t NodeIndexProperty;
92 
93  //! This boost graph property is used to determine the label of a given
94  //! node. If the specified node changes the label in the left and right
95  //! side of the graph grammar rule, this property specifies the label of
96  //! the LEFT side of the node.
97  typedef boost::vertex_name_t NodeLabelProperty;
98 
99  //! This boost graph property is used to determine if a node is part of
100  //! the left, right, or both sides of a graph grammar rule.
102  typedef boost::vertex_property_tag kind;
103  };
104 
105  //! This boost graph property is used to determine if a node is part of
106  //! the left, right, or both sides of a graph grammar rule.
108  typedef boost::vertex_property_tag kind;
109  };
110 
111 
112  //! The properties available for the nodes of a CoreGraph
113  typedef boost::property< NodeIndexProperty, size_t
114  , boost::property< NodeLabelProperty, std::string
115  , boost::property< NodeRightLabelProperty, std::string
116  , boost::property< NodeContextProperty, RuleContext
117  > > > >
119 
120  //! This boost graph property is used to determine the label of a given
121  //! edge. If the specified edge changes the label in the left and right
122  //! side of the graph grammar rule, this property specifies the label of
123  //! the LEFT side of the edge.
124  typedef boost::edge_name_t EdgeLabelProperty;
125 
126  //! This boost graph property is used to determine if an edge is part of
127  //! the left, right, or both sides of a graph grammar rule.
129  typedef boost::edge_property_tag kind;
130  };
131 
132  /*! The properties available for the edges of a CoreGraph
133  *
134  * NOTE: EdgeContextProperty is NOT TO BE allowed to be RULE_LABEL_CHANGE.
135  * Label changes of edges are to be encoded by insertion and deletion of
136  * the edge, ie. present two times with according RULE_LEFT_SIDE and
137  * RULE_RIGHT_SIDE EdgeContextProperty value.
138  */
139  typedef boost::property< EdgeLabelProperty, std::string
140  , boost::property< EdgeContextProperty, RuleContext
141  > >
143 
144  //! The definition of a the internal graph representation of a graph
145  //! grammar rule of undirected graphs.
146  typedef boost::adjacency_list<
147  boost::vecS, // store edges
148  boost::vecS, // store vertices
149  boost::undirectedS, // is an undirected graph
150  CoreGraph_NodeProperties, // (atom symbols etc)
151  CoreGraph_EdgeProperties // (edge symbols etc)
152  >
154 
155  //! vector of constraints to be matched
156  typedef std::vector< sgm::Pattern_Interface::Match_Constraint* >
158 
159  protected:
160  ///////////////// DATA MEMBERS /////////////////////////
161 
162  //! the boost graph that is encoding the graph grammar rule
164 
165  //! the id or description of this rules
166  std::string id;
167 
168  //! the wildcard string to use for left side pattern matching
169  std::string * wildcard;
170 
171 
172  public:
173  ///////////////// CONSISTENCY TYPEDEFS /////////////////////////
174 
175  //! consistency code : everything fine
177  //! consistency code : no rule ID is present
178  using Rule_ConCodes::C_NoID;
179 
180  ///////////////////////////////////////////////////////////////////////
181 
182  //! A container that stores the information of a context side of
183  //! a graph grammar rule like left, right, and context.
184  class RuleSide {
185  protected:
186 
187  // to enable direct access from enclosing class
188  friend class Rule;
189 
190  /*! Construction
191  * @param nodes the node indices in the core graph that are
192  * present in this Rule side
193  * @param constraints the additional constraint for this rule side
194  * where the node IDs are given according the core of the
195  * rule
196  */
197  RuleSide( const NodeVec& nodes
198  , const MatchConstraintVec * const constraints = NULL );
199 
200  //! copy construction
201  //! @param toCopy the object to make a copy of
202  RuleSide( const RuleSide& toCopy );
203 
204  //! destruction
205  ~RuleSide();
206 
207  //! assignment operator
208  //! @param toCopy the object to make this a copy of
209  //! @return access to the changed *this object
210  RuleSide&
211  operator=( const RuleSide& toCopy );
212 
213  public:
214 
215  //! the list of node indices in the core graph present in this
216  //! RuleSide
218  //! the additional constraints for this RuleSide
220  };
221 
222  ///////////////////////////////////////////////////////////////////////
223 
224 
225  /*!
226  * Class to define copy-and-Paste operations for adjacent edges of
227  * nodes to be removed. They are used to relink dangling edges to
228  * other nodes of the result graph.
229  */
230  class RuleCnP {
231 
232  public:
233 
234  //! the type that defines a set of labels
235  typedef std::set< std::string > LabelSet;
236 
237  /*! Default construction initializting with INT_MAX
238  */
239  RuleCnP( );
240 
241  /*! Construction
242  * @param source the node that will be deleted and which is the
243  * source of the dangling edges
244  * @param pasteID the node where the dangling edges will be
245  * reconnected to
246  */
247  RuleCnP( const size_t source
248  , const size_t pasteID );
249 
250  /*! Construction
251  * @param source the node that will be deleted and which is the
252  * source of the dangling edges
253  * @param pasteID the node where the dangling edges will be
254  * reconnected to
255  * @param target the target node id of the edges to copy-and-paste
256  */
257  RuleCnP( const size_t source
258  , const size_t pasteID
259  , const size_t target );
260 
261  /*! Construction
262  * @param source the node that will be deleted and which is the
263  * source of the dangling edges
264  * @param pasteID the node where the dangling edges will be
265  * reconnected to
266  * @param edgeLabels the labels of edges that have to be
267  * copy-and-pasted; if the set is empty, all edges will be copied.
268  */
269  RuleCnP( const size_t source
270  , const size_t pasteID
271  , const LabelSet & edgeLabels );
272 
273  /*! Construction
274  * @param source the node that will be deleted and which is the
275  * source of the dangling edges
276  * @param pasteID the node where the dangling edges will be
277  * reconnected to
278  * @param target the target node id of the edges to copy-and-paste
279  * @param edgeLabels the labels of edges that have to be
280  * copy-and-pasted; if the set is empty, all edges will be copied.
281  */
282  RuleCnP( const size_t source
283  , const size_t pasteID
284  , const size_t target
285  , const LabelSet & edgeLabels );
286 
287  //! the node that will be deleted and which is the source of
288  //! the dangling edges
289  size_t source;
290  //! the node where the dangling edges will be reconnected to
291  size_t pasteID;
292  //! optional: the target id of the edges to copy-and-paste
293  size_t target;
294  //! the labels of edges that have to be copy-and-pasted;
295  //! if the set is empty, all edges will be copied.
297 
298  /*!
299  * Order comparison: this object is smaller iff
300  * (this.source < larger.source)
301  * or
302  * (this.source == larger.source and this.target < larger.target).
303  * @param larger the copy-and-paste object to compare to
304  * @return whether this object is of smaller order or not.
305  */
306  bool
307  operator<( const RuleCnP& larger ) const;
308 
309  };
310 
311  //! container definition to store copy-and-Paste operations to do
312  //! the key value defines the source node of the copy-and-Paste
313  typedef std::map< size_t, std::set<RuleCnP> > CopyAndPasteOperations;
314 
315  ///////////////////////////////////////////////////////////////////////
316 
317  //! Construction of a rule based on the given boost graph based core
318  //! description.
319  //! @param core the boost graph that describes the graph grammar rule
320  //! @param id the identifier or description of the graph grammar rule
321  //! @param wildcardToUse the wildcard string to be used for the left
322  //! side pattern matching, or NULL if no wildcard to be applied
323  Rule ( const CoreGraph & core
324  , const std::string& id
325  , const std::string* wildcardToUse = NULL );
326 
327  //! Construction of a rule based on the given boost graph based core
328  //! description and the additional constraints of the left side
329  //! matching.
330  //! @param core the boost graph that describes the graph grammar rule
331  //! @param id the identifier or description of the graph grammar rule
332  //! @param matchConstraints the additional constraints for the
333  //! matching of the left side of the rule
334  //! @param wildcardToUse the wildcard string to be used for the left
335  //! side pattern matching, or NULL if no wildcard to be applied
336  Rule ( const CoreGraph & core
337  , const std::string& id
338  , const MatchConstraintVec & matchConstraints
339  , const std::string* wildcardToUse = NULL );
340 
341  //! Construction of a rule based on the given boost graph based core
342  //! description and the additional constraints of the left side
343  //! matching.
344  //! @param core the boost graph that describes the graph grammar rule
345  //! @param id the identifier or description of the graph grammar rule
346  //! @param matchConstraints the additional constraints for the
347  //! matching of the left side of the rule
348  //! @param copyAndPaste the copy-and-Paste operations to perform
349  //! @param wildcardToUse the wildcard string to be used for the left
350  //! side pattern matching, or NULL if no wildcard to be applied
351  Rule ( const CoreGraph & core
352  , const std::string& id
353  , const MatchConstraintVec & matchConstraints
354  , const CopyAndPasteOperations & copyAndPaste
355  , const std::string* wildcardToUse = NULL );
356 
357  //! Copy construction.
358  //! @param toCopy the Rule to make this a copy of
359  Rule ( const Rule & toCopy );
360 
361  //! Assignment operator
362  //! @param toCopy the Rule to make this a copy of
363  //! @return *this
364  Rule &
365  operator =( const Rule & toCopy );
366 
367  //! Destruction
368  virtual
369  ~Rule();
370 
371 
372  /*!
373  * Access to the core graph that encodes the whole Rule.
374  * @return the Rule's core graph
375  */
376  const CoreGraph&
377  getCore(void) const;
378 
379  protected:
380 
381  //! the information describing the left side pattern of the Rule
383  //! the information describing the right side (result) of the Rule
385  //! the information describing the invariant context of the Rule
387 
388  //! the copy-and-Paste operations to perform
389  CopyAndPasteOperations copyAndPaste;
390 
391  /*! Utility member function to derive the node information
392  * that describes a certain part of the Rule.
393  * @param graph the core graph that contains the whole information
394  * @param level identifies what RuleSide is of interest
395  * @return the vector of node indices of the requested RuleSide
396  */
397  static
398  NodeVec
399  getRuleSideNodes( const CoreGraph &graph,
400  const RuleContext &level );
401 
402  public:
403 
404 
405  /*! Checks whether or not this rule is a consistent one. If not, a
406  * combination of according error codes is given.
407  * @return the value is either C_Consistent, or a product of all other
408  * consistency error codes encountered
409  */
410  virtual
411  size_t
412  isConsistent( void ) const;
413 
414  /*!
415  * Writes a description of the consistency status or errors, encoded
416  * in a consistency code produced by isConsistent*(...), to a given
417  * outstream. The function returns whether or not an error occured.
418  *
419  * @param consistencyCode the error code to parse, produced by
420  * a call to isConsistent(...)
421  * @param errorStream the output stream to write the error decription
422  * to
423  * @param completeCheck if true: tries to decode the whole
424  * consistencyCode and reports an error message if this is
425  * not possible. if false: decodes and reports only the
426  * known error codes from consistencyCode
427  * @return true if no error is encoded; false otherwise
428  */
429  virtual
430  bool
431  decodeConsistencyStatus( const size_t consistencyCode
432  , std::ostream& errorStream
433  , const bool completeCheck = true ) ;
434 
435  /*!
436  * Access to the left side pattern of the rule that has to be matched
437  * when the Rule is applied.
438  * @return the left side pattern
439  */
440  const RuleSide&
441  getLeftSide(void) const;
442 
443  /*!
444  * Access to the right side pattern that is produced after the Rule
445  * was applied.
446  * @return the right side pattern
447  */
448  const RuleSide&
449  getRightSide(void) const;
450 
451  /*!
452  * Access to the context of the Rule that is fixed during the Rule
453  * application and that is present in the left and right side of the
454  * Rule.
455  * @return the context
456  */
457  const RuleSide&
458  getContext(void) const;
459 
460  /*!
461  * Access to the string identifier or description of the Rule.
462  * @return the identifier
463  */
464  const std::string&
465  getID(void) const;
466 
467  /*!
468  * Sets the string identifier or description of the Rule.
469  * @param newID the new identifier to set
470  */
471  void
472  setID( const std::string& newID );
473 
474  /*!
475  * Access to the wildcard string to use for left side pattern matching
476  * @return the wildcard or NULL if no wildcard is to be applied
477  */
478  virtual
479  const std::string*
480  getUsedWildcard(void) const;
481 
482  /*!
483  * Sets the wildcard string to use for left side pattern matching.
484  * To disable wildcard matching set the wildcard to NULL.
485  * @param wildcardToUse the wildcard to be applied or NULL if no
486  * wildcard matching should be done.
487  */
488  virtual
489  void
490  setUsedWildcard( const std::string* const wildcardToUse );
491 
492  /*!
493  * Access to the match constraints to fulfill for each left side match
494  * @return the match constraint container
495  */
496  const MatchConstraintVec &
497  getMatchConstraints( void ) const;
498 
499  /*!
500  * Access to the copy-and-Paste operations to perform
501  * @return the CnP operation container
502  */
503  const CopyAndPasteOperations &
504  getCopyAndPasteOperations( void ) const;
505 
506 
507  protected:
508 
509  /*! Checks the context assumptions of all edges, ie.
510  * - context edges have to connect context nodes,
511  * - left side edges have to connect left or context nodes, and
512  * - right side edges have to connect right or context nodes.
513  *
514  * @param coreGraph the rule core graph to check
515  * @return true if all edges show correct context use; false otherwise
516  */
517  static
518  bool
519  checkEdgeContext( const CoreGraph & coreGraph );
520 
521 
522  };
523 
524 
525 ////////////////////////////////////////////////////////////////////////////////
526 ////////////////////////////////////////////////////////////////////////////////
527 
528 }
529 
530  // implementation
531 #include "ggl/Rule.icc"
532 
533 #endif /*RULE_HH_*/