Class: RDF::FourStore::Repository
- Inherits:
-
SPARQL::Client::Repository
- Object
- SPARQL::Client::Repository
- RDF::FourStore::Repository
- Defined in:
- lib/rdf/four_store/repository.rb
Overview
RDF::Repository backend for 4store
Constant Summary collapse
- DEFAULT_CONTEXT =
"default:".freeze
Instance Attribute Summary collapse
-
#dataURI ⇒ Object
readonly
Returns the value of attribute dataURI.
-
#endpointURI ⇒ Object
readonly
Returns the value of attribute endpointURI.
-
#sizeURI ⇒ Object
readonly
Returns the value of attribute sizeURI.
-
#statusURI ⇒ Object
readonly
Returns the value of attribute statusURI.
-
#updateURI ⇒ Object
readonly
Returns the value of attribute updateURI.
Instance Method Summary collapse
- #clear_statements ⇒ Object
-
#count ⇒ Integer
(also: #size, #length)
Returns the number of statements in this repository.
- #delete_statement(statement) ⇒ Object
-
#dump_statement(statement) ⇒ String
Makes a RDF string from a RDF Statement.
-
#dump_statements(statements) ⇒ String
Makes a RDF string from RDF Statements Blank nodes are quoted to be used as constants in queries.
- #durable? ⇒ Boolean
-
#each {|statement| ... } ⇒ Enumerator
Enumerates each RDF statement in this repository.
- #empty? ⇒ Boolean
- #has_quad?(quad) ⇒ Boolean
- #has_statement?(statement) ⇒ Boolean
- #has_triple?(triple) ⇒ Boolean
-
#initialize(uri_or_options = {}) ⇒ RDF::FourStore::Repository
constructor
Constructor of RDF::FourStore::Repository.
- #insert_statement(statement) ⇒ Object
-
#load(filename, options = {}) ⇒ void
(also: #load!)
Loads RDF statements from the given file or URL into ‘self`.
-
#post_data(content, context = nil) ⇒ Object
Uploads a RDF string to a repository.
-
#post_update(query, context = nil) ⇒ Object
Sends a SPARUL query to update content in a repository.
-
#query_pattern(pattern, &block) ⇒ Object
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 || :s, :predicate => pattern || :p, :object => pattern || :o, :context => pattern || nil } super(RDF::Query::Pattern.new(h), &block) when Hash pattern ||= :s pattern ||= :p pattern ||= :o super(RDF::Query::Pattern.new(pattern), &block) else super(pattern, &block) end end.
- #writable? ⇒ Boolean
Constructor Details
#initialize(uri_or_options = {}) ⇒ RDF::FourStore::Repository
Constructor of RDF::FourStore::Repository
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/rdf/four_store/repository.rb', line 31 def initialize( = {}) case when String @options = {} @uri = .to_s when Hash @options = .dup @uri = @options.delete([:uri]) else raise ArgumentError, "expected String or Hash, but got #{.inspect}" end @uri.sub!(/\/$/, '') @endpointURI = @uri + "/sparql/" @dataURI = @uri + "/data/" @updateURI = @uri + "/update/" @statusURI = @uri + "/status/" @sizeURI = @statusURI + "size/" super(@endpointURI, ) end |
Instance Attribute Details
#dataURI ⇒ Object (readonly)
Returns the value of attribute dataURI.
18 19 20 |
# File 'lib/rdf/four_store/repository.rb', line 18 def dataURI @dataURI end |
#endpointURI ⇒ Object (readonly)
Returns the value of attribute endpointURI.
18 19 20 |
# File 'lib/rdf/four_store/repository.rb', line 18 def endpointURI @endpointURI end |
#sizeURI ⇒ Object (readonly)
Returns the value of attribute sizeURI.
18 19 20 |
# File 'lib/rdf/four_store/repository.rb', line 18 def sizeURI @sizeURI end |
#statusURI ⇒ Object (readonly)
Returns the value of attribute statusURI.
18 19 20 |
# File 'lib/rdf/four_store/repository.rb', line 18 def statusURI @statusURI end |
#updateURI ⇒ Object (readonly)
Returns the value of attribute updateURI.
18 19 20 |
# File 'lib/rdf/four_store/repository.rb', line 18 def updateURI @updateURI end |
Instance Method Details
#clear_statements ⇒ Object
170 171 172 173 174 175 176 |
# 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 ⇒ Integer Also known as: size, length
Returns the number of statements in this repository.
88 89 90 91 92 93 94 95 96 |
# 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 |
#delete_statement(statement) ⇒ Object
158 159 160 161 162 163 164 165 |
# 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) ⇒ String
Makes a RDF string from a RDF Statement
241 242 243 |
# File 'lib/rdf/four_store/repository.rb', line 241 def dump_statement(statement) dump_statements([statement]) end |
#dump_statements(statements) ⇒ String
Makes a RDF string from RDF Statements Blank nodes are quoted to be used as constants in queries
252 253 254 255 256 257 |
# 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? ⇒ Boolean
311 312 313 |
# File 'lib/rdf/four_store/repository.rb', line 311 def durable? true end |
#each {|statement| ... } ⇒ Enumerator
Enumerates each RDF statement in this repository.
108 109 110 111 112 113 114 115 116 |
# 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? ⇒ Boolean
319 320 321 |
# File 'lib/rdf/four_store/repository.rb', line 319 def empty? count.zero? end |
#has_quad?(quad) ⇒ Boolean
128 129 130 |
# 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) ⇒ Boolean
135 136 137 138 139 140 141 142 143 |
# 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) ⇒ Boolean
121 122 123 |
# File 'lib/rdf/four_store/repository.rb', line 121 def has_triple?(triple) has_statement?(RDF::Statement.from(triple)) end |
#insert_statement(statement) ⇒ Object
148 149 150 151 152 153 |
# 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 |
#load(filename, options = {}) ⇒ void Also known as: load!
This method returns an undefined value.
Loads RDF statements from the given file or URL into ‘self`.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/rdf/four_store/repository.rb', line 59 def load(filename, = {}) return super(filename, ) if /^https?:\/\//.match(filename) uri = nil if [:context] uri = @dataURI + [:context] else uri = @dataURI + 'file://' + File.(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 |
#post_data(content, context = nil) ⇒ Object
Uploads a RDF string to a repository
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# 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) ⇒ Object
Sends a SPARUL query to update content in a repository
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# 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) ⇒ Object
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
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# 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 |
#writable? ⇒ Boolean
303 304 305 |
# File 'lib/rdf/four_store/repository.rb', line 303 def writable? true end |