pddl_planner
Expression.hpp
Go to the documentation of this file.
1 #ifndef PDDL_PLANNER_REPRESENTATION_GRAMMAR_LISP_EXPRESSION_HPP
2 #define PDDL_PLANNER_REPRESENTATION_GRAMMAR_LISP_EXPRESSION_HPP
3 
4 #include <boost/version.hpp>
5 #include <boost/spirit/include/qi.hpp>
6 #include <boost/spirit/include/phoenix_operator.hpp>
7 #include <boost/spirit/include/phoenix_fusion.hpp>
8 #include <boost/spirit/include/phoenix_stl.hpp>
9 #include <boost/fusion/include/adapt_struct.hpp>
10 
11 //#define BOOST_SPIRIT_DEBUG
12 #ifdef BOOST_SPIRIT_DEBUG
13 // include stream operators
14 #include <pddl_planner/representation/grammar/Debug.hpp>
15 #warning "SPIRIT DEBUGGING ENABLED"
16 
17 #define GRAMMAR_DEBUG_RULE(X) { using boost::spirit::qi::debug; X.name(#X); debug(X); }
18 #else
19 #define GRAMMAR_DEBUG_RULE(X)
20 #endif
21 
22 #include <boost/fusion/include/adapt_struct.hpp>
23 
24 // BOOST_FUSION_ADAPT_CLASS has been renamed to BOOST_FUSION_ADAPT_ADT for boost version > 104500
25 #if BOOST_VERSION < 104500
26 #warning "BOOST_VERSION < 104500"
27 #include <boost/fusion/include/adapt_class.hpp>
28 #else
29 #include <boost/fusion/adapted/adt/adapt_adt.hpp>
30 #include <boost/fusion/include/adapt_adt.hpp>
31 #endif
32 
33 #if BOOST_VERSION < 104500
34 #define PDDL_ACL_FUSION_ADAPT BOOST_FUSION_ADAPT_CLASS
35 #else
36 #define PDDL_FUSION_ADAPT BOOST_FUSION_ADAPT_ADT
37 #endif
38 
39 #include <pddl_planner/representation/Problem.hpp>
40 #include <base-logging/Logging.hpp>
41 
42 BOOST_FUSION_ADAPT_STRUCT(
47 )
48 
49 BOOST_FUSION_ADAPT_STRUCT(
53 )
54 
55 namespace qi = boost::spirit::qi;
56 namespace label = qi::labels;
57 namespace fusion = boost::fusion;
58 namespace phoenix = boost::phoenix;
59 namespace spirit = boost::spirit;
60 namespace encoding = boost::spirit::ascii;
61 
62 namespace pddl_planner {
63 namespace representation {
64 namespace grammar {
74 namespace lisp {
75 
77 {
78  template <typename E, typename P>
79  struct result
80  {
81  typedef void type;
82  };
83  template<typename E, typename P>
84  void operator()(E& expression, P parameter) const
85  {
86  expression.addParameter(parameter);
87  }
88 };
89 extern phoenix::function<addExpressionParameterImpl> addExpressionParameter;
90 
91 
92 template<typename Iterator>
93 struct Quantifier : qi::grammar<Iterator, std::string()>
94 {
95  Quantifier() : Quantifier::base_type(quantifier_rule, "Quantifier-lisp_grammar")
96  {
97  quantifier_rule = encoding::string("forall") | encoding::string("exists");
98  GRAMMAR_DEBUG_RULE(quantifier_rule);
99  }
100 
101  qi::rule<Iterator, std::string()> quantifier_rule;
102 };
103 
104 template<typename Iterator>
105 struct MultiOperator : qi::grammar<Iterator, std::string()>
106 {
107  MultiOperator() : MultiOperator::base_type(multi_operator_rule, "MultiOperator-lisp_grammar")
108  {
109  multi_operator_rule = encoding::char_("*+");
110  GRAMMAR_DEBUG_RULE(multi_operator_rule);
111  }
112 
113  qi::rule<Iterator, std::string()> multi_operator_rule;
114 };
115 
116 template<typename Iterator>
117 struct BinaryOperator : qi::grammar<Iterator, std::string()>
118 {
119  BinaryOperator() : BinaryOperator::base_type(binary_operator_rule, "BinaryOperator-lisp_grammar")
120  {
121  binary_operator_rule = encoding::char_("-/") | multi_operator;
122  GRAMMAR_DEBUG_RULE(binary_operator_rule);
123  }
124 
125  qi::rule<Iterator, std::string()> binary_operator_rule;
127 };
128 
129 template<typename Iterator>
130 struct BinaryComparison : qi::grammar<Iterator, std::string()>
131 {
132  BinaryComparison() : BinaryComparison::base_type(binary_comparison_rule, "BinaryComparison-lisp_grammar")
133  {
134  binary_comparison_rule = encoding::char_("><=")
135  | encoding::string(">=")
136  | encoding::string("<=");
137 
138  GRAMMAR_DEBUG_RULE(binary_comparison_rule);
139  }
140 
141  qi::rule<Iterator, std::string()> binary_comparison_rule;
142 };
143 
144 
145 template<typename Iterator>
146 struct Label : qi::grammar<Iterator, std::string()>
147 {
148  Label() : Label::base_type(label_rule, "Label-lisp_grammar")
149  {
150  label_rule = (qi::alnum >> +(qi::alnum | encoding::char_("+-_[]"))) | binary_operator | binary_comparison;
151  GRAMMAR_DEBUG_RULE(label_rule);
152  }
153 
154  qi::rule<Iterator, std::string()> label_rule;
157 };
158 
159 
160 
161 template<typename Iterator>
162 struct Variable : qi::grammar<Iterator, std::string()>
163 {
164  Variable() : Variable::base_type(variable_rule, "Variable-lisp_grammar")
165  {
166  variable_rule = encoding::string("?") >> +qi::alnum;
167  GRAMMAR_DEBUG_RULE(variable_rule);
168  }
169 
170  qi::rule<Iterator, std::string()> variable_rule;
171 };
172 
173 template<typename Iterator, typename Skipper = qi::space_type>
174 struct TypedItem : qi::grammar<Iterator, pddl_planner::representation::TypedItem(), Skipper>
175 {
176  TypedItem() : TypedItem::base_type(typed_item_rule, "TypedItem-lisp_grammar")
177  {
178  typed_item_rule = (label | variable) [ phoenix::at_c<0>(label::_val) = label::_1 ]
179  >> *( spirit::lit("-")
180  >> label [ phoenix::at_c<1>(label::_val) = label::_1 ]
181  )
182  ;
183 
184  GRAMMAR_DEBUG_RULE(typed_item_rule)
185  }
186 
187  qi::rule<Iterator, pddl_planner::representation::TypedItem(), Skipper> typed_item_rule;
190 
191 };
192 
193 template<typename Iterator, typename Skipper = qi::space_type>
194 struct Expression : qi::grammar<Iterator, pddl_planner::representation::Expression(), Skipper>
195 {
196  Expression() : Expression::base_type(expression_rule, "Expression-lisp_grammar")
197  {
198  expression_rule = quantifier_expression | general_expression;
199  general_expression = "("
200  >> label [ phoenix::at_c<0>(label::_val) = label::_1 ]
201  >> *(expression | simple_expression) [ addExpressionParameter(label::_val, label::_1) ]
202  >> ")";
203 
204  simple_expression = ( "("
205  >> label [ phoenix::at_c<0>(label::_val) = label::_1 ]
206  >> *(label | variable) [ addExpressionParameter(label::_val, label::_1) ]
207  >> ")"
208  ) | ( variable | label) [ phoenix::at_c<0>(label::_val) = label::_1 ]
209  ;
210 
211  quantifier_expression = spirit::lit("(")
212  >> quantifier [ phoenix::at_c<0>(label::_val) = label::_1 ]
213  >> spirit::lit("(")
214  >> typedItem [ label::_a = label::_1 ]
215  >> spirit::lit(")")
216  >> general_expression [ addExpressionParameter(label::_val, label::_1) ]
217  >> spirit::lit(")") [ phoenix::at_c<2>(label::_val) = label::_a];
218 
219 
220  expression = general_expression.alias();
221 
222  GRAMMAR_DEBUG_RULE(expression_rule);
223  GRAMMAR_DEBUG_RULE(general_expression);
224  GRAMMAR_DEBUG_RULE(simple_expression);
225  GRAMMAR_DEBUG_RULE(general_expression);
226  GRAMMAR_DEBUG_RULE(simple_expression);
227  }
228 
229  qi::rule<Iterator, pddl_planner::representation::Expression(), Skipper> expression_rule;
234 
235  qi::rule<Iterator, pddl_planner::representation::Expression(), Skipper> general_expression;
236  qi::rule<Iterator, pddl_planner::representation::Expression(), Skipper> expression;
237  qi::rule<Iterator, pddl_planner::representation::Expression(), Skipper> simple_expression;
238  qi::rule<Iterator, pddl_planner::representation::Expression(), qi::locals< pddl_planner::representation::TypedItem>, Skipper > quantifier_expression;
239 };
240 
241 template<typename Iterator, typename Skipper = qi::space_type>
242 struct ExpressionList : qi::grammar<Iterator, pddl_planner::representation::ExpressionList(), Skipper>
243 {
244  ExpressionList() : ExpressionList::base_type(expression_list_rule, "ExpressionList-lisp_grammar")
245  {
246  expression_list_rule = +expression [ phoenix::push_back(label::_val, label::_1) ];
247  GRAMMAR_DEBUG_RULE(expression_list_rule);
248  }
249 
250  qi::rule<Iterator, ExpressionList(), Skipper> expression_list_rule;
252 };
253 
254 } // end namespace lips
255 } // end namespace grammar
256 } // end namespace representation
257 } // end namespace pddl_planner
258 #endif // PDDL_PLANNER_REPRESENTATION_GRAMMAR_LISP_EXPRESSION_HPP
BinaryComparison< Iterator > binary_comparison
Definition: Expression.hpp:156
qi::rule< Iterator, std::string()> multi_operator_rule
Definition: Expression.hpp:113
Variable< Iterator > variable
Definition: Expression.hpp:231
Representation of (LISP) expressions.
Definition: Domain.hpp:274
qi::rule< Iterator, std::string()> label_rule
Definition: Expression.hpp:154
Quantifier< Iterator > quantifier
Definition: Expression.hpp:233
qi::rule< Iterator, pddl_planner::representation::Expression(), Skipper > expression
Definition: Expression.hpp:236
qi::rule< Iterator, ExpressionList(), Skipper > expression_list_rule
Definition: Expression.hpp:250
A tuple representation providing a name/label and a type.
Definition: Domain.hpp:28
std::string Type
Definition: Domain.hpp:18
std::vector< Expression * > ExpressionPtrList
Definition: Domain.hpp:267
Label< Iterator > label
Definition: Expression.hpp:230
void operator()(E &expression, P parameter) const
Definition: Expression.hpp:84
qi::rule< Iterator, pddl_planner::representation::Expression(), qi::locals< pddl_planner::representation::TypedItem >, Skipper > quantifier_expression
Definition: Expression.hpp:238
MultiOperator< Iterator > multi_operator
Definition: Expression.hpp:126
Label< Iterator > label
Definition: Expression.hpp:189
qi::rule< Iterator, pddl_planner::representation::Expression(), Skipper > expression_rule
Definition: Expression.hpp:229
Definition: PDDLPlannerInterface.cpp:18
#define GRAMMAR_DEBUG_RULE(X)
Definition: Expression.hpp:19
qi::rule< Iterator, pddl_planner::representation::TypedItem(), Skipper > typed_item_rule
Definition: Expression.hpp:187
qi::rule< Iterator, std::string()> quantifier_rule
Definition: Expression.hpp:101
phoenix::function< addExpressionParameterImpl > addExpressionParameter
Definition: Expression.cpp:8
qi::rule< Iterator, pddl_planner::representation::Expression(), Skipper > simple_expression
Definition: Expression.hpp:237
TypedItem< Iterator, Skipper > typedItem
Definition: Expression.hpp:232
qi::rule< Iterator, std::string()> binary_operator_rule
Definition: Expression.hpp:125
qi::rule< Iterator, std::string()> binary_comparison_rule
Definition: Expression.hpp:141
Expression< Iterator, Skipper > expression
Definition: Expression.hpp:251
BinaryOperator< Iterator > binary_operator
Definition: Expression.hpp:155
qi::rule< Iterator, pddl_planner::representation::Expression(), Skipper > general_expression
Definition: Expression.hpp:235
std::string Label
Definition: Domain.hpp:16
qi::rule< Iterator, std::string()> variable_rule
Definition: Expression.hpp:170
Variable< Iterator > variable
Definition: Expression.hpp:188