-
Notifications
You must be signed in to change notification settings - Fork 14
/
extend.rb
171 lines (160 loc) · 5.92 KB
/
extend.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
module SPARQL; module Algebra
class Operator
##
# The SPARQL `Extend` operator.
#
# Extends a solution
#
# [60] Bind ::= 'BIND' '(' Expression 'AS' Var ')'
#
# @example SPARQL Grammar
# SELECT ?z
# {
# ?x <http://example.org/p> ?o
# BIND(?o+10 AS ?z)
# }
#
# @example SSE
# (project (?z)
# (extend ((?z (+ ?o 10)))
# (bgp (triple ?x <http://example.org/p> ?o))))
#
# @example SPARQL Grammar (cast as boolean)
# PREFIX : <http://example.org/>
# PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
# PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
# SELECT ?a ?v (xsd:boolean(?v) AS ?boolean)
# WHERE { ?a :p ?v . }
#
# @example SSE (cast as boolean)
# (prefix ((: <http://example.org/>)
# (rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>)
# (xsd: <http://www.w3.org/2001/XMLSchema#>))
# (project (?a ?v ?boolean)
# (extend ((?boolean (xsd:boolean ?v)))
# (bgp (triple ?a :p ?v)))))
#
# @example SPARQL Grammar (inner bind)
# PREFIX : <http://example.org/>
#
# SELECT ?z ?s1
# {
# ?s ?p ?o .
# BIND(?o+1 AS ?z)
# ?s1 ?p1 ?z
# }
#
# @example SSE (inner bind)
# (prefix ((: <http://example.org/>))
# (project (?z ?s1)
# (join
# (extend ((?z (+ ?o 1)))
# (bgp (triple ?s ?p ?o)))
# (bgp (triple ?s1 ?p1 ?z)))))
#
# @see https://www.w3.org/TR/sparql11-query/#evaluation
class Extend < Operator::Binary
include Query
NAME = [:extend]
##
# Let μ be a solution mapping, Ω a multiset of solution mappings, var a variable and expr be an expression, then we define:
#
# Extend(μ, var, expr) = μ ∪ { (var,value) | var not in dom(μ) and value = expr(μ) }
#
# Extend(μ, var, expr) = μ if var not in dom(μ) and expr(μ) is an error
#
# Extend is undefined when var in dom(μ).
#
# Extend(Ω, var, expr) = { Extend(μ, var, expr) | μ in Ω }
#
# @param [RDF::Queryable] queryable
# the graph or repository to query
# @param [Hash{Symbol => Object}] options
# any additional keyword options
# @yield [solution]
# each matching solution
# @yieldparam [RDF::Query::Solution] solution
# @yieldreturn [void] ignored
# @return [RDF::Query::Solutions]
# the resulting solution sequence
# @see https://www.w3.org/TR/sparql11-query/#evaluation
def execute(queryable, **options, &block)
debug(options) {"Extend"}
@solutions = operand(1).execute(queryable, depth: options[:depth].to_i + 1, **options)
@solutions.each do |solution|
# Re-bind to bindings, if defined, as they might not be found in solution
options[:bindings].each_binding do |name, value|
solution[name] = value if operands.first.variables.include?(name)
end if options[:bindings] && operands.first.respond_to?(:variables)
debug(options) {"===> soln #{solution.to_h.inspect}"}
operand(0).each do |(var, expr)|
begin
val = expr.evaluate(solution, queryable: queryable,
depth: options[:depth].to_i + 1,
**options)
debug(options) {"===> + #{var} => #{val.inspect}"}
val = val.dup.bind(solution) if val.is_a?(RDF::Query::Pattern)
solution.bindings[var.to_sym] = val
rescue TypeError => e
# Evaluates to error, ignore
debug(options) {"===> #{var} error: #{e.message}"}
end
end
end
@solutions.each(&block) if block_given?
@solutions
end
# The variable introduced by the BIND clause must not have been used in the group graph pattern up to the point of use in BIND
#
# Also, variables used in a binding expression must be projected by the query.
def validate!
bind_vars = operand(0).map(&:first).map(&:name)
query_vars = operand(1).variables.keys
unless (bind_vars.compact & query_vars.compact).empty?
raise ArgumentError,
"bound variable used in query: #{(bind_vars.compact & query_vars.compact).to_sse}"
end
# Special case for group variables
if operands.last.is_a?(Group)
bind_expr_vars = operand(0).map(&:last).variables.keys
group_vars = operands.last.variables.keys
group_internal_vars = operands.last.internal_variables.keys
bind_expr_vars.each do |v|
raise ArgumentError,
"extension expression uses variable not in scope: #{v}" if
group_internal_vars.include?(v) &&
!group_vars.include?(v)
end
end
super
end
##
# The variables used in the extension.
# Includes extended variables.
#
# @return [Hash{Symbol => RDF::Query::Variable}]
def variables
operands.first.
map(&:first).
map(&:variables).
inject(operands.last.variables) {|memo, h| memo.merge(h)}
end
##
#
# Returns a partial SPARQL grammar for this operator.
#
# Extracts bindings.
#
# @return [String]
def to_sparql(**options)
extensions = operands.first.inject({}) do |memo, (as, expression)|
# Use string/name of variable "as" to aid in later matching
memo.merge(as.to_s => expression)
end
# Merge any inherited extensions from options
extensions = options.delete(:extensions).merge(extensions) if options.key?(:extensions)
operands.last.to_sparql(extensions: extensions, **options)
end
end # Extend
end # Operator
end; end # SPARQL::Algebra