Class: RDF::FourStore::Repository

Inherits:
SPARQL::Client::Repository
  • Object
show all
Defined in:
lib/rdf/four_store/repository.rb

Overview

RDF::Repository backend for 4store

See Also:

Constant Summary collapse

DEFAULT_CONTEXT =
"default:".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri_or_options = {}) ⇒ RDF::FourStore::Repository

Constructor of RDF::FourStore::Repository

Examples:

RDF::FourStore::Respository.new('http://localhost:8080')

Parameters:

  • uri (String)
  • options (Hash)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rdf/four_store/repository.rb', line 31

def initialize(uri_or_options = {})
  case uri_or_options
  when String
    @options = {}
    @uri = uri_or_options.to_s
  when Hash
    @options = uri_or_options.dup
    @uri = @options.delete([:uri])
  else
    raise ArgumentError, "expected String or Hash, but got #{uri_or_options.inspect}"
  end
  @uri.sub!(/\/$/, '')
  @endpointURI = @uri + "/sparql/"
  @dataURI = @uri + "/data/"
  @updateURI = @uri + "/update/"
  @statusURI = @uri + "/status/"
  @sizeURI = @statusURI + "size/"

  super(@endpointURI, options)
end

Instance Attribute Details

#dataURIObject (readonly)

Returns the value of attribute dataURI.



18
19
20
# File 'lib/rdf/four_store/repository.rb', line 18

def dataURI
  @dataURI
end

#endpointURIObject (readonly)

Returns the value of attribute endpointURI.



18
19
20
# File 'lib/rdf/four_store/repository.rb', line 18

def endpointURI
  @endpointURI
end

#sizeURIObject (readonly)

Returns the value of attribute sizeURI.



18
19
20
# File 'lib/rdf/four_store/repository.rb', line 18

def sizeURI
  @sizeURI
end

#statusURIObject (readonly)

Returns the value of attribute statusURI.



18
19
20
# File 'lib/rdf/four_store/repository.rb', line 18

def statusURI
  @statusURI
end

#updateURIObject (readonly)

Returns the value of attribute updateURI.



18
19
20
# File 'lib/rdf/four_store/repository.rb', line 18

def updateURI
  @updateURI
end

Instance Method Details

#clear_statementsObject

See Also:

  • Mutable#clear


170
171
172
173
174
175
176
# File 'lib/rdf/four_store/repository.rb', line 170

def clear_statements
  q = "SELECT ?g WHERE { GRAPH ?g { ?s ?p ?o . } FILTER (?g != <#{DEFAULT_CONTEXT}>) }"
  @client.query(q).each do |solution|
    post_update("CLEAR GRAPH <#{solution[:g]}>") 
  end
  post_update("CLEAR GRAPH <#{DEFAULT_CONTEXT}>") 
end

#countInteger Also known as: size, length

Returns the number of statements in this repository.

Returns:

  • (Integer)

See Also:

  • Repository#count


88
89
90
91
92
93
94
95
96
# File 'lib/rdf/four_store/repository.rb', line 88

def count
  c = 0
  doc = Nokogiri::HTML(open(@sizeURI))
  doc.search('tr').each do |tr|
    td = tr.search('td')
    c = td[0].content if td[0]
  end
  c.to_i # the last one is the total number
end

#delete_statement(statement) ⇒ Object

See Also:

  • Mutable#delete_statement


158
159
160
161
162
163
164
165
# File 'lib/rdf/four_store/repository.rb', line 158

def delete_statement(statement)
  if has_statement?(statement)
    context = statement.context || DEFAULT_CONTEXT
    dump = dump_statement(statement)
    q = "DELETE DATA { GRAPH <#{context}> { #{dump} } }"
    post_update(q, context)
  end
end

#dump_statement(statement) ⇒ String

Makes a RDF string from a RDF Statement

Parameters:

  • statement (RDF::Statement)

Returns:

  • (String)


241
242
243
# File 'lib/rdf/four_store/repository.rb', line 241

def dump_statement(statement)
  dump_statements([statement])
end

#dump_statements(statements) ⇒ String

Makes a RDF string from RDF Statements Blank nodes are quoted to be used as constants in queries

Parameters:

  • statements (Array(RDF::Statement))

Returns:

  • (String)

See Also:



252
253
254
255
256
257
# File 'lib/rdf/four_store/repository.rb', line 252

def dump_statements(statements)
  graph = RDF::Graph.new
  graph.insert_statements(statements)
  dump = RDF::Writer.for(:ntriples).dump(graph)
  dump.gsub(/(_:\w+?) /, "<#{DEFAULT_CONTEXT}\\1> ")
end

#durable?Boolean

Returns:

  • (Boolean)

See Also:

  • Durable#durable?


311
312
313
# File 'lib/rdf/four_store/repository.rb', line 311

def durable?
  true
end

#each {|statement| ... } ⇒ Enumerator

Enumerates each RDF statement in this repository.

Yields:

  • (statement)

Yield Parameters:

  • statement (RDF::Statement)

Returns:

  • (Enumerator)

See Also:

  • Repository#each
  • SPARQL::Client::Rpository#each


108
109
110
111
112
113
114
115
116
# File 'lib/rdf/four_store/repository.rb', line 108

def each(&block)
  unless block_given?
    RDF::Enumerator.new(self, :each)
  else
    # TODO: check why @client.construct does not work here.
    statements = @client.query("CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o }")
    statements.each_statement(&block) if statements
  end
end

#empty?Boolean

Returns:

  • (Boolean)

See Also:

  • Countable#empty?


319
320
321
# File 'lib/rdf/four_store/repository.rb', line 319

def empty?
  count.zero?
end

#has_quad?(quad) ⇒ Boolean

Returns:

  • (Boolean)

See Also:

  • Enumerable#has_quad?


128
129
130
# File 'lib/rdf/four_store/repository.rb', line 128

def has_quad?(quad)
  has_statement?(RDF::Statement.new(quad[0], quad[1], quad[2], :context => quad[3]))
end

#has_statement?(statement) ⇒ Boolean

Returns:

  • (Boolean)

See Also:

  • Enumerable#has_statement?


135
136
137
138
139
140
141
142
143
# File 'lib/rdf/four_store/repository.rb', line 135

def has_statement?(statement)
  context = statement.context
  dump = dump_statement(statement)
  if context
    @client.query("ASK { GRAPH <#{context}> { #{dump} } } ")
  else
    @client.query("ASK { #{dump} } ")
  end
end

#has_triple?(triple) ⇒ Boolean

Returns:

  • (Boolean)

See Also:

  • Enumerable#has_triple?


121
122
123
# File 'lib/rdf/four_store/repository.rb', line 121

def has_triple?(triple)
  has_statement?(RDF::Statement.from(triple))
end

#insert_statement(statement) ⇒ Object

See Also:

  • Mutable#insert_statement


148
149
150
151
152
153
# File 'lib/rdf/four_store/repository.rb', line 148

def insert_statement(statement)
  unless has_statement?(statement)
    dump = dump_statement(statement)
    post_data(dump, statement.context)
  end
end

#load(filename, options = {}) ⇒ void Also known as: load!

This method returns an undefined value.

Loads RDF statements from the given file or URL into ‘self`.

Parameters:

  • filename (String, #to_s)
  • options (Hash{Symbol => Object}) (defaults to: {})

See Also:

  • Mutable#load


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rdf/four_store/repository.rb', line 59

def load(filename, options = {})
  return super(filename, options) if /^https?:\/\//.match(filename)

  uri = nil

  if options[:context]
    uri = @dataURI + options[:context]
  else
    uri = @dataURI + 'file://' + File.expand_path(filename)
  end

  uri = URI.parse(uri)
  content = open(filename).read
  begin
    req = Net::HTTP::Put.new(uri.path)
    Net::HTTP.start(uri.host, uri.port) do |http|
      http.request(req, content)
    end
  rescue Errno::ECONNREFUSED, Errno::ECONNRESET, TimeoutError
    retry
  end
end

#post_data(content, context = nil) ⇒ Object

Uploads a RDF string to a repository

Parameters:

  • content (String)
  • context (String) (defaults to: nil)


263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/rdf/four_store/repository.rb', line 263

def post_data(content, context = nil)
  context ||= DEFAULT_CONTEXT
  uri = URI.parse(@dataURI)

  req = Net::HTTP::Post.new(uri.path)
  req.form_data = {
    'data' => content,
    'graph' => context,
    'mime-type' => 'application/x-turtle'
  }

  Net::HTTP.start(uri.host, uri.port) do |http|
    http.request(req)
  end
end

#post_update(query, context = nil) ⇒ Object

Sends a SPARUL query to update content in a repository

Parameters:

  • query (String)
  • context (String) (defaults to: nil)


283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/rdf/four_store/repository.rb', line 283

def post_update(query, context = nil)
  context ||= DEFAULT_CONTEXT
  uri = URI.parse(@updateURI)

  req = Net::HTTP::Post.new(uri.path)
  req.form_data = {
    'update' => query,
    'graph' => context,
    'content-type' => 'triples',
  }

  Net::HTTP.start(uri.host, uri.port) do |http|
    http.request(req)
  end
end

#query_pattern(pattern, &block) ⇒ Object

def query(pattern, &block)

  case pattern
  when RDF::Statement
    h = {
      :subject => pattern.subject || :s,
      :predicate => pattern.predicate || :p,
      :object => pattern.object || :o,
      :context => pattern.context || nil
    }
    super(RDF::Query::Pattern.new(h), &block)
  when Array
    h = {
      :subject => pattern[0] || :s,
      :predicate => pattern[1] || :p,
      :object => pattern[2]  || :o,
      :context => pattern[3]  || nil
    }
    super(RDF::Query::Pattern.new(h), &block)
  when Hash
    pattern[:subject] ||= :s
    pattern[:predicate] ||= :p
    pattern[:object] ||= :o
    super(RDF::Query::Pattern.new(pattern), &block)
  else
    super(pattern, &block)
  end
end


213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/rdf/four_store/repository.rb', line 213

def query_pattern(pattern, &block)
  pattern = pattern.dup
  pattern.subject ||= RDF::Query::Variable.new
  pattern.predicate ||= RDF::Query::Variable.new
  pattern.object ||= RDF::Query::Variable.new
  pattern.context ||= nil
  str = pattern.to_s
  q = ""
  if pattern.context
    q = "CONSTRUCT { #{str} } WHERE { GRAPH <#{pattern.context}> { #{str} } } "
  else
    q = "CONSTRUCT { #{str} } WHERE { #{str} } "
  end
  result = @client.query(q)
  if result
    if block_given?
      result.each_statement(&block)
    else
      result.solutions.to_a.extend(RDF::Enumerable, RDF::Queryable)
    end
  end
end

#writable?Boolean

Returns:

  • (Boolean)

See Also:

  • Writable#writable?


303
304
305
# File 'lib/rdf/four_store/repository.rb', line 303

def writable?
  true
end