Skip to content

Commit

Permalink
Defining success for a no-variable query as returning [{}], all special
Browse files Browse the repository at this point in the history
casing for variable count can be removed.
  • Loading branch information
bhuga committed Dec 12, 2010
1 parent d27936c commit 9e36a2c
Showing 1 changed file with 22 additions and 31 deletions.
53 changes: 22 additions & 31 deletions lib/rdf/query.rb
Expand Up @@ -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
Expand Down

0 comments on commit 9e36a2c

Please sign in to comment.