Skip to content

Commit

Permalink
Extract domain knowledge responsability to CSP
Browse files Browse the repository at this point in the history
  • Loading branch information
dmnelson committed Feb 3, 2015
1 parent 9482eba commit 4b0e668
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/winston/backtrack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def select_unassigned_variable(assignments)
end

def domain_values(var)
csp.variables[var.name].domain.values
csp.domain_for(var.name)
end
end
end
7 changes: 7 additions & 0 deletions lib/winston/csp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ def validate(changed_var, assignments)
true
end

def domain_for(variable_name)
variable = variables[variable_name]
return [] if variable.nil? || variable.domain.nil?

variable.domain.values
end

private

def var_assignments
Expand Down
19 changes: 19 additions & 0 deletions spec/winston/csp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,25 @@
end
end

describe "#domain_for" do
before do
subject.add_variable :a, value: 1
subject.add_variable :b, domain: [1, 2]
end

it "returns domain values for a given variable" do
expect(subject.domain_for(:b)).to eq([1, 2])
end

it "returns an empty list when a variable doesn't exist in the problem" do
expect(subject.domain_for(:c)).to be_empty
end

it "return an empty list when a variable doesn't have a domain" do
expect(subject.domain_for(:a)).to be_empty
end
end

describe "#solve" do
let(:solver) { double("Solver") }
before do
Expand Down

0 comments on commit 4b0e668

Please sign in to comment.