Class: Sasquatch::SparqlBuilder

Inherits:
SPARQL::Client::Query
  • Object
show all
Defined in:
lib/sasquatch/sparql_builder.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store, options = {}, &block) ⇒ SparqlBuilder

Returns a new instance of SparqlBuilder.



9
10
11
12
13
# File 'lib/sasquatch/sparql_builder.rb', line 9

def initialize(store, options={}, &block)
  @store = store

  super(:sparql, options, &block)
end

Instance Attribute Details

#storeObject (readonly)

Returns the value of attribute store.



3
4
5
# File 'lib/sasquatch/sparql_builder.rb', line 3

def store
  @store
end

#variablesObject (readonly)

Returns the value of attribute variables.



3
4
5
# File 'lib/sasquatch/sparql_builder.rb', line 3

def variables
  @variables
end

Class Method Details

.init(store, *variables) ⇒ Object



5
6
7
8
# File 'lib/sasquatch/sparql_builder.rb', line 5

def self.init(store, *variables)
  options = variables.last.is_a?(Hash) ? variables.pop : {}
  self.new(store, options).set_variables(*variables)
end

Instance Method Details

#askObject



53
54
55
56
# File 'lib/sasquatch/sparql_builder.rb', line 53

def ask
  @form = :ask
  self
end

#ask!(graph = :default) ⇒ Object



48
49
50
51
# File 'lib/sasquatch/sparql_builder.rb', line 48

def ask!(graph=:default)      
  ask
  execute(graph)
end

#constructObject



63
64
65
66
67
# File 'lib/sasquatch/sparql_builder.rb', line 63

def construct
  @form = :construct
  options[:template] = build_patterns(*@variables)
  self
end

#construct!(graph = :default) ⇒ Object



58
59
60
61
# File 'lib/sasquatch/sparql_builder.rb', line 58

def construct!(graph=:default)
  construct
  execute(graph)
end

#describeObject



29
30
31
32
33
34
35
# File 'lib/sasquatch/sparql_builder.rb', line 29

def describe
  @form = :describe
  @values = *@variables.flatten.map { |var|
    [var, var.is_a?(RDF::URI) ? var : RDF::Query::Variable.new(var)]
  }      
  self
end

#describe!(graph = :default) ⇒ Object



24
25
26
27
# File 'lib/sasquatch/sparql_builder.rb', line 24

def describe!(graph=:default)
  describe
  execute(graph)
end

#execute(graph) ⇒ Object



69
70
71
# File 'lib/sasquatch/sparql_builder.rb', line 69

def execute(graph)
  @store.send(:"sparql_#{@form}", self.to_s, graph)
end

#selectObject



42
43
44
45
46
# File 'lib/sasquatch/sparql_builder.rb', line 42

def select
  @form = :select
  @values = @variables.flatten.map { |var| [var, RDF::Query::Variable.new(var)] }
  self
end

#select!(graph = :default) ⇒ Object



37
38
39
40
# File 'lib/sasquatch/sparql_builder.rb', line 37

def select!(graph=:default)
  select
  execute(graph)
end

#set_variables(*vars) ⇒ Object



20
21
22
23
# File 'lib/sasquatch/sparql_builder.rb', line 20

def set_variables(*vars)
  @variables = vars
  self
end

#sparqlObject



15
16
17
18
# File 'lib/sasquatch/sparql_builder.rb', line 15

def sparql
  @form = "CHANGEME"
  self
end

#to_sString

Returns the string representation of this query.

Returns:

  • (String)


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
112
113
114
115
116
117
118
# File 'lib/sasquatch/sparql_builder.rb', line 77

def to_s
  buffer = [form.to_s.upcase]
  case form
    when :select, :describe
      buffer << 'DISTINCT' if options[:distinct]
      buffer << 'REDUCED' if options[:reduced]
      buffer << (values.empty? ? '*' : values.map { |v| serialize_value(v[1]) }.join(' '))
    when :construct
      buffer << '{'
      buffer += serialize_patterns(options[:template])
      buffer << '}'
  end

  buffer << "FROM #{serialize_value(options[:from])}" if options[:from]

  unless (patterns.empty? && (options[:optionals].nil? || options[:optionals].empty?)) && form == :describe
    buffer << 'WHERE {'
    buffer += serialize_patterns(patterns)
    if options[:optionals]
      options[:optionals].each do |patterns|
        buffer << 'OPTIONAL {'
        buffer += serialize_patterns(patterns)
        buffer << '}'
      end
    end
    if options[:filters]
      buffer += options[:filters].map { |filter| "FILTER(#{filter})" }
    end
    buffer << '}'
  end

  if options[:order_by]
    buffer << 'ORDER BY'
    buffer += options[:order_by].map { |var| var.is_a?(String) ? var : "?#{var}" }
  end

  buffer << "OFFSET #{options[:offset]}" if options[:offset]
  buffer << "LIMIT #{options[:limit]}" if options[:limit]
  options[:prefixes].reverse.each {|e| buffer.unshift("PREFIX #{e}") } if options[:prefixes]

  buffer.join(' ')
end