forked from anubhav-gres/VAL-4.2.08
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvents.cpp
More file actions
2019 lines (1628 loc) · 72.6 KB
/
Events.cpp
File metadata and controls
2019 lines (1628 loc) · 72.6 KB
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/>.
*
************************************************************************/
/*-----------------------------------------------------------------------------
VAL - The Automatic Plan Validator for PDDL+
$Date: 2009-02-05 10:50:12 $
$Revision: 1.2 $
Maria Fox, Richard Howey and Derek Long - PDDL+ and VAL
Stephen Cresswell - PDDL Parser
maria.fox@cis.strath.ac.uk
derek.long@cis.strath.ac.uk
stephen.cresswell@cis.strath.ac.uk
richard.howey@cis.strath.ac.uk
By releasing this code we imply no warranty as to its reliability
and its use is entirely at your own risk.
Strathclyde Planning Group
http://planning.cis.strath.ac.uk
----------------------------------------------------------------------------*/
#include "Events.h"
#include "Plan.h"
#include "Validator.h"
#include "Action.h"
#include "RobustAnalyse.h"
namespace VAL {
Events::Events(const operator_list * ops) : ungroundEvents(), ungroundProcesses(),
triggeredEvents(), triggeredProcesses(), untriggeredProcesses(), activeProcesses(), eventsForMutexCheck(), oldTriggeredEvents(), lastHappeningTime(0), ctsEventTriggered(false) //, noevents(0)
{
for(operator_list::const_iterator i = ops->begin();i != ops->end();++i)
{
if(event * e = dynamic_cast<event *>(*i))
{
ungroundEvents.push_back(e);
}
else if(process * p = dynamic_cast<process *>(*i))
{
ungroundProcesses.push_back(p);
};
};
};
Events::~Events()
{
ungroundEvents.clear();
triggeredEvents.clear();
ungroundProcesses.clear();
triggeredProcesses.clear();
for(vector<const Action *>::iterator i = oldTriggeredEvents.begin(); i != oldTriggeredEvents.end(); ++i)
delete *i;
for(map<const StartAction *, pair<const Proposition *,const_symbol_list *> >::iterator j = activeProcesses.begin(); j != activeProcesses.end(); ++j)
{j->second.first->destroy(); delete j->second.second;};
activeProcesses.clear();
};
string Events::getName(operator_ * op,const_symbol_list * csl) const
{
string eventName = "("+op->name->getName();
for(const_symbol_list::iterator cs = csl->begin(); cs != csl->end(); ++cs)
{
eventName += " "+(*cs)->getName();
};
return eventName +")";
};
bool Events::isTriggered(event * eve,const_symbol_list * csl) const
{
string eventName = eve->name->getName();
for(const_symbol_list::iterator cs = csl->begin(); cs != csl->end(); ++cs)
{
eventName += (*cs)->getName();
};
set<string>::const_iterator i = triggeredEvents.find(eventName);
if(i != triggeredEvents.end())
{
eventName = "(" + eve->name->getName();
for(const_symbol_list::iterator cs = csl->begin(); cs != csl->end(); ++cs)
{
eventName += " "+(*cs)->getName();
};
eventName += ")";
if(LaTeX)
{
*report << "\\errorr{Attempt to trigger event \\exprn{"<<eventName<<"} twice}\\\\\n";
}
else if(Verbose) *report << "Attempt to trigger event "<<eventName<<" twice\n";
return true;
};
return false;
};
bool Events::isTriggered(const Action * act) const
{
string eventName = act->getName0();
set<string>::const_iterator i = triggeredEvents.find(eventName);
if(i != triggeredEvents.end())
{
if(LaTeX)
{
*report << "\\errorr{Attempt to trigger event \\exprn{"<<*act<<"} twice}\\\\\n";
}
else if(Verbose) *report << "Attempt to trigger event "<<*act<<" twice\n";
return true;
};
return false;
};
bool Events::isProcessTriggered(const StartAction * sa) const
{
set<const StartAction *>::iterator i = triggeredProcesses.find(sa);
return (i != triggeredProcesses.end());
};
bool Events::isProcessActive(process * pro,const_symbol_list * csl) const
{
string processName = pro->name->getName();
for(const_symbol_list::iterator cs = csl->begin(); cs != csl->end(); ++cs)
{
processName += (*cs)->getName();
};
for(map<const StartAction *, pair<const Proposition *,const_symbol_list *> >::const_iterator i = activeProcesses.begin(); i != activeProcesses.end(); ++i)
{
if(processName == i->first->getName0())
{
return true;
};
};
return false;
};
bool Events::isProcessUntriggered(process * pro,const_symbol_list * csl) const
{
string processName = pro->name->getName();
for(const_symbol_list::iterator cs = csl->begin(); cs != csl->end(); ++cs)
{
processName += (*cs)->getName();
};
set<string>::iterator i = untriggeredProcesses.find(processName);
return (i != untriggeredProcesses.end());
};
void deleteParameters(vector<const_symbol_list*> & vcsl)
{
for(vector<const_symbol_list*>::iterator p = vcsl.begin(); p != vcsl.end(); ++p)
delete *p;
};
const vector<const_symbol_list*> removeRepeatedParameters(const vector<const_symbol_list*> & vcsl)
{
vector<const_symbol_list*> listOfParameters;
set<string> parameterLists;
for(vector<const_symbol_list*>::const_iterator i = vcsl.begin(); i != vcsl.end(); ++i)
{
string eventName;
for(const_symbol_list::iterator cs = (*i)->begin(); cs != (*i)->end(); ++cs)
{
eventName += (*cs)->getName();
};
set<string>::const_iterator pl = parameterLists.find(eventName);
if(pl == parameterLists.end())
{
listOfParameters.push_back(*i);
parameterLists.insert(eventName);
}
else
delete *i;
};
return listOfParameters;
};
const_symbol_list * newBlankConstSymbolList(var_symbol_list * parameters,Validator * v)
{
//create blank list of parameters, ie every parameter is undefined
const_symbol_list * blankcsl = new const_symbol_list();
for(var_symbol_list::const_iterator i = parameters->begin(); i != parameters->end(); ++i)
{
const_symbol * cs = 0; //v->getAnalysis()->const_tab.symbol_get("");
blankcsl->push_back(cs);
};
return blankcsl;
};
const EndAction * addEndProcess(vector<const Action*> & processes,const StartAction * sa,const_symbol_list * csl,Validator * v)
{
//create end action to end the process
const operator_ * pro = sa->getAction();
action * dae = new safeaction(pro->name,pro->parameters,new conj_goal(new goal_list()),new effect_lists(),pro->symtab);
const EndAction * endAct = new EndAction(v,dae,csl,sa,0,new goal_list());
processes.push_back(endAct);
return endAct;
};
void Events::updateHappeningTime(double t)
{
if(lastHappeningTime != t)
{
triggeredEvents.clear(); //to check for events being triggered twice at the same time point
triggeredProcesses.clear(); //to ensure a process is not triggered and untriggered at same time point due to rounding errors
untriggeredProcesses.clear(); //to ensure a processes is not untriggered and then retriggered at the same time point due to rounding when triggering processes by discrete change!
};
lastHappeningTime = t;
};
void Events::updateEventsForMutexCheck(Validator * v)
{
eventsForMutexCheck.clear();
vector<const_symbol_list*> listOfParameters0;
vector<const_symbol_list*> listOfParameters00;
set<var_symbol*> svs;
vector<const_symbol_list*> listOfParameters;
for(vector<event*>::iterator e = ungroundEvents.begin(); e != ungroundEvents.end(); ++e)
{
listOfParameters0 = getParametersCts((*e)->precondition,*e,v,false,true);
listOfParameters00 = removeRepeatedParameters(listOfParameters0);
svs = getVariables(*e);
listOfParameters = defineUndefinedParameters(listOfParameters00,*e,v,svs);
//add events to a event happening
for(vector<const_symbol_list*>::iterator p = listOfParameters.begin(); p != listOfParameters.end(); ++p)
{
const Action * anAction = new Action(v,*e,*p);
eventsForMutexCheck.push_back(anAction);
oldTriggeredEvents.push_back(anAction);
}; //end of looping thro' parameters
deleteParameters(listOfParameters);
};
};
//trigger processes and events before the first happening in the plan
bool Events::triggerInitialEvents(Validator * v,double firstHappeningTime)
{
if(!hasEvents()) return true;
bool isOK = true;
ActiveCtsEffects * ace = v->getActiveCtsEffects();
ace->clearCtsEffects(); //clear cts effect given by the first happening
//check for events and processes in the intitial state
isOK = triggerDiscreteEvents(v,true);
//update values before the first happening of the plan!
if(ace->hasCtsEffects() && (isOK || ContinueAnyway)) //if processes have been activated in the intitial state then there may be events to trigger and we need to update values before the first happening
{
ace->setTime(firstHappeningTime);
ace->setLocalUpdateTime(firstHappeningTime);
isOK = triggerEventsOnInterval(v,true) && isOK;
if(isOK || ContinueAnyway)
{
//execute cts update happening
if(firstHappeningTime != 0)
{
if(Verbose) *report << "\n";
ace->setLocalUpdateTime(firstHappeningTime - v->getState().getTime());
const Happening * updateHap = ace->getCtsEffectUpdate();
updateHap->adjustActiveCtsEffects(*ace);
isOK = v->executeHappening(updateHap) && isOK;
ace->ctsEffectsProcessed = false; ace->addActiveFEs();//update cts effects
};
};
};
v->adjustActiveCtsEffects(ace);
return isOK;
};
bool Events::triggerEventsOnInterval(Validator * v,bool init)
{
bool isOK = true;
do
{
isOK = triggerDiscreteEvents(v,init) && isOK; if(!isOK && !ContinueAnyway) return false;
isOK = triggerContinuousEvents(v,init) && isOK; if(!isOK && !ContinueAnyway) return false;
}while(ctsEventTriggered);
return isOK;
};
bool checkPreconditionsAreNotSatisfied(const State * s,const vector<const Action *> & events)
{
for(vector<const Action *>::const_iterator i = events.begin(); i != events.end(); ++i)
{
if((*i)->getPrecondition()->evaluate(s))
{
if(LaTeX) *report << "\\> Event "<<*i<<" does not falsify its precondition!\\\\\n";
else if(Verbose) cout << "Event "<<*i<<" does not falsify its precondition!\n";
return false;
};
};
return true;
};
//trigger events given by discrete change
bool Events::triggerDiscreteEvents(Validator * v,bool init)
{
if(EventPNEJuddering) JudderPNEs = true;
double theCurrentTime = v->getState().getTime();
updateHappeningTime(theCurrentTime);
bool isOK = true;
vector<const_symbol_list*> listOfParameters;
int numberofEvents = 0;
//trigger events: build up list of events to be triggered then add to a happening and execute the happening
//then test for more events
bool eventTriggered;
do{
eventTriggered = false;
vector<const Action*> acts;
for(vector<event*>::iterator e = ungroundEvents.begin(); e != ungroundEvents.end(); ++e)
{
if(!init || (theCurrentTime != 0))
{
listOfParameters = getParametersDiscreteFinal((*e)->precondition,*e,v->getState());
}
else
{
listOfParameters = getParametersDiscreteInitialFinal((*e)->precondition,*e,v);
if(!listOfParameters.empty())
{
*report << "Event preconditions are not permitted to be satisfied in the initial state!\n";
if(!ContinueAnyway) return false;
isOK = false;
};
};
//add events to a event happening and remove repetitions
for(vector<const_symbol_list*>::iterator p = listOfParameters.begin(); p != listOfParameters.end(); ++p)
{
if(isTriggered(*e,*p)) isOK = false;
else
{
const Action * anAction = new Action(v,*e,*p); //include precondition to check for mutex events? yes
acts.push_back(anAction);
triggeredEvents.insert(anAction->getName0());
oldTriggeredEvents.push_back(anAction);
};
}; //end of looping thro' parameters
deleteParameters(listOfParameters);
}; //end of looping thro' events
//apply events as a happening to the state
if(acts.size() != 0 && (isOK || ContinueAnyway))
{
if(numberofEvents > 1)
{
*report << "Event cascades of this nature are not yet handled!\n";
if(!ContinueAnyway) return false;
isOK = false;
};
if(Verbose) cout << "\n";
Happening * eventHappening = new Happening(v,acts,true);
isOK = v->executeHappening(eventHappening) && isOK;
isOK = checkPreconditionsAreNotSatisfied(&(v->getState()),acts) && isOK;
eventHappening->clearActions(); delete eventHappening;
if(!isOK && !ContinueAnyway) return false;
eventTriggered = true;
numberofEvents = acts.size();
};
isOK = triggerDiscreteProcesses(v) && isOK;
}while(eventTriggered); //if events were triggered this may result in more events triggering
eventsForMutexCheck.clear();
if(EventPNEJuddering) JudderPNEs = false;
return isOK;
};
const StartAction * newStartProcessAction(process* pro, const_symbol_list* csl,Validator * v)
{
effect_lists * elc = new effect_lists();
for(pc_list<timed_effect*>::const_iterator i = pro->effects->timed_effects.begin();
i != pro->effects->timed_effects.end();++i)
{
if((*i)->ts == E_CONTINUOUS)
{
insert_effects(elc,(*i)->effs);
};
};
//create a StartAction here
action * das = new safeaction(pro->name,pro->parameters,new conj_goal(new goal_list()),new effect_lists(),pro->symtab);
return new StartAction(v,das,csl,new conj_goal(new goal_list()),elc,0.0,new goal_list(),vector<const CondCommunicationAction *>(),vector<const CondCommunicationAction *>());
};
const EndAction * newEndProcess(const StartAction * sa,const_symbol_list * csl,Validator * v)
{
//create end action to end the process
const operator_ * pro = sa->getAction();
action * dae = new safeaction(pro->name,pro->parameters,new conj_goal(new goal_list()),new effect_lists(),pro->symtab);
const EndAction * endAct = new EndAction(v,dae,csl,sa,0,new goal_list());
return endAct;
};
//trigger processes given by discrete change
bool Events::triggerDiscreteProcesses(Validator * v)
{
if(EventPNEJuddering) JudderPNEs = true;
bool isOK = true;
vector<const Action*> processes;
double time = v->getState().getTime();
//find processes that are ended
if(time > 0)
{
vector<const StartAction *> eraseElements;
for(map<const StartAction *, pair<const Proposition *,const_symbol_list *> >::iterator ap = activeProcesses.begin();
ap != activeProcesses.end() ; ++ap)
{
const_cast<Proposition *>(ap->second.first)->resetCtsFunctions(); //ensure we are checking the precondition at a point not on an interval
if(!(ap->second.first->evaluate(&v->getState())) && !(isProcessTriggered(ap->first)))
{
const EndAction * endAct = addEndProcess(processes,ap->first,ap->second.second,v);
oldTriggeredEvents.push_back(endAct);
ap->second.first->destroy(); delete ap->second.second; eraseElements.push_back(ap->first);
untriggeredProcesses.insert(ap->first->getName0());
};
};
for(vector<const StartAction *>::iterator ee = eraseElements.begin(); ee != eraseElements.end(); ++ee)
activeProcesses.erase(*ee);
};
for(vector<process*>::const_iterator p = ungroundProcesses.begin(); p != ungroundProcesses.end(); ++p)
{
//for every process check if it could be triggered in the initial state or later on
//get list of parameters (may be empty)
vector<const_symbol_list*> listOfParameters;
if(time == 0.0)
{
listOfParameters = getParametersDiscreteInitialFinal((*p)->precondition,*p,v);
}
else
{
listOfParameters = getParametersDiscreteFinal((*p)->precondition,*p,v->getState()); //need to check for all changes after the event cascade, this is the best way, only consider the process if something has changed
};
//cout << "Process " << (*p)->name->getName() << " with " << listOfParameters.size() << "\n";
//add processes to an event happening and remove repetitions
for(vector<const_symbol_list*>::iterator pa = listOfParameters.begin(); pa != listOfParameters.end(); ++pa)
{
if(!isProcessActive(*p,*pa) /*&& !isProcessUntriggered(*p,*pa)*/) //no need to check if it has not been untriggered, because above we now check that the process is triggered by something that has changed
{
const StartAction * sa = newStartProcessAction(*p,*pa,v);
processes.push_back(sa);
//only add the precondition to the list of triggered processes, as we already know it to be satisfied when applied
const Proposition * prop = v->pf.buildProposition((*p)->precondition,*(buildBindings(*p,**pa).copy(v)));
activeProcesses[sa] = make_pair(prop,*pa);
oldTriggeredEvents.push_back(sa);
triggeredProcesses.insert(sa);
//cout << "Triggered " << *sa << "\n";
}
else
delete *pa;
}; //end of looping thro' parameters
}; //end of looping thro' processes
//apply processes as a happening to the state
if(processes.size() != 0)
{
if(Verbose) cout << "\n";
ActiveCtsEffects * ace = v->getActiveCtsEffects();
Happening * eventHappening = new Happening(v,processes,true);
eventHappening->adjustActiveCtsEffects(*ace);
isOK = v->executeHappening(eventHappening);
eventHappening->clearActions(); delete eventHappening;
if(!isOK && !ContinueAnyway) return false;
};
if(EventPNEJuddering) JudderPNEs = false;
return isOK;
};
const vector<const_symbol_list*> getParametersDiscreteInitialFinal(goal * g,operator_ * op,Validator * v)
{
vector<const_symbol_list *> aParameterList;
const_symbol_list * blankcsl = newBlankConstSymbolList(op->parameters,v);
aParameterList.push_back(blankcsl);
const vector<const_symbol_list*> listOfParameters0 = getParametersList(g,op,v,aParameterList,false,true);
delete blankcsl;
const vector<const_symbol_list*> listOfParameters00 = removeRepeatedParameters(listOfParameters0);
const set<var_symbol*> svs = getVariables(op);
return defineUndefinedParameters(listOfParameters00,op,v,svs);
};
const vector<const_symbol_list*> getParametersDiscreteFinal(goal * g,operator_ * op, const State & s)
{
//get list of parameters (may be empty)
const vector<const_symbol_list*> listOfParameters0 = getParametersDiscrete(g,op,s);
const vector<const_symbol_list*> listOfParameters00 = removeRepeatedParameters(listOfParameters0);
const set<var_symbol*> svs = getVariables(op);
return defineUndefinedParameters(listOfParameters00,op,s.getValidator(),svs);
};
const vector<const_symbol_list*> getParametersCtsFinal(goal * g,operator_ * op,Validator * v)
{
const vector<const_symbol_list*> listOfParameters0 = getParametersCts(g,op,v);
const vector<const_symbol_list*> listOfParameters00 = removeRepeatedParameters(listOfParameters0);
const set<var_symbol*> svs = getVariables(op);
return defineUndefinedParameters(listOfParameters00,op,v,svs);
};
//trigger events given by ctsly changing PNEs
bool Events::triggerContinuousEvents(Validator * v,bool init)
{
if(!init && v->isLastHappening()) return true;
if(EventPNEJuddering) JudderPNEs = true;
ctsEventTriggered = false;
ActiveCtsEffects * ace = v->getActiveCtsEffects();
ExecutionContext * ec = v->getExecutionContext();
vector<const_symbol_list*> listOfParameters;
double currentTime = v->getState().getTime();
double nextTime;
if(!init) nextTime = v->getNextHappeningTime(); else nextTime = v->getCurrentHappeningTime();
double localTime = nextTime - currentTime;
if(localTime == 0) return true;
bool isOK = true;
vector<const Action*> acts;
vector<const Action*> processes;
ace->setLocalUpdateTime(localTime); //set value of LocalUpdateTime - time since last regular happening
//process cts effects, build polys etc, conditional cts effects handled here also
if(TestingPNERobustness) ace->addActiveFEs(true);
else ace->addActiveFEs();
if(!ace->hasCtsEffects()) return true;
//get parameters for all events that may be triggered due to cts effects
for(vector<event*>::iterator e = ungroundEvents.begin(); e != ungroundEvents.end(); ++e)
{
listOfParameters = getParametersCtsFinal((*e)->precondition,*e,v);
for(vector<const_symbol_list*>::iterator p = listOfParameters.begin(); p != listOfParameters.end(); ++p)
{
const Action * anAction = new Action(v,*e,*p);
const_cast<Proposition*>(anAction->getPrecondition())->setUpComparisons(ace);
acts.push_back(anAction);
};//end of loop thro' parameters
deleteParameters(listOfParameters);
};
//find out when events are first triggered, if at all
vector<pair<const Action *,intervalEnd> > ctsTriggeredEvents;
Intervals eventIntervals;
for(vector<const Action*>::iterator e = acts.begin(); e != acts.end(); ++e)
{
try
{
eventIntervals = (*e)->getPrecondition()->getIntervals(&(v->getState()));
}
catch(BadAccessError & e)
{
//if a PNE is not defined, then no problem, the event is simply not triggered
};
if(!eventIntervals.intervals.empty())
{
ctsTriggeredEvents.push_back(make_pair(*e, eventIntervals.intervals.begin()->first));//intervals are constructed left to right so this is the minimum point
}
else
delete *e;
};
//get start time for processes to trigger - add to list of actions
map<const StartAction *, pair<const Proposition *,const_symbol_list *> > startProcesses;
for(vector<process*>::iterator pro = ungroundProcesses.begin(); pro != ungroundProcesses.end(); ++pro)
{
listOfParameters = getParametersCtsFinal((*pro)->precondition,*pro,v);
for(vector<const_symbol_list*>::iterator p = listOfParameters.begin(); p != listOfParameters.end(); ++p)
{
if(!isProcessActive(*pro,*p) /*&& !isProcessUntriggered(*pro,*p)*/)
{
//create a StartAction here
const StartAction * sa = newStartProcessAction(*pro,*p,v);
const Proposition * prop = v->pf.buildProposition((*pro)->precondition,*(buildBindings(*pro,**p).copy(v)));
const_cast<Proposition*>(prop)->setUpComparisons(ace);
eventIntervals = prop->getIntervals(&(v->getState()));
if(!eventIntervals.intervals.empty())
{
intervalEnd startTime = make_pair(-1,true);
//do not trigger a process on an isolated point
for(vector< pair<intervalEnd,intervalEnd> >::const_iterator ei = eventIntervals.intervals.begin(); ei != eventIntervals.intervals.end();++ei)
{
if(ei->first.first != ei->second.first)
{
startTime = ei->first; break;
};
};
if(startTime.first != -1)
{
ctsTriggeredEvents.push_back(make_pair(sa, startTime));//intervals are constructed left to right so this is the minimum point
startProcesses[sa] = make_pair(prop,new const_symbol_list(**p));
};
}
else
{
delete sa;
prop->destroy();
};
};
};
deleteParameters(listOfParameters);
};
map<const EndAction *,const StartAction*> endStartMap;
//find processes that could be ended
for(map<const StartAction *, pair<const Proposition *,const_symbol_list *> >::iterator ap = activeProcesses.begin(); ap != activeProcesses.end() ; ++ap)
{
const_cast<Proposition*>(ap->second.first)->setUpComparisons(ace);
Intervals eventIntervals0 = ap->second.first->getIntervals(&(v->getState()));
eventIntervals = setComplement(eventIntervals0,localTime);
if(!(eventIntervals.intervals.empty()))
{
const EndAction * endAct = newEndProcess(ap->first,ap->second.second,v);
endStartMap[endAct] = ap->first;
intervalEnd startTime = make_pair(-1,true);
//do not untrigger a process on an isolated point
for(vector< pair<intervalEnd,intervalEnd> >::const_iterator ei = eventIntervals.intervals.begin(); ei != eventIntervals.intervals.end();++ei)
{
if(ei->first.first != ei->second.first)
{
startTime = ei->first; break;
};
};
if((startTime.first != -1) && isProcessTriggered(ap->first))
{
ctsTriggeredEvents.push_back(make_pair(endAct, startTime));
};
};
};
vector<const Action*> eventsToTrigger;
CoScalar eventTriggerTime;
//extract the first group of events to be triggered (including processes)
for(vector<pair<const Action *,intervalEnd> >::iterator i = ctsTriggeredEvents.begin(); i != ctsTriggeredEvents.end(); ++i)
{
if(eventsToTrigger.empty())
{
eventTriggerTime = i->second.first;
eventsToTrigger.push_back(i->first);
}
else if(i->second.first == eventTriggerTime)
{
eventsToTrigger.push_back(i->first);
}
else if(i->second.first < eventTriggerTime)
{
eventTriggerTime = i->second.first;
for(vector<const Action*>::iterator k = eventsToTrigger.begin(); k != eventsToTrigger.end(); ++k) delete *k;
eventsToTrigger.clear();
eventsToTrigger.push_back(i->first);
}
else
delete i->first;
};
//trigger events
Happening * eventHappening;
updateHappeningTime(eventTriggerTime + currentTime);
bool triggerEventsExist = !eventsToTrigger.empty();
//remember the no loops rule, i.e. an event cannot be triggered more than once at a given time point, only the same time point as last trigger
//loop thro' events check if they have already been triggered (unless a process)
if(triggerEventsExist)
{
for(vector<const Action*>::const_iterator j = eventsToTrigger.begin(); j != eventsToTrigger.end(); ++j)
if(isTriggered(*j))
{
if(!ContinueAnyway) return false;
isOK = false;
};
};
if(triggerEventsExist && isOK)
{
ctsEventTriggered = true;
ace->setTime(eventTriggerTime + currentTime); //same time as event trigger time
ace->setLocalUpdateTime(eventTriggerTime);
if(eventTriggerTime != 0)
{
//execute invariant happening
ec->setActiveCtsEffects(ace);
ec->setTime(eventTriggerTime + currentTime); //same time as event trigger time
if(ec->hasInvariants()) {if(Verbose) cout << "\n"; isOK = v->executeHappening(ec->getInvariants()) && isOK;};
ec->setTime(nextTime); //same time as event trigger time
//execute cts update happening
if(Verbose) cout << "\n";
isOK = v->executeHappening(ace->getCtsEffectUpdate()) && isOK;
};
//execute event happening
if(Verbose) cout << "\n";
eventHappening = new Happening(v,eventsToTrigger,eventTriggerTime + currentTime,true);
eventHappening->adjustContext(*ec);
eventHappening->adjustActiveCtsEffects(*ace); //update cts effects for interval after event happening
isOK = v->executeHappeningCtsEvent(eventHappening) && isOK;
lastHappeningTime = eventTriggerTime + currentTime;
vector<const StartAction *> triggeredStartProcesses;
vector<const EndAction *> triggeredEndProcesses;
vector<const Action *> events;
//add to list of triggered events
for(vector<const Action*>::const_iterator j = eventsToTrigger.begin(); j != eventsToTrigger.end(); ++j)
{
(*j)->addTriggeredEvents(events,oldTriggeredEvents,triggeredStartProcesses,triggeredEndProcesses);
};
//check triggered events have falsified their preconditions and add to list of triggered events
isOK = checkPreconditionsAreNotSatisfied(&(v->getState()),events) && isOK;
for(vector<const Action *>::const_iterator te = events.begin(); te != events.end(); ++te) triggeredEvents.insert((*te)->getName0());
//add triggered processes to list of triggered processes
for(vector<const StartAction *>::const_iterator trs = triggeredStartProcesses.begin(); trs != triggeredStartProcesses.end(); ++trs)
{
pair<const Proposition *,const_symbol_list *> propcsl = startProcesses[*trs];
activeProcesses[*trs] = make_pair(propcsl.first,propcsl.second);
triggeredProcesses.insert(*trs);
startProcesses.erase(*trs);
};
//delete unused startProcesses' props and csls
for(map<const StartAction *, pair<const Proposition *,const_symbol_list *> >::iterator sp = startProcesses.begin(); sp != startProcesses.end(); ++sp)
{
sp->second.first->destroy(); delete sp->second.first;
};
//remove untriggered processes from list of triggered processes
for(vector<const EndAction *>::const_iterator trs = triggeredEndProcesses.begin(); trs != triggeredEndProcesses.end(); ++trs)
{
const StartAction * stAction = endStartMap[*trs];
map<const StartAction *, pair<const Proposition *,const_symbol_list *> >::iterator ap = activeProcesses.find(stAction);
if(ap != activeProcesses.end())
{
ap->second.first->destroy(); delete ap->second.second;
activeProcesses.erase(ap);
untriggeredProcesses.insert(stAction->getName0());
};
};
eventHappening->clearActions(); delete eventHappening;
//ready for triggering discrete events
ace->setTime(nextTime);
ace->setEventTime(eventTriggerTime + currentTime);
}
else
for(vector<const Action*>::iterator k = eventsToTrigger.begin(); k != eventsToTrigger.end(); ++k) delete *k;
if(EventPNEJuddering) JudderPNEs = false;
return isOK;
};
//for discretly triggered events
const vector<const_symbol_list*> getParametersDiscrete(goal * g,operator_ * op, const State & s,bool neg)
{
vector<const_symbol_list*> listOfparameters;
///Comparison
if(const comparison * comp = dynamic_cast<const comparison*>(g))
{
//get list of PNEs, then make list of parameter lists from them
set<const func_term*> pnes;
getPNEs(comp->getLHS(),pnes);
getPNEs(comp->getRHS(),pnes);
set<const FuncExp *> changedPNEs = s.getChangedPNEs();
for(set<const func_term*>::iterator pne = pnes.begin(); pne != pnes.end(); ++pne)
{
//has pne changed since last happening
for(set<const FuncExp *>::const_iterator fe = changedPNEs.begin(); fe != changedPNEs.end(); ++fe)
{
if(((*fe)->getName() == (*pne)->getFunction()->getName())
&& (*fe)->checkConstantsMatch((*pne)->getArgs())) //+check consts match!
{
const_symbol_list* parametersForEvent = new const_symbol_list();
for(var_symbol_list::iterator j = op->parameters->begin(); j != op->parameters->end(); ++j)
{
string para = getParameter(*fe,*j,*pne);
const_symbol * cs = para==""?0:s.getValidator()->getAnalysis()->const_tab.symbol_get(para);
parametersForEvent->push_back(cs);
}; //end of filling in parameters
//ground out undefineds here to produce list of parameter lists
//check the comp holds for each parameter list! use only parameters used in comp
set<var_symbol*> svs = getVariables(comp);
listOfparameters = defineUndefinedParametersPropVar(parametersForEvent,op,s.getValidator(),g,false,neg,svs);
};
};//end of loop for checking if pne could be a changed fe
}; //end of looping thro' pnes in expression
return listOfparameters;
};
//Literal
if(const simple_goal * sg = dynamic_cast<const simple_goal*>(g))
{
//handle derived predicates first!
string literalName = sg->getProp()->head->getName();
vector<const_symbol_list*> aParameterList;
if(s.getValidator()->getDerivRules()->isDerivedPred(literalName))
{
aParameterList.push_back(newBlankConstSymbolList(op->parameters,s.getValidator()));
return getParametersList(g,op,s.getValidator(),aParameterList,neg,true);
};
//check if literal is one that has changed since last happening
//if so fill in parameters with one from a simple prop
set<const SimpleProposition *> changedLiterals = s.getChangedLiterals();
for(set<const SimpleProposition *>::const_iterator sp = changedLiterals.begin(); sp != changedLiterals.end(); ++sp)
{
if( ((*sp)->getPropName() == literalName) && ((!neg && s.evaluate(*sp)) || (neg && !s.evaluate(*sp)))
&& (*sp)->checkConstantsMatch(sg->getProp()->args) )
{
const_symbol_list* parametersForEvent = new const_symbol_list();
//fill in parameters for event
for(var_symbol_list::iterator j = op->parameters->begin(); j != op->parameters->end(); ++j)
{
string para = getParameter(*sp,*j,sg);
const_symbol * cs = para==""?0:s.getValidator()->getAnalysis()->const_tab.symbol_get(para);
parametersForEvent->push_back(cs);
}; //end of filling in parameters
listOfparameters.push_back(parametersForEvent);
};
};
return listOfparameters;
};
//Negation
if(dynamic_cast<const neg_goal *>(g))
{
return getParametersDiscrete(const_cast<goal*>(dynamic_cast<const neg_goal *>(g)->getGoal()),op,s,!neg);
};
//Other
listOfparameters = getParameters(g,op,s.getValidator(),true,neg);
return listOfparameters;
};
goal * newQfiedGoal(const qfied_goal * qg,operator_ * op,Validator * v)
{
//get list of instansigated parameters for the qfied prop, so for forall(?z ?y), we have a grounded list for every z? and ?y
set<var_symbol*> svs = getVariables(qg);
vector<const_symbol_list*> constantsList = defineUndefinedParameters(newBlankConstSymbolList(const_cast<var_symbol_list*>(qg->getVars()),v),const_cast<var_symbol_list*>(qg->getVars()),v,svs);
//now create a conjunction or disjunction with the qfied variables substituted
map<parameter_symbol*,parameter_symbol*> newvars;
goal_list* theGoals = new goal_list();
for(vector<const_symbol_list*>::iterator k = constantsList.begin(); k != constantsList.end(); ++k)
{
const goal * aGoal = copyGoal(qg->getGoal());
//define mapping of parameter symbol to constant
const_symbol_list::iterator consList = (*k)->begin();
for(var_symbol_list::const_iterator i = qg->getVars()->begin(); i != qg->getVars()->end(); ++i)
{
newvars[const_cast<var_symbol*>(*i)] = *consList;
consList++;
};
//add conjunct/disjunct with qfied variables substituted
changeVars(const_cast<goal *>(aGoal),newvars);
theGoals->push_back(const_cast<goal *>(aGoal));
};
deleteParameters(constantsList);
goal * goalToCheck;
if(qg->getQuantifier()==E_FORALL)
{
goalToCheck = new conj_goal(theGoals);
}
else
{
goalToCheck = new disj_goal(theGoals);
};
return goalToCheck;