rdf 0.0.3 → 0.0.4

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 CHANGED
@@ -53,6 +53,38 @@ Documentation
53
53
 
54
54
  * <http://rdf.rubyforge.org/>
55
55
 
56
+ ### RDF Objects
57
+
58
+ * [RDF::Graph](http://rdf.rubyforge.org/RDF/Graph.html)
59
+ * [RDF::Literal](http://rdf.rubyforge.org/RDF/Literal.html)
60
+ * [RDF::Node](http://rdf.rubyforge.org/RDF/Node.html)
61
+ * [RDF::Resource](http://rdf.rubyforge.org/RDF/Resource.html)
62
+ * [RDF::Statement](http://rdf.rubyforge.org/RDF/Statement.html)
63
+ * [RDF::URI](http://rdf.rubyforge.org/RDF/URI.html)
64
+ * [RDF::Value](http://rdf.rubyforge.org/RDF/Value.html)
65
+
66
+ ### RDF Serialization
67
+
68
+ * [RDF::Reader](http://rdf.rubyforge.org/RDF/Reader.html)
69
+ * [RDF::Writer](http://rdf.rubyforge.org/RDF/Writer.html)
70
+
71
+ ### RDF Vocabularies
72
+
73
+ * [RDF::CC](http://rdf.rubyforge.org/RDF/CC.html) - Creative Commons (CC)
74
+ * [RDF::DC](http://rdf.rubyforge.org/RDF/DC.html) - Dublin Core (DC)
75
+ * [RDF::DOAP](http://rdf.rubyforge.org/RDF/DOAP.html) - Description of a Project (DOAP)
76
+ * [RDF::EXIF](http://rdf.rubyforge.org/RDF/EXIF.html) - Exchangeable Image File Format (EXIF)
77
+ * [RDF::FOAF](http://rdf.rubyforge.org/RDF/FOAF.html) - Friend of a Friend (FOAF)
78
+ * [RDF::HTTP](http://rdf.rubyforge.org/RDF/HTTP.html) - Hypertext Transfer Protocol (HTTP)
79
+ * [RDF::OWL](http://rdf.rubyforge.org/RDF/OWL.html) - Web Ontology Language (OWL)
80
+ * [RDF::RDFS](http://rdf.rubyforge.org/RDF/RDFS.html) - RDF Schema (RDFS)
81
+ * [RDF::RSS](http://rdf.rubyforge.org/RDF/RSS.html) - RDF Site Summary (RSS)
82
+ * [RDF::SIOC](http://rdf.rubyforge.org/RDF/SIOC.html) - Semantically-Interlinked Online Communities (SIOC)
83
+ * [RDF::SKOS](http://rdf.rubyforge.org/RDF/SKOS.html) - Simple Knowledge Organization System (SKOS)
84
+ * [RDF::WOT](http://rdf.rubyforge.org/RDF/WOT.html) - Web of Trust (WOT)
85
+ * [RDF::XHTML](http://rdf.rubyforge.org/RDF/XHTML.html) - Extensible HyperText Markup Language (XHTML)
86
+ * [RDF::XSD](http://rdf.rubyforge.org/RDF/XSD.html) - XML Schema (XSD)
87
+
56
88
  Download
57
89
  --------
58
90
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.4
data/bin/rdf CHANGED
@@ -1,31 +1,22 @@
1
- #!/usr/bin/env ruby -rubygems
1
+ #!/usr/bin/env ruby
2
2
  $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')))
3
- require 'rdf'
3
+ require 'rdf/cli'
4
4
 
5
- abort "Usage: #{File.basename($0)} command [args...]" if ARGV.empty?
6
-
7
- module RDF
8
- class CLI
9
- def count(*files)
10
- count = 0
11
- files.each do |file|
12
- RDF::Reader::NTriples.open(file) do |reader|
13
- reader.each do |statement|
14
- count += 1
15
- end
16
- end
17
- end
18
- puts count
19
- end
20
-
21
- def query(*args)
22
- # TODO
23
- end
24
-
25
- def method_missing(command, *args, &block)
26
- abort "#{File.basename($0)}: unknown command `#{command}'"
27
- end
5
+ 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
+ self.on('-v', '--verbose', 'Enable verbose output. May be given more than once.') do
11
+ $VERBOSE = true
12
+ end
13
+ self.on('-V', '--version', 'Display the RDF.rb version and exit.') do
14
+ puts RDF::VERSION; exit
28
15
  end
29
16
  end
30
17
 
31
- RDF::CLI.new.send(ARGV.shift, *ARGV)
18
+ abort options.banner if ARGV.empty?
19
+
20
+ unless RDF::CLI.exec_command(command = ARGV.shift, *ARGV)
21
+ abort "#{File.basename($0)}: unknown command `#{command}'"
22
+ end
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')))
3
+ require 'rdf/cli'
4
+
5
+ abort "Usage: #{RDF::CLI.basename} [files...]" if ARGV.empty?
6
+
7
+ count = 0
8
+ RDF::CLI.each_statement(*ARGV) do |statement|
9
+ count += 1
10
+ end
11
+ puts count
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+ $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')))
3
+ require 'rdf/cli'
4
+
5
+ abort "Usage: #{RDF::CLI.basename} [files...]" if ARGV.empty?
6
+
7
+ RDF::CLI.each_statement(*ARGV) do |statement|
8
+ puts statement.to_s.size
9
+ end
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+ $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')))
3
+ require 'rdf/cli'
4
+
5
+ abort "Usage: #{RDF::CLI.basename} [files...]" if ARGV.empty?
6
+
7
+ RDF::CLI.each_statement(*ARGV) do |statement|
8
+ puts statement.object
9
+ end
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+ $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')))
3
+ require 'rdf/cli'
4
+
5
+ abort "Usage: #{RDF::CLI.basename} [files...]" if ARGV.empty?
6
+
7
+ RDF::CLI.each_statement(*ARGV) do |statement|
8
+ puts statement.predicate
9
+ end
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+ $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')))
3
+ require 'rdf/cli'
4
+
5
+ abort "Usage: #{RDF::CLI.basename} [files...]" if ARGV.empty?
6
+
7
+ RDF::CLI.each_statement(*ARGV) do |statement|
8
+ puts statement.subject
9
+ end
@@ -0,0 +1 @@
1
+ require 'rdf'
data/lib/rdf.rb CHANGED
@@ -1,11 +1,21 @@
1
1
  require 'rdf/version'
2
2
 
3
3
  module RDF
4
- autoload :Reader, 'rdf/reader'
4
+ # RDF objects
5
+ autoload :Graph, 'rdf/graph'
6
+ autoload :Literal, 'rdf/literal'
7
+ autoload :Node, 'rdf/node'
8
+ autoload :Resource, 'rdf/resource'
5
9
  autoload :Statement, 'rdf/statement'
6
10
  autoload :URI, 'rdf/uri'
7
- autoload :Vocabulary, 'rdf/vocabulary'
11
+ autoload :Value, 'rdf/value'
12
+
13
+ # RDF serialization
14
+ autoload :Reader, 'rdf/reader'
15
+ autoload :Writer, 'rdf/writer'
8
16
 
17
+ # RDF vocabularies
18
+ autoload :Vocabulary, 'rdf/vocabulary'
9
19
  autoload :CC, 'rdf/vocabulary/cc'
10
20
  autoload :DC, 'rdf/vocabulary/dc'
11
21
  autoload :DOAP, 'rdf/vocabulary/doap'
@@ -21,14 +31,24 @@ module RDF
21
31
  autoload :XHTML, 'rdf/vocabulary/xhtml'
22
32
  autoload :XSD, 'rdf/vocabulary/xsd'
23
33
 
34
+ ##
35
+ # @return [String] uri
36
+ # @return [Class]
24
37
  def self.Vocabulary(uri)
25
38
  Vocabulary.create(uri)
26
39
  end
27
40
 
41
+ ##
42
+ # @return [#to_s] property
43
+ # @return [URI]
28
44
  def self.[](property)
29
45
  RDF::URI.parse([to_s, property.to_s].join)
30
46
  end
31
47
 
48
+ ##
49
+ # @param [Symbol] property
50
+ # @return [URI]
51
+ # @raise [NoMethodError]
32
52
  def self.method_missing(property, *args, &block)
33
53
  if args.empty?
34
54
  self[property]
@@ -37,6 +57,15 @@ module RDF
37
57
  end
38
58
  end
39
59
 
40
- def self.to_uri() RDF::URI.parse(to_s) end
41
- def self.to_s() "http://www.w3.org/1999/02/22-rdf-syntax-ns#" end
60
+ ##
61
+ # @return [URI]
62
+ def self.to_uri
63
+ RDF::URI.parse(to_s)
64
+ end
65
+
66
+ ##
67
+ # @return [String]
68
+ def self.to_s # FIXME
69
+ "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
70
+ end
42
71
  end
@@ -0,0 +1,73 @@
1
+ require 'rdf'
2
+ require 'optparse'
3
+
4
+ module RDF
5
+ class CLI
6
+ ##
7
+ # @return [String]
8
+ def self.basename() File.basename($0) end
9
+
10
+ ##
11
+ # @yield [options]
12
+ # @yieldparam [OptionParser]
13
+ # @return [OptionParser]
14
+ def self.options(&block)
15
+ options = OptionParser.new
16
+
17
+ if block_given?
18
+ case block.arity
19
+ when 1 then block.call(options)
20
+ else options.instance_eval(&block)
21
+ end
22
+ end
23
+
24
+ begin
25
+ options.parse!
26
+ rescue OptionParser::InvalidOption => e
27
+ abort e
28
+ end
29
+
30
+ options
31
+ end
32
+
33
+ ##
34
+ # @param [String] command
35
+ # @param [Array<String>] args
36
+ # @return [Boolean]
37
+ def self.exec_command(command, *args)
38
+ if binary = RDF::CLI.find_binary(command)
39
+ exec(binary, *args)
40
+ else
41
+ false
42
+ end
43
+ end
44
+
45
+ ##
46
+ # @param [String] command
47
+ # @return [String, nil]
48
+ def self.find_binary(command)
49
+ binary = File.join(File.dirname(__FILE__), '..', '..', 'bin', 'rdf-' + command)
50
+ File.exists?(binary) ? binary : nil
51
+ end
52
+
53
+ ##
54
+ # @param [Array<String>] files
55
+ # @yield [statement]
56
+ # @yieldparam [Statement]
57
+ # @return [nil]
58
+ def self.each_statement(*files, &block)
59
+ files.each do |file|
60
+ RDF::Reader::NTriples.open(file) do |reader|
61
+ reader.each_statement(&block)
62
+ end
63
+ end
64
+ end
65
+
66
+ ##
67
+ # @param [String] msg
68
+ # @return [void]
69
+ def self.abort(msg)
70
+ Kernel.abort "#{basename}: #{msg}"
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,189 @@
1
+ module RDF
2
+ ##
3
+ # An RDF graph.
4
+ class Graph < Resource
5
+ include Enumerable
6
+
7
+ # @return [URI]
8
+ attr_accessor :uri
9
+
10
+ # @return [Array<Statement>]
11
+ attr_accessor :data
12
+
13
+ ##
14
+ # @param [URI] uri
15
+ # @yield [graph]
16
+ # @yieldparam [Graph]
17
+ def initialize(uri = nil, options = {}, &block)
18
+ @uri, @options = uri, options
19
+
20
+ if block_given?
21
+ case block.arity
22
+ when 1 then block.call(self)
23
+ else instance_eval(&block)
24
+ end
25
+ end
26
+ end
27
+
28
+ ##
29
+ # @return [Boolean]
30
+ def named?() !unnamed? end
31
+
32
+ ##
33
+ # @return [Boolean]
34
+ def unnamed?() uri.nil? end
35
+
36
+ ##
37
+ # @return [Integer]
38
+ def size() @data.size end
39
+
40
+ ##
41
+ # @yield [statement]
42
+ # @yieldparam [Array<Statement>]
43
+ # @return [Graph]
44
+ def each(&block)
45
+ each_statement(&block)
46
+ end
47
+
48
+ ##
49
+ # @yield [statement]
50
+ # @yieldparam [Array<Statement>]
51
+ # @return [Graph]
52
+ def each_statement(&block)
53
+ @data.each(&block)
54
+ self
55
+ end
56
+
57
+ ##
58
+ # @yield [triple]
59
+ # @yieldparam [Array<Value>]
60
+ # @return [Graph]
61
+ def each_triple(&block)
62
+ @data.each do |statement|
63
+ block.call(*statement.to_triple)
64
+ end
65
+ self
66
+ end
67
+
68
+ ##
69
+ # @yield [quad]
70
+ # @yieldparam [Array<Value>]
71
+ # @return [Graph]
72
+ def each_quad(&block)
73
+ @data.each do |statement|
74
+ block.call(*statement.to_quad) # FIXME?
75
+ end
76
+ self
77
+ end
78
+
79
+ ##
80
+ # @yield [context]
81
+ # @yieldparam [Resource]
82
+ # @return [Graph]
83
+ def each_context(&block)
84
+ block.call(uri) unless unnamed?
85
+ self
86
+ end
87
+
88
+ ##
89
+ # @yield [subject]
90
+ # @yieldparam [Resource]
91
+ # @return [Graph]
92
+ def each_subject(&block)
93
+ @data.each do |statement|
94
+ block.call(statement.subject)
95
+ end
96
+ self
97
+ end
98
+
99
+ ##
100
+ # @yield [predicate]
101
+ # @yieldparam [URI]
102
+ # @return [Graph]
103
+ def each_predicate(&block)
104
+ @data.each do |statement|
105
+ block.call(statement.predicate)
106
+ end
107
+ self
108
+ end
109
+
110
+ ##
111
+ # @yield [object]
112
+ # @yieldparam [Value]
113
+ # @return [Graph]
114
+ def each_object(&block)
115
+ @data.each do |statement|
116
+ block.call(statement.object)
117
+ end
118
+ self
119
+ end
120
+
121
+ ##
122
+ # @return [Array<Statement>]
123
+ def statements(&block)
124
+ block_given? ? each_statement(&block) : @data
125
+ end
126
+
127
+ ##
128
+ # @return [Array<Array>]
129
+ def triples(&block)
130
+ block_given? ? each_triple(&block) : map { |statement| statement.to_triple }
131
+ end
132
+
133
+ ##
134
+ # @return [Array<Array>]
135
+ def quads(&block)
136
+ block_given? ? each_quad(&block) : map { |statement| statement.to_quad }
137
+ end
138
+
139
+ ##
140
+ # @return [Resource]
141
+ def context() uri end
142
+
143
+ ##
144
+ # @return [Array<Resource>]
145
+ def contexts
146
+ block_given? ? each_context(&block) : (named? ? [uri] : [])
147
+ end
148
+
149
+ ##
150
+ # @return [Array<Resource>]
151
+ def subjects
152
+ block_given? ? each_subject(&block) : map(&:subject)
153
+ end
154
+
155
+ ##
156
+ # @return [Array<URI>]
157
+ def predicates
158
+ block_given? ? each_predicate(&block) : map(&:predicate)
159
+ end
160
+
161
+ ##
162
+ # @return [Array<Value>]
163
+ def objects
164
+ block_given? ? each_object(&block) : map(&:object)
165
+ end
166
+
167
+ ##
168
+ # @param [Statement, Array]
169
+ # @return [Graph]
170
+ def <<(statement)
171
+ @data << case statement
172
+ when Array then Statement.new(*statement)
173
+ when Statement then statement
174
+ else statement
175
+ end
176
+ self
177
+ end
178
+
179
+ ##
180
+ # @return [URI]
181
+ def to_uri() uri end
182
+
183
+ ##
184
+ # @return [String]
185
+ def to_s
186
+ named? ? uri.to_s : "<>"
187
+ end
188
+ end
189
+ end