symbolic
proposition.h
1 
10 #ifndef SYMBOLIC_PROPOSITION_H_
11 #define SYMBOLIC_PROPOSITION_H_
12 
13 #include <functional> // std::equal_to, std::hash
14 #include <ostream> // std::ostream
15 #include <string> // std::string
16 #include <utility> // std::tie
17 #include <vector> // std::vector
18 
19 #include "symbolic/object.h"
20 
21 namespace symbolic {
22 
23 class Pddl;
24 
26  public:
27  virtual const std::string& name() const = 0;
28 
29  virtual const std::vector<Object>& arguments() const = 0;
30 
31  virtual std::string to_string() const;
32  virtual std::string to_pddl() const;
33 
34  size_t hash() const { return hash_; }
35 
36  static std::string ParseHead(const std::string& atom) {
37  return atom.substr(0, atom.find_first_of('('));
38  }
39 
40  friend bool operator<(const PropositionBase& lhs,
41  const PropositionBase& rhs) {
42  return std::tie(lhs.name(), lhs.arguments()) <
43  std::tie(rhs.name(), rhs.arguments());
44  }
45  friend bool operator==(const PropositionBase& lhs,
46  const PropositionBase& rhs) {
47  return std::tie(lhs.hash_, lhs.name(), lhs.arguments()) ==
48  std::tie(rhs.hash_, rhs.name(), rhs.arguments());
49  }
50  friend bool operator!=(const PropositionBase& lhs,
51  const PropositionBase& rhs) {
52  return !(lhs == rhs);
53  }
54 
55  friend std::ostream& operator<<(std::ostream& os, const PropositionBase& P);
56 
57  protected:
58  static size_t Hash(const PropositionBase& prop);
59  static size_t Hash(const PropositionBase& prop, size_t predicate_hash);
60 
61  void PrecomputeHash() { hash_ = Hash(*this); }
62  void PrecomputeHash(size_t predicate_hash) { hash_ = Hash(*this, predicate_hash); }
63 
64  size_t hash_;
65 };
66 
67 class Proposition : public PropositionBase {
68  public:
69  Proposition() = default;
70 
71  Proposition(const std::string& name_predicate,
72  std::vector<Object>&& arguments)
73  : name_(name_predicate), arguments_(std::move(arguments)) {
74  PrecomputeHash();
75  }
76 
77  Proposition(const std::string& name_predicate,
78  const std::vector<Object>& arguments)
79  : name_(name_predicate), arguments_(arguments) {
80  PrecomputeHash();
81  }
82 
83  Proposition(const Pddl& pddl, const std::string& str_prop)
84  : name_(ParseHead(str_prop)),
85  arguments_(Object::ParseArguments(pddl, str_prop)) {
86  PrecomputeHash();
87  }
88 
89  // explicit Proposition(const std::string& str_prop)
90  // : name_(ParseHead(str_prop)),
91  // arguments_(Object::ParseArguments(str_prop)) {
92  // PrecomputeHash();
93  // }
94 
95  explicit Proposition(const PropositionBase& other)
96  : PropositionBase(other),
97  name_(other.name()),
98  arguments_(other.arguments()) {}
99 
100  const std::string& name() const override { return name_; }
101 
102  const std::vector<Object>& arguments() const override { return arguments_; }
103 
104  private:
105  std::string name_;
106  std::vector<Object> arguments_;
107 };
108 
110  public:
111  PropositionRef(const std::string* name_predicate,
112  const std::vector<Object>* arguments,
113  size_t predicate_hash)
114  : name_(name_predicate), arguments_(arguments) {
115  PrecomputeHash(predicate_hash);
116  }
117 
118  const std::string& name() const override { return *name_; }
119 
120  const std::vector<Object>& arguments() const override { return *arguments_; }
121 
122  private:
123  const std::string* name_;
124  const std::vector<Object>* arguments_;
125 };
126 
128  public:
129  SignedProposition(Proposition&& prop, bool is_pos)
130  : Proposition(std::move(prop)), is_pos_(is_pos) {}
131 
132  SignedProposition(const std::string& name_predicate,
133  std::vector<Object>&& arguments, bool is_pos)
134  : Proposition(name_predicate, std::move(arguments)), is_pos_(is_pos) {}
135 
136  bool is_pos() const { return is_pos_; }
137 
138  std::string sign() const { return Sign(is_pos()); };
139 
140  static std::string Sign(bool is_pos) { return is_pos ? "+" : "-"; };
141 
142  private:
143  bool is_pos_;
144 };
145 
146 } // namespace symbolic
147 
148 namespace std {
149 
150 template <>
151 struct hash<symbolic::PropositionBase> {
152  using is_transparent = void;
153  size_t operator()(const symbolic::PropositionBase& prop) const noexcept {
154  return prop.hash();
155  }
156 };
157 
158 template <>
159 struct hash<symbolic::Proposition> {
160  using is_transparent = void;
161  size_t operator()(const symbolic::Proposition& prop) const noexcept {
162  return prop.hash();
163  }
164 };
165 
166 template <>
167 struct hash<symbolic::PropositionRef> {
168  using is_transparent = void;
169  size_t operator()(const symbolic::PropositionRef& prop) const noexcept {
170  return prop.hash();
171  }
172 };
173 
174 } // namespace std
175 
176 #endif // SYMBOLIC_PROPOSITION_H_
symbolic::PropositionBase
Definition: proposition.h:25
symbolic
Definition: action.cc:329
symbolic::Pddl
Definition: pddl.h:40
symbolic::Proposition
Definition: proposition.h:67
symbolic::PropositionRef
Definition: proposition.h:109
symbolic::SignedProposition
Definition: proposition.h:127