File size: 1,776 Bytes
e2896cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import Literal, TypedDict, List

Priority = Literal["low", "normal", "high", "critical"]

class Task(TypedDict):
    task_id: str
    task_type: str
    priority: Priority
    source: str  # user, schedule, sensor, system


class ScheduledTask(TypedDict):
    task_id: str
    order: int
    reason: str


def schedule_tasks(tasks: List[Task]) -> List[ScheduledTask]:
    """Order tasks using simple priority and source rules.

    - critical > high > normal > low
    - user > sensor > schedule > system (within same priority)
    """
    pr_order = {"critical": 0, "high": 1, "normal": 2, "low": 3}
    src_order = {"user": 0, "sensor": 1, "schedule": 2, "system": 3}

    sorted_tasks = sorted(
        tasks,
        key=lambda t: (
            pr_order.get(t["priority"], 2),
            src_order.get(t["source"], 3),
        ),
    )

    result: List[ScheduledTask] = []
    for idx, t in enumerate(sorted_tasks, start=1):
        reason = f"priority={t['priority']}, source={t['source']} -> position {idx}."
        result.append({"task_id": t["task_id"], "order": idx, "reason": reason})

    return result


if __name__ == "__main__":
    sample: List[Task] = [
        {"task_id": "T_CLEAN_KITCHEN", "task_type": "clean_room", "priority": "high", "source": "schedule"},
        {"task_id": "T_REMIND_TRASH", "task_type": "reminder", "priority": "high", "source": "sensor"},
        {"task_id": "T_PATROL_NIGHT", "task_type": "patrol", "priority": "normal", "source": "schedule"},
        {"task_id": "T_STATUS", "task_type": "status_report", "priority": "low", "source": "system"},
        {"task_id": "T_CLEAN_LIVING", "task_type": "clean_room", "priority": "critical", "source": "user"},
    ]
    for st in schedule_tasks(sample):
        print(st)