Module: RDF::Oxigraph::SPARQL

Defined in:
lib/rdf/oxigraph/sparql.rb

Defined Under Namespace

Classes: Client

Class Method Summary collapse

Class Method Details

.results(tagged) ⇒ Object

Convert a native Store#query tagged result into RDF.rb-native objects:

["boolean", bool]                  -> true / false
["solutions", vars, rows]          -> RDF::Query::Solutions
["graph", triples]                 -> RDF::Graph


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rdf/oxigraph/sparql.rb', line 10

def results(tagged)
  case tagged[0]
  when "boolean"
    tagged[1]
  when "solutions"
    _, vars, rows = tagged
    solutions = RDF::Query::Solutions.new
    rows.each do |row|
      bindings = {}
      vars.each_with_index do |var, i|
        term = Conversion.deserialize(row[i])
        bindings[var.to_sym] = term unless term.nil?
      end
      solutions << RDF::Query::Solution.new(bindings)
    end
    solutions
  when "graph"
    graph = RDF::Graph.new
    tagged[1].each do |triple|
      graph << RDF::Statement.new(
        Conversion.deserialize(triple[0]),
        Conversion.deserialize(triple[1]),
        Conversion.deserialize(triple[2])
      )
    end
    graph
  else
    raise ArgumentError, "unknown query result kind: #{tagged[0].inspect}"
  end
end