-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTimSupport.h
1279 lines (1102 loc) · 32.6 KB
/
TimSupport.h
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/>.
*
************************************************************************/
#ifndef __TIMSUPPORT
#define __TIMSUPPORT
#include <algorithm>
#include <iostream>
#include <set>
#include <memory>
#include <iterator>
#include <string.h>
#include "TIMUtilities.h"
#include "TypedAnalyser.h"
#define OUTPUT if(getenv("TIMOUT"))
#define OUTPUT1 if(getenv("TIMOUT") && !strcmp(getenv("TIMOUT"),"Hi"))
using std::multiset;
using std::make_pair;
using std::max;
using std::min;
using std::for_each;
using std::bind2nd;
using std::not1;
using std::mem_fun;
namespace TIM {
class PropertySpace;
class TIMobjectSymbol;
class TIMAnalyser;
class Property {
private:
VAL::pred_symbol * predicate;
int posn;
vector<PropertySpace*> belongTo;
vector<TIMobjectSymbol*> exhibitors;
bool isSV;
bool isReq;
public:
Property() : predicate(0), isSV(false), isReq(false) {};
Property(VAL::pred_symbol * p,int a) : predicate(p), posn(a), isSV(false), isReq(false) {};
void setSV(bool sv,bool rq)
{
isSV = sv;
isReq = rq;
};
bool isSingleValued() const {return isSV;};
bool isRequired() const {return isReq;};
void write(ostream & o) const
{
o << predicate->getName() << "_" << posn;
/*
o << "[";
for(vector<pddl_type *>::iterator i = EPS(predicate)->tBegin();
i != EPS(predicate)->tEnd();++i)
{
o << (*i)->getName() << " ";
};
o << "]";
*/
};
void addIn(PropertySpace * p)
{
belongTo.push_back(p);
};
typedef vector<PropertySpace *>::iterator SpaceIt;
SpaceIt begin() {return belongTo.begin();};
SpaceIt end() {return belongTo.end();};
void add(TIMobjectSymbol * t)
{
exhibitors.push_back(t);
};
typedef vector<TIMobjectSymbol *>::iterator ObjectIt;
ObjectIt oBegin() {return exhibitors.begin();};
ObjectIt oEnd() {return exhibitors.end();};
Property * getBaseProperty(const VAL::pddl_type * pt) const;
vector<Property *> matchers();
bool matches(const VAL::extended_pred_symbol * prop,VAL::pddl_type * pt);
bool applicableTo(VAL::TypeChecker & tc,const VAL::pddl_type * tp) const;
int familySize() const {return EPS(predicate)->arity();};
int aPosn() const {return posn;};
const VAL::extended_pred_symbol * root() const {return EPS(predicate);};
bool equivalent(const Property * p) const;
};
ostream & operator<<(ostream & o, const Property & p);
struct setUpProps {
unsigned int a;
VAL::pred_symbol * pred;
setUpProps(VAL::pred_symbol * p) :
a(0), pred(p) {};
void operator()(Property & p)
{
p = Property(pred,a);
++a;
};
};
class TIMpredSymbol : public VAL::extended_pred_symbol {
private:
vector<Property> props;
typedef map<TIMpredSymbol *,vector<pair<int,int> > > MutexRecords;
MutexRecords mutexes;
public:
TIMpredSymbol(VAL::pred_symbol * p,VAL::proposition * q) :
extended_pred_symbol(p,q), props(q->args->size())
{
for_each(props.begin(),props.end(),setUpProps(this));
};
template<class TI>
TIMpredSymbol(pred_symbol * p,TI s,TI e) :
extended_pred_symbol(p,s,e), props(e-s)
{
for_each(props.begin(),props.end(),setUpProps(this));
EPS(p)->getParent()->add(this);
};
Property * property(int a) {return &(props[a]);};
void setMutex(int p1,TIMpredSymbol * tps,int p2)
{
mutexes[tps].push_back(make_pair(p1,p2));
};
template<typename TI>
bool checkMutex(TI sa,TI ea,TIMpredSymbol * pb,TI sb,TI eb)
{
MutexRecords::iterator i = mutexes.find(pb);
if(i == mutexes.end()) return false;
if(pb == this)
{
for(vector<pair<int,int> >::const_iterator p = i->second.begin();
p != i->second.end();++p)
{
OUTPUT cout << "Examining " << p->first << " " << p->second << "\n";
if(*(sa+p->first) == *(sb+p->second))
{
// Same object has two copies of the same property.
// Now check whether they are identical propositions.
for(;sa != ea;++sa,++sb)
{
if(*sa != *sb) return true;
};
};
};
}
else
{
for(vector<pair<int,int> >::const_iterator p = i->second.begin();
p != i->second.end();++p)
{
OUTPUT cout << "Examining " << p->first << " " << p->second << "\n";
// Same value has two mutex properties...
if(*(sa+p->first) == *(sb+p->second)) return true;
};
};
return false;
};
};
#define TPS(x) static_cast<TIMpredSymbol*>(x)
#define cTPS(x) const_cast<TIMpredSymbol *>(static_cast<const TIMpredSymbol*>(x))
class TIMobjectSymbol : public VAL::const_symbol {
private:
vector<Property*> initial;
vector<VAL::proposition *> initialps;
vector<Property*> final;
vector<PropertySpace *> spaces;
public:
TIMobjectSymbol(const string & s) : const_symbol(s) {};
void addInitial(Property*p,VAL::proposition * prp)
{
initial.push_back(p);
initialps.push_back(prp);
};
void addFinal(Property*p) {final.push_back(p);};
void addIn(PropertySpace * p) {spaces.push_back(p);};
void distributeStates(TIMAnalyser * tan);
void write(ostream & o) const
{
o << getName();
};
const vector<VAL::proposition*> & getInits() const {return initialps;};
VAL::proposition * find(const Property * p) const
{
for(vector<Property*>::const_iterator i = initial.begin();i != initial.end();++i)
{
if(p && p->equivalent(*i))
{
return initialps[i-initial.begin()];
};
};
return 0;
/*
vector<Property*>::const_iterator i = std::find(initial.begin(),initial.end(),p);
if(i == initial.end())
{
for(vector<Property*>::const_iterator ii = initial.begin();ii != initial.end();++ii)
{
vector<Property*> ps = (*ii)->matchers();
vector<Property*>::const_iterator j = std::find(ps.begin(),ps.end(),p);
if(j != ps.end())
{
return initialps[ii-initial.begin()];
};
};
return 0;
};
return initialps[i-initial.begin()];
*/
};
};
ostream & operator <<(ostream & o,const TIMobjectSymbol & t);
#define TOB(x) static_cast<TIM::TIMobjectSymbol*>(x)
class PropertyState {
private:
typedef multiset<Property *> Properties;
typedef CascadeMap<Property *,PropertyState> PMap;
static PMap pmap;
TIMAnalyser * tan;
Properties properties;
template<class TI>
PropertyState(TIMAnalyser * t,TI s,TI e) : tan(t), properties(s,e) {};
template<class TI>
static PropertyState * retrieve(TIMAnalyser * tan,TI s,TI e)
{
PropertyState * & ps = pmap.forceGet(s,e);
if(ps==0)
{
ps = new PropertyState(tan,s,e);
};
return ps;
};
public:
template<class TI>
static PropertyState * getPS(TIMAnalyser * tan,const VAL::pddl_type * pt,TI s,TI e)
{
vector<Property *> props;
transform(s,e,inserter(props,props.begin()),
bind2nd(mem_fun(&Property::getBaseProperty),pt));
return retrieve(tan,props.begin(),props.end());
};
void write(ostream & o) const
{
o << "{";
for_each(properties.begin(),properties.end(),
ptrwriter<Property>(o," "));
o << "}";
};
int count(Property * p) const
{
return std::count(properties.begin(),properties.end(),p);
};
bool contains(Property * p) const
{
return std::find(properties.begin(),properties.end(),p) != properties.end();
};
bool empty() const
{
return properties.empty();
};
size_t size() const
{
return properties.size();
};
typedef Properties::const_iterator PSIterator;
PSIterator begin() const {return properties.begin();};
PSIterator end() const {return properties.end();};
PropertyState * adjust(const PropertyState * del,const PropertyState * add)
{
// Simple implementation is to remove del from this and then check that the result
// found all entries in del. If so, union add and return a new PropertyState, else
// return 0.
//
// There is probably an issue over types though. For example, if dels are for a more
// specialised type than the entries in this what should happen?
vector<Property *> ps;
set_difference(properties.begin(),properties.end(),
del->properties.begin(),del->properties.end(),
inserter(ps,ps.begin()));
if(ps.size() + del->properties.size() == properties.size())
{
vector<Property *> qs;
merge(ps.begin(),ps.end(),add->properties.begin(),add->properties.end(),
inserter(qs,qs.begin()));
return retrieve(tan,qs.begin(),qs.end());
}
else
{
return 0;
};
};
pair<PropertyState *,PropertyState *> split(Property *);
template<class TI>
PropertyState * add(TI s,TI e)
{
if(s==e) return this;
vector<Property *> qs;
merge(properties.begin(),properties.end(),s,e,inserter(qs,qs.begin()));
return retrieve(tan,qs.begin(),qs.end());
};
};
ostream & operator<<(ostream & o,const PropertyState & p);
class TransitionRule;
class mRec;
struct recordIn {
PropertySpace * ps;
recordIn(PropertySpace * p) : ps(p) {};
void operator()(Property * p)
{
p->addIn(ps);
};
};
struct countInState {
Property * prop;
countInState(Property * p) : prop(p) {};
int operator()(PropertyState* ps)
{
return ps->count(prop);
};
};
struct recordSV {
PropertySpace * ps;
vector<Property *> & sv;
recordSV(PropertySpace * p,vector<Property *> & s) : ps(p), sv(s) {};
void operator()(Property * p);
};
// Two facts will be mutex if they define properties that appear in different
// states of the same (SV) property space, or if they define properties that are
// different instantiations for the same object when only one instance of the
// property appears in a state in a (SV) property space.
class PropertySpace {
private:
set<PropertyState *> states;
set<TransitionRule *> rules;
vector<Property *> properties;
vector<TIMobjectSymbol *> objects;
bool isStateValued;
bool isLS;
bool LSchecked;
public:
PropertySpace(Property * p,TransitionRule * t);
PropertySpace(Property * p) :
states(), rules(), properties(1,p), objects(), isStateValued(true) {};
void checkStateValued();
void merge(PropertySpace * ps)
{
copy(ps->states.begin(),ps->states.end(),
inserter(states,states.end()));
copy(ps->rules.begin(),ps->rules.end(),
inserter(rules,rules.end()));
copy(ps->properties.begin(),ps->properties.end(),
inserter(properties,properties.end()));
copy(ps->objects.begin(),ps->objects.end(),
inserter(objects,objects.end()));
isStateValued &= ps->isStateValued;
delete ps;
};
vector<int> countsFor(Property * p)
{
vector<int> cs;
transform(states.begin(),states.end(),inserter(cs,cs.begin()),countInState(p));
return cs;
};
void checkSV(vector<Property *> & sv)
{
for_each(properties.begin(),properties.end(),recordSV(this,sv));
};
void add(TransitionRule * t);
PropertySpace * finalise()
{
for_each(properties.begin(),properties.end(),recordIn(this));
return this;
};
void assembleMutexes();
void assembleMutexes(TransitionRule *);
// void assembleMutexes(TransitionRule *,Property *);
void assembleMutexes(Property *);
void assembleMutexes(Property *,Property *);
void assembleMutexes(VAL::operator_ *,const mRec &);
void recordRulesInActions();
void add(PropertyState * ps) {states.insert(ps);};
void add(TIMobjectSymbol * t) {objects.push_back(t);};
void write(ostream & o) const;
bool isState() const {return isStateValued;};
bool isStatic() const {return rules.empty();};
void sortObjects() {sort(objects.begin(),objects.end());};
bool contains(TIMobjectSymbol * to) const;
typedef vector<TIMobjectSymbol *>::const_iterator OIterator;
OIterator obegin() const {return objects.begin();};
OIterator oend() const {return objects.end();};
bool extend();
bool examine(vector<PropertySpace*> &);
PropertySpace * slice(Property * p);
bool applicableTo(VAL::TypeChecker & tc,const VAL::pddl_type * tp) const;
typedef set<PropertyState*>::const_iterator SIterator;
SIterator begin() const {return states.begin();};
SIterator end() const {return states.end();};
int numStates() const {return states.size();};
bool isLockingSpace();
};
ostream & operator<<(ostream & o,const PropertySpace & p);
class rulePartitioner;
class RuleObjectIterator;
enum opType {INSTANT = 0,START = 1,MIDDLE = 2,END = 3};
class TransitionRule {
private:
TIMAnalyser * tan;
VAL::operator_ * op;
VAL::derivation_rule * drv;
opType opt;
int var;
PropertyState * enablers;
PropertyState * lhs;
PropertyState * rhs;
vector<VAL::const_symbol*> objects;
friend class rulePartitioner;
friend class RuleObjectIterator;
TransitionRule(TransitionRule * t,PropertyState * e,PropertyState * l,PropertyState * r);
public:
TransitionRule(TIMAnalyser * t,VAL::operator_ * o,int v,
PropertyState * e,PropertyState * l,PropertyState * r,
opType ty = INSTANT);
TransitionRule(TIMAnalyser * t,VAL::derivation_rule * o,int v,
PropertyState * e,PropertyState * l,PropertyState * r,
opType ty = INSTANT);
bool isTrivial() const
{
return lhs->empty() && rhs->empty();
};
bool isAttribute() const
{
return lhs->empty() || rhs->empty();
};
bool isIncreasing() const
{
return lhs->empty() && !rhs->empty();
};
bool isDecreasing() const
{
return rhs->empty() && !lhs->empty();
};
void distributeEnablers();
void write(ostream & o) const
{
o << (*enablers) << " => " << (*lhs) << " -> " << (*rhs) <<
(isAttribute()?" attribute rule: ":"") <<
(isIncreasing()?"increasing":"") <<
(isDecreasing()?"decreasing":"");
};
RuleObjectIterator beginEnabledObjects();
RuleObjectIterator endEnabledObjects();
PropertyState * tryRule(PropertyState * p)
{
return p->adjust(lhs,rhs);
};
void assembleMutex(TransitionRule *);
void assembleMutex(VAL::operator_*,const mRec & pr);
Property * candidateSplit();
void recordInAction(PropertySpace * p);
int paramNum() const {return var;};
TransitionRule * splitRule(Property * p);
const PropertyState * getLHS() const {return lhs;};
const PropertyState * getRHS() const {return rhs;};
const PropertyState * getEnablers() const {return enablers;};
const VAL::operator_ * byWhat() const {return op;};
bool applicableIn(const PropertyState * p) const;
};
ostream & operator<<(ostream & o,const TransitionRule & tr);
typedef vector<TransitionRule *> TRules;
struct ProtoRule {
TIMAnalyser * tan;
VAL::operator_ * op;
VAL::derivation_rule * drv;
opType opt;
int var;
vector<Property *> enablers;
vector<Property *> adds;
vector<Property *> dels;
ProtoRule(TIMAnalyser * t,VAL::operator_ * o,int v,opType ty = INSTANT) :
tan(t), op(o), drv(0), opt(ty), var(v) {};
ProtoRule(TIMAnalyser * t,VAL::derivation_rule * o,int v,opType ty = INSTANT) :
tan(t), op(0), drv(o), opt(ty), var(v) {};
void insertPre(Property * p)
{
enablers.push_back(p);
};
void insertAdd(Property * p)
{
adds.push_back(p);
};
void insertDel(Property * p)
{
dels.push_back(p);
};
void addRules(TRules & trules);
};
struct processRule {
TRules & trules;
processRule(TRules & tr) : trules(tr) {};
void operator()(ProtoRule * pr)
{
if(!pr) return;
pr->addRules(trules);
delete pr;
};
};
struct doExtension {
bool again;
doExtension() : again(false) {};
void operator()(PropertySpace * p)
{
again |= p->extend();
};
operator bool() {return again;};
};
struct doExamine {
TIMAnalyser * tan;
vector<PropertySpace *> newas;
doExamine(TIMAnalyser * t) : tan(t) {};
void operator()(PropertySpace * p);
operator vector<PropertySpace *>() {return newas;};
};
// Need this because mem_fun won't work with non-const methods like sortObjects.
inline void sortObjects(PropertySpace * p)
{
p->sortObjects();
};
class TIMpred_decl : public VAL::pred_decl {
public:
TIMpred_decl() : pred_decl(0,0,0) {};
TIMpred_decl(VAL::pred_symbol * h,VAL::var_symbol_list * a,VAL::var_symbol_table * vt) :
pred_decl(h,a,vt) {};
~TIMpred_decl()
{
args = 0;
var_tab = 0;
};
};
class DurativeActionPredicateBuilder : public VAL::VisitController {
private:
bool inserting;
vector<VAL::pred_symbol *> toIgnore;
VAL::durative_action * replacePreconditionsOf;
public:
DurativeActionPredicateBuilder() : VisitController(), inserting(true) {};
const vector<VAL::pred_symbol *> & getIgnores() const {return toIgnore;};
void reverse() {inserting = false;};
virtual void visit_conj_goal(VAL::conj_goal * cg) {
using namespace VAL;
replacePreconditionsOf->precondition = cg->getGoals()->front();
const_cast<goal_list*>(cg->getGoals())->pop_front();
}
virtual void visit_timed_goal(VAL::timed_goal* ) {
replacePreconditionsOf->precondition = 0;
}
virtual void visit_durative_action(VAL::durative_action * p)
{
using namespace VAL;
// cout << "Treating " << p->name->getName() << "\n";
if(inserting) {
pred_symbol * nm = current_analysis->pred_tab.symbol_put(p->name->getName());
toIgnore.push_back(nm);
pred_decl * pd = new TIMpred_decl(nm,p->parameters,p->symtab);
current_analysis->the_domain->predicates->push_front(pd);
effect_lists * es = new effect_lists;
effect_lists * ee = new effect_lists;
timed_effect * ts = new timed_effect(es,E_AT_START);
es->add_effects.push_front(new simple_effect(new proposition(nm,p->parameters)));
timed_effect * te = new timed_effect(ee,E_AT_END);
ee->del_effects.push_front(new simple_effect(new proposition(nm,p->parameters)));
p->effects->timed_effects.push_front(ts);
p->effects->timed_effects.push_front(te);
timed_goal * tg = new timed_goal(new simple_goal(new proposition(nm,p->parameters),E_POS),E_OVER_ALL);
if(p->precondition)
{
goal_list * gs = new goal_list;
gs->push_front(tg);
gs->push_front(p->precondition);
conj_goal * cg = new conj_goal(gs);
p->precondition = cg;
} else {
p->precondition = tg;
}
}
else
{
timed_effect * t = p->effects->timed_effects.front();
p->effects->timed_effects.pop_front();
delete t;
t = p->effects->timed_effects.front();
p->effects->timed_effects.pop_front();
delete t;
replacePreconditionsOf = p;
goal * oldprecondition = p->precondition;
p->precondition->visit(this);
delete oldprecondition;
};
};
virtual void visit_domain(VAL::domain * p)
{
visit_operator_list(p->ops);
};
};
struct CheckSV {
vector<Property *> & sv;
CheckSV(vector<Property*> & s) : sv(s) {};
void operator()(PropertySpace * ps)
{
ps->checkSV(sv);
};
};
class TIMactionSymbol : public VAL::operator_symbol {
private:
vector<PropertySpace*> stateChanger;
vector<TransitionRule*> rules;
bool fixedDuration;
public:
TIMactionSymbol(const string & nm) : operator_symbol(nm), fixedDuration(false) {};
void addStateChanger(PropertySpace * ps,TransitionRule * tr)
{
stateChanger.push_back(ps);
rules.push_back(tr);
};
void write(ostream & o) const
{
o << name;
if(fixedDuration) o << "!";
};
typedef vector<TransitionRule*>::const_iterator RCiterator;
RCiterator begin() const {return rules.begin();};
RCiterator end() const {return rules.end();};
bool hasRuleFor(int prm) const;
bool isFixedDuration() const
{
return fixedDuration;
};
void assertFixedDuration()
{
fixedDuration = true;
};
};
inline ostream & operator<<(ostream & o,const TIMactionSymbol & a)
{
a.write(o);
return o;
};
#define TAS(x) static_cast<TIM::TIMactionSymbol*>(x)
#define TASc(x) static_cast<const TIM::TIMactionSymbol* const>(x)
class TIMAnalyser : public VAL::VisitController {
private:
VAL::TypeChecker & tcheck;
VAL::analysis * an;
VAL::FuncAnalysis fan;
bool adding;
bool initially;
bool finally;
bool isDurative;
bool atStart;
bool overall;
VAL::operator_ * op;
VAL::derivation_rule * drv;
vector<ProtoRule *> rules;
TRules trules;
vector<PropertySpace *> propspaces;
vector<PropertySpace *> attrspaces;
vector<PropertySpace *> staticspaces;
vector<Property*> singleValued;
void setUpSpaces();
static void assembleMutexes(PropertySpace *);
static void recordRulesInActions(PropertySpace *);
friend class doExamine;
public:
TIMAnalyser(VAL::TypeChecker & tc,VAL::analysis * a) :
tcheck(tc), an(a), fan(a->func_tab),
adding(true), initially(false), finally(false),
isDurative(false), overall(false), op(0) ,drv(0)
{};
VAL::TypeChecker & getTC() {return tcheck;};
void insertPre(int v,Property * p);
void insertEff(int v,Property * p);
void insertGoal(VAL::parameter_symbol * c,Property * p);
void insertInitial(VAL::parameter_symbol * c,Property * p,VAL::proposition * prp);
virtual void visit_simple_goal(VAL::simple_goal * p);
virtual void visit_qfied_goal(VAL::qfied_goal * p)
{OUTPUT cout << "Quantified goal\n";};
virtual void visit_conj_goal(VAL::conj_goal * p)
{p->getGoals()->visit(this);};
virtual void visit_disj_goal(VAL::disj_goal * p)
{OUTPUT cout << "Disjunctive goal\n";};
virtual void visit_timed_goal(VAL::timed_goal * p)
{
using namespace VAL;
if(p->getTime() == (atStart?E_AT_START:E_AT_END) || (overall && p->getTime()==E_OVER_ALL))
p->getGoal()->visit(this);
};
virtual void visit_imply_goal(VAL::imply_goal * p)
{
OUTPUT cout << "Implication goal\n";
};
virtual void visit_neg_goal(VAL::neg_goal * p)
{
OUTPUT cout << "Negative goal\n";
};
virtual void visit_simple_effect(VAL::simple_effect * p);
virtual void visit_simple_derivation_effect(VAL::derivation_rule * p);
virtual void visit_forall_effect(VAL::forall_effect * p)
{
OUTPUT cout << "Quantified effect\n";
};
virtual void visit_cond_effect(VAL::cond_effect * p)
{
OUTPUT cout << "Conditional effect\n";
};
virtual void visit_timed_effect(VAL::timed_effect * p)
{
using namespace VAL;
if(p->ts==(atStart?E_AT_START:E_AT_END))
p->effs->visit(this);
};
virtual void visit_effect_lists(VAL::effect_lists * p)
{
using namespace VAL;
p->add_effects.pc_list<simple_effect*>::visit(this);
p->forall_effects.pc_list<forall_effect*>::visit(this);
p->cond_effects.pc_list<cond_effect*>::visit(this);
p->timed_effects.pc_list<timed_effect*>::visit(this);
bool whatwas = adding;
adding = !adding;
p->del_effects.pc_list<simple_effect*>::visit(this);
adding = whatwas;
};
virtual void visit_derivation_rule(VAL::derivation_rule * p)
{
drv = p;
adding = true;
rules = vector<ProtoRule*>(p->get_head()->args->size(),0);
p->get_body()->visit(this);
visit_simple_derivation_effect(p);
for_each(rules.begin(),rules.end(),processRule(trules));
drv = 0;
};
virtual void visit_operator_(VAL::operator_ * p)
{
op = p;
adding = true;
rules = vector<ProtoRule*>(p->parameters->size(),0);
p->precondition->visit(this);
p->effects->visit(this);
for_each(rules.begin(),rules.end(),processRule(trules));
op = 0;
};
virtual void visit_action(VAL::action * p)
{
visit_operator_(p);
}
virtual void visit_durative_action(VAL::durative_action * p)
{
// I think that we can do this in two stages - the at start and the at end.
// We can have a filter on timed goals and effects that decides whether it
// is relevant. Might need to store an optype flag with op, so that we can
// tell whether we generated from a start or end point.
//
// The tricky bit is the linkage: we need record invariants and we also need
// to ensure that we don't lose state change across durative actions. Toni's
// idea is to have a dummy add effect at start that is preconditioned and deleted
// at the end. That should work, but needs a couple of tweaks:
// 1: If the action has several state change effects then this technique will leave
// them linked together in one state space. Not entirely clear how these could be
// split, but maybe we can use a technique that generalises the state splitting
// idea (ab->c, c->ab becomes a->c, c->a and b->c', c'->b).
// 2: The mechanism artificially creates increasing/decreasing effects if there was
// actually no property that was unlinked before introducing the dummies. We can
// filter these cases out, I think.
//
isDurative = true;
atStart = true;
overall = false;
visit_operator_(p);
atStart = false;
visit_operator_(p);
overall = true;
visit_operator_(p);
overall = false;
isDurative = false;
};
virtual void visit_domain(VAL::domain * p)
{
visit_operator_list(p->ops);
if (p->drvs) visit_derivations_list(p->drvs);
setUpSpaces();
};
virtual void visit_problem(VAL::problem * p)
{
initially = true;
p->initial_state->visit(this);
initially = false;
finally = true;
if(p->the_goal) p->the_goal->visit(this);
finally = false;
if(p->objects) p->objects->visit(this);
for_each(propspaces.begin(),propspaces.end(),&sortObjects);
vector<PropertySpace*>::iterator a =
partition(propspaces.begin(),propspaces.end(),
mem_fun(&PropertySpace::isState));
copy(a,propspaces.end(),inserter(attrspaces,attrspaces.begin()));
propspaces.erase(a,propspaces.end());
a = partition(propspaces.begin(),propspaces.end(),
not1(mem_fun(&PropertySpace::isStatic)));
copy(a,propspaces.end(),inserter(staticspaces,staticspaces.end()));
propspaces.erase(a,propspaces.end());
while(for_each(attrspaces.begin(),attrspaces.end(),doExtension()));
while(for_each(propspaces.begin(),propspaces.end(),doExtension()));
OUTPUT1 {
for_each(trules.begin(),trules.end(),ptrwriter<TransitionRule>(cout,"\n"));
for_each(propspaces.begin(),propspaces.end(),
ptrwriter<PropertySpace>(cout,"\n"));
};
for_each(propspaces.begin(),propspaces.end(),assembleMutexes);
for_each(propspaces.begin(),propspaces.end(),recordRulesInActions);
attrspaces = for_each(attrspaces.begin(),attrspaces.end(),doExamine(this));
OUTPUT1 {
cout << "Spaces now look like this:\n";
for_each(propspaces.begin(),propspaces.end(),
ptrwriter<PropertySpace>(cout,"\n"));
};
};
virtual void visit_const_symbol(VAL::const_symbol * p)
{
TIMobjectSymbol * t = dynamic_cast<TIMobjectSymbol*>(p);
t->distributeStates(this);
};
void checkSV()
{
for_each(propspaces.begin(),propspaces.end(),CheckSV(singleValued));
};
set<PropertySpace *> relevant(VAL::pddl_type * tp);
void close(set<Property*> & seed,const VAL::pddl_type * pt);
typedef vector<PropertySpace *>::const_iterator const_iterator;
const_iterator pbegin() const {return propspaces.begin();};
const_iterator pend() const {return propspaces.end();};
const_iterator abegin() const {return attrspaces.begin();};
const_iterator aend() const {return attrspaces.end();};
const_iterator sbegin() const {return staticspaces.begin();};
const_iterator send() const {return staticspaces.end();};
};
class mutex;
struct mRec {
Property * first;
int second;