rdf 0.3.3 → 0.3.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README +107 -20
- data/VERSION +1 -1
- data/bin/rdf +4 -8
- data/etc/doap.nt +8 -8
- data/lib/rdf.rb +1 -0
- data/lib/rdf/cli.rb +146 -19
- data/lib/rdf/format.rb +59 -10
- data/lib/rdf/mixin/type_check.rb +21 -0
- data/lib/rdf/model/graph.rb +1 -0
- data/lib/rdf/model/list.rb +36 -3
- data/lib/rdf/model/literal.rb +113 -74
- data/lib/rdf/model/literal/boolean.rb +15 -5
- data/lib/rdf/model/literal/date.rb +24 -6
- data/lib/rdf/model/literal/datetime.rb +21 -4
- data/lib/rdf/model/literal/decimal.rb +3 -128
- data/lib/rdf/model/literal/double.rb +4 -107
- data/lib/rdf/model/literal/integer.rb +3 -97
- data/lib/rdf/model/literal/numeric.rb +178 -3
- data/lib/rdf/model/literal/time.rb +23 -3
- data/lib/rdf/model/literal/token.rb +2 -2
- data/lib/rdf/model/literal/xml.rb +1 -1
- data/lib/rdf/model/node.rb +35 -5
- data/lib/rdf/model/statement.rb +7 -6
- data/lib/rdf/model/term.rb +32 -0
- data/lib/rdf/model/uri.rb +13 -7
- data/lib/rdf/model/value.rb +10 -0
- data/lib/rdf/nquads.rb +120 -3
- data/lib/rdf/ntriples/format.rb +9 -1
- data/lib/rdf/ntriples/writer.rb +1 -0
- data/lib/rdf/query.rb +121 -13
- data/lib/rdf/query/pattern.rb +28 -15
- data/lib/rdf/query/solution.rb +47 -0
- data/lib/rdf/query/solutions.rb +45 -10
- data/lib/rdf/query/variable.rb +39 -4
- data/lib/rdf/reader.rb +47 -4
- data/lib/rdf/repository.rb +8 -4
- data/lib/rdf/util/file.rb +5 -2
- data/lib/rdf/version.rb +1 -1
- data/lib/rdf/writer.rb +34 -5
- metadata +56 -88
data/README
CHANGED
@@ -27,44 +27,98 @@ Features
|
|
27
27
|
use of any one part of the library without needing to load up the rest.
|
28
28
|
* Compatible with Ruby 1.8.7+, Ruby 1.9.x, and JRuby 1.4/1.5.
|
29
29
|
* Compatible with older Ruby versions with the help of the [Backports][] gem.
|
30
|
+
* Performs auto-detection of input to select appropriate Reader class if one
|
31
|
+
cannot be determined from file characteristics.
|
30
32
|
|
31
33
|
Tutorials
|
32
34
|
---------
|
33
35
|
|
34
|
-
* [Getting data from the Semantic Web using Ruby and RDF.rb]
|
35
|
-
|
36
|
-
* [
|
37
|
-
|
38
|
-
|
39
|
-
|
36
|
+
* [Getting data from the Semantic Web using Ruby and RDF.rb](http://semanticweb.org/wiki/Getting_data_from_the_Semantic_Web_%28Ruby%29)
|
37
|
+
* [Using RDF.rb and Spira to process RDF data from the British Ordnance Survey](http://stephenpope.co.uk/?p=85)
|
38
|
+
* [Getting started with RDF and SPARQL using 4store and RDF.rb](http://www.jenitennison.com/blog/node/152)
|
39
|
+
|
40
|
+
Command Line
|
41
|
+
------------
|
42
|
+
When installed, RDF.rb includes a `rdf` shell script which acts as a wrapper to perform a number of different
|
43
|
+
operations on RDF files using available readers and writers.
|
44
|
+
|
45
|
+
* `serialize`: Parse an RDF input and re-serializing to N-Triples or another available format using `--output-format` option.
|
46
|
+
* `count`: Parse and RDF input and count the number of statements.
|
47
|
+
* `subjects`: Returns unique subjects from parsed input.
|
48
|
+
* `objects`: Returns unique objects from parsed input.
|
49
|
+
* `predicates`: Returns unique objects from parsed input.
|
40
50
|
|
41
51
|
Examples
|
42
52
|
--------
|
43
53
|
|
44
54
|
require 'rdf'
|
45
|
-
|
46
55
|
include RDF
|
47
56
|
|
48
57
|
### Writing RDF data using the N-Triples format
|
49
58
|
|
50
59
|
require 'rdf/ntriples'
|
60
|
+
graph = RDF::Graph.new << [:hello, RDF::DC.title, "Hello, world!"]
|
61
|
+
graph.dump(:ntriples)
|
51
62
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
end
|
56
|
-
end
|
63
|
+
or
|
64
|
+
|
65
|
+
RDF::Writer.open("hello.nt") { |writer| writer << graph }
|
57
66
|
|
58
67
|
### Reading RDF data in the N-Triples format
|
59
68
|
|
60
69
|
require 'rdf/ntriples'
|
70
|
+
graph = RDF::Graph.load("http://rdf.rubyforge.org/doap.nt")
|
61
71
|
|
72
|
+
or
|
73
|
+
|
62
74
|
RDF::Reader.open("http://rdf.rubyforge.org/doap.nt") do |reader|
|
63
75
|
reader.each_statement do |statement|
|
64
76
|
puts statement.inspect
|
65
77
|
end
|
66
78
|
end
|
67
79
|
|
80
|
+
### Reading RDF data in other formats
|
81
|
+
{RDF::Reader.open} and {RDF::Repository.load} use a number of mechanisms to determine the appropriate reader
|
82
|
+
to use when loading a file. The specific format to use can be forced using, e.g. `:format => :ntriples`
|
83
|
+
option where the specific format symbol is determined by the available readers. Both also use
|
84
|
+
MimeType or file extension, where available.
|
85
|
+
|
86
|
+
require 'linkeddata'
|
87
|
+
|
88
|
+
graph = RDF::Graph.load("etc/doap.nq", :format => :nquads)
|
89
|
+
|
90
|
+
A specific sub-type of Reader can also be invoked directly:
|
91
|
+
|
92
|
+
require 'rdf/nquads'
|
93
|
+
|
94
|
+
RDF::NQuads::Reader.open("http://rdf.rubyforge.org/doap.nq") do |reader|
|
95
|
+
reader.each_statement do |statement|
|
96
|
+
puts statement.inspect
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
Reader/Writer implementations may override {RDF::Format.detect}, which takes a small sample if input
|
101
|
+
and return a boolean indicating if it matches that specific format. In the case that a format cannot
|
102
|
+
be detected from filename or other options, or that more than one format is identified,
|
103
|
+
{RDF::Format.for} will query each loaded format by invoking it's `detect` method, and the first successful
|
104
|
+
match will be used to read the input.
|
105
|
+
|
106
|
+
### Writing RDF data using other formats
|
107
|
+
{RDF::Writer.open}, {RDF::Enumerable#dump}, {RDF::Writer.dump} take similar options to {RDF::Reader.open} to determine the
|
108
|
+
appropriate writer to use.
|
109
|
+
|
110
|
+
require 'linkeddata'
|
111
|
+
|
112
|
+
RDF::Writer.open("hello.nq", :format => :nquads) do |writer|
|
113
|
+
writer << RDF::Repository.new do |repo|
|
114
|
+
repo << RDF::Statement.new(:hello, RDF::DC.title, "Hello, world!", :context => RDF::URI("context"))
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
A specific sub-type of Writer can also be invoked directly:
|
119
|
+
|
120
|
+
graph.dump(:nq)
|
121
|
+
|
68
122
|
### Querying RDF data using basic graph patterns (BGPs)
|
69
123
|
|
70
124
|
require 'rdf/ntriples'
|
@@ -82,6 +136,8 @@ Examples
|
|
82
136
|
puts "name=#{solution.name} email=#{solution.email}"
|
83
137
|
end
|
84
138
|
|
139
|
+
A separate [SPARQL][SPARQL doc] gem builds on basic BGP support to provide full support for [SPARQL 1.0](http://www.w3.org/TR/rdf-sparql-query/) queries.
|
140
|
+
|
85
141
|
### Using pre-defined RDF vocabularies
|
86
142
|
|
87
143
|
DC.title #=> RDF::URI("http://purl.org/dc/terms/title")
|
@@ -111,7 +167,16 @@ Documentation
|
|
111
167
|
* {RDF::Value}
|
112
168
|
* {RDF::Term}
|
113
169
|
* {RDF::Literal}
|
170
|
+
* {RDF::Literal::Boolean}
|
171
|
+
* {RDF::Literal::Date}
|
172
|
+
* {RDF::Literal::DateTime}
|
173
|
+
* {RDF::Literal::Decimal}
|
174
|
+
* {RDF::Literal::Double}
|
175
|
+
* {RDF::Literal::Integer}
|
176
|
+
* {RDF::Literal::Time}
|
177
|
+
* [RDF::XSD](http://rubydoc.info/github/gkellogg/rdf-xsd/master/frames) (plugin)
|
114
178
|
* {RDF::Resource}
|
179
|
+
* {RDF::List}
|
115
180
|
* {RDF::Node}
|
116
181
|
* {RDF::URI}
|
117
182
|
* {RDF::Graph}
|
@@ -127,14 +192,23 @@ Documentation
|
|
127
192
|
|
128
193
|
### RDF Serialization Formats
|
129
194
|
|
195
|
+
The following is a partial list of RDF formats implemented either natively, or through the inclusion of
|
196
|
+
other gems:
|
197
|
+
|
130
198
|
* {RDF::NTriples}
|
199
|
+
* {RDF::NQuads}
|
200
|
+
* [JSON::LD][JSONLD doc] (plugin)
|
131
201
|
* [RDF::JSON](http://rdf.rubyforge.org/json/) (plugin)
|
132
|
-
* [RDF::
|
202
|
+
* [RDF::Microdata][Microdata doc] (plugin)
|
203
|
+
* [RDF::N3][N3 doc] (plugin)
|
133
204
|
* [RDF::Raptor::RDFXML](http://rdf.rubyforge.org/raptor/) (plugin)
|
134
205
|
* [RDF::Raptor::Turtle](http://rdf.rubyforge.org/raptor/) (plugin)
|
135
|
-
* [RDF::RDFa]
|
136
|
-
* [RDF::RDFXML]
|
206
|
+
* [RDF::RDFa][RDFa doc] (plugin)
|
207
|
+
* [RDF::RDFXML][RDFXML doc] (plugin)
|
137
208
|
* [RDF::Trix](http://rdf.rubyforge.org/trix/) (plugin)
|
209
|
+
* [RDF::Turtle][Turtle doc] (plugin)
|
210
|
+
|
211
|
+
The meta-gem [LinkedData][LinkedData doc] includes many of these gems.
|
138
212
|
|
139
213
|
### RDF Storage
|
140
214
|
|
@@ -149,6 +223,8 @@ Documentation
|
|
149
223
|
* {RDF::Mutable}
|
150
224
|
* {RDF::Durable}
|
151
225
|
* {RDF::Transaction}
|
226
|
+
* [RDF::AllegroGraph](http://rubydoc.info/github/emk/rdf-agraph/master/frames) (plugin)
|
227
|
+
* [RDF::Mongo](http://rubydoc.info/github/pius/rdf-mongo/master/frames) (plugin)
|
152
228
|
* [RDF::DataObjects](http://rdf.rubyforge.org/do/) (plugin)
|
153
229
|
* [RDF::Sesame](http://rdf.rubyforge.org/sesame/) (plugin)
|
154
230
|
|
@@ -159,6 +235,8 @@ Documentation
|
|
159
235
|
* {RDF::Query::Solution}
|
160
236
|
* {RDF::Query::Solutions}
|
161
237
|
* {RDF::Query::Variable}
|
238
|
+
* [SPARQL](http://rubydoc.info/github/gkellogg/sparql/frames) (plugin)
|
239
|
+
|
162
240
|
|
163
241
|
### RDF Vocabularies
|
164
242
|
|
@@ -267,8 +345,17 @@ License
|
|
267
345
|
This is free and unencumbered public domain software. For more information,
|
268
346
|
see <http://unlicense.org/> or the accompanying {file:UNLICENSE} file.
|
269
347
|
|
270
|
-
[RDF]:
|
271
|
-
[YARD]:
|
272
|
-
[YARD-GS]:
|
273
|
-
[PDD]:
|
274
|
-
[Backports]:
|
348
|
+
[RDF]: http://www.w3.org/RDF/
|
349
|
+
[YARD]: http://yardoc.org/
|
350
|
+
[YARD-GS]: http://rubydoc.info/docs/yard/file/docs/GettingStarted.md
|
351
|
+
[PDD]: http://lists.w3.org/Archives/Public/public-rdf-ruby/2010May/0013.html
|
352
|
+
[Backports]: http://rubygems.org/gems/backports
|
353
|
+
[JSONLD doc]: http://rubydoc.info/github/gkellogg/json-ld/frames
|
354
|
+
[LinkedData doc]: http://rubydoc.info/github/datagraph/linkeddata/master/frames
|
355
|
+
[Microdata doc]: http://rubydoc.info/github/gkellogg/rdf-microdata/frames
|
356
|
+
[N3 doc]: http://rubydoc.info/github/gkellogg/rdf-n3/master/frames
|
357
|
+
[RDFa doc]: http://rubydoc.info/github/gkellogg/rdf-rdfa/master/frames
|
358
|
+
[RDFXML doc]: http://rubydoc.info/github/gkellogg/rdf-rdfxml/master/frames
|
359
|
+
[Turtle doc]: http://rubydoc.info/github/gkellogg/rdf-turtle/master/frames
|
360
|
+
[SPARQL doc]: http://rubydoc.info/github/gkellogg/sparql/frames
|
361
|
+
[SPARQL 1.0]: http://www.w3.org/TR/rdf-sparql-query/
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.4
|
data/bin/rdf
CHANGED
@@ -1,22 +1,18 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')))
|
3
|
+
require 'rubygems'
|
3
4
|
require 'rdf/cli'
|
4
5
|
|
5
6
|
options = RDF::CLI.options do
|
6
|
-
self.banner = "Usage: #{RDF::CLI.basename} [options] command [args...]"
|
7
|
-
self.on('-d', '--debug', 'Enable debug output for troubleshooting.') do
|
8
|
-
$DEBUG = true
|
9
|
-
end
|
10
7
|
self.on('-v', '--verbose', 'Enable verbose output. May be given more than once.') do
|
11
8
|
$VERBOSE = true
|
12
9
|
end
|
10
|
+
|
13
11
|
self.on('-V', '--version', 'Display the RDF.rb version and exit.') do
|
14
12
|
puts RDF::VERSION; exit
|
15
13
|
end
|
16
14
|
end
|
17
15
|
|
18
|
-
abort options.banner if ARGV.empty?
|
16
|
+
abort options.banner if ARGV.empty? && !options.has_key?(:evaluate)
|
19
17
|
|
20
|
-
|
21
|
-
abort "#{File.basename($0)}: unknown command `#{command}'"
|
22
|
-
end
|
18
|
+
RDF::CLI.exec_command(command = ARGV.shift, ARGV, options.options)
|
data/etc/doap.nt
CHANGED
@@ -12,11 +12,11 @@
|
|
12
12
|
<http://bhuga.net/#ben> <http://xmlns.com/foaf/0.1/mbox> <mailto:blavender@gmail.com> .
|
13
13
|
<http://bhuga.net/#ben> <http://xmlns.com/foaf/0.1/mbox_sha1sum> "dbf45f4ffbd27b67aa84f02a6a31c144727d10af" .
|
14
14
|
<http://bhuga.net/#ben> <http://xmlns.com/foaf/0.1/name> "Ben Lavender" .
|
15
|
-
<http://
|
16
|
-
<http://
|
17
|
-
<http://
|
18
|
-
<http://
|
19
|
-
<http://
|
15
|
+
<http://greggkellogg.net/foaf.rdf#me> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person> .
|
16
|
+
<http://greggkellogg.net/foaf.rdf#me> <http://xmlns.com/foaf/0.1/name> "Gregg Kellogg" .
|
17
|
+
<http://greggkellogg.net/foaf.rdf#me> <http://xmlns.com/foaf/0.1/mbox> <mailto:gregg@kellogg-assoc.com> .
|
18
|
+
<http://greggkellogg.net/foaf.rdf#me> <http://xmlns.com/foaf/0.1/mbox_sha1sum> "35bc44e6d0070e5ad50ccbe0d24403c96af2b9bd" .
|
19
|
+
<http://greggkellogg.net/foaf.rdf#me> <http://xmlns.com/foaf/0.1/homepage> <http://greggkellogg.net/> .
|
20
20
|
<http://rubygems.org/gems/rdf> <http://purl.org/dc/terms/creator> <http://ar.to/#self> .
|
21
21
|
<http://rubygems.org/gems/rdf> <http://usefulinc.com/ns/doap#blog> <http://ar.to/> .
|
22
22
|
<http://rubygems.org/gems/rdf> <http://usefulinc.com/ns/doap#blog> <http://blog.datagraph.org/> .
|
@@ -27,10 +27,10 @@
|
|
27
27
|
<http://rubygems.org/gems/rdf> <http://usefulinc.com/ns/doap#description> "RDF.rb is a pure-Ruby library for working with Resource Description Framework (RDF) data."@en .
|
28
28
|
<http://rubygems.org/gems/rdf> <http://usefulinc.com/ns/doap#developer> <http://ar.to/#self> .
|
29
29
|
<http://rubygems.org/gems/rdf> <http://usefulinc.com/ns/doap#developer> <http://bhuga.net/#ben> .
|
30
|
-
<http://rubygems.org/gems/rdf> <http://usefulinc.com/ns/doap#developer> <http://
|
30
|
+
<http://rubygems.org/gems/rdf> <http://usefulinc.com/ns/doap#developer> <http://greggkellogg.net/foaf.rdf#me> .
|
31
31
|
<http://rubygems.org/gems/rdf> <http://usefulinc.com/ns/doap#documenter> <http://ar.to/#self> .
|
32
32
|
<http://rubygems.org/gems/rdf> <http://usefulinc.com/ns/doap#documenter> <http://bhuga.net/#ben> .
|
33
|
-
<http://rubygems.org/gems/rdf> <http://usefulinc.com/ns/doap#documenter> <http://
|
33
|
+
<http://rubygems.org/gems/rdf> <http://usefulinc.com/ns/doap#documenter> <http://greggkellogg.net/foaf.rdf#me> .
|
34
34
|
<http://rubygems.org/gems/rdf> <http://usefulinc.com/ns/doap#download-page> <http://rubyforge.org/projects/rdf/> .
|
35
35
|
<http://rubygems.org/gems/rdf> <http://usefulinc.com/ns/doap#helper> _:genid1 .
|
36
36
|
<http://rubygems.org/gems/rdf> <http://usefulinc.com/ns/doap#helper> _:genid2 .
|
@@ -45,7 +45,7 @@
|
|
45
45
|
<http://rubygems.org/gems/rdf> <http://usefulinc.com/ns/doap#license> <http://creativecommons.org/licenses/publicdomain/> .
|
46
46
|
<http://rubygems.org/gems/rdf> <http://usefulinc.com/ns/doap#maintainer> <http://ar.to/#self> .
|
47
47
|
<http://rubygems.org/gems/rdf> <http://usefulinc.com/ns/doap#maintainer> <http://bhuga.net/#ben> .
|
48
|
-
<http://rubygems.org/gems/rdf> <http://usefulinc.com/ns/doap#maintainer> <http://
|
48
|
+
<http://rubygems.org/gems/rdf> <http://usefulinc.com/ns/doap#maintainer> <http://greggkellogg.net/foaf.rdf#me> .
|
49
49
|
<http://rubygems.org/gems/rdf> <http://usefulinc.com/ns/doap#name> "RDF.rb" .
|
50
50
|
<http://rubygems.org/gems/rdf> <http://usefulinc.com/ns/doap#platform> "Ruby" .
|
51
51
|
<http://rubygems.org/gems/rdf> <http://usefulinc.com/ns/doap#shortdesc> "A Ruby library for working with Resource Description Framework (RDF) data."@en .
|
data/lib/rdf.rb
CHANGED
data/lib/rdf/cli.rb
CHANGED
@@ -1,8 +1,75 @@
|
|
1
1
|
require 'rdf'
|
2
|
+
require 'rdf/ntriples'
|
3
|
+
require 'rdf/nquads'
|
2
4
|
require 'optparse'
|
5
|
+
begin
|
6
|
+
gem 'linkeddata'
|
7
|
+
require 'linkeddata'
|
8
|
+
rescue LoadError
|
9
|
+
# Silently load without linkeddata
|
10
|
+
end
|
11
|
+
|
12
|
+
class OptionParser
|
13
|
+
def options; @options || {}; end
|
14
|
+
def options=(value); @options = value; end
|
15
|
+
end
|
3
16
|
|
4
17
|
module RDF
|
5
18
|
class CLI
|
19
|
+
|
20
|
+
COMMANDS = {
|
21
|
+
"count" => lambda do |argv, opts|
|
22
|
+
start = Time.new
|
23
|
+
count = 0
|
24
|
+
self.parse(argv, opts) do |reader|
|
25
|
+
reader.each_statement do |statement|
|
26
|
+
count += 1
|
27
|
+
end
|
28
|
+
end
|
29
|
+
secs = Time.new - start
|
30
|
+
$stdout.puts "Parsed #{count} statements with #{@readers.join(', ')} in #{secs} seconds @ #{count/secs} statements/second."
|
31
|
+
end,
|
32
|
+
"lenghts" => lambda do |argv, opts|
|
33
|
+
self.parse(argv, opts) do |reader|
|
34
|
+
reader.each_statement do |statement|
|
35
|
+
$stdout.puts statement.to_s.size
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end,
|
39
|
+
"objects" => lambda do |argv, opts|
|
40
|
+
self.parse(argv, opts) do |reader|
|
41
|
+
reader.each_statement do |statement|
|
42
|
+
$stdout.puts statement.object.to_ntriples
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end,
|
46
|
+
"predicates" => lambda do |argv, opts|
|
47
|
+
self.parse(argv, opts) do |reader|
|
48
|
+
reader.each_statement do |statement|
|
49
|
+
$stdout.puts statement.predicate.to_ntriples
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end,
|
53
|
+
"serialize" => lambda do |argv, opts|
|
54
|
+
writer_class = RDF::Writer.for(opts[:output_format]) || RDF::NTriples::Writer
|
55
|
+
out = opts[:output] || $stdout
|
56
|
+
opts = opts.merge(:prefixes => {})
|
57
|
+
writer_opts = opts.merge(:standard_prefixes => true)
|
58
|
+
self.parse(argv, opts) do |reader|
|
59
|
+
writer_class.new(out, writer_opts) do |writer|
|
60
|
+
writer << reader
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end,
|
64
|
+
"subjects" => lambda do |argv, opts|
|
65
|
+
self.parse(argv, opts) do |reader|
|
66
|
+
reader.each_statement do |statement|
|
67
|
+
$stdout.puts statement.subject.to_ntriples
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
}
|
72
|
+
|
6
73
|
##
|
7
74
|
# @return [String]
|
8
75
|
def self.basename() File.basename($0) end
|
@@ -13,20 +80,70 @@ module RDF
|
|
13
80
|
# @return [OptionParser]
|
14
81
|
def self.options(&block)
|
15
82
|
options = OptionParser.new
|
83
|
+
opts = options.options = {
|
84
|
+
:base_uri => nil,
|
85
|
+
:canonicalize => false,
|
86
|
+
:debug => false,
|
87
|
+
:evaluate => nil,
|
88
|
+
:format => nil,
|
89
|
+
:output => $stdout,
|
90
|
+
:output_format => :ntriples,
|
91
|
+
:validate => false,
|
92
|
+
}
|
16
93
|
|
94
|
+
# Command-specific options
|
17
95
|
if block_given?
|
18
96
|
case block.arity
|
19
97
|
when 1 then block.call(options)
|
20
98
|
else options.instance_eval(&block)
|
21
99
|
end
|
22
100
|
end
|
101
|
+
options.banner ||= "Usage: #{self.basename} [options] command [args...]"
|
102
|
+
|
103
|
+
options.on('--canonicalize', 'Canonicalize input.') do
|
104
|
+
opts[:canonicalize] = true
|
105
|
+
end
|
106
|
+
|
107
|
+
options.on('-d', '--debug', 'Enable debug output for troubleshooting.') do
|
108
|
+
opts[:debug] = $DEBUG = true
|
109
|
+
end
|
110
|
+
|
111
|
+
options.on("-e", "--evaluate STRING", "Evaluate argument as RDF input, if no files are specified") do |arg|
|
112
|
+
opts[:evaluate] = arg
|
113
|
+
end
|
114
|
+
|
115
|
+
options.on("--input-format FORMAT", "Format of input file, uses heuristic if not specified") do |arg|
|
116
|
+
opts[:format] = arg.downcase.to_sym
|
117
|
+
end
|
118
|
+
|
119
|
+
options.on("-o", "--output FILE", "File to write output, defaults to STDOUT") do |arg|
|
120
|
+
opts[:output] = File.open(arg, "w")
|
121
|
+
end
|
122
|
+
|
123
|
+
options.on("--output-format FORMAT", "Format of output file, defaults to NTriples") do |arg|
|
124
|
+
opts[:output_format] = arg.downcase.to_sym
|
125
|
+
end
|
126
|
+
|
127
|
+
options.on('--uri URI', 'Base URI of input file, defaults to the filename.') do |arg|
|
128
|
+
opts[:base_uri] = arg
|
129
|
+
end
|
130
|
+
|
131
|
+
options.on('--validate', 'Validate input file.') do
|
132
|
+
opts[:validate] = true
|
133
|
+
end
|
23
134
|
|
135
|
+
options.on_tail("-h", "--help", "Show this message") do
|
136
|
+
$stdout.puts options
|
137
|
+
$stdout.puts "Available commands:\n\t#{self.commands.join("\n\t")}"
|
138
|
+
exit
|
139
|
+
end
|
140
|
+
|
24
141
|
begin
|
25
142
|
options.parse!
|
26
143
|
rescue OptionParser::InvalidOption => e
|
27
144
|
abort e
|
28
145
|
end
|
29
|
-
|
146
|
+
|
30
147
|
options
|
31
148
|
end
|
32
149
|
|
@@ -34,35 +151,45 @@ module RDF
|
|
34
151
|
# @param [String] command
|
35
152
|
# @param [Array<String>] args
|
36
153
|
# @return [Boolean]
|
37
|
-
def self.exec_command(command,
|
38
|
-
|
39
|
-
|
40
|
-
else
|
41
|
-
false
|
154
|
+
def self.exec_command(command, args, options = {})
|
155
|
+
unless COMMANDS.has_key?(command)
|
156
|
+
abort "#{File.basename($0)}: unknown command `#{command}'"
|
42
157
|
end
|
158
|
+
|
159
|
+
COMMANDS[command].call(args, options)
|
43
160
|
end
|
44
|
-
|
161
|
+
|
45
162
|
##
|
46
|
-
# @
|
47
|
-
|
48
|
-
|
49
|
-
binary = File.join(File.dirname(__FILE__), '..', '..', 'bin', 'rdf-' + command)
|
50
|
-
File.exists?(binary) ? binary : nil
|
163
|
+
# @return [Array<String>] list of executable commands
|
164
|
+
def self.commands
|
165
|
+
COMMANDS.keys
|
51
166
|
end
|
52
167
|
|
53
168
|
##
|
169
|
+
# Parse each file, STDIN or specified string in options[:evaluate] yielding
|
170
|
+
# a reader
|
171
|
+
#
|
54
172
|
# @param [Array<String>] files
|
55
|
-
# @yield [
|
56
|
-
# @yieldparam [
|
173
|
+
# @yield [reader]
|
174
|
+
# @yieldparam [RDF::Reader]
|
57
175
|
# @return [nil]
|
58
|
-
def self.
|
59
|
-
files.
|
60
|
-
|
61
|
-
|
176
|
+
def self.parse(files, options = {}, &block)
|
177
|
+
if files.empty?
|
178
|
+
# If files are empty, either use options[:execute]
|
179
|
+
input = options[:evaluate] ? StringIO.new(options[:evaluate]) : STDIN
|
180
|
+
RDF::Reader.for(options[:format] || :ntriples).new(input, options) do |reader|
|
181
|
+
yield(reader)
|
182
|
+
end
|
183
|
+
else
|
184
|
+
files.each do |file|
|
185
|
+
RDF::Reader.open(file, options) do |reader|
|
186
|
+
(@readers ||= []) << reader.class.to_s
|
187
|
+
yield(reader)
|
188
|
+
end
|
62
189
|
end
|
63
190
|
end
|
64
191
|
end
|
65
|
-
|
192
|
+
|
66
193
|
##
|
67
194
|
# @param [String] msg
|
68
195
|
# @return [void]
|