-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpphop.hpp
323 lines (276 loc) · 9.5 KB
/
cpphop.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
cpphop, version 1.0.0 -- a simple SHOP-like planner based in PyHop written in C++.
Author: Marcos Grillo, 2014.06.26
Copyright 2014 Marcos Grillo
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Cpphop should work correctly with g++ 4.8.2 and above, it needs the boost library
For examples of how to use it, see the example files that come with Cpphop.
*/
#include <vector>
#include <string>
#include <map>
#include <boost/any.hpp>
#include <tr1/unordered_map>
#include <sys/time.h>
#include <boost/atomic.hpp>
#include <boost/thread.hpp>
namespace cpphophtn{
class state{
public:
std::string name;
std::map<std::string, boost::any> variables;
inline state operator= (const state& s) {
this->name = s.name;
this->variables.clear();
this->variables.insert(s.variables.begin(), s.variables.end());
return *this;
}
};
inline std::ostream& operator<< (std::ostream& o, const std::map<std::string, boost::any> v){
std::map<std::string, boost::any>::const_iterator it;
for(it = v.begin(); it != v.end(); it++){
if(it != v.begin())
o << ", ";
o << (*it).first;
if((*it).second.type() == typeid(std::string)){
o << ": \"" << boost::any_cast<std::string>((*it).second) << "\"";
}else if((*it).second.type() == typeid(int)){
o << ": " << boost::any_cast<int>((*it).second);
}else if((*it).second.type() == typeid(float)){
o << ": " << boost::any_cast<float>((*it).second);
}else if((*it).second.type() == typeid(double)){
o << ": " << boost::any_cast<double>((*it).second);
}else if((*it).second.type() == typeid(std::map<std::string, std::string>)){
o << ": {";
std::map<std::string, std::string> v = boost::any_cast< std::map<std::string, std::string> >((*it).second);
std::map<std::string, std::string>::iterator i;
for(i = v.begin(); i != v.end(); ++i){
if(i != v.begin()){
o << ", ";
}
o << "" << (*i).first << ": \"" << (*i).second << "\"";
}
o << "}";
}else if((*it).second.type() == typeid(std::map<std::string, bool>)){
o << ": {";
std::map<std::string, bool> v = boost::any_cast< std::map<std::string, bool> >((*it).second);
std::map<std::string, bool>::iterator i;
for(i = v.begin(); i != v.end();++i){
if(i != v.begin())
o << ", ";
std::string value((*i).second ? "true" : "false");
o << "" << (*i).first << ": " << value << "";
}
o << "}";
}else if((*it).second.type() == typeid(state)){
state s = boost::any_cast<state>((*it).second);
o << ": {" << s.variables << "}";
}
}
return o;
}
inline std::ostream& operator<< (std::ostream& o, const state& t){
return o << t.name << ": {" << t.variables << "}";
}
inline std::ostream& operator<< (std::ostream& o, const std::vector<state>& op){
for(std::vector<state>::const_iterator it = op.begin(); it != op.end(); ++it)
o << (*it) << ", ";
return o << std::endl;
}
class htn_operator{
public:
std::string name;
bool (*action)(state &init_state, std::map<std::string, boost::any>& parameters, state &final_state);
htn_operator(){}
htn_operator(std::string n):name(n){}
// To be declared in the operator classes derived from this
inline htn_operator operator= (const htn_operator& h) {
this->name = h.name;
this->action = h.action;
return *this;
}
};
inline std::ostream& operator<< (std::ostream& o, const htn_operator& op){
return o << op.name;
}
inline std::ostream& operator<< (std::ostream& o, const std::vector<htn_operator>& op){
for(std::vector<htn_operator>::const_iterator it = op.begin(); it != op.end(); ++it)
o << (*it) << ", ";
return o << std::endl;
}
class task{
public:
std::string name;
std::map<std::string, boost::any> parameters;
task(){}
inline task operator= (const task& t) {
this->parameters.clear();
this->name = t.name;
this->parameters.insert(t.parameters.begin(), t.parameters.end());
return *this;
}
};
inline std::ostream& operator<< (std::ostream& o, const task& t){
return o << "('" << t.name << "' " << t.parameters << ")";
}
inline std::ostream& operator<< (std::ostream& o, const std::vector<task>& op){
o << "[";
for(std::vector<task>::const_iterator it = op.begin(); it != op.end(); ++it){
if(it != op.begin())
o << ", ";
o << (*it);
}
o << "]";
return o << std::endl;
}
class method{
public:
std::string name;
// To be declared in the method classes derived from this, tasks will hold the decomposition of the vector
bool (*method_exe)(state &init_state, std::map<std::string, boost::any>& parameters, std::vector<task>& tasks);
method(){}
inline method operator= (const method& m) {
this->name = m.name;
this->method_exe = m.method_exe;
return *this;
}
};
inline std::ostream& operator<< (std::ostream& o, const method& m){
return o << m.name;
}
inline std::ostream& operator<< (std::ostream& o, const std::vector<method>& m){
for(std::vector<method>::const_iterator it = m.begin(); it != m.end(); ++it)
o << (*it) << ", ";
return o << std::endl;
}
class cpphop_pause_info{
public:
bool saved;
state saved_state;
std::vector<task>tasks;
int depth;
std::vector<task>result;
inline cpphop_pause_info operator=(const cpphop_pause_info & c){
saved = c.saved;
saved_state = c.saved_state;
tasks.clear();
for(std::vector<task>::const_iterator it = c.tasks.begin(); it != c.tasks.end(); ++it){
tasks.push_back(*it);
}
depth = c.depth;
result.clear();
for(std::vector<task>::const_iterator it = c.result.begin(); it != c.result.end(); ++it){
result.push_back(*it);
}
}
};
typedef enum return_state_t{
STATE_TRUE,
STATE_FALSE,
STATE_PAUSED
}return_state_t;
class cpphop{
public:
std::tr1::unordered_map< std::string, htn_operator, std::tr1::hash<std::string> > operators;
std::tr1::unordered_map< std::string, std::vector<method>, std::tr1::hash<std::string> > methods;
cpphop(): pause(false), paused(false){paused_info.saved = false;}
cpphop(const cpphop& c){
this->operators.clear();
this->operators.insert(c.operators.begin(), c.operators.end());
this->methods.clear();
this->methods.insert(c.methods.begin(), c.methods.end());
if(c.pause)
this->pause = true;
else
this->pause = false;
if(c.paused)
this->paused = true;
else
this->paused = false;
if(c.running)
this->running = true;
else
this->running = false;
}
void declare_operator(const std::string& name, htn_operator &op){
operators.insert(std::make_pair<std::string, htn_operator>(name, op));
}
void declare_method(const std::string& name, method &m){
std::tr1::unordered_map< std::string, std::vector<method> >::iterator it = methods.find(name);
if(it == methods.end()){
std::vector<method> v;
v.push_back(m);
methods.insert(std::make_pair<std::string, std::vector<method> >(name, v));
}else{
std::vector<method> &v = methods[name];
v.push_back(m);
}
}
void declare_methods(const std::string& name, std::vector<method> &m){
methods.insert(std::make_pair< std::string, std::vector<method> >(name, m));
}
return_state_t plan(state& state, std::vector<task> &tasks, std::vector<task>& result, int verbose, suseconds_t miliseconds);
bool pause_plan();
return_state_t resume();
bool get_operator(const std::string &name, htn_operator &op){
std::tr1::unordered_map< std::string, htn_operator>::iterator it = operators.find(name);
if(it == operators.end()){
return false;
}
op = (*it).second;
return true;
}
bool get_methods(const std::string &name, std::vector<method> &rv){
std::tr1::unordered_map< std::string,std::vector<method> >::iterator it = methods.find(name);
if(it == methods.end()){
return false;
}
rv = methods[name];
return true;
}
void clear(){
operators.clear();
methods.clear();
}
inline cpphop operator= (const cpphop& c){
this->operators.clear();
this->operators.insert(c.operators.begin(), c.operators.end());
this->methods.clear();
this->methods.insert(c.methods.begin(), c.methods.end());
if(c.pause)
this->pause = true;
else
this->pause = false;
if(c.paused)
this->paused = true;
else
this->paused = false;
if(c.running)
this->running = true;
else
this->running = false;
return *this;
}
bool isRunningPlanification(){
return running;
}
private:
bool running;
bool pause;
bool paused;
boost::condition_variable plan_paused;
boost::mutex mutex;
cpphop_pause_info paused_info;
void load_pause_info(state& state, std::vector<task>& tasks, int& depth, std::vector<task>& result);
void save_pause_info(state& state, std::vector<task>& tasks, int& depth, std::vector<task>& result);
return_state_t seek_plan(state& state, std::vector<task>& tasks, int depth, std::vector<task>& result, int verbose, suseconds_t miliseconds, suseconds_t time_elapsed);
};
}