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_hash       #=> {:mbox => "[email protected]", ...}

Since:

  • 0.3.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#contexts, #dump, #each_context, #each_graph, #each_object, #each_predicate, #each_quad, #each_statement, #each_subject, #each_triple, #enum_context, #enum_graph, #enum_object, #enum_predicate, #enum_quad, #enum_statement, #enum_subject, #enum_triple, #has_context?, #has_object?, #has_predicate?, #has_quad?, #has_statement?, #has_subject?, #has_triple?, #objects, #predicates, #quads, #statements, #subjects, #supports?, #to_set, #triples

Methods included from Util::Aliasing::LateBound

#alias_method

Methods included from Countable

#count, #empty?, #enum_for

Constructor Details

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

Initializes the query solution.

Parameters:

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

Yields:

  • (solution)

Since:

  • 0.3.0



36
37
38
39
40
41
42
43
44
45
# File 'lib/rdf/query/solution.rb', line 36

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

#method_missing(name, *args, &block) ⇒ RDF::Term (protected)

Parameters:

  • name (Symbol)

Returns:

Since:

  • 0.3.0



244
245
246
247
248
249
250
# File 'lib/rdf/query/solution.rb', line 244

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

Instance Attribute Details

#bindingsObject (readonly)

Since:

  • 0.3.0



48
49
50
# File 'lib/rdf/query/solution.rb', line 48

def bindings
  @bindings
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



133
134
135
# File 'lib/rdf/query/solution.rb', line 133

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



145
146
147
# File 'lib/rdf/query/solution.rb', line 145

def []=(name, value)
  @bindings[name.to_sym] = 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



113
114
115
# File 'lib/rdf/query/solution.rb', line 113

def bound?(name)
  !unbound?(name)
end

#compatible?(other) ⇒ Boolean

Compatible Mappings Two solution mappings μ1 and μ2 are compatible if, for every variable v in dom(μ1) and in dom(μ2), μ1(v) = μ2(v).

Parameters:

Returns:

  • (Boolean)

Since:

  • 0.3.0



181
182
183
184
185
# File 'lib/rdf/query/solution.rb', line 181

def compatible?(other)
  @bindings.all? do |k, v|
    !other.to_hash.has_key?(k) || other[k].eql?(v)
  end
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



57
58
59
# File 'lib/rdf/query/solution.rb', line 57

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



68
69
70
# File 'lib/rdf/query/solution.rb', line 68

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



79
80
81
# File 'lib/rdf/query/solution.rb', line 79

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



101
102
103
104
105
# File 'lib/rdf/query/solution.rb', line 101

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

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

Equivalence of solution

Returns:

  • (Boolean)

Since:

  • 0.3.0



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

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



91
92
93
# File 'lib/rdf/query/solution.rb', line 91

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

#hashInteger

Integer hash of this solution

Returns:

  • (Integer)

Since:

  • 0.3.0



216
217
218
# File 'lib/rdf/query/solution.rb', line 216

def hash
  @bindings.hash
end

#inspectString

Returns:

  • (String)

Since:

  • 0.3.0



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

def inspect
  sprintf("#<%s:%#0x(%s)>", self.class.name, __id__, @bindings.inspect)
end

#isomorphic_with?(other) ⇒ Boolean

Isomorphic Mappings Two solution mappings μ1 and μ2 are isomorphic if, for every variable v in dom(μ1) and in dom(μ2), μ1(v) = μ2(v).

Parameters:

Returns:

  • (Boolean)

Since:

  • 0.3.0



195
196
197
198
199
# File 'lib/rdf/query/solution.rb', line 195

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



170
171
172
# File 'lib/rdf/query/solution.rb', line 170

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.

Parameters:

Since:

  • 0.3.0



157
158
159
160
# File 'lib/rdf/query/solution.rb', line 157

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

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

Returns:

Since:

  • 0.3.0



203
204
205
# File 'lib/rdf/query/solution.rb', line 203

def to_a
  @bindings.to_a
end

#to_hashHash{Symbol => RDF::Term}

Returns:

Since:

  • 0.3.0



209
210
211
# File 'lib/rdf/query/solution.rb', line 209

def to_hash
  @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



123
124
125
# File 'lib/rdf/query/solution.rb', line 123

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