openlogic-rdf 0.3.6
Sign up to get free protection for your applications and to get access to all the features.
- data/AUTHORS +3 -0
- data/CREDITS +9 -0
- data/README +361 -0
- data/UNLICENSE +24 -0
- data/VERSION +1 -0
- data/bin/rdf +18 -0
- data/etc/doap.nt +62 -0
- data/lib/df.rb +1 -0
- data/lib/rdf/cli.rb +200 -0
- data/lib/rdf/format.rb +383 -0
- data/lib/rdf/mixin/countable.rb +39 -0
- data/lib/rdf/mixin/durable.rb +31 -0
- data/lib/rdf/mixin/enumerable.rb +637 -0
- data/lib/rdf/mixin/indexable.rb +26 -0
- data/lib/rdf/mixin/inferable.rb +5 -0
- data/lib/rdf/mixin/mutable.rb +191 -0
- data/lib/rdf/mixin/queryable.rb +265 -0
- data/lib/rdf/mixin/readable.rb +15 -0
- data/lib/rdf/mixin/type_check.rb +21 -0
- data/lib/rdf/mixin/writable.rb +152 -0
- data/lib/rdf/model/graph.rb +263 -0
- data/lib/rdf/model/list.rb +731 -0
- data/lib/rdf/model/literal/boolean.rb +121 -0
- data/lib/rdf/model/literal/date.rb +73 -0
- data/lib/rdf/model/literal/datetime.rb +72 -0
- data/lib/rdf/model/literal/decimal.rb +86 -0
- data/lib/rdf/model/literal/double.rb +189 -0
- data/lib/rdf/model/literal/integer.rb +126 -0
- data/lib/rdf/model/literal/numeric.rb +184 -0
- data/lib/rdf/model/literal/time.rb +87 -0
- data/lib/rdf/model/literal/token.rb +47 -0
- data/lib/rdf/model/literal/xml.rb +39 -0
- data/lib/rdf/model/literal.rb +373 -0
- data/lib/rdf/model/node.rb +156 -0
- data/lib/rdf/model/resource.rb +28 -0
- data/lib/rdf/model/statement.rb +296 -0
- data/lib/rdf/model/term.rb +77 -0
- data/lib/rdf/model/uri.rb +570 -0
- data/lib/rdf/model/value.rb +133 -0
- data/lib/rdf/nquads.rb +152 -0
- data/lib/rdf/ntriples/format.rb +48 -0
- data/lib/rdf/ntriples/reader.rb +239 -0
- data/lib/rdf/ntriples/writer.rb +219 -0
- data/lib/rdf/ntriples.rb +104 -0
- data/lib/rdf/query/pattern.rb +329 -0
- data/lib/rdf/query/solution.rb +252 -0
- data/lib/rdf/query/solutions.rb +237 -0
- data/lib/rdf/query/variable.rb +221 -0
- data/lib/rdf/query.rb +404 -0
- data/lib/rdf/reader.rb +511 -0
- data/lib/rdf/repository.rb +389 -0
- data/lib/rdf/transaction.rb +161 -0
- data/lib/rdf/util/aliasing.rb +63 -0
- data/lib/rdf/util/cache.rb +139 -0
- data/lib/rdf/util/file.rb +38 -0
- data/lib/rdf/util/uuid.rb +36 -0
- data/lib/rdf/util.rb +6 -0
- data/lib/rdf/version.rb +19 -0
- data/lib/rdf/vocab/cc.rb +18 -0
- data/lib/rdf/vocab/cert.rb +13 -0
- data/lib/rdf/vocab/dc.rb +63 -0
- data/lib/rdf/vocab/dc11.rb +23 -0
- data/lib/rdf/vocab/doap.rb +45 -0
- data/lib/rdf/vocab/exif.rb +168 -0
- data/lib/rdf/vocab/foaf.rb +69 -0
- data/lib/rdf/vocab/geo.rb +13 -0
- data/lib/rdf/vocab/http.rb +26 -0
- data/lib/rdf/vocab/owl.rb +59 -0
- data/lib/rdf/vocab/rdfs.rb +17 -0
- data/lib/rdf/vocab/rsa.rb +12 -0
- data/lib/rdf/vocab/rss.rb +14 -0
- data/lib/rdf/vocab/sioc.rb +93 -0
- data/lib/rdf/vocab/skos.rb +36 -0
- data/lib/rdf/vocab/wot.rb +21 -0
- data/lib/rdf/vocab/xhtml.rb +9 -0
- data/lib/rdf/vocab/xsd.rb +58 -0
- data/lib/rdf/vocab.rb +215 -0
- data/lib/rdf/writer.rb +475 -0
- data/lib/rdf.rb +192 -0
- metadata +173 -0
@@ -0,0 +1,296 @@
|
|
1
|
+
module RDF
|
2
|
+
##
|
3
|
+
# An RDF statement.
|
4
|
+
#
|
5
|
+
# @example Creating an RDF statement
|
6
|
+
# s = RDF::URI.new("http://rubygems.org/gems/rdf")
|
7
|
+
# p = RDF::DC.creator
|
8
|
+
# o = RDF::URI.new("http://ar.to/#self")
|
9
|
+
# RDF::Statement.new(s, p, o)
|
10
|
+
#
|
11
|
+
# @example Creating an RDF statement with a context
|
12
|
+
# RDF::Statement.new(s, p, o, :context => uri)
|
13
|
+
#
|
14
|
+
# @example Creating an RDF statement from a `Hash`
|
15
|
+
# RDF::Statement.new({
|
16
|
+
# :subject => RDF::URI.new("http://rubygems.org/gems/rdf"),
|
17
|
+
# :predicate => RDF::DC.creator,
|
18
|
+
# :object => RDF::URI.new("http://ar.to/#self"),
|
19
|
+
# })
|
20
|
+
#
|
21
|
+
class Statement
|
22
|
+
include RDF::Value
|
23
|
+
|
24
|
+
##
|
25
|
+
# @private
|
26
|
+
# @since 0.2.2
|
27
|
+
def self.from(statement, options = {})
|
28
|
+
case statement
|
29
|
+
when Array, Query::Pattern
|
30
|
+
context = statement[3] == false ? nil : statement[3]
|
31
|
+
self.new(statement[0], statement[1], statement[2], options.merge(:context => context))
|
32
|
+
when Statement then statement
|
33
|
+
when Hash then self.new(options.merge(statement))
|
34
|
+
else raise ArgumentError, "expected RDF::Statement, Hash, or Array, but got #{statement.inspect}"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# @return [Object]
|
39
|
+
attr_accessor :id
|
40
|
+
|
41
|
+
# @return [RDF::Resource]
|
42
|
+
attr_accessor :context
|
43
|
+
|
44
|
+
# @return [RDF::Resource]
|
45
|
+
attr_accessor :subject
|
46
|
+
|
47
|
+
# @return [RDF::URI]
|
48
|
+
attr_accessor :predicate
|
49
|
+
|
50
|
+
# @return [RDF::Term]
|
51
|
+
attr_accessor :object
|
52
|
+
|
53
|
+
##
|
54
|
+
# @overload initialize(options = {})
|
55
|
+
# @param [Hash{Symbol => Object}] options
|
56
|
+
# @option options [RDF::Resource] :subject (nil)
|
57
|
+
# @option options [RDF::URI] :predicate (nil)
|
58
|
+
# @option options [RDF::Term] :object (nil)
|
59
|
+
# @option options [RDF::Resource] :context (nil)
|
60
|
+
#
|
61
|
+
# @overload initialize(subject, predicate, object, options = {})
|
62
|
+
# @param [RDF::Resource] subject
|
63
|
+
# @param [RDF::URI] predicate
|
64
|
+
# @param [RDF::Term] object
|
65
|
+
# @param [Hash{Symbol => Object}] options
|
66
|
+
# @option options [RDF::Resource] :context (nil)
|
67
|
+
def initialize(subject = nil, predicate = nil, object = nil, options = {})
|
68
|
+
case subject
|
69
|
+
when Hash
|
70
|
+
@options = subject.dup
|
71
|
+
@subject = @options.delete(:subject)
|
72
|
+
@predicate = @options.delete(:predicate)
|
73
|
+
@object = @options.delete(:object)
|
74
|
+
else
|
75
|
+
@options = !options.empty? ? options.dup : {}
|
76
|
+
@subject = subject
|
77
|
+
@predicate = predicate
|
78
|
+
@object = object
|
79
|
+
end
|
80
|
+
@id = @options.delete(:id) if @options.has_key?(:id)
|
81
|
+
@context = @options.delete(:context)
|
82
|
+
initialize!
|
83
|
+
end
|
84
|
+
|
85
|
+
##
|
86
|
+
# @private
|
87
|
+
def initialize!
|
88
|
+
@context = Node.intern(@context) if @context.is_a?(Symbol)
|
89
|
+
@subject = Node.intern(@subject) if @subject.is_a?(Symbol)
|
90
|
+
@predicate = Node.intern(@predicate) if @predicate.is_a?(Symbol)
|
91
|
+
@object = case @object
|
92
|
+
when nil then nil
|
93
|
+
when Symbol then Node.intern(@object)
|
94
|
+
when Term then @object
|
95
|
+
else Literal.new(@object)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
##
|
100
|
+
# Returns `true` to indicate that this value is a statement.
|
101
|
+
#
|
102
|
+
# @return [Boolean]
|
103
|
+
def statement?
|
104
|
+
true
|
105
|
+
end
|
106
|
+
|
107
|
+
##
|
108
|
+
# @return [Boolean]
|
109
|
+
def invalid?
|
110
|
+
!valid?
|
111
|
+
end
|
112
|
+
|
113
|
+
##
|
114
|
+
# @return [Boolean]
|
115
|
+
def valid?
|
116
|
+
has_subject? && has_predicate? && has_object?
|
117
|
+
end
|
118
|
+
|
119
|
+
##
|
120
|
+
# @return [Boolean]
|
121
|
+
def asserted?
|
122
|
+
!quoted?
|
123
|
+
end
|
124
|
+
|
125
|
+
##
|
126
|
+
# @return [Boolean]
|
127
|
+
def quoted?
|
128
|
+
false
|
129
|
+
end
|
130
|
+
|
131
|
+
##
|
132
|
+
# @return [Boolean]
|
133
|
+
def inferred?
|
134
|
+
false
|
135
|
+
end
|
136
|
+
|
137
|
+
##
|
138
|
+
# @return [Boolean]
|
139
|
+
def has_graph?
|
140
|
+
has_context?
|
141
|
+
end
|
142
|
+
|
143
|
+
##
|
144
|
+
# @return [Boolean]
|
145
|
+
def has_context?
|
146
|
+
!!context
|
147
|
+
end
|
148
|
+
|
149
|
+
##
|
150
|
+
# @return [Boolean]
|
151
|
+
def has_subject?
|
152
|
+
!!subject
|
153
|
+
end
|
154
|
+
|
155
|
+
##
|
156
|
+
# @return [Boolean]
|
157
|
+
def has_predicate?
|
158
|
+
!!predicate
|
159
|
+
end
|
160
|
+
|
161
|
+
##
|
162
|
+
# @return [Boolean]
|
163
|
+
def has_object?
|
164
|
+
!!object
|
165
|
+
end
|
166
|
+
|
167
|
+
##
|
168
|
+
# Returns `true` if the subject or object of this statement is a blank
|
169
|
+
# node.
|
170
|
+
#
|
171
|
+
# @return [Boolean]
|
172
|
+
def has_blank_nodes?
|
173
|
+
(has_object? && object.node?) || (has_subject? && subject.node?)
|
174
|
+
end
|
175
|
+
|
176
|
+
##
|
177
|
+
# @param [Statement] other
|
178
|
+
# @return [Boolean]
|
179
|
+
def eql?(other)
|
180
|
+
other.is_a?(Statement) && self == other && (self.context || false) == (other.context || false)
|
181
|
+
end
|
182
|
+
|
183
|
+
##
|
184
|
+
# @param [Object] other
|
185
|
+
# @return [Boolean]
|
186
|
+
def ==(other)
|
187
|
+
to_a == other.to_a
|
188
|
+
end
|
189
|
+
|
190
|
+
##
|
191
|
+
# @param [Statement] other
|
192
|
+
# @return [Boolean]
|
193
|
+
def ===(other)
|
194
|
+
return false if has_context? && !context.eql?(other.context)
|
195
|
+
return false if has_subject? && !subject.eql?(other.subject)
|
196
|
+
return false if has_predicate? && !predicate.eql?(other.predicate)
|
197
|
+
return false if has_object? && !object.eql?(other.object)
|
198
|
+
return true
|
199
|
+
end
|
200
|
+
|
201
|
+
##
|
202
|
+
# @param [Integer] index
|
203
|
+
# @return [RDF::Term]
|
204
|
+
def [](index)
|
205
|
+
case index
|
206
|
+
when 0 then self.subject
|
207
|
+
when 1 then self.predicate
|
208
|
+
when 2 then self.object
|
209
|
+
when 3 then self.context
|
210
|
+
else nil
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
214
|
+
##
|
215
|
+
# @param [Integer] index
|
216
|
+
# @param [RDF::Term] value
|
217
|
+
# @return [RDF::Term]
|
218
|
+
def []=(index, value)
|
219
|
+
case index
|
220
|
+
when 0 then self.subject = value
|
221
|
+
when 1 then self.predicate = value
|
222
|
+
when 2 then self.object = value
|
223
|
+
when 3 then self.context = value
|
224
|
+
else nil
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
##
|
229
|
+
# @return [Array(RDF::Term)]
|
230
|
+
def to_quad
|
231
|
+
[subject, predicate, object, context]
|
232
|
+
end
|
233
|
+
|
234
|
+
##
|
235
|
+
# @return [Array(RDF::Term)]
|
236
|
+
def to_triple
|
237
|
+
[subject, predicate, object]
|
238
|
+
end
|
239
|
+
|
240
|
+
alias_method :to_a, :to_triple
|
241
|
+
alias_method :to_ary, :to_triple
|
242
|
+
|
243
|
+
##
|
244
|
+
# Returns the terms of this statement as a `Hash`.
|
245
|
+
#
|
246
|
+
# @param [Symbol] subject_key
|
247
|
+
# @param [Symbol] predicate_key
|
248
|
+
# @param [Symbol] object_key
|
249
|
+
# @return [Hash{Symbol => RDF::Term}]
|
250
|
+
def to_hash(subject_key = :subject, predicate_key = :predicate, object_key = :object, context_key = :context)
|
251
|
+
{subject_key => subject, predicate_key => predicate, object_key => object, context_key => context}
|
252
|
+
end
|
253
|
+
|
254
|
+
##
|
255
|
+
# Returns a string representation of this statement.
|
256
|
+
#
|
257
|
+
# @return [String]
|
258
|
+
def to_s
|
259
|
+
StringIO.open do |buffer|
|
260
|
+
buffer << case subject
|
261
|
+
when RDF::Node then subject.to_s
|
262
|
+
when RDF::URI then "<#{subject}>"
|
263
|
+
else subject.inspect
|
264
|
+
end
|
265
|
+
buffer << " <#{predicate}> "
|
266
|
+
buffer << case object
|
267
|
+
when RDF::Literal then object.to_s
|
268
|
+
when RDF::Node then object.to_s
|
269
|
+
when RDF::URI then "<#{object}>"
|
270
|
+
else object.inspect
|
271
|
+
end
|
272
|
+
buffer << case context
|
273
|
+
when nil then " ."
|
274
|
+
else " <#{context}> ."
|
275
|
+
end
|
276
|
+
buffer.string
|
277
|
+
end
|
278
|
+
end
|
279
|
+
|
280
|
+
##
|
281
|
+
# Returns a graph containing this statement in reified form.
|
282
|
+
#
|
283
|
+
# @param [Hash{Symbol => Object}] options
|
284
|
+
# @return [RDF::Graph]
|
285
|
+
# @see http://www.w3.org/TR/rdf-primer/#reification
|
286
|
+
def reified(options = {})
|
287
|
+
RDF::Graph.new(options[:context]) do |graph|
|
288
|
+
subject = options[:subject] || RDF::Node.new(options[:id])
|
289
|
+
graph << [subject, RDF.type, RDF[:Statement]]
|
290
|
+
graph << [subject, RDF.subject, self.subject]
|
291
|
+
graph << [subject, RDF.predicate, self.predicate]
|
292
|
+
graph << [subject, RDF.object, self.object]
|
293
|
+
end
|
294
|
+
end
|
295
|
+
end
|
296
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
module RDF
|
2
|
+
##
|
3
|
+
# An RDF term.
|
4
|
+
#
|
5
|
+
# Terms can be used as subjects, predicates, objects, and contexts of
|
6
|
+
# statements.
|
7
|
+
#
|
8
|
+
# @since 0.3.0
|
9
|
+
module Term
|
10
|
+
include RDF::Value
|
11
|
+
include Comparable
|
12
|
+
|
13
|
+
##
|
14
|
+
# Compares `self` to `other` for sorting purposes.
|
15
|
+
#
|
16
|
+
# Subclasses should override this to provide a more meaningful
|
17
|
+
# implementation than the default which simply performs a string
|
18
|
+
# comparison based on `#to_s`.
|
19
|
+
#
|
20
|
+
# @abstract
|
21
|
+
# @param [Object] other
|
22
|
+
# @return [Integer] `-1`, `0`, or `1`
|
23
|
+
def <=>(other)
|
24
|
+
self.to_s <=> other.to_s
|
25
|
+
end
|
26
|
+
|
27
|
+
##
|
28
|
+
# Compares `self` to `other` to implement RDFterm-equal.
|
29
|
+
#
|
30
|
+
# Subclasses should override this to provide a more meaningful
|
31
|
+
# implementation than the default which simply performs a string
|
32
|
+
# comparison based on `#to_s`.
|
33
|
+
#
|
34
|
+
# @abstract
|
35
|
+
# @param [Object] other
|
36
|
+
# @return [Integer] `-1`, `0`, or `1`
|
37
|
+
#
|
38
|
+
# @see http://www.w3.org/TR/rdf-sparql-query/#func-RDFterm-equal
|
39
|
+
def ==(other)
|
40
|
+
super
|
41
|
+
end
|
42
|
+
|
43
|
+
##
|
44
|
+
# Determins if `self` is the same term as `other`.
|
45
|
+
#
|
46
|
+
# Subclasses should override this to provide a more meaningful
|
47
|
+
# implementation than the default which simply performs a string
|
48
|
+
# comparison based on `#to_s`.
|
49
|
+
#
|
50
|
+
# @abstract
|
51
|
+
# @param [Object] other
|
52
|
+
# @return [Integer] `-1`, `0`, or `1`
|
53
|
+
#
|
54
|
+
# @see http://www.w3.org/TR/rdf-sparql-query/#func-sameTerm
|
55
|
+
def eql?(other)
|
56
|
+
super
|
57
|
+
end
|
58
|
+
|
59
|
+
##
|
60
|
+
# Returns `true` if this term is constant.
|
61
|
+
#
|
62
|
+
# @return [Boolean] `true` or `false`
|
63
|
+
# @see #variable?
|
64
|
+
def constant?
|
65
|
+
!(variable?)
|
66
|
+
end
|
67
|
+
|
68
|
+
##
|
69
|
+
# Returns `true` if this term is variable.
|
70
|
+
#
|
71
|
+
# @return [Boolean] `true` or `false`
|
72
|
+
# @see #constant?
|
73
|
+
def variable?
|
74
|
+
false
|
75
|
+
end
|
76
|
+
end # Term
|
77
|
+
end # RDF
|