forked from anubhav-gres/VAL-4.2.08
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimSupport.cpp
1552 lines (1332 loc) · 36.4 KB
/
TimSupport.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
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 2008, Strathclyde Planning Group,
* Department of Computer and Information Sciences,
* University of Strathclyde, Glasgow, UK
* http://planning.cis.strath.ac.uk/
*
* Maria Fox, Richard Howey and Derek Long - VAL
* Stephen Cresswell - PDDL Parser
*
* This file is part of VAL, the PDDL validator.
*
* VAL 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.
*
* VAL 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 VAL. If not, see <http://www.gnu.org/licenses/>.
*
************************************************************************/
#include "FastEnvironment.h"
#include "TimSupport.h"
#include "ptree.h"
#include "Partitions.h"
#include <numeric>
#include <set>
using std::greater;
using std::max_element;
using std::min_element;
using std::set;
using std::copy;
using std::inserter;
#include <assert.h>
namespace TIM {
using namespace VAL;
ostream & operator<<(ostream & o, const Property & p)
{
p.write(o);
return o;
};
int getId(parameter_symbol* s)
{
const IDsymbol<var_symbol> * i = dynamic_cast<const IDsymbol<var_symbol>*>(s);
if(!i) return -1;
return i->getId();
};
template<class T>
TIMpredSymbol * findPred(T * g);
template<>
TIMpredSymbol * findPred(simple_effect * g)
{
return static_cast<TIMpredSymbol*>(const_cast<pred_symbol *>(g->prop->head));
};
template<>
TIMpredSymbol * findPred(derivation_rule * g)
{
return static_cast<TIMpredSymbol*>(const_cast<pred_symbol *>(g->get_head()->head));
};
bool PropertySpace::contains(TIMobjectSymbol * t) const
{
return binary_search(objects.begin(),objects.end(),t);
};
struct recordObjectIn {
PropertySpace * ps;
bool added;
recordObjectIn(PropertySpace * p) : ps(p), added(false) {};
void operator()(TIMobjectSymbol * o)
{
if(!ps->contains(o))
{
o->addIn(ps);
ps->add(o);
added = true;
};
};
operator bool() {return added;};
};
var_symbol * getAt(var_symbol_list * ps,int v)
{
var_symbol_list::iterator i = ps->begin();
for(;v > 0;--v,++i);
return *i;
};
parameter_symbol * getAt(parameter_symbol_list * ps,int v)
{
parameter_symbol_list::iterator i = ps->begin();
for(;v > 0;--v,++i);
return *i;
};
TransitionRule::TransitionRule(TIMAnalyser * t,operator_ * o,int v,
PropertyState * e,PropertyState * l,PropertyState * r,
opType ty) :
tan(t), op(o), drv(0), opt(ty), var(v), enablers(e), lhs(l), rhs(r),
objects(var>=0?tan->getTC().range(getAt(op->parameters,var)):vector<const_symbol*>())
{};
TransitionRule::TransitionRule(TIMAnalyser * t,derivation_rule * o,int v,
PropertyState * e,PropertyState * l,PropertyState * r,
opType ty) :
tan(t), op(0), drv(o), opt(ty), var(v), enablers(e), lhs(l), rhs(r),
objects(var>=0?tan->getTC().range(getAt(drv->get_head()->args,var)):vector<const_symbol*>())
{};
bool TransitionRule::applicableIn(const PropertyState * ps) const
{
return std::includes(ps->begin(),ps->end(),lhs->begin(),lhs->end());
};
struct checkNotApplicable {
TIMobjectSymbol * tos;
checkNotApplicable(const_symbol * c) :
tos(static_cast<TIMobjectSymbol*>(c))
{};
bool operator()(Property * p)
{
return find_if(p->begin(),p->end(),
not1(bind2nd(mem_fun(&PropertySpace::contains),tos)))
!= p->end();
};
};
class RuleObjectIterator {
private:
TransitionRule * trule;
vector<const_symbol *>::iterator obit;
void findValid()
{
while(obit != trule->objects.end())
{
if(find_if(trule->enablers->begin(),trule->enablers->end(),
checkNotApplicable(*obit)) == trule->enablers->end()) break;
++obit;
};
};
public:
RuleObjectIterator(TransitionRule * tr) :
trule(tr), obit(trule->objects.begin())
{
findValid();
};
void toEnd()
{
obit = trule->objects.end();
};
RuleObjectIterator & operator++()
{
++obit;
findValid();
return *this;
};
TIMobjectSymbol * operator*()
{
return static_cast<TIMobjectSymbol*>(*obit);
};
bool operator==(const RuleObjectIterator & roi) const
{
return trule == roi.trule && obit == roi.obit;
};
bool operator!=(const RuleObjectIterator & roi) const
{
return trule != roi.trule || obit != roi.obit;
};
};
RuleObjectIterator TransitionRule::endEnabledObjects()
{
RuleObjectIterator i(this);
i.toEnd();
return i;
};
RuleObjectIterator TransitionRule::beginEnabledObjects()
{
return RuleObjectIterator(this);
};
struct extendWithIncrRule {
PropertySpace * ps;
bool extended;
extendWithIncrRule(PropertySpace * p) : ps(p), extended(false) {};
void operator()(TransitionRule * tr)
{
if(tr->isIncreasing())
{
extended = for_each(tr->beginEnabledObjects(),tr->endEnabledObjects(),
recordObjectIn(ps));
};
};
operator bool() {return extended;};
};
template<class T>
T isSuper(const PropertyState * p,T b,T e)
{
while(b != e)
{
if(std::includes(p->begin(),p->end(),(*b)->begin(),(*b)->end())) return b;
++b;
};
return b;
};
struct extendWithStateRule {
set<PropertyState *> & got;
list<PropertyState *> & toExtend;
PropertyState * prop;
extendWithStateRule(set<PropertyState*> & s,list<PropertyState*> & l) :
got(s), toExtend(l), prop(toExtend.empty()?0:*(toExtend.begin())) {};
void operator()(TransitionRule * tr)
{
if(!prop) return;
PropertyState * p = tr->tryRule(prop);
if(p && got.find(p) == got.end())
{
set<PropertyState *>::const_iterator i = isSuper(p,got.begin(),got.end());
if(i != got.end())
{
OUTPUT cout << *p << " is a superset of a state we already have: " <<
**i << "\n";
}
else
{
got.insert(p);
toExtend.push_back(p);
};
};
};
void next()
{
toExtend.pop_front();
prop = toExtend.empty()?0:*(toExtend.begin());
};
operator bool() {return !toExtend.empty();};
};
bool PropertySpace::extend()
{
if(!isStateValued)
{
// cout << "\nExtending attribute space...\n";
// write(cout);
// We add to this space every object that is currently in all of the spaces for
// enabling properties of any increasing rule in this space.
bool b = for_each(rules.begin(),rules.end(),extendWithIncrRule(this));
if(b) sort(objects.begin(),objects.end());
return b;
}
else
{
// cout << "\nExtending state space...\n";
// For each state in the space we need to apply each rule (if it can be applied) and
// add the corresponding new state to the space. We really should confirm that we only
// use states for enabled objects, but maybe that's a refinement we can save until
// later.
//
// Basic pattern: for each state and rule pair, if rule applies then produce new state.
// Key issue: avoid retesting states with rules. One way to do this would be to take
// each state in turn and extend it with all rules, adding new states to a list as they
// are generated. As they are added to the list they can also be added to the internal
// set of states, so that membership can be checked fast.
list<PropertyState *> toExtend;
copy(states.begin(),states.end(),inserter(toExtend,toExtend.begin()));
extendWithStateRule ewsr(this->states,toExtend);
while(for_each(rules.begin(),rules.end(),ewsr))
ewsr.next();
// write(cout);
return false;
};
};
template<>
TIMpredSymbol * findPred(simple_goal * g)
{
return static_cast<TIMpredSymbol*>(const_cast<pred_symbol *>(g->getProp()->head));
};
struct process_argument {
TIMAnalyser * ta;
TIMpredSymbol * timps;
int arg;
proposition * prp;
virtual ~process_argument() {};
template<class T>
process_argument(TIMAnalyser * t,T * g,proposition * p) :
ta(t), timps(findPred<T>(g)), arg(0), prp(p) {};
template<class T>
process_argument(TIMAnalyser * t,T * g) :
ta(t), timps(findPred<T>(g)), arg(0), prp(0) {};
virtual void operator()(parameter_symbol * p) = 0;
};
struct process_argument_in_goal : public process_argument {
process_argument_in_goal(TIMAnalyser * t,simple_goal * g) :
process_argument(t,g)
{};
virtual ~process_argument_in_goal() {};
virtual void operator()(parameter_symbol * p)
{
Property * prop = timps->property(arg);
++arg;
ta->insertPre(getId(p),prop);
};
};
struct process_argument_in_effect : public process_argument {
process_argument_in_effect(TIMAnalyser * t,simple_effect * g) :
process_argument(t,g)
{};
virtual ~process_argument_in_effect() {};
virtual void operator()(parameter_symbol * p)
{
Property * prop = timps->property(arg);
++arg;
ta->insertEff(getId(p),prop);
};
};
struct process_argument_in_derivation_effect : public process_argument {
process_argument_in_derivation_effect(TIMAnalyser * t,derivation_rule * g) :
process_argument(t,g)
{};
virtual ~process_argument_in_derivation_effect() {};
virtual void operator()(parameter_symbol * p)
{
Property * prop = timps->property(arg);
++arg;
ta->insertEff(getId(p),prop);
};
};
struct process_constant_in_goal : public process_argument {
process_constant_in_goal(TIMAnalyser * t,simple_goal * g) :
process_argument(t,g)
{};
virtual ~process_constant_in_goal() {};
virtual void operator()(parameter_symbol * p)
{
Property * prop = timps->property(arg);
++arg;
ta->insertGoal(p,prop);
};
};
struct process_constant_in_initial : public process_argument {
process_constant_in_initial(TIMAnalyser * t,simple_effect * g) :
process_argument(t,g,g->prop)
{};
virtual ~process_constant_in_initial() {};
virtual void operator()(parameter_symbol * p)
{
Property * prop = timps->property(arg);
++arg;
ta->insertInitial(p,prop,prp);
};
};
void TIMAnalyser::visit_simple_goal(simple_goal * p)
{
if(finally)
{
for_each(p->getProp()->args->begin(),p->getProp()->args->end(),
process_constant_in_goal(this,p));
}
else
{
for_each(p->getProp()->args->begin(),p->getProp()->args->end(),
process_argument_in_goal(this,p));
};
};
struct notIn {
PropertySpace * ps;
notIn(PropertySpace * p) : ps(p) {};
bool operator()(Property * p)
{
return (p->begin()==p->end()) || *(p->begin()) != ps;
};
};
bool Property::matches(const extended_pred_symbol * eps,pddl_type * pt)
{
if(EPS(predicate)->getParent() != eps->getParent()) return false;
//cout << "A: " << *pt << "\n";
//cout << "B: " << *eps << "\n";
Types::const_iterator tcItr = eps->tcBegin()+posn;
if (tcItr == eps->tcEnd()) {
std::cerr << "A problem has been encountered with your domain/problem file.\n";
std::cerr << "-------------------------------------------------------------\n";
std::cerr << "Unfortunately, a bug has been encountered in your domain and problem file,\n";
std::cerr << "and the planner has to terminate. The predicate:\n\n";
std::cerr << "\t" << eps->getName() << "\n\n";
int realArgs = 0;
{
Types::const_iterator tcsItr = eps->tcBegin();
Types::const_iterator tcsEnd = eps->tcEnd();
for (; tcsItr != tcsEnd; ++tcsItr) ++realArgs;
}
std::cerr << "...takes " << realArgs << " argument";
if (realArgs != 1) std::cerr << "s";
std::cerr << ", but has been given at least " << posn + 1 << ".\n";
exit(0);
}
if(*tcItr)
{
// cout << "C: " << **(eps->tcBegin()+posn) << "\n";
}
else
{
// cout << "C: ***\n";
return false;
};
return (pt == (*tcItr)->type);
};
bool Property::equivalent(const Property * p) const
{
if(this==p) return true;
if(posn != p->posn ||
EPS(predicate)->getParent() != EPS(p->predicate)->getParent())
return false;
return true;
};
struct notMatchFor {
pddl_type * pt;
Property * prop;
notMatchFor(pddl_type * p,Property * pr) : pt(p), prop(pr)
{};
bool operator()(extended_pred_symbol * eps)
{
return !(prop->matches(eps,pt));
};
};
struct toProp {
int arg;
toProp(int a) : arg(a) {};
Property * operator()(extended_pred_symbol * eps)
{
return static_cast<TIMpredSymbol*>(eps)->property(arg);
};
};
void TIMAnalyser::close(set<Property*> & seed,const pddl_type * pt)
{
bool adding = true;
while(adding)
{
adding = false;
PropertyState * ps = PropertyState::getPS(this,pt,seed.begin(),seed.end());
for(TRules::const_iterator r = trules.begin();r != trules.end();++r)
{
if((*r)->applicableIn(ps))
{
copy((*r)->getRHS()->begin(),(*r)->getRHS()->end(),
inserter(seed,seed.begin()));
adding |= (seed.size() > ps->size());
};
};
};
};
vector<Property *> Property::matchers()
{
vector<extended_pred_symbol *> v;
holding_pred_symbol * h = EPS(predicate)->getParent();
std::remove_copy_if(h->pBegin(),h->pEnd(),inserter(v,v.begin()),
notMatchFor((*((EPS(predicate)->tBegin())+posn))->type,this));
vector<Property *> ps;
transform(v.begin(),v.end(),inserter(ps,ps.begin()),toProp(posn));
return ps;
};
struct setMatchers {
vector<Property *> & ps;
setMatchers(vector<Property *> & p) : ps(p) {};
void operator()(Property * p)
{
vector<Property *> props = p->matchers();
copy(props.begin(),props.end(),inserter(ps,ps.end()));
};
};
void TIMobjectSymbol::distributeStates(TIMAnalyser * tan)
{
vector<Property *>::iterator s,e,m;
vector<Property *> matchers;
for_each(initial.begin(),initial.end(),setMatchers(matchers));
s = matchers.begin();
e = matchers.end();
while(s != e)
{
if((*s)->begin() == (*s)->end())
{
// State belonging to no property space.
// This should not happen...but, see notes.
++s;
continue;
};
PropertySpace * p = *((*s)->begin()); // Assumes only one space at this stage
p->add(this);
m = std::partition(s,e,notIn(p));
std::sort(m,e);
PropertyState * ps = PropertyState::getPS(tan,type,m,e);
p->add(ps);
e = m;
};
};
Property * Property::getBaseProperty(const pddl_type * pt) const
{
if(!EPS(predicate)->getParent()) return const_cast<Property*>(this);
TIMpredSymbol * tps =
static_cast<TIMpredSymbol*>(EPS(predicate)->getParent()->
find(predicate,makeTT(EPS(predicate)->tBegin(),posn,pt),
makeTT(EPS(predicate)->tEnd(),0,0)));
return tps->property(posn);
};
ostream & operator <<(ostream & o,const TIMobjectSymbol & t)
{
t.write(o);
return o;
};
void TIMAnalyser::visit_simple_effect(simple_effect * p)
{
if(initially)
{
for_each(p->prop->args->begin(),p->prop->args->end(),
process_constant_in_initial(this,p));
}
else
{
for_each(p->prop->args->begin(),p->prop->args->end(),
process_argument_in_effect(this,p));
};
};
void TIMAnalyser::visit_simple_derivation_effect(derivation_rule * p)
{
for_each(p->get_head()->args->begin(),p->get_head()->args->end(),
process_argument_in_derivation_effect(this,p));
};
void TIMAnalyser::insertPre(int v,Property * p)
{
if(v<0)
{
OUTPUT cout << "Property for a constant\n";
return;
};
if(overall)
{
MEX(op)->addInvariant(p,v);
return;
}
else
{
if (op) MEX(op)->addCondition(p,v,isDurative?(atStart?START:END):INSTANT);
};
if(!rules[v]) {
if (op) rules[v] = new ProtoRule(this,op,v,isDurative?(atStart?START:END):INSTANT);
if (drv) rules[v] = new ProtoRule(this,drv,v,isDurative?(atStart?START:END):INSTANT);
}
rules[v]->insertPre(p);
};
void TIMAnalyser::insertEff(int v,Property * p)
{
if(v<0)
{
OUTPUT cout << "Property for a constant\n";
return;
};
if(!rules[v]) {
if (op) rules[v] = new ProtoRule(this,op,v,isDurative?(atStart?START:END):INSTANT);
if (drv) rules[v] = new ProtoRule(this,drv,v,isDurative?(atStart?START:END):INSTANT);
}
if(adding)
{
rules[v]->insertAdd(p);
}
else
{
rules[v]->insertDel(p);
};
};
void TIMAnalyser::insertGoal(parameter_symbol * c,Property * p)
{
TIMobjectSymbol * cc = dynamic_cast<TIMobjectSymbol *>(c);
cc->addFinal(p);
};
void TIMAnalyser::insertInitial(parameter_symbol * c,Property * p,proposition * prp)
{
TIMobjectSymbol * cc = dynamic_cast<TIMobjectSymbol *>(c);
cc->addInitial(p,prp);
};
ostream & operator<<(ostream & o,const PropertyState & p)
{
p.write(o);
return o;
};
ostream & operator<<(ostream & o,const TransitionRule & tr)
{
tr.write(o);
return o;
};
void ProtoRule::addRules(TRules & trules)
{
sort(enablers.begin(),enablers.end());
sort(adds.begin(),adds.end());
sort(dels.begin(),dels.end());
vector<Property *> es;
set_difference(enablers.begin(),enablers.end(),dels.begin(),dels.end(),
inserter(es,es.begin()));
vector<Property *> is;
set_intersection(dels.begin(),dels.end(),adds.begin(),adds.end(),
inserter(is,is.begin()));
// Problem here: if we have a constant we still need to know what it is...
parameter_symbol * v = (var>=0?(op ? getAt(op->parameters,var) : getAt(drv->get_head()->args,var)):0);
vector<const pddl_type *> types(tan->getTC().leaves(v->type));
if(types.empty()) types.push_back(v->type);
if(is.size()>1 || ((is.size()==1) && (dels.size()>1 || adds.size()>1)))
{
assert(op);
//don't worry, cannot ever be true for derivation rules: have no delete effects, so is empty, dels empty....
for(vector<Property*>::iterator i = is.begin();i != is.end();++i)
{
Property * p = *i;
if(find(enablers.begin(),enablers.end(),p)!=enablers.end())
{
enablers.erase(find(enablers.begin(),enablers.end(),p));
};
// Else we are deleting a non-precondition - which could be OK if we
// are looking at an action end point.
for(vector<const pddl_type *>::const_iterator pt = types.begin();
pt != types.end();++pt)
{
PropertyState * x = PropertyState::getPS(tan,*pt,i,i+1);
PropertyState * en = PropertyState::getPS(tan,*pt,enablers.begin(),enablers.end());
trules.push_back(new TransitionRule(tan,op,var,en,x,x,opt));
};
enablers.insert(find_if(enablers.begin(),enablers.end(),
bind2nd(greater<Property*>(),p)),p);
};
if(adds.size() > is.size() || dels.size() > is.size())
{
vector<Property *> nadds, ndels;
set_difference(adds.begin(),adds.end(),is.begin(),is.end(),
inserter(nadds,nadds.begin()));
set_difference(dels.begin(),dels.end(),is.begin(),is.end(),
inserter(ndels,ndels.begin()));
enablers.clear();
merge(es.begin(),es.end(),is.begin(),is.end(),
inserter(enablers,enablers.begin()));
es.swap(enablers);
}
else
{
// No other rule to construct (all in split forms).
return;
};
};
for(vector<const pddl_type *>::const_iterator pt = types.begin();
pt != types.end();++pt)
{
PropertyState * e = PropertyState::getPS(tan,*pt,es.begin(),es.end());
PropertyState * l = PropertyState::getPS(tan,*pt,dels.begin(),dels.end());
PropertyState * r = PropertyState::getPS(tan,*pt,adds.begin(),adds.end());
if (op) {
trules.push_back(new TransitionRule(tan,op,var,e,l,r,opt));
} else {
trules.push_back(new TransitionRule(tan,drv,var,e,l,r,opt));
}
};
};
PropertyState::PMap PropertyState::pmap;
PropertySpace * PSCombiner(PropertySpace * p1,PropertySpace * p2)
{
p1->merge(p2);
return p1;
};
typedef PropertySpace *(*PSC)(PropertySpace *,PropertySpace *);
typedef Partitioner<Property*,PropertySpace *,PSC> PM;
struct unifyWith {
PM & pm;
Property * pr;
TransitionRule * trl;
unifyWith(PM & p,Property * pp,TransitionRule * tr) : pm(p), pr(pp), trl(tr) {};
void operator()(Property * p)
{
if(!pm.contains(p))
{
pm.add(p,new PropertySpace(p,trl));
}
else
{
//cout << "Adding rule " << *trl << " to " << *(pm.getData(p)) << "\n";
pm.getData(p)->add(trl);
};
pm.connect(pr,p);
};
};
struct makeSpace {
PM & pm;
makeSpace(PM & p) : pm(p) {};
void operator()(Property * p)
{
if(!pm.contains(p))
{
pm.add(p,new PropertySpace(p));
};
};
};
struct rulePartitioner {
PM & pm;
rulePartitioner(PM & p) : pm(p) {};
void operator()(TransitionRule * tr)
{
if(tr->isTrivial())
{
tr->distributeEnablers();
return;
};
Property * p = tr->isIncreasing()?*(tr->rhs->begin()):*(tr->lhs->begin());
for_each(tr->lhs->begin(),tr->lhs->end(),unifyWith(pm,p,tr));
for_each(tr->rhs->begin(),tr->rhs->end(),unifyWith(pm,p,tr));
for_each(tr->enablers->begin(),tr->enablers->end(),makeSpace(pm));
};
};
void TransitionRule::distributeEnablers()
{
if (op) {
MutexStore *m = MEX(op);
// The enabler mRecs in an op record the enabling property, the variable affected
// by the rule that is enabled and the part of the operator the rule comes from:
// START, MIDDLE or END
m->add(enablers->begin(),enablers->end(),var,opt);
}
};
PropertySpace::PropertySpace(Property * p,TransitionRule * t) :
properties(1,p), isStateValued(!t->isAttribute()), LSchecked(false)
{
rules.insert(t);
};
void PropertySpace::add(TransitionRule * t)
{
rules.insert(t);
isStateValued &= !t->isAttribute();
};
void PropertySpace::write(ostream & o) const
{
o << "\nState space states:\n";
for_each(states.begin(),states.end(),ptrwriter<PropertyState>(o,"\n"));
o << "\nSpace properties: ";
for_each(properties.begin(),properties.end(),ptrwriter<Property>(o," "));
o << "\nSpace objects: ";
for_each(objects.begin(),objects.end(),ptrwriter<const_symbol>(o," "));
o << "\nSpace rules:\n";
for_each(rules.begin(),rules.end(),ptrwriter<TransitionRule>(o,"\n"));
o << "Space is: " << (isStateValued?"state valued":"attribute valued");
};
ostream & operator<<(ostream & o,const PropertySpace & p)
{
p.write(o);
return o;
};
PropertySpace * spaceSet(PM::DataSource & p)
{
return PM::grabData(p)->finalise();
};
bool PropertySpace::isLockingSpace()
{
if(LSchecked) return isLS;
LSchecked = true;
cout << "Complete check on locking spaces\n";
return false;
};
void TIMAnalyser::setUpSpaces()
{
PM pts(&PSCombiner);
for_each(trules.begin(),trules.end(),rulePartitioner(pts));
transform(pts.begin(),pts.end(),inserter(propspaces,propspaces.begin()),
spaceSet);
// for_each(propspaces.begin(),propspaces.end(),ptrwriter<PropertySpace>(cout,"\n"));
};
// Need this because assembleMutexes is not const.
struct aMxs {
PropertySpace * p;
aMxs(PropertySpace * ps) : p(ps) {};
void operator()(TransitionRule * tr)
{
p->assembleMutexes(tr);
};
void operator()(Property * ps)
{
p->assembleMutexes(ps);
};
};
struct aMxs1 {
TransitionRule * tr;
aMxs1(TransitionRule * t) : tr(t) {};
void operator()(TransitionRule * t)
{
t->assembleMutex(tr);
};
};
struct aMxs2 {
PropertySpace * ps;
Property * p;
aMxs2(PropertySpace * tps,Property * tp) : ps(tps), p(tp) {};
void operator()(Property * tp)
{
if(p <= tp) ps->assembleMutexes(p,tp);
};
};
/*
struct aMxs3 {
TransitionRule * tr;
aMxs3(TransitionRule * t) : tr(t) {};
void operator()(Property * p)
{
for(Property::SpaceIt i = p->begin();i != p->end();++i)
{
if((*i)->isState())
{
cout << *p << " is candidate enabler for action " << tr->byWhat()->name->getName() << "\n";
(*i)->assembleMutexes(tr,p);
};
};
};
};
// tr is enabled by property p which appears in this PropertySpace
void PropertySpace::assembleMutexes(TransitionRule * tr,Property * p)
{
for_each(rules.begin(),rules.end(),aMxs1(tr));
};
*/
// This machinery might need to be reviewed to ensure it handles subtypes
// properly.
void PropertySpace::assembleMutexes(Property * p1,Property * p2)
{
if(p1==p2)
{
if(p1->root()->arity() == 1) return;
// Same property - special case
SIterator s = begin();
for(;s != end();++s)
{
if((*s)->count(p1)>1)
{
break;
};
};
if(s==end())
{
// Got one...
OUTPUT cout << "Mutex between " << *p1 << " and " << *p2 << "\n";
cTPS(p1->root())->setMutex(p1->aPosn(),cTPS(p2->root()),p2->aPosn());
};
}
else
{
// Different properties
// If they only ever appear in different states then they are mutex...
SIterator s = begin();
for(;s != end();++s)
{
if((*s)->contains(p1) && (*s)->contains(p2))
{
break;
};
};
if(s==end())
{
// Got a mutex...
OUTPUT cout << "Mutex between " << *p1 << " and " << *p2 << "\n";
cTPS(p1->root())->setMutex(p1->aPosn(),cTPS(p2->root()),p2->aPosn());