예제 #1
0
def calculateSchedule(globalDays: [Day],
                      tasks: [Task],
                      currentSchedule: Schedule,
                      start: arrow.Arrow,
                      debug=False) -> Schedule:
    currentTime = util.smoothCurrentArrow()
    if start < currentTime:
        raise Exception("Cannot calculate a schedule for the past")

    days = currentSchedule.days()
    oldDays = [day.copy() for day in days if day.date <= start.date()
               ]  # Contains all days PRIOR to start date (last is popped off)

    oldTimeSlots = []
    if oldDays:  # On initial creation, there are no old days
        lastDay = oldDays.pop(
        )  # This day is changed after the current timeslot and kept the same before.
        splitTime = Time(start.hour, start.minute)

        for ts in lastDay.timeSlots:
            if ts.endTime > splitTime and ts.startTime < splitTime:
                # Update start time to be the end of the current timeslot

                # Update Arrow object only if it isn't before the smoothCurrentArrow()
                splitArrow = arrow.Arrow(start.year,
                                         start.month,
                                         start.day,
                                         hour=ts.endTime.hours,
                                         minute=ts.endTime.minutes)
                if splitArrow >= currentTime:
                    start = splitArrow
                    splitTime = Time(start.hour, start.minute)

            if ts.startTime < splitTime and ts.taskOrAppointment is not None:
                oldTimeSlots.append(ts)

    startIndex = None
    for i, day in enumerate(globalDays):
        if day.date == start.date():
            startIndex = i
            break
    else:
        raise Exception("Error in schedule_alg 555255GGG")

    newDays = [day.copy() for day in globalDays[startIndex:]]

    # Remove TimeSlots prior to start
    firstNewDay = newDays[0]

    oldTimeSlotsToRemove = []
    for ts in firstNewDay.timeSlots:
        if ts.startTime < splitTime:
            oldTimeSlotsToRemove.append(ts)
    for otstr in oldTimeSlotsToRemove:
        firstNewDay.timeSlots.remove(otstr)

    for oldTimeSlot in oldTimeSlots:
        firstNewDay.addTimeSlot(oldTimeSlot)

    lastWorkConfirmed = currentSchedule.lastWorkConfirmed

    if lastWorkConfirmed is None:
        lastWorkConfirmed = start.clone()

    # Creates a clean copy of the tasks and days, so that no evil mutation occurs
    newDays = sorted(newDays, key=lambda d: d.date)
    tmpTasks = sorted([task.copy() for task in tasks],
                      key=lambda t: t.deadline)

    if isSolvable(tasks, newDays, start, useMinimum=False):
        happySchedule = calculateHappySchedule(
            tmpTasks, newDays, lastWorkConfirmed, start, debug=debug
        )  # Adds the history (previous schedule) to the newly calculated schedule
        happySchedule.addHistory(oldDays)
        return happySchedule
    elif isSolvable(tasks, newDays, start, useMinimum=True):
        riskySchedule = calculateSadSchedule(tmpTasks,
                                             newDays,
                                             lastWorkConfirmed,
                                             start,
                                             debug=debug)
        riskySchedule.addHistory(oldDays)
        return riskySchedule
        #return calculateRiskySchedule(tmpTasks, newDays, created=start)
    else:
        sadSchedule = calculateSadSchedule(tmpTasks,
                                           newDays,
                                           lastWorkConfirmed,
                                           start,
                                           debug=debug)
        sadSchedule.addHistory(oldDays)
        return sadSchedule