class RDF::Query::Solution
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_hash #=> {mbox: "jrhacker@example.org", ...}
Attributes
@private
Public Class Methods
Initializes the query solution.
@param [Hash{Symbol => RDF::Term}] bindings @yield [solution]
# File lib/rdf/query/solution.rb, line 38 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
Public Instance Methods
Returns the value of the variable `name`.
@param [Symbol, to_sym] name
the variable name
@return [RDF::Term]
# File lib/rdf/query/solution.rb, line 135 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
# File lib/rdf/query/solution.rb, line 147 def []=(name, value) @bindings[name.to_sym] = value.is_a?(RDF::Term) ? value : RDF::Literal(value) 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`
# File lib/rdf/query/solution.rb, line 115 def bound?(name) !unbound?(name) 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_hash] other
another query solution or hash bindings
@return [Boolean] @see www.w3.org/TR/2013/REC-sparql11-query-20130321/#defn_algCompatibleMapping
# File lib/rdf/query/solution.rb, line 192 def compatible?(other) @bindings.all? do |k, v| !other.to_hash.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 www.w3.org/TR/2013/REC-sparql11-query-20130321/#defn_algMinus
# File lib/rdf/query/solution.rb, line 206 def disjoint?(other) @bindings.none? do |k, v| v && other.to_hash.has_key?(k) && other[k].eql?(v) end end
Duplicate solution, preserving patterns @return [RDF::Statement]
# File lib/rdf/query/solution.rb, line 179 def dup merge({}) end
Enumerates over every variable binding in this solution.
@yield [name, value] @yieldparam [Symbol] name @yieldparam [RDF::Term] value @return [Enumerator]
# File lib/rdf/query/solution.rb, line 59 def each_binding(&block) @bindings.each(&block) end
Enumerates over every variable name in this solution.
@yield [name] @yieldparam [Symbol] name @return [Enumerator]
# File lib/rdf/query/solution.rb, line 70 def each_name(&block) @bindings.each_key(&block) end
Enumerates over every variable value in this solution.
@yield [value] @yieldparam [RDF::Term] value @return [Enumerator]
# File lib/rdf/query/solution.rb, line 81 def each_value(&block) @bindings.each_value(&block) end
Enumerates over every variable in this solution.
@yield [variable] @yieldparam [Variable] @return [Enumerator]
# File lib/rdf/query/solution.rb, line 103 def each_variable @bindings.each do |name, value| yield Variable.new(name, value) end end
Equivalence of solution
# File lib/rdf/query/solution.rb, line 247 def eql?(other) other.is_a?(Solution) && @bindings.eql?(other.bindings) 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
# File lib/rdf/query/solution.rb, line 93 def has_variables?(variables) variables.any? { |variable| bound?(variable) } end
Integer hash of this solution @return [Integer]
# File lib/rdf/query/solution.rb, line 241 def hash @bindings.hash end
@return [String]
# File lib/rdf/query/solution.rb, line 260 def inspect sprintf("#<%s:%#0x(%s)>", self.class.name, __id__, @bindings.inspect) 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_hash] other
another query solution or hash bindings
@return [Boolean]
# File lib/rdf/query/solution.rb, line 220 def isomorphic_with?(other) @bindings.all? do |k, v| !other.to_hash.has_key?(k) || other[k].eql?(v) end end
Merges the bindings from the given `other` query solution with a copy of this one.
@param [RDF::Query::Solution, to_hash] other
another query solution or hash bindings
@return [RDF::Query::Solution] @since 0.3.0
# File lib/rdf/query/solution.rb, line 172 def merge(other) self.class.new(@bindings.dup).merge!(other) 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_hash] other
another query solution or hash bindings
@return [void] self @since 0.3.0
# File lib/rdf/query/solution.rb, line 159 def merge!(other) @bindings.merge!(other.to_hash) self end
@return [Array<Array(Symbol, RDF::Term)>}
# File lib/rdf/query/solution.rb, line 228 def to_a @bindings.to_a end
@return [Hash{Symbol => RDF::Term}}
# File lib/rdf/query/solution.rb, line 234 def to_hash @bindings.dup 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`
# File lib/rdf/query/solution.rb, line 125 def unbound?(name) @bindings[name.to_sym].nil? end
Protected Instance Methods
@param [Symbol] name @return [RDF::Term]
# File lib/rdf/query/solution.rb, line 269 def method_missing(name, *args, &block) if args.empty? && @bindings.has_key?(name.to_sym) @bindings[name.to_sym] else super # raises NoMethodError end end
@return [Boolean]
# File lib/rdf/query/solution.rb, line 279 def respond_to_missing?(name, include_private = false) @bindings.has_key?(name.to_sym) || super end