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 +32 -0
- data/VERSION +1 -1
- data/bin/rdf +17 -26
- data/bin/rdf-count +11 -0
- data/bin/rdf-lengths +9 -0
- data/bin/rdf-objects +9 -0
- data/bin/rdf-predicates +9 -0
- data/bin/rdf-subjects +9 -0
- data/lib/df.rb +1 -0
- data/lib/rdf.rb +33 -4
- data/lib/rdf/cli.rb +73 -0
- data/lib/rdf/graph.rb +189 -0
- data/lib/rdf/literal.rb +96 -0
- data/lib/rdf/node.rb +32 -0
- data/lib/rdf/reader.rb +52 -5
- data/lib/rdf/reader/ntriples.rb +23 -15
- data/lib/rdf/resource.rb +10 -0
- data/lib/rdf/statement.rb +76 -8
- data/lib/rdf/uri.rb +65 -1
- data/lib/rdf/value.rb +25 -0
- data/lib/rdf/version.rb +6 -1
- data/lib/rdf/vocabulary.rb +42 -7
- data/lib/rdf/vocabulary/cc.rb +3 -1
- data/lib/rdf/vocabulary/dc.rb +3 -1
- data/lib/rdf/vocabulary/doap.rb +3 -1
- data/lib/rdf/vocabulary/exif.rb +3 -1
- data/lib/rdf/vocabulary/foaf.rb +3 -1
- data/lib/rdf/vocabulary/http.rb +3 -1
- data/lib/rdf/vocabulary/owl.rb +3 -1
- data/lib/rdf/vocabulary/rdf.rb +3 -1
- data/lib/rdf/vocabulary/rdfs.rb +3 -1
- data/lib/rdf/vocabulary/rss.rb +3 -1
- data/lib/rdf/vocabulary/sioc.rb +3 -1
- data/lib/rdf/vocabulary/skos.rb +3 -1
- data/lib/rdf/vocabulary/wot.rb +3 -1
- data/lib/rdf/vocabulary/xhtml.rb +3 -1
- data/lib/rdf/vocabulary/xsd.rb +3 -1
- data/lib/rdf/writer.rb +220 -0
- data/lib/rdf/writer/ntriples.rb +50 -0
- metadata +17 -2
data/lib/rdf/uri.rb
CHANGED
@@ -1,5 +1,69 @@
|
|
1
1
|
require 'addressable/uri'
|
2
2
|
|
3
3
|
module RDF
|
4
|
-
|
4
|
+
##
|
5
|
+
# A Uniform Resource Identifier (URI).
|
6
|
+
class URI < Node
|
7
|
+
##
|
8
|
+
# @param [String] uri
|
9
|
+
# @return [URI]
|
10
|
+
def self.parse(uri)
|
11
|
+
self.new(uri)
|
12
|
+
end
|
13
|
+
|
14
|
+
##
|
15
|
+
# @param [URI, String, #to_s] uri
|
16
|
+
def initialize(uri)
|
17
|
+
@uri = case uri
|
18
|
+
when Addressable::URI then uri
|
19
|
+
else Addressable::URI.parse(uri.to_s)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
##
|
24
|
+
# @return [Boolean]
|
25
|
+
def anonymous?
|
26
|
+
false
|
27
|
+
end
|
28
|
+
|
29
|
+
##
|
30
|
+
# @param [URI] other
|
31
|
+
# @return [Boolean]
|
32
|
+
def eql?(other)
|
33
|
+
other.is_a?(URI) && self == other
|
34
|
+
end
|
35
|
+
|
36
|
+
##
|
37
|
+
# @param [Object] other
|
38
|
+
# @return [Boolean]
|
39
|
+
def ==(other)
|
40
|
+
other.respond_to?(:to_uri) && @uri == other.to_uri
|
41
|
+
end
|
42
|
+
|
43
|
+
##
|
44
|
+
# @return [URI]
|
45
|
+
def to_uri
|
46
|
+
@uri
|
47
|
+
end
|
48
|
+
|
49
|
+
##
|
50
|
+
# @return [String]
|
51
|
+
def to_s
|
52
|
+
@uri.to_s
|
53
|
+
end
|
54
|
+
|
55
|
+
protected
|
56
|
+
|
57
|
+
def respond_to?(symbol) #:nodoc:
|
58
|
+
@uri.respond_to?(symbol) || super
|
59
|
+
end
|
60
|
+
|
61
|
+
def method_missing(symbol, *args, &block) #:nodoc:
|
62
|
+
if @uri.respond_to?(symbol)
|
63
|
+
@uri.send(symbol, *args, &block)
|
64
|
+
else
|
65
|
+
super
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
5
69
|
end
|
data/lib/rdf/value.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
module RDF
|
2
|
+
##
|
3
|
+
# An RDF value.
|
4
|
+
#
|
5
|
+
# @abstract
|
6
|
+
class Value
|
7
|
+
# Prevent instantiation of this class.
|
8
|
+
private_class_method :new
|
9
|
+
|
10
|
+
##
|
11
|
+
# @return [String]
|
12
|
+
def inspect
|
13
|
+
sprintf("#<%s:%#0x(%s)>", self.class.name, object_id, to_s)
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def self.inherited(child) #:nodoc:
|
19
|
+
# Enable instantiation of subclasses.
|
20
|
+
child.send(:public_class_method, :new)
|
21
|
+
super
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
data/lib/rdf/version.rb
CHANGED
@@ -2,13 +2,18 @@ module RDF
|
|
2
2
|
module VERSION
|
3
3
|
MAJOR = 0
|
4
4
|
MINOR = 0
|
5
|
-
TINY =
|
5
|
+
TINY = 4
|
6
6
|
EXTRA = nil
|
7
7
|
|
8
8
|
STRING = [MAJOR, MINOR, TINY].join('.')
|
9
9
|
STRING << "-#{EXTRA}" if EXTRA
|
10
10
|
|
11
|
+
##
|
12
|
+
# @return [String]
|
11
13
|
def self.to_s() STRING end
|
14
|
+
|
15
|
+
##
|
16
|
+
# @return [String]
|
12
17
|
def self.to_str() STRING end
|
13
18
|
end
|
14
19
|
end
|
data/lib/rdf/vocabulary.rb
CHANGED
@@ -2,7 +2,8 @@ module RDF
|
|
2
2
|
##
|
3
3
|
# An RDF vocabulary.
|
4
4
|
class Vocabulary
|
5
|
-
|
5
|
+
##
|
6
|
+
# @return [String]
|
6
7
|
def self.inspect
|
7
8
|
if self == Vocabulary
|
8
9
|
self.to_s
|
@@ -11,15 +12,32 @@ module RDF
|
|
11
12
|
end
|
12
13
|
end
|
13
14
|
|
14
|
-
|
15
|
-
|
15
|
+
##
|
16
|
+
# @return [URI]
|
17
|
+
def self.to_uri
|
18
|
+
RDF::URI.parse(to_s)
|
19
|
+
end
|
20
|
+
|
21
|
+
##
|
22
|
+
# @return [String]
|
23
|
+
def self.to_s
|
24
|
+
@@uris.has_key?(self) ? @@uris[self].to_s : super
|
25
|
+
end
|
16
26
|
|
27
|
+
##
|
28
|
+
# @param [#to_s] property
|
29
|
+
# @return [URI]
|
17
30
|
def self.[](property)
|
18
31
|
RDF::URI.parse([to_s, property.to_s].join(''))
|
19
32
|
end
|
20
33
|
|
34
|
+
##
|
35
|
+
# @param [Symbol]
|
36
|
+
# @return [void]
|
21
37
|
def self.property(symbol) end # TODO
|
22
38
|
|
39
|
+
##
|
40
|
+
# @param [URI, String]
|
23
41
|
def initialize(uri)
|
24
42
|
case uri
|
25
43
|
when RDF::URI then @uri = uri.to_s
|
@@ -27,13 +45,25 @@ module RDF
|
|
27
45
|
end
|
28
46
|
end
|
29
47
|
|
48
|
+
##
|
49
|
+
# @return [String]
|
30
50
|
def inspect
|
31
51
|
sprintf("#<%s:%#0x(%s)>", self.class.name, object_id, to_s)
|
32
52
|
end
|
33
53
|
|
34
|
-
|
35
|
-
|
54
|
+
##
|
55
|
+
# @return [URI]
|
56
|
+
def to_uri
|
57
|
+
RDF::URI.parse(to_s)
|
58
|
+
end
|
59
|
+
|
60
|
+
##
|
61
|
+
# @return [String]
|
62
|
+
def to_s() @uri.to_s end
|
36
63
|
|
64
|
+
##
|
65
|
+
# @param [#to_s] property
|
66
|
+
# @return [URI]
|
37
67
|
def [](property)
|
38
68
|
RDF::URI.parse([to_s, property.to_s].join(''))
|
39
69
|
end
|
@@ -42,14 +72,16 @@ module RDF
|
|
42
72
|
@@uris = {}
|
43
73
|
@@uri = nil
|
44
74
|
|
75
|
+
##
|
45
76
|
# @private
|
46
|
-
def self.create(uri)
|
77
|
+
def self.create(uri) #:nodoc:
|
47
78
|
@@uri = uri
|
48
79
|
self
|
49
80
|
end
|
50
81
|
|
82
|
+
##
|
51
83
|
# @private
|
52
|
-
def self.inherited(subclass)
|
84
|
+
def self.inherited(subclass) #:nodoc:
|
53
85
|
unless @@uri.nil?
|
54
86
|
subclass.send(:private_class_method, :new)
|
55
87
|
@@uris[subclass] = @@uri
|
@@ -75,6 +107,9 @@ module RDF
|
|
75
107
|
|
76
108
|
end
|
77
109
|
|
110
|
+
##
|
111
|
+
# @param [String] uri
|
112
|
+
# @return [Class]
|
78
113
|
def self.Vocabulary(uri)
|
79
114
|
Vocabulary.create(uri)
|
80
115
|
end
|
data/lib/rdf/vocabulary/cc.rb
CHANGED
data/lib/rdf/vocabulary/dc.rb
CHANGED
data/lib/rdf/vocabulary/doap.rb
CHANGED
data/lib/rdf/vocabulary/exif.rb
CHANGED
data/lib/rdf/vocabulary/foaf.rb
CHANGED
data/lib/rdf/vocabulary/http.rb
CHANGED
data/lib/rdf/vocabulary/owl.rb
CHANGED
data/lib/rdf/vocabulary/rdf.rb
CHANGED
data/lib/rdf/vocabulary/rdfs.rb
CHANGED
data/lib/rdf/vocabulary/rss.rb
CHANGED
data/lib/rdf/vocabulary/sioc.rb
CHANGED
data/lib/rdf/vocabulary/skos.rb
CHANGED
data/lib/rdf/vocabulary/wot.rb
CHANGED
data/lib/rdf/vocabulary/xhtml.rb
CHANGED
data/lib/rdf/vocabulary/xsd.rb
CHANGED
data/lib/rdf/writer.rb
ADDED
@@ -0,0 +1,220 @@
|
|
1
|
+
module RDF
|
2
|
+
##
|
3
|
+
# An RDF serializer.
|
4
|
+
#
|
5
|
+
# @abstract
|
6
|
+
class Writer
|
7
|
+
autoload :NTriples, 'rdf/writer/ntriples'
|
8
|
+
|
9
|
+
include Enumerable
|
10
|
+
|
11
|
+
@@subclasses = []
|
12
|
+
@@file_extensions = {}
|
13
|
+
@@content_types = {}
|
14
|
+
@@content_encoding = {}
|
15
|
+
|
16
|
+
##
|
17
|
+
# Enumerates known RDF writer classes.
|
18
|
+
#
|
19
|
+
# @yield [klass]
|
20
|
+
# @yieldparam [Class]
|
21
|
+
def self.each(&block)
|
22
|
+
!block_given? ? @@subclasses : @@subclasses.each { |klass| yield klass }
|
23
|
+
end
|
24
|
+
|
25
|
+
##
|
26
|
+
# Returns the list of known MIME content types.
|
27
|
+
def self.content_types
|
28
|
+
@@content_types
|
29
|
+
end
|
30
|
+
|
31
|
+
##
|
32
|
+
# Returns the list of known file extensions.
|
33
|
+
def self.file_extensions
|
34
|
+
@@file_extensions
|
35
|
+
end
|
36
|
+
|
37
|
+
##
|
38
|
+
# Returns the RDF writer class for the given format.
|
39
|
+
def self.for(format)
|
40
|
+
klass = case format.to_s.downcase.to_sym
|
41
|
+
when :ntriples then RDF::Writer::NTriples
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.buffer(*args, &block)
|
46
|
+
require 'stringio' unless defined?(StringIO)
|
47
|
+
|
48
|
+
StringIO.open do |buffer|
|
49
|
+
self.new(buffer, *args) { |writer| block.call(writer) }
|
50
|
+
buffer.string
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.open(filename, options = {}, &block)
|
55
|
+
options[:format] ||= :ntriples # FIXME
|
56
|
+
|
57
|
+
File.open(filename, 'wb') do |file|
|
58
|
+
self.for(options[:format]).new(file, options, &block)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def initialize(output = $stdout, options = {}, &block)
|
63
|
+
@output, @options = output, options
|
64
|
+
@nodes, @node_id = {}, 0
|
65
|
+
|
66
|
+
if block_given?
|
67
|
+
write_prologue
|
68
|
+
block.call(self)
|
69
|
+
write_epilogue
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
##
|
74
|
+
# @abstract
|
75
|
+
def write_prologue() end
|
76
|
+
|
77
|
+
##
|
78
|
+
# @abstract
|
79
|
+
def write_epilogue() end
|
80
|
+
|
81
|
+
##
|
82
|
+
# @abstract
|
83
|
+
def write_comment(text) end
|
84
|
+
|
85
|
+
##
|
86
|
+
# @raise [ArgumentError]
|
87
|
+
def <<(data)
|
88
|
+
case data # TODO
|
89
|
+
#when Graph
|
90
|
+
# write_graph(data)
|
91
|
+
#when Resource
|
92
|
+
# #register!(resource) && write_node(resource)
|
93
|
+
# write_resource(data)
|
94
|
+
when Statement
|
95
|
+
write_statement(data)
|
96
|
+
else
|
97
|
+
if data.respond_to?(:to_a)
|
98
|
+
write_triple(*data.to_a)
|
99
|
+
else
|
100
|
+
raise ArgumentError.new("expected RDF::Statement or RDF::Resource, got #{data.inspect}")
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
##
|
106
|
+
# @param [Graph] graph
|
107
|
+
def write_graph(graph)
|
108
|
+
write_triples(*graph.triples)
|
109
|
+
end
|
110
|
+
|
111
|
+
def write_resource(subject) # FIXME
|
112
|
+
edge_nodes = []
|
113
|
+
subject.each do |predicate, objects|
|
114
|
+
[objects].flatten.each do |object|
|
115
|
+
edge_nodes << object if register!(object)
|
116
|
+
write_triple subject, predicate, object
|
117
|
+
end
|
118
|
+
end
|
119
|
+
edge_nodes.each { |node| write_resource node }
|
120
|
+
end
|
121
|
+
|
122
|
+
##
|
123
|
+
# @param [Array<Statement>] statements
|
124
|
+
def write_statements(*statements)
|
125
|
+
statements.flatten.each { |stmt| write_statement(stmt) }
|
126
|
+
end
|
127
|
+
|
128
|
+
##
|
129
|
+
# @param [Statement] statement
|
130
|
+
def write_statement(statement)
|
131
|
+
write_triple(*statement.to_a)
|
132
|
+
end
|
133
|
+
|
134
|
+
##
|
135
|
+
# @param [Array<Array>] triples
|
136
|
+
def write_triples(*triples)
|
137
|
+
triples.each { |triple| write_triple(*triple) }
|
138
|
+
end
|
139
|
+
|
140
|
+
##
|
141
|
+
# @param [Resource] subject
|
142
|
+
# @param [URI] predicate
|
143
|
+
# @param [Value] object
|
144
|
+
# @raise [NotImplementedError] unless implemented in subclass
|
145
|
+
# @abstract
|
146
|
+
def write_triple(subject, predicate, object)
|
147
|
+
raise NotImplementedError # override in subclasses
|
148
|
+
end
|
149
|
+
|
150
|
+
protected
|
151
|
+
|
152
|
+
def self.inherited(child) #:nodoc:
|
153
|
+
@@subclasses << child
|
154
|
+
super
|
155
|
+
end
|
156
|
+
|
157
|
+
def self.content_type(type, options = {})
|
158
|
+
@@content_types[type] ||= []
|
159
|
+
@@content_types[type] << self
|
160
|
+
|
161
|
+
if options[:extension]
|
162
|
+
extensions = [options[:extension]].flatten.map { |ext| ext.to_sym }
|
163
|
+
extensions.each { |ext| @@file_extensions[ext] = type }
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
def self.content_encoding(encoding)
|
168
|
+
@@content_encoding[self] = encoding.to_sym
|
169
|
+
end
|
170
|
+
|
171
|
+
def puts(*args)
|
172
|
+
@output.puts(*args)
|
173
|
+
end
|
174
|
+
|
175
|
+
##
|
176
|
+
# @param [Resource] uriref
|
177
|
+
# @return [String]
|
178
|
+
def uri_for(uriref)
|
179
|
+
if uriref.respond_to?(:anonymous?) && uriref.anonymous?
|
180
|
+
@nodes[uriref]
|
181
|
+
elsif uriref.respond_to?(:to_uri)
|
182
|
+
uriref.to_uri.to_s
|
183
|
+
else
|
184
|
+
uriref.to_s
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
##
|
189
|
+
# @return [String]
|
190
|
+
def node_id
|
191
|
+
"_:n#{@node_id += 1}"
|
192
|
+
end
|
193
|
+
|
194
|
+
def register!(resource)
|
195
|
+
if resource.kind_of?(RDF::Resource)
|
196
|
+
unless @nodes[resource] # have we already seen it?
|
197
|
+
@nodes[resource] = resource.uri || node_id
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
##
|
203
|
+
# @param [String] string
|
204
|
+
# @return [String]
|
205
|
+
def escaped(string)
|
206
|
+
string.gsub("\\", "\\\\").gsub("\t", "\\\t").
|
207
|
+
gsub("\n", "\\\n").gsub("\r", "\\\r").gsub("\"", "\\\"")
|
208
|
+
end
|
209
|
+
|
210
|
+
##
|
211
|
+
# @param [String] string
|
212
|
+
# @return [String]
|
213
|
+
def quoted(string)
|
214
|
+
"\"#{string}\""
|
215
|
+
end
|
216
|
+
|
217
|
+
end
|
218
|
+
|
219
|
+
class WriterError < IOError; end
|
220
|
+
end
|