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 """
from bopytex.planner import Task
class Scheduler:
def __init__(self, actions: list):
def __init__(self, actions: list, done: list[str] = None):
self.actions = actions
if done is None:
self._done = []
else:
self._done = done
self._tasks = []
self._done = []
@property
def tasks(self):
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
def done(self):
return self._done
def append(self, tasks):
def append(self, tasks: list[Task]):
self._tasks += tasks
def dispatch(self, task):
@ -53,3 +69,6 @@ class Scheduler:
def run(self):
for _ in self:
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():
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)
assert scheduler.tasks == tasks
assert scheduler.all_deps == {"dep1", "dep2", "dep3"}
assert scheduler.all_output == {"end1", "end2"}
def test_schedule_dispatch():
@ -48,6 +53,9 @@ def test_schedule_multiple_tasks():
t2 = Task(action="DO", args={"task": "two"}, deps=[], output="two")
t3 = Task(action="DO", args={"task": "three"}, deps=[], output="three")
scheduler.append([t1, t2, t3])
assert scheduler.is_finishable() == True
result = scheduler.next()
assert result == "[] - {'task': 'one'} - one - done"
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")
scheduler.append([t1, t2, t3])
assert scheduler.is_finishable() == True
result = scheduler.next()
assert result == "[] - {'task': 'three'} - three - done"
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")
t2 = Task(action="DO", args={"task": "two"}, deps=[], output="two")
scheduler.append([t1, t2])
assert scheduler.is_finishable() == False
scheduler.run()
assert scheduler.tasks == [t1]
assert scheduler.done == ["two"]