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: "[email protected]", ...}

Since:

  • 0.3.0

Instance Attribute Summary collapse

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



39
40
41
42
43
44
45
46
47
48
# File 'lib/rdf/query/solution.rb', line 39

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)

Return the binding for this name

Parameters:

  • name (Symbol)

Returns:

Since:

  • 0.3.0



334
335
336
337
338
339
340
# File 'lib/rdf/query/solution.rb', line 334

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



51
52
53
# File 'lib/rdf/query/solution.rb', line 51

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



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

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



190
191
192
# File 'lib/rdf/query/solution.rb', line 190

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



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

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



254
255
256
257
258
# File 'lib/rdf/query/solution.rb', line 254

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



268
269
270
271
272
# File 'lib/rdf/query/solution.rb', line 268

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



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

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



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

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.

Yields:

  • (name)

Yield Parameters:

  • name (Symbol)

Returns:

Since:

  • 0.3.0



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

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.

Yields:

  • (value)

Yield Parameters:

Returns:

Since:

  • 0.3.0



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

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.

Yields:

  • (variable)

Yield Parameters:

Returns:

Since:

  • 0.3.0



134
135
136
137
138
139
140
141
# File 'lib/rdf/query/solution.rb', line 134

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

#enum_bindingEnumerator<RDF::Resource>

Returns an enumerator for #each_binding.

Returns:

See Also:

Since:

  • 0.3.0



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

def enum_binding
  enum_for(:each_binding)
end

#enum_nameEnumerator<RDF::Resource>

Returns an enumerator for #each_name.

Returns:

See Also:

Since:

  • 0.3.0



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

def enum_name
  enum_for(:each_name)
end

#enum_valueEnumerator<RDF::Resource>

Returns an enumerator for #each_value.

Returns:

See Also:

Since:

  • 0.3.0



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

def enum_value
  enum_for(:each_value)
end

#enum_variableEnumerator<RDF::Resource>

Returns an enumerator for #each_variable.

Returns:

See Also:

Since:

  • 0.3.0



148
149
150
# File 'lib/rdf/query/solution.rb', line 148

def enum_variable
  enum_for(:each_variable)
end

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

Equivalence of solution

Returns:

  • (Boolean)

Since:

  • 0.3.0



309
310
311
# File 'lib/rdf/query/solution.rb', line 309

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



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

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

#hashInteger

Integer hash of this solution

Returns:

  • (Integer)

Since:

  • 0.3.0



303
304
305
# File 'lib/rdf/query/solution.rb', line 303

def hash
  @bindings.hash
end

#inspectString

Returns:

Since:

  • 0.3.0



322
323
324
# File 'lib/rdf/query/solution.rb', line 322

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



282
283
284
285
286
# File 'lib/rdf/query/solution.rb', line 282

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



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

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.

## RDFStar (RDF*)

If merging a binding for a statement to a pattern, merge their embedded solutions.

Parameters:

Since:

  • 0.3.0



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/rdf/query/solution.rb', line 207

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
  embedded_solutions = []
  @bindings.each do |k, v|
    if v.is_a?(Pattern) && other[k].is_a?(RDF::Statement)
      embedded_solutions << v.solution(other[k])
    end
  end
  # Merge embedded solutions
  embedded_solutions.each {|soln| merge!(soln)}
  self
end

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

Returns:

Since:

  • 0.3.0



290
291
292
# File 'lib/rdf/query/solution.rb', line 290

def to_a
  @bindings.to_a
end

#to_hHash{Symbol => RDF::Term}

Returns:

Since:

  • 0.3.0



296
297
298
# File 'lib/rdf/query/solution.rb', line 296

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



168
169
170
# File 'lib/rdf/query/solution.rb', line 168

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