Class: SPARQL::Client::Repository

Inherits:
RDF::Repository show all
Defined in:
vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/repository.rb

Overview

A read-only repository view of a SPARQL endpoint.

See Also:

  • `RDF`RDF::Repository`

Constant Summary

Constants inherited from RDF::Repository

RDF::Repository::DEFAULT_TX_CLASS

Constants inherited from RDF::Dataset

RDF::Dataset::DEFAULT_GRAPH, RDF::Dataset::ISOLATION_LEVELS

Instance Attribute Summary collapse

Attributes inherited from RDF::Repository

#options, #title, #uri

Instance Method Summary collapse

Methods inherited from RDF::Repository

#delete_insert, #isolation_level, load, #snapshot

Methods included from RDF::Transactable

#begin_transaction, #commit_transaction, #rollback_transaction, #transaction

Methods included from RDF::Mutable

#<<, add_entailment, #apply_changeset, #clear, #delete_insert, #delete_statement, #entail, #entail!, #immutable?, #insert, #load, #method_missing, #mutable?, #respond_to_missing?, #snapshot, #update

Methods included from RDF::Util::Aliasing::LateBound

#alias_method

Methods included from RDF::Writable

#<<, #insert, #insert_graph, #insert_reader

Methods included from RDF::Readable

#readable?

Methods inherited from RDF::Dataset

#inspect, #inspect!, #isolation_level

Methods included from RDF::Queryable

#concise_bounded_description, #first, #first_literal, #first_object, #first_predicate, #first_subject, #first_value, #lint, #query, #query_without_sparql

Methods included from RDF::Durable

#durable?, #nondurable?

Methods included from RDF::Enumerable

add_entailment, #dump, #each_graph, #each_quad, #each_term, #each_triple, #entail, #enum_graph, #enum_object, #enum_predicate, #enum_quad, #enum_statement, #enum_subject, #enum_term, #enum_triple, #graph_names, #has_graph?, #has_quad?, #has_term?, #invalid?, #method_missing, #objects, #predicates, #project_graph, #quads, #respond_to_missing?, #statements, #subjects, #terms, #to_a, #to_h, #to_set, #triples, #valid?, #validate!

Methods included from RDF::Isomorphic

#bijection_to, #isomorphic_with?

Constructor Details

#initialize(uri: nil, **options, &block) ⇒ Repository

Returns a new instance of Repository

Parameters:

  • uri (URI, #to_s)

    Endpoint of this repository

  • title (String, #to_s)

    (nil)

  • options (Hash{Symbol => Object})

    passed to RDF::Repository

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/repository.rb', line 15

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RDF::Mutable

Instance Attribute Details

#clientSPARQL::Client (readonly)

Returns:



8
9
10
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/repository.rb', line 8

def client
  @client
end

Instance Method Details

#countInteger Also known as: size, length

Returns the number of statements in this repository.

Returns:

See Also:

  • RDF::Repository#count?


177
178
179
180
181
182
183
184
185
186
187
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/repository.rb', line 177

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:
    count = 0
    each_statement { count += 1 } # TODO: optimize this
    count
  end
end

#delete(*statements) ⇒ self #delete(statements) ⇒ self

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.

Overloads:

  • #delete(*statements) ⇒ self

    Parameters:

    Returns:

    • (self)

    Raises:

    • (TypeError)

      if self is immutable

  • #delete(statements) ⇒ self

    Parameters:

    Returns:

    • (self)

    Raises:

    • (TypeError)

      if self is immutable

See Also:



234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/repository.rb', line 234

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

#delete_statements(statements) (protected)

This method returns an undefined value.

Deletes the given RDF statements from the underlying storage.

Overridden here to use SPARQL/UPDATE

Parameters:



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/repository.rb', line 310

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

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

Enumerates each RDF statement in this repository.

Yields:

  • (statement)

Yield Parameters:

See Also:

  • RDF::Repository#each


37
38
39
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/repository.rb', line 37

def each(&block)
  client.construct([:s, :p, :o]).where([:s, :p, :o]).each_statement(&block)
end

#each_object {|object| ... } ⇒ Enumerator

Iterates over each object in this repository.

Yields:

  • (object)

Yield Parameters:

Returns:

See Also:

  • RDF::Repository#each_object?


145
146
147
148
149
150
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/repository.rb', line 145

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 {|predicate| ... } ⇒ Enumerator

Iterates over each predicate in this repository.

Yields:

  • (predicate)

Yield Parameters:

Returns:

See Also:

  • RDF::Repository#each_predicate?


131
132
133
134
135
136
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/repository.rb', line 131

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 {|statement| ... } #each_statementEnumerator<RDF::Statement>

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.

Overloads:



57
58
59
60
61
62
63
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/repository.rb', line 57

def each_statement(&block)
  if block_given?
    # Invoke {#each} in the containing class:
    each(&block)
  end
  enum_statement
end

#each_subject {|subject| ... } ⇒ Enumerator

Iterates over each subject in this repository.

Yields:

  • (subject)

Yield Parameters:

Returns:

See Also:

  • RDF::Repository#each_subject?


117
118
119
120
121
122
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/repository.rb', line 117

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?Boolean

Returns true if this repository contains no statements.

Returns:

  • (Boolean)

See Also:

  • RDF::Repository#empty?


197
198
199
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/repository.rb', line 197

def empty?
  client.ask.whether([:s, :p, :o]).false?
end

#has_object?(object) ⇒ Boolean

Returns true if this repository contains the given object.

Parameters:

Returns:

  • (Boolean)

See Also:

  • RDF::Repository#has_object?


106
107
108
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/repository.rb', line 106

def has_object?(object)
  client.ask.whether([:s, :p, object]).true?
end

#has_predicate?(predicate) ⇒ Boolean

Returns true if this repository contains the given predicate.

Parameters:

Returns:

  • (Boolean)

See Also:

  • RDF::Repository#has_predicate?


96
97
98
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/repository.rb', line 96

def has_predicate?(predicate)
  client.ask.whether([:s, predicate, :o]).true?
end

#has_statement?(statement) ⇒ Boolean

Returns true if this repository contains the given statement.

Parameters:

Returns:

  • (Boolean)

See Also:

  • RDF::Repository#has_statement?


168
169
170
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/repository.rb', line 168

def has_statement?(statement)
  has_triple?(statement.to_triple)
end

#has_subject?(subject) ⇒ Boolean

Returns true if this repository contains the given subject.

Parameters:

Returns:

  • (Boolean)

See Also:

  • RDF::Repository#has_subject?


86
87
88
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/repository.rb', line 86

def has_subject?(subject)
  client.ask.whether([subject, :p, :o]).true?
end

#has_triple?(triple) ⇒ Boolean

Returns true if this repository contains the given triple.

Parameters:

Returns:

  • (Boolean)

See Also:

  • RDF::Repository#has_triple?


158
159
160
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/repository.rb', line 158

def has_triple?(triple)
  client.ask.whether(triple.to_a[0...3]).true?
end

#insert_statements(statements) (protected)

This method returns an undefined value.

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

Overridden here to use SPARQL/UPDATE

Parameters:

Raises:

  • (ArgumentError)

Since:

  • 0.1.6



335
336
337
338
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/repository.rb', line 335

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| ... } (protected)

This method returns an undefined value.

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

Parameters:

  • query (RDF::Query)

    the query to execute

  • options (Hash{Symbol => Object}) (defaults to: {})

    ({}) Any other options passed to query.execute

Yields:

  • (solution)

Yield Parameters:

Yield Returns:

  • (void)

    ignored

See Also:



266
267
268
269
270
271
272
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/repository.rb', line 266

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 = {}) {|statement| ... } ⇒ Enumerable<Statement> (protected)

TODO:

This should use basic SPARQL query mechanism.

Queries self for RDF statements matching the given pattern.

Examples:

repository.query([nil, RDF::DOAP.developer, nil])
repository.query(:predicate => RDF::DOAP.developer)

Parameters:

  • pattern (Pattern)

Yields:

  • (statement)

Yield Parameters:

  • (Statement)

Returns:

  • (Enumerable<Statement>)

See Also:



288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/repository.rb', line 288

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

#update_clientSPARQL::Client

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

Returns:



28
29
30
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/repository.rb', line 28

def update_client
  @update_client || @client
end

#writable?Boolean

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

Returns:

  • (Boolean)

See Also:



206
207
208
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/repository.rb', line 206

def writable?
  true
end