pddl_planner
Domain.hpp
Go to the documentation of this file.
1 #ifndef PDDL_PLANNER_REPRESENTATION_DOMAIN
2 #define PDDL_PLANNER_REPRESENTATION_DOMAIN
3 
4 #include <string>
5 #include <map>
6 #include <vector>
7 #include <stdint.h>
8 #include <limits>
9 #include <stdexcept>
10 #include <cstdarg>
11 #include <stack>
12 
13 namespace pddl_planner {
14 namespace representation {
15 
16 typedef std::string Label;
17 typedef Label Operator;
18 typedef std::string Type;
19 typedef std::string Requirement;
20 
21 typedef std::vector<Type> TypeList;
22 typedef std::vector<Requirement> RequirementList;
23 
28 struct TypedItem
29 {
30  Label label;
31  Type type;
32 
34  {}
35 
36  TypedItem(const Label& label, const Type& type)
37  : label(label)
38  , type(type)
39  {}
40 
41  bool undefined() const { return label.empty() || type.empty(); }
42 
43  bool operator!=(const TypedItem& other) const { return ! operator==(other); }
44  bool operator==(const TypedItem& other) const { return label == other.label && type == other.type; }
45 
46 };
47 
49 
50 typedef std::vector<TypedItem> TypedItemList;
51 typedef TypedItemList ConstantList;
52 typedef TypedItemList ArgumentList;
53 typedef TypedItemList ParameterList;
54 
55 
61 {
62  std::vector<std::string> mKnownVariables;
63  std::vector<Label> mOperatorStack;
64 
65 public:
66  VariableManager(const ArgumentList& arguments = ArgumentList());
67 
68  void push(const Label& label);
69  Label pop();
70 
75  std::vector<Label> getOperatorStack() const { return mOperatorStack; }
76 
81  std::string getOperatorStackAsString() const;
82 
83  /*
84  * Create a variable name, i.e. a string prefixed with ?
85  * \return canonized string of given name
86  */
87  static std::string canonize(const std::string& name);
88 
89  /*
90  * Test if the given name indicates a variable (currently indicated by starting with ?)
91  * \return true if name is a variable, false otherwise
92  */
93  static bool isVariable(const std::string& name);
94 
99  void registerVariable(const std::string& name);
100 
106  bool isKnownVariable(const std::string& name) const;
107 
113  bool hasTypedVariable(const TypedItemList& list, const TypedItem& item) const;
114 
119  static void addTypedVariable(TypedItemList& list, const TypedItem& item);
120 };
121 
127 struct Predicate
128 {
129  Label label;
130  ArgumentList arguments;
131 
132  Predicate(const Label& label, const ArgumentList& argumentList)
133  : label(label)
134  , arguments(argumentList)
135  {}
136 
137  Predicate(const Label& label, const TypedItem& arg0 = TypedItem(), const TypedItem& arg1 = TypedItem(), const TypedItem& arg2 = TypedItem())
138  : label(label)
139  {
140  if(!arg0.undefined())
141  {
142  VariableManager::addTypedVariable(arguments, arg0);
143  }
144 
145  if(!arg1.undefined())
146  {
147  VariableManager::addTypedVariable(arguments, arg1);
148  }
149 
150  if(!arg2.undefined())
151  {
152  VariableManager::addTypedVariable(arguments, arg2);
153  }
154 
155  }
156 };
157 typedef std::vector<Predicate> PredicateList;
158 
159 // Function has same structure as a predicate
161 typedef std::vector<Function> FunctionList;
162 
167 class Arity
168 {
169  uint8_t mMin;
170  uint8_t mMax;
171 
172 public:
177  : mMin( std::numeric_limits<uint8_t>::min() )
178  , mMax( std::numeric_limits<uint8_t>::max() )
179  {}
180 
181  Arity(uint8_t min, uint8_t max)
182  : mMin(min)
183  , mMax(max)
184  {
185  if(mMin > mMax)
186  {
187  throw std::invalid_argument("pddl_planner::representation::Arity: min arity greater than max");
188  }
189  }
190 
195  uint8_t getMin() const { return mMin; }
196 
201  uint8_t getMax() const { return mMax; }
202 
208  static Arity exact(uint8_t n) { return Arity(n,n); }
209 
215  static Arity min(uint8_t n) { return Arity(n,std::numeric_limits<uint8_t>::max()); }
216 
222  static Arity max(uint8_t n) { return Arity(std::numeric_limits<uint8_t>::min(),n); }
223 };
224 
225 typedef std::map<Label, Arity> ArityMap;
226 
232 {
233  ArityMap mArityMap;
234 
238  void addDefaults();
239 
240 public:
241  ArityValidator(const PredicateList& predicates = PredicateList());
242 
247  bool isOperator(const Label& label) const;
248 
253  bool isQuantifier(const Label& label) const;
254 
260  void validate(const Label& label, uint8_t arity);
261 };
262 
263 
265 extern std::map<Quantor,std::string> QuantorTxt;
266 
268 typedef std::vector<Expression*> ExpressionPtrList;
275 {
276  Label label;
277  ExpressionPtrList parameters;
278 
279  // For quantor expression
281 
282  Expression(const Label& label = "");
283  Expression(const Expression& other);
284  virtual ~Expression();
285 
286  Expression(const Label& label, const Expression& arg0, const Expression& arg1 = Expression(), const Expression& arg2 = Expression(), const Expression& arg3 = Expression(), const Expression& arg4 = Expression(), const Expression& arg5 = Expression(), const Expression& arg6 = Expression(), const Expression& arg7 = Expression(), const Expression& arg8 = Expression(), const Expression& arg9 = Expression(), const Expression& arg10 = Expression());
287 
288  Expression(const Label& label, const Label& arg0, const Label& arg1 = "", const Label& arg2 = "");
289 
290  Expression(Quantor quantor, const TypedItem& typedItem, const Expression& e);
291 
295  void addParameter(const Label& e);
296  void addParameter(const Expression& e);
297 
298  bool isAtomic() const { return parameters.empty(); }
299 
300  bool isNull() const { return label.empty(); }
301 
302  static bool isQuantor(const Label& label);
303 
307  Expression& operator=(const Expression& other);
308 
313  bool operator==(const Expression& other) const;
314 
319  std::string toLISP() const;
320 
325  static Expression fromString(const std::string& expressionString);
326 };
327 
328 typedef std::vector<Expression> ExpressionList;
329 
334 struct Action
335 {
336  Label label;
337  ArgumentList arguments;
338  ExpressionList preconditions;
339  ExpressionList effects;
340 
346  Action(const Label& label, const ArgumentList& arguments)
347  : label(label)
348  , arguments(arguments)
349  {}
350 
359  Action(const Label& label, const TypedItem& arg0 = TypedItem(), const TypedItem& arg1 = TypedItem(), const TypedItem& arg2 = TypedItem(), const TypedItem& arg3 = TypedItem())
360  : label(label)
361  {
362  if(!arg0.undefined())
363  {
364  addArgument(arg0);
365  }
366 
367  if(!arg1.undefined())
368  {
369  addArgument(arg1);
370  }
371 
372  if(!arg2.undefined())
373  {
374  addArgument(arg2);
375  }
376 
377  if(!arg3.undefined())
378  {
379  addArgument(arg3);
380  }
381  }
382 
388  {
389  preconditions.push_back(e);
390  }
391 
396  void addEffect(const Expression& e)
397  {
398  effects.push_back(e);
399  }
400 
406  void addArgument(const TypedItem& arg);
407 
412  bool isArgument(const Label& label);
413 };
414 typedef std::vector<Action> ActionList;
415 
423 struct Domain
424 {
425  // Domain name
426  std::string name;
427  // List of types of this domain
428  TypeList types;
429  // Map a type to a parent type if there is any
430  std::map<Type,Type> type2parent;
431  // List of constants in this domain
432  ConstantList constants;
433  // List of predicates in this domain
434  PredicateList predicates;
435  // List of requirements for using this domain description, e.g. 'strips' or 'typing'
436  RequirementList requirements;
437  // List of actions in this domain
438  ActionList actions;
439  // List of functions in this domain
440  FunctionList functions;
441 
446  Domain(const std::string& name = "");
447 
452  void addType(const Type& type, const Type& parentType = "");
453 
454  void addConstant(const TypedItem& type, bool overwrite = false);
455  void addPredicate(const Predicate& predicate, bool overwrite = false);
456  void addRequirement(const Requirement& requirement);
457  void addAction(const Action& action, bool overwrite = false);
458  void addFunction(const Function& function, bool overwrite = false);
459 
460  void removeConstant(const Label& label);
461  void removePredicate(const Label& label);
462  void removeAction(const Label& label);
463  void removeFunction(const Label& label);
464 
465  bool isType(const Type& type) const;
466  bool isConstant(const Label& label) const;
467  bool isPredicate(const Label& label) const;
468  bool isRequirement(const Requirement& requirement) const;
469  bool isAction(const Label& label) const;
470  bool isFunction(const Label& label) const;
471 
472  Predicate getPredicate(const Label& label) const;
473  Action getAction(const Label& label) const;
474 
475  std::string toLISP() const;
476 
481  bool hasName() const { return !name.empty(); }
482 
487  bool isNull() const { return !hasName(); }
488 
493  void validate(const Expression& e, const VariableManager& variableManager = VariableManager()) const;
494 
499  void validate() const;
500 };
501 
502 } // end namespace representation
503 } // end namespace pddl_planner
504 
505 #endif // PDDL_PLANNER_REPRESENTATION_DOMAIN
TypeList types
Definition: Domain.hpp:428
Label label
Definition: Domain.hpp:276
TypedItem typedItem
Definition: Domain.hpp:280
ConstantList constants
Definition: Domain.hpp:432
Manage variable, i.e. for PDDL description these variables start with a quotation mark...
Definition: Domain.hpp:60
Action(const Label &label, const ArgumentList &arguments)
Definition: Domain.hpp:346
std::vector< Type > TypeList
Definition: Domain.hpp:21
Type type
Definition: Domain.hpp:31
Predicate(const Label &label, const TypedItem &arg0=TypedItem(), const TypedItem &arg1=TypedItem(), const TypedItem &arg2=TypedItem())
Definition: Domain.hpp:137
Label label
Definition: Domain.hpp:129
FunctionList functions
Definition: Domain.hpp:440
TypedItemList ConstantList
Definition: Domain.hpp:51
std::map< Type, Type > type2parent
Definition: Domain.hpp:430
bool isNull() const
Definition: Domain.hpp:487
ArgumentList arguments
Definition: Domain.hpp:130
std::vector< Expression > ExpressionList
Definition: Domain.hpp:328
Representation of a predicate.
Definition: Domain.hpp:127
bool isAtomic() const
Definition: Domain.hpp:298
Representation of (LISP) expressions.
Definition: Domain.hpp:274
Action(const Label &label, const TypedItem &arg0=TypedItem(), const TypedItem &arg1=TypedItem(), const TypedItem &arg2=TypedItem(), const TypedItem &arg3=TypedItem())
Definition: Domain.hpp:359
std::vector< Label > getOperatorStack() const
Definition: Domain.hpp:75
bool operator==(const TypedItem &other) const
Definition: Domain.hpp:44
uint8_t getMax() const
Definition: Domain.hpp:201
PredicateList predicates
Definition: Domain.hpp:434
Definition: Debug.hpp:10
ArgumentList arguments
Definition: Domain.hpp:337
std::map< Label, Arity > ArityMap
Definition: Domain.hpp:225
void addEffect(const Expression &e)
Definition: Domain.hpp:396
TypedItem()
Definition: Domain.hpp:33
A tuple representation providing a name/label and a type.
Definition: Domain.hpp:28
std::string Requirement
Definition: Domain.hpp:19
Class to validate usage of operations against argument requirements.
Definition: Domain.hpp:231
std::string Type
Definition: Domain.hpp:18
static void addTypedVariable(TypedItemList &list, const TypedItem &item)
Definition: Domain.cpp:347
Arity represents the arity of operations allowing to define exact, minimum and maximum arity...
Definition: Domain.hpp:167
std::vector< Expression * > ExpressionPtrList
Definition: Domain.hpp:267
ExpressionList preconditions
Definition: Domain.hpp:338
TypedItemList ArgumentList
Definition: Domain.hpp:52
An internal representation of a PDDL domain description.
Definition: Domain.hpp:423
std::vector< Function > FunctionList
Definition: Domain.hpp:161
Definition: Domain.hpp:264
void addPrecondition(const Expression &e)
Definition: Domain.hpp:387
ExpressionList effects
Definition: Domain.hpp:339
std::vector< Action > ActionList
Definition: Domain.hpp:414
bool isNull() const
Definition: Domain.hpp:300
bool operator!=(const TypedItem &other) const
Definition: Domain.hpp:43
std::vector< TypedItem > TypedItemList
Definition: Domain.hpp:50
TypedItemList ParameterList
Definition: Domain.hpp:53
Arity(uint8_t min, uint8_t max)
Definition: Domain.hpp:181
static Arity exact(uint8_t n)
Definition: Domain.hpp:208
Label Operator
Definition: Domain.hpp:17
Arity()
Definition: Domain.hpp:176
Definition: PDDLPlannerInterface.cpp:18
RequirementList requirements
Definition: Domain.hpp:436
bool hasName() const
Definition: Domain.hpp:481
Quantor
Definition: Domain.hpp:264
std::vector< Predicate > PredicateList
Definition: Domain.hpp:157
ExpressionPtrList parameters
Definition: Domain.hpp:277
Predicate(const Label &label, const ArgumentList &argumentList)
Definition: Domain.hpp:132
uint8_t getMin() const
Definition: Domain.hpp:195
std::map< Quantor, std::string > QuantorTxt
Definition: Domain.cpp:147
static Arity min(uint8_t n)
Definition: Domain.hpp:215
static Arity max(uint8_t n)
Definition: Domain.hpp:222
bool undefined() const
Definition: Domain.hpp:41
Definition: Domain.hpp:264
ActionList actions
Definition: Domain.hpp:438
std::vector< Requirement > RequirementList
Definition: Domain.hpp:22
TypedItem Constant
Definition: Domain.hpp:48
std::string Label
Definition: Domain.hpp:16
std::string name
Definition: Domain.hpp:426
Label label
Definition: Domain.hpp:336
Label label
Definition: Domain.hpp:30
Predicate Function
Definition: Domain.hpp:160
TypedItem(const Label &label, const Type &type)
Definition: Domain.hpp:36
Definition: Domain.hpp:334