Generated on Wed Apr 29 2015 11:51:40 for GGL-4.1.2 by doxygen 1.8.3.1
MoleculeComponent_GML_grammar.hh
Go to the documentation of this file.
1 #ifndef GGL_CHEM_MOLECULECOMPONENT_GML_GRAMMAR_HH_
2 #define GGL_CHEM_MOLECULECOMPONENT_GML_GRAMMAR_HH_
3 
4 #include <utility>
5 #include <vector>
6 #include <string>
7 #include <stdexcept>
8 
9 #include "sgm/HashMap.hh"
10 
11 #if HAVE_UNORDERED_MAP > 0
12  #include <unordered_map>
13 #elif HAVE_TR1_UNORDERED_MAP > 0
14  #include <tr1/unordered_map>
15 #elif HAVE_GNU_HASH_MAP > 0
16  #include <ext/hash_map>
17 #else
18  #include <map>
19 #endif
20 
21  // set spirit closure limit if neccessary
22 #if !defined(BOOST_SPIRIT_CLOSURE_LIMIT)
23 #define BOOST_SPIRIT_CLOSURE_LIMIT 5
24 #elif BOOST_SPIRIT_CLOSURE_LIMIT < 5
25 #error "GGL_CHEM_MOLECULECOMPONENT_GML_GRAMMAR : BOOST_SPIRIT_CLOSURE_LIMIT too low, has to be at least 5"
26 #endif
27 
28  // set phoenix limit if neccessary
29 #if !defined(PHOENIX_LIMIT)
30 #define PHOENIX_LIMIT 5
31 #elif PHOENIX_LIMIT < 5
32 #error "GGL_CHEM_MOLECULECOMPONENT_GML_GRAMMAR : PHOENIX_LIMIT too low, has to be at least 5"
33 #endif
34 
35 #include <boost/version.hpp>
36 #if BOOST_VERSION >= 103800
37 #include <boost/spirit/include/classic.hpp>
38 #include <boost/spirit/include/phoenix1.hpp>
39 #include <boost/spirit/include/classic_actor.hpp>
40 #define NS_BOOSTSPIRIT boost::spirit::classic
41 #else
42 #include <boost/spirit.hpp>
43 #include <boost/spirit/phoenix.hpp>
44 #include <boost/spirit/actor.hpp>
45 #define NS_BOOSTSPIRIT boost::spirit
46 #endif
47 
49 #include "sgm/Pattern.hh"
50 #include "ggl/chem/MC_MC_Node.hh"
51 
52 namespace ggl {
53 namespace chem {
54 
55 
56  /*! @brief MoleculeComponent parser
57  *
58  * Parses a GML string representation of a
59  * ggl::MoleculeDecomposition::MoleculeComponent object. This includes its
60  * properties as well as the additional constraints needed for matching.
61  *
62  * Example :
63  *
64  * \verbatim
65  ======= GRAPH IN GML =========================
66 
67  molcomp [
68  description " '-Cl' (attached to a primary carbon with no other clorine atoms attached)"
69  priority 4
70  energy -11.7
71  node [ id 0 label "C" ]
72  node [ id 1 label "Cl" ]
73  edge [ source 0 target 1 label "-" ]
74  constrainAdj [
75  id 0
76  op =
77  count 1
78  nodeLabels [ label "Cl" ]
79  ]
80  constrainAdj [
81  id 0
82  op =
83  count 2
84  nodeLabels [ label "H" ]
85  ]
86  ]
87 
88  ==============================================
89  \endverbatim
90  *
91  * @author Martin Mann (c) 2010 http://www.bioinf.uni-freiburg.de/~mmann/
92  *
93  */
95  : public NS_BOOSTSPIRIT::grammar< MoleculeComponent_GML_grammar >
96  {
97  protected:
98 
99 
100  //! type for mapping integers to size_t
101  typedef
102 #if HAVE_UNORDERED_MAP > 0
103  std::unordered_map<int, size_t>
104 #elif HAVE_TR1_UNORDERED_MAP > 0
105  std::tr1::unordered_map<int, size_t>
106 #else
107  std::map<int, size_t>
108 #endif
110 
111 
112  protected:
113 
114  //! The boost core graph object that is filled to represent the next
115  //! parsed Rule.
117 
118  public:
119 
120  //! Constructs the definitions of a GML graph grammar to parse
121  //! a GML graph string representation and to fill the encoded graph
122  //! into a given boost graph object.
123  //! @param toFill the object to add nodes and edges to
125 
126  //! Parses a GML string and generates a MoleculeComponent::PatternGraph object
127  //! @param GML_string the string to parse
128  //! @return pair.first = the graph encoding of the molecule
129  //! pair.second = -1 if parsing was successfull,
130  //! in error case it returns the string position that caused
131  //! the parsing error
132  //! @throw std::invalid_argument in case a check fails
133  static
134  std::pair< MoleculeComponent, int >
135  parseGML( const std::string & GML_string ) throw (std::invalid_argument);
136 
137 
138  //! The definition of the GML grammar.
139  template <typename ScannerT>
140  struct definition
141  {
142  public:
143 
144  //! Construction of the GML BNF grammar rules to parse a
145  //! MoleculeComponent
146  //! @param self access to the calling grammar
148 
149  //! start parsing
150  NS_BOOSTSPIRIT::rule<ScannerT> const&
151  start() const;
152 
153  protected:
154 
155 
156  //! the molecule component to be filled
158 
159  //! Access to the node label property_map of toFill to set node labels
160  boost::property_map< MoleculeComponent::PatternGraph, PropNodeLabel>::type
162 
163  //! Access to the edge label property_map of g2fill to set edge labels
164  boost::property_map< MoleculeComponent::PatternGraph, PropEdgeLabel>::type
166 
167  //! the mapping of node IDs in the GML notation and their
168  //! corresponding node IDs in the created pattern graph
170 
171 
172  //! the rules to be parsed
173  NS_BOOSTSPIRIT::rule<ScannerT> molcomp, content, node, edge,
175 
176 
177  // temporary data structures
181  // temporary constraint objects
186 
187  enum WhatList {
193  };
194 
195  // helper functions
196 
197  /*!
198  * Resets toFill to enable the detection of missing information
199  * within final_checks()
200  */
201  void clear_toFill(void);
202 
203  /*!
204  * Performs final checks on toFill to ensure that the parse was
205  * correct
206  *
207  * @throw std::invalid_argument in case a check fails
208  */
209  void final_checks(void) throw (std::invalid_argument);
210 
211  /*!
212  * Stores a node of the MoleculeComponent pattern graph.
213  * @throw std::invalid_argument in case a check fails
214  */
215  void store_node(void) throw (std::invalid_argument);
216 
217  /*!
218  * Stores an edge of the MoleculeComponent pattern graph.
219  * @throw std::invalid_argument in case a check fails
220  */
221  void store_edge(void) throw (std::invalid_argument);
222 
223  /*!
224  * Stores a node adjacency constraint of the MoleculeComponent
225  */
226  void store_constrAdj(void);
227 
228  /*!
229  * Stores a node label constraint of the MoleculeComponent
230  */
231  void store_constrLabel(void);
232 
233  /*!
234  * Inserts a string into a given string set
235  * @param list the encoding which list to be fill
236  */
237  void insert_to_list( WhatList list );
238 
239  }; // end of description
240 
241 
242  }; // end of MoleculeComponent_GML_grammar
243 
244 
245 } // namespace chem
246 } // namespace ggl
247 
248 // include implementation
249 #include "ggl/chem/MoleculeComponent_GML_grammar.icc"
250 
251 #endif /*GGL_CHEM_MOLECULECOMPONENT_GML_GRAMMAR_HH_*/