Class: SPARQL::Client::Repository
- Inherits:
-
RDF::Repository
- Object
- RDF::Repository
- SPARQL::Client::Repository
- Defined in:
- lib/sparql/client/repository.rb
Overview
A read-only repository view of a SPARQL endpoint.
Instance Attribute Summary collapse
- #client ⇒ SPARQL::Client readonly
Instance Method Summary collapse
- #clear_statements ⇒ Object
-
#count ⇒ Integer
(also: #size, #length)
Returns the number of statements in this repository.
-
#delete(*statements) ⇒ Object
Deletes RDF statements from ‘self`.
-
#each {|statement| ... } ⇒ Object
Enumerates each RDF statement in this repository.
-
#each_object {|object| ... } ⇒ Enumerator
Iterates over each object in this repository.
-
#each_predicate {|predicate| ... } ⇒ Enumerator
Iterates over each predicate in this repository.
-
#each_subject {|subject| ... } ⇒ Enumerator
Iterates over each subject in this repository.
-
#empty? ⇒ Boolean
Returns ‘true` if this repository contains no statements.
-
#has_object?(object) ⇒ Boolean
Returns ‘true` if this repository contains the given object.
-
#has_predicate?(predicate) ⇒ Boolean
Returns ‘true` if this repository contains the given predicate.
-
#has_statement?(statement) ⇒ Boolean
Returns ‘true` if this repository contains the given `statement`.
-
#has_subject?(subject) ⇒ Boolean
Returns ‘true` if this repository contains the given subject.
-
#has_triple?(triple) ⇒ Boolean
Returns ‘true` if this repository contains the given `triple`.
-
#initialize(uri: nil, **options, &block) ⇒ Repository
constructor
A new instance of Repository.
- #supports?(feature) ⇒ Boolean
-
#update_client ⇒ SPARQL::Client
Returns the client for the update_endpoint if specified, otherwise the #client.
-
#writable? ⇒ Boolean
Returns ‘false` to indicate that this is a read-only repository.
Constructor Details
#initialize(uri: nil, **options, &block) ⇒ Repository
Returns a new instance of Repository.
15 16 17 18 19 20 21 |
# File 'lib/sparql/client/repository.rb', line 15 def initialize(uri: nil, **, &block) raise ArgumentError, "uri is a required parameter" unless uri @options = .merge(uri: uri) @update_client = SPARQL::Client.new(.delete(:update_endpoint), ) if [:update_endpoint] @client = SPARQL::Client.new(uri, ) super(@options, &block) end |
Instance Attribute Details
#client ⇒ SPARQL::Client (readonly)
8 9 10 |
# File 'lib/sparql/client/repository.rb', line 8 def client @client end |
Instance Method Details
#clear_statements ⇒ Object
188 189 190 |
# File 'lib/sparql/client/repository.rb', line 188 def clear_statements update_client.clear(:all) end |
#count ⇒ Integer Also known as: size, length
Returns the number of statements in this repository.
152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/sparql/client/repository.rb', line 152 def count begin binding = client.query("SELECT (COUNT(*) AS ?count) WHERE { ?s ?p ?o }").first.to_hash 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.
209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/sparql/client/repository.rb', line 209 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.
37 38 39 |
# File '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.
120 121 122 123 124 125 |
# File 'lib/sparql/client/repository.rb', line 120 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.
106 107 108 109 110 111 |
# File 'lib/sparql/client/repository.rb', line 106 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_subject {|subject| ... } ⇒ Enumerator
Iterates over each subject in this repository.
92 93 94 95 96 97 |
# File 'lib/sparql/client/repository.rb', line 92 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.
172 173 174 |
# File 'lib/sparql/client/repository.rb', line 172 def empty? client.ask.whether([:s, :p, :o]).false? end |
#has_object?(object) ⇒ Boolean
Returns ‘true` if this repository contains the given object.
81 82 83 |
# File 'lib/sparql/client/repository.rb', line 81 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.
71 72 73 |
# File 'lib/sparql/client/repository.rb', line 71 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`.
143 144 145 |
# File 'lib/sparql/client/repository.rb', line 143 def has_statement?(statement) has_triple?(statement.to_triple) end |
#has_subject?(subject) ⇒ Boolean
Returns ‘true` if this repository contains the given subject.
61 62 63 |
# File 'lib/sparql/client/repository.rb', line 61 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`.
133 134 135 |
# File 'lib/sparql/client/repository.rb', line 133 def has_triple?(triple) client.ask.whether(triple.to_a[0...3]).true? end |
#supports?(feature) ⇒ Boolean
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/sparql/client/repository.rb', line 44 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 else false end end |
#update_client ⇒ SPARQL::Client
Returns the client for the update_endpoint if specified, otherwise the #client.
28 29 30 |
# File '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.
181 182 183 |
# File 'lib/sparql/client/repository.rb', line 181 def writable? true end |