-
Notifications
You must be signed in to change notification settings - Fork 98
/
pattern.rb
378 lines (349 loc) · 13.6 KB
/
pattern.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
module RDF; class Query
##
# An RDF query pattern.
class Pattern < RDF::Statement
##
# @private
# @since 0.2.2
def self.from(pattern, graph_name: nil, **options)
case pattern
when Pattern then pattern
when Array, Statement
graph_name ||= pattern[3]
self.new(pattern[0], pattern[1], pattern[2], graph_name: graph_name, **options)
when Hash then self.new(options.merge(pattern))
else raise ArgumentError, "expected RDF::Query::Pattern, RDF::Statement, Hash, or Array, but got #{pattern.inspect}"
end
end
##
# @overload initialize(options = {})
# @param [Hash{Symbol => Object}] options
# @option options [Variable, Resource, Symbol, nil] :subject (nil)
# @option options [Variable, URI, Symbol, nil] :predicate (nil)
# @option options [Variable, Term, Symbol, nil] :object (nil)
# @option options [Variable, Resource, Symbol, nil, false] :graph_name (nil)
# A graph_name of nil matches any graph, a graph_name of false, matches only the default graph. (See {RDF::Query#initialize})
# @option options [Boolean] :optional (false)
#
# @overload initialize(subject, predicate, object, options = {})
# @param [Variable, Resource, Symbol, nil] subject
# @param [Variable, URI, Symbol, nil] predicate
# @param [Variable, Termm, Symbol, nil] object
# @param [Hash{Symbol => Object}] options
# @option options [Variable, Resource, Symbol, nil, false] :graph_name (nil)
# A graph_name of nil matches any graph, a graph_name of false, matches only the default graph. (See {RDF::Query#initialize})
# @option options [Boolean] :optional (false)
#
# @note {Statement} treats symbols as interned {Node} instances, in a {Pattern}, they are treated as {Variable}.
def initialize(subject = nil, predicate = nil, object = nil, options = {})
super
end
##
# @private
def initialize!
@graph_name = Variable.new(@graph_name) if @graph_name.is_a?(Symbol)
@subject = Variable.new(@subject) if @subject.is_a?(Symbol)
@predicate = Variable.new(@predicate) if @predicate.is_a?(Symbol)
@object = Variable.new(@object) if @object.is_a?(Symbol)
# Estmate cost positionally, with variables being least expensive as objects, then predicates, then subjects, then graph_names.
# XXX does not consider bound variables, which would need to be dynamically calculated.
@cost = (@object.nil? || @object.is_a?(Variable) ? 8 : 0) +
(@predicate.nil? || @predicate.is_a?(Variable) ? 4 : 0) +
(@subject.nil? || @subject.is_a?(Variable) ? 2 : 0) +
(@graph_name.is_a?(Variable) ? 1 : 0) +
(@object.is_a?(Pattern) ? (@object.cost * 4) : 0) +
(@subject.is_a?(Pattern) ? (@subject.cost * 2) : 0)
super
end
##
# Create a new pattern from the quads, recursivly dupping sub-patterns.
def dup
self.class.from(self.to_quad.map {|t| t.is_a?(RDF::Query::Pattern) ? t.dup : t})
end
##
# Any additional options for this pattern.
#
# @return [Hash]
attr_reader :options
##
# The estimated cost of this pattern (for query optimization).
#
# @return [Numeric]
attr_accessor :cost
##
# Returns `true` if this is a blank pattern, with all terms being `nil`.
#
# @return [Boolean] `true` or `false`
# @since 0.3.0
def blank?
subject.nil? && predicate.nil? && object.nil? && graph_name.nil?
end
##
# Returns `true` if this pattern contains any variables.
#
# @return [Boolean] `true` or `false`
# @since 0.3.0
def variables?
subject && subject.variable? ||
predicate && predicate.variable? ||
object && object.variable? ||
graph_name && graph_name.variable?
end
alias_method :has_variables?, :variables?
##
# Returns `true` if this is an optional pattern.
#
# @example
# Pattern.new(:s, :p, :o).optional? #=> false
# Pattern.new(:s, :p, :o, optional: true).optional? #=> true
#
# @return [Boolean] `true` or `false`
# @since 0.3.0
def optional?
!!options[:optional]
end
##
# Is this pattern composed only of valid components?
#
# @return [Boolean] `true` or `false`
def valid?
(subject? ? (subject.resource? || subject.variable?) && subject.valid? : true) &&
(predicate? ? (predicate.uri? || predicate.variable?) && predicate.valid? : true) &&
(object? ? (object.term? || object.variable?) && object.valid? : true) &&
(graph? ? (graph_name.resource? || graph_name.variable?) && graph_name.valid? : true)
rescue NoMethodError
false
end
##
# Checks pattern equality against a statement, considering nesting.
#
# * A pattern which has a pattern as a subject or an object, matches
# a statement having a statement as a subject or an object using {#eql?}.
#
# @param [Statement] other
# @return [Boolean]
#
# @see RDF::URI#==
# @see RDF::Node#==
# @see RDF::Literal#==
# @see RDF::Query::Variable#==
def eql?(other)
return false unless other.is_a?(Statement) && (self.graph_name || false) == (other.graph_name || false)
predicate == other.predicate &&
(subject.is_a?(Pattern) ? subject.eql?(other.subject) : subject == other.subject) &&
(object.is_a?(Pattern) ? object.eql?(other.object) : object == other.object)
end
##
# Executes this query pattern on the given `queryable` object.
#
# Values are matched using using Queryable#query_pattern.
#
# If the optional `bindings` are given, variables will be substituted with their values when executing the query.
#
# To match triples only in the default graph, set graph_name to `false`.
#
# @example
# Pattern.new(:s, :p, :o).execute(RDF::Repository.load('etc/doap.nt'))
#
# @param [RDF::Queryable] queryable
# the graph or repository to query
# @param [Hash{Symbol => RDF::Term}, RDF::Query::Solution] bindings
# optional variable bindings to use
# @yield [statement]
# each matching statement
# @yieldparam [RDF::Statement] statement
# an RDF statement matching this pattern
# @return [Enumerable<RDF::Query::Pattern>]
# an enumerator yielding matching statements
# @see RDF::Queryable#query
# @since 0.3.0
def execute(queryable, bindings = {}, &block)
bindings = bindings.to_h if bindings.is_a?(Solution)
query = {
subject: subject.is_a?(Variable) && bindings[subject.to_sym] ? bindings[subject.to_sym] : subject,
predicate: predicate.is_a?(Variable) && bindings[predicate.to_sym] ? bindings[predicate.to_sym] : predicate,
object: object.is_a?(Variable) && bindings[object.to_sym] ? bindings[object.to_sym] : object,
graph_name: graph_name.is_a?(Variable) && bindings[graph_name.to_sym] ? bindings[graph_name.to_sym] : graph_name,
}.delete_if{|k,v| v.nil?}
# Do all the variable terms refer to distinct variables?
variables = self.variables
if variable_count == variables.size
# If so, we can just let the repository implementation handle
# everything and yield matching statements directly:
queryable.query(query, &block)
# No, some terms actually refer to the same variable...
else
# Considering embedding, figure out if variables that may appear more than once resolve to the same value.
vars = variables.keys
queryable.query(query).select do |statement|
if vars.all? {|var| self.var_values(var, statement).uniq.size == 1}
yield statement if block_given?
true
end
end
end
end
##
# Returns a query solution constructed by binding any variables in this
# pattern with the corresponding terms in the given `statement`.
#
# @example
# pattern = Pattern.new(:s, :p, :o)
# solution = pattern.solution(statement)
#
# pattern[:s] #=> statement.subject
# pattern[:p] #=> statement.predicate
# pattern[:o] #=> statement.object
#
# @param [RDF::Statement] statement
# an RDF statement to bind terms from
# @return [RDF::Query::Solution]
# @since 0.3.0
def solution(statement)
RDF::Query::Solution.new do |solution|
solution[subject.to_sym] = statement.subject if subject.is_a?(Variable)
solution[predicate.to_sym] = statement.predicate if predicate.is_a?(Variable)
solution[object.to_sym] = statement.object if object.is_a?(Variable)
solution[graph_name.to_sym] = statement.graph_name if graph_name.is_a?(Variable)
solution.merge!(subject.solution(statement.subject)) if subject.respond_to?(:solution)
solution.merge!(object.solution(statement.object)) if object.respond_to?(:solution)
end
end
##
# Returns the variable terms in this pattern.
#
# @example
# Pattern.new(RDF::Node.new, :p, 123).variable_terms #=> [:predicate]
#
# @param [Symbol, #to_sym] name
# an optional variable name
# @return [Array<Symbol>]
# @deprecated use {#var_values} instead
# @since 0.3.0
def variable_terms(name = nil)
warn "[DEPRECATION] RDF::Query::Pattern#variable_terms is deprecated and will be removed in a future version.\n" +
"Called from #{Gem.location_of_caller.join(':')}"
terms = []
terms << :subject if subject.is_a?(Variable) && (!name || name.eql?(subject.name))
terms << :predicate if predicate.is_a?(Variable) && (!name || name.eql?(predicate.name))
terms << :object if object.is_a?(Variable) && (!name || name.eql?(object.name))
terms << :graph_name if graph_name.is_a?(Variable) && (!name || name.eql?(graph_name.name))
terms
end
##
# Returns all values the statement in the same pattern position
#
# @param [Symbol] var
# @param [RDF::Statement] statement
# @return [Array<RDF::Term>]
def var_values(var, statement)
%i(subject predicate object graph_name).map do |position|
po = self.send(position)
so = statement.send(position)
po.var_values(var, so) if po.respond_to?(:var_values)
end.flatten.compact
end
##
# Returns the number of variables in this pattern.
#
# Note: this does not count distinct variables, and will therefore e.g.
# return 3 even if two terms are actually the same variable.
#
# @return [Integer] (0..3)
def variable_count
[subject, predicate, object, graph_name].inject(0) do |memo, term|
memo += (term.is_a?(Variable) ? 1 :
(term.respond_to?(:variable_count) ? term.variable_count : 0))
end
end
alias_method :cardinality, :variable_count
alias_method :arity, :variable_count
##
# Returns all variables in this pattern.
#
# Note: this returns a hash containing distinct variables only.
#
# @return [Hash{Symbol => Variable}]
def variables
[subject, predicate, object, graph_name].inject({}) do |memo, term|
term && term.variable? ? memo.merge(term.variables) : memo
end
end
##
# Binds the pattern to a solution, making it no longer variable if all variables are resolved to bound variables
#
# @param [RDF::Query::Solution] solution
# @return [self]
def bind(solution)
self.to_quad.each_with_index do |term, index|
if term.is_a?(Variable) && solution[term]
self[index] = solution[term]
elsif term.is_a?(Pattern)
term.bind(solution)
end
end
self
end
##
# Returns `true` if this pattern contains bindings.
#
# @return [Boolean] `true` or `false`
def bindings?
!bindings.empty?
end
##
# Returns the number of bindings in this pattern.
#
# @return [Integer] (0..3)
def binding_count
bindings.size
end
##
# Returns all bindings in this pattern.
#
# @return [Hash{Symbol => RDF::Term}]
def bindings
bindings = {}
bindings.merge!(subject.bindings) if subject && subject.variable?
bindings.merge!(predicate.bindings) if predicate && predicate.variable?
bindings.merge!(object.bindings) if object && object.variable?
bindings.merge!(graph_name.bindings) if graph_name && graph_name.variable?
bindings
end
##
# Returns `true` if all variables in this pattern are bound.
#
# @return [Boolean] `true` or `false`
def bound?
!variables.empty? && variables.values.all?(&:bound?)
end
##
# Returns all bound variables in this pattern.
#
# @return [Hash{Symbol => Variable}]
def bound_variables
variables.reject { |name, variable| variable.unbound? }
end
##
# Returns `true` if all variables in this pattern are unbound.
#
# @return [Boolean] `true` or `false`
def unbound?
!variables.empty? && variables.values.all?(&:unbound?)
end
##
# Returns all unbound variables in this pattern.
#
# @return [Hash{Symbol => Variable}]
def unbound_variables
variables.reject { |name, variable| variable.bound? }
end
##
# Returns a string representation of this pattern.
#
# @return [String]
def to_s
(optional? ? 'OPTIONAL ' : '') + super
end
end # Pattern
end; end # RDF::Query