Fawkes API Fawkes Development Version
pddl_semantics.h
1/***************************************************************************
2 * pddl_semantics.h
3 *
4 * Created: Thursday 15 October 2020
5 * Copyright 2020 Tarik Viehmann
6 ****************************************************************************/
7
8/* This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Library General Public License for more details.
17 *
18 * Read the full text in the LICENSE.GPL file in the doc directory.
19 */
20
21#ifndef PLUGINS_PDDL_SEMANTICS_H
22#define PLUGINS_PDDL_SEMANTICS_H
23
24#include "pddl_ast.h"
25
26#include <typeindex>
27
28namespace pddl_parser {
29
30/** @class ExpressionTypeVisitor
31 * Retrieve the type index of an expression_t expression to determine the
32 * underlying type of the variant.
33 */
34struct ExpressionTypeVisitor : public boost::static_visitor<std::type_index>
35{
36 /** Visitor for Atom.
37 * @param a Atom.
38 * @return Type index of struct Atom.
39 */
40 std::type_index
41 operator()(const Atom &a) const
42 {
43 return std::type_index(typeid(a));
44 }
45 /** Visitor for Predicate.
46 * @param p Predicate.
47 * @return Type index of struct Predicate.
48 */
49 std::type_index
50 operator()(const Predicate &p) const
51 {
52 return std::type_index(typeid(p));
53 }
54 /** Visitor for QuantifiedFormula.
55 * @param p Quantified formula.
56 * @return Type index of struct QuantifiedFormula.
57 */
58 std::type_index
60 {
61 return std::type_index(typeid(p));
62 }
63};
64
65/** @class TypeSemantics
66 * Functor for semantic checks when parsing PDDL types.
67 */
69{
70 /**
71 * Throw an exception if the parsed type is a sub-type but the domain does not
72 * have the requirement :typing enabled.
73 * @param where Position of the parsed type in the string to parse.
74 * @param parsed Type that got parsed.
75 * @param domain Partial domain containing everything that was parsed so far
76 * @return the parsed type.
77 */
78 pair_type
79 operator()(const iterator_type &where, const pair_type &parsed, const Domain &domain) const;
80};
81
82/** @class ParamTransformer
83 * Functor to uniformly handle disjunctive types and shorthand notations.
84 */
86{
87 /**
88 * Transform a pair of string vectors to pairs of strings.
89 *
90 * Parameters may be given in a form '?a ?b - (either x y)', which is parsed
91 * as <[a, b],[x,y]>. The Transformation creates <a,x> <b,x>, <a,y> and
92 * <b,y> out of this.
93 *
94 * @param where Position of the parsed param type in the string to parse.
95 * @param parsed Parameters that got parsed.
96 * @param target The vector that is extended by all the constructed pairs.
97 * @return The last created param tuple, this is not added to target in this
98 * function but rather is added through the semantic action when
99 * parsing.
100 */
101 pair_type operator()(const iterator_type & where,
102 const pair_strings_type &parsed,
103 string_pairs_type & target) const;
104};
105
106/** @class ConstantSemantics
107 * Functor for semantic checks when parsing constants of a PDDL domain.
108 */
110{
111 /**
112 * Check whether the given type for a set of constants is defined and
113 * registers warnings if constants are defined multiple times with conflicting
114 * types.
115 *
116 * @param where Position of the parsed constants.
117 * @param parsed Constants that got parsed.
118 * @param domain Partial domain containing everything that was parsed so far
119 * @param warnings Hook from the parser where non-fatal warnings can be stored
120 * @return the parsed constants.
121 */
122 pair_multi_const operator()(const iterator_type & where,
123 const pair_multi_const & parsed,
124 const Domain & domain,
125 std::vector<std::string> &warnings) const;
126};
127
128/** @class ActionSemantics
129 * Functor for semantic checks when parsing actions of a PDDL domain.
130 */
132{
133 /**
134 * Helper to check whether a type matches the expected one.
135 *
136 * Recursively steps up the type hierarchy until the expected type is found or
137 * the topmost level is reached.
138 *
139 * @param where Position of the parsed action.
140 * @param got Type that has to be checked.
141 * @param expected Type that is to be matched.
142 * @param domain partial domain containing everything that was parsed so far
143 * @return true iff got is a sub-type of/the same type as expected.
144 */
145 static bool check_type(const iterator_type &where,
146 const std::string & got,
147 const std::string & expected,
148 const Domain & domain);
149
150 /**
151 * Helper to recursively check expression semantics within precondition and
152 * effects of actions.
153 *
154 * @param where Position of the parsed action.
155 * @param expr Expression to be checked.
156 * @param domain Partial domain containing everything that was parsed so far.
157 * @param action Action containing the expression to check.
158 * @param bound_vars Variables that are bound through quantified formulas on
159 * an upper recursion level.
160 * */
161 static void check_action_condition(const iterator_type &where,
162 const Expression & expr,
163 const Domain & domain,
164 const Action & action,
165 string_pairs_type & bound_vars);
166 /**
167 * Helper to recursively check expression semantics within predicates.
168 *
169 * @param where Position of the parsed action.
170 * @param pred Predicate to be checked.
171 * @param type Expression type of the predicate.
172 * @param domain Partial domain containing everything that was parsed so far.
173 * @param action Action containing the expression to check.
174 * @param bound_vars Variables that are bound through quantified formulas on
175 * an upper recursion level.
176 * */
177 static void check_action_predicate(const iterator_type & where,
178 const Predicate & pred,
179 const ExpressionType &type,
180 const Domain & domain,
181 const Action & action,
182 string_pairs_type & bound_vars);
183
184 /**
185 * Check whether the parameter list is properly typed and all expressions
186 * that express conditions/effects are well-formed in the domain.
187 *
188 * @param where Position of the parsed action.
189 * @param parsed Action that got parsed.
190 * @param domain Partial domain containing everything that was parsed so far.
191 * @return parsed Action.
192 */
193 Action operator()(const iterator_type &where, const Action &parsed, const Domain &domain) const;
194};
195
196} // namespace pddl_parser
197
198#endif /* PLUGINS_PDDL_SEMANTICS_H */
Functor for semantic checks when parsing actions of a PDDL domain.
static bool check_type(const iterator_type &where, const std::string &got, const std::string &expected, const Domain &domain)
Helper to check whether a type matches the expected one.
Action operator()(const iterator_type &where, const Action &parsed, const Domain &domain) const
Check whether the parameter list is properly typed and all expressions that express conditions/effect...
static void check_action_condition(const iterator_type &where, const Expression &expr, const Domain &domain, const Action &action, string_pairs_type &bound_vars)
Helper to recursively check expression semantics within precondition and effects of actions.
static void check_action_predicate(const iterator_type &where, const Predicate &pred, const ExpressionType &type, const Domain &domain, const Action &action, string_pairs_type &bound_vars)
Helper to recursively check expression semantics within predicates.
A structured representation of a PDDL action.
Definition: pddl_ast.h:133
Functor for semantic checks when parsing constants of a PDDL domain.
pair_multi_const operator()(const iterator_type &where, const pair_multi_const &parsed, const Domain &domain, std::vector< std::string > &warnings) const
Check whether the given type for a set of constants is defined and registers warnings if constants ar...
A structured representation of a PDDL domain.
Definition: pddl_ast.h:157
Retrieve the type index of an expression_t expression to determine the underlying type of the variant...
std::type_index operator()(const Predicate &p) const
Visitor for Predicate.
std::type_index operator()(const QuantifiedFormula &p) const
Visitor for QuantifiedFormula.
std::type_index operator()(const Atom &a) const
Visitor for Atom.
A PDDL Expression.
Definition: pddl_ast.h:78
Functor to uniformly handle disjunctive types and shorthand notations.
pair_type operator()(const iterator_type &where, const pair_strings_type &parsed, string_pairs_type &target) const
Transform a pair of string vectors to pairs of strings.
A PDDL formula (either part of a precondition or an effect(.
Definition: pddl_ast.h:107
A PDDL quantified formula.
Definition: pddl_ast.h:89
Functor for semantic checks when parsing PDDL types.
pair_type operator()(const iterator_type &where, const pair_type &parsed, const Domain &domain) const
Throw an exception if the parsed type is a sub-type but the domain does not have the requirement :typ...