Feat: write is_finishable

This commit is contained in:
Bertrand Benjamin 2022-04-09 06:56:57 +02:00
parent 4d76dc8992
commit 32c74ae679
2 changed files with 36 additions and 4 deletions

View File

@ -1,21 +1,37 @@
""" Scheduler for action to make """ """ Scheduler for action to make """
from bopytex.planner import Task
class Scheduler: class Scheduler:
def __init__(self, actions: list): def __init__(self, actions: list, done: list[str] = None):
self.actions = actions self.actions = actions
self._tasks = []
if done is None:
self._done = [] self._done = []
else:
self._done = done
self._tasks = []
@property @property
def tasks(self): def tasks(self):
return self._tasks return self._tasks
@property
def all_deps(self):
return {d for task in self.tasks for d in task.deps}
@property
def all_output(self):
return {task.output for task in self.tasks}
@property @property
def done(self): def done(self):
return self._done return self._done
def append(self, tasks): def append(self, tasks: list[Task]):
self._tasks += tasks self._tasks += tasks
def dispatch(self, task): def dispatch(self, task):
@ -53,3 +69,6 @@ class Scheduler:
def run(self): def run(self):
for _ in self: for _ in self:
pass pass
def is_finishable(self):
return self.all_deps.issubset(self.all_output)

View File

@ -12,9 +12,14 @@ actions = {"DO": action_done}
def test_schedule_append(): def test_schedule_append():
scheduler = Scheduler(actions) scheduler = Scheduler(actions)
tasks = [Task(action="DO", args={}, deps=[], output="end")] tasks = [
Task(action="DO", args={}, deps=["dep1", "dep2"], output="end1"),
Task(action="DO", args={}, deps=["dep1", "dep3"], output="end2"),
]
scheduler.append(tasks) scheduler.append(tasks)
assert scheduler.tasks == tasks assert scheduler.tasks == tasks
assert scheduler.all_deps == {"dep1", "dep2", "dep3"}
assert scheduler.all_output == {"end1", "end2"}
def test_schedule_dispatch(): def test_schedule_dispatch():
@ -48,6 +53,9 @@ def test_schedule_multiple_tasks():
t2 = Task(action="DO", args={"task": "two"}, deps=[], output="two") t2 = Task(action="DO", args={"task": "two"}, deps=[], output="two")
t3 = Task(action="DO", args={"task": "three"}, deps=[], output="three") t3 = Task(action="DO", args={"task": "three"}, deps=[], output="three")
scheduler.append([t1, t2, t3]) scheduler.append([t1, t2, t3])
assert scheduler.is_finishable() == True
result = scheduler.next() result = scheduler.next()
assert result == "[] - {'task': 'one'} - one - done" assert result == "[] - {'task': 'one'} - one - done"
assert scheduler.tasks == [t2, t3] assert scheduler.tasks == [t2, t3]
@ -71,6 +79,8 @@ def test_schedule_multiple_tasks_with_dependencies():
t3 = Task(action="DO", args={"task": "three"}, deps=[], output="three") t3 = Task(action="DO", args={"task": "three"}, deps=[], output="three")
scheduler.append([t1, t2, t3]) scheduler.append([t1, t2, t3])
assert scheduler.is_finishable() == True
result = scheduler.next() result = scheduler.next()
assert result == "[] - {'task': 'three'} - three - done" assert result == "[] - {'task': 'three'} - three - done"
assert scheduler.tasks == [t1, t2] assert scheduler.tasks == [t1, t2]
@ -99,6 +109,9 @@ def test_schedule_multiple_tasks_with_undoable_dependencies():
t1 = Task(action="DO", args={"task": "one"}, deps=["three"], output="one") t1 = Task(action="DO", args={"task": "one"}, deps=["three"], output="one")
t2 = Task(action="DO", args={"task": "two"}, deps=[], output="two") t2 = Task(action="DO", args={"task": "two"}, deps=[], output="two")
scheduler.append([t1, t2]) scheduler.append([t1, t2])
assert scheduler.is_finishable() == False
scheduler.run() scheduler.run()
assert scheduler.tasks == [t1] assert scheduler.tasks == [t1]
assert scheduler.done == ["two"] assert scheduler.done == ["two"]