Class: OWLIM

Inherits:
Object
  • Object
show all
Defined in:
lib/owlim.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ OWLIM

Returns a new instance of OWLIM.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/owlim.rb', line 14

def initialize(url)
  @endpoint = url
  uri = URI.parse(url)

  @host = uri.host
  @port = uri.port
  @path = uri.path

  @prefix_hash = {}

  Net::HTTP.version_1_2
end

Instance Attribute Details

#prefix_hashObject (readonly)

Returns the value of attribute prefix_hash.



12
13
14
# File 'lib/owlim.rb', line 12

def prefix_hash
  @prefix_hash
end

Instance Method Details

#clear(repository) ⇒ Object



119
120
121
122
123
124
# File 'lib/owlim.rb', line 119

def clear(repository)
  Net::HTTP.start(@host, @port) do |http|
    http.read_timeout = 60 * 60
    response = http.delete("#{@path}/repositories/#{repository}/statements")
  end
end

#create(repository, opts = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/owlim.rb', line 45

def create(repository, opts={})
  rdftransaction = create_repository(repository, opts)

  Net::HTTP.start(@host, @port) do |http|
    response = http.post("#{@path}/repositories/SYSTEM/statements",
                         rdftransaction,
                         {"Content-Type" => "application/x-rdftransaction"})
  end
end

#drop(repository) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/owlim.rb', line 126

def drop(repository)
  clear(repository)

  rdftransaction = drop_repository(repository)

  Net::HTTP.start(@host, @port) do |http|
    response = http.post("#{@path}/repositories/SYSTEM/statements",
                         rdftransaction,
                         {"Content-Type" => "application/x-rdftransaction"})
  end
end

#export(repository, opts = {}) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/owlim.rb', line 88

def export(repository, opts={})
  result = ""

  if opts[:format]
    unless format = content_type(opts[:format])
      raise "ERROR: Invalid format (#{opts[:format]})."
    end
  else
    format = "application/rdf+xml"
  end

  Net::HTTP.start(@host, @port) do |http|
    path = "#{@path}/repositories/#{repository}/statements?infer=false"
    response = http.get(path, {"Accept" => format})
    result = response.body
  end

  result
end

#find(repository, keyword, opts = {}, &block) ⇒ Object



183
184
185
186
# File 'lib/owlim.rb', line 183

def find(repository, keyword, opts={}, &block)
  sparql = "select ?s ?p ?o where { ?s ?t '#{keyword}'. ?s ?p ?o . }"
  query(repository, sparql, opts, &block)
end

#head(repository, opts = {}, &block) ⇒ Object



188
189
190
191
192
193
# File 'lib/owlim.rb', line 188

def head(repository, opts={}, &block)
  limit  = opts[:limit] || 20
  offset = (opts[:offset] || 1).to_i + 61
  sparql = "select ?s ?p ?o where { ?s ?p ?o . } offset #{offset} limit #{limit}"
  query(repository, sparql, opts, &block)
end

#hostObject



27
28
29
# File 'lib/owlim.rb', line 27

def host
  return @endpoint
end

#import(repository, data_file, opts = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/owlim.rb', line 55

def import(repository, data_file, opts={})
  http = Net::HTTP.new(@host, @port)
  http.read_timeout = 60 * 60
  req = Net::HTTP::Post.new("#{@path}/repositories/#{repository}/statements")
  if opts[:content_type]
    req["Content-Type"] = opts[:content_type]
  elsif opts[:format]
    if ct = content_type(opts[:format])
      req["Content-Type"] = ct
    else
      raise "ERROR: Content-Type detection failed for #{data_file}."
    end
  else
    if suffix = File.basename(data_file)[/\.(\w+)$/, 1]
      if ct = content_type(suffix.downcase)
        req["Content-Type"] = ct
      else
        raise "ERROR: Content-Type detection failed for '#{data_file}'."
      end
    else
      raise "ERROR: Invalid suffix (#{data_file}). Explicitly specify the RDF format."
    end
  end
  #req["Transfer-Encoding"] = "chunked"
  req["Content-Length"] = File.size(data_file)

  res = nil
  File.open(data_file, "r") do |f|
    req.body_stream = f
    res = http.request(req)
  end
end

#listObject



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/owlim.rb', line 31

def list
  result = ""

  Net::HTTP.start(@host, @port) do |http|
    response = http.get("#{@path}/repositories",
                        {"Accept" => "application/sparql-results+json"})
    result = response.body
  end

  hash = JSON.parse(result)

  hash["results"]["bindings"].map {|b| b["id"]["value"]}
end

#prefixObject



138
139
140
141
142
143
144
# File 'lib/owlim.rb', line 138

def prefix
  ary = []
  @prefix_hash.sort.each { |key, value|
    ary << "PREFIX #{key}: <#{value}>"
  }
  return ary.join("\n")
end

#prefix_defaultObject



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/owlim.rb', line 195

def prefix_default
  @prefix_hash = {
    "rdf"       => "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
    "rdfs"      => "http://www.w3.org/2000/01/rdf-schema#",
    "owl"       => "http://www.w3.org/2002/07/owl#",
    "xsd"       => "http://www.w3.org/2001/XMLSchema#",
    "pext"      => "http://proton.semanticweb.org/protonext#",
    "psys"      => "http://proton.semanticweb.org/protonsys#",
    "xhtml"     => "http://www.w3.org/1999/xhtml#",
    "dc"        => "http://purl.org/dc/elements/1.1/",
    "dcterms"   => "http://purl.org/dc/terms/",
    "foaf"      => "http://xmlns.com/foaf/0.1/",
    "skos"      => "http://www.w3.org/2004/02/skos/core#",
    "void"      => "http://rdfs.org/ns/void#",
    "dbpedia"   => "http://dbpedia.org/resource/",
    "dbp"       => "http://dbpedia.org/property/",
    "dbo"       => "http://dbpedia.org/ontology/",
    "yago"      => "http://dbpedia.org/class/yago/",
    "fb"        => "http://rdf.freebase.com/ns/",
    "sioc"      => "http://rdfs.org/sioc/ns#",
    "geo"       => "http://www.w3.org/2003/01/geo/wgs84_pos#",
    "geonames"  => "http://www.geonames.org/ontology#",
    "bibo"      => "http://purl.org/ontology/bibo/",
    "prism"     => "http://prismstandard.org/namespaces/basic/2.1/",
  }
end

#query(repository, sparql, opts = {}, &block) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/owlim.rb', line 146

def query(repository, sparql, opts={}, &block)
  result = ""

  case opts[:format]
  when "xml"
    format = "application/sparql-results+xml"
  when "json"
    format = "application/sparql-results+json"
  else # tabular text
    format = "application/sparql-results+json"
  end

  sparql_str = CGI.escape(prefix + sparql)
  
  Net::HTTP.start(@host, @port) do |http|
    path = "#{@path}/repositories/#{repository}?query=#{sparql_str}"
    http.get(path, {"Accept" => "#{format}"}) { |body|
      if block and opts[:format] # xml or json
        yield body
      else # tabular text
        result += body
      end
    }
  end

  if opts[:format] # xml or json
    return result
  else # generate tabular text
    table = format_json(result)
    if block
      yield table
    else
      return table
    end
  end
end

#size(repository) ⇒ Object



108
109
110
111
112
113
114
115
116
117
# File 'lib/owlim.rb', line 108

def size(repository)
  result = ""

  Net::HTTP.start(@host, @port) do |http|
    response = http.get("#{@path}/repositories/#{repository}/size")
    result = response.body
  end

  result.to_i
end