| 1 |
/* This file is part of the KDE project |
| 2 |
* SPDX-FileCopyrightText: 2009, 2010, 2011, 2012 Dag Andersen <[email protected]> |
| 3 |
* SPDX-FileCopyrightText: 2016 Dag Andersen <[email protected]> |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: LGPL-2.0-or-later |
| 6 |
*/ |
| 7 |
|
| 8 |
// clazy:excludeall=qstring-arg |
| 9 |
#include "PlanTJScheduler.h" |
| 10 |
|
| 11 |
#include "kptproject.h" |
| 12 |
#include "kptschedule.h" |
| 13 |
#include "kptresource.h" |
| 14 |
#include "kpttask.h" |
| 15 |
#include "kptrelation.h" |
| 16 |
#include "kptduration.h" |
| 17 |
#include "kptcalendar.h" |
| 18 |
#include "kptdebug.h" |
| 19 |
|
| 20 |
#include <KoDocument.h> |
| 21 |
#include <ExtraProperties.h> |
| 22 |
|
| 23 |
#include "taskjuggler/taskjuggler.h" |
| 24 |
#include "taskjuggler/Project.h" |
| 25 |
#include "taskjuggler/Scenario.h" |
| 26 |
#include "taskjuggler/Resource.h" |
| 27 |
#include "taskjuggler/Task.h" |
| 28 |
#include "taskjuggler/Interval.h" |
| 29 |
#include "taskjuggler/Allocation.h" |
| 30 |
#include "taskjuggler/Utility.h" |
| 31 |
#include "taskjuggler/UsageLimits.h" |
| 32 |
#include "taskjuggler/CoreAttributes.h" |
| 33 |
#include "taskjuggler/TjMessageHandler.h" |
| 34 |
#include "taskjuggler/debug.h" |
| 35 |
|
| 36 |
#include <QString> |
| 37 |
#include <QTime> |
| 38 |
#include <QMutexLocker> |
| 39 |
#include <QMap> |
| 40 |
#include <QLocale> |
| 41 |
|
| 42 |
#include <KLocalizedString> |
| 43 |
#include <KFormat> |
| 44 |
|
| 45 |
#include <iostream> |
| 46 |
|
| 47 |
#define PROGRESS_MAX_VALUE 100 |
| 48 |
|
| 49 |
|
| 50 |
PlanTJScheduler::PlanTJScheduler(Project *project, ScheduleManager *sm, ulong granularity, QObject *parent) |
| 51 |
: SchedulerThread(project, sm, granularity, parent), |
| 52 |
result(-1), |
| 53 |
m_schedule(nullptr), |
| 54 |
m_recalculate(false), |
| 55 |
m_usePert(false), |
| 56 |
m_backward(false), |
| 57 |
m_tjProject(nullptr) |
| 58 |
{ |
| 59 |
TJ::TJMH.reset(); |
| 60 |
connect(&TJ::TJMH, &TJ::TjMessageHandler::message, this, &PlanTJScheduler::slotMessage); |
| 61 |
|
| 62 |
connect(this, &PlanTJScheduler::sigCalculationStarted, project, &KPlato::Project::sigCalculationStarted); |
| 63 |
|
| 64 |
connect(this, &PlanTJScheduler::sigCalculationFinished, project, &KPlato::Project::sigCalculationFinished); |
| 65 |
} |
| 66 |
|
| 67 |
PlanTJScheduler::PlanTJScheduler(ulong granularity, QObject *parent) |
| 68 |
: SchedulerThread(granularity, parent) |
| 69 |
, result(-1) |
| 70 |
, m_schedule(nullptr) |
| 71 |
, m_recalculate(false) |
| 72 |
, m_usePert(false) |
| 73 |
, m_backward(false) |
| 74 |
, m_tjProject(nullptr) |
| 75 |
{ |
| 76 |
TJ::TJMH.reset(); |
| 77 |
//connect(&TJ::TJMH, &TJ::TjMessageHandler::message, this, &PlanTJScheduler::slotMessage); |
| 78 |
|
| 79 |
// connect(this, &PlanTJScheduler::sigCalculationStarted, project, &KPlato::Project::sigCalculationStarted); |
| 80 |
// Q_EMIT sigCalculationStarted(project, sm); |
| 81 |
// |
| 82 |
// connect(this, &PlanTJScheduler::sigCalculationFinished, project, &KPlato::Project::sigCalculationFinished); |
| 83 |
} |
| 84 |
|
| 85 |
PlanTJScheduler::~PlanTJScheduler() |
| 86 |
{ |
| 87 |
delete m_tjProject; |
| 88 |
} |
| 89 |
|
| 90 |
void PlanTJScheduler::slotMessage(int type, const QString &msg, TJ::CoreAttributes *object) |
| 91 |
{ |
| 92 |
// debugPlan<<"PlanTJScheduler::slotMessage:"<<msg; |
| 93 |
Schedule::Log log; |
| 94 |
if (object && object->getType() == CA_Task && m_taskmap.contains(static_cast<TJ::Task*>(object))) { |
| 95 |
log = Schedule::Log(static_cast<Node*>(m_taskmap[ static_cast<TJ::Task*>(object) ]), type, msg); |
| 96 |
} else if (object && object->getType() == CA_Resource && m_resourcemap.contains(static_cast<TJ::Resource*>(object))) { |
| 97 |
log = Schedule::Log(nullptr, m_resourcemap[ static_cast<TJ::Resource*>(object) ], type, msg); |
| 98 |
} else if (object && ! object->getName().isEmpty()) { |
| 99 |
log = Schedule::Log(static_cast<Node*>(m_project), type, QString("%1: %2").arg(object->getName()).arg(msg)); |
| 100 |
} else { |
| 101 |
log = Schedule::Log(static_cast<Node*>(m_project), type, msg); |
| 102 |
} |
| 103 |
slotAddLog(log); |
| 104 |
} |
| 105 |
|
| 106 |
void PlanTJScheduler::run() |
| 107 |
{ |
| 108 |
if (m_haltScheduling) { |
| 109 |
deleteLater(); |
| 110 |
return; |
| 111 |
} |
| 112 |
if (m_stopScheduling) { |
| 113 |
return; |
| 114 |
} |
| 115 |
Q_EMIT sigCalculationStarted(m_mainproject, m_mainmanager); |
| 116 |
setMaxProgress(PROGRESS_MAX_VALUE); |
| 117 |
m_tjProject = new TJ::Project(); |
| 118 |
{ // mutex --> |
| 119 |
m_projectMutex.lock(); |
| 120 |
m_managerMutex.lock(); |
| 121 |
|
| 122 |
m_project = new Project(); |
| 123 |
loadProject(m_project, m_pdoc); |
| 124 |
m_project->setName("Schedule: " + m_project->name()); //Debug |
| 125 |
m_project->stopcalculation = false; |
| 126 |
m_manager = m_project->scheduleManager(m_mainmanagerId); |
| 127 |
Q_CHECK_PTR(m_manager); |
| 128 |
Q_ASSERT(m_manager->expected()); |
| 129 |
Q_ASSERT(m_manager != m_mainmanager); |
| 130 |
Q_ASSERT(m_manager->scheduleId() == m_mainmanager->scheduleId()); |
| 131 |
Q_ASSERT(m_manager->expected() != m_mainmanager->expected()); |
| 132 |
m_manager->setName("Schedule: " + m_manager->name()); //Debug |
| 133 |
m_schedule = m_manager->expected(); |
| 134 |
|
| 135 |
bool x = connect(m_manager, SIGNAL(sigLogAdded(KPlato::Schedule::Log)), this, SLOT(slotAddLog(KPlato::Schedule::Log))); |
| 136 |
Q_ASSERT(x); |
| 137 |
Q_UNUSED(x); |
| 138 |
|
| 139 |
m_project->initiateCalculation(*m_schedule); |
| 140 |
m_project->initiateCalculationLists(*m_schedule); |
| 141 |
|
| 142 |
m_usePert = m_manager->usePert(); |
| 143 |
m_recalculate = m_manager->recalculate(); |
| 144 |
if (m_recalculate) { |
| 145 |
m_backward = false; |
| 146 |
} else { |
| 147 |
m_backward = m_manager->schedulingDirection(); |
| 148 |
} |
| 149 |
m_project->setCurrentSchedule(m_manager->expected()->id()); |
| 150 |
|
| 151 |
m_schedule->setPhaseName(0, xi18nc("@info/plain" , "Init")); |
| 152 |
QLocale locale; |
| 153 |
KFormat format(locale); |
| 154 |
if (! m_backward) { |
| 155 |
logDebug(m_project, nullptr, QString("Schedule project using TJ Scheduler, starting at %1, granularity %2").arg(QDateTime::currentDateTime().toString()).arg(format.formatDuration(m_granularity)), 0); |
| 156 |
if (m_recalculate) { |
| 157 |
m_recalculateFrom = m_manager->recalculateFrom(); |
| 158 |
logInfo(m_project, nullptr, xi18nc("@info/plain" , "Re-calculate project from start time: %1", locale.toString(m_recalculateFrom, QLocale::ShortFormat)), 0); |
| 159 |
auto t = new TJ::Task(m_tjProject, "TJ::RECALCULATE_FROM", "TJ::RECALCULATE_FROM", nullptr, QString(), 0); |
| 160 |
t->setMilestone(true); |
| 161 |
t->setSpecifiedStart(0, toTJTime_t(m_recalculateFrom, tjGranularity())); |
| 162 |
logDebug(m_project, nullptr, QString("Re-calculate milestone created at: %1").arg(TJ::formatTime(t->getSpecifiedStart(0)))); //NOLINT(clang-analyzer-cplusplus.NewDeleteLeaks) |
| 163 |
} else { |
| 164 |
logInfo(m_project, nullptr, xi18nc("@info/plain" , "Schedule project from start time: %1", locale.toString(m_project->constraintStartTime(), QLocale::ShortFormat)), 0); |
| 165 |
} |
| 166 |
logInfo(m_project, nullptr, xi18nc("@info/plain" , "Project target finish time: %1", locale.toString(m_project->constraintEndTime(), QLocale::ShortFormat)), 0); |
| 167 |
} else { |
| 168 |
logDebug(m_project, nullptr, QString("Schedule project backward using TJ Scheduler, starting at %1, granularity %2").arg(locale.toString(QDateTime::currentDateTime(), QLocale::ShortFormat)).arg(format.formatDuration(m_granularity)), 0); |
| 169 |
logInfo(m_project, nullptr, xi18nc("@info/plain" , "Schedule project from end time: %1", locale.toString(m_project->constraintEndTime(), QLocale::ShortFormat)), 0); |
| 170 |
} |
| 171 |
|
| 172 |
m_managerMutex.unlock(); |
| 173 |
m_projectMutex.unlock(); |
| 174 |
} // <--- mutex |
| 175 |
setProgress(2); |
| 176 |
if (! kplatoToTJ()) { |
| 177 |
result = 1; |
| 178 |
m_manager->setCalculationResult(ScheduleManager::CalculationError); |
| 179 |
setProgress(PROGRESS_MAX_VALUE); |
| 180 |
return; |
| 181 |
} |
| 182 |
setMaxProgress(PROGRESS_MAX_VALUE); |
| 183 |
connect(m_tjProject, SIGNAL(updateProgressBar(int,int)), this, SLOT(setProgress(int))); |
| 184 |
|
| 185 |
m_schedule->setPhaseName(1, xi18nc("@info/plain" , "Schedule")); |
| 186 |
logInfo(m_project, nullptr, "Start scheduling", 1); |
| 187 |
bool r = solve(); |
| 188 |
if (! r) { |
| 189 |
debugPlan<<"Scheduling failed"; |
| 190 |
result = 2; |
| 191 |
m_manager->setCalculationResult(ScheduleManager::CalculationError); |
| 192 |
logError(m_project, nullptr, xi18nc("@info/plain" , "Failed to schedule project")); |
| 193 |
setProgress(PROGRESS_MAX_VALUE); |
| 194 |
return; |
| 195 |
} |
| 196 |
if (m_haltScheduling) { |
| 197 |
debugPlan<<"Scheduling halted"; |
| 198 |
m_manager->setCalculationResult(ScheduleManager::CalculationStopped); |
| 199 |
logInfo(m_project, nullptr, "Scheduling halted"); |
| 200 |
deleteLater(); |
| 201 |
return; |
| 202 |
} |
| 203 |
m_schedule->setPhaseName(2, xi18nc("@info/plain" , "Update")); |
| 204 |
logInfo(m_project, nullptr, "Scheduling finished, update project", 2); |
| 205 |
if (! kplatoFromTJ()) { |
| 206 |
m_manager->setCalculationResult(ScheduleManager::CalculationError); |
| 207 |
logError(m_project, nullptr, "Project update failed"); |
| 208 |
} |
| 209 |
m_manager->setCalculationResult(ScheduleManager::CalculationDone); |
| 210 |
setProgress(PROGRESS_MAX_VALUE); |
| 211 |
m_schedule->setPhaseName(3, xi18nc("@info/plain" , "Finish")); |
| 212 |
} |
| 213 |
|
| 214 |
bool PlanTJScheduler::check() |
| 215 |
{ |
| 216 |
DebugCtrl.setDebugMode(0); |
| 217 |
DebugCtrl.setDebugLevel(1000); |
| 218 |
return m_tjProject->pass2(true); |
| 219 |
} |
| 220 |
|
| 221 |
bool PlanTJScheduler::solve() |
| 222 |
{ |
| 223 |
debugPlan; |
| 224 |
TJ::Scenario *sc = m_tjProject->getScenario(0); |
| 225 |
if (! sc) { |
| 226 |
logError(m_project, nullptr, xi18nc("@info/plain" , "Failed to find scenario to schedule")); |
| 227 |
return false; |
| 228 |
} |
| 229 |
DebugCtrl.setDebugLevel(0); |
| 230 |
DebugCtrl.setDebugMode(PSDEBUG+TSDEBUG+RSDEBUG+PADEBUG); |
| 231 |
|
| 232 |
return m_tjProject->scheduleScenario(sc); |
| 233 |
} |
| 234 |
|
| 235 |
bool PlanTJScheduler::kplatoToTJ() |
| 236 |
{ |
| 237 |
m_tjProject->setPriority(m_project->priority()); |
| 238 |
m_tjProject->setScheduleGranularity(m_granularity / 1000); |
| 239 |
m_tjProject->getScenario(0)->setMinSlackRate(0.0); // Do not calculate critical path |
| 240 |
|
| 241 |
m_tjProject->setNow(m_project->constraintStartTime().toSecsSinceEpoch()); |
| 242 |
m_tjProject->setStart(m_project->constraintStartTime().toSecsSinceEpoch()); |
| 243 |
m_tjProject->setEnd(m_project->constraintEndTime().toSecsSinceEpoch()); |
| 244 |
|
| 245 |
m_tjProject->setDailyWorkingHours(m_project->standardWorktime()->day()); |
| 246 |
|
| 247 |
// Set working days for the project, it is used for tasks with a length specification |
| 248 |
// FIXME: Plan has task specific calendars for this estimate type |
| 249 |
KPlato::Calendar *cal = m_project->defaultCalendar(); |
| 250 |
if (! cal) { |
| 251 |
cal = m_project->calendars().value(0); |
| 252 |
} |
| 253 |
if (cal) { |
| 254 |
int days[ 7 ] = { Qt::Sunday, Qt::Monday, Qt::Tuesday, Qt::Wednesday, Qt::Thursday, Qt::Friday, Qt::Saturday }; |
| 255 |
for (int i = 0; i < 7; ++i) { |
| 256 |
CalendarDay *d = nullptr; |
| 257 |
for (Calendar *c = cal; c; c = c->parentCal()) { |
| 258 |
QElapsedTimer t; t.start(); |
| 259 |
d = c->weekday(days[ i ]); |
| 260 |
Q_ASSERT(d); |
| 261 |
if (d == nullptr || d->state() != CalendarDay::Undefined) { |
| 262 |
break; |
| 263 |
} |
| 264 |
} |
| 265 |
if (d && d->state() == CalendarDay::Working) { |
| 266 |
QList<TJ::Interval*> lst; |
| 267 |
const auto intervals = d->timeIntervals(); |
| 268 |
for (const TimeInterval *ti : intervals) { |
| 269 |
TJ::Interval *tji = new TJ::Interval(toTJInterval(ti->startTime(), ti->endTime(),tjGranularity())); |
| 270 |
lst << tji; |
| 271 |
} |
| 272 |
m_tjProject->setWorkingHours(i, lst); |
| 273 |
qDeleteAll(lst); |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
addTasks(); |
| 278 |
setConstraints(); |
| 279 |
addDependencies(); |
| 280 |
addRequests(); |
| 281 |
addStartEndJob(); |
| 282 |
|
| 283 |
if (result != -1) { |
| 284 |
return false; |
| 285 |
} |
| 286 |
return check(); |
| 287 |
} |
| 288 |
|
| 289 |
void PlanTJScheduler::addStartEndJob() |
| 290 |
{ |
| 291 |
const auto startId("TJ::StartJob"); |
| 292 |
const auto endId("TJ::EndJob"); |
| 293 |
|
| 294 |
TJ::Task *start = m_tjProject->getTask(startId); |
| 295 |
if (!start) { |
| 296 |
start = new TJ::Task(m_tjProject, startId, startId, nullptr, QString(), 0); |
| 297 |
} |
| 298 |
start->setMilestone(true); |
| 299 |
if (! m_backward) { |
| 300 |
start->setSpecifiedStart(0, m_tjProject->getStart()); |
| 301 |
start->setPriority(999); |
| 302 |
} else { |
| 303 |
// backwards: insert a new ms before start and make start an ALAP to push all other jobs ALAP |
| 304 |
TJ::Task *bs = m_tjProject->getTask(startId); |
| 305 |
if (!bs) { |
| 306 |
bs = new TJ::Task(m_tjProject, "TJ::StartJob-B", "TJ::StartJob-B", nullptr, QString(), 0); |
| 307 |
bs->setMilestone(true); |
| 308 |
bs->addPrecedes(start->getId()); |
| 309 |
start->addDepends(bs->getId()); |
| 310 |
start->setScheduling(TJ::Task::ALAP); |
| 311 |
} |
| 312 |
bs->setSpecifiedStart(0, m_tjProject->getStart()); |
| 313 |
bs->setPriority(999); |
| 314 |
} |
| 315 |
TJ::Task *end = m_tjProject->getTask(endId); |
| 316 |
if (!end) { |
| 317 |
end = new TJ::Task(m_tjProject, endId, endId, nullptr, QString(), 0); |
| 318 |
} |
| 319 |
end->setMilestone(true); |
| 320 |
if (m_backward) { |
| 321 |
end->setSpecifiedEnd(0, m_tjProject->getEnd() - 1); |
| 322 |
end->setScheduling(TJ::Task::ALAP); |
| 323 |
} |
| 324 |
for (QMap<TJ::Task*, Node*>::ConstIterator it = m_taskmap.constBegin(); it != m_taskmap.constEnd(); ++it) { |
| 325 |
if (it.value()->isStartNode()) { |
| 326 |
it.key()->addDepends(start->getId()); |
| 327 |
//logDebug(m_project, 0, QString("'%1' depends on: '%2'").arg(it.key()->getName()).arg(start->getName())); |
| 328 |
if (start->getScheduling() == TJ::Task::ALAP) { |
| 329 |
start->addPrecedes(it.key()->getId()); |
| 330 |
//logDebug(m_project, 0, QString("'%1' precedes: '%2'").arg(start->getName()).arg(it.key()->getName())); |
| 331 |
} |
| 332 |
} |
| 333 |
if (it.value()->isEndNode()) { |
| 334 |
end->addDepends(it.key()->getId()); |
| 335 |
if (it.key()->getScheduling() == TJ::Task::ALAP) { |
| 336 |
it.key()->addPrecedes(end->getId()); |
| 337 |
} |
| 338 |
} |
| 339 |
} |
| 340 |
} //NOLINT(clang-analyzer-cplusplus.NewDeleteLeaks) |
| 341 |
|
| 342 |
// static |
| 343 |
int PlanTJScheduler::toTJDayOfWeek(int day) |
| 344 |
{ |
| 345 |
return day == 7 ? 0 : day; |
| 346 |
} |
| 347 |
|
| 348 |
// static |
| 349 |
DateTime PlanTJScheduler::fromTime_t(time_t t, const QTimeZone &tz) { |
| 350 |
return DateTime (QDateTime::fromSecsSinceEpoch(t).toTimeZone(tz)); |
| 351 |
} |
| 352 |
|
| 353 |
time_t PlanTJScheduler::toTJTime_t(const QDateTime &dt, ulong granularity) |
| 354 |
{ |
| 355 |
int secs = QTime(0, 0, 0).secsTo(dt.time()); |
| 356 |
secs -= secs % granularity; |
| 357 |
return QDateTime(dt.date(), QTime(0, 0, 0).addSecs(secs), dt.timeZone()).toSecsSinceEpoch(); |
| 358 |
} |
| 359 |
|
| 360 |
// static |
| 361 |
AppointmentInterval PlanTJScheduler::fromTJInterval(const TJ::Interval &tji, const QTimeZone &tz) { |
| 362 |
AppointmentInterval a(fromTime_t(tji.getStart(), tz), fromTime_t(tji.getEnd(), tz).addSecs(1)); |
| 363 |
return a; |
| 364 |
} |
| 365 |
|
| 366 |
// static |
| 367 |
TJ::Interval PlanTJScheduler::toTJInterval(const QDateTime &start, const QDateTime &end, ulong granularity) { |
| 368 |
int secs = QTime(0, 0, 0).secsTo(start.time()); |
| 369 |
secs -= secs % granularity; |
| 370 |
QDateTime s(start.date(), QTime(0, 0, 0).addSecs(secs), start.timeZone()); |
| 371 |
secs = QTime(0, 0, 0).secsTo(end.time()); |
| 372 |
secs -= secs % granularity; |
| 373 |
QDateTime e(end.date(), QTime(0, 0, 0).addSecs(secs), end.timeZone()); |
| 374 |
TJ::Interval ti(s.toSecsSinceEpoch(), e.addSecs(-1).toSecsSinceEpoch()); |
| 375 |
return ti; |
| 376 |
} |
| 377 |
|
| 378 |
// static |
| 379 |
TJ::Interval PlanTJScheduler::toTJInterval(const QTime &start, const QTime &end, ulong granularity) { |
| 380 |
int secs = QTime(0, 0, 0).secsTo(start); |
| 381 |
time_t s = secs - (secs % granularity); |
| 382 |
secs = (end == QTime(0, 0, 0)) ? 86399 : QTime(0, 0, 0).secsTo(end); |
| 383 |
time_t e = secs - (secs % granularity) - 1; |
| 384 |
TJ::Interval ti(s, e); |
| 385 |
return ti; |
| 386 |
} |
| 387 |
|
| 388 |
ulong PlanTJScheduler::tjGranularity() const |
| 389 |
{ |
| 390 |
return m_tjProject->getScheduleGranularity(); |
| 391 |
} |
| 392 |
|
| 393 |
bool PlanTJScheduler::kplatoFromTJ() |
| 394 |
{ |
| 395 |
MainSchedule *cs = static_cast<MainSchedule*>(m_project->currentSchedule()); |
| 396 |
|
| 397 |
QDateTime start; |
| 398 |
QDateTime end; |
| 399 |
for (QMap<TJ::Task*, Node*>::ConstIterator it = m_taskmap.constBegin(); it != m_taskmap.constEnd(); ++it) { |
| 400 |
if (! taskFromTJ(it.key(), it.value())) { |
| 401 |
return false; |
| 402 |
} |
| 403 |
if (! start.isValid() || it.value()->startTime() < start) { |
| 404 |
start = it.value()->startTime(); |
| 405 |
} |
| 406 |
if (! end.isValid() || it.value()->endTime() > end) { |
| 407 |
end = it.value()->endTime(); |
| 408 |
} |
| 409 |
} |
| 410 |
m_project->setStartTime(start.isValid() ? start : m_project->constraintStartTime()); |
| 411 |
m_project->setEndTime(end.isValid() ? end : m_project->constraintEndTime()); |
| 412 |
|
| 413 |
adjustSummaryTasks(m_schedule->summaryTasks()); |
| 414 |
|
| 415 |
for (Node *task : std::as_const(m_taskmap)) { |
| 416 |
calcPertValues(task); |
| 417 |
} |
| 418 |
|
| 419 |
m_project->calcCriticalPathList(m_schedule); |
| 420 |
// calculate positive float |
| 421 |
for (Node* t : std::as_const(m_taskmap)) { |
| 422 |
if (! t->inCriticalPath() && t->isStartNode()) { |
| 423 |
calcPositiveFloat(t); |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
QLocale locale; |
| 428 |
logInfo(m_project, nullptr, xi18nc("@info/plain" , "Project scheduled to start at %1 and finish at %2", locale.toString(m_project->startTime(), QLocale::ShortFormat), locale.toString(m_project->endTime(), QLocale::ShortFormat))); |
| 429 |
|
| 430 |
if (m_manager) { |
| 431 |
logDebug(m_project, nullptr, QString("Project scheduling finished at %1").arg(locale.toString(QDateTime::currentDateTime(), QLocale::ShortFormat))); |
| 432 |
m_project->finishCalculation(*m_manager); |
| 433 |
m_manager->scheduleChanged(cs); |
| 434 |
} |
| 435 |
return true; |
| 436 |
} |
| 437 |
|
| 438 |
bool PlanTJScheduler::taskFromTJ(TJ::Task *job, Node *task) |
| 439 |
{ |
| 440 |
return taskFromTJ(m_project, job, task); |
| 441 |
} |
| 442 |
|
| 443 |
bool PlanTJScheduler::taskFromTJ(Project *project, TJ::Task *job, Node *task) |
| 444 |
{ |
| 445 |
if (m_haltScheduling) { |
| 446 |
return true; |
| 447 |
} |
| 448 |
if (task->type() == KPlato::Node::Type_Summarytask || task->type() == KPlato::Node::Type_Project) { |
| 449 |
return true; |
| 450 |
} |
| 451 |
Q_ASSERT(project == task->projectNode()); |
| 452 |
Schedule *cs = task->currentSchedule(); |
| 453 |
Q_ASSERT(cs); |
| 454 |
QTimeZone tz = m_project->timeZone(); |
| 455 |
time_t s = job->getStart(0); |
| 456 |
if (s < m_tjProject->getStart() || s > m_tjProject->getEnd()) { |
| 457 |
project->currentSchedule()->setSchedulingError(true); |
| 458 |
cs->setSchedulingError(true); |
| 459 |
s = m_tjProject->getStart(); |
| 460 |
} |
| 461 |
time_t e = job->getEnd(0); |
| 462 |
if (job->isMilestone()) { |
| 463 |
Q_ASSERT(s = (e + 1)); |
| 464 |
e = s - 1; |
| 465 |
} else if (e <= s || e > m_tjProject->getEnd()) { |
| 466 |
project->currentSchedule()->setSchedulingError(true); |
| 467 |
cs->setSchedulingError(true); |
| 468 |
e = s + (8*60*60); |
| 469 |
} |
| 470 |
task->setStartTime(fromTime_t(s, tz)); |
| 471 |
task->setEndTime(fromTime_t(e + 1, tz)); |
| 472 |
task->setDuration(task->endTime() - task->startTime()); |
| 473 |
|
| 474 |
//logDebug(task, nullptr, QString("%1, %2 - %3, %4").arg(TJ::time2ISO(s)).arg(task->startTime().toString(Qt::ISODate)).arg(TJ::time2ISO(e+1)).arg(task->endTime().toString(Qt::ISODate))); |
| 475 |
if (! task->startTime().isValid()) { |
| 476 |
logError(task, nullptr, xi18nc("@info/plain", "Invalid start time")); |
| 477 |
return false; |
| 478 |
} |
| 479 |
if (! task->endTime().isValid()) { |
| 480 |
logError(task, nullptr, xi18nc("@info/plain", "Invalid end time")); |
| 481 |
return false; |
| 482 |
} |
| 483 |
if (!project->startTime().isValid() || project->startTime() > task->startTime()) { |
| 484 |
project->setStartTime(task->startTime()); |
| 485 |
} |
| 486 |
if (task->endTime() > project->endTime()) { |
| 487 |
project->setEndTime(task->endTime()); |
| 488 |
} |
| 489 |
const auto lst = job->getBookedResources(0); |
| 490 |
for (TJ::CoreAttributes *a : lst) { |
| 491 |
TJ::Resource *r = static_cast<TJ::Resource*>(a); |
| 492 |
Resource *res = resource(project, r); |
| 493 |
Q_ASSERT(res); |
| 494 |
const QVector<TJ::Interval> lst = r->getBookedIntervals(0, job); |
| 495 |
for (const TJ::Interval &tji : lst) { |
| 496 |
AppointmentInterval ai = fromTJInterval(tji, tz); |
| 497 |
double load = res->type() == Resource::Type_Material ? res->units() : ai.load() * r->getEfficiency(); |
| 498 |
res->addAppointment(cs, ai.startTime(), ai.endTime(), load); |
| 499 |
logDebug(task, nullptr, '\'' + res->name() + "' added appointment: " + ai.startTime().toString(Qt::ISODate) + " - " + ai.endTime().toString(Qt::ISODate)); |
| 500 |
} |
| 501 |
} |
| 502 |
if (m_recalculate && static_cast<Task*>(task)->isStarted() && task->estimate()->type() == Estimate::Type_Effort) { |
| 503 |
addPastAppointments(task); |
| 504 |
} |
| 505 |
|
| 506 |
cs->setScheduled(true); |
| 507 |
QLocale locale; |
| 508 |
if (task->type() == Node::Type_Milestone) { |
| 509 |
logInfo(task, nullptr, xi18nc("@info/plain" , "Scheduled milestone: %1", locale.toString(task->startTime(), QLocale::ShortFormat))); |
| 510 |
} else { |
| 511 |
logInfo(task, nullptr, xi18nc("@info/plain" , "Scheduled task: %1 - %2", locale.toString(task->startTime(), QLocale::ShortFormat), locale.toString(task->endTime(), QLocale::ShortFormat))); |
| 512 |
} |
| 513 |
return true; |
| 514 |
} |
| 515 |
|
| 516 |
void PlanTJScheduler::addPastAppointments(Node *task) |
| 517 |
{ |
| 518 |
if (!static_cast<Task*>(task)->isStarted()) { |
| 519 |
logDebug(task, nullptr, QStringLiteral("Task is not started, no appointments to copy")); |
| 520 |
return; |
| 521 |
} |
| 522 |
if (static_cast<Task*>(task)->completion().isFinished()) { |
| 523 |
static_cast<Task*>(task)->copySchedule(); |
| 524 |
} else { |
| 525 |
static_cast<Task*>(task)->createAndMergeAppointmentsFromCompletion(); |
| 526 |
} |
| 527 |
return; |
| 528 |
} |
| 529 |
|
| 530 |
Resource *PlanTJScheduler::resource(Project *project, TJ::Resource *tjResource) |
| 531 |
{ |
| 532 |
return project->resource(tjResource->getId()); |
| 533 |
} |
| 534 |
|
| 535 |
void PlanTJScheduler::adjustSummaryTasks(const QList<Node*> &nodes) |
| 536 |
{ |
| 537 |
for (Node *n : nodes) { |
| 538 |
adjustSummaryTasks(n->childNodeIterator()); |
| 539 |
if (n->parentNode()->type() == Node::Type_Summarytask) { |
| 540 |
DateTime pt = n->parentNode()->startTime(); |
| 541 |
DateTime nt = n->startTime(); |
| 542 |
if (! pt.isValid() || pt > nt) { |
| 543 |
n->parentNode()->setStartTime(nt); |
| 544 |
} |
| 545 |
pt = n->parentNode()->endTime(); |
| 546 |
nt = n->endTime(); |
| 547 |
if (! pt.isValid() || pt < nt) { |
| 548 |
n->parentNode()->setEndTime(nt); |
| 549 |
} |
| 550 |
} |
| 551 |
} |
| 552 |
} |
| 553 |
|
| 554 |
Duration PlanTJScheduler::calcPositiveFloat(Node *task) |
| 555 |
{ |
| 556 |
if (static_cast<Task*>(task)->positiveFloat() != 0) { |
| 557 |
return static_cast<Task*>(task)->positiveFloat(); |
| 558 |
} |
| 559 |
Duration x; |
| 560 |
if (task->dependChildNodes().isEmpty() && static_cast<Task*>(task)->childProxyRelations().isEmpty()) { |
| 561 |
x = m_project->endTime() - task->endTime(); |
| 562 |
} else { |
| 563 |
const auto lst = task->dependChildNodes() + static_cast<Task*>(task)->childProxyRelations(); |
| 564 |
for (const Relation *r : lst) { |
| 565 |
if (! r->child()->inCriticalPath()) { |
| 566 |
Duration f = calcPositiveFloat(static_cast<Task*>(r->child())); |
| 567 |
if (x == 0 || f < x) { |
| 568 |
x = f; |
| 569 |
} |
| 570 |
} |
| 571 |
} |
| 572 |
} |
| 573 |
Duration totfloat = static_cast<Task*>(task)->freeFloat() + x; |
| 574 |
static_cast<Task*>(task)->setPositiveFloat(totfloat); |
| 575 |
return totfloat; |
| 576 |
} |
| 577 |
|
| 578 |
void PlanTJScheduler::calcPertValues(Node *t) |
| 579 |
{ |
| 580 |
switch (t->constraint()) { |
| 581 |
case Node::MustStartOn: |
| 582 |
if (t->constraintStartTime() != t->startTime()) { |
| 583 |
static_cast<Task*>(t)->setNegativeFloat(t->startTime() - t->constraintStartTime()); |
| 584 |
} |
| 585 |
break; |
| 586 |
case Node::StartNotEarlier: |
| 587 |
if (t->startTime() < t->constraintStartTime()) { |
| 588 |
static_cast<Task*>(t)->setNegativeFloat(t->constraintStartTime() - t->startTime()); |
| 589 |
} |
| 590 |
break; |
| 591 |
case Node::MustFinishOn: |
| 592 |
if (t->constraintEndTime() != t->endTime()) { |
| 593 |
static_cast<Task*>(t)->setNegativeFloat(t->endTime() - t->constraintEndTime()); |
| 594 |
} |
| 595 |
break; |
| 596 |
case Node::FinishNotLater: |
| 597 |
if (t->endTime() > t->constraintEndTime()) { |
| 598 |
static_cast<Task*>(t)->setNegativeFloat(t->endTime() - t->constraintEndTime()); |
| 599 |
} |
| 600 |
break; |
| 601 |
case Node::FixedInterval: |
| 602 |
if (t->constraintStartTime() != t->startTime()) { |
| 603 |
static_cast<Task*>(t)->setNegativeFloat(t->startTime() - t->constraintStartTime()); |
| 604 |
} else if (t->endTime() != t->constraintEndTime()) { |
| 605 |
static_cast<Task*>(t)->setNegativeFloat(t->endTime() - t->constraintEndTime()); |
| 606 |
} |
| 607 |
break; |
| 608 |
default: |
| 609 |
break; |
| 610 |
} |
| 611 |
if (static_cast<Task*>(t)->negativeFloat() != 0) { |
| 612 |
t->currentSchedule()->setConstraintError(true); |
| 613 |
m_project->currentSchedule()->setSchedulingError(true); |
| 614 |
logError(t, nullptr, i18nc("1=type of constraint", "%1: Failed to meet constraint. Negative float=%2", t->constraintToString(true), static_cast<Task*>(t)->negativeFloat().toString(Duration::Format_i18nHour))); |
| 615 |
} |
| 616 |
//debugPlan<<t->name()<<t->startTime()<<t->endTime(); |
| 617 |
Duration negativefloat; |
| 618 |
const auto lst = t->dependParentNodes() + static_cast<Task*>(t)->parentProxyRelations(); |
| 619 |
for (const Relation *r : lst) { |
| 620 |
if (r->parent()->endTime() + r->lag() > t->startTime()) { |
| 621 |
Duration f = r->parent()->endTime() + r->lag() - t->startTime(); |
| 622 |
if (f > negativefloat) { |
| 623 |
negativefloat = f; |
| 624 |
} |
| 625 |
} |
| 626 |
} |
| 627 |
if (negativefloat > 0) { |
| 628 |
t->currentSchedule()->setSchedulingError(true); |
| 629 |
m_project->currentSchedule()->setSchedulingError(true); |
| 630 |
logWarning(t, nullptr, xi18nc("@info/plain", "Failed to meet dependency. Negative float=%1", negativefloat.toString(Duration::Format_i18nHour))); |
| 631 |
if (static_cast<Task*>(t)->negativeFloat() < negativefloat) { |
| 632 |
static_cast<Task*>(t)->setNegativeFloat(negativefloat); |
| 633 |
} |
| 634 |
} |
| 635 |
Duration freefloat; |
| 636 |
const auto relations = t->dependChildNodes() + static_cast<Task*>(t)->childProxyRelations(); |
| 637 |
for (const Relation *r : relations) { |
| 638 |
if (t->endTime() + r->lag() < r->child()->startTime()) { |
| 639 |
Duration f = r->child()->startTime() - r->lag() - t->endTime(); |
| 640 |
if (f > 0 && (freefloat == 0 || freefloat > f)) { |
| 641 |
freefloat = f; |
| 642 |
} |
| 643 |
} |
| 644 |
} |
| 645 |
static_cast<Task*>(t)->setFreeFloat(freefloat); |
| 646 |
} |
| 647 |
|
| 648 |
bool PlanTJScheduler::exists(QList<CalendarDay*> &lst, CalendarDay *day) |
| 649 |
{ |
| 650 |
for (CalendarDay *d : std::as_const(lst)) { |
| 651 |
if (d->date() == day->date() && day->state() != CalendarDay::Undefined && d->state() != CalendarDay::Undefined) { |
| 652 |
return true; |
| 653 |
} |
| 654 |
} |
| 655 |
return false; |
| 656 |
} |
| 657 |
|
| 658 |
TJ::Resource *PlanTJScheduler::addResource(KPlato::Resource *r) |
| 659 |
{ |
| 660 |
KPlato::Resource *resource = m_resourceIds.value(r->id()); |
| 661 |
if (!resource) { |
| 662 |
m_resourceIds.insert(r->id(), r); |
| 663 |
resource = r; |
| 664 |
} |
| 665 |
TJ::Resource *res = m_resourcemap.key(resource); |
| 666 |
if (res) { |
| 667 |
debugPlan<<r->name()<<"already exist"; |
| 668 |
return res; |
| 669 |
} |
| 670 |
res = new TJ::Resource(m_tjProject, resource->id(), resource->name(), nullptr); |
| 671 |
if (resource->type() == Resource::Type_Material) { |
| 672 |
res->setEfficiency(0.0); |
| 673 |
} else { |
| 674 |
res->setEfficiency((double)(resource->units()) / 100.); |
| 675 |
} |
| 676 |
Calendar *cal = resource->calendar(); |
| 677 |
Q_ASSERT(cal); |
| 678 |
DateTime start = qMax(resource->availableFrom(), m_project->constraintStartTime()); |
| 679 |
DateTime end = m_project->constraintEndTime(); |
| 680 |
if (resource->availableUntil().isValid() && end > resource->availableUntil()) { |
| 681 |
end = resource->availableUntil(); |
| 682 |
} |
| 683 |
AppointmentIntervalList lst = cal->workIntervals(start, end, 1.0); |
| 684 |
// qDebug()<<r<<lst; |
| 685 |
const QMultiMap<QDate, AppointmentInterval> &map = lst.map(); |
| 686 |
QMultiMap<QDate, AppointmentInterval>::const_iterator mapend = map.constEnd(); |
| 687 |
QMultiMap<QDate, AppointmentInterval>::const_iterator it = map.constBegin(); |
| 688 |
TJ::Shift *shift = new TJ::Shift(m_tjProject, resource->id(), resource->name(), nullptr, QString(), 0); |
| 689 |
for (; it != mapend; ++it) { |
| 690 |
shift->addWorkingInterval(toTJInterval(it.value().startTime(), it.value().endTime(), m_granularity/1000)); |
| 691 |
} |
| 692 |
res->addShift(toTJInterval(start, end, m_granularity/1000), shift); |
| 693 |
m_resourcemap[res] = resource; |
| 694 |
logDebug(m_project, nullptr, "Added resource: " + resource->name()); |
| 695 |
/* QListIterator<TJ::Interval*> vit = res->getVacationListIterator(); |
| 696 |
while (vit.hasNext()) { |
| 697 |
TJ::Interval *i = vit.next(); |
| 698 |
logDebug(m_project, 0, "Vacation: " + TJ::time2ISO(i->getStart()) + " - " + TJ::time2ISO(i->getEnd())); |
| 699 |
}*/ |
| 700 |
return res; |
| 701 |
} |
| 702 |
|
| 703 |
TJ::Task *PlanTJScheduler::addTask(const KPlato::Node *node, TJ::Task *parent) |
| 704 |
{ |
| 705 |
/* if (m_backward && task->isStartNode()) { |
| 706 |
Relation *r = new Relation(m_backwardTask, task); |
| 707 |
m_project->addRelation(r); |
| 708 |
}*/ |
| 709 |
TJ::Task *t = new TJ::Task(m_tjProject, node->id(), node->name(), parent, QString(), 0); |
| 710 |
t->setPriority(node->priority()); |
| 711 |
if (node->type() == KPlato::Node::Type_Task) { |
| 712 |
m_taskmap[t] = const_cast<KPlato::Task*>(static_cast<const KPlato::Task*>(node)); |
| 713 |
addWorkingTime(static_cast<const KPlato::Task*>(node), t); |
| 714 |
} else if (node->type() == KPlato::Node::Type_Milestone) { |
| 715 |
m_taskmap[t] = const_cast<KPlato::Task*>(static_cast<const KPlato::Task*>(node)); |
| 716 |
} else if (node->type() == KPlato::Node::Type_Project) { |
| 717 |
if (node->constraint() == Node::MustStartOn) { |
| 718 |
t->setSpecifiedStart(0, node->constraintStartTime().toSecsSinceEpoch()); |
| 719 |
} else { |
| 720 |
t->setSpecifiedEnd(0, node->constraintEndTime().toSecsSinceEpoch()); |
| 721 |
} |
| 722 |
} |
| 723 |
return t; |
| 724 |
} |
| 725 |
|
| 726 |
void PlanTJScheduler::addWorkingTime(const KPlato::Task *task, TJ::Task *job) |
| 727 |
{ |
| 728 |
if (task->type() != Node::Type_Task || task->estimate()->type() != Estimate::Type_Duration || ! task->estimate()->calendar()) { |
| 729 |
return; |
| 730 |
} |
| 731 |
int id = 0; |
| 732 |
Calendar *cal = task->estimate()->calendar(); |
| 733 |
DateTime start = m_project->constraintStartTime(); |
| 734 |
DateTime end = m_project->constraintEndTime(); |
| 735 |
|
| 736 |
AppointmentIntervalList lst = cal->workIntervals(start, end, 1.0); |
| 737 |
const QMultiMap<QDate, AppointmentInterval> &map = lst.map(); |
| 738 |
QMultiMap<QDate, AppointmentInterval>::const_iterator mapend = map.constEnd(); |
| 739 |
QMultiMap<QDate, AppointmentInterval>::const_iterator it = map.constBegin(); |
| 740 |
TJ::Shift *shift = new TJ::Shift(m_tjProject, task->id() + QString("-%1").arg(++id), task->name(), nullptr, QString(), 0); |
| 741 |
for (; it != mapend; ++it) { |
| 742 |
shift->addWorkingInterval(toTJInterval(it.value().startTime(), it.value().endTime(), m_granularity/1000)); |
| 743 |
} |
| 744 |
job->addShift(toTJInterval(start, end, m_granularity/1000), shift); |
| 745 |
} |
| 746 |
|
| 747 |
void PlanTJScheduler::addTasks() |
| 748 |
{ |
| 749 |
//debugPlan; |
| 750 |
QList<Node*> list = m_project->allNodes(); |
| 751 |
for (int i = 0; i < list.count(); ++i) { |
| 752 |
Node *n = list.at(i); |
| 753 |
TJ::Task *parent = nullptr; |
| 754 |
switch (n->type()) { |
| 755 |
case Node::Type_Summarytask: |
| 756 |
m_schedule->insertSummaryTask(n); |
| 757 |
break; |
| 758 |
case Node::Type_Task: |
| 759 |
case Node::Type_Milestone: |
| 760 |
switch (n->constraint()) { |
| 761 |
case Node::StartNotEarlier: |
| 762 |
parent = addStartNotEarlier(n); |
| 763 |
break; |
| 764 |
case Node::FinishNotLater: |
| 765 |
parent = addFinishNotLater(n); |
| 766 |
break; |
| 767 |
} |
| 768 |
addTask(static_cast<Task*>(n), parent); |
| 769 |
break; |
| 770 |
default: |
| 771 |
break; |
| 772 |
} |
| 773 |
} |
| 774 |
} |
| 775 |
|
| 776 |
void PlanTJScheduler::addDepends(const Relation *rel) |
| 777 |
{ |
| 778 |
TJ::Task *child = m_tjProject->getTask(rel->child()->id()); |
| 779 |
if (!child) { |
| 780 |
logWarning(rel->parent(), nullptr, xi18nc("@info/plain" , "Failed to add as predecessor to task '%1'", rel->child()->name())); |
| 781 |
return; |
| 782 |
} |
| 783 |
TJ::TaskDependency *d = child->addDepends(rel->parent()->id()); |
| 784 |
d->setGapDuration(0, rel->lag().seconds()); |
| 785 |
} |
| 786 |
|
| 787 |
void PlanTJScheduler::addPrecedes(const Relation *rel) |
| 788 |
{ |
| 789 |
TJ::Task *parent = m_tjProject->getTask(rel->parent()->id()); |
| 790 |
if (!parent) { |
| 791 |
logWarning(rel->child(), nullptr, xi18nc("@info/plain" , "Failed to add as successor to task '%1'", rel->parent()->name())); |
| 792 |
return; |
| 793 |
} |
| 794 |
TJ::TaskDependency *d = parent->addPrecedes(rel->child()->id()); |
| 795 |
d->setGapDuration(0, rel->lag().seconds()); |
| 796 |
} |
| 797 |
|
| 798 |
void PlanTJScheduler::addDependencies(KPlato::Node *task) |
| 799 |
{ |
| 800 |
const auto lst = task->dependParentNodes() + static_cast<Task*>(task)->parentProxyRelations(); |
| 801 |
for (Relation *r : lst) { |
| 802 |
Node *n = r->parent(); |
| 803 |
if (n == nullptr || n->type() == Node::Type_Summarytask) { |
| 804 |
continue; |
| 805 |
} |
| 806 |
switch (r->type()) { |
| 807 |
case Relation::FinishStart: |
| 808 |
break; |
| 809 |
case Relation::FinishFinish: |
| 810 |
case Relation::StartStart: |
| 811 |
warnPlan<<"Dependency type not handled. Using FinishStart."; |
| 812 |
logWarning(task, nullptr, xi18nc("@info/plain" , "Dependency type '%1' not handled. Using FinishStart.", r->typeToString(true))); |
| 813 |
break; |
| 814 |
} |
| 815 |
switch (task->constraint()) { |
| 816 |
case Node::ASAP: |
| 817 |
case Node::ALAP: |
| 818 |
addPrecedes(r); |
| 819 |
addDepends(r); |
| 820 |
break; |
| 821 |
case Node::MustStartOn: |
| 822 |
case Node::StartNotEarlier: |
| 823 |
addPrecedes(r); |
| 824 |
if (task->constraintStartTime() < m_project->constraintStartTime()) { |
| 825 |
addDepends(r); |
| 826 |
} |
| 827 |
break; |
| 828 |
case Node::MustFinishOn: |
| 829 |
case Node::FinishNotLater: |
| 830 |
addDepends(r); |
| 831 |
if (task->constraintEndTime() < m_project->constraintEndTime()) { |
| 832 |
addPrecedes(r); |
| 833 |
} |
| 834 |
break; |
| 835 |
case Node::FixedInterval: |
| 836 |
break; |
| 837 |
} |
| 838 |
} |
| 839 |
} |
| 840 |
|
| 841 |
void PlanTJScheduler::addDependencies() |
| 842 |
{ |
| 843 |
for (Node *t : std::as_const(m_taskmap)) { |
| 844 |
addDependencies(t); |
| 845 |
} |
| 846 |
} |
| 847 |
|
| 848 |
void PlanTJScheduler::setConstraints() |
| 849 |
{ |
| 850 |
QMap<TJ::Task*, Node*> ::const_iterator it = m_taskmap.constBegin(); |
| 851 |
for (; it != m_taskmap.constEnd(); ++it) { |
| 852 |
setConstraint(it.key(), it.value()); |
| 853 |
} |
| 854 |
} |
| 855 |
|
| 856 |
void PlanTJScheduler::setConstraint(TJ::Task *job, KPlato::Node *task) |
| 857 |
{ |
| 858 |
switch (task->constraint()) { |
| 859 |
case Node::ASAP: |
| 860 |
if (! job->isMilestone()) { |
| 861 |
job->setScheduling(m_backward ? TJ::Task::ALAP : TJ::Task::ASAP); |
| 862 |
} |
| 863 |
break; |
| 864 |
case Node::ALAP: |
| 865 |
job->setScheduling(TJ::Task::ALAP); |
| 866 |
break; |
| 867 |
case Node::MustStartOn: |
| 868 |
if (task->constraintStartTime() >= m_project->constraintStartTime()) { |
| 869 |
job->setPriority(600); |
| 870 |
job->setSpecifiedStart(0, task->constraintStartTime().toSecsSinceEpoch()); |
| 871 |
logDebug(task, nullptr, QString("MSO: set specified start: %1").arg(TJ::time2ISO(task->constraintStartTime().toSecsSinceEpoch()))); |
| 872 |
} else { |
| 873 |
logWarning(task, nullptr, xi18nc("@info/plain", "%1: Invalid start constraint", task->constraintToString(true))); |
| 874 |
} |
| 875 |
break; |
| 876 |
case Node::StartNotEarlier: { |
| 877 |
break; |
| 878 |
} |
| 879 |
case Node::MustFinishOn: |
| 880 |
if (task->constraintEndTime() <= m_project->constraintEndTime()) { |
| 881 |
job->setPriority(600); |
| 882 |
job->setScheduling(TJ::Task::ALAP); |
| 883 |
job->setSpecifiedEnd(0, task->constraintEndTime().toSecsSinceEpoch() - 1); |
| 884 |
logDebug(task, nullptr, QString("MFO: set specified end: %1").arg(TJ::time2ISO(task->constraintEndTime().toSecsSinceEpoch()))); |
| 885 |
} else { |
| 886 |
logWarning(task, nullptr, xi18nc("@info/plain", "%1: Invalid end constraint", task->constraintToString(true))); |
| 887 |
} |
| 888 |
break; |
| 889 |
case Node::FinishNotLater: { |
| 890 |
break; |
| 891 |
} |
| 892 |
case Node::FixedInterval: { |
| 893 |
job->setPriority(700); |
| 894 |
TJ::Interval i(toTJInterval(task->constraintStartTime(), task->constraintEndTime(), tjGranularity())); |
| 895 |
job->setSpecifiedPeriod(0, i); |
| 896 |
// estimate not allowed |
| 897 |
job->setDuration(0, 0.0); |
| 898 |
job->setLength(0, 0.0); |
| 899 |
job->setEffort(0, 0.0); |
| 900 |
logDebug(task, nullptr, QString("FI: set specified: %1 - %2 -> %3 - %4 (%5)") |
| 901 |
.arg(TJ::time2ISO(task->constraintStartTime().toSecsSinceEpoch())) |
| 902 |
.arg(TJ::time2ISO(task->constraintEndTime().toSecsSinceEpoch())) |
| 903 |
.arg(TJ::time2ISO(i.getStart())) |
| 904 |
.arg(TJ::time2ISO(i.getEnd())) |
| 905 |
.arg(tjGranularity())); |
| 906 |
break; |
| 907 |
} |
| 908 |
default: |
| 909 |
logWarning(task, nullptr, xi18nc("@info/plain", "Unhandled time constraint type")); |
| 910 |
break; |
| 911 |
} |
| 912 |
} |
| 913 |
|
| 914 |
TJ::Task *PlanTJScheduler::addStartNotEarlier(Node *task) |
| 915 |
{ |
| 916 |
DateTime time = task->constraintStartTime(); |
| 917 |
if (task->estimate()->type() == Estimate::Type_Duration && task->estimate()->calendar() != nullptr) { |
| 918 |
Calendar *cal = task->estimate()->calendar(); |
| 919 |
if (cal != m_project->defaultCalendar() && cal != m_project->calendars().value(0)) { |
| 920 |
logWarning(task, nullptr, xi18nc("@info/plain", "Could not use the correct calendar for calculation of task duration")); |
| 921 |
} else { |
| 922 |
time = cal->firstAvailableAfter(time, m_project->constraintEndTime()); |
| 923 |
} |
| 924 |
} |
| 925 |
TJ::Task *p = new TJ::Task(m_tjProject, QString("%1-sne").arg(m_tjProject->taskCount() + 1), task->name() + "-sne", nullptr, QString(), 0); |
| 926 |
p->setSpecifiedStart(0, toTJTime_t(time, tjGranularity())); |
| 927 |
p->setSpecifiedEnd(0, m_tjProject->getEnd() - 1); |
| 928 |
//debugPlan<<"PlanTJScheduler::addStartNotEarlier:"<<time<<TJ::time2ISO(toTJTime_t(time, tjGranularity())); |
| 929 |
return p; |
| 930 |
} |
| 931 |
|
| 932 |
TJ::Task *PlanTJScheduler::addFinishNotLater(Node *task) |
| 933 |
{ |
| 934 |
DateTime time = task->constraintEndTime(); |
| 935 |
if (task->estimate()->type() == Estimate::Type_Duration && task->estimate()->calendar() != nullptr) { |
| 936 |
Calendar *cal = task->estimate()->calendar(); |
| 937 |
if (cal != m_project->defaultCalendar() && cal != m_project->calendars().value(0)) { |
| 938 |
logWarning(task, nullptr, xi18nc("@info/plain", "Could not use the correct calendar for calculation of task duration")); |
| 939 |
} else { |
| 940 |
time = cal->firstAvailableBefore(time, m_project->constraintStartTime()); |
| 941 |
} |
| 942 |
} |
| 943 |
TJ::Task *p = new TJ::Task(m_tjProject, QString("%1-fnl").arg(m_tjProject->taskCount() + 1), task->name() + "-fnl", nullptr, QString(), 0); |
| 944 |
p->setSpecifiedEnd(0, toTJTime_t(time, tjGranularity()) - 1); |
| 945 |
p->setSpecifiedStart(0, m_tjProject->getStart()); |
| 946 |
return p; |
| 947 |
} |
| 948 |
|
| 949 |
|
| 950 |
void PlanTJScheduler::addRequests() |
| 951 |
{ |
| 952 |
//debugPlan; |
| 953 |
QMap<TJ::Task*, Node*> ::const_iterator it = m_taskmap.constBegin(); |
| 954 |
for (; it != m_taskmap.constEnd(); ++it) { |
| 955 |
addRequest(it.key(), it.value()); |
| 956 |
} |
| 957 |
} |
| 958 |
|
| 959 |
void PlanTJScheduler::addRequest(TJ::Task *job, Node *task_) |
| 960 |
{ |
| 961 |
//debugPlan; |
| 962 |
if (task_->type() == Node::Type_Milestone || task_->estimate() == nullptr || (m_recalculate && static_cast<Task*>(task_)->completion().isFinished())) { |
| 963 |
job->setMilestone(true); |
| 964 |
job->setDuration(0, 0.0); |
| 965 |
return; |
| 966 |
} |
| 967 |
auto task = static_cast<Task*>(task_); |
| 968 |
// Note: FI tasks can never have an estimate set (duration, length or effort) |
| 969 |
if (task->constraint() != Node::FixedInterval) { |
| 970 |
if (task->estimate()->type() == Estimate::Type_Duration && task->estimate()->calendar() == nullptr) { |
| 971 |
job->setDuration(0, task->estimate()->value(Estimate::Use_Expected, m_usePert).toDouble(Duration::Unit_d)); |
| 972 |
return; |
| 973 |
} |
| 974 |
if (task->estimate()->type() == Estimate::Type_Duration && task->estimate()->calendar() != nullptr) { |
| 975 |
job->setLength(0, task->estimate()->value(Estimate::Use_Expected, m_usePert).toDouble(Duration::Unit_d) * 24.0 / m_tjProject->getDailyWorkingHours()); |
| 976 |
if (static_cast<Task*>(task)->isStarted()) { |
| 977 |
job->setSpecifiedStart(0, toTJTime_t(task->completion().startTime(), tjGranularity())); |
| 978 |
} |
| 979 |
return; |
| 980 |
} |
| 981 |
if (m_recalculate) { |
| 982 |
auto tjTask = m_tjProject->getTask("TJ::RECALCULATE_FROM"); |
| 983 |
if (tjTask) { |
| 984 |
job->addDepends(tjTask->getId()); |
| 985 |
tjTask->addPrecedes(job->getId()); |
| 986 |
logInfo(task, nullptr, i18n("Recalculate, earliest start: %1", QLocale().toString(m_recalculateFrom, QLocale::ShortFormat))); |
| 987 |
} |
| 988 |
} |
| 989 |
if (m_recalculate && task->completion().isStarted()) { |
| 990 |
auto id = QString("TJ::%1").arg(task->id()); |
| 991 |
auto t = new TJ::Task(m_tjProject, id, id, nullptr, QString(), 0); |
| 992 |
t->setMilestone(true); |
| 993 |
t->setSpecifiedStart(0, toTJTime_t(task->completion().startTime(), tjGranularity())); |
| 994 |
job->addDepends(t->getId()); |
| 995 |
t->addPrecedes(job->getId()); |
| 996 |
|
| 997 |
const Estimate *estimate = task->estimate(); |
| 998 |
const double e = estimate->scale(task->completion().remainingEffort(), Duration::Unit_d, estimate->scales()); |
| 999 |
job->setEffort(0, e); |
| 1000 |
logInfo(task, nullptr, i18n("Task has started. Remaining effort: %1d", e)); |
| 1001 |
} else { |
| 1002 |
Estimate *estimate = task->estimate(); |
| 1003 |
double e = estimate->scale(estimate->value(Estimate::Use_Expected, m_usePert), Duration::Unit_d, estimate->scales()); |
| 1004 |
job->setEffort(0, e); |
| 1005 |
} |
| 1006 |
} |
| 1007 |
if (task->requests().isEmpty()) { |
| 1008 |
if (task->type() == Node::Type_Task) { |
| 1009 |
warnPlan<<"Task:"<<task<<"has no allocations"; |
| 1010 |
logDebug(task, nullptr, "Task has no resource allocations"); |
| 1011 |
} |
| 1012 |
return; |
| 1013 |
} |
| 1014 |
const auto lst = task->requests().resourceRequests(true /*resolveTeam*/); |
| 1015 |
for (ResourceRequest *rr : lst) { |
| 1016 |
if (!rr->resource()->calendar()) { |
| 1017 |
result = 1; // stops scheduling |
| 1018 |
logError(task, nullptr, i18n("No working hours defined for resource: %1", rr->resource()->name())); |
| 1019 |
continue; // may happen if no calendar is set, and no default calendar |
| 1020 |
} |
| 1021 |
TJ::Resource *tjr = addResource(rr->resource()); |
| 1022 |
TJ::Allocation *a = new TJ::Allocation(); |
| 1023 |
a->setSelectionMode(TJ::Allocation::order); |
| 1024 |
if (rr->units() != 100) { |
| 1025 |
TJ::UsageLimits *l = new TJ::UsageLimits(); |
| 1026 |
l->setDailyUnits(rr->units()); |
| 1027 |
a->setLimits(l); |
| 1028 |
} |
| 1029 |
a->addCandidate(tjr); |
| 1030 |
const auto alternativeRequests = rr->alternativeRequests(); |
| 1031 |
for (ResourceRequest *alt : alternativeRequests) { |
| 1032 |
TJ::Resource *atjr = addResource(alt->resource()); |
| 1033 |
if (alt->units() != 100) { |
| 1034 |
TJ::UsageLimits *l = new TJ::UsageLimits(); |
| 1035 |
l->setDailyUnits(alt->units()); |
| 1036 |
a->setLimits(l); |
| 1037 |
} |
| 1038 |
a->addCandidate(atjr); |
| 1039 |
} |
| 1040 |
job->addAllocation(a); |
| 1041 |
logDebug(task, nullptr, "Added resource candidate: " + rr->resource()->name()); |
| 1042 |
const auto lst = rr->requiredResources(); |
| 1043 |
for (Resource *r : lst) { |
| 1044 |
TJ::Resource *tr = addResource(r); |
| 1045 |
a->addRequiredResource(tjr, tr); |
| 1046 |
logDebug(task, nullptr, "Added required resource: " + r->name()); |
| 1047 |
} |
| 1048 |
} |
| 1049 |
} |
| 1050 |
|
| 1051 |
void PlanTJScheduler::schedule(SchedulingContext &context) |
| 1052 |
{ |
| 1053 |
if (context.projects.isEmpty()) { |
| 1054 |
warnPlan<<"No projects"; |
| 1055 |
logError(context.project, nullptr, "No projects to schedule"); |
| 1056 |
return; |
| 1057 |
} |
| 1058 |
QElapsedTimer timer; |
| 1059 |
timer.start(); |
| 1060 |
|
| 1061 |
m_project = context.project; |
| 1062 |
m_tjProject = new TJ::Project(); |
| 1063 |
connect(m_tjProject, &TJ::Project::updateProgressBar, this, [this](int value, int) { |
| 1064 |
Q_EMIT progressChanged(value); |
| 1065 |
}); |
| 1066 |
|
| 1067 |
m_tjProject->setPriority(0); |
| 1068 |
m_tjProject->setScheduleGranularity(m_granularity / 1000); |
| 1069 |
m_tjProject->getScenario(0)->setMinSlackRate(0.0); // Do not calculate critical path |
| 1070 |
if (context.calculateFrom.isValid()) { |
| 1071 |
m_recalculate = true; |
| 1072 |
m_recalculateFrom = context.calculateFrom; |
| 1073 |
auto t = new TJ::Task(m_tjProject, "TJ::RECALCULATE_FROM", "TJ::RECALCULATE_FROM", nullptr, QString(), 0); |
| 1074 |
t->setMilestone(true); |
| 1075 |
t->setSpecifiedStart(0, toTJTime_t(m_recalculateFrom, tjGranularity())); |
| 1076 |
} |
| 1077 |
|
| 1078 |
connect(&TJ::TJMH, &TJ::TjMessageHandler::message, this, &PlanTJScheduler::slotMessage); |
| 1079 |
|
| 1080 |
logInfo(m_project, nullptr, i18n("Scheduling started: %1", QDateTime::currentDateTime().toString(Qt::ISODate))); |
| 1081 |
if (m_recalculate) { |
| 1082 |
logInfo(m_project, nullptr, i18n("Recalculating from: %1", context.calculateFrom.toString(Qt::ISODate))); |
| 1083 |
} |
| 1084 |
|
| 1085 |
if (context.scheduleInParallel) { |
| 1086 |
calculateParallel(context); |
| 1087 |
} else { |
| 1088 |
calculateSequential(context); |
| 1089 |
} |
| 1090 |
if (context.cancelScheduling) { |
| 1091 |
KPlato::Schedule::Log log(m_project, KPlato::Schedule::Log::Type_Warning, i18n("Scheduling canceled")); |
| 1092 |
context.log << log; |
| 1093 |
} else { |
| 1094 |
logInfo(m_project, nullptr, i18n("Scheduling finished at %1, elapsed time: %2 seconds", QDateTime::currentDateTime().toString(Qt::ISODate), (double)timer.elapsed()/1000)); |
| 1095 |
context.log = takeLog(); |
| 1096 |
QMultiMap<int, KoDocument*>::const_iterator it = context.projects.constBegin(); |
| 1097 |
for (; it != context.projects.constEnd(); ++it) { |
| 1098 |
it.value()->setModified(true); |
| 1099 |
} |
| 1100 |
} |
| 1101 |
m_project = nullptr; // or else it is deleted |
| 1102 |
} |
| 1103 |
|
| 1104 |
void PlanTJScheduler::calculateParallel(KPlato::SchedulingContext &context) |
| 1105 |
{ |
| 1106 |
QMultiMap<int, KoDocument*>::const_iterator it = context.projects.constBegin(); |
| 1107 |
for (; it != context.projects.constEnd(); ++it) { |
| 1108 |
logInfo(m_project, nullptr, QString("Inserting project: %1, priority %2").arg(it.value()->projectName()).arg(it.key())); // TODO i18n |
| 1109 |
insertProject(it.value(), it.key(), context); |
| 1110 |
} |
| 1111 |
logInfo(m_project, nullptr, i18n("Scheduling interval: %1 - %2, granularity: %3 minutes", |
| 1112 |
QDateTime::fromSecsSinceEpoch(m_tjProject->getStart()).toString(Qt::ISODate), |
| 1113 |
QDateTime::fromSecsSinceEpoch(m_tjProject->getEnd()).toString(Qt::ISODate), |
| 1114 |
(double)m_tjProject->getScheduleGranularity()/60)); |
| 1115 |
if (m_tjProject->getStart() > m_tjProject->getEnd()) { |
| 1116 |
logError(m_project, nullptr, i18n("Invalid project, start > end")); |
| 1117 |
return; |
| 1118 |
} |
| 1119 |
addRequests(); |
| 1120 |
insertBookings(context); |
| 1121 |
setConstraints(); |
| 1122 |
addDependencies(); |
| 1123 |
addStartEndJob(); |
| 1124 |
if (!check()) { |
| 1125 |
logError(m_project, nullptr, i18n("Project check failed")); |
| 1126 |
} else { |
| 1127 |
if (solve() && !context.cancelScheduling) { |
| 1128 |
populateProjects(context); |
| 1129 |
const auto &docs = context.projects.values(); |
| 1130 |
for (auto *doc : docs) { |
| 1131 |
auto p = doc->project(); |
| 1132 |
if (p->currentSchedule()->isScheduled()) { |
| 1133 |
logInfo(p, nullptr, i18n("Scheduled: %1 - %2", p->startTime().toString(Qt::ISODate), p->endTime().toString(Qt::ISODate))); |
| 1134 |
} else { |
| 1135 |
logError(p, nullptr, i18n("Scheduling failed")); |
| 1136 |
} |
| 1137 |
} |
| 1138 |
} else if (!context.cancelScheduling) { |
| 1139 |
logError(m_project, nullptr, i18n("Project scheduling failed")); |
| 1140 |
} |
| 1141 |
} |
| 1142 |
} |
| 1143 |
|
| 1144 |
void PlanTJScheduler::calculateSequential(KPlato::SchedulingContext &context) |
| 1145 |
{ |
| 1146 |
QMultiMapIterator<int, KoDocument*> it(context.projects); |
| 1147 |
for (it.toBack(); it.hasPrevious();) { |
| 1148 |
if (context.cancelScheduling) { |
| 1149 |
break; |
| 1150 |
} |
| 1151 |
it.previous(); |
| 1152 |
auto project = it.value()->project(); |
| 1153 |
logInfo(project, nullptr, QString("Inserting project")); // TODO i18n |
| 1154 |
insertProject(it.value(), it.key(), context); |
| 1155 |
logInfo(project, nullptr, i18n("Scheduling interval: %1 - %2, granularity: %3 minutes", |
| 1156 |
QDateTime::fromSecsSinceEpoch(m_tjProject->getStart()).toString(Qt::ISODate), |
| 1157 |
QDateTime::fromSecsSinceEpoch(m_tjProject->getEnd()).toString(Qt::ISODate), |
| 1158 |
(double)m_tjProject->getScheduleGranularity()/60.)); |
| 1159 |
|
| 1160 |
if (m_tjProject->getStart() > m_tjProject->getEnd()) { |
| 1161 |
logError(project, nullptr, i18n("Invalid project, start > end")); |
| 1162 |
return; |
| 1163 |
} |
| 1164 |
addRequests(); |
| 1165 |
insertBookings(context); |
| 1166 |
setConstraints(); |
| 1167 |
addDependencies(); |
| 1168 |
addStartEndJob(); |
| 1169 |
if (!check()) { |
| 1170 |
logError(project, nullptr, i18n("Project check failed")); |
| 1171 |
} else { |
| 1172 |
if (solve()) { |
| 1173 |
if (context.cancelScheduling) { |
| 1174 |
logWarning(context.project, nullptr, i18n("Scheduling canceled")); |
| 1175 |
} else { |
| 1176 |
populateProjects(context); |
| 1177 |
context.resourceBookings << it.value(); |
| 1178 |
} |
| 1179 |
} else { |
| 1180 |
logError(project, nullptr, i18n("Project scheduling failed")); |
| 1181 |
} |
| 1182 |
} |
| 1183 |
if (!context.cancelScheduling && it.hasPrevious()) { |
| 1184 |
delete m_tjProject; |
| 1185 |
m_taskmap.clear(); |
| 1186 |
m_resourcemap.clear(); |
| 1187 |
m_resourceIds.clear(); |
| 1188 |
m_durationTasks.clear(); |
| 1189 |
|
| 1190 |
//m_granularity = std::max(context.granularity, 5*60*1000 /*5 minutes*/); |
| 1191 |
m_tjProject = new TJ::Project(); |
| 1192 |
connect(m_tjProject, &TJ::Project::updateProgressBar, this, [this](int value, int) { |
| 1193 |
Q_EMIT progressChanged(value); |
| 1194 |
}); |
| 1195 |
m_tjProject->setPriority(0); |
| 1196 |
m_tjProject->setScheduleGranularity(m_granularity / 1000); |
| 1197 |
m_tjProject->getScenario(0)->setMinSlackRate(0.0); // Do not calculate critical path |
| 1198 |
if (context.calculateFrom.isValid()) { |
| 1199 |
m_recalculate = true; |
| 1200 |
m_recalculateFrom = context.calculateFrom; |
| 1201 |
auto t = new TJ::Task(m_tjProject, "TJ::RECALCULATE_FROM", "TJ::RECALCULATE_FROM", nullptr, QString(), 0); |
| 1202 |
t->setMilestone(true); |
| 1203 |
t->setSpecifiedStart(0, toTJTime_t(m_recalculateFrom, tjGranularity())); |
| 1204 |
} |
| 1205 |
} |
| 1206 |
} |
| 1207 |
} |
| 1208 |
|
| 1209 |
void PlanTJScheduler::insertBookings(KPlato::SchedulingContext &context) |
| 1210 |
{ |
| 1211 |
// Collect appointments from all resource in all projects and |
| 1212 |
// map them to tj resources |
| 1213 |
QHash<TJ::Resource*, KPlato::Appointment> apps; |
| 1214 |
for (const auto doc : std::as_const(context.resourceBookings)) { |
| 1215 |
const auto project = doc->project(); |
| 1216 |
const auto manager = project->findScheduleManagerByName(doc->property(SCHEDULEMANAGERNAME).toString()); |
| 1217 |
long sid = ANYSCHEDULED; |
| 1218 |
if (manager) { |
| 1219 |
sid = manager->scheduleId(); |
| 1220 |
} |
| 1221 |
const auto resourceList = project->resourceList(); |
| 1222 |
for (Resource *r : resourceList) { |
| 1223 |
TJ::Resource *tjResource = m_resourcemap.key(m_resourceIds.value(r->id())); |
| 1224 |
if (!tjResource) { |
| 1225 |
continue; |
| 1226 |
} |
| 1227 |
apps[tjResource] += r->appointmentIntervals(sid); |
| 1228 |
} |
| 1229 |
} |
| 1230 |
// TODO: Handle load < 100% |
| 1231 |
QHash<TJ::Resource*, KPlato::Appointment>::const_iterator ait; |
| 1232 |
for (ait = apps.constBegin(); ait != apps.constEnd(); ++ait) { |
| 1233 |
KPlato::Resource *r = m_resourcemap.value(ait.key()); |
| 1234 |
Q_ASSERT(r); |
| 1235 |
const QMultiMap<QDate, AppointmentInterval> map = ait.value().intervals().map(); |
| 1236 |
QMultiMap<QDate, AppointmentInterval>::const_iterator it; |
| 1237 |
for (it = map.constBegin(); it != map.constEnd(); ++it) { |
| 1238 |
TJ::Interval interval = toTJInterval(it.value().startTime(), it.value().endTime(), m_granularity / 1000); |
| 1239 |
ait.key()->bookInterval(0, interval, 3 /*undefined*/); |
| 1240 |
if (it.value().load() < r->units()) { |
| 1241 |
logWarning(m_project, r, i18n("Appointment with load (%1) less than available resource units (%2) not supported").arg(it.value().load(), r->units())); |
| 1242 |
} |
| 1243 |
} |
| 1244 |
} |
| 1245 |
} |
| 1246 |
|
| 1247 |
void PlanTJScheduler::insertProject(KoDocument *doc, int priority, KPlato::SchedulingContext &context) |
| 1248 |
{ |
| 1249 |
auto project = doc->project(); |
| 1250 |
auto sm = getScheduleManager(project); |
| 1251 |
Q_ASSERT(sm); |
| 1252 |
doc->setProperty(SCHEDULEMANAGERNAME, sm->name()); |
| 1253 |
if (m_recalculate) { |
| 1254 |
sm->setRecalculate(true); |
| 1255 |
sm->setRecalculateFrom(context.calculateFrom); |
| 1256 |
} |
| 1257 |
time_t time = project->constraintStartTime().toSecsSinceEpoch(); |
| 1258 |
if (m_tjProject->getStart() == 0 || m_tjProject->getStart() > time) { |
| 1259 |
m_tjProject->setStart(time); |
| 1260 |
} |
| 1261 |
time = project->constraintEndTime().toSecsSinceEpoch(); |
| 1262 |
if (m_tjProject->getEnd() < time) { |
| 1263 |
m_tjProject->setEnd(time); |
| 1264 |
} |
| 1265 |
m_project->setConstraintStartTime(QDateTime::fromSecsSinceEpoch(m_tjProject->getStart())); |
| 1266 |
m_project->setConstraintEndTime(QDateTime::fromSecsSinceEpoch(m_tjProject->getEnd())); |
| 1267 |
m_tjProject->setDailyWorkingHours(const_cast<KPlato::Project*>(project)->standardWorktime()->day()); |
| 1268 |
|
| 1269 |
// Set working days for the project, it is used for tasks with a length specification |
| 1270 |
// FIXME: Plan has task specific calendars for this estimate type |
| 1271 |
KPlato::Calendar *cal = project->defaultCalendar(); |
| 1272 |
if (! cal) { |
| 1273 |
cal = project->calendars().value(0); |
| 1274 |
} |
| 1275 |
if (cal) { |
| 1276 |
// TJ: Sun = 0, Mon = 1 ... Sat = 6, ours: Mon = 1 ... Sun = 7 (as qt) |
| 1277 |
int days[ 7 ] = { Qt::Sunday, Qt::Monday, Qt::Tuesday, Qt::Wednesday, Qt::Thursday, Qt::Friday, Qt::Saturday }; |
| 1278 |
for (int i = 0; i < 7; ++i) { |
| 1279 |
CalendarDay *d = nullptr; |
| 1280 |
for (Calendar *c = cal; c; c = c->parentCal()) { |
| 1281 |
QElapsedTimer t; t.start(); |
| 1282 |
d = c->weekday(days[ i ]); |
| 1283 |
Q_ASSERT(d); |
| 1284 |
if (d == nullptr || d->state() != CalendarDay::Undefined) { |
| 1285 |
break; |
| 1286 |
} |
| 1287 |
} |
| 1288 |
if (d && d->state() == CalendarDay::Working) { |
| 1289 |
QList<TJ::Interval*> lst; |
| 1290 |
const auto intervals = d->timeIntervals(); |
| 1291 |
for (const TimeInterval *ti : intervals) { |
| 1292 |
TJ::Interval *tji = new TJ::Interval(toTJInterval(ti->startTime(), ti->endTime(),tjGranularity())); |
| 1293 |
lst << tji; |
| 1294 |
} |
| 1295 |
m_tjProject->setWorkingHours(i, lst); |
| 1296 |
qDeleteAll(lst); |
| 1297 |
} |
| 1298 |
} |
| 1299 |
} |
| 1300 |
TJ::Task *t = addTask(project, nullptr); |
| 1301 |
addTasks(project, t, priority); |
| 1302 |
} |
| 1303 |
|
| 1304 |
void PlanTJScheduler::addTasks(const KPlato::Node *parent, TJ::Task *tjParent, int projectPriority) |
| 1305 |
{ |
| 1306 |
//debugPlan; |
| 1307 |
for (const KPlato::Node *n : parent->childNodeIterator()) { |
| 1308 |
switch (n->type()) { |
| 1309 |
case Node::Type_Project: |
| 1310 |
case Node::Type_Summarytask: { |
| 1311 |
TJ::Task *t = addTask(n, tjParent); |
| 1312 |
addTasks(n, t); |
| 1313 |
break; |
| 1314 |
} |
| 1315 |
case Node::Type_Task: |
| 1316 |
case Node::Type_Milestone: { |
| 1317 |
switch (n->constraint()) { |
| 1318 |
case Node::StartNotEarlier: |
| 1319 |
// TODO parent = addStartNotEarlier(n); |
| 1320 |
break; |
| 1321 |
case Node::FinishNotLater: |
| 1322 |
// TODO parent = addFinishNotLater(n); |
| 1323 |
break; |
| 1324 |
default: |
| 1325 |
break; |
| 1326 |
} |
| 1327 |
TJ::Task *t = addTask(n, tjParent); |
| 1328 |
t->setPriority(t->getPriority() + projectPriority); |
| 1329 |
break; |
| 1330 |
} |
| 1331 |
default: |
| 1332 |
break; |
| 1333 |
} |
| 1334 |
} |
| 1335 |
} |
| 1336 |
|
| 1337 |
void PlanTJScheduler::cancelScheduling(SchedulingContext &context) |
| 1338 |
{ |
| 1339 |
Q_UNUSED(context); |
| 1340 |
context.cancelScheduling = true; |
| 1341 |
if (m_tjProject) { |
| 1342 |
m_tjProject->cancelScheduling(); |
| 1343 |
} |
| 1344 |
} |
| 1345 |
|
| 1346 |
void PlanTJScheduler::populateProjects(KPlato::SchedulingContext &context) |
| 1347 |
{ |
| 1348 |
Q_UNUSED(context) |
| 1349 |
|
| 1350 |
QMap<TJ::Task*, Node*>::const_iterator it; |
| 1351 |
for (it = m_taskmap.constBegin(); it != m_taskmap.constEnd(); ++it) { |
| 1352 |
KPlato::Node *node = it.value(); |
| 1353 |
KPlato::Project *project = qobject_cast<KPlato::Project*>(node->projectNode()); |
| 1354 |
Q_ASSERT(project); |
| 1355 |
if (!project) { |
| 1356 |
continue; |
| 1357 |
} |
| 1358 |
if (!project->currentScheduleManager()->expected()) { |
| 1359 |
project->currentScheduleManager()->createSchedules(); |
| 1360 |
} |
| 1361 |
auto mainschedule = project->currentScheduleManager()->expected(); |
| 1362 |
project->setCurrentSchedule(mainschedule->id()); |
| 1363 |
switch (node->type()) { |
| 1364 |
case KPlato::Node::Type_Task: |
| 1365 |
case KPlato::Node::Type_Milestone: |
| 1366 |
node->createSchedule(mainschedule); |
| 1367 |
node->setCurrentSchedule(mainschedule->id()); |
| 1368 |
break; |
| 1369 |
default: |
| 1370 |
break; |
| 1371 |
} |
| 1372 |
switch (node->type()) { |
| 1373 |
case KPlato::Node::Type_Project: |
| 1374 |
break; |
| 1375 |
case KPlato::Node::Type_Summarytask: |
| 1376 |
break; |
| 1377 |
case KPlato::Node::Type_Task: |
| 1378 |
case KPlato::Node::Type_Milestone: |
| 1379 |
taskFromTJ(project, m_tjProject->getTask(node->id()), node); |
| 1380 |
break; |
| 1381 |
default: break; |
| 1382 |
} |
| 1383 |
mainschedule->setScheduled(true); |
| 1384 |
} |
| 1385 |
} |