/
bindings.cc
1450 lines (1340 loc) · 47.6 KB
/
bindings.cc
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) 2002--2005 Carnegie Mellon University
// Copyright (C) 2019 Google Inc
//
// This file is part of VHPOP.
//
// VHPOP is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// VHPOP is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
// License for more details.
//
// You should have received a copy of the GNU General Public License
// along with VHPOP; if not, write to the Free Software Foundation,
// Inc., #59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include "bindings.h"
#include <limits.h>
#include <algorithm>
#include <limits>
#include <typeinfo>
#include "debug.h"
#include "domains.h"
#include "formulas.h"
#include "heuristics.h"
#include "plans.h"
#include "problems.h"
#include "refcount.h"
#include "types.h"
/* ====================================================================== */
/* StepVariable */
typedef std::pair<Variable, size_t> StepVariable;
/* ====================================================================== */
/* VariableSet */
/*
* A set of variables.
*/
struct VariableSet : public std::set<StepVariable> {
};
/* ====================================================================== */
/* Varset */
/*
* Variable codesignation, and non-codesignation.
*/
struct Varset {
/* Constructs a varset. */
Varset(const Object* constant, const Chain<StepVariable>* cd_set,
const Chain<StepVariable>* ncd_set, const Type& type)
: constant_((constant != 0) ? new Object(*constant) : 0),
cd_set_(cd_set), ncd_set_(ncd_set), type_(type) {
RCObject::ref(cd_set_);
RCObject::ref(ncd_set_);
}
/* Constructs a varset. */
Varset(const Varset& vs)
: constant_((vs.constant_ != 0) ? new Object(*vs.constant_) : 0),
cd_set_(vs.cd_set_), ncd_set_(vs.ncd_set_), type_(vs.type_) {
RCObject::ref(cd_set_);
RCObject::ref(ncd_set_);
}
/* Deletes this varset. */
~Varset() {
if (constant_ != 0) {
delete constant_;
}
RCObject::destructive_deref(cd_set_);
RCObject::destructive_deref(ncd_set_);
}
/* Pointer to the constant of this varset, or 0. */
const Object* constant() const { return constant_; }
/* The codesignation list. */
const Chain<StepVariable>* cd_set() const { return cd_set_; }
/* The non-codesignation list. */
const Chain<StepVariable>* ncd_set() const { return ncd_set_; }
/* Checks if this varset includes the given object. */
bool includes(const Object& obj) const {
return constant() != 0 && *constant() == obj;
}
/* Checks if this varset includes the given variable. */
bool includes(const Variable& var, size_t step_id) const {
for (const Chain<StepVariable>* vc = cd_set(); vc != 0; vc = vc->tail) {
if (vc->head.first == var && vc->head.second == step_id) {
return true;
}
}
return false;
}
/* Checks if this varset excludes the given variable. */
bool excludes(const Variable& var, size_t step_id) const {
for (const Chain<StepVariable>* vc = ncd_set(); vc != 0; vc = vc->tail) {
if (vc->head.first == var && vc->head.second == step_id) {
return true;
}
}
return false;
}
/* Returns the varset obtained by adding the given object to this
varset, or 0 if the object is excluded from this varset. */
const Varset* add(const Chain<Varset>*& vsc, const Object& obj) const {
if (constant() != 0) {
return (*constant() == obj) ? this : 0;
} else {
const Type& ot = TermTable::type(obj);
if (TypeTable::subtype(ot, type_)) {
vsc = new Chain<Varset>(Varset(&obj, cd_set(), ncd_set(), ot), vsc);
return &vsc->head;
} else {
return 0;
}
}
}
/* Returns the varset obtained by adding the given variable to this
varset, or 0 if the variable is excluded from this varset. */
const Varset* add(const Chain<Varset>*& vsc, const Variable& var,
size_t step_id) const {
if (excludes(var, step_id)) {
return 0;
} else {
const Type* tt;
if (constant() != 0) {
if (!TypeTable::subtype(type_, TermTable::type(var))) {
return 0;
}
tt = &type_;
} else {
tt = TypeTable::most_specific(type_, TermTable::type(var));
if (tt == 0) {
return 0;
}
}
const Chain<StepVariable>* new_cd =
new Chain<StepVariable>(std::make_pair(var, step_id), cd_set());
vsc = new Chain<Varset>(Varset(constant(), new_cd, ncd_set(), *tt), vsc);
return &vsc->head;
}
}
/* Returns the varset obtained by adding the given term to this
varset, or 0 if the term is excluded from this varset. */
const Varset* add(const Chain<Varset>*& vsc, const Term& term,
size_t step_id) const {
if (term.object()) {
return add(vsc, term.as_object());
} else {
return add(vsc, term.as_variable(), step_id);
}
}
/* Returns the varset obtained by adding the given variable to the
non-codesignation list of this varset; N.B. assumes that the
variable is not included in the varset already. */
const Varset* restrict(const Chain<Varset>*& vsc,
const Variable& var, size_t step_id) const {
const Chain<StepVariable>* new_ncd =
new Chain<StepVariable>(std::make_pair(var, step_id), ncd_set());
vsc = new Chain<Varset>(Varset(constant(), cd_set(), new_ncd, type_), vsc);
return &vsc->head;
}
/* Returns the combination of this and the given varset, or 0 if
the combination is inconsistent. */
const Varset* combine(const Chain<Varset>*& vsc, const Varset& vs) const {
const Object* comb_obj;
const Type* tt;
if (constant() != 0) {
if (vs.constant() != 0) {
if (constant() != vs.constant()) {
return 0;
}
} else if (!TypeTable::subtype(type_, vs.type_)) {
return 0;
}
comb_obj = constant();
tt = &type_;
} else if (vs.constant() != 0) {
if (!TypeTable::subtype(vs.type_, type_)) {
return 0;
}
comb_obj = vs.constant();
tt = &vs.type_;
} else {
comb_obj = 0;
tt = TypeTable::most_specific(type_, vs.type_);
if (tt == 0) {
return 0;
}
}
const Chain<StepVariable>* comb_cd = cd_set();
for (const Chain<StepVariable>* vc = vs.cd_set(); vc != 0; vc = vc->tail) {
const StepVariable& step_var = vc->head;
if (excludes(step_var.first, step_var.second)) {
RCObject::ref(comb_cd);
RCObject::destructive_deref(comb_cd);
return 0;
} else {
comb_cd = new Chain<StepVariable>(step_var, comb_cd);
}
}
const Chain<StepVariable>* comb_ncd = ncd_set();
for (const Chain<StepVariable>* vc = vs.ncd_set();
vc != 0; vc = vc->tail) {
const StepVariable& step_var = vc->head;
if (includes(step_var.first, step_var.second)) {
RCObject::ref(comb_cd);
RCObject::destructive_deref(comb_cd);
RCObject::ref(comb_ncd);
RCObject::destructive_deref(comb_ncd);
return 0;
} else if (!excludes(step_var.first, step_var.second)) {
comb_ncd = new Chain<StepVariable>(step_var, comb_ncd);
}
}
vsc = new Chain<Varset>(Varset(comb_obj, comb_cd, comb_ncd, *tt), vsc);
return &vsc->head;
}
/* Returns the varset representing the given equality binding. */
static const Varset* make(const Chain<Varset>*& vsc, const Binding& b,
bool reverse = false) {
if (b.equality()) {
const Chain<StepVariable>* cd_set =
new Chain<StepVariable>(std::make_pair(b.var(), b.var_id()), 0);
if (b.term().object()) {
Object obj = b.term().as_object();
vsc = new Chain<Varset>(Varset(&obj, cd_set, 0,
TermTable::type(b.term())),
vsc);
} else {
const Type* tt = TypeTable::most_specific(TermTable::type(b.var()),
TermTable::type(b.term()));
if (tt == 0) {
RCObject::ref(cd_set);
RCObject::destructive_deref(cd_set);
return 0;
}
cd_set = new Chain<StepVariable>(std::make_pair(b.term().as_variable(),
b.term_id()),
cd_set);
vsc = new Chain<Varset>(Varset(0, cd_set, 0, *tt), vsc);
}
return &vsc->head;
} else {
if (reverse) {
const Chain<StepVariable>* ncd_set =
new Chain<StepVariable>(std::make_pair(b.var(), b.var_id()), 0);
if (b.term().object()) {
Object obj = b.term().as_object();
vsc = new Chain<Varset>(Varset(&obj, 0, ncd_set,
TermTable::type(b.term())),
vsc);
} else {
Variable var = b.term().as_variable();
const Chain<StepVariable>* cd_set =
new Chain<StepVariable>(std::make_pair(var, b.term_id()), 0);
vsc = new Chain<Varset>(Varset(0, cd_set, ncd_set,
TermTable::type(b.term())),
vsc);
}
return &vsc->head;
} else { /* !reverse */
if (b.term().object()) {
return 0;
} else {
Variable var = b.term().as_variable();
const Chain<StepVariable>* cd_set =
new Chain<StepVariable>(std::make_pair(b.var(), b.var_id()), 0);
const Chain<StepVariable>* ncd_set =
new Chain<StepVariable>(std::make_pair(var, b.term_id()), 0);
vsc = new Chain<Varset>(Varset(0, cd_set, ncd_set,
TermTable::type(b.var())),
vsc);
return &vsc->head;
}
}
}
}
private:
/* Pointer to the constant of this varset, or 0. */
const Object* constant_;
/* The codesignation list. */
const Chain<StepVariable>* cd_set_;
/* The non-codesignation list. */
const Chain<StepVariable>* ncd_set_;
/* The most specific type of any variable in this set. */
Type type_;
};
/* Returns the varset containing the given object, or 0 if none do. */
static const Varset* find_varset(const Chain<Varset>* varsets,
const Object& obj) {
for (const Chain<Varset>* vsc = varsets; vsc != 0; vsc = vsc->tail) {
const Varset& vs = vsc->head;
if (vs.includes(obj)) {
return &vs;
}
}
return 0;
}
/* Returns the varset containing the given variable, or 0 if none do. */
static const Varset* find_varset(const Chain<Varset>* varsets,
const Variable& var, size_t step_id) {
for (const Chain<Varset>* vsc = varsets; vsc != 0; vsc = vsc->tail) {
const Varset& vs = vsc->head;
if (vs.includes(var, step_id)) {
return &vs;
}
}
return 0;
}
/* Returns the varset containing the given term, or 0 if none do. */
static const Varset* find_varset(const Chain<Varset>* varsets,
const Term& term, size_t step_id) {
if (term.object()) {
return find_varset(varsets, term.as_object());
} else {
return find_varset(varsets, term.as_variable(), step_id);
}
}
/* ====================================================================== */
/* StepDomain */
/*
* A step domain.
*/
struct StepDomain {
/* Constructs a step domain. */
StepDomain(size_t id, const std::vector<Variable>& parameters,
const ActionDomain& domain)
: id_(id), parameters_(¶meters), domain_(&domain) {
ActionDomain::register_use(domain_);
}
/* Constructs a step domain. */
StepDomain(const StepDomain& sd)
: id_(sd.id_), parameters_(sd.parameters_), domain_(sd.domain_) {
ActionDomain::register_use(domain_);
}
/* Deletes this step domain. */
~StepDomain() {
ActionDomain::unregister_use(domain_);
}
/* Returns the step id. */
size_t id() const { return id_; }
/* Returns the step parameters. */
const std::vector<Variable>& parameters() const { return *parameters_; }
/* Returns the parameter domains. */
const ActionDomain& domain() const { return *domain_; }
/* Returns the index of the variable in this step domain, or -1 if
the variable is not included. */
int index_of(const Variable& var) const {
std::vector<Variable>::const_iterator vi =
find(parameters().begin(), parameters().end(), var);
return (vi != parameters().end()) ? vi - parameters().begin() : -1;
}
/* Checks if this step domain includes the given object in the given
column. */
bool includes(const Object& obj, size_t column) const {
for (TupleList::const_iterator ti = domain().tuples().begin();
ti != domain().tuples().end(); ti++) {
if ((**ti)[column] == obj) {
return true;
}
}
return false;
}
/* Returns the set of objects from the given column. */
const NameSet& projection(size_t column) const {
return domain().projection(column);
}
/* Returns the size of the projection of the given column. */
const size_t projection_size(size_t column) const {
return domain().projection_size(column);
}
/* Returns a domain where the given column has been restricted to
the given object, or 0 if this would leave an empty domain. */
const StepDomain* restrict(const Chain<StepDomain>*& sdc,
const Object& obj, size_t column) const {
const ActionDomain* ad = domain().restrict(obj, column);
if (ad == 0) {
return 0;
} else if (ad == &domain()) {
return this;
} else {
sdc = new Chain<StepDomain>(StepDomain(id(), parameters(), *ad), sdc);
return &sdc->head;
}
}
/* Returns a domain where the given column has been restricted to
the given set of objects, or 0 if this would leave an empty
domain. */
const StepDomain* restrict(const Chain<StepDomain>*& sdc,
const NameSet& names, size_t column) const {
const ActionDomain* ad = domain().restrict(names, column);
if (ad == 0) {
return 0;
} else if (ad == &domain()) {
return this;
} else {
sdc = new Chain<StepDomain>(StepDomain(id(), parameters(), *ad), sdc);
return &sdc->head;
}
}
/* Returns a domain where the given column exclues the given object,
or 0 if this would leave an empty domain. */
const StepDomain* exclude(const Chain<StepDomain>*& sdc,
const Object& obj, size_t column) const {
const ActionDomain* ad = domain().exclude(obj, column);
if (ad == 0) {
return 0;
} else if (ad == &domain()) {
return this;
} else {
sdc = new Chain<StepDomain>(StepDomain(id(), parameters(), *ad), sdc);
return &sdc->head;
}
}
/* Prints this object on the given stream. */
void print(std::ostream& os) const;
private:
/* The id of the step. */
size_t id_;
/* Parameters of the step. */
const std::vector<Variable>* parameters_;
/* Domain of the parameters. */
const ActionDomain* domain_;
};
/* Prints this object on the given stream. */
void StepDomain::print(std::ostream& os) const {
os << "<";
for (std::vector<Variable>::const_iterator vi = parameters().begin();
vi != parameters().end(); vi++) {
if (vi != parameters().begin()) {
os << ' ';
}
os << *vi << '(' << id() << ')';
}
os << "> in ";
domain().print(os);
}
/* Returns the step domain containing the given variable and the
column of the variable, or 0 no step domain contains the
variable. */
static std::pair<const StepDomain*, size_t>
find_step_domain(const Chain<StepDomain>* step_domains,
const Variable& var, size_t step_id) {
if (step_id > 0) {
for (const Chain<StepDomain>* sd = step_domains; sd != 0; sd = sd->tail) {
const StepDomain& step_domain = sd->head;
if (step_domain.id() == step_id) {
int column = step_domain.index_of(var);
if (column >= 0) {
return std::make_pair(&step_domain, column);
} else {
break;
}
}
}
}
return std::pair<const StepDomain*, size_t>(0, 0);
}
/* ====================================================================== */
/* ActionDomain */
/* Constructs an action domain with a single tuple. */
ActionDomain::ActionDomain(const std::vector<Object>& tuple) : ref_count_(0) {
add(tuple);
}
/* Deletes this action domain. */
ActionDomain::~ActionDomain() {
for (ProjectionMap::const_iterator pi = projections_.begin();
pi != projections_.end(); pi++) {
delete (*pi).second;
}
}
/* Adds a tuple to this domain. */
void ActionDomain::add(const std::vector<Object>& tuple) {
tuples_.push_back(&tuple);
}
/* Returns the set of names from the given column. */
const NameSet& ActionDomain::projection(size_t column) const {
ProjectionMap::const_iterator pi = projections_.find(column);
if (pi != projections_.end()) {
return *(*pi).second;
} else {
NameSet* projection = new NameSet();
for (TupleList::const_iterator ti = tuples().begin();
ti != tuples().end(); ti++) {
const std::vector<Object>& tuple = **ti;
projection->insert(tuple[column]);
}
projections_.insert(std::make_pair(column, projection));
return *projection;
}
}
/* Returns the size of the projection of the given column. */
const size_t ActionDomain::projection_size(size_t column) const {
return projection(column).size();
}
/* Returns a domain where the given column has been restricted to
the given object, or 0 if this would leave an empty domain. */
const ActionDomain* ActionDomain::restrict(const Object& obj,
size_t column) const {
ActionDomain* new_domain = 0;
for (TupleList::const_iterator ti = tuples().begin();
ti != tuples().end(); ti++) {
const std::vector<Object>& tuple = **ti;
if (tuple[column] == obj) {
if (new_domain == 0) {
new_domain = new ActionDomain(tuple);
} else {
new_domain->add(tuple);
}
}
}
if (new_domain != 0 && new_domain->size() == size()) {
ActionDomain::register_use(new_domain);
ActionDomain::unregister_use(new_domain);
return this;
} else {
return new_domain;
}
}
/* Returns a domain where the given column has been restricted to
the given set of names, or 0 if this would leave an empty
domain. */
const ActionDomain* ActionDomain::restrict(const NameSet& names,
size_t column) const {
ActionDomain* new_domain = 0;
for (TupleList::const_iterator ti = tuples().begin();
ti != tuples().end(); ti++) {
const std::vector<Object>& tuple = **ti;
if (names.find(tuple[column]) != names.end()) {
if (new_domain == 0) {
new_domain = new ActionDomain(tuple);
} else {
new_domain->add(tuple);
}
}
}
if (new_domain != 0 && new_domain->size() == size()) {
ActionDomain::register_use(new_domain);
ActionDomain::unregister_use(new_domain);
return this;
} else {
return new_domain;
}
}
/* Returns a domain where the given column exclues the given object,
or 0 if this would leave an empty domain. */
const ActionDomain* ActionDomain::exclude(const Object& obj,
size_t column) const {
ActionDomain* new_domain = 0;
for (TupleList::const_iterator ti = tuples().begin();
ti != tuples().end(); ti++) {
const std::vector<Object>& tuple = **ti;
if (tuple[column] != obj) {
if (new_domain == 0) {
new_domain = new ActionDomain(tuple);
} else {
new_domain->add(tuple);
}
}
}
if (new_domain != 0 && new_domain->size() == size()) {
ActionDomain::register_use(new_domain);
ActionDomain::unregister_use(new_domain);
return this;
} else {
return new_domain;
}
}
/* Prints this object on the given stream. */
void ActionDomain::print(std::ostream& os) const {
os << '{';
for (TupleList::const_iterator ti = tuples().begin();
ti != tuples().end(); ti++) {
if (ti != tuples().begin()) {
os << ' ';
}
os << '<';
const std::vector<Object>& tuple = **ti;
for (std::vector<Object>::const_iterator ni = tuple.begin();
ni != tuple.end(); ni++) {
if (ni != tuple.begin()) {
os << ' ';
}
os << *ni;
}
os << '>';
}
os << '}';
}
/* ====================================================================== */
/* Bindings */
/* Empty bindings. */
const Bindings Bindings::EMPTY = Bindings();
/* Checks if the given formulas can be unified. */
bool Bindings::unifiable(const Literal& l1, size_t id1,
const Literal& l2, size_t id2) {
BindingList dummy;
return unifiable(dummy, l1, id1, l2, id2);
}
/* Checks if the given formulas can be unified; the most general
unifier is added to the provided substitution list. */
bool Bindings::unifiable(BindingList& mgu,
const Literal& l1, size_t id1,
const Literal& l2, size_t id2) {
return EMPTY.unify(mgu, l1, id1, l2, id2);
}
/* Constructs an empty binding collection. */
Bindings::Bindings()
: varsets_(0), high_step_(0), step_domains_(0), ref_count_(1) {
}
/* Constructs a binding collection. */
Bindings::Bindings(const Chain<Varset>* varsets, size_t high_step,
const Chain<StepDomain>* step_domains)
: varsets_(varsets), high_step_(high_step), step_domains_(step_domains),
ref_count_(0) {
RCObject::ref(varsets_);
RCObject::ref(step_domains_);
}
/* Deletes this binding collection. */
Bindings::~Bindings() {
RCObject::destructive_deref(varsets_);
RCObject::destructive_deref(step_domains_);
}
/* Returns the binding for the given term, or the term itself if it is
not bound to a single object. */
Term Bindings::binding(const Term& term, size_t step_id) const {
if (term.variable()) {
const Varset* vs =
((step_id <= high_step_)
? find_varset(varsets_, term.as_variable(), step_id) : 0);
if (vs != 0 && vs->constant() != 0) {
return *vs->constant();
}
}
return term;
}
/* Returns the domain for the given step variable. */
const NameSet& Bindings::domain(const Variable& var, size_t step_id,
const Problem& problem) const {
std::pair<const StepDomain*, size_t> sd =
find_step_domain(step_domains_, var, step_id);
if (sd.first != 0) {
return sd.first->projection(sd.second);
} else {
const std::vector<Object>& objects =
problem.terms().compatible_objects(TermTable::type(var));
NameSet* names = new NameSet();
names->insert(objects.begin(), objects.end());
const Varset* vs =
(step_id <= high_step_) ? find_varset(varsets_, var, step_id) : 0;
if (vs != 0) {
for (const Chain<StepVariable>* vc = vs->ncd_set();
vc != 0; vc = vc->tail) {
const StepVariable& sv = vc->head;
const Varset* vs2 = ((sv.second <= high_step_)
? find_varset(varsets_, sv.first, sv.second) : 0);
if (vs2 != 0 && vs2->constant() != 0) {
names->erase(*vs2->constant());
}
}
}
return *names;
}
}
/* Checks if one of the given formulas is the negation of the other,
and the atomic formulas can be unified. */
bool Bindings::affects(const Literal& l1, size_t id1,
const Literal& l2, size_t id2) const {
BindingList dummy;
return affects(dummy, l1, id1, l2, id2);
}
/* Checks if one of the given formulas is the negation of the other,
and the atomic formulas can be unified; the most general unifier
is added to the provided substitution list. */
bool Bindings::affects(BindingList& mgu, const Literal& l1, size_t id1,
const Literal& l2, size_t id2) const {
const Negation* negation = dynamic_cast<const Negation*>(&l1);
if (negation != 0) {
return unify(mgu, l2, id2, negation->atom(), id1);
} else {
negation = dynamic_cast<const Negation*>(&l2);
if (negation != 0) {
return unify(mgu, negation->atom(), id2, l1, id1);
} else {
return false;
}
}
}
/* Checks if the given formulas can be unified. */
bool Bindings::unify(const Literal& l1, size_t id1,
const Literal& l2, size_t id2) const {
BindingList dummy;
return unify(dummy, l1, id1, l2, id2);
}
/* Checks if the given formulas can be unified; the most general
unifier is added to the provided substitution list. */
bool Bindings::unify(BindingList& mgu, const Literal& l1, size_t id1,
const Literal& l2, size_t id2) const {
if (l1.id() > 0 && l2.id() > 0) {
/* Both literals are fully instantiated. */
return &l1 == &l2;
} else if (typeid(l1) != typeid(l2)) {
/* Not the same type of literal. */
return false;
} else if (l1.predicate() != l2.predicate()) {
/* The predicates do not match. */
return false;
} else if (l1.id() > 0 || l2.id() > 0) {
/* One of the literals is fully instantiated. */
const Literal* ll;
const Literal* lg;
size_t idl;
if (l1.id() > 0) {
ll = &l2;
lg = &l1;
idl = id2;
} else {
ll = &l1;
lg = &l2;
idl = id1;
}
std::map<Variable, Term> bind;
size_t n = ll->arity();
for (size_t i = 0; i < n; i++) {
const Term& term1 = ll->term(i);
Object obj2 = lg->term(i).as_object();
if (term1.object()) {
if (term1 != obj2) {
return false;
}
} else {
Variable var1 = term1.as_variable();
std::map<Variable, Term>::const_iterator b = bind.find(var1);
if (b != bind.end()) {
if ((*b).second != obj2) {
return false;
}
} else {
Term bt = binding(term1, idl);
if (bt.object()) {
if (bt != obj2) {
return false;
}
} else {
if (!TypeTable::subtype(TermTable::type(obj2),
TermTable::type(term1))) {
return false;
}
mgu.push_back(Binding(var1, idl, obj2, 0, true));
}
bind.insert(std::make_pair(var1, obj2));
}
}
}
} else {
/*
* Try to unify the terms of the literals.
*/
/* Number of terms for the first literal. */
size_t n = l1.arity();
for (size_t i = 0; i < n; i++) {
/*
* Try to unify a pair of terms.
*/
const Term& term1 = l1.term(i);
const Term& term2 = l2.term(i);
if (term1.object()) {
/* The first term is an object. */
if (term2.object()) {
/*
* Both terms are objects.
*/
if (term1 != term2) {
/* The two terms are different objects. */
return false;
}
} else {
/*
* The first term is an object and the second is a variable.
*/
if (!TypeTable::subtype(TermTable::type(term1),
TermTable::type(term2))) {
/* Incompatible term types. */
return false;
}
mgu.push_back(Binding(term2.as_variable(), id2, term1, 0, true));
}
} else {
/*
* The first term is a variable.
*/
if (term2.object()) {
if (!TypeTable::subtype(TermTable::type(term2),
TermTable::type(term1))) {
/* Incompatible term types. */
return false;
}
} else if (!TypeTable::compatible(TermTable::type(term1),
TermTable::type(term2))) {
/* Incompatible term types. */
return false;
}
mgu.push_back(Binding(term1.as_variable(), id1, term2, id2, true));
}
}
}
if (add(mgu, true) == 0) {
/* Unification is inconsistent with current bindings. */
return false;
} else {
/* Successful unification. */
return true;
}
}
/* Checks if the given equality is consistent with the current
bindings. */
bool Bindings::consistent_with(const Equality& eq, size_t step_id) const {
size_t var_id = eq.step_id1(step_id);
size_t term_id = eq.step_id2(step_id);
const Varset* vs =
(term_id <= high_step_) ? find_varset(varsets_, eq.term(), term_id) : 0;
if (vs == 0 || vs->includes(eq.variable(), var_id)) {
return true;
} else if (vs->excludes(eq.variable(), var_id)) {
return false;
} else if (vs->constant() != 0) {
std::pair<const StepDomain*, size_t> sd =
find_step_domain(step_domains_, eq.variable(), var_id);
if (sd.first != 0) {
return sd.first->includes(*vs->constant(), sd.second);
}
}
return true;
}
/* Checks if the given inequality is consistent with the current
bindings. */
bool Bindings::consistent_with(const Inequality& neq, size_t step_id) const {
size_t var_id = neq.step_id1(step_id);
size_t term_id = neq.step_id2(step_id);
const Varset* vs =
(term_id <= high_step_) ? find_varset(varsets_, neq.term(), term_id) : 0;
return (vs == 0
|| !vs->includes(neq.variable(), var_id)
|| vs->excludes(neq.variable(), var_id));
}
/* Adds bindings to the list as determined by difference between the
given step domains */
static void add_domain_bindings(BindingList& bindings,
const StepDomain& old_sd,
const StepDomain& new_sd,
size_t ex_column = std::numeric_limits<unsigned int>::max()) {
for (size_t c = 0; c < old_sd.parameters().size(); c++) {
if (c != ex_column && new_sd.projection_size(c) == 1
&& old_sd.projection_size(c) > 1) {
bindings.push_back(Binding(new_sd.parameters()[c], new_sd.id(),
*new_sd.projection(c).begin(), 0, true));
}
}
}
/* Returns the binding collection obtained by adding the given
bindings to this binding collection, or 0 if the new bindings
are inconsistent with the current. */
const Bindings* Bindings::add(const BindingList& new_bindings,
bool test_only) const {
if (new_bindings.empty()) {
/* No new bindings. */
return this;
}
/* Varsets for new binding collection */
const Chain<Varset>* varsets = varsets_;
/* Highest step id of variable in varsets. */
size_t high_step = high_step_;
/* Variables above previous high step. */
VariableSet high_step_vars;
/* Step domains for new binding collection */
const Chain<StepDomain>* step_domains = step_domains_;
BindingList new_binds(new_bindings);
/*
* Add new bindings one at a time.
*/
for (size_t i = 0; i < new_binds.size(); i++) {
/*
* N.B. Make a copy of the binding instead of just saving a
* reference, because new_binds can be expanded in the loop in
* which case the reference may become invalid.
*/
const Binding bind = new_binds[i];
if (bind.equality()) {
/*
* Adding equality binding.
*/
/* Varset for variable. */
const Varset* vs1;
StepVariable sv(bind.var(), bind.var_id());
if (bind.var_id() <= high_step_
|| high_step_vars.find(sv) != high_step_vars.end()) {
vs1 = find_varset(varsets, bind.var(), bind.var_id());
} else {
if (bind.var_id() > high_step) {