Class: SPARQL::Client::Repository

Inherits:
RDF::Repository
  • Object
show all
Defined in:
lib/sparql/client/repository.rb

Overview

A read-only repository view of a SPARQL endpoint.

See Also:

  • `RDF`RDF::Repository`

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Repository.

Parameters:

  • uri (URI, #to_s) (defaults to: nil)

    Endpoint of this repository

  • options (Hash{Symbol => Object})

    passed to RDF::Repository

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
# 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

Instance Attribute Details

#clientSPARQL::Client (readonly)

Returns:



8
9
10
# File 'lib/sparql/client/repository.rb', line 8

def client
  @client
end

Instance Method Details

#clear_statementsObject

See Also:

  • RDF::Mutable#clear


212
213
214
# File 'lib/sparql/client/repository.rb', line 212

def clear_statements
  update_client.clear(:all)
end

#countInteger Also known as: size, length

Returns the number of statements in this repository.

Returns:

  • (Integer)

See Also:

  • RDF::Repository#count?


176
177
178
179
180
181
182
183
184
185
186
# 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:
    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:

    • statements (Array<RDF::Statement>)

    Returns:

    • (self)

    Raises:

    • (TypeError)

      if ‘self` is immutable

  • #delete(statements) ⇒ self

    Parameters:

    • statements (Enumerable<RDF::Statement>)

    Returns:

    • (self)

    Raises:

    • (TypeError)

      if ‘self` is immutable

See Also:

  • RDF::Mutable#delete


233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/sparql/client/repository.rb', line 233

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 {|statement| ... } ⇒ Object

Enumerates each RDF statement in this repository.

Yields:

  • (statement)

Yield Parameters:

  • statement (RDF::Statement)

See Also:

  • RDF::Repository#each


36
37
38
# 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 {|object| ... } ⇒ Enumerator

Iterates over each object in this repository.

Yields:

  • (object)

Yield Parameters:

  • object (RDF::Value)

Returns:

  • (Enumerator)

See Also:

  • RDF::Repository#each_object?


144
145
146
147
148
149
# 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 {|predicate| ... } ⇒ Enumerator

Iterates over each predicate in this repository.

Yields:

  • (predicate)

Yield Parameters:

  • predicate (RDF::URI)

Returns:

  • (Enumerator)

See Also:

  • RDF::Repository#each_predicate?


130
131
132
133
134
135
# 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 {|statement| ... } ⇒ void #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:

  • #each_statement {|statement| ... } ⇒ void

    This method returns an undefined value.

    Yields:

    • (statement)

      each statement

    Yield Parameters:

    • statement (RDF::Statement)

    Yield Returns:

    • (void)

      ignored

  • #each_statementEnumerator<RDF::Statement>

    Returns:

    • (Enumerator<RDF::Statement>)


56
57
58
59
60
61
62
# 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 {|subject| ... } ⇒ Enumerator

Iterates over each subject in this repository.

Yields:

  • (subject)

Yield Parameters:

  • subject (RDF::Resource)

Returns:

  • (Enumerator)

See Also:

  • RDF::Repository#each_subject?


116
117
118
119
120
121
# 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?Boolean

Returns ‘true` if this repository contains no statements.

Returns:

  • (Boolean)

See Also:

  • RDF::Repository#empty?


196
197
198
# File 'lib/sparql/client/repository.rb', line 196

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

#has_object?(object) ⇒ Boolean

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

Parameters:

  • object (RDF::Value)

Returns:

  • (Boolean)

See Also:

  • RDF::Repository#has_object?


105
106
107
# File 'lib/sparql/client/repository.rb', line 105

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:

  • predicate (RDF::URI)

Returns:

  • (Boolean)

See Also:

  • RDF::Repository#has_predicate?


95
96
97
# File 'lib/sparql/client/repository.rb', line 95

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:

  • statement (RDF::Statement)

Returns:

  • (Boolean)

See Also:

  • RDF::Repository#has_statement?


167
168
169
# File 'lib/sparql/client/repository.rb', line 167

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

#has_subject?(subject) ⇒ Boolean

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

Parameters:

  • subject (RDF::Resource)

Returns:

  • (Boolean)

See Also:

  • RDF::Repository#has_subject?


85
86
87
# File 'lib/sparql/client/repository.rb', line 85

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:

  • triple (Array<RDF::Resource, RDF::URI, RDF::Value>)

Returns:

  • (Boolean)

See Also:

  • RDF::Repository#has_triple?


157
158
159
# File 'lib/sparql/client/repository.rb', line 157

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

#supports?(feature) ⇒ Boolean

Returns:

  • (Boolean)

See Also:

  • RDF::Enumerable#supports?


67
68
69
70
71
72
73
74
75
76
77
# 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_clientSPARQL::Client

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

Returns:



27
28
29
# File 'lib/sparql/client/repository.rb', line 27

def update_client
  @update_client || @client
end

#writable?Boolean

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

Returns:

  • (Boolean)

See Also:

  • RDF::Mutable#mutable?


205
206
207
# File 'lib/sparql/client/repository.rb', line 205

def writable?
  true
end