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 => "jrhacker@example.org", ...}

Since:

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, #to_set, #triples

Methods included from Util::Aliasing::LateBound

#alias_method

Methods included from Countable

#count, #empty?

Constructor Details

- (Solution) initialize(bindings = {}) {|solution| ... }

Initializes the query solution.

Parameters:

  • (Hash{Symbol => RDF::Term}) bindings (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

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

Parameters:

  • (Symbol) name

Returns:

Since:

  • 0.3.0



197
198
199
200
201
202
203
# File 'lib/rdf/query/solution.rb', line 197

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

- (RDF::Term) [](name)

Returns the value of the variable name.

Parameters:

  • (Symbol, #to_sym) name

    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

- (RDF::Term) []=(name, value)

Binds or rebinds the variable name to the given value.

Parameters:

  • (Symbol, #to_sym) name

    the variable name

  • (RDF::Term) value

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

- (Boolean) bound?(name)

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

Parameters:

  • (Symbol, #to_sym) name

    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

- (Enumerator) each_binding {|name, value| ... } 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

- (Enumerator) each_name {|name| ... } Also known as: each_key

Enumerates over every variable name in this solution.

Yields:

  • (name)

Yield Parameters:

  • (Symbol) name

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

- (Enumerator) each_value {|value| ... }

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

- (Enumerator) each_variable {|variable| ... }

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

- (Boolean) has_variables?(variables)

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

Parameters:

  • (Array<Symbol, #to_sym>) variables

    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

- (String) inspect

Returns:

  • (String)

Since:

  • 0.3.0



188
189
190
# File 'lib/rdf/query/solution.rb', line 188

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

- (RDF::Query::Solution) merge(other)

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)

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

- (Array<Array(Symbol, RDF::Term)>) to_a

Returns:

Since:

  • 0.3.0



176
177
178
# File 'lib/rdf/query/solution.rb', line 176

def to_a
  @bindings.to_a
end

- (Hash{Symbol => RDF::Term}) to_hash

Returns:

Since:

  • 0.3.0



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

def to_hash
  @bindings.dup
end

- (Boolean) unbound?(name)

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

Parameters:

  • (Symbol, #to_sym) name

    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