Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: define Plan#useful_tasks_using #236

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions lib/roby/plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1317,8 +1317,28 @@ def remote_tasks
end
end

# Ensures that the given tasks will end up being processed without
# forcefully stopping anything
# Return all mission/permanent tasks that current depend on the given task
def useful_tasks_using(tasks)
all_tasks = compute_useful_tasks(
Array(tasks), graphs: default_useful_task_graphs.map(&:reverse)
).to_set
all_tasks.compare_by_identity

result = []
@task_index.mission_tasks.dup.each do |t|
result << t if all_tasks.include?(t)
end
@task_index.permanent_tasks.dup.each do |t|
result << t if all_tasks.include?(t)
end
result
end

# Unmark mission/permanent tasks that depend on the tasks given as argument
#
# By doing so, it makes the tasks eligible for garbage collection. This
# is mostly used to shut down tasks from a specific task within their
# dependency graph.
def make_useless(tasks)
all_tasks = compute_useful_tasks(
Array(tasks), graphs: default_useful_task_graphs.map(&:reverse)
Expand Down
43 changes: 43 additions & 0 deletions test/test_plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,49 @@ def action; end
end
end

describe "#useful_tasks_using" do
it "returns the given task if it is a mission" do
plan.add_mission_task(task = Tasks::Simple.new)
assert_equal Set[task], plan.useful_tasks_using(task)
end

it "returns the given task if it is permanent" do
plan.add_permanent_task(task = Tasks::Simple.new)
assert_equal Set[task], plan.useful_tasks_using(task)
end

it "returns parents of the given task that are missions" do
plan.add_mission_task(a = Tasks::Simple.new)
a.depends_on(b = Tasks::Simple.new)
plan.add_mission_task(c = Tasks::Simple.new)
b.depends_on(leaf = Tasks::Simple.new)
c.depends_on(leaf = Tasks::Simple.new)
assert_equal Set[b, c], plan.useful_tasks_using(leaf)
end

it "returns parents of the given task that are permanent" do
plan.add_permanent_task(a = Tasks::Simple.new)
a.depends_on(b = Tasks::Simple.new)
plan.add_permanent_task(c = Tasks::Simple.new)
b.depends_on(leaf = Tasks::Simple.new)
c.depends_on(leaf = Tasks::Simple.new)
assert_equal Set[b, c], plan.useful_tasks_using(leaf)
plan.add_permanent_task(a = Tasks::Simple.new)
a.depends_on(b = Tasks::Simple.new)
plan.add_permanent_task(c = Tasks::Simple.new)
b.depends_on(leaf = Tasks::Simple.new)
c.depends_on(leaf = Tasks::Simple.new)
assert_equal Set[b, c], plan.useful_tasks_using(leaf)
end

it "goes across useful graphs" do
plan.add_permanent_task(a = Tasks::Simple.new)
a.planned_by(b = Tasks::Simple.new)
b.depends_on(leaf = Tasks::Simple.new)
assert_equal Set[a], plan.useful_tasks_using(leaf)
end
end

describe "#make_useless" do
it "marks a mission task as non-mission" do
plan.add_mission_task(task = Roby::Task.new)
Expand Down