class SPARQL::Client::Repository

A read-only repository view of a SPARQL endpoint.

@see `RDF::Repository`

Attributes

client[R]

@return [SPARQL::Client]

Public Class Methods

new(uri: nil, **options, &block) click to toggle source

@param [URI, to_s] uri

Endpoint of this repository

@param [Hash{Symbol => Object}] options passed to RDF::Repository

Calls superclass method
# File lib/sparql/client/repository.rb, line 14
def initialize(uri: nil, **options, &block)
  raise ArgumentError, "uri is a required parameter" unless uri
  @options = options.merge(uri: uri)
  @update_client = SPARQL::Client.new(options.delete(:update_endpoint), **options) if options[:update_endpoint]
  @client  = SPARQL::Client.new(uri, **options)
  super(**@options, &block)
end

Public Instance Methods

clear_statements() click to toggle source

@private @see RDF::Mutable#clear

# File lib/sparql/client/repository.rb, line 210
def clear_statements
  update_client.clear(:all)
end
count() click to toggle source

Returns the number of statements in this repository.

@return [Integer] @see RDF::Repository#count?

# File lib/sparql/client/repository.rb, line 176
def count
  begin
    binding = client.query("SELECT (COUNT(*) AS ?count) WHERE { ?s ?p ?o }").first.to_h
    binding[:count].value.to_i rescue 0
  rescue SPARQL::Client::MalformedQuery => e
    # SPARQL 1.0 does not include support for aggregate functions:
    each_statement.count
  end
end
Also aliased as: size, length
delete(*statements) click to toggle source

Deletes RDF statements from `self`. If any statement contains an `RDF::Query::Variable`, it is considered to be a pattern, and used to query self to find matching statements to delete.

@overload delete(*statements)

@param  [Array<RDF::Statement>] statements
@raise  [TypeError] if `self` is immutable
@return [self]

@overload delete(statements)

@param  [Enumerable<RDF::Statement>] statements
@raise  [TypeError] if `self` is immutable
@return [self]

@see RDF::Mutable#delete

# File lib/sparql/client/repository.rb, line 231
def delete(*statements)
  statements.map! do |value|
    if value.respond_to?(:each_statement)
      delete_statements(value)
      nil
    else
      value
    end
  end
  statements.compact!
  delete_statements(statements) unless statements.empty?
  return self
end
each(&block) click to toggle source

Enumerates each RDF statement in this repository.

@yield [statement] @yieldparam [RDF::Statement] statement @see RDF::Repository#each

# File lib/sparql/client/repository.rb, line 36
def each(&block)
  client.construct([:s, :p, :o]).where([:s, :p, :o]).each_statement(&block)
end
each_object(&block) click to toggle source

Iterates over each object in this repository.

@yield [object] @yieldparam [RDF::Value] object @return [Enumerator] @see RDF::Repository#each_object?

# File lib/sparql/client/repository.rb, line 144
def each_object(&block)
  if block_given?
    client.select(:o, distinct: true).where([:s, :p, :o]).each_solution { |solution| block.call(solution[:o]) }
  end
  enum_object
end
each_predicate(&block) click to toggle source

Iterates over each predicate in this repository.

@yield [predicate] @yieldparam [RDF::URI] predicate @return [Enumerator] @see RDF::Repository#each_predicate?

# File lib/sparql/client/repository.rb, line 130
def each_predicate(&block)
  if block_given?
    client.select(:p, distinct: true).where([:s, :p, :o]).each_solution { |solution| block.call(solution[:p]) }
  end
  enum_predicate
end
each_statement(&block) click to toggle source

Iterates the given block for each RDF statement.

If no block was given, returns an enumerator.

The order in which statements are yielded is undefined.

@overload each_statement

@yield  [statement]
  each statement
@yieldparam  [RDF::Statement] statement
@yieldreturn [void] ignored
@return [void]

@overload each_statement

@return [Enumerator<RDF::Statement>]
# File lib/sparql/client/repository.rb, line 56
def each_statement(&block)
  if block_given?
    # Invoke {#each} in the containing class:
    each(&block)
  end
  enum_statement
end
each_subject(&block) click to toggle source

Iterates over each subject in this repository.

@yield [subject] @yieldparam [RDF::Resource] subject @return [Enumerator] @see RDF::Repository#each_subject?

# File lib/sparql/client/repository.rb, line 116
def each_subject(&block)
  if block_given?
    client.select(:s, distinct: true).where([:s, :p, :o]).each_solution { |solution| block.call(solution[:s]) }
  end
  enum_subject
end
empty?() click to toggle source

Returns `true` if this repository contains no statements.

@return [Boolean] @see RDF::Repository#empty?

# File lib/sparql/client/repository.rb, line 194
def empty?
  client.ask.whether([:s, :p, :o]).false?
end
has_object?(object) click to toggle source

Returns `true` if this repository contains the given object.

@param [RDF::Value] object @return [Boolean] @see RDF::Repository#has_object?

# File lib/sparql/client/repository.rb, line 105
def has_object?(object)
  client.ask.whether([:s, :p, object]).true?
end
has_predicate?(predicate) click to toggle source

Returns `true` if this repository contains the given predicate.

@param [RDF::URI] predicate @return [Boolean] @see RDF::Repository#has_predicate?

# File lib/sparql/client/repository.rb, line 95
def has_predicate?(predicate)
  client.ask.whether([:s, predicate, :o]).true?
end
has_statement?(statement) click to toggle source

Returns `true` if this repository contains the given `statement`.

@param [RDF::Statement] statement @return [Boolean] @see RDF::Repository#has_statement?

# File lib/sparql/client/repository.rb, line 167
def has_statement?(statement)
  has_triple?(statement.to_triple)
end
has_subject?(subject) click to toggle source

Returns `true` if this repository contains the given subject.

@param [RDF::Resource] subject @return [Boolean] @see RDF::Repository#has_subject?

# File lib/sparql/client/repository.rb, line 85
def has_subject?(subject)
  client.ask.whether([subject, :p, :o]).true?
end
has_triple?(triple) click to toggle source

Returns `true` if this repository contains the given `triple`.

@param [Array<RDF::Resource, RDF::URI, RDF::Value>] triple @return [Boolean] @see RDF::Repository#has_triple?

# File lib/sparql/client/repository.rb, line 157
def has_triple?(triple)
  client.ask.whether(triple.to_a[0...3]).true?
end
length()
Alias for: count
size()
Alias for: count
supports?(feature) click to toggle source

@private @see RDF::Enumerable#supports?

# File lib/sparql/client/repository.rb, line 67
def supports?(feature)
  case feature.to_sym
    # statement contexts / named graphs
    when :context     then false
    when :graph_name  then false
    when :inference   then false  # forward-chaining inference
    when :validity    then false
    when :literal_equality then true
    else false
  end
end
update_client() click to toggle source

Returns the client for the update_endpoint if specified, otherwise the {#client}.

@return [SPARQL::Client]

# File lib/sparql/client/repository.rb, line 27
def update_client
  @update_client || @client
end
writable?() click to toggle source

Returns `false` to indicate that this is a read-only repository.

@return [Boolean] @see RDF::Mutable#mutable?

# File lib/sparql/client/repository.rb, line 203
def writable?
  true
end

Protected Instance Methods

delete_statements(statements) click to toggle source

Deletes the given RDF statements from the underlying storage.

Overridden here to use SPARQL/UPDATE

@param [RDF::Enumerable] statements @return [void]

# File lib/sparql/client/repository.rb, line 309
def delete_statements(statements)
  constant = statements.all? do |value|
    # needs to be flattened... urgh
    !value.respond_to?(:each_statement) && begin
      statement = RDF::Statement.from(value)
      statement.constant? && !statement.has_blank_nodes?
    end
  end

  if constant
    update_client.delete_data(statements)
  else
    update_client.delete_insert(statements)
  end
end
insert_statement(statement) click to toggle source

@private @see RDF::Mutable#insert

# File lib/sparql/client/repository.rb, line 342
def insert_statement(statement)
  raise ArgumentError, "Statement #{statement.inspect} is incomplete" if statement.incomplete?
  update_client.insert_data([statement])
end
insert_statements(statements) click to toggle source

Inserts the given RDF statements into the underlying storage or output stream.

Overridden here to use SPARQL/UPDATE

@param [RDF::Enumerable] statements @return [void] @since 0.1.6

# File lib/sparql/client/repository.rb, line 334
def insert_statements(statements)
  raise ArgumentError, "Some statement is incomplete" if statements.any?(&:incomplete?)
  update_client.insert_data(statements)
end
query_execute(query, **options) { |solution| ... } click to toggle source

Queries `self` using the given basic graph pattern (BGP) query, yielding each matched solution to the given block.

Overrides Queryable::query_execute to use SPARQL::Client::query

@param [RDF::Query] query

the query to execute

@param [Hash{Symbol => Object}] options ({})

Any other options passed to `query.execute`

@yield [solution] @yieldparam [RDF::Query::Solution] solution @yieldreturn [void] ignored @return [void] ignored @see RDF::Queryable#query @see RDF::Query#execute

# File lib/sparql/client/repository.rb, line 263
def query_execute(query, **options, &block)
  return nil unless block_given?
  q = SPARQL::Client::Query.
    select(query.variables, **{}).
    where(*query.patterns)
  client.query(q, **options).each do |solution|
    yield solution
  end
end
query_pattern(pattern, **options, &block) click to toggle source

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

@example

repository.query([nil, RDF::DOAP.developer, nil])
repository.query({predicate: RDF::DOAP.developer})

@todo This should use basic SPARQL query mechanism.

@param [Pattern] pattern @see RDF::Queryable#query_pattern @yield [statement] @yieldparam [Statement] @return [Enumerable<Statement>]

# File lib/sparql/client/repository.rb, line 287
def query_pattern(pattern, **options, &block)
  pattern = pattern.dup
  pattern.subject   ||= RDF::Query::Variable.new
  pattern.predicate ||= RDF::Query::Variable.new
  pattern.object    ||= RDF::Query::Variable.new
  pattern.initialize!
  query = client.construct(pattern).where(pattern)

  if block_given?
    query.each_statement(&block)
  else
    query.solutions.to_a.extend(RDF::Enumerable, RDF::Queryable)
  end
end