summaryrefslogtreecommitdiff
path: root/SA_PlanCommands.cpp
blob: 6f2b22764b4314cf27c524a189e0cf9e4728934d (plain)
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
// -*- C++ -*-
// $Id$

//=============================================================================
/**
 * @file  SA_PlanCommands.cpp
 *
 * This file contains the implementation of the concrete classes,
 * which implement PlanCommand.
 *
 * @author  John S. Kinnebrew <john.s.kinnebrew@vanderbilt.edu>
 */
//=============================================================================

#include "SA_POP_Types.h"
#include "SA_PlanCommands.h"
#include "SA_WorkingPlan.h"
#include "SA_PlanStrategy.h"
#include <string>
#include <stdlib.h>
#include <fstream>
#include <iostream>

using namespace SA_POP;

// Constructor.
SA_AdjustMinTimesCmd::SA_AdjustMinTimesCmd (SA_WorkingPlan *working_plan)
: working_plan_ (working_plan)
{
  //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP
};

// Destructor.
SA_AdjustMinTimesCmd::~SA_AdjustMinTimesCmd (void)
{
  //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP
};

// Create a deep copy of this command.
PlanCommand *SA_AdjustMinTimesCmd::clone (void)
{
  return new SA_AdjustMinTimesCmd (*this);
};

// Execute next option for this command.
bool SA_AdjustMinTimesCmd::execute_next (void)
{
  //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP
//  SA_POP_DEBUG_STR (SA_POP_DEBUG_NORMAL, this->get_log_text ());
//  this->working_plan_->execute (this);
  SA_POP_DEBUG_STR (SA_POP_DEBUG_NORMAL, this->get_log_text ());
  return this->working_plan_->execute (this);
};

// Undo this command.
void SA_AdjustMinTimesCmd::undo (void)
{
  //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP
  this->working_plan_->undo (this);
};

// Get log text for most recent execution of command.
std::string SA_AdjustMinTimesCmd::get_log_text (void)
{
  std::string log_str = "";
  log_str += "Adjusting Schedule Minimum Times (CommandID ";
  log_str += to_string (this->get_id ());
  //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP
  log_str += "): [NOT HANDLING FULL SCHEDULING YET]";

  return log_str;
};

// Set the task instance and window adjustment.
void SA_AdjustMinTimesCmd::set_times (TaskInstID task_inst, TimeValue start_min,
TimeValue end_min)
{
  //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP
  TimeWindow start_old = this->working_plan_->get_start_window(task_inst);
  TimeWindow end_old = this->working_plan_->get_end_window(task_inst);
  this->task_inst_=task_inst;
  this->old_start_min = start_old.second;
  this->old_end_min = end_old.second;
  this->new_start_min = start_min;
  this->new_end_min = end_min;
};



// Constructor.
SA_AdjustMaxTimesCmd::SA_AdjustMaxTimesCmd (SA_WorkingPlan *working_plan)
: working_plan_ (working_plan)
{
  //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP
};

// Destructor.
SA_AdjustMaxTimesCmd::~SA_AdjustMaxTimesCmd (void)
{
  //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP
};

// Create a deep copy of this command.
PlanCommand *SA_AdjustMaxTimesCmd::clone (void)
{
  return new SA_AdjustMaxTimesCmd (*this);
};

// Execute next option for this command.
bool SA_AdjustMaxTimesCmd::execute_next (void)
{
  //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP
//  SA_POP_DEBUG_STR (SA_POP_DEBUG_NORMAL, this->get_log_text ());
//  this->working_plan_->execute (this);
  SA_POP_DEBUG_STR (SA_POP_DEBUG_NORMAL, this->get_log_text ());
  return this->working_plan_->execute (this);
};

// Undo this command.
void SA_AdjustMaxTimesCmd::undo (void)
{
  //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP
  this->working_plan_->undo (this);
};

// Get log text for most recent execution of command.
std::string SA_AdjustMaxTimesCmd::get_log_text (void)
{
  std::string log_str = "";
  log_str += "Adjusting Schedule Maximum Times (CommandID ";
  log_str += to_string (this->get_id ());
  //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP
  log_str += "): [NOT HANDLING FULL SCHEDULING YET]";

  return log_str;
};

// Set the task instance and window adjustment.
void SA_AdjustMaxTimesCmd::set_times (TaskInstID task_inst, TimeValue start_max,
TimeValue end_max)
{
  //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP
  TimeWindow start_old = this->working_plan_->get_start_window(task_inst);
  TimeWindow end_old = this->working_plan_->get_end_window(task_inst);
  this->task_inst_=task_inst;
  this->old_start_max = start_old.second;
  this->old_end_max = end_old.second;
  this->new_start_max = start_max;
  this->new_end_max = end_max;
  // std::cout<<"setting max times "<<start_max<<" "<<end_max<<std::endl;
};



// Constructor.
SA_AddTaskCmd::SA_AddTaskCmd (SA_WorkingPlan *working_plan)
: working_plan_ (working_plan),
last_task_ (SA_POP::NULL_TASK_ID),
last_task_inst_ (SA_POP::NULL_TASK_INST_ID),
num_tries_ (0)
{
	this->tasks_.clear ();
  this->cond_.id = SA_POP::NULL_COND_ID;
  this->task_insts_.clear ();
  this->used_task_insts_.clear();
};

// Destructor.
SA_AddTaskCmd::~SA_AddTaskCmd (void)
{
  // Nothing to do.
};

// Create a deep copy of this command.
PlanCommand *SA_AddTaskCmd::clone (void)
{
  return new SA_AddTaskCmd (*this);
};

// Execute next option for this command.
bool SA_AddTaskCmd::execute_next (void)
{

  SA_POP_DEBUG_STR (SA_POP_DEBUG_NORMAL, this->get_log_text ());


  this->undo();



  bool isInitial = false;

  if (this->tasks_.empty())
    return false;

  /*
  if(this->tasks_.front().task_id == 20 && this->tasks_.front){
    isInitial = true;
  }
  */
  this->working_plan_->execute (this);
  this->num_tries_++;

  return true;
};

// Undo this command.
void SA_AddTaskCmd::undo (void)
{
  if (this->last_task_ == SA_POP::NULL_TASK_ID)
    return;

  this->working_plan_->undo (this);
  
  if (this->tasks_.empty ()) {
    this->task_insts_.clear ();
    this->cond_.id = SA_POP::NULL_COND_ID;
    this->last_task_ = SA_POP::NULL_TASK_ID;
  }
};

// Get log text for most recent execution of command.
std::string SA_AddTaskCmd::get_log_text (void)
{
  std::string log_str = "";
  log_str += "Adding Task (CommandID ";
  log_str += to_string (this->get_id ());
  log_str += "): ";
  TaskChoiceList::iterator task_iter = this->tasks_.begin ();
  if (task_iter == this->tasks_.end ())
    log_str += "[NO TASKS TO ADD]";
  else
    log_str += to_string (task_iter->task_id);

  return log_str;
};

// Set (ordered) list of tasks to add (one per execution) to the plan.
void SA_AddTaskCmd::set_tasks (const TaskChoiceList &tasks)
{
  if (!this->tasks_.empty ())
    throw "SA_POP::SA_AddTaskCmd::set_tasks (): called while current task list is not empty.";

  if (this->last_task_ != SA_POP::NULL_TASK_ID)
    throw "SA_POP::SA_AddTaskCmd::set_tasks (): called before last task was undone.";

  this->tasks_ = tasks;
};

// Set causal link info to add to the plan with task.
void SA_AddTaskCmd::set_causal_info (Condition cond, TaskInstSet task_insts)
{
  if (!this->task_insts_.empty ())
    throw "SA_POP::SA_AddTaskCmd::set_causal_info (): called while current task instance set is not empty.";

  if (this->cond_.id != SA_POP::NULL_COND_ID)
    throw "SA_POP::SA_AddTaskCmd::set_causal_info (): called while current condition is already set.";

  this->cond_ = cond;
  this->task_insts_ = task_insts;
};

// Get ID of last task added to the plan by this command.
TaskID SA_AddTaskCmd::get_task (void)
{
  return this->last_task_;
};

// Get ID of instance of last task added to the plan by this command.
TaskInstID SA_AddTaskCmd::get_task_inst (void)
{
  return this->last_task_inst_;
};

TaskInstSet SA_AddTaskCmd::get_satisfied_tasks(void){
  return this->task_insts_;
}

/// Check if the instance id used by the task of this command already exists.


bool SA_AddTaskCmd::inst_exists (void)
{
	return (this->last_task_choice_.choice == REUSE_INST);

}

// Constructor.
SA_AssocTaskImplCmd::SA_AssocTaskImplCmd (SA_WorkingPlan *working_plan)
: working_plan_ (working_plan),
task_inst_ (SA_POP::NULL_TASK_INST_ID),
last_impl_ (SA_POP::NULL_TASK_IMPL_ID),
num_tries_ (0),
min_adj_cmd(working_plan),
max_adj_cmd(working_plan)
{
  this->causal_insertions.clear();
  this->simul_insertions.clear();
  this->impls_.clear ();
};

// Destructor.
SA_AssocTaskImplCmd::~SA_AssocTaskImplCmd (void)
{
  // Nothing to do.
};

// Create a deep copy of this command.
PlanCommand *SA_AssocTaskImplCmd::clone (void)
{
  return new SA_AssocTaskImplCmd (*this);
};

// Execute next option for this command.
bool SA_AssocTaskImplCmd::execute_next (void)
{
  SA_POP_DEBUG_STR (SA_POP_DEBUG_NORMAL, this->get_log_text ());

  	if(get_id().step == 12 && get_id().decision_pt == 2 && get_id().seq_num == 1){
		bool k = true;
	}

  this->undo();

  got_to_scheduling = false;


  if (this->impls_.empty ()){
    return false;
  }


  this->num_tries_++;
  return this->working_plan_->execute (this);
  //return true;
};

// Undo this command.
void SA_AssocTaskImplCmd::undo (void)
{
  if (this->last_impl_ == SA_POP::NULL_TASK_IMPL_ID)
    return;

  this->working_plan_->undo (this);
  this->last_impl_ = SA_POP::NULL_TASK_IMPL_ID;

  if (this->impls_.empty ())
    this->task_inst_ = SA_POP::NULL_TASK_INST_ID;
};

// Get log text for most recent execution of command.
std::string SA_AssocTaskImplCmd::get_log_text (void)
{
  std::string log_str = "";
  log_str += "Associating Task(Instance)->Implementation (CommandID ";
  log_str += to_string (this->get_id ());
  log_str += "): ";
  log_str += to_string (this->working_plan_->get_task_from_inst (this->task_inst_));
  log_str += "(";
  log_str += to_string (this->task_inst_);
  log_str += ")->";
  TaskImplList::iterator impl_iter = this->impls_.begin ();
  if (impl_iter == this->impls_.end ())
    log_str += "[NO IMPLEMENTATIONS TO ASSOCIATE]";
  else
    log_str += *impl_iter;

  return log_str;
};

// Set the task->implementation associations.
void SA_AssocTaskImplCmd::set_assoc (TaskInstID task_inst,
                                     TaskImplList impl_list)
{
  if (!this->impls_.empty ())
    throw "SA_POP::SA_AssocTaskImplCmd::set_assoc (): called while current task implementation list is not empty.";

  if (this->last_impl_ != SA_POP::NULL_TASK_IMPL_ID)
    throw "SA_POP::SA_AssocTaskImplCmd::set_assoc (): called before last execution was undone.";

  this->impls_ = impl_list;
  this->task_inst_ = task_inst;
};

// Get ID of instance of this command.
TaskInstID SA_AssocTaskImplCmd::get_task_inst (void)
{
  return this->task_inst_;
};


// Constructor.
SA_AddOpenCondsCmd::SA_AddOpenCondsCmd (SA_PlanStrategy *plan_strat)
: plan_strat_ (plan_strat),
task_inst_ (SA_POP::NULL_TASK_INST_ID),
has_executed_ (false)
{
  this->conds_.clear ();
};

// Destructor.
SA_AddOpenCondsCmd::~SA_AddOpenCondsCmd (void)
{
  // Nothing to do.
};

// Create a deep copy of this command.
PlanCommand *SA_AddOpenCondsCmd::clone (void)
{
  return new SA_AddOpenCondsCmd (*this);
};

// Execute next option for this command.
bool SA_AddOpenCondsCmd::execute_next (void)
{
  SA_POP_DEBUG_STR (SA_POP_DEBUG_NORMAL, this->get_log_text ());

  if (this->conds_.empty ())
    return false;

  if (this->task_inst_ == SA_POP::NULL_TASK_INST_ID)
    throw "SA_POP::SA_AddOpenCondsCmd::execute_next (): called without task instance set.";

  this->undo();
  this->plan_strat_->execute (this);
  this->has_executed_ = true;
  return true;
};

// Undo this command.
void SA_AddOpenCondsCmd::undo (void)
{
  if (!this->has_executed_ || this->conds_.empty ())
    return;

  this->plan_strat_->undo (this);
  this->task_inst_ = SA_POP::NULL_TASK_INST_ID;
  this->conds_.clear ();
};

// Get log text for most recent execution of command.
std::string SA_AddOpenCondsCmd::get_log_text (void)
{
  std::string log_str = "";
  log_str += "Adding Open Conditions (CommandID ";
  log_str += to_string (this->get_id ());
  log_str += "):";
  for (CondSet::iterator cond_iter = this->conds_.begin ();
    cond_iter != this->conds_.end (); cond_iter++)
  {
    log_str += " ";
    log_str += to_string ((*cond_iter).id);
    log_str += " = ";
    if ((*cond_iter).value)
      log_str += "TRUE;";
    else
      log_str += "FALSE;";
  }

  return log_str;
};

// Set the open conditions to add.
void SA_AddOpenCondsCmd::set_conds (const CondSet &conds)
{
  if (!this->conds_.empty ())
    throw "SA_POP::SA_AddOpenCondsCmd::set_conds (): called while current condition set is not empty.";

  this->conds_ = conds;
};

// Set the task instance for which these are preconditions.
void SA_AddOpenCondsCmd::set_task_inst (TaskInstID task_inst)
{
  if (this->task_inst_ != SA_POP::NULL_TASK_INST_ID)
    throw "SA_POP::SA_AddOpenCondsCmd::set_conds (): called before last command was undone.";

  this->task_inst_ = task_inst;
};



// Constructor.
SA_RemoveOpenCondsCmd::SA_RemoveOpenCondsCmd (SA_PlanStrategy *plan_strat)
: plan_strat_ (plan_strat)
{
  this->conds_.clear ();
  this->removed_.clear ();
};

// Destructor.
SA_RemoveOpenCondsCmd::~SA_RemoveOpenCondsCmd (void)
{
  // Nothing to do.
};

// Create a deep copy of this command.
PlanCommand *SA_RemoveOpenCondsCmd::clone (void)
{
  return new SA_RemoveOpenCondsCmd (*this);
};

// Execute next option for this command.
bool SA_RemoveOpenCondsCmd::execute_next (void)
{
  SA_POP_DEBUG_STR (SA_POP_DEBUG_NORMAL, this->get_log_text ());

  if (this->conds_.empty ())
    return false;

  this->undo();
  this->plan_strat_->execute (this);
  return true;
};

// Undo this command.
void SA_RemoveOpenCondsCmd::undo (void)
{
  if (this->removed_.empty ())
    return;

  this->plan_strat_->undo (this);
  this->removed_.clear ();
  this->conds_.clear ();
};

// Get log text for most recent execution of command.
std::string SA_RemoveOpenCondsCmd::get_log_text (void)
{
  std::string log_str = "";
  log_str += "Removing Open Conditions (CommandID ";
  log_str += to_string (this->get_id ());
  log_str += "):";
  for (CondSet::iterator cond_iter = this->conds_.begin ();
    cond_iter != this->conds_.end (); cond_iter++)
  {
    log_str += " ";
    log_str += to_string ((*cond_iter).id);
    log_str += " = ";
    if ((*cond_iter).value)
      log_str += "TRUE;";
    else
      log_str += "FALSE;";
  }

  return log_str;
};

// Set the open conditions to remove.
void SA_RemoveOpenCondsCmd::set_conds (const CondSet &conds, const TaskInstSet &tasks)
{
  if (!this->conds_.empty ())
    throw "SA_POP::SA_RemoveOpenCondsCmd::set_conds (): called while current condition set is not empty.";

  if (!this->removed_.empty ())
    throw "SA_POP::SA_RemoveOpenCondsCmd::set_conds (): called before last execution undone.";

  this->conds_ = conds;
  this->tasks_ = tasks;
};



// Constructor.
SA_AddOpenThreatsCmd::SA_AddOpenThreatsCmd (SA_PlanStrategy *plan_strat)
: plan_strat_ (plan_strat),
has_executed_ (false)
{
  this->threats_.clear ();
};

// Destructor.
SA_AddOpenThreatsCmd::~SA_AddOpenThreatsCmd (void)
{
  // Nothing specific to destruct
};

// Create a deep copy of this command.
PlanCommand *SA_AddOpenThreatsCmd::clone (void)
{
  return new SA_AddOpenThreatsCmd (*this);
};

// Execute next option for this command.
bool SA_AddOpenThreatsCmd::execute_next (void)
{
  SA_POP_DEBUG_STR (SA_POP_DEBUG_NORMAL, this->get_log_text ());

  if (this->threats_.empty ())
    return false;

  this->undo();
  this->plan_strat_->execute (this);
  this->has_executed_ = true;
  return true;
};

// Undo this command.
void SA_AddOpenThreatsCmd::undo (void)
{
  if (!this->has_executed_ || this->threats_.empty ())
    return;

  this->plan_strat_->undo (this);
  this->threats_.clear ();
};

// Get log text for most recent execution of command.
std::string SA_AddOpenThreatsCmd::get_log_text (void)
{
  std::string log_str = "";
  log_str += "Adding Open Threats (CommandID ";
  log_str += to_string (this->get_id ());
  //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP
  log_str += "): [NOT HANDLING THREATS YET]";

  return log_str;
};

// Set the open threats to add.
void SA_AddOpenThreatsCmd::set_threats (const CLThreatSet & threats)
{
    if (!this->threats_.empty ())
    throw "SA_POP::SA_AddOpenThreatsCmd::set_conds (): called while current Threat set is not empty.";

  this->threats_ = threats;
};



// Constructor.
SA_RemoveOpenThreatsCmd::SA_RemoveOpenThreatsCmd (SA_PlanStrategy *plan_strat)
: plan_strat_ (plan_strat),
executed_ (false)
{
  this->threats_.clear ();
};

// Destructor.
SA_RemoveOpenThreatsCmd::~SA_RemoveOpenThreatsCmd (void)
{
  //Nothing to destruct
};

// Create a deep copy of this command.
PlanCommand *SA_RemoveOpenThreatsCmd::clone (void)
{
  return new SA_RemoveOpenThreatsCmd (*this);
};

// Execute next option for this command.
bool SA_RemoveOpenThreatsCmd::execute_next (void)
{
  SA_POP_DEBUG_STR (SA_POP_DEBUG_NORMAL, this->get_log_text ());

  if (this->threats_.empty ())
    return false;

  this->undo();
  this->plan_strat_->execute (this);
  this->executed_ = true;
  return true;
};

// Undo this command.
void SA_RemoveOpenThreatsCmd::undo (void)
{  if (!(this->executed_) || (this->threats_.empty ()))
    return;

  this->plan_strat_->undo (this);
  this->threats_.clear ();
};

// Get log text for most recent execution of command.
std::string SA_RemoveOpenThreatsCmd::get_log_text (void)
{
  std::string log_str = "";
  log_str += "Removing Open Threats (CommandID ";
  log_str += to_string (this->get_id ());
  //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP
  log_str += "): [NOT HANDLING THREATS YET]";

  return log_str;
};

// Set the open threats to remove.
void SA_RemoveOpenThreatsCmd::set_threats (const CLThreatSet & threats)
{
  if (!this->threats_.empty ())
    throw "SA_POP::SA_RemoveOpenThreatsCmd::set_threats (): called while current threat set is not empty.";


  this->threats_ = threats;
};



// Constructor.
SA_ResolveCLThreatCmd::SA_ResolveCLThreatCmd (SA_WorkingPlan *working_plan)
: working_plan_ (working_plan)
{
  choices = 0;
};

// Destructor.
SA_ResolveCLThreatCmd::~SA_ResolveCLThreatCmd (void)
{
  //No Temps Necessary, this does nothing
};

// Create a deep copy of this command.
PlanCommand *SA_ResolveCLThreatCmd::clone (void)
{
  return new SA_ResolveCLThreatCmd (*this);
};

// Execute next option for this command.
bool SA_ResolveCLThreatCmd::execute_next (void)
{
  bool goodOption = true;

  SA_POP_DEBUG_STR (SA_POP_DEBUG_NORMAL, this->get_log_text ());
  if(choices == 0)
  {
	got_to_change_precedences = false;

    //Add this next line to execute function
    this->working_plan_->add_sched_link(this->threat.threat,this->threat.clink.first);
    choices++;
    this->first = this->threat.threat;
    this->second = this->threat.clink.first;
    this->condition = this->threat.clink.cond;

      
    goodOption =  this->working_plan_->execute (this);

    if(goodOption)
    {
      return goodOption;
    }

  }

  if(choices == 1)
  {
     this->undo();

	 got_to_change_precedences = false;

    //Add this next line to execute function
     this->working_plan_->add_sched_link(this->threat.clink.second, this->threat.threat);
     choices++;
     this->second = this->threat.threat;
     this->first = this->threat.clink.second;
    this->condition = this->threat.clink.cond;

	  goodOption = this->working_plan_->execute (this);

    if(goodOption)
    {
      return goodOption;
    }
    else
    {
        return false;
    }


  }
  else
  {
    return false;
  }
	
  
};

// Undo this command.
void SA_ResolveCLThreatCmd::undo (void)
{
    SA_POP_DEBUG_STR (SA_POP_DEBUG_NORMAL, this->get_log_text ());
  if(choices == 1)
  {
    //Add this next line to execute function
    this->working_plan_->remove_sched_link(this->threat.threat,this->threat.clink.first);
	  return this->working_plan_->undo (this);
  }
  else if(choices == 2)
  {
    //Add this next line to execute function
     this->working_plan_->remove_sched_link(this->threat.clink.second, this->threat.threat);
	   return this->working_plan_->undo (this);
  }
};

// Get log text for most recent execution of command.
std::string SA_ResolveCLThreatCmd::get_log_text (void)
{
  std::string log_str = "";
  log_str += "Resolving Open Threat (CommandID ";
  log_str += to_string (this->get_id ());
  //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP
  log_str += "): [NOT HANDLING THREATS YET]";

  return log_str;
};

// Set the causal link threat to resolve.
void SA_ResolveCLThreatCmd::set_threat (CLThreat & tht)
{
  threat = tht;
};

// Set the task instances to order.
void SA_ResolveCLThreatCmd::set_task_insts (TaskInstID task_inst_a, TaskInstID task_inst_b)
{
	this->first = task_inst_a;
	this->second = task_inst_b;
};


// Constructor.
SA_ResolveSchedOrderCmd::SA_ResolveSchedOrderCmd (SA_WorkingPlan *working_plan)
: working_plan_ (working_plan)
{
  //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP
};

// Destructor.
SA_ResolveSchedOrderCmd::~SA_ResolveSchedOrderCmd (void)
{
	this->adj_max_times_cmd_=NULL;
	this->adj_min_times_cmd_=NULL;
};

// Create a deep copy of this command.
PlanCommand *SA_ResolveSchedOrderCmd::clone (void)
{
  return new SA_ResolveSchedOrderCmd (*this);
};

// Execute next option for this command.
bool SA_ResolveSchedOrderCmd::execute_next (void)
{
	SA_POP_DEBUG_STR (SA_POP_DEBUG_NORMAL, this->get_log_text ());
	this->working_plan_->add_sched_link(this->first,this->second);
	return this->working_plan_->execute (this);
};

// Undo this command.
void SA_ResolveSchedOrderCmd::undo (void)
{
	this->working_plan_->remove_sched_link(this->first,this->second);
	this->working_plan_->undo (this);
};

// Get log text for most recent execution of command.
std::string SA_ResolveSchedOrderCmd::get_log_text (void)
{
  std::string log_str = "";
  log_str += "Resolving Schedule Ordering (CommandID ";
  log_str += to_string (this->get_id ());
  //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP
  log_str += "): [NOT HANDLING FULL SCHEDULING YET]";

  return log_str;
};

// Set the task instances to order.
void SA_ResolveSchedOrderCmd::set_task_insts (TaskInstID task_inst_a, TaskInstID task_inst_b)
{
	this->first = task_inst_a;
	this->second = task_inst_b;
};

/// Get the affected task instances.
TaskInstSet SA_ResolveSchedOrderCmd::get_affected_insts ()
{
	TaskInstSet affected_insts;
	affected_insts.insert(this->first);
	affected_insts.insert(this->second);
	for(ResolveSchedOrderCmdList::iterator iter=this->cmds_.begin();iter!=this->cmds_.end();iter++)
	{
    TaskInstSet temp=(*iter)->get_affected_insts();
    for(TaskInstSet::iterator iter2=temp.begin();iter2!=temp.end();iter2++)
    {
      if(*iter2!=this->first && *iter2!=this->second) affected_insts.insert(*iter2);
    }
  }
	return affected_insts;
};