Inter4ql  5.2
ModuleProblem.h
1 #ifndef __MODULE_PROBLEM_H__
2 #define __MODULE_PROBLEM_H__
3 
4 #include <vector>
5 #include <string>
6 #include <stack>
7 
8 #include "ModuleAction.h"
9 #include "ModuleGeneric.h"
10 
11 namespace Inter4ql {
12 
13  class Expression;
14  class Value;
15 
16  enum TagType {
17  START_PARALLEL, RESET_ENV_PARALLEL, END_PARALLEL, EXPRESSION
18  };
19 
20  class ActionEntry {
21  public:
22  ActionEntry(ModuleGeneric* belief, ModuleGeneric* world,
23  ModuleAction* action, std::vector<Value *>* valuation,
24  std::vector<std::string>* vn, int rating) {
25  this->belief_ = belief;
26  this->world_ = world;
27  this->action_ = action;
28  this->valuation_ = valuation;
29  this->variable_names_ = vn;
30 
31  this->rating_ = rating;
32  }
33 
34  ModuleGeneric* get_belief() const {
35  return this->belief_;
36  }
37 
38  ModuleGeneric* get_world() const {
39  return this->world_;
40  }
41 
42  ModuleAction* get_action() const {
43  return this->action_;
44  }
45 
46  std::vector<Value *>* get_valuation() const {
47  return this->valuation_;
48  }
49 
50  std::vector<std::string>* get_variable_names() const {
51  return this->variable_names_;
52  }
53 
54  int get_rating() const {
55  return this->rating_;
56  }
57 
58  private:
59  ModuleGeneric* belief_;
60  ModuleGeneric* world_;
61  ModuleAction* action_;
62  std::vector<Value *>* valuation_;
63  std::vector<std::string>* variable_names_;
64  int rating_;
65  };
66 
68  bool operator() (const ActionEntry* lhs, const ActionEntry* rhs) const {
69  return lhs->get_rating() > rhs->get_rating();
70  }
71  };
72 
73  class StackElem {
74  public:
75  StackElem(TagType type, std::string action_name, int level) {
76  this->elem_type = type;
77  this->action_name = action_name;
78  this->expr = nullptr;
79  this->parallel_end = nullptr;
80 
81  this->stored_to_add_partial = nullptr;
82  this->stored_to_remove_partial = nullptr;
83 
84  this->stored_to_add = nullptr;
85  this->stored_to_remove = nullptr;
86 
87  this->required_binding = nullptr;
88 
89  this->level = level;
90  this->is_root_in_parallel = false;
91  }
92 
93  StackElem(TagType type, Expression* expr, std::map<std::string, Value*>* required_binding,
94  std::string action_name, int level) {
95  this->elem_type = type;
96  this->action_name = action_name;
97  this->expr = expr;
98  this->parallel_end = nullptr;
99 
100  this->stored_to_add_partial = nullptr;
101  this->stored_to_remove_partial = nullptr;
102 
103  this->stored_to_add = nullptr;
104  this->stored_to_remove = nullptr;
105 
106  this->required_binding = required_binding;
107 
108  this->level = level;
109  this->is_root_in_parallel = false;
110  }
111 
112  StackElem(TagType type, StackElem* parallel_end, std::string action_name, int level) {
113  this->elem_type = type;
114  this->action_name = action_name;
115  this->expr = nullptr;
116  this->parallel_end = parallel_end;
117 
118  this->stored_to_add_partial = nullptr;
119  this->stored_to_remove_partial = nullptr;
120 
121  this->stored_to_add = nullptr;
122  this->stored_to_remove = nullptr;
123 
124  this->required_binding = nullptr;
125 
126  this->level = level;
127  this->is_root_in_parallel = false;
128  }
129 
130  std::string print() {
131  if (elem_type == START_PARALLEL)
132  return "START_PARALLEL";
133  if (elem_type == RESET_ENV_PARALLEL)
134  return "RESET_ENV_PARALLEL";
135  if (elem_type == END_PARALLEL)
136  return "END_PARALLEL";
137  if (elem_type == EXPRESSION)
138  return print_expression(expr);
139  }
140 
141  Expression* expr;
142  TagType elem_type;
143  StackElem* parallel_end;
144 
145  database* stored_to_add_partial;
146  database* stored_to_remove_partial;
147 
148  database* stored_to_add;
149  database* stored_to_remove;
150 
151  int level;
152  bool is_root_in_parallel;
153 
154  std::string action_name;
155 
156  std::map<std::string, Value*>* required_binding;
157  };
158 
161  class ModuleProblem : public ModuleGeneric {
162  public:
169  ModuleProblem(std::string _name,
170  std::vector<std::string>* _beliefs,
171  std::vector<std::string>* _actions,
172  Expression* _goal,
173  int _max_depth,
174  std::vector<std::string>* _heuristics);
178  ~ModuleProblem();
179 
180  std::string get_name() const;
181  std::string print();
182  module_type::type get_module_type() const;
183  void reason();
184  Result ask_for_result(Fact * term) const;
185 
190  std::vector<std::string>* get_beliefs();
195  std::vector<std::string>* get_actions();
200  Expression* get_goal();
205  int get_max_depth();
210  void add_belief_module(ModuleGeneric* module);
215  std::vector<ModuleGeneric*>* get_belief_modules();
220  void add_action_module(ModuleGeneric* module);
225  std::vector<ModuleGeneric*>* get_action_modules();
230  void replace_actions(std::vector<std::string>* actions);
235  bool contains_action(std::string action_name);
236 
237  private:
238  ModuleGeneric* select_action(std::vector<ModuleGeneric*>* actions, std::vector<ModuleGeneric*>* composite);
239  std::vector<Value *>* select_valuation(std::vector<std::vector<Value *>*>* valuations);
240  ModuleGeneric* select_generic_module(std::vector<ModuleGeneric*>* modules);
241 
242  bool find_and_apply_action(int step, std::vector<std::string>& plan, int& rating);
243 
244  bool traverse_composite_expr(std::stack<StackElem>* composite_stack, std::stack<database*>* add_database_stack,
245  std::stack<database*>* remove_database_stack, ModuleAction* current_action, ModuleGeneric* member, ModuleGeneric* belief,
246  std::vector<ModuleGeneric*>* action_modules, int step, int steps_from_start, std::vector<std::string>& plan, int& rating,
247  int best_rating, std::string previous_id);
248 
249  std::vector<std::string>* beliefs;
250  std::vector<ModuleGeneric*>* belief_modules;
251  std::vector<std::string>* actions;
252  std::vector<ModuleGeneric*>* action_modules;
253  Expression* goal;
254  int max_depth;
255  std::string name;
256  std::vector<std::string>* heuristics;
257 
258  bool goal_probing;
259  bool increase_rating_only;
260  bool strict_increase_rating_only;
261  bool composite_first;
262  bool random;
263  bool disallow_failed_preconditions;
264 
265  std::stack<StackElem>* composite_stack;
266  std::stack<database*>* remove_database_stack;
267  std::stack<database*>* add_database_stack;
268 
269  std::stack<int>* selected_rating;
270 
271  std::map<int, std::map<std::string, int>> composite_ratings;
272  };
273 }
274 
275 #endif /* __MODULE_PROBLEM_H__ */
276 
class that implements a fact
Definition: Fact.h:14
Implementation of ModuleAction for modules with reasoning.
Definition: ModuleAction.h:21
Definition: Expression.h:10
Implementation of ModuleAction for modules with reasoning.
Definition: ModuleProblem.h:161
Definition: Result.h:16
Definition: Application.cc:37
Generic class of module (only virtual methods)
Definition: ModuleGeneric.h:21
Definition: ModuleProblem.h:67
Definition: ModuleProblem.h:73
Definition: ModuleProblem.h:20