Class: RDF::Query::Solution
- Inherits:
-
Object
- Object
- RDF::Query::Solution
- Includes:
- Enumerable
- Defined in:
- lib/rdf/query/solution.rb
Overview
An RDF query solution.
Constant Summary collapse
- INSTANCE_METHODS =
Temporarily remember instance method for deprecation message in ‘method_missing`.
instance_methods
Instance Attribute Summary collapse
- #bindings ⇒ Object readonly
Instance Method Summary collapse
-
#==(other) ⇒ Object
Equals of solution.
-
#[](name) ⇒ RDF::Term
Returns the value of the variable ‘name`.
-
#[]=(name, value) ⇒ RDF::Term
Binds or rebinds the variable ‘name` to the given `value`.
-
#bound?(name) ⇒ Boolean
Returns ‘true` if the variable `name` is bound in this solution.
-
#compatible?(other) ⇒ Boolean
Compatible Mappings.
-
#disjoint?(other) ⇒ Boolean
Disjoint mapping.
-
#dup ⇒ RDF::Statement
Duplicate solution, preserving patterns.
-
#each_binding {|name, value| ... } ⇒ Enumerator
(also: #each)
Enumerates over every variable binding in this solution.
-
#each_name {|name| ... } ⇒ Enumerator
(also: #each_key)
Enumerates over every variable name in this solution.
-
#each_value {|value| ... } ⇒ Enumerator
Enumerates over every variable value in this solution.
-
#each_variable {|variable| ... } ⇒ Enumerator
Enumerates over every variable in this solution.
-
#enum_binding ⇒ Enumerator<RDF::Resource>
Returns an enumerator for #each_binding.
-
#enum_name ⇒ Enumerator<RDF::Resource>
Returns an enumerator for #each_name.
-
#enum_value ⇒ Enumerator<RDF::Resource>
Returns an enumerator for #each_value.
-
#enum_variable ⇒ Enumerator<RDF::Resource>
Returns an enumerator for #each_variable.
-
#eql?(other) ⇒ Boolean
Equivalence of solution.
-
#hash ⇒ Integer
Integer hash of this solution.
-
#initialize(bindings = {}) {|solution| ... } ⇒ Solution
constructor
Initializes the query solution.
- #inspect ⇒ String
-
#isomorphic_with?(other) ⇒ Boolean
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).
-
#merge(other) ⇒ RDF::Query::Solution
Merges the bindings from the given ‘other` query solution with a copy of this one.
-
#merge!(other) ⇒ void
Merges the bindings from the given ‘other` query solution into this one, overwriting any existing ones having the same name.
- #to_a ⇒ Array<Array(Symbol, RDF::Term)>
- #to_h ⇒ Hash{Symbol => RDF::Term}
-
#unbound?(name) ⇒ Boolean
Returns ‘true` if the variable `name` is unbound in this solution.
-
#variable?(*args) ⇒ Object
(also: #variables?, #has_variables?)
‘variables`.
Methods included from Enumerable
#canonicalize, #canonicalize!, #dump, #each_graph, #each_object, #each_predicate, #each_quad, #each_statement, #each_subject, #each_term, #each_triple, #enum_graph, #enum_object, #enum_predicate, #enum_quad, #enum_statement, #enum_subject, #enum_term, #enum_triple, #graph?, #graph_names, #invalid?, #object?, #objects, #predicate?, #predicates, #project_graph, #quad?, #quads, #statement?, #statements, #subject?, #subjects, #supports?, #term?, #terms, #to_set, #triple?, #triples, #valid?, #validate!
Methods included from Util::Aliasing::LateBound
Methods included from Countable
Constructor Details
#initialize(bindings = {}) {|solution| ... } ⇒ Solution
Initializes the query solution.
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/rdf/query/solution.rb', line 42 def initialize(bindings = {}, &block) @bindings = bindings.to_h if block_given? case block.arity when 1 then block.call(self) else instance_eval(&block) end end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#binding(name) ⇒ RDF::Term (protected)
347 348 349 350 351 352 353 354 355 356 357 358 359 |
# File 'lib/rdf/query/solution.rb', line 347 def method_missing(name, *args, &block) if args.empty? && @bindings.key?(name.to_sym) if INSTANCE_METHODS.include?(name) warn "[DEPRECATION] RDF::Query::Solution##{name} is an overridden instance method.\n" + "Its use as a solution accessor is deprecated and will be removed in a future version.\n" + "Use #[] for safe access.\n" + "Called from #{Gem.location_of_caller.join(':')}" end @bindings[name.to_sym] else super # raises NoMethodError end end |
Instance Attribute Details
#bindings ⇒ Object (readonly)
54 55 56 |
# File 'lib/rdf/query/solution.rb', line 54 def bindings @bindings end |
Instance Method Details
#==(other) ⇒ Object
Equals of solution
329 330 331 |
# File 'lib/rdf/query/solution.rb', line 329 def ==(other) other.is_a?(Solution) && @bindings == other.bindings end |
#[](name) ⇒ RDF::Term
Returns the value of the variable ‘name`.
192 193 194 |
# File 'lib/rdf/query/solution.rb', line 192 def [](name) @bindings[name.to_sym] end |
#[]=(name, value) ⇒ RDF::Term
Binds or rebinds the variable ‘name` to the given `value`.
204 205 206 |
# File 'lib/rdf/query/solution.rb', line 204 def []=(name, value) @bindings[name.to_sym] = value.is_a?(RDF::Term) ? value : RDF::Literal(value) end |
#bound?(name) ⇒ Boolean
Returns ‘true` if the variable `name` is bound in this solution.
172 173 174 |
# File 'lib/rdf/query/solution.rb', line 172 def bound?(name) !unbound?(name) end |
#compatible?(other) ⇒ Boolean
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).
268 269 270 271 272 |
# File 'lib/rdf/query/solution.rb', line 268 def compatible?(other) @bindings.all? do |k, v| !other.to_h.key?(k) || other[k].eql?(v) end end |
#disjoint?(other) ⇒ Boolean
Disjoint mapping
A solution is disjoint with another solution if it shares no common variables in their domains.
282 283 284 285 286 |
# File 'lib/rdf/query/solution.rb', line 282 def disjoint?(other) @bindings.none? do |k, v| v && other.to_h.key?(k) && other[k].eql?(v) end end |
#dup ⇒ RDF::Statement
Duplicate solution, preserving patterns
255 256 257 |
# File 'lib/rdf/query/solution.rb', line 255 def dup merge({}) end |
#each_binding {|name, value| ... } ⇒ Enumerator Also known as: each
Enumerates over every variable binding in this solution.
63 64 65 66 |
# File 'lib/rdf/query/solution.rb', line 63 def each_binding(&block) @bindings.each(&block) if block_given? enum_binding end |
#each_name {|name| ... } ⇒ Enumerator Also known as: each_key
Enumerates over every variable name in this solution.
84 85 86 87 |
# File 'lib/rdf/query/solution.rb', line 84 def each_name(&block) @bindings.each_key(&block) if block_given? enum_name end |
#each_value {|value| ... } ⇒ Enumerator
Enumerates over every variable value in this solution.
105 106 107 108 |
# File 'lib/rdf/query/solution.rb', line 105 def each_value(&block) @bindings.each_value(&block) if block_given? enum_value end |
#each_variable {|variable| ... } ⇒ Enumerator
Enumerates over every variable in this solution.
148 149 150 151 152 153 154 155 |
# File 'lib/rdf/query/solution.rb', line 148 def each_variable if block_given? @bindings.each do |name, value| yield Variable.new(name, value) end end enum_variable end |
#enum_binding ⇒ Enumerator<RDF::Resource>
Returns an enumerator for #each_binding.
74 75 76 |
# File 'lib/rdf/query/solution.rb', line 74 def enum_binding enum_for(:each_binding) end |
#enum_name ⇒ Enumerator<RDF::Resource>
Returns an enumerator for #each_name.
95 96 97 |
# File 'lib/rdf/query/solution.rb', line 95 def enum_name enum_for(:each_name) end |
#enum_value ⇒ Enumerator<RDF::Resource>
Returns an enumerator for #each_value.
115 116 117 |
# File 'lib/rdf/query/solution.rb', line 115 def enum_value enum_for(:each_value) end |
#enum_variable ⇒ Enumerator<RDF::Resource>
Returns an enumerator for #each_variable.
162 163 164 |
# File 'lib/rdf/query/solution.rb', line 162 def enum_variable enum_for(:each_variable) end |
#eql?(other) ⇒ Boolean
Equivalence of solution
323 324 325 |
# File 'lib/rdf/query/solution.rb', line 323 def eql?(other) other.is_a?(Solution) && @bindings.eql?(other.bindings) end |
#hash ⇒ Integer
Integer hash of this solution
317 318 319 |
# File 'lib/rdf/query/solution.rb', line 317 def hash @bindings.hash end |
#inspect ⇒ String
335 336 337 |
# File 'lib/rdf/query/solution.rb', line 335 def inspect sprintf("#<%s:%#0x(%s)>", self.class.name, __id__, @bindings.inspect) end |
#isomorphic_with?(other) ⇒ Boolean
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).
296 297 298 299 300 |
# File 'lib/rdf/query/solution.rb', line 296 def isomorphic_with?(other) @bindings.all? do |k, v| !other.to_h.key?(k) || other[k].eql?(v) end end |
#merge(other) ⇒ RDF::Query::Solution
Merges the bindings from the given ‘other` query solution with a copy of this one.
248 249 250 |
# File 'lib/rdf/query/solution.rb', line 248 def merge(other) self.class.new(@bindings.dup).merge!(other) end |
#merge!(other) ⇒ void
This method returns an undefined value.
Merges the bindings from the given ‘other` query solution into this one, overwriting any existing ones having the same name.
## RDF-star
If merging a binding for a statement to a pattern, merge their embedded solutions.
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/rdf/query/solution.rb', line 221 def merge!(other) @bindings.merge!(other.to_h) do |key, v1, v2| # Don't merge a pattern over a statement # This happens because JOIN does a reverse merge, # and a pattern is set in v2. v2.is_a?(Pattern) ? v1 : v2 end # Merge bindings from patterns = [] @bindings.each do |k, v| if v.is_a?(Pattern) && other[k].is_a?(RDF::Statement) << v.solution(other[k]) end end # Merge embedded solutions .each {|soln| merge!(soln)} self end |
#to_a ⇒ Array<Array(Symbol, RDF::Term)>
304 305 306 |
# File 'lib/rdf/query/solution.rb', line 304 def to_a @bindings.to_a end |
#to_h ⇒ Hash{Symbol => RDF::Term}
310 311 312 |
# File 'lib/rdf/query/solution.rb', line 310 def to_h @bindings.dup end |
#unbound?(name) ⇒ Boolean
Returns ‘true` if the variable `name` is unbound in this solution.
182 183 184 |
# File 'lib/rdf/query/solution.rb', line 182 def unbound?(name) @bindings[name.to_sym].nil? end |
#variable? ⇒ Boolean #variable?(variables) ⇒ Object Also known as: variables?, has_variables?
‘variables`.
@param [Array<Symbol, #to_sym>] variables
@return [Boolean]
131 132 133 134 135 136 137 138 |
# File 'lib/rdf/query/solution.rb', line 131 def variable?(*args) case args.length when 0 then false when 1 args.first.any? { |variable| bound?(variable) } else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)") end end |