-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongTermTimeSlot.cpp
101 lines (84 loc) · 2.76 KB
/
LongTermTimeSlot.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "classes.hpp"
using namespace std;
LongTermTimeSlot::LongTermTimeSlot(ProjectHandler *p, unsigned int index, time_t startDate, time_t endDate, bool available)
{
this->projectHandler = p;
this->longTermSlotIndex = index;
this->startDate = startDate;
this->endDate = endDate;
this->availability = available;
// Stop here if time slot unavailable.
if (!this->availability)
return;
// Initialize execFeasibility map
list<Project*> projects = this->projectHandler->getPendingProjects();
for(list<Project*>::iterator i = projects.begin(); i != projects.end(); ++i) {
list<SchedBlock*> s = (*i)->getPendingSchedBlocks();
for(list<SchedBlock*>::iterator j = s.begin(); j != s.end(); ++j) {
if(LongTermTimeSlot::calculateFeasibility(*j)) {
execFeasibility.push_back(*j);
(*j)->registerObserver(this);
}
}
}
}
LongTermTimeSlot::~LongTermTimeSlot()
{
// Unregister from observed SchedBlocks
//TODO: This is missing schedBlocks that were executed in the meantime.
list<SchedBlock*>::iterator it;
for (it=execFeasibility.begin(); it!=execFeasibility.end(); it++)
(*it)->unregisterObserver(this);
}
bool LongTermTimeSlot::isAvailable(){
return this->availability;
}
list<SchedBlock*> LongTermTimeSlot::getFeasibleSchedBlocks(){
return this->execFeasibility;
}
bool LongTermTimeSlot::getFeasibility(SchedBlock* schedBlock){
list<SchedBlock*>::iterator it;
for(it = execFeasibility.begin(); it != execFeasibility.end(); ++it) {
if((*it) == schedBlock)
return true;
}
return false;
}
time_t LongTermTimeSlot::getStartDate(){
return this->startDate;
}
time_t LongTermTimeSlot::getEndDate(){
return this->endDate;
}
unsigned int LongTermTimeSlot::getIndex(){
return longTermSlotIndex;
}
bool LongTermTimeSlot::calculateFeasibility(SchedBlock* sb){
// In this case, it is sufficient if sources are visible at some
// point of the interval (not the entire one).
time_t start;
time_t duration = 60*60; //TODO: Get this from somewhere (considering 1 hour intervals)
for (start=this->startDate; start+duration-1 <= this->endDate; start+=duration)
if (sb->isVisible(start, start+duration-1))
return true;
return false;
}
unsigned int LongTermTimeSlot::getSBListSize()
{
return this->execFeasibility.size();
}
void LongTermTimeSlot::notify(Observable* o)
{
// Notification by SchedBlock
SchedBlock* schedBlockSubject = dynamic_cast<SchedBlock*>(o);
if (schedBlockSubject != NULL) {
// cout << "-- LongTermTimeSlot received SchedBlock notification." << endl;
// Remove executed SchedBlock from feasibility list
if (schedBlockSubject->isExecuted()) {
// schedBlockSubject->unregisterObserver(this);
execFeasibility.remove(schedBlockSubject);
}
return;
}
cerr << "ERROR: LongTermTimeSlot received notification from unknown subject." << endl;
}