Class: IqTriplestorage::VirtuosoAdaptor

Inherits:
BaseAdaptor show all
Defined in:
lib/iq_triplestorage/virtuoso_adaptor.rb

Instance Attribute Summary

Attributes inherited from BaseAdaptor

#host

Instance Method Summary collapse

Methods inherited from BaseAdaptor

#http_request

Constructor Details

#initialize(host, options = {}) ⇒ VirtuosoAdaptor

Returns a new instance of VirtuosoAdaptor.

Raises:

  • (ArgumentError)


7
8
9
10
11
# File 'lib/iq_triplestorage/virtuoso_adaptor.rb', line 7

def initialize(host, options={})
  super
  # validate to avoid nasty errors
  raise(ArgumentError, "username must not be nil") if @username.nil?
end

Instance Method Details

#batch_update(triples_by_graph) ⇒ Object

expects a hash of N-Triples by graph URI



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/iq_triplestorage/virtuoso_adaptor.rb', line 18

def batch_update(triples_by_graph)
  # apparently Virtuoso gets confused when mixing CLEAR and INSERT queries,
  # so we have to do use separate requests

  reset_queries = triples_by_graph.keys.map do |graph_uri|
    "CLEAR GRAPH <#{graph_uri}>" # XXX: duplicates `reset`
  end
  success = sparql_query(reset_queries)
  return false unless success

  insert_queries = triples_by_graph.map do |graph_uri, ntriples|
    "INSERT IN GRAPH <#{graph_uri}> {\n#{ntriples}\n}"
  end
  success = sparql_query(insert_queries)

  return success
end

#reset(uri) ⇒ Object



13
14
15
# File 'lib/iq_triplestorage/virtuoso_adaptor.rb', line 13

def reset(uri)
  return sparql_pull("CLEAR GRAPH <#{uri}>") # XXX: s/CLEAR/DROP/ was rejected (405)
end

#sparql_pull(query) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/iq_triplestorage/virtuoso_adaptor.rb', line 62

def sparql_pull(query)
  path = "/DAV/home/#{@username}/rdf_sink" # XXX: shouldn't this be /sparql?
  res = http_request("POST", path, query, {
    "Content-Type" => "application/sparql-query"
  })
  return res.code == "200" # XXX: always returns 409
end

#sparql_push(uri, rdf_data, content_type) ⇒ Object

Raises:

  • (TypeError)


49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/iq_triplestorage/virtuoso_adaptor.rb', line 49

def sparql_push(uri, rdf_data, content_type)
  raise TypeError, "missing content type" unless content_type

  filename = uri.gsub(/[^0-9A-Za-z]/, "_") # XXX: too simplistic?
  path = "/DAV/home/#{@username}/rdf_sink/#{filename}"

  res = http_request("PUT", path, rdf_data, { # XXX: is PUT correct here?
    "Content-Type" => content_type
  })

  return res.code == "201"
end

#sparql_query(query) ⇒ Object

query is a string or an array of strings



71
72
73
74
75
76
77
78
79
80
# File 'lib/iq_triplestorage/virtuoso_adaptor.rb', line 71

def sparql_query(query)
  query = query.join("\n\n") + "\n" rescue query
  path = "/DAV/home/#{@username}/query"

  res = http_request("POST", path, query, {
    "Content-Type" => "application/sparql-query"
  })

  return res.code == "201"
end

#update(uri, rdf_data = nil, content_type = nil) ⇒ Object

uses push method if ‘rdf_data` is provided, pull otherwise



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/iq_triplestorage/virtuoso_adaptor.rb', line 37

def update(uri, rdf_data=nil, content_type=nil)
  reset(uri)

  if rdf_data
    res = sparql_push(uri, rdf_data.strip, content_type)
  else
    res = sparql_pull(%{LOAD "#{uri}" INTO GRAPH <#{uri}>})
  end

  return res
end