Class: RDF::Query::Solution

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rdf/query/solution.rb

Overview

An RDF query solution.

Examples:

Iterating over every binding in the solution

solution.each_binding  { |name, value| puts value.inspect }
solution.each_variable { |variable| puts variable.value.inspect }

Iterating over every value in the solution

solution.each_value    { |value| puts value.inspect }

Checking whether a variable is bound or unbound

solution.bound?(:title)
solution.unbound?(:mbox)

Retrieving the value of a bound variable

solution[:mbox]
solution.mbox

Retrieving all bindings in the solution as a Hash

solution.to_h       #=> {mbox: "jrhacker@example.org", ...}

Since:

  • 0.3.0

Instance Method Summary collapse

Methods included from Enumerable

#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_names, #has_graph?, #has_object?, #has_predicate?, #has_quad?, #has_statement?, #has_subject?, #has_term?, #has_triple?, #invalid?, #objects, #predicates, #project_graph, #quads, #statements, #subjects, #supports?, #terms, #to_set, #triples, #valid?, #validate!

Methods included from Util::Aliasing::LateBound

#alias_method

Methods included from Countable

#count, #empty?

Constructor Details

#initialize(bindings = {}) {|solution| ... } ⇒ Solution

Initializes the query solution.

Parameters:

  • bindings (Hash{Symbol => RDF::Term}) (defaults to: {})

Yields:

  • (solution)

Since:

  • 0.3.0



38
39
40
41
42
43
44
45
46
47
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#Object (protected) #binding(name) ⇒ RDF::Term (protected)

Overloads:

  • #Object
    Deprecated.

    Use #to_h instead.

    Returns object representation of this URI, broken into components

  • #binding(name) ⇒ RDF::Term

    Return the binding for this name

    Parameters:

    • name (Symbol)

    Returns:

Since:

  • 0.3.0



278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/rdf/query/solution.rb', line 278

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

Instance Method Details

#[](name) ⇒ RDF::Term

Returns the value of the variable name.

Parameters:

  • name (Symbol, #to_sym)

    the variable name

Returns:

Since:

  • 0.3.0



135
136
137
# File 'lib/rdf/query/solution.rb', line 135

def [](name)
  @bindings[name.to_sym]
end

#[]=(name, value) ⇒ RDF::Term

Binds or rebinds the variable name to the given value.

Parameters:

  • name (Symbol, #to_sym)

    the variable name

  • value (RDF::Term)

Returns:

Since:

  • 0.3.0



147
148
149
# 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

#bound?(name) ⇒ Boolean

Returns true if the variable name is bound in this solution.

Parameters:

  • name (Symbol, #to_sym)

    the variable name

Returns:

  • (Boolean)

    true or false

Since:

  • 0.3.0



115
116
117
# File 'lib/rdf/query/solution.rb', line 115

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).

Parameters:

Returns:

  • (Boolean)

See Also:

Since:

  • 0.3.0



192
193
194
195
196
# File 'lib/rdf/query/solution.rb', line 192

def compatible?(other)
  @bindings.all? do |k, v|
    !other.to_h.has_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.

Parameters:

Returns:

  • (Boolean)

See Also:

Since:

  • 0.3.0



206
207
208
209
210
# File 'lib/rdf/query/solution.rb', line 206

def disjoint?(other)
  @bindings.none? do |k, v|
    v && other.to_h.has_key?(k) && other[k].eql?(v)
  end
end

#dupRDF::Statement

Duplicate solution, preserving patterns

Returns:

Since:

  • 0.3.0



179
180
181
# File 'lib/rdf/query/solution.rb', line 179

def dup
  merge({})
end

#each_binding {|name, value| ... } ⇒ Enumerator Also known as: each

Enumerates over every variable binding in this solution.

Yields:

  • (name, value)

Yield Parameters:

Returns:

Since:

  • 0.3.0



59
60
61
# File 'lib/rdf/query/solution.rb', line 59

def each_binding(&block)
  @bindings.each(&block)
end

#each_name {|name| ... } ⇒ Enumerator Also known as: each_key

Enumerates over every variable name in this solution.

Yields:

  • (name)

Yield Parameters:

  • name (Symbol)

Returns:

Since:

  • 0.3.0



70
71
72
# File 'lib/rdf/query/solution.rb', line 70

def each_name(&block)
  @bindings.each_key(&block)
end

#each_value {|value| ... } ⇒ Enumerator

Enumerates over every variable value in this solution.

Yields:

  • (value)

Yield Parameters:

Returns:

Since:

  • 0.3.0



81
82
83
# File 'lib/rdf/query/solution.rb', line 81

def each_value(&block)
  @bindings.each_value(&block)
end

#each_variable {|variable| ... } ⇒ Enumerator

Enumerates over every variable in this solution.

Yields:

  • (variable)

Yield Parameters:

Returns:

Since:

  • 0.3.0



103
104
105
106
107
# File 'lib/rdf/query/solution.rb', line 103

def each_variable
  @bindings.each do |name, value|
    yield Variable.new(name, value)
  end
end

#eql?(other) ⇒ Boolean Also known as: ==

Equivalence of solution

Returns:

  • (Boolean)

Since:

  • 0.3.0



247
248
249
# File 'lib/rdf/query/solution.rb', line 247

def eql?(other)
  other.is_a?(Solution) && @bindings.eql?(other.bindings)
end

#has_variables?(variables) ⇒ Boolean

Returns true if this solution contains bindings for any of the given variables.

Parameters:

  • variables (Array<Symbol, #to_sym>)

    an array of variables to check

Returns:

  • (Boolean)

    true or false

Since:

  • 0.3.0



93
94
95
# File 'lib/rdf/query/solution.rb', line 93

def has_variables?(variables)
  variables.any? { |variable| bound?(variable) }
end

#hashInteger

Integer hash of this solution

Returns:

  • (Integer)

Since:

  • 0.3.0



241
242
243
# File 'lib/rdf/query/solution.rb', line 241

def hash
  @bindings.hash
end

#inspectString

Returns:

  • (String)

Since:

  • 0.3.0



260
261
262
# File 'lib/rdf/query/solution.rb', line 260

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).

Parameters:

Returns:

  • (Boolean)

Since:

  • 0.3.0



220
221
222
223
224
# File 'lib/rdf/query/solution.rb', line 220

def isomorphic_with?(other)
  @bindings.all? do |k, v|
    !other.to_h.has_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.

Parameters:

Returns:

Since:

  • 0.3.0



172
173
174
# File 'lib/rdf/query/solution.rb', line 172

def merge(other)
  self.class.new(@bindings.dup).merge!(other)
end

#merge!(other)

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.

Parameters:

Since:

  • 0.3.0



159
160
161
162
# File 'lib/rdf/query/solution.rb', line 159

def merge!(other)
  @bindings.merge!(other.to_h)
  self
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean (protected)

Returns:

  • (Boolean)

Since:

  • 0.3.0



294
295
296
# File 'lib/rdf/query/solution.rb', line 294

def respond_to_missing?(name, include_private = false)
  @bindings.has_key?(name.to_sym) || super
end

#to_aArray<Array(Symbol, RDF::Term)>

Returns:

Since:

  • 0.3.0



228
229
230
# File 'lib/rdf/query/solution.rb', line 228

def to_a
  @bindings.to_a
end

#to_hHash{Symbol => RDF::Term}

Returns:

Since:

  • 0.3.0



234
235
236
# File 'lib/rdf/query/solution.rb', line 234

def to_h
  @bindings.dup
end

#unbound?(name) ⇒ Boolean

Returns true if the variable name is unbound in this solution.

Parameters:

  • name (Symbol, #to_sym)

    the variable name

Returns:

  • (Boolean)

    true or false

Since:

  • 0.3.0



125
126
127
# File 'lib/rdf/query/solution.rb', line 125

def unbound?(name)
  @bindings[name.to_sym].nil?
end