From 9e36a2c306d1ebf9b7e36fe5686a94d8c05453d5 Mon Sep 17 00:00:00 2001 From: Ben Lavender Date: Sat, 11 Dec 2010 20:42:05 -0600 Subject: [PATCH] Defining success for a no-variable query as returning [{}], all special casing for variable count can be removed. --- lib/rdf/query.rb | 53 ++++++++++++++++++++---------------------------- 1 file changed, 22 insertions(+), 31 deletions(-) diff --git a/lib/rdf/query.rb b/lib/rdf/query.rb index 11eeab7f..90793e97 100644 --- a/lib/rdf/query.rb +++ b/lib/rdf/query.rb @@ -192,46 +192,37 @@ def optimize!(options = {}) # @see http://www.holygoat.co.uk/blog/entry/2005-10-25-1 def execute(queryable, options = {}) @solutions = Solutions.new - @failed = false @patterns.each do |pattern| - case pattern.variable_count - when 0 # no variables - if pattern.execute(queryable).empty? - # return an empty solution sequence: - @solutions.clear - @failed = true - break - end - - else case # 1 or 2 variables + case # 1 or 2 variables - when !@solutions.have_variables?(pattern.variables.values) - if @solutions.empty? - pattern.execute(queryable) do |statement| - @solutions << pattern.solution(statement) - end - else # union - old_solutions, @solutions = @solutions, Solutions.new - pattern.execute(queryable) do |statement| - old_solutions.each do |solution| - @solutions << solution.merge(pattern.solution(statement)) - end - end + when !@solutions.have_variables?(pattern.variables.values) + if @solutions.empty? + pattern.execute(queryable) do |statement| + @solutions << pattern.solution(statement) end - - else # also union + else # union old_solutions, @solutions = @solutions, Solutions.new - old_solutions.each_with_index do |solution, index| - pattern.execute(queryable, solution) do |statement| + pattern.execute(queryable) do |statement| + old_solutions.each do |solution| @solutions << solution.merge(pattern.solution(statement)) end end - end + end + + else # also union + old_solutions, @solutions = @solutions, Solutions.new + old_solutions.each_with_index do |solution, index| + pattern.execute(queryable, solution) do |statement| + @solutions << solution.merge(pattern.solution(statement)) + end + end end + # It's important to abort failed queries quickly because later patterns + # that can have constraints are often broad without them. + # We have no solutions at all: + return @solutions if @solutions.empty? + # We have no solutions for variables we should have solutions for: if !pattern.optional? && pattern.variables.keys.any? { |variable| !@solutions.variable_names.include?(variable) } - # no point continuing, there are no bindings. It's important to - # abort because later patterns that can have constraints are often - # broad without them. return Solutions.new end end