-
Notifications
You must be signed in to change notification settings - Fork 14
/
sequence.rb
66 lines (58 loc) · 2.11 KB
/
sequence.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
module SPARQL; module Algebra
class Operator
##
# The SPARQL UPDATE `sequence` operator.
#
# Sequences through each operand.
#
# [103] CollectionPath ::= '(' GraphNodePath+ ')'
#
# @see https://www.w3.org/TR/sparql11-query/#collections
class Sequence < Operator
include SPARQL::Algebra::Update
NAME = :sequence
##
# Basically a JOIN across multiple operands
#
# @param [RDF::Queryable] queryable
# the graph or repository to write
# @param [Hash{Symbol => Object}] options
# any additional keyword options
# @option options [Boolean] debug
# Query execution debugging
# @yield [solution]
# each matching solution
# @yieldparam [RDF::Query::Solution] solution
# @yieldreturn [void] ignored
# @see https://www.w3.org/TR/sparql11-query/#sparqlAlgebra
def execute(queryable, **options, &block)
debug(options) {"Sequence #{operands.to_sse}"}
last = queryable.query(operands.shift, **options.merge(depth: options[:depth].to_i + 1))
debug(options) {"(sequence)=>(last) #{last.map(&:to_h).to_sse}"}
operands.each do |op|
this = queryable.query(op, **options.merge(depth: options[:depth].to_i + 1))
debug(options) {"(sequence)=>(this) #{this.map(&:to_h).to_sse}"}
last = last.map do |s1|
this.map do |s2|
s2.merge(s1) if s2.compatible?(s1)
end
end.flatten.compact
debug(options) {"(sequence)=>(next) #{last.map(&:to_h).to_sse}"}
end
@solutions = RDF::Query::Solutions.new(last)
debug(options) {"(sequence)=> #{@solutions.map(&:to_h).to_sse}"}
@solutions.each(&block) if block_given?
@solutions
end
##
#
# Returns a partial SPARQL grammar for this operator.
#
# @return [String]
def to_sparql(**options)
str = "{\n" + operands.to_sparql(top_level: false, **options) + "\n}"
Operator.to_sparql(str, **options)
end
end # Sequence
end # Operator
end; end # SPARQL::Algebra