class RDF::FourStore::Repository

RDF::Repository backend for 4store

@see 4store.org @see

Constants

DEFAULT_CONTEXT

Attributes

dataURI[R]
endpointURI[R]
sizeURI[R]
statusURI[R]
updateURI[R]

Public Class Methods

new(uri_or_options = {}) click to toggle source

Constructor of RDF::FourStore::Repository

@param [String] uri @param [Hash] options @return [RDF::FourStore::Repository] @example

RDF::FourStore::Respository.new('http://localhost:8080')
Calls superclass method
# 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

Public Instance Methods

clear_statements() click to toggle source

@private @see RDF::Mutable#clear

# 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
count() click to toggle source

Returns the number of statements in this repository. @see RDF::Repository#count @return [Integer]

# 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
Also aliased as: size, length
delete_statement(statement) click to toggle source

@see RDF::Mutable#delete_statement @private

# 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) click to toggle source

Makes a RDF string from a RDF Statement

@param [RDF::Statement] statement @return [String]

# File lib/rdf/four_store/repository.rb, line 241
def dump_statement(statement)
  dump_statements([statement])
end
dump_statements(statements) click to toggle source

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

@param [Array(RDF::Statement)] statements @return [String] @see 4store.org/presentations/bbc-2009-09-21/slides.html#(38)

# 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?() click to toggle source

@private @see RDF::Durable#durable? @return [Boolean]

# File lib/rdf/four_store/repository.rb, line 311
def durable?
  true
end
each(&block) click to toggle source

Enumerates each RDF statement in this repository.

@yield [statement] @yieldparam [RDF::Statement] statement @return [Enumerator] @see RDF::Repository#each @see SPARQL::Client::Rpository#each

# 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?() click to toggle source

@private @see RDF::Countable#empty? @return [Boolean]

# File lib/rdf/four_store/repository.rb, line 319
def empty?
  count.zero?
end
has_quad?(quad) click to toggle source

@private @see RDF::Enumerable#has_quad?

# 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) click to toggle source

@private @see RDF::Enumerable#has_statement?

# 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) click to toggle source

@private @see RDF::Enumerable#has_triple?

# File lib/rdf/four_store/repository.rb, line 121
def has_triple?(triple)
  has_statement?(RDF::Statement.from(triple))
end
insert_statement(statement) click to toggle source

@see RDF::Mutable#insert_statement @private

# 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
length()
Alias for: count
load(filename, options = {}) click to toggle source

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

@see RDF::Mutable#load @param [String, to_s] filename @param [Hash{Symbol => Object}] options @return [void]

Calls superclass method
# 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
Also aliased as: load!
load!(filename, options = {})
Alias for: load
post_data(content, context = nil) click to toggle source

Uploads a RDF string to a repository @param [String] content @param [String] context

# 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) click to toggle source

Sends a SPARUL query to update content in a repository @param [String] query @param [String] context

# 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) click to toggle source

Queries ‘self` for RDF statements matching the given `pattern`.

@param [Query, Statement, Array(Value), Hash] pattern @yield [statement] @yieldparam [Statement] @return [Enumerable<Statement>]

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
# 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
size()
Alias for: count
writable?() click to toggle source

@private @see RDF::Writable#writable? @return [Boolean]

# File lib/rdf/four_store/repository.rb, line 303
def writable?
  true
end