owlim-ruby 0.9.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +194 -0
- data/bin/owlim +225 -0
- data/gemspec.rb +34 -0
- data/lib/owlim.rb +259 -0
- data/lib/owlim/create_repository.xml.erb +129 -0
- data/lib/owlim/drop_repository.xml.erb +14 -0
- metadata +95 -0
data/README.rdoc
ADDED
@@ -0,0 +1,194 @@
|
|
1
|
+
= OWLIM-RUBY
|
2
|
+
|
3
|
+
This is a Semantic Web program which provides a front-end library and
|
4
|
+
a command line tool for using the OWLIM (http://www.ontotext.com/owlim)
|
5
|
+
triple store with ease. You need a remote or local OWLIM installation.
|
6
|
+
|
7
|
+
== Install
|
8
|
+
|
9
|
+
gem install owlim-ruby
|
10
|
+
|
11
|
+
== Usage as a command
|
12
|
+
|
13
|
+
owlim command [repository [arguments]]
|
14
|
+
|
15
|
+
=== Environmental variable
|
16
|
+
|
17
|
+
Specify your OWLIM server by the environmental variable 'SESAME_URL'.
|
18
|
+
Default is "http://localhost:8080/openrdf-sesame".
|
19
|
+
|
20
|
+
for B shell:
|
21
|
+
|
22
|
+
export SESAME_URL="http://example.org/openrdf-sesame"
|
23
|
+
|
24
|
+
for C shell:
|
25
|
+
|
26
|
+
setenv SESAME_URL "http://example.org/openrdf-sesame"
|
27
|
+
|
28
|
+
=== Info commands
|
29
|
+
|
30
|
+
Show the Sesame OWLIM server URI in use.
|
31
|
+
|
32
|
+
owlim host
|
33
|
+
|
34
|
+
Show list of repositories.
|
35
|
+
|
36
|
+
owlim list
|
37
|
+
|
38
|
+
Show number of triples in a repository.
|
39
|
+
|
40
|
+
owlim size repository
|
41
|
+
|
42
|
+
=== CRUD commands
|
43
|
+
|
44
|
+
Create a new repository.
|
45
|
+
|
46
|
+
owlim create repository
|
47
|
+
|
48
|
+
Import a RDF file to a repository.
|
49
|
+
|
50
|
+
owlim import repository file [format]
|
51
|
+
|
52
|
+
Export contents of a repository in RDF (default is RDF/XML).
|
53
|
+
|
54
|
+
owlim export repository [format]
|
55
|
+
|
56
|
+
Clear the contents of a repository.
|
57
|
+
|
58
|
+
owlim clear repository
|
59
|
+
|
60
|
+
Drop a repository.
|
61
|
+
|
62
|
+
owlim drop repository
|
63
|
+
|
64
|
+
==== Valid formats for import and export
|
65
|
+
|
66
|
+
* RDF/XML: "rdf" or "rdfxml"
|
67
|
+
* Turtle: "ttl" or "turtle"
|
68
|
+
* N3: "n3"
|
69
|
+
* N-Triples: "nt"
|
70
|
+
* TriX: "trix"
|
71
|
+
* TriG: "trig"
|
72
|
+
* Bionary RDF: "rdfbin"
|
73
|
+
|
74
|
+
If not specified, format will be guessed from the file name suffix for import.
|
75
|
+
|
76
|
+
=== Query commands
|
77
|
+
|
78
|
+
SPARQL query against a repository.
|
79
|
+
|
80
|
+
owlim query repository "SPARQL" [format]
|
81
|
+
|
82
|
+
Search for literal objects by a keyword.
|
83
|
+
|
84
|
+
owlim find repository "keyword" [format]
|
85
|
+
|
86
|
+
Result will be printed in a tabular text format by default.
|
87
|
+
|
88
|
+
==== Available alternative formats
|
89
|
+
|
90
|
+
* "json" for "application/sparql-result+json"
|
91
|
+
* "xml" for "application/sparql-result+xml".
|
92
|
+
|
93
|
+
=== Help
|
94
|
+
|
95
|
+
owlim help
|
96
|
+
|
97
|
+
=== Examples
|
98
|
+
|
99
|
+
owlim host
|
100
|
+
owlim list
|
101
|
+
|
102
|
+
owlim create hoge
|
103
|
+
owlim import hoge ../hoge.ttl
|
104
|
+
owlim export hoge > hoge.rdf
|
105
|
+
owlim size hoge
|
106
|
+
owlim clear hoge
|
107
|
+
owlim drop hoge
|
108
|
+
|
109
|
+
owlim query hoge 'select * where { ?s ?p ?o . } limit 1000'
|
110
|
+
owlim query hoge 'select * where { ?s ?p ?o . } limit 1000' json
|
111
|
+
owlim query hoge 'select * where { ?s ?p ?o . } limit 1000' xml
|
112
|
+
|
113
|
+
owlim find hoge "fuga"
|
114
|
+
owlim find hoge "fuga" json
|
115
|
+
owlim find hoge "fuga" xml
|
116
|
+
|
117
|
+
== Usage as a library
|
118
|
+
|
119
|
+
Enable this library.
|
120
|
+
|
121
|
+
require 'rubygems'
|
122
|
+
require 'owlim'
|
123
|
+
|
124
|
+
Create a server object. Argument is a URI of the OWLIM endpoint.
|
125
|
+
|
126
|
+
uri = "http://localhost:8080/openrdf-sesame"
|
127
|
+
owlim = OWLIM.new(uri)
|
128
|
+
|
129
|
+
Show the endpoint URI.
|
130
|
+
|
131
|
+
puts owlim.sesame_url
|
132
|
+
|
133
|
+
Show all repositories.
|
134
|
+
|
135
|
+
puts owlim.list
|
136
|
+
|
137
|
+
Show the size of a given repository.
|
138
|
+
|
139
|
+
puts owlim.size(repository)
|
140
|
+
|
141
|
+
Create new repository.
|
142
|
+
|
143
|
+
owlim.create(repository)
|
144
|
+
|
145
|
+
Import a RDF file into a repository.
|
146
|
+
|
147
|
+
owlim.import(repository, rdf_file)
|
148
|
+
owlim.import(repository, rdf_file, :format => "turtle")
|
149
|
+
|
150
|
+
The file format is automatically guessed by the suffix of a given file name.
|
151
|
+
To specify the format, use :format => format option. Available formats are
|
152
|
+
described in the "Valid formats for import and export" section above.
|
153
|
+
|
154
|
+
Export triples from a repository.
|
155
|
+
|
156
|
+
owlim.export(repository)
|
157
|
+
owlim.export(repository, :format => "turtle")
|
158
|
+
|
159
|
+
Clear a repository.
|
160
|
+
|
161
|
+
owlim.clear(repository)
|
162
|
+
|
163
|
+
Drop a repository.
|
164
|
+
|
165
|
+
owlim.drop(repository)
|
166
|
+
|
167
|
+
SPARQL query to a repository.
|
168
|
+
|
169
|
+
owlim.query(repository, sparql) {|x| print x}
|
170
|
+
|
171
|
+
Use :format => "xml" option to retrieve results in the SPARQL Query
|
172
|
+
Results XML Format (http://www.w3.org/TR/rdf-sparql-XMLres/;
|
173
|
+
"application/sparql-results+xml") and use :format => "json" to retrieve
|
174
|
+
in the JSON (http://www.w3.org/2001/sw/DataAccess/json-sparql/;
|
175
|
+
"application/sparql-results+json") format.
|
176
|
+
|
177
|
+
owlim.query(repository, sparql, :format => "xml") {|x| print x}
|
178
|
+
owlim.query(repository, sparql, :format => "json") {|x| print x}
|
179
|
+
|
180
|
+
Search for a triple which has a keyword as a literal in its object and
|
181
|
+
finds all relevant triples sharing the same subject.
|
182
|
+
|
183
|
+
owlim.find(repository, keyword) {|x| print x}
|
184
|
+
|
185
|
+
== Authors
|
186
|
+
|
187
|
+
* Toshiaki Katayama (DBCLS; ktym@dbcls.jp)
|
188
|
+
* Tatsuya Nishizawa (IMSBIO)
|
189
|
+
|
190
|
+
== License
|
191
|
+
|
192
|
+
This program is free software. You can redistribute it and/or modify it
|
193
|
+
under the same terms as Ruby itself.
|
194
|
+
|
data/bin/owlim
ADDED
@@ -0,0 +1,225 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'owlim'
|
5
|
+
|
6
|
+
def help
|
7
|
+
puts <<HELP
|
8
|
+
|
9
|
+
Usage:
|
10
|
+
|
11
|
+
> owlim command [repository [arguments]]
|
12
|
+
|
13
|
+
Info commands:
|
14
|
+
|
15
|
+
# Show the Sesame OWLIM server URI in use
|
16
|
+
> owlim host
|
17
|
+
|
18
|
+
# Show list of repositories
|
19
|
+
> owlim list
|
20
|
+
|
21
|
+
# Show number of triples in a repository
|
22
|
+
> owlim size repository
|
23
|
+
|
24
|
+
CRUD commands:
|
25
|
+
|
26
|
+
# Create a new repository
|
27
|
+
> owlim create repository
|
28
|
+
|
29
|
+
# Import a RDF file to a repository
|
30
|
+
> owlim import repository file [format]
|
31
|
+
|
32
|
+
# Export contents of a repository in RDF (default is RDF/XML)
|
33
|
+
> owlim export repository [format]
|
34
|
+
|
35
|
+
# Clear the contents of a repository
|
36
|
+
> owlim clear repository
|
37
|
+
|
38
|
+
# Drop a repository
|
39
|
+
> owlim drop repository
|
40
|
+
|
41
|
+
Valid formats for import and export are:
|
42
|
+
* RDF/XML: "rdf" or "rdfxml"
|
43
|
+
* Turtle: "ttl" or "turtle"
|
44
|
+
* N3: "n3"
|
45
|
+
* N-Triples: "nt"
|
46
|
+
* TriX: "trix"
|
47
|
+
* TriG: "trig"
|
48
|
+
* Bionary RDF: "rdfbin"
|
49
|
+
If not specified, format will be guessed from the file name suffix for import.
|
50
|
+
|
51
|
+
Query commands:
|
52
|
+
|
53
|
+
Query a repository by SPARQL or a keyword.
|
54
|
+
Result will be printed in a tabular text format by default.
|
55
|
+
|
56
|
+
Available alternative formats are:
|
57
|
+
* "json" for "application/sparql-result+json"
|
58
|
+
* "xml" for "application/sparql-result+xml".
|
59
|
+
|
60
|
+
# SPARQL query against a repository
|
61
|
+
> owlim query repository "SPARQL" [format]
|
62
|
+
|
63
|
+
# Search for literal objects matching a keyword and return relevant triples
|
64
|
+
> owlim find repository "keyword" [format]
|
65
|
+
|
66
|
+
Environmental variable:
|
67
|
+
|
68
|
+
Specify your OWLIM server by the environmental variable 'SESAME_URL'.
|
69
|
+
Default is "http://localhost:8080/openrdf-sesame"
|
70
|
+
|
71
|
+
# for B shell
|
72
|
+
> export SESAME_URL="http://example.org/openrdf-sesame"
|
73
|
+
|
74
|
+
# for C shell
|
75
|
+
> setenv SESAME_URL "http://example.org/openrdf-sesame"
|
76
|
+
|
77
|
+
HELP
|
78
|
+
end
|
79
|
+
|
80
|
+
def usage
|
81
|
+
puts <<USAGE
|
82
|
+
Help:
|
83
|
+
|
84
|
+
> owlim help
|
85
|
+
|
86
|
+
Examples:
|
87
|
+
|
88
|
+
> owlim host
|
89
|
+
> owlim list
|
90
|
+
|
91
|
+
> owlim create hoge
|
92
|
+
> owlim import hoge ../hoge.ttl
|
93
|
+
> owlim export hoge > hoge.rdf
|
94
|
+
> owlim size hoge
|
95
|
+
> owlim clear hoge
|
96
|
+
> owlim drop hoge
|
97
|
+
|
98
|
+
> owlim query hoge 'select * where { ?s ?p ?o . } limit 1000'
|
99
|
+
> owlim query hoge 'select * where { ?s ?p ?o . } limit 1000' json
|
100
|
+
> owlim query hoge 'select * where { ?s ?p ?o . } limit 1000' xml
|
101
|
+
|
102
|
+
> owlim find hoge "fuga"
|
103
|
+
> owlim find hoge "fuga" json
|
104
|
+
> owlim find hoge "fuga" xml
|
105
|
+
|
106
|
+
USAGE
|
107
|
+
end
|
108
|
+
|
109
|
+
host = ENV['SESAME_URL'] || "http://localhost:8080/openrdf-sesame"
|
110
|
+
serv = OWLIM.new(host)
|
111
|
+
|
112
|
+
command = ARGV.shift
|
113
|
+
repository = ARGV.shift
|
114
|
+
arguments = ARGV
|
115
|
+
|
116
|
+
case command
|
117
|
+
when "host"
|
118
|
+
puts serv.sesame_url
|
119
|
+
when "list"
|
120
|
+
puts serv.list
|
121
|
+
when "size"
|
122
|
+
if repository
|
123
|
+
puts serv.size(repository)
|
124
|
+
else
|
125
|
+
$stderr.puts "ERROR: missing a repository name."
|
126
|
+
$stderr.puts "> owlim size repository"
|
127
|
+
end
|
128
|
+
when "create"
|
129
|
+
if repository
|
130
|
+
serv.create(repository)
|
131
|
+
$stderr.puts "#{repository} created"
|
132
|
+
else
|
133
|
+
$stderr.puts "ERROR: missing a repository name."
|
134
|
+
$stderr.puts "> owlim create repository"
|
135
|
+
end
|
136
|
+
when "import"
|
137
|
+
if repository
|
138
|
+
if arguments.size > 1
|
139
|
+
rdf_file = arguments.shift
|
140
|
+
format = arguments.shift
|
141
|
+
serv.import(repository, rdf_file, :format => format)
|
142
|
+
$stderr.puts "#{rdf_file} imported to #{repository}"
|
143
|
+
elsif arguments.size > 0
|
144
|
+
rdf_file = arguments.shift
|
145
|
+
serv.import(repository, rdf_file)
|
146
|
+
$stderr.puts "#{rdf_file} imported to #{repository}"
|
147
|
+
else
|
148
|
+
$stderr.puts "ERROR: missing a file to import."
|
149
|
+
$stderr.puts "> owlim import repository file [format]"
|
150
|
+
end
|
151
|
+
else
|
152
|
+
$stderr.puts "ERROR: missing a repository name to import."
|
153
|
+
$stderr.puts "> owlim import repository file [format]"
|
154
|
+
end
|
155
|
+
when "export"
|
156
|
+
if repository
|
157
|
+
if arguments.size > 0
|
158
|
+
format = arguments.shift
|
159
|
+
puts serv.export(repository, :format => format)
|
160
|
+
else
|
161
|
+
puts serv.export(repository)
|
162
|
+
end
|
163
|
+
else
|
164
|
+
$stderr.puts "ERROR: missing a repository name to export."
|
165
|
+
$stderr.puts "> owlim export repository [format]"
|
166
|
+
end
|
167
|
+
when "clear"
|
168
|
+
if repository
|
169
|
+
serv.clear(repository)
|
170
|
+
$stderr.puts "#{repository} cleared"
|
171
|
+
else
|
172
|
+
$stderr.puts "ERROR: missing a repository name to clear."
|
173
|
+
$stderr.puts "> owlim clear repository"
|
174
|
+
end
|
175
|
+
when "drop"
|
176
|
+
if repository
|
177
|
+
serv.drop(repository)
|
178
|
+
$stderr.puts "#{repository} dropped"
|
179
|
+
else
|
180
|
+
$stderr.puts "ERROR: missing a repository name to drop."
|
181
|
+
$stderr.puts "> owlim drop repository"
|
182
|
+
end
|
183
|
+
when "query"
|
184
|
+
if repository
|
185
|
+
if arguments.size > 1
|
186
|
+
sparql = arguments.shift
|
187
|
+
format = arguments.shift
|
188
|
+
$stderr.puts "WARNING: invalid format #{format} (use 'xml' or 'json')" unless format[/(xml|json)/]
|
189
|
+
serv.query(repository, sparql, :format => format) {|x| print x}
|
190
|
+
elsif arguments.size > 0
|
191
|
+
sparql = arguments.shift
|
192
|
+
serv.query(repository, sparql) {|x| print x}
|
193
|
+
else
|
194
|
+
$stderr.puts "ERROR: missing SPARQL to query."
|
195
|
+
$stderr.puts "> owlim query repository SPARQL [format]"
|
196
|
+
end
|
197
|
+
else
|
198
|
+
$stderr.puts "ERROR: missing a repository name to query."
|
199
|
+
$stderr.puts "> owlim query repository SPARQL [format]"
|
200
|
+
end
|
201
|
+
when "find"
|
202
|
+
if repository
|
203
|
+
if arguments.size > 1
|
204
|
+
keyword = arguments.shift
|
205
|
+
format = arguments.shift
|
206
|
+
$stderr.puts "WARNING: invalid format '#{format}' (use 'xml' or 'json')" unless format[/(xml|json)/]
|
207
|
+
serv.find(repository, keyword, :format => format) {|x| print x}
|
208
|
+
elsif arguments.size > 0
|
209
|
+
keyword = arguments.shift
|
210
|
+
serv.find(repository, keyword) {|x| print x}
|
211
|
+
else
|
212
|
+
$stderr.puts "ERROR: missing a keyword to search."
|
213
|
+
$stderr.puts "> owlim find repository keyword"
|
214
|
+
end
|
215
|
+
else
|
216
|
+
$stderr.puts "ERROR: missing a repository name to search."
|
217
|
+
$stderr.puts "> owlim find repository keyword"
|
218
|
+
end
|
219
|
+
when "help"
|
220
|
+
help
|
221
|
+
usage
|
222
|
+
else
|
223
|
+
usage
|
224
|
+
end
|
225
|
+
|
data/gemspec.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'owlim-ruby'
|
3
|
+
s.version = "0.9.9"
|
4
|
+
s.license = "Ruby's"
|
5
|
+
|
6
|
+
s.authors = ["Toshiaki Katayama", "Tatsuya Nishizawa"]
|
7
|
+
s.email = "k@bioruby.org"
|
8
|
+
s.homepage = "https://github.com/ktym/owlim-ruby"
|
9
|
+
s.rubyforge_project = "owlim-ruby"
|
10
|
+
s.summary = "OWLIM client library for Ruby"
|
11
|
+
s.description = "Query and manage OWLIM triple store with ease."
|
12
|
+
|
13
|
+
s.platform = Gem::Platform::RUBY
|
14
|
+
s.files = [
|
15
|
+
"README.rdoc",
|
16
|
+
"bin/owlim",
|
17
|
+
"lib/owlim.rb",
|
18
|
+
"lib/owlim/create_repository.xml.erb",
|
19
|
+
"lib/owlim/drop_repository.xml.erb",
|
20
|
+
"gemspec.rb"
|
21
|
+
]
|
22
|
+
|
23
|
+
s.require_path = 'lib'
|
24
|
+
s.autorequire = 'owlim'
|
25
|
+
|
26
|
+
s.bindir = "bin"
|
27
|
+
s.executables = [
|
28
|
+
"owlim",
|
29
|
+
]
|
30
|
+
s.default_executable = "owlim"
|
31
|
+
|
32
|
+
s.add_runtime_dependency "uuid"
|
33
|
+
s.add_runtime_dependency "json"
|
34
|
+
end
|
data/lib/owlim.rb
ADDED
@@ -0,0 +1,259 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "uri"
|
3
|
+
require "uuid"
|
4
|
+
require "erb"
|
5
|
+
require "net/http"
|
6
|
+
require "cgi"
|
7
|
+
#require "yaml"
|
8
|
+
require "json"
|
9
|
+
require "rexml/document"
|
10
|
+
|
11
|
+
class OWLIM
|
12
|
+
|
13
|
+
def initialize(url)
|
14
|
+
sesame_url(url)
|
15
|
+
Net::HTTP.version_1_2
|
16
|
+
end
|
17
|
+
|
18
|
+
def sesame_url(url = nil)
|
19
|
+
if url
|
20
|
+
@server = url
|
21
|
+
@uri = URI.parse(url)
|
22
|
+
@host = @uri.host
|
23
|
+
@port = @uri.port
|
24
|
+
@path = @uri.path
|
25
|
+
else
|
26
|
+
return @server
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def list
|
31
|
+
result = ""
|
32
|
+
|
33
|
+
Net::HTTP.start(@host, @port) do |http|
|
34
|
+
response = http.get("#{@path}/repositories",
|
35
|
+
{"Accept" => "application/sparql-results+json"})
|
36
|
+
result = response.body
|
37
|
+
end
|
38
|
+
|
39
|
+
#hash = YAML.load(result)
|
40
|
+
hash = JSON.parse(result)
|
41
|
+
|
42
|
+
hash["results"]["bindings"].map {|b| b["id"]["value"]}
|
43
|
+
end
|
44
|
+
|
45
|
+
def create(repository, opts={})
|
46
|
+
rdftransaction = create_repository(repository, opts)
|
47
|
+
|
48
|
+
Net::HTTP.start(@host, @port) do |http|
|
49
|
+
response = http.post("#{@path}/repositories/SYSTEM/statements",
|
50
|
+
rdftransaction,
|
51
|
+
{"Content-Type" => "application/x-rdftransaction"})
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def import(repository, data_file, opts={})
|
56
|
+
http = Net::HTTP.new(@host, @port)
|
57
|
+
http.read_timeout = 60 * 60
|
58
|
+
req = Net::HTTP::Post.new("#{@path}/repositories/#{repository}/statements")
|
59
|
+
if opts[:content_type]
|
60
|
+
req["Content-Type"] = opts[:content_type]
|
61
|
+
elsif opts[:format]
|
62
|
+
if ct = content_type(opts[:format])
|
63
|
+
req["Content-Type"] = ct
|
64
|
+
else
|
65
|
+
raise "ERROR: Content-Type detection failed for #{data_file}."
|
66
|
+
end
|
67
|
+
else
|
68
|
+
if suffix = File.basename(data_file)[/\.(\w+)$/, 1]
|
69
|
+
if ct = content_type(suffix.downcase)
|
70
|
+
req["Content-Type"] = ct
|
71
|
+
else
|
72
|
+
raise "ERROR: Content-Type detection failed for '#{data_file}'."
|
73
|
+
end
|
74
|
+
else
|
75
|
+
raise "ERROR: Invalid suffix (#{data_file}). Explicitly specify the RDF format."
|
76
|
+
end
|
77
|
+
end
|
78
|
+
#req["Transfer-Encoding"] = "chunked"
|
79
|
+
req["Content-Length"] = File.size(data_file)
|
80
|
+
|
81
|
+
res = nil
|
82
|
+
File.open(data_file, "r") do |f|
|
83
|
+
req.body_stream = f
|
84
|
+
res = http.request(req)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def export(repository, opts={})
|
89
|
+
result = ""
|
90
|
+
|
91
|
+
if opts[:format]
|
92
|
+
unless format = content_type(opts[:format])
|
93
|
+
raise "ERROR: Invalid format (#{opts[:format]})."
|
94
|
+
end
|
95
|
+
else
|
96
|
+
format = "application/rdf+xml"
|
97
|
+
end
|
98
|
+
|
99
|
+
Net::HTTP.start(@host, @port) do |http|
|
100
|
+
path = "#{@path}/repositories/#{repository}/statements?infer=false"
|
101
|
+
response = http.get(path, {"Accept" => format})
|
102
|
+
result = response.body
|
103
|
+
end
|
104
|
+
|
105
|
+
result
|
106
|
+
end
|
107
|
+
|
108
|
+
def size(repository)
|
109
|
+
result = ""
|
110
|
+
|
111
|
+
Net::HTTP.start(@host, @port) do |http|
|
112
|
+
response = http.get("#{@path}/repositories/#{repository}/size")
|
113
|
+
result = response.body
|
114
|
+
end
|
115
|
+
|
116
|
+
result.to_i
|
117
|
+
end
|
118
|
+
|
119
|
+
def clear(repository)
|
120
|
+
Net::HTTP.start(@host, @port) do |http|
|
121
|
+
http.read_timeout = 60 * 60
|
122
|
+
response = http.delete("#{@path}/repositories/#{repository}/statements")
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
def drop(repository)
|
127
|
+
rdftransaction = drop_repository(repository)
|
128
|
+
|
129
|
+
Net::HTTP.start(@host, @port) do |http|
|
130
|
+
response = http.post("#{@path}/repositories/SYSTEM/statements",
|
131
|
+
rdftransaction,
|
132
|
+
{"Content-Type" => "application/x-rdftransaction"})
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def query(repository, sparql, opts={}, &block)
|
137
|
+
result = ""
|
138
|
+
|
139
|
+
case opts[:format]
|
140
|
+
when "xml"
|
141
|
+
format = "application/sparql-results+xml"
|
142
|
+
when "json"
|
143
|
+
format = "application/sparql-results+json"
|
144
|
+
else # tabular text
|
145
|
+
format = "application/sparql-results+json"
|
146
|
+
end
|
147
|
+
|
148
|
+
Net::HTTP.start(@host, @port) do |http|
|
149
|
+
path = "#{@path}/repositories/#{repository}?query=#{CGI.escape(sparql)}"
|
150
|
+
http.get(path, {"Accept" => "#{format}"}) { |body|
|
151
|
+
if block and opts[:format]
|
152
|
+
yield body
|
153
|
+
else
|
154
|
+
result += body
|
155
|
+
end
|
156
|
+
}
|
157
|
+
end
|
158
|
+
|
159
|
+
if table = format_json(result)
|
160
|
+
if block
|
161
|
+
yield table
|
162
|
+
else
|
163
|
+
return table
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
def find(repository, keyword, opts={}, &block)
|
169
|
+
sparql = "select ?s ?p ?o where { ?s ?t '#{keyword}'. ?s ?p ?o . }"
|
170
|
+
query(repository, sparql, opts, &block)
|
171
|
+
end
|
172
|
+
|
173
|
+
private
|
174
|
+
|
175
|
+
def content_type(format)
|
176
|
+
case format
|
177
|
+
when "ttl", "turtle"
|
178
|
+
ct = 'application/x-turtle'
|
179
|
+
when "rdf", "rdfxml"
|
180
|
+
ct = 'application/rdf+xml'
|
181
|
+
when "n3"
|
182
|
+
ct = 'text/rdf+n3'
|
183
|
+
when "nt"
|
184
|
+
ct = 'text/plain'
|
185
|
+
when "trix"
|
186
|
+
ct = 'application/trix'
|
187
|
+
when "trig"
|
188
|
+
ct = 'application/x-trig'
|
189
|
+
when "rdfbin"
|
190
|
+
ct = 'application/x-binary-rdf'
|
191
|
+
else
|
192
|
+
ct = false
|
193
|
+
end
|
194
|
+
return ct
|
195
|
+
end
|
196
|
+
|
197
|
+
def format_json(json)
|
198
|
+
begin
|
199
|
+
#hash = YAML.load(json)
|
200
|
+
hash = JSON.parse(json)
|
201
|
+
head = hash["head"]["vars"]
|
202
|
+
body = hash["results"]["bindings"]
|
203
|
+
rescue
|
204
|
+
return ""
|
205
|
+
end
|
206
|
+
text = ""
|
207
|
+
text << head.join("\t") + "\n"
|
208
|
+
body.each do |result|
|
209
|
+
ary = []
|
210
|
+
head.each do |key|
|
211
|
+
data = result[key] || { "type" => '', "value" => ''}
|
212
|
+
if data["type"] == "uri"
|
213
|
+
uri = '<' + data["value"].gsub('\\', '') + '>'
|
214
|
+
ary << uri
|
215
|
+
else
|
216
|
+
val = data["value"].gsub('\/', '/')
|
217
|
+
ary << val
|
218
|
+
end
|
219
|
+
end
|
220
|
+
text << ary.join("\t") + "\n"
|
221
|
+
end
|
222
|
+
return text
|
223
|
+
end
|
224
|
+
|
225
|
+
def create_repository(repository, opts = {})
|
226
|
+
uuid0 = UUID.new.generate
|
227
|
+
uuid1 = UUID.new.generate
|
228
|
+
uuid2 = UUID.new.generate
|
229
|
+
uuid3 = UUID.new.generate
|
230
|
+
repository_id = repository
|
231
|
+
repository_label = opts[:repository_label]
|
232
|
+
default_namespace = opts[:default_namespace]
|
233
|
+
base_url = opts[:base_url]
|
234
|
+
|
235
|
+
template = "/owlim/create_repository.xml.erb"
|
236
|
+
erb = ERB.new(File.read(File.dirname(__FILE__) + template), nil, "-")
|
237
|
+
erb.result(binding)
|
238
|
+
end
|
239
|
+
|
240
|
+
def drop_repository(repository)
|
241
|
+
pred = "<http://www.openrdf.org/config/repository#repositoryID>"
|
242
|
+
obj = "\"#{repository}\""
|
243
|
+
|
244
|
+
result = ""
|
245
|
+
Net::HTTP.start(@host, @port) do |http|
|
246
|
+
path = "#{@path}/repositories/SYSTEM/statements?pred=#{CGI.escape(pred)}&obj=#{CGI.escape(obj)}&infer=true"
|
247
|
+
response = http.get(path, {"Accept" => "application/trix"})
|
248
|
+
result = response.body
|
249
|
+
end
|
250
|
+
|
251
|
+
doc = REXML::Document.new result
|
252
|
+
bnode_id = doc.elements["TriX/graph/id"].text
|
253
|
+
|
254
|
+
template = "/owlim/drop_repository.xml.erb"
|
255
|
+
erb = ERB.new(File.read(File.dirname(__FILE__) + template), nil, "-")
|
256
|
+
erb.result(binding)
|
257
|
+
end
|
258
|
+
|
259
|
+
end
|
@@ -0,0 +1,129 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<transaction>
|
3
|
+
<add>
|
4
|
+
<bnode><%= uuid0 %></bnode>
|
5
|
+
<uri>http://www.w3.org/1999/02/22-rdf-syntax-ns#type</uri>
|
6
|
+
<uri>http://www.openrdf.org/config/repository#RepositoryContext</uri>
|
7
|
+
<contexts/>
|
8
|
+
</add>
|
9
|
+
<add>
|
10
|
+
<bnode><%= uuid1 %></bnode>
|
11
|
+
<uri>http://www.w3.org/1999/02/22-rdf-syntax-ns#type</uri>
|
12
|
+
<uri>http://www.openrdf.org/config/repository#Repository</uri>
|
13
|
+
<contexts>
|
14
|
+
<bnode><%= uuid0 %></bnode>
|
15
|
+
</contexts>
|
16
|
+
</add>
|
17
|
+
<add>
|
18
|
+
<bnode><%= uuid1 %></bnode>
|
19
|
+
<uri>http://www.openrdf.org/config/repository#repositoryID</uri>
|
20
|
+
<literal><%= repository_id %></literal>
|
21
|
+
<contexts>
|
22
|
+
<bnode><%= uuid0 %></bnode>
|
23
|
+
</contexts>
|
24
|
+
</add>
|
25
|
+
<add>
|
26
|
+
<bnode><%= uuid1 %></bnode>
|
27
|
+
<uri>http://www.w3.org/2000/01/rdf-schema#label</uri>
|
28
|
+
<literal><%= repository_label %></literal>
|
29
|
+
<contexts>
|
30
|
+
<bnode><%= uuid0 %></bnode>
|
31
|
+
</contexts>
|
32
|
+
</add>
|
33
|
+
<add>
|
34
|
+
<bnode><%= uuid2 %></bnode>
|
35
|
+
<uri>http://www.openrdf.org/config/repository#repositoryType</uri>
|
36
|
+
<literal>openrdf:SailRepository</literal>
|
37
|
+
<contexts>
|
38
|
+
<bnode><%= uuid0 %></bnode>
|
39
|
+
</contexts>
|
40
|
+
</add>
|
41
|
+
<add>
|
42
|
+
<bnode><%= uuid3 %></bnode>
|
43
|
+
<uri>http://www.openrdf.org/config/sail#sailType</uri>
|
44
|
+
<literal>swiftowlim:Sail</literal>
|
45
|
+
<contexts>
|
46
|
+
<bnode><%= uuid0 %></bnode>
|
47
|
+
</contexts>
|
48
|
+
</add>
|
49
|
+
<add>
|
50
|
+
<bnode><%= uuid3 %></bnode>
|
51
|
+
<uri>http://www.ontotext.com/trree/owlim#ruleset</uri>
|
52
|
+
<literal>owl-horst-optimized</literal>
|
53
|
+
<contexts>
|
54
|
+
<bnode><%= uuid0 %></bnode>
|
55
|
+
</contexts>
|
56
|
+
</add>
|
57
|
+
<add>
|
58
|
+
<bnode><%= uuid3 %></bnode>
|
59
|
+
<uri>http://www.ontotext.com/trree/owlim#imports</uri>
|
60
|
+
<literal></literal>
|
61
|
+
<contexts>
|
62
|
+
<bnode><%= uuid0 %></bnode>
|
63
|
+
</contexts>
|
64
|
+
</add>
|
65
|
+
<add>
|
66
|
+
<bnode><%= uuid3 %></bnode>
|
67
|
+
<uri>http://www.ontotext.com/trree/owlim#repository-type</uri>
|
68
|
+
<literal>in-memory-repository</literal>
|
69
|
+
<contexts>
|
70
|
+
<bnode><%= uuid0 %></bnode>
|
71
|
+
</contexts>
|
72
|
+
</add>
|
73
|
+
<add>
|
74
|
+
<bnode><%= uuid3 %></bnode>
|
75
|
+
<uri>http://www.ontotext.com/trree/owlim#defaultNS</uri>
|
76
|
+
<literal><%= default_namespace %></literal>
|
77
|
+
<contexts>
|
78
|
+
<bnode><%= uuid0 %></bnode>
|
79
|
+
</contexts>
|
80
|
+
</add>
|
81
|
+
<add>
|
82
|
+
<bnode><%= uuid3 %></bnode>
|
83
|
+
<uri>http://www.ontotext.com/trree/owlim#storage-folder</uri>
|
84
|
+
<literal>storage</literal>
|
85
|
+
<contexts>
|
86
|
+
<bnode><%= uuid0 %></bnode>
|
87
|
+
</contexts>
|
88
|
+
</add>
|
89
|
+
<add>
|
90
|
+
<bnode><%= uuid3 %></bnode>
|
91
|
+
<uri>http://www.ontotext.com/trree/owlim#entity-index-size</uri>
|
92
|
+
<literal>200000</literal>
|
93
|
+
<contexts>
|
94
|
+
<bnode><%= uuid0 %></bnode>
|
95
|
+
</contexts>
|
96
|
+
</add>
|
97
|
+
<add>
|
98
|
+
<bnode><%= uuid3 %></bnode>
|
99
|
+
<uri>http://www.ontotext.com/trree/owlim#noPersist</uri>
|
100
|
+
<literal>false</literal>
|
101
|
+
<contexts>
|
102
|
+
<bnode><%= uuid0 %></bnode>
|
103
|
+
</contexts>
|
104
|
+
</add>
|
105
|
+
<add>
|
106
|
+
<bnode><%= uuid3 %></bnode>
|
107
|
+
<uri>http://www.ontotext.com/trree/owlim#base-URL</uri>
|
108
|
+
<literal><%= base_url %></literal>
|
109
|
+
<contexts>
|
110
|
+
<bnode><%= uuid0 %></bnode>
|
111
|
+
</contexts>
|
112
|
+
</add>
|
113
|
+
<add>
|
114
|
+
<bnode><%= uuid2 %></bnode>
|
115
|
+
<uri>http://www.openrdf.org/config/repository/sail#sailImpl</uri>
|
116
|
+
<bnode><%= uuid3 %></bnode>
|
117
|
+
<contexts>
|
118
|
+
<bnode><%= uuid0 %></bnode>
|
119
|
+
</contexts>
|
120
|
+
</add>
|
121
|
+
<add>
|
122
|
+
<bnode><%= uuid1 %></bnode>
|
123
|
+
<uri>http://www.openrdf.org/config/repository#repositoryImpl</uri>
|
124
|
+
<bnode><%= uuid2 %></bnode>
|
125
|
+
<contexts>
|
126
|
+
<bnode><%= uuid0 %></bnode>
|
127
|
+
</contexts>
|
128
|
+
</add>
|
129
|
+
</transaction>
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<transaction>
|
3
|
+
<clear>
|
4
|
+
<contexts>
|
5
|
+
<bnode><%= bnode_id %></bnode>
|
6
|
+
</contexts>
|
7
|
+
</clear>
|
8
|
+
<remove>
|
9
|
+
<bnode><%= bnode_id %></bnode>
|
10
|
+
<uri>http://www.w3.org/1999/02/22-rdf-syntax-ns#type</uri>
|
11
|
+
<uri>http://www.openrdf.org/config/repository#RepositoryContext</uri>
|
12
|
+
<contexts/>
|
13
|
+
</remove>
|
14
|
+
</transaction>
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: owlim-ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 9
|
8
|
+
- 9
|
9
|
+
version: 0.9.9
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Toshiaki Katayama
|
13
|
+
- Tatsuya Nishizawa
|
14
|
+
autorequire: owlim
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-05-15 00:00:00 +09:00
|
19
|
+
default_executable: owlim
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: uuid
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: json
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
segments:
|
43
|
+
- 0
|
44
|
+
version: "0"
|
45
|
+
type: :runtime
|
46
|
+
version_requirements: *id002
|
47
|
+
description: Query and manage OWLIM triple store with ease.
|
48
|
+
email: k@bioruby.org
|
49
|
+
executables:
|
50
|
+
- owlim
|
51
|
+
extensions: []
|
52
|
+
|
53
|
+
extra_rdoc_files: []
|
54
|
+
|
55
|
+
files:
|
56
|
+
- README.rdoc
|
57
|
+
- bin/owlim
|
58
|
+
- lib/owlim.rb
|
59
|
+
- lib/owlim/create_repository.xml.erb
|
60
|
+
- lib/owlim/drop_repository.xml.erb
|
61
|
+
- gemspec.rb
|
62
|
+
has_rdoc: true
|
63
|
+
homepage: https://github.com/ktym/owlim-ruby
|
64
|
+
licenses:
|
65
|
+
- Ruby's
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
segments:
|
77
|
+
- 0
|
78
|
+
version: "0"
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
segments:
|
85
|
+
- 0
|
86
|
+
version: "0"
|
87
|
+
requirements: []
|
88
|
+
|
89
|
+
rubyforge_project: owlim-ruby
|
90
|
+
rubygems_version: 1.3.7
|
91
|
+
signing_key:
|
92
|
+
specification_version: 3
|
93
|
+
summary: OWLIM client library for Ruby
|
94
|
+
test_files: []
|
95
|
+
|