summaryrefslogtreecommitdiff
path: root/SA_POP/SA_WorkingPlan.h
blob: e9944e5fea0c0a79a50d4e0fa750ab77265aebf6 (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
// -*- C++ -*-
// $Id$

//=============================================================================
/**
 * @file  SA_WorkingPlan.h
 *
 * This file contains the definition of the SA_WorkingPlan concrete class,
 * which implements a WorkingPlan that uses precedence graphs for scheduling.
 *
 * @author  John S. Kinnebrew <john.s.kinnebrew@vanderbilt.edu>
 */
//=============================================================================

#ifndef SA_POP_SA_WORKING_PLAN_H_
#define SA_POP_SA_WORKING_PLAN_H_

#include <map>
#include "SA_POP_Types.h"
#include "WorkingPlan.h"
#include "PlanCommands.h"
#include "SA_PlanCommands.h"


namespace SA_POP {

  /**
   * @class SA_WorkingPlan
   *
   * @brief WorkingPlan concrete class for encapsulation of a plan that uses
   *        precedence graphs for scheduling.
   */
  class SA_WorkingPlan : public WorkingPlan {
  public:
    /// Constructor.
    /**
     * @param planner  Planner object to use.
     */
    SA_WorkingPlan (SA_POP::Planner *planner);

    /// Destructor.
    virtual ~SA_WorkingPlan (void);

    /// Set command prototypes to use.
    /**
     * @param add_threats_cmd  Prototype of command for adding open causal
     *                         link threats to be handled.
     */
    virtual void set_commands (AddOpenThreatsCmd *add_threats_cmd);

    /// Get current plan.
    /**
     * @return  Reference to current plan.
     */
    virtual const Plan& get_plan (void);

    /// Get set of task instances for a precedence relation to a task instance.
    /**
     * @param task_inst  Task instance for which to get precedence set.
     *
     * @param prec_rel  Precedence relation.
     *
     * @return  Set of task instances for given precedence relation to given
     *          task instance.
     */
    virtual const TaskInstSet *get_prec_set (TaskInstID task_inst,
      PrecedenceRelation prec_rel);

    /// Get task id of a task instance.
    /**
     * @param inst_id  The task instance id.
     *
     * @return  The task id of this task instance.
     */
    virtual TaskID get_task_from_inst (TaskInstID inst_id);

    /// Get task implementation id of a task instance.
    /**
     * @param inst_id  The task instance id.
     *
     * @return  The task implementation id of this task instance.
     */
	virtual TaskImplID get_task_impl_from_inst (TaskInstID inst_id);

	/// Get all current causal link threats.
    /**
     * @return  Set of all current causal link threats.
     */
    virtual CLThreatSet get_all_threats (void);

   //Get the Start Window for a task instance
    /**
     * @param task_inst The task instance of which the start window is returned
     *
     * @return The start window for the task instance
     */
    virtual TimeWindow get_start_window(TaskInstID task_inst);

    //Get the End Window for a task instance
    /**
     * @param task_inst The task instance of which the end window is returned
     *
     * @return The end window for the task instance
     */
    virtual TimeWindow get_end_window(TaskInstID task_inst);

    //Get the duration of a task instance
    /**
     * @param task_inst The task instance of which the duration is returned
     *
     * @return The duration of the task instance
     */
    virtual TimeValue get_duration(TaskInstID task_inst);

    //Get all the causal and data links TO the task instance
    /**
     * @param task_inst All causal and data links to this task instance have to be returned
     *
     * @return All causal and data links to this task instance
     */
    virtual CLSet get_before(TaskInstID task_inst);
    //Get all the causal and data links FROM the task instance
    /**
     * @param task_inst All causal and data links from this task instance have to be returned
     *
     * @return All causal and data links from this task instance
     */
    virtual CLSet get_after(TaskInstID task_inst);

    /// Get the Causal and Scheduling orderings to this task instance
    /**
     * @param inst_id The task instance to which all orderings are required
     */
  virtual TaskInstSet before_orderings (TaskInstID inst_id);

    /// Get the Causal and Scheduling orderings from this task instance
    /**
     * @param inst_id The task instance from which all orderings are required
     */
  virtual TaskInstSet after_orderings (TaskInstID inst_id);

  /// Adds the sched order to the sched_links_ map by putting the first task instance before the second
  /**
   * @param first_task_inst The task instance before the second one
   *
   * @param second_task_inst The task instance after the first one
   */
	virtual void add_sched_link(TaskInstID first_task_inst, TaskInstID second_task_inst);
	/// Removes the sched order from the sched_links_ map
  /**
   * @param first_task_inst The task instance before the second one
   *
   * @param second_task_inst The task instance after the first one
   */
	virtual void remove_sched_link(TaskInstID first_task_inst, TaskInstID second_task_inst);
    
	/// Get all the task instances
	virtual TaskInstSet get_all_insts();

     /// Check if the instance id already exists and is being reused.
    /**
     * @param task_inst The task instance being checked
     *
     * @return  True If this task instance already exists.
     */
    virtual bool inst_exists (TaskInstID task_inst);

    /// Get task implementation for a task instance.
    /**
     * @param task_inst  The task instance.
     *
     * @return The task implementation id.
     */
    virtual TaskImplID get_impl_id (TaskInstID task_inst);

    // ************************************************************************
    // Methods for obtaining prototypes of commands that work on this object.
    // ************************************************************************

    /// Get a PlanCommand prototype for adding a task to the plan.
    /**
     * @return  An AddTaskCmd prototype for this WorkingPlan.
     */
    virtual AddTaskCmd *get_AddTaskCmd (void);

    /// Get a PlanCommand prototype for associating an implementation with a
    /// task instance in the plan.
    /**
     * @return  An AssocTaskImplCmd prototype for this WorkingPlan.
     */
    virtual AssocTaskImplCmd *get_AssocTaskImplCmd (void);

    /// Get a PlanCommand prototype for resolving a causal link threat in the
    /// plan (with promotion or demotion).
    /**
     * @return  An ResolveCLThreatCmd prototype for this WorkingPlan.
     */
    virtual ResolveCLThreatCmd *get_ResolveCLThreatCmd (void);

    /// Get a PlanCommand prototype for resolving a scheduling conflict (i.e.
    /// non-causal-link ordering constraint (with promotion or demotion)
    /// between two task instances in the plan.
    /**
     * @return  An ResolveSchedOrderCmd prototype for this WorkingPlan.
     */
    virtual ResolveSchedOrderCmd *get_ResolveSchedOrderCmd (void);

    /// Get a PlanCommand prototype for adjusting the minimum times of a
    /// task instance in the plan.
    /**
     * @return  An AdjustStartWindowCmd prototype for this WorkingPlan.
     */
    virtual AdjustMinTimesCmd *get_AdjustMinTimesCmd (void);

    /// Get a PlanCommand prototype for adjusting the max times of a
    /// task instance in the plan.
    /**
     * @return  An AdjustEndWindowCmd prototype for this WorkingPlan.
     */
    virtual AdjustMaxTimesCmd *get_AdjustMaxTimesCmd (void);



    // ************************************************************************
    // Methods for executing and undoing commands.
    // ************************************************************************

    /// Execute a command to add a task to the plan.
    /**
     * @param cmd  Command object.
     */
    virtual void execute (SA_AddTaskCmd *cmd);

    /// Undo a command to add a task to the plan.
    /**
     * @param cmd  Command object.
     */
    virtual void undo (SA_AddTaskCmd *cmd);

    /// Execute a command to associate an implementation with a
    /// task instance in the plan.
    /**
     * @param cmd  Command object.
     */
    virtual bool execute (SA_AssocTaskImplCmd *cmd);

    /// Undo a command to associate an implementation with a
    /// task instance in the plan.
    /**
     * @param cmd  Command object.
     */
    virtual void undo (SA_AssocTaskImplCmd *cmd);

    /// Execute a command to resolve a causal link threat in the
    /// plan (with promotion or demotion).
    /**
     * @param cmd  Command object.
     */
    virtual void execute (SA_ResolveCLThreatCmd *cmd);

    /// Undo a command to resolve a causal link threat in the
    /// plan (with promotion or demotion).
    /**
     * @param cmd  Command object.
     */
    virtual void undo (SA_ResolveCLThreatCmd *cmd);

    /// Execute a command to resolve a scheduling conflict (i.e.
    /// non-causal-link ordering constraint with promotion or demotion)
    /// between two task instances in the plan.
    /**
     * @param cmd  Command object.
     *
     * @return Returns true if the new scheduling order is without any violations
     */
    virtual bool execute (SA_ResolveSchedOrderCmd *cmd);

    /// Undo a command to resolve a scheduling conflict (i.e.
    /// non-causal-link ordering constraint with promotion or demotion)
    /// between two task instances in the plan.
    /**
     * @param cmd  Command object.
     */
    virtual void undo (SA_ResolveSchedOrderCmd *cmd);

    /// Execute a command to adjust the minimum times of a
    /// task instance in the plan.
    /**
     * @param cmd  Command object.
     *
     * @return Returns true if the adjustment succeeded without any violations
     */
    virtual bool execute (SA_AdjustMinTimesCmd *cmd);

    /// Undo a command to adjust the minimum times of a
    /// task instance in the plan.
    /**
     * @param cmd  Command object.
     */
    virtual void undo (SA_AdjustMinTimesCmd *cmd);

    /// Execute a command to adjust the maximum times of a
    /// task instance in the plan.
    /**
     * @param cmd  Command object.
     *
     * @return Returns true if the adjustment succeeded without any violations
     */
    virtual bool execute (SA_AdjustMaxTimesCmd *cmd);

    /// Undo a command to adjust the maximum times of a
    /// task instance in the plan.
    /**
     * @param cmd  Command object.
     */
    virtual void undo (SA_AdjustMaxTimesCmd *cmd);

  protected:
    // ************************************************************************
    // State information.
    // ************************************************************************

    /// Flag for whether command prototypes have been set.
    bool has_cmds_;

    /// Task instance ID to use for next instance created.
    TaskInstID next_inst_id_;

    typedef std::map <TaskInstID, TaskID> InstToTaskMap;
    /// Task instances in plan (mapping to task id).
    InstToTaskMap task_insts_;

    /// Implementations of task instances in plan (mapping from task instance).
    InstToImplMap task_impls_;

    // Type of a map from conditions to causal links.
    typedef std::multimap <Condition, CausalLink> CondToCLinksMap;

    /// Causal links in plan (mapping from conditions to all causal links
    /// containing that condition).
    CondToCLinksMap causal_links_;

    /// Current plan (generated upon request).
    Plan plan_;

    /// Helper function to create next instance id.
    virtual TaskInstID get_next_inst_id (void);

  	/// Type of particular Task Implementation mapped to a Task Implementation Set.
  	/// This is a particular Precedence set. Like Before, After etc.
	  typedef std::map <TaskInstID, TaskInstSet> PrecedenceSet;

	  /// Type of a Precedence Relation mapped to a Precedence Set. 
	  /// This gives the whole Precedence Graph
	  typedef std::map <PrecedenceRelation, PrecedenceSet> PrecedenceGraph;

	/// PrecedenceGraph
	PrecedenceGraph precedence_graph_;
	
	/// Type of an ordering imposed during Scheduling.
	/// First task instance is ordered before the second.
	typedef std::multimap <TaskInstID, TaskInstID> SchedulingLinks;
	
	/// Map of the imposed orderings
	SchedulingLinks sched_links_;

	/// Map from task instances to the time windows
	typedef std::map <TaskInstID, TimeWindow> InstToWinMap;
	typedef std::map <TaskInstID, TimeWindow> InstToWinMap;

	/// InstToWinMap and InstToWinMap
	InstToWinMap start_window_map_;
	InstToWinMap end_window_map_;
	
  /// Type of a map of task instances and the durations
  typedef std::map <TaskInstID, TimeValue> DurationMap;

  DurationMap durations;

  // The initial start and end windows. There has to be something 
  // in this for every task instance be it {NULL_TIME,NULL_TIME}
  InstToWinMap init_start;
	InstToWinMap init_end;

	// The set of reused task instances
	std::multiset<TaskInstID> reused_insts;
  /// Insert initially task by task in the precedence graph
  /**
   * @param task_inst The task instance to insert into the precedence graph
   *
   * @param cmd The task Command executing this
   *
   * @return True if this can be successfully added into the precedence graph
   */
	virtual bool init_prec_insert(TaskInstID task_inst, SA_AssocTaskImplCmd *cmd);

/// prints the precedence graph
  	virtual void print_prec(void);

	/// Erases a task instance from the precedence graph
	/**
	 * @param task_instance The task instance to be erased
   *
   * @param cmd The task Command executing this
   *
	 */
	virtual void prec_erase(TaskInstID task_inst, SA_AssocTaskImplCmd *cmd);


    // ************************************************************************
    // Prototypes of commands that work on this object.
    // ************************************************************************

    /// PlanCommand prototype for adding a task to the plan.
    SA_AddTaskCmd *add_task_cmd_;

    /// PlanCommand prototype for associating an implementation with a
    /// task instance in the plan.
    SA_AssocTaskImplCmd *assoc_impl_cmd_;

    /// PlanCommand prototype for resolving a causal link threat in the
    /// plan (with promotion or demotion).
    SA_ResolveCLThreatCmd *resolve_threat_cmd_;

    /// PlanCommand prototype for resolving a scheduling conflict (i.e.
    /// non-causal-link ordering constraint (with promotion or demotion)
    /// between two task instances in the plan.
    SA_ResolveSchedOrderCmd *resolve_sched_cmd_;

    /// PlanCommand prototype for adjusting the minimum times of a
    /// task instance in the plan.
    SA_AdjustMinTimesCmd *adj_min_times_cmd_;

    /// PlanCommand prototype for adjusting the maximum times of a
    /// task instance in the plan.
    SA_AdjustMaxTimesCmd *adj_max_times_cmd_;



    // ************************************************************************
    // Prototypes of commands that work on other objects.
    // ************************************************************************

    /// PlanCommand prototype for adding open causal link threats to be
    /// handled.
    AddOpenThreatsCmd *add_threats_cmd_;
  };

};  /* SA_POP namespace */

#endif /* SA_POP_SA_WORKING_PLAN_H_ */