symbolic
object.h
1 
10 #ifndef SYMBOLIC_OBJECTS_H_
11 #define SYMBOLIC_OBJECTS_H_
12 
13 #include <memory> // std::shared_ptr
14 #include <ostream> // std::ostream
15 #include <utility> // std::tie
16 #include <vector> // std::vector
17 
18 namespace VAL {
19 
20 class pddl_type;
21 class pddl_typed_symbol;
22 
23 template <typename T>
25 
26 using pddl_type_list = class typed_symbol_list<pddl_type>;
27 
28 } // namespace VAL
29 
30 namespace symbolic {
31 
32 class Pddl;
33 
34 class Object {
35  public:
36  class Type;
37 
38  Object() = default;
39 
40  Object(const Pddl& pddl, const VAL::pddl_typed_symbol* symbol);
41 
42  Object(const VAL::pddl_type_list* types,
43  const VAL::pddl_typed_symbol* symbol);
44 
45  Object(const Pddl& pddl, const std::string& name_object);
46 
47  const VAL::pddl_typed_symbol* symbol() const { return symbol_; }
48 
49  const std::string& name() const;
50 
51  const Type& type() const { return type_; }
52 
53  size_t hash() const { return hash_; }
54 
55  // Atom is a proposition or action
56  static std::vector<Object> ParseArguments(const Pddl& pddl,
57  const std::string& atom);
58 
59  static std::vector<Object> ParseArguments(const Pddl& pddl,
60  const std::vector<std::string>& str_args);
61 
62  template <typename T>
63  static std::vector<Object> CreateList(
64  const Pddl& pddl, const VAL::typed_symbol_list<T>* symbols);
65 
66  template <typename T>
67  static std::vector<Object> CreateList(
68  const VAL::pddl_type_list* types,
69  const VAL::typed_symbol_list<T>* symbols);
70 
71  friend bool operator<(const Object& lhs, const Object& rhs) {
72  return &lhs.name() != &rhs.name() && lhs.name() < rhs.name();
73  }
74 
75  friend bool operator==(const Object& lhs, const Object& rhs) {
76  return &lhs.name() == &rhs.name() ||
77  std::tie(lhs.hash_, lhs.name()) == std::tie(rhs.hash_, rhs.name());
78  }
79 
80  friend bool operator!=(const Object& lhs, const Object& rhs) {
81  return !(lhs == rhs);
82  }
83 
84  friend std::ostream& operator<<(std::ostream& os, const Object& object) {
85  os << object.name();
86  return os;
87  }
88 
89  class Type {
90  public:
91  Type() = default;
92 
93  explicit Type(const VAL::pddl_type* symbol) : symbol_(symbol) {}
94 
95  const VAL::pddl_type* symbol() const { return symbol_; }
96 
97  bool IsSubtype(const std::string& type) const;
98  bool IsSubtype(const Type& type) const { return IsSubtype(type.name()); }
99 
100  std::vector<std::string> ListTypes() const;
101 
102  const std::string& name() const;
103 
104  friend bool operator<(const Object::Type& lhs, const Object::Type& rhs) {
105  return lhs.name() < rhs.name();
106  }
107 
108  friend bool operator==(const Object::Type& lhs, const Object::Type& rhs) {
109  return lhs.name() == rhs.name();
110  }
111 
112  friend std::ostream& operator<<(std::ostream& os,
113  const Object::Type& type) {
114  os << type.name();
115  return os;
116  }
117 
118  private:
119  const VAL::pddl_type* symbol_ = nullptr;
120  };
121 
122  private:
123  const VAL::pddl_typed_symbol* symbol_ = nullptr;
124 
125  Type type_;
126 
127  size_t hash_;
128 };
129 
130 template <typename T>
131 std::vector<Object> Object::CreateList(
132  const Pddl& pddl, const VAL::typed_symbol_list<T>* symbols) {
133  std::vector<Object> objects;
134  if (symbols == nullptr) return objects;
135  objects.reserve(symbols->size());
136  for (const T* symbol : *symbols) {
137  objects.emplace_back(pddl, symbol);
138  }
139  return objects;
140 }
141 
142 template <typename T>
143 std::vector<Object> Object::CreateList(
144  const VAL::pddl_type_list* types,
145  const VAL::typed_symbol_list<T>* symbols) {
146  std::vector<Object> objects;
147  if (symbols == nullptr) return objects;
148  objects.reserve(symbols->size());
149  for (const T* symbol : *symbols) {
150  objects.emplace_back(types, symbol);
151  }
152  return objects;
153 }
154 
155 std::ostream& operator<<(std::ostream& os, const std::vector<Object>& objects);
156 
157 } // namespace symbolic
158 
159 namespace std {
160 
161 template <>
162 struct hash<symbolic::Object> {
163  size_t operator()(const symbolic::Object& obj) const noexcept {
164  return obj.hash();
165  }
166 };
167 
168 } // namespace std
169 
170 #endif // SYMBOLIC_OBJECTS_H_
VAL
Definition: pddl.cc:27
symbolic
Definition: action.cc:329
symbolic::Object
Definition: object.h:34
symbolic::Pddl
Definition: pddl.h:40
symbolic::Object::Type
Definition: object.h:89
VAL::typed_symbol_list
Definition: object.h:24