-
Notifications
You must be signed in to change notification settings - Fork 98
/
solution.rb
298 lines (270 loc) · 7.88 KB
/
solution.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
class RDF::Query
##
# An RDF query solution.
#
# @example Iterating over every binding in the solution
# solution.each_binding { |name, value| puts value.inspect }
# solution.each_variable { |variable| puts variable.value.inspect }
#
# @example Iterating over every value in the solution
# solution.each_value { |value| puts value.inspect }
#
# @example Checking whether a variable is bound or unbound
# solution.bound?(:title)
# solution.unbound?(:mbox)
#
# @example Retrieving the value of a bound variable
# solution[:mbox]
# solution.mbox
#
# @example Retrieving all bindings in the solution as a `Hash`
# solution.to_h #=> {mbox: "jrhacker@example.org", ...}
#
class Solution
# Undefine all superfluous instance methods:
undef_method(*instance_methods.
map(&:to_s).
select {|m| m =~ /^\w+$/}.
reject {|m| %w(object_id dup instance_eval inspect to_s private_methods class should should_not pretty_print).include?(m) || m[0,2] == '__'}.
map(&:to_sym))
include Enumerable
##
# Initializes the query solution.
#
# @param [Hash{Symbol => RDF::Term}] bindings
# @yield [solution]
def initialize(bindings = {}, &block)
@bindings = bindings.to_hash
if block_given?
case block.arity
when 1 then block.call(self)
else instance_eval(&block)
end
end
end
# @private
attr_reader :bindings
##
# Enumerates over every variable binding in this solution.
#
# @yield [name, value]
# @yieldparam [Symbol] name
# @yieldparam [RDF::Term] value
# @return [Enumerator]
def each_binding(&block)
@bindings.each(&block)
end
alias_method :each, :each_binding
##
# Enumerates over every variable name in this solution.
#
# @yield [name]
# @yieldparam [Symbol] name
# @return [Enumerator]
def each_name(&block)
@bindings.each_key(&block)
end
alias_method :each_key, :each_name
##
# Enumerates over every variable value in this solution.
#
# @yield [value]
# @yieldparam [RDF::Term] value
# @return [Enumerator]
def each_value(&block)
@bindings.each_value(&block)
end
##
# Returns `true` if this solution contains bindings for any of the given
# `variables`.
#
# @param [Array<Symbol, #to_sym>] variables
# an array of variables to check
# @return [Boolean] `true` or `false`
# @since 0.3.0
def has_variables?(variables)
variables.any? { |variable| bound?(variable) }
end
##
# Enumerates over every variable in this solution.
#
# @yield [variable]
# @yieldparam [Variable]
# @return [Enumerator]
def each_variable
@bindings.each do |name, value|
yield Variable.new(name, value)
end
end
##
# Returns `true` if the variable `name` is bound in this solution.
#
# @param [Symbol, #to_sym] name
# the variable name
# @return [Boolean] `true` or `false`
def bound?(name)
!unbound?(name)
end
##
# Returns `true` if the variable `name` is unbound in this solution.
#
# @param [Symbol, #to_sym] name
# the variable name
# @return [Boolean] `true` or `false`
def unbound?(name)
@bindings[name.to_sym].nil?
end
##
# Returns the value of the variable `name`.
#
# @param [Symbol, #to_sym] name
# the variable name
# @return [RDF::Term]
def [](name)
@bindings[name.to_sym]
end
##
# Binds or rebinds the variable `name` to the given `value`.
#
# @param [Symbol, #to_sym] name
# the variable name
# @param [RDF::Term] value
# @return [RDF::Term]
# @since 0.3.0
def []=(name, value)
@bindings[name.to_sym] = value.is_a?(RDF::Term) ? value : RDF::Literal(value)
end
##
# Merges the bindings from the given `other` query solution into this
# one, overwriting any existing ones having the same name.
#
# @param [RDF::Query::Solution, #to_h] other
# another query solution or hash bindings
# @return [void] self
# @since 0.3.0
def merge!(other)
@bindings.merge!(other.to_h)
self
end
##
# Merges the bindings from the given `other` query solution with a copy
# of this one.
#
# @param [RDF::Query::Solution, #to_h] other
# another query solution or hash bindings
# @return [RDF::Query::Solution]
# @since 0.3.0
def merge(other)
self.class.new(@bindings.dup).merge!(other)
end
##
# Duplicate solution, preserving patterns
# @return [RDF::Statement]
def dup
merge({})
end
##
# Compatible Mappings
#
# Two solution mappings u1 and u2 are compatible if, for every variable v in dom(u1) and in dom(u2), u1(v) = u2(v).
#
# @param [RDF::Query::Solution, #to_h] other
# another query solution or hash bindings
# @return [Boolean]
# @see http://www.w3.org/TR/2013/REC-sparql11-query-20130321/#defn_algCompatibleMapping
def compatible?(other)
@bindings.all? do |k, v|
!other.to_h.has_key?(k) || other[k].eql?(v)
end
end
##
# Disjoint mapping
#
# A solution is disjoint with another solution if it shares no common variables in their domains.
#
# @param [RDF::Query::Solution] other
# @return [Boolean]
# @see http://www.w3.org/TR/2013/REC-sparql11-query-20130321/#defn_algMinus
def disjoint?(other)
@bindings.none? do |k, v|
v && other.to_h.has_key?(k) && other[k].eql?(v)
end
end
##
# Isomorphic Mappings
# Two solution mappings u1 and u2 are isomorphic if,
# for every variable v in dom(u1) and in dom(u2), u1(v) = u2(v).
#
# @param [RDF::Query::Solution, #to_h] other
# another query solution or hash bindings
# @return [Boolean]
def isomorphic_with?(other)
@bindings.all? do |k, v|
!other.to_h.has_key?(k) || other[k].eql?(v)
end
end
##
# @return [Array<Array(Symbol, RDF::Term)>}
def to_a
@bindings.to_a
end
##
# @return [Hash{Symbol => RDF::Term}}
def to_h
@bindings.dup
end
##
# Integer hash of this solution
# @return [Integer]
def hash
@bindings.hash
end
##
# Equivalence of solution
def eql?(other)
other.is_a?(Solution) && @bindings.eql?(other.bindings)
end
alias_method :==, :eql?
##
# Equals of solution
def ==(other)
other.is_a?(Solution) && @bindings == other.bindings
end
##
# @return [String]
def inspect
sprintf("#<%s:%#0x(%s)>", self.class.name, __id__, @bindings.inspect)
end
protected
##
# @overload #to_hash
# Returns object representation of this URI, broken into components
#
# @return (see #to_h)
# @deprecated Use {#to_h} instead.
#
# @overload binding(name)
# Return the binding for this name
#
# @param [Symbol] name
# @return [RDF::Term]
def method_missing(name, *args, &block)
if name == :to_hash
warn "[DEPRECATION] RDF::Query::Solution#to_hash is deprecated, use RDF::Query::Solution#to_h instead.\n" +
"This is due to the introduction of keyword arugments that attempt to turn the last argument into a hash using #to_hash.\n" +
"This can be avoided by explicitly passing an options hash as the last argument.\n" +
"Called from #{Gem.location_of_caller.join(':')}"
self.to_h
elsif args.empty? && @bindings.has_key?(name.to_sym)
@bindings[name.to_sym]
else
super # raises NoMethodError
end
end
##
# @return [Boolean]
def respond_to_missing?(name, include_private = false)
@bindings.has_key?(name.to_sym) || super
end
end # Solution
end # RDF::Query