Module: SPARQL::Results

Included in:
RDF::Query::Solutions
Defined in:
lib/sparql/results.rb

Overview

Generate SPARQL Results as Boolean, XML or JSON

This module is a mixin for RDF::Query::Solutions

Constant Summary collapse

MIME_TYPES =
{
  json: 'application/sparql-results+json',
  html: 'text/html',
  :xml  => 'application/sparql-results+xml',
  csv: 'text/csv',
  tsv: 'text/tab-separated-values'
}

Instance Method Summary collapse

Instance Method Details

#to_csv(bnode_map: {}) ⇒ String

Generate Solutions as CSV



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/sparql/results.rb', line 143

def to_csv(bnode_map: {})
  require 'csv' unless defined?(::CSV)
  bnode_gen = "_:a"
  CSV.generate(row_sep: "\r\n") do |csv|
    csv << variable_names.to_a
    self.each do |solution|
      csv << variable_names.map do |n|
        case term = solution[n]
        when RDF::Node then bnode_map[term] ||=
          begin
            this = bnode_gen
            bnode_gen = bnode_gen.succ
            this
          end
        when RDF::Statement
          RDF::Query::Solutions(
            RDF::Query::Solution.new(subject: term.subject, predicate: term.predicate, object: term.object)
          ).to_csv(bnode_map: bnode_map).split.last.strip
        else
          solution[n].to_s.strip
        end
      end
    end
  end
end

#to_htmlString

Generate Solutions as HTML

Returns:

  • (String)

See Also:



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/sparql/results.rb', line 117

def to_html
  require 'builder' unless defined?(::Builder)
  
  xml = ::Builder::XmlMarkup.new(indent: 2)
  xml.table(class: "sparql") do
    xml.tbody do
      xml.tr do
        variable_names.each do |n|
          xml.th(n.to_s)
        end
      end
      self.each do |solution|
        xml.tr do
          variable_names.each do |n|
            xml.td(RDF::NTriples.serialize(solution[n]))
          end
        end
      end
    end
  end
end

#to_jsonString

Generate Solutions as JSON

Returns:

  • (String)

See Also:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/sparql/results.rb', line 18

def to_json
  require 'json' unless defined?(::JSON)

  format = ->(value) do
    case value
    when RDF::URI then {type: "uri", value: value.to_s }
    when RDF::Node then {type: "bnode", value: value.id }
    when RDF::Literal
      if value.datatype?
        {type: "typed-literal", datatype: value.datatype.to_s, value: value.to_s }
      elsif value.language?
        {type: "literal", "xml:lang" => value.language.to_s, value: value.to_s }
      else
        {type: "literal", value: value.to_s }
      end
    when RDF::Statement
      {
        type: 'triple',
        value: {
          subject: format.call(value.subject),
          predicate: format.call(value.predicate),
          object: format.call(value.object)
        }
      }
    end
  end

  bindings = self.map do |solution|
    variable_names.inject({}) do |memo, n|
      rep = format.call(solution[n])
      rep ? memo.merge(n => rep) : memo
    end
  end

  {
    :head     => { vars: variable_names },
    :results  => { bindings: bindings}
  }.to_json
end

#to_tsvString

Generate Solutions as TSV



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/sparql/results.rb', line 172

def to_tsv
  require 'csv' unless defined?(::CSV)
  results = [
    variable_names.map {|v| "?#{v}"}.join("\t")
  ] + self.map do |solution|
    variable_names.map do |n|
      case term = solution[n]
      when RDF::Literal::Integer, RDF::Literal::Decimal, RDF::Literal::Double
        term.canonicalize.to_s
      when RDF::Statement
        emb_stmt = RDF::Query::Solutions(
          RDF::Query::Solution.new(subject: term.subject, predicate: term.predicate, object: term.object)
        ).to_tsv.split("\n").last.strip
        emb_stmt.gsub(/[\t\n\r]/, "\t" => '\t', "\n" => '\n', "\r" => '\r')
      when nil
        ""
      else
        RDF::NTriples.serialize(term).strip.gsub(/[\t\n\r]/, "\t" => '\t', "\n" => '\n', "\r" => '\r')
      end
    end.join("\t")
  end
  results.join("\n") + "\n"
end

#to_xmlString

Generate Solutions as XML

Returns:

  • (String)

See Also:



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/sparql/results.rb', line 62

def to_xml
  require 'builder' unless defined?(::Builder)
  
  xml = ::Builder::XmlMarkup.new(indent: 2)
  xml.instruct!

  format = ->(s) do
    case s
    when RDF::URI
      xml.uri(s.to_s)
    when RDF::Node
      xml.bnode(s.id)
    when RDF::Literal
      if s.datatype?
        xml.literal(s.to_s, "datatype" => s.datatype.to_s)
      elsif s.language
        xml.literal(s.to_s, "xml:lang" => s.language.to_s)
      else
        xml.literal(s.to_s)
      end
    when RDF::Statement
      xml.triple do
        xml.subject {format.call(s.subject)}
        xml.predicate {format.call(s.predicate)}
        xml.object {format.call(s.object)}
      end
    end
  end

  xml.sparql(xmlns: "http://www.w3.org/2005/sparql-results#") do
    xml.head do
      variable_names.each do |n|
        xml.variable(name: n)
      end
    end
    xml.results do
      self.each do |solution|
        xml.result do
          variable_names.each do |n|
            s = solution[n]
            next unless s
            xml.binding(name: n) do
              format.call(s)
            end
          end
        end
      end
    end
  end
end