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: add fine-grained policy merge #466

Open
wants to merge 2 commits into
base: transition-to-runkit
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
21 changes: 14 additions & 7 deletions lib/syskit/network_generation/dataflow_dynamics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,16 @@ def compute_connection_policies
policy_graph
end

def merge_policy(explicit_policy, computed_policy)
merged_policy = computed_policy.merge(explicit_policy)

if merged_policy[:type] == :data
merged_policy.delete(:size)
end

merged_policy
end

# @api private
#
# Compute the policies for all connections starting from a given task
Expand All @@ -563,13 +573,10 @@ def compute_policies_from(connection_graph, source_task, policy_graph = {})
mappings.each_with_object({}) do |(port_pair, policy), h|
policy = policy.dup
fallback_policy = policy.delete(:fallback_policy)
if policy.empty?
h[port_pair] =
policy_for(source_task, *port_pair, sink_task,
fallback_policy)
else
h[port_pair] = policy
end
computed_policy = policy_for(
source_task, *port_pair, sink_task, fallback_policy
)
h[port_pair] = merge_policy(policy, computed_policy)
end
policy_graph[[source_task, sink_task]] = computed_policies
end
Expand Down
67 changes: 64 additions & 3 deletions test/network_generation/test_dataflow_dynamics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ module NetworkGeneration
policy_graph[[cmp.c_child, task]][%w[out in]])
end

it "uses in-graph policies over the computed ones" do
it "merges in-graph policies with the computed ones" do
plan.add(task0 = @task_m.new)
plan.add(task1 = @task_m.new)

Expand All @@ -307,10 +307,13 @@ module NetworkGeneration

task0.out_port.connect_to(task1.in_port, type: :buffer, size: 42)

@dynamics.should_receive(:policy_for).never
@dynamics
.should_receive(:policy_for)
.with(task0, "out", "in", task1, nil)
.and_return(type: :buffer, size: 10, init: nil)
policy_graph = @dynamics.compute_connection_policies

assert_equal({ type: :buffer, size: 42 },
assert_equal({ type: :buffer, size: 42, init: nil },
policy_graph[[task0, task1]][%w[out in]])
end

Expand Down Expand Up @@ -360,6 +363,64 @@ module NetworkGeneration
add_agents(tasks[0, 2])
flexmock(@dynamics).should_receive(:propagate).with(tasks[0, 2])
end

it "handles the case where the explicit policy sets the type to :data" do
plan.add(task0 = @task_m.new)
plan.add(task1 = @task_m.new)

add_agents(tasks = [task0, task1])
flexmock(@dynamics).should_receive(:propagate).with(tasks)

task0.out_port.connect_to task1.in_port, type: :data

flexmock(@dynamics)
.should_receive(:policy_for)
.with(task0, "out", "in", task1, nil)
.and_return(type: :buffer, size: 10, init: true)

policy_graph = @dynamics.compute_connection_policies
expected_policy = { type: :data, init: true }
assert_equal(expected_policy,
policy_graph[[task0, task1]][%w[out in]])
end
end

describe "merge_policy" do
before do
@dynamics = NetworkGeneration::DataFlowDynamics.new(plan)
end

it "merges policies by preferring explicit values over " \
"computed values" do
explicit_policy = { type: :buffer, size: 20, init: true }
computed_policy = { type: :buffer, size: 10, init: true }

merged_policy =
@dynamics.merge_policy(explicit_policy, computed_policy)

assert_equal({ type: :buffer, size: 20, init: true }, merged_policy)
end

it "removes the size value when the type is set to :data" do
explicit_policy = { type: :data, init: true }
computed_policy = { type: :buffer, size: 10, init: true }

merged_policy =
@dynamics.merge_policy(explicit_policy, computed_policy)

assert_equal({ type: :data, init: true }, merged_policy)
end

it "falls back to computed values when explicit values " \
"are not provided" do
explicit_policy = {}
computed_policy = { type: :buffer, size: 10, init: true }

merged_policy =
@dynamics.merge_policy(explicit_policy, computed_policy)

assert_equal({ type: :buffer, size: 10, init: true }, merged_policy)
end
end

describe "#policy_for" do
Expand Down