-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathterm_manager.cpp
558 lines (467 loc) · 15 KB
/
term_manager.cpp
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
#include "term_manager.h"
#include "type_manager.h"
#include "plan_bindings.h"
#include <boost/algorithm/string.hpp>
///#define MYPOP_TERM_MANAGER_COMMENTS
///#define MYPOP_TERM_MANAGER_DEBUG
namespace MyPOP {
/*************************
* The Term class
*************************/
Term::Term(const Type& type, const std::string& name)
: type_(&type), name_(name)
{
}
bool Term::isTheSameAs(StepID lhs_id, const Term& rhs, StepID rhs_id, const Bindings& bindings) const
{
const std::vector<const Object*>& lhs_domain = getDomain(lhs_id, bindings);
const std::vector<const Object*>& rhs_domain = rhs.getDomain(rhs_id, bindings);
return &lhs_domain == &rhs_domain;
}
bool Term::isEquivalentTo(StepID lhs_id, const Term& rhs, StepID rhs_id, const Bindings& lhs_bindings, const Bindings* rhs_bindings) const
{
if (rhs_bindings == NULL)
{
rhs_bindings = &lhs_bindings;
}
const std::vector<const Object*>& lhs_domain = getDomain(lhs_id, lhs_bindings);
const std::vector<const Object*>& rhs_domain = rhs.getDomain(rhs_id, *rhs_bindings);
if (lhs_domain.size() != rhs_domain.size())
{
return false;
}
for (std::vector<const Object*>::const_iterator ci = lhs_domain.begin(); ci != lhs_domain.end(); ci++)
{
const Object* lhs_object = *ci;
bool found = false;
for (std::vector<const Object*>::const_iterator ci = rhs_domain.begin(); ci != rhs_domain.end(); ci++)
{
const Object* rhs_object = *ci;
if (lhs_object == rhs_object)
{
found = true;
break;
}
}
if (!found)
{
return false;
}
}
return true;
}
bool Term::canUnify(StepID lhs_id, const Term& rhs, StepID rhs_id, const Bindings& lhs_bindings, const Bindings* rhs_bindings) const
{
if (rhs_bindings == NULL)
{
rhs_bindings = &lhs_bindings;
}
const std::vector<const Object*>& lhs_domain = getDomain(lhs_id, lhs_bindings);
const std::vector<const Object*>& rhs_domain = rhs.getDomain(rhs_id, *rhs_bindings);
for (std::vector<const Object*>::const_iterator ci = lhs_domain.begin(); ci != lhs_domain.end(); ci++)
{
const Object* lhs_object = *ci;
for (std::vector<const Object*>::const_iterator ci = rhs_domain.begin(); ci != rhs_domain.end(); ci++)
{
const Object* rhs_object = *ci;
if (lhs_object == rhs_object)
{
return true;
}
}
}
return false;
}
bool Term::contains(const Object& object, StepID lhs_id, const Bindings& bindings) const
{
const std::vector<const Object*>& domain = getDomain(lhs_id, bindings);
for (std::vector<const Object*>::const_iterator ci = domain.begin(); ci != domain.end(); ci++)
{
if (*ci == &object)
return true;
}
return false;
}
bool Term::containsAtLeastOneOf(const std::vector<const Object*>& objects, StepID lhs_id, const Bindings& bindings) const
{
for (std::vector<const Object*>::const_iterator ci = objects.begin(); ci != objects.end(); ci++)
{
if (contains(**ci, lhs_id, bindings))
{
return true;
}
}
return false;
}
bool Term::isProperSuperSetOf(StepID lhs_id, const Term& other, StepID rhs_id, const Bindings& bindings) const
{
const std::vector<const Object*>& other_variable_domain = other.getDomain(rhs_id, bindings);
const std::vector<const Object*>& this_variable_domain = getDomain(lhs_id, bindings);
// If the domain of the other is equal or larger than this domain it cannot be a proper superset.
if (other_variable_domain.size() >= this_variable_domain.size())
return false;
// Make sure this contains all objects contained by other.
for (std::vector<const Object*>::const_iterator ci = other_variable_domain.begin(); ci != other_variable_domain.end(); ci++)
{
const Object* other_object = *ci;
bool contains_object = false;
for (std::vector<const Object*>::const_iterator ci = this_variable_domain.begin(); ci != this_variable_domain.end(); ci++)
{
const Object* this_object = *ci;
if (other_object == this_object)
{
contains_object = true;
break;
}
}
if (!contains_object)
{
return false;
}
}
return true;
}
bool Term::isProperSubSetOf(StepID lhs_id, const Term& other, StepID rhs_id, const Bindings& bindings) const
{
const std::vector<const Object*>& other_variable_domain = other.getDomain(rhs_id, bindings);
const std::vector<const Object*>& this_variable_domain = getDomain(lhs_id, bindings);
// If the domain of the other is equal or smaller than this domain it cannot be a proper subset.
if (other_variable_domain.size() <= this_variable_domain.size())
return false;
// Make sure other contains all objects contained by this.
for (std::vector<const Object*>::const_iterator ci = this_variable_domain.begin(); ci != this_variable_domain.end(); ci++)
{
const Object* other_object = *ci;
bool contains_object = false;
for (std::vector<const Object*>::const_iterator ci = other_variable_domain.begin(); ci != other_variable_domain.end(); ci++)
{
const Object* this_object = *ci;
if (other_object == this_object)
{
contains_object = true;
break;
}
}
if (!contains_object)
{
return false;
}
}
return true;
}
std::ostream& operator<<(std::ostream& os, const Term& term)
{
os << term.getName();
return os;
}
/*************************
* The Object class
*************************/
Object::Object(const Type& type, const std::string& name)
: Term(type, name)
{
domain_.push_back(this);
}
Object::~Object()
{
}
bool Object::unify(StepID lhs_id, const Term& rhs, StepID rhs_id, Bindings& bindings) const
{
return rhs.unifyWith(rhs_id, *this, bindings);
}
bool Object::makeDisjunct(StepID lhs_id, const Term& rhs, StepID rhs_id, Bindings& bindings) const
{
#ifdef MYPOP_TERM_MANAGER_DEBUG
// We cannot make an object disjunct from anything.
if (rhs.contains(*this, rhs_id, bindings))
{
std::cout << "Cannot make an object disjunct from itself!" << std::endl;
assert (false);
}
#endif
return false;
}
bool Object::makeDomainEqualTo(StepID lhs_id, const Term& rhs, StepID rhs_id, Bindings& lhs_bindings, Bindings* rhs_bindings) const
{
if (rhs_bindings == NULL)
{
rhs_bindings = &lhs_bindings;
}
#ifdef MYPOP_TERM_MANAGER_DEBUG
// Not sure what to do if asked to make equal to a term which does not contain the object in its domain.
if (!rhs.contains(*this, rhs_id, *rhs_bindings))
{
std::cout << "Cannot make an object equal to something that does not contain the object..." << std::endl;
assert (false);
}
#endif
// We cannot manipulate the object.
return false;
}
bool Object::makeDomainEqualTo(StepID lhs_id, const std::vector<const Object*>& objects, Bindings& bindings) const
{
#ifdef MYPOP_TERM_MANAGER_DEBUG
// Not sure what to do if we are asked to make an object equal to an empty set.
for (std::vector<const Object*>::const_iterator ci = objects.begin(); ci != objects.end(); ci++)
{
if (*ci == this)
{
return false;
}
}
std::cout << "Tried to make the domain of an object empty." << std::endl;
assert (false);
return true;
#else
return false;
#endif
}
bool Object::makeDomainUnequalTo(StepID lhs_id, const Term& rhs, StepID rhs_id, Bindings& lhs_bindings, Bindings* rhs_bindings) const
{
if (rhs_bindings == NULL)
{
rhs_bindings = &lhs_bindings;
}
#ifdef MYPOP_TERM_MANAGER_DEBUG
if (rhs.contains(*this, rhs_id, *rhs_bindings))
{
std::cout << "Tried to make an object unequal to itself..." << std::endl;
assert (false);
}
#endif
return false;
}
bool Object::makeDomainUnequalTo(StepID lhs_id, const std::vector<const Object*>& objects, Bindings& bindings) const
{
#ifdef MYPOP_TERM_MANAGER_DEBUG
for (std::vector<const Object*>::const_iterator ci = objects.begin(); ci != objects.end(); ci++)
{
if (*ci == this)
{
std::cout << "Tried to make an object unequal to itself..." << std::endl;
assert (false);
}
}
#endif
return false;
}
const std::vector<const Object*>& Object::getDomain(StepID id, const Bindings& bindings) const
{
return domain_;
}
bool Object::unifyWith(StepID lhs_id, const Object& object, Bindings& bindings) const
{
return this == &object;
}
bool Object::unifyWith(StepID lhs_id, const Variable& variable, StepID rhs_id, Bindings& bindings) const
{
return variable.unifyWith(rhs_id, *this, bindings);
}
bool Object::makeDisjunctWith(StepID lhs_id, const Variable& variable, StepID rhs_id, Bindings& bindings) const
{
VariableDomain& vd = bindings.getNonConstVariableDomain(rhs_id, variable);
return vd.makeUnequalTo(*this);
}
void Object::bind(Bindings& bindings, StepID new_step_id) const
{
}
void Object::print(std::ostream& os, const Bindings& bindings, StepID id) const
{
os << getName();
}
/*************************
* The Variable class
*************************/
Variable::Variable(const Type& type, const std::string& name)
: Term(type, name)
{
}
Variable::~Variable()
{
}
bool Variable::unify(StepID lhs_id, const Term& rhs, StepID rhs_id, Bindings& bindings) const
{
return rhs.unifyWith(rhs_id, *this, lhs_id, bindings);
}
bool Variable::makeDisjunct(StepID lhs_id, const Term& rhs, StepID rhs_id, Bindings& bindings) const
{
return rhs.makeDisjunctWith(rhs_id, *this, lhs_id, bindings);
}
bool Variable::makeDomainEqualTo(StepID lhs_id, const Term& rhs, StepID rhs_id, Bindings& lhs_bindings, Bindings* rhs_bindings) const
{
if (rhs_bindings == NULL)
{
rhs_bindings = &lhs_bindings;
}
VariableDomain& vd = lhs_bindings.getNonConstVariableDomain(lhs_id, *this);
return vd.makeEqualTo(rhs.getDomain(rhs_id, *rhs_bindings));
}
bool Variable::makeDomainEqualTo(StepID lhs_id, const std::vector<const Object*>& objects, Bindings& bindings) const
{
VariableDomain& vd = bindings.getNonConstVariableDomain(lhs_id, *this);
return vd.makeEqualTo(objects);
}
bool Variable::makeDomainUnequalTo(StepID lhs_id, const Term& rhs, StepID rhs_id, Bindings& lhs_bindings, Bindings* rhs_bindings) const
{
if (rhs_bindings == NULL)
{
rhs_bindings = &lhs_bindings;
}
VariableDomain& vd = lhs_bindings.getNonConstVariableDomain(lhs_id, *this);
return vd.makeUnequalTo(rhs.getDomain(rhs_id, *rhs_bindings));
}
bool Variable::makeDomainUnequalTo(StepID lhs_id, const std::vector<const Object*>& objects, Bindings& bindings) const
{
VariableDomain& vd = bindings.getNonConstVariableDomain(lhs_id, *this);
return vd.makeUnequalTo(objects);
}
const std::vector<const Object*>& Variable::getDomain(StepID id, const Bindings& bindings) const
{
const VariableDomain& vd = bindings.getVariableDomain(id, *this);
return vd.getDomain();
}
bool Variable::unifyWith(StepID lhs_id, const Object& object, Bindings& bindings) const
{
VariableDomain& vd = bindings.getNonConstVariableDomain(lhs_id, *this);
vd.makeEqualTo(object);
return !vd.getDomain().empty();
}
bool Variable::unifyWith(StepID lhs_id, const Variable& variable, StepID rhs_id, Bindings& bindings) const
{
if (isTheSameAs(lhs_id, variable, rhs_id, bindings))
{
return true;
}
if (!canUnify(lhs_id, variable, rhs_id, bindings))
{
return false;
}
/* std::cout << "Unify two variables: ";
print(std::cout, bindings, lhs_id);
std::cout << "(" << this << ") with ";
variable.print(std::cout, bindings, rhs_id);
std::cout << "(" << &variable << ")" << std::endl;
*/
VariableDomain& lhs_vd = bindings.getNonConstVariableDomain(lhs_id, *this);
VariableDomain& rhs_vd = bindings.getNonConstVariableDomain(rhs_id, variable);
if (lhs_vd.makeEqualTo(rhs_vd))
{
bindings.postProcessMerge(lhs_vd, rhs_vd);
}
//std::cout << "Result: " << result << std::endl;
#ifdef MYPOP_TERM_MANAGER_DEBUG
assert (isTheSameAs(lhs_id, variable, rhs_id, bindings));
#endif
return true;
}
bool Variable::makeDisjunctWith(StepID lhs_id, const Variable& variable, StepID rhs_id, Bindings& bindings) const
{
VariableDomain& lhs_vd = bindings.getNonConstVariableDomain(lhs_id, *this);
VariableDomain& rhs_vd = bindings.getNonConstVariableDomain(rhs_id, variable);
return lhs_vd.makeUnequalTo(rhs_vd);
}
void Variable::bind(Bindings& bindings, StepID new_step_id) const
{
bindings.createVariableDomain(new_step_id, *this);
}
void Variable::print(std::ostream& os, const Bindings& bindings, StepID id) const
{
const std::vector<const Object*>& domain = getDomain(id, bindings);
os << "{";
for (std::vector<const Object*>::const_iterator ci = domain.begin(); ci != domain.end(); ci++)
{
os << **ci;
if (ci != domain.end() - 1)
{
os << ", ";
}
}
os << "}";
}
/*************************
* The TermManager class
*************************/
TermManager::TermManager(const TypeManager& type_manager)
: type_manager_(&type_manager)
{
// During processing map the pddl symbols to our internal types for
// easy access during the parsing phase. This indexing is removed
// once we don't need it anymore, i.e. after the parsing phase.
term_indexing_ = new std::map<const VAL::symbol*, const Term*>();
term_string_indexing_ = new std::map<string, const Term*>();
}
TermManager::~TermManager()
{
delete term_indexing_;
delete term_string_indexing_;
//for (std::map<const Type*, std::vector<const Object*>*>::const_iterator ci = objects_per_type_.begin(); ci != objects_per_type_.end(); ci++)
// delete (*ci).second;
}
void TermManager::processActionVariables(const VAL::operator_list& operators)
{
for (VAL::operator_list::const_iterator ci = operators.begin(); ci != operators.end(); ci++)
{
const VAL::operator_* op = *ci;
const VAL::var_symbol_list* parameters = op->parameters;
//int var_counter = 0;
for (VAL::var_symbol_list::const_iterator i = parameters->begin(); i != parameters->end(); i++)
{
VAL::var_symbol* parameter = *i;
// Get the type of the parameter.
const Type* type = type_manager_->getType(parameter->type->getName());
Variable* var = new Variable(*type, parameter->getName());
addTerm(*parameter, *var);
}
}
}
void TermManager::addTerm(const VAL::symbol& symbol, Term& term)
{
addManagableObject(&term);
(*term_indexing_)[&symbol] = &term;
(*term_string_indexing_)[symbol.getName()] = &term;
}
void TermManager::addTerm(const VAL::symbol& symbol, Object& object)
{
addTerm(symbol, (Term&)object);
domain_objects_.push_back(&object);
}
const Term* TermManager::getTerm(const VAL::symbol& symbol) const
{
return (*term_indexing_)[&symbol];
}
const Term* TermManager::getTerm(const std::string& name) const
{
return (*term_string_indexing_)[name];
}
const Object& TermManager::getObject(const std::string& name) const
{
for (std::vector<const Object*>::const_iterator ci = domain_objects_.begin(); ci != domain_objects_.end(); ci++)
{
const Object* object = *ci;
if (boost::iequals(object->getName(), name))
{
return *object;
}
}
std::cerr << "Cannot find an object with the name " << name << std::endl;
for (std::vector<const Object*>::const_iterator ci = domain_objects_.begin(); ci != domain_objects_.end(); ci++)
{
std::cerr << "* " << **ci << std::endl;
}
assert(false);
exit(1);
}
std::ostream& operator<<(std::ostream& os, const TermManager& term_manager)
{
os << " === Summary of all Objects in the current domain. === " << std::endl;
for (std::vector<Term*>::const_iterator ci = term_manager.getManagableObjects().begin(); ci != term_manager.getManagableObjects().end(); ci++)
{
// if ((*ci)->isObject())
// {
os << **ci << std::endl;
// }
}
os << " === End of Summary ===" << std::endl;
return os;
}
};