Class: RDF::Query::Solution

Inherits:
Object show all
Includes:
Enumerable
Defined in:
vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/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 Attribute Summary

Attributes included from Enumerable

#existentials, #universals

Instance Method Summary collapse

Methods included from Enumerable

add_entailment, #contain?, #dump, #each_graph, #each_object, #each_predicate, #each_quad, #each_statement, #each_subject, #each_term, #each_triple, #entail, #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?

Methods included from Isomorphic

#bijection_to

Constructor Details

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

Initializes the query solution.

Parameters:

Yields:

  • (solution)

Since:

  • 0.3.0



39
40
41
42
43
44
45
46
47
48
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/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:

Returns:

Since:

  • 0.3.0



315
316
317
318
319
320
321
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/query/solution.rb', line 315

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 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 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/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:

Returns:

Since:

  • 0.3.0



190
191
192
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/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 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/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



235
236
237
238
239
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/query/solution.rb', line 235

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



249
250
251
252
253
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/query/solution.rb', line 249

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



222
223
224
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/query/solution.rb', line 222

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 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/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:

Returns:

Since:

  • 0.3.0



81
82
83
84
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/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 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/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 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/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 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/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 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/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 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/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 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/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



290
291
292
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/query/solution.rb', line 290

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 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/query/solution.rb', line 124

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

#hashInteger

Integer hash of this solution

Returns:

Since:

  • 0.3.0



284
285
286
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/query/solution.rb', line 284

def hash
  @bindings.hash
end

#inspectString

Returns:

Since:

  • 0.3.0



303
304
305
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/query/solution.rb', line 303

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



263
264
265
266
267
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/query/solution.rb', line 263

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



215
216
217
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/query/solution.rb', line 215

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



202
203
204
205
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/query/solution.rb', line 202

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



325
326
327
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/query/solution.rb', line 325

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



271
272
273
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/query/solution.rb', line 271

def to_a
  @bindings.to_a
end

#to_hHash{Symbol => RDF::Term}

Returns:

Since:

  • 0.3.0



277
278
279
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/query/solution.rb', line 277

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 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/query/solution.rb', line 168

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