rdf 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- data/README +5 -4
- data/VERSION +1 -1
- data/lib/rdf/mixin/mutable.rb +74 -21
- data/lib/rdf/model/statement.rb +17 -1
- data/lib/rdf/model/value.rb +1 -1
- data/lib/rdf/query.rb +33 -20
- data/lib/rdf/query/solution.rb +1 -1
- data/lib/rdf/version.rb +1 -1
- metadata +5 -5
data/README
CHANGED
@@ -6,6 +6,7 @@ This is a pure-Ruby library for working with [Resource Description Framework
|
|
6
6
|
|
7
7
|
* <http://github.com/bendiken/rdf>
|
8
8
|
* <http://blog.datagraph.org/2010/03/rdf-for-ruby>
|
9
|
+
* <http://blog.datagraph.org/2010/04/rdf-repository-howto>
|
9
10
|
|
10
11
|
Features
|
11
12
|
--------
|
@@ -28,11 +29,11 @@ Examples
|
|
28
29
|
|
29
30
|
### Creating an RDF statement
|
30
31
|
|
31
|
-
s = RDF::URI
|
32
|
+
s = RDF::URI("http://rubygems.org/gems/rdf")
|
32
33
|
p = RDF::DC.creator
|
33
|
-
o = RDF::URI
|
34
|
+
o = RDF::URI("http://ar.to/#self")
|
34
35
|
|
35
|
-
stmt = RDF::Statement
|
36
|
+
stmt = RDF::Statement(s, p, o)
|
36
37
|
|
37
38
|
### Using pre-defined RDF vocabularies
|
38
39
|
|
@@ -55,7 +56,7 @@ Examples
|
|
55
56
|
|
56
57
|
### Reading N-Triples data
|
57
58
|
|
58
|
-
RDF::
|
59
|
+
RDF::Reader.open("spec/data/test.nt") do |reader|
|
59
60
|
reader.each_statement do |statement|
|
60
61
|
puts statement.inspect
|
61
62
|
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.6
|
data/lib/rdf/mixin/mutable.rb
CHANGED
@@ -36,15 +36,20 @@ module RDF
|
|
36
36
|
def load(filename, options = {})
|
37
37
|
raise TypeError.new("#{self} is immutable") if immutable?
|
38
38
|
|
39
|
-
count = 0
|
40
39
|
Reader.open(filename, options) do |reader|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
40
|
+
if options[:context]
|
41
|
+
statements = []
|
42
|
+
reader.each_statement do |statement|
|
43
|
+
statement.context = options[:context]
|
44
|
+
statements << statement
|
45
|
+
end
|
46
|
+
insert_statements(statements)
|
47
|
+
statements.size
|
48
|
+
else
|
49
|
+
insert_statements(reader)
|
50
|
+
nil # FIXME
|
45
51
|
end
|
46
52
|
end
|
47
|
-
count
|
48
53
|
end
|
49
54
|
|
50
55
|
alias_method :load!, :load
|
@@ -58,7 +63,8 @@ module RDF
|
|
58
63
|
raise TypeError.new("#{self} is immutable") if immutable?
|
59
64
|
|
60
65
|
insert_statement(create_statement(statement))
|
61
|
-
|
66
|
+
|
67
|
+
return self
|
62
68
|
end
|
63
69
|
|
64
70
|
##
|
@@ -70,14 +76,21 @@ module RDF
|
|
70
76
|
def insert(*statements)
|
71
77
|
raise TypeError.new("#{self} is immutable") if immutable?
|
72
78
|
|
73
|
-
statements.
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
79
|
+
statements.map! do |value|
|
80
|
+
case
|
81
|
+
when value.respond_to?(:each_statement)
|
82
|
+
insert_statements(value)
|
83
|
+
nil
|
84
|
+
when (statement = create_statement(value)).valid?
|
85
|
+
statement
|
86
|
+
else
|
87
|
+
raise ArgumentError.new("not a valid statement: #{value.inspect}")
|
78
88
|
end
|
79
89
|
end
|
80
|
-
|
90
|
+
statements.compact!
|
91
|
+
insert_statements(statements) unless statements.empty?
|
92
|
+
|
93
|
+
return self
|
81
94
|
end
|
82
95
|
|
83
96
|
alias_method :insert!, :insert
|
@@ -91,16 +104,22 @@ module RDF
|
|
91
104
|
def delete(*statements)
|
92
105
|
raise TypeError.new("#{self} is immutable") if immutable?
|
93
106
|
|
94
|
-
statements.
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
107
|
+
statements.map! do |value|
|
108
|
+
case
|
109
|
+
when value.respond_to?(:each_statement)
|
110
|
+
delete_statements(value)
|
111
|
+
nil
|
112
|
+
when (statement = create_statement(value)).valid?
|
113
|
+
statement
|
114
|
+
else
|
115
|
+
delete_statements(query(value))
|
116
|
+
nil
|
101
117
|
end
|
102
118
|
end
|
103
|
-
|
119
|
+
statements.compact!
|
120
|
+
delete_statements(statements) unless statements.empty?
|
121
|
+
|
122
|
+
return self
|
104
123
|
end
|
105
124
|
|
106
125
|
alias_method :delete!, :delete
|
@@ -174,6 +193,22 @@ module RDF
|
|
174
193
|
raise NotImplementedError
|
175
194
|
end
|
176
195
|
|
196
|
+
##
|
197
|
+
# Inserts a list of RDF statement into the underlying storage.
|
198
|
+
#
|
199
|
+
# Subclasses of {RDF::Repository} may implement this method if they can
|
200
|
+
# efficiently insert multiple statements at once. This will otherwise
|
201
|
+
# default to invoking {#insert_statement} for each given statement.
|
202
|
+
#
|
203
|
+
# @param [RDF::Enumerable, #each] statements
|
204
|
+
# @return [void]
|
205
|
+
def insert_statements(statements)
|
206
|
+
each = statements.respond_to?(:each_statement) ? :each_statement : :each
|
207
|
+
statements.__send__(each) do |statement|
|
208
|
+
insert_statement(statement)
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
177
212
|
##
|
178
213
|
# Deletes an RDF statement from the underlying storage.
|
179
214
|
#
|
@@ -187,8 +222,26 @@ module RDF
|
|
187
222
|
raise NotImplementedError
|
188
223
|
end
|
189
224
|
|
225
|
+
##
|
226
|
+
# Deletes a list of RDF statement from the underlying storage.
|
227
|
+
#
|
228
|
+
# Subclasses of {RDF::Repository} may implement this method if they can
|
229
|
+
# efficiently delete multiple statements at once. This will otherwise
|
230
|
+
# default to invoking {#delete_statement} for each given statement.
|
231
|
+
#
|
232
|
+
# @param [RDF::Enumerable, #each] statements
|
233
|
+
# @return [void]
|
234
|
+
def delete_statements(statements)
|
235
|
+
each = statements.respond_to?(:each_statement) ? :each_statement : :each
|
236
|
+
statements.__send__(each) do |statement|
|
237
|
+
delete_statement(statement)
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
190
241
|
protected :create_statement
|
191
242
|
protected :insert_statement
|
243
|
+
protected :insert_statements
|
192
244
|
protected :delete_statement
|
245
|
+
protected :delete_statements
|
193
246
|
end
|
194
247
|
end
|
data/lib/rdf/model/statement.rb
CHANGED
@@ -201,7 +201,7 @@ module RDF
|
|
201
201
|
alias_method :to_ary, :to_triple
|
202
202
|
|
203
203
|
##
|
204
|
-
# Returns the
|
204
|
+
# Returns the terms of this statement as a `Hash`.
|
205
205
|
#
|
206
206
|
# @param [Symbol] subject_key
|
207
207
|
# @param [Symbol] predicate_key
|
@@ -237,5 +237,21 @@ module RDF
|
|
237
237
|
buffer.string
|
238
238
|
end
|
239
239
|
end
|
240
|
+
|
241
|
+
##
|
242
|
+
# Returns a graph containing this statement in reified form.
|
243
|
+
#
|
244
|
+
# @param [Hash{Symbol => Object}] options
|
245
|
+
# @return [RDF::Graph]
|
246
|
+
# @see http://www.w3.org/TR/rdf-primer/#reification
|
247
|
+
def reified(options = {})
|
248
|
+
RDF::Graph.new(options[:context]) do |graph|
|
249
|
+
subject = options[:subject] || RDF::Node.new(options[:id])
|
250
|
+
graph << [subject, RDF.type, RDF[:Statement]]
|
251
|
+
graph << [subject, RDF.subject, self.subject]
|
252
|
+
graph << [subject, RDF.predicate, self.predicate]
|
253
|
+
graph << [subject, RDF.object, self.object]
|
254
|
+
end
|
255
|
+
end
|
240
256
|
end
|
241
257
|
end
|
data/lib/rdf/model/value.rb
CHANGED
@@ -3,7 +3,7 @@ module RDF
|
|
3
3
|
# An RDF value.
|
4
4
|
#
|
5
5
|
# This is the base class for the RDF.rb class hierarchy. The class of
|
6
|
-
# every object that can be a
|
6
|
+
# every object that can be a term of {RDF::Statement statements} is a
|
7
7
|
# subclass of this class.
|
8
8
|
#
|
9
9
|
# @example Checking if a value is a resource (blank node or URI reference)
|
data/lib/rdf/query.rb
CHANGED
@@ -52,15 +52,18 @@ module RDF
|
|
52
52
|
# @return [Array<Hash{Symbol => Value}>] An unordered sequence of query solutions.
|
53
53
|
attr_accessor :solutions
|
54
54
|
|
55
|
+
# @return [Hash]
|
56
|
+
attr_reader :options
|
57
|
+
|
55
58
|
##
|
56
59
|
# @param [Hash{Symbol => Object}] options
|
57
60
|
# @yield [query]
|
58
61
|
# @yieldparam [Query]
|
59
62
|
def initialize(options = {}, &block)
|
60
|
-
@
|
61
|
-
@
|
62
|
-
@
|
63
|
-
@
|
63
|
+
@options = options.dup
|
64
|
+
@variables = @options.delete(:variables) || {}
|
65
|
+
@patterns = @options.delete(:patterns) || []
|
66
|
+
@solutions = @options.delete(:solutions) || []
|
64
67
|
|
65
68
|
if block_given?
|
66
69
|
case block.arity
|
@@ -77,8 +80,13 @@ module RDF
|
|
77
80
|
# @yieldparam [Solution]
|
78
81
|
# @return [Enumerator]
|
79
82
|
def each_solution(&block)
|
80
|
-
|
81
|
-
|
83
|
+
unless block_given?
|
84
|
+
require 'enumerator' unless defined?(::Enumerable::Enumerator)
|
85
|
+
::Enumerable::Enumerator.new(self, :each_solution)
|
86
|
+
else
|
87
|
+
solutions.each do |solution|
|
88
|
+
block.call(solution.is_a?(Solution) ? solution : Solution.new(solution))
|
89
|
+
end
|
82
90
|
end
|
83
91
|
end
|
84
92
|
|
@@ -88,11 +96,11 @@ module RDF
|
|
88
96
|
# Returns the number of query solutions.
|
89
97
|
#
|
90
98
|
# @return [Integer]
|
91
|
-
def
|
99
|
+
def count
|
92
100
|
solutions.size
|
93
101
|
end
|
94
102
|
|
95
|
-
alias_method :
|
103
|
+
alias_method :size, :count
|
96
104
|
|
97
105
|
##
|
98
106
|
# Filters the solution sequence by the given criteria.
|
@@ -104,13 +112,14 @@ module RDF
|
|
104
112
|
# @return [Query]
|
105
113
|
def filter(criteria = {}, &block)
|
106
114
|
if block_given?
|
107
|
-
solutions.reject! do |
|
108
|
-
!block.call(Solution.new(
|
115
|
+
solutions.reject! do |solution|
|
116
|
+
!block.call(solution.is_a?(Solution) ? solution : Solution.new(solution))
|
109
117
|
end
|
110
118
|
else
|
111
|
-
solutions.reject! do |
|
119
|
+
solutions.reject! do |solution|
|
120
|
+
solution = solution.is_a?(Solution) ? solution : Solution.new(solution)
|
112
121
|
results = criteria.map do |name, value|
|
113
|
-
|
122
|
+
solution[name] == value
|
114
123
|
end
|
115
124
|
!results.all?
|
116
125
|
end
|
@@ -151,7 +160,7 @@ module RDF
|
|
151
160
|
unless variables.empty?
|
152
161
|
variables.map! { |variable| variable.to_sym }
|
153
162
|
solutions.each do |bindings|
|
154
|
-
bindings.delete_if { |k, v| !variables.include?(k) }
|
163
|
+
bindings.delete_if { |k, v| !variables.include?(k) } # FIXME
|
155
164
|
end
|
156
165
|
end
|
157
166
|
self
|
@@ -176,31 +185,35 @@ module RDF
|
|
176
185
|
# Limits the solution sequence to bindings starting from the `start`
|
177
186
|
# offset in the overall solution sequence.
|
178
187
|
#
|
179
|
-
# @param [Integer] start
|
188
|
+
# @param [Integer, #to_i] start
|
180
189
|
# @return [Query]
|
181
190
|
def offset(start)
|
182
|
-
slice(start, solutions.size - start)
|
191
|
+
slice(start, solutions.size - start.to_i)
|
183
192
|
end
|
184
193
|
|
194
|
+
alias_method :offset!, :offset
|
195
|
+
|
185
196
|
##
|
186
197
|
# Limits the number of solutions to `length`.
|
187
198
|
#
|
188
|
-
# @param [Integer] length
|
199
|
+
# @param [Integer, #to_i] length
|
189
200
|
# @return [Query]
|
190
201
|
def limit(length)
|
191
202
|
slice(0, length)
|
192
203
|
end
|
193
204
|
|
205
|
+
alias_method :limit!, :limit
|
206
|
+
|
194
207
|
##
|
195
208
|
# Limits the solution sequence to `length` bindings starting from the
|
196
209
|
# `start` offset in the overall solution sequence.
|
197
210
|
#
|
198
|
-
# @param [Integer] start
|
199
|
-
# @param [Integer] length
|
211
|
+
# @param [Integer, #to_i] start
|
212
|
+
# @param [Integer, #to_i] length
|
200
213
|
# @return [Query]
|
201
214
|
def slice(start, length)
|
202
|
-
if start < solutions.size
|
203
|
-
solutions.slice!(start, length)
|
215
|
+
if (start = start.to_i) < solutions.size
|
216
|
+
solutions.slice!(start, length.to_i)
|
204
217
|
else
|
205
218
|
solutions = []
|
206
219
|
end
|
data/lib/rdf/query/solution.rb
CHANGED
data/lib/rdf/version.rb
CHANGED
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 6
|
9
|
+
version: 0.1.6
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Arto Bendiken
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-04-
|
18
|
+
date: 2010-04-12 00:00:00 +02:00
|
19
19
|
default_executable: rdf
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -28,8 +28,8 @@ dependencies:
|
|
28
28
|
segments:
|
29
29
|
- 0
|
30
30
|
- 1
|
31
|
-
-
|
32
|
-
version: 0.1.
|
31
|
+
- 6
|
32
|
+
version: 0.1.6
|
33
33
|
type: :development
|
34
34
|
version_requirements: *id001
|
35
35
|
- !ruby/object:Gem::Dependency
|