openlogic-rdf 0.3.6

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.
Files changed (80) hide show
  1. data/AUTHORS +3 -0
  2. data/CREDITS +9 -0
  3. data/README +361 -0
  4. data/UNLICENSE +24 -0
  5. data/VERSION +1 -0
  6. data/bin/rdf +18 -0
  7. data/etc/doap.nt +62 -0
  8. data/lib/df.rb +1 -0
  9. data/lib/rdf/cli.rb +200 -0
  10. data/lib/rdf/format.rb +383 -0
  11. data/lib/rdf/mixin/countable.rb +39 -0
  12. data/lib/rdf/mixin/durable.rb +31 -0
  13. data/lib/rdf/mixin/enumerable.rb +637 -0
  14. data/lib/rdf/mixin/indexable.rb +26 -0
  15. data/lib/rdf/mixin/inferable.rb +5 -0
  16. data/lib/rdf/mixin/mutable.rb +191 -0
  17. data/lib/rdf/mixin/queryable.rb +265 -0
  18. data/lib/rdf/mixin/readable.rb +15 -0
  19. data/lib/rdf/mixin/type_check.rb +21 -0
  20. data/lib/rdf/mixin/writable.rb +152 -0
  21. data/lib/rdf/model/graph.rb +263 -0
  22. data/lib/rdf/model/list.rb +731 -0
  23. data/lib/rdf/model/literal/boolean.rb +121 -0
  24. data/lib/rdf/model/literal/date.rb +73 -0
  25. data/lib/rdf/model/literal/datetime.rb +72 -0
  26. data/lib/rdf/model/literal/decimal.rb +86 -0
  27. data/lib/rdf/model/literal/double.rb +189 -0
  28. data/lib/rdf/model/literal/integer.rb +126 -0
  29. data/lib/rdf/model/literal/numeric.rb +184 -0
  30. data/lib/rdf/model/literal/time.rb +87 -0
  31. data/lib/rdf/model/literal/token.rb +47 -0
  32. data/lib/rdf/model/literal/xml.rb +39 -0
  33. data/lib/rdf/model/literal.rb +373 -0
  34. data/lib/rdf/model/node.rb +156 -0
  35. data/lib/rdf/model/resource.rb +28 -0
  36. data/lib/rdf/model/statement.rb +296 -0
  37. data/lib/rdf/model/term.rb +77 -0
  38. data/lib/rdf/model/uri.rb +570 -0
  39. data/lib/rdf/model/value.rb +133 -0
  40. data/lib/rdf/nquads.rb +152 -0
  41. data/lib/rdf/ntriples/format.rb +48 -0
  42. data/lib/rdf/ntriples/reader.rb +239 -0
  43. data/lib/rdf/ntriples/writer.rb +219 -0
  44. data/lib/rdf/ntriples.rb +104 -0
  45. data/lib/rdf/query/pattern.rb +329 -0
  46. data/lib/rdf/query/solution.rb +252 -0
  47. data/lib/rdf/query/solutions.rb +237 -0
  48. data/lib/rdf/query/variable.rb +221 -0
  49. data/lib/rdf/query.rb +404 -0
  50. data/lib/rdf/reader.rb +511 -0
  51. data/lib/rdf/repository.rb +389 -0
  52. data/lib/rdf/transaction.rb +161 -0
  53. data/lib/rdf/util/aliasing.rb +63 -0
  54. data/lib/rdf/util/cache.rb +139 -0
  55. data/lib/rdf/util/file.rb +38 -0
  56. data/lib/rdf/util/uuid.rb +36 -0
  57. data/lib/rdf/util.rb +6 -0
  58. data/lib/rdf/version.rb +19 -0
  59. data/lib/rdf/vocab/cc.rb +18 -0
  60. data/lib/rdf/vocab/cert.rb +13 -0
  61. data/lib/rdf/vocab/dc.rb +63 -0
  62. data/lib/rdf/vocab/dc11.rb +23 -0
  63. data/lib/rdf/vocab/doap.rb +45 -0
  64. data/lib/rdf/vocab/exif.rb +168 -0
  65. data/lib/rdf/vocab/foaf.rb +69 -0
  66. data/lib/rdf/vocab/geo.rb +13 -0
  67. data/lib/rdf/vocab/http.rb +26 -0
  68. data/lib/rdf/vocab/owl.rb +59 -0
  69. data/lib/rdf/vocab/rdfs.rb +17 -0
  70. data/lib/rdf/vocab/rsa.rb +12 -0
  71. data/lib/rdf/vocab/rss.rb +14 -0
  72. data/lib/rdf/vocab/sioc.rb +93 -0
  73. data/lib/rdf/vocab/skos.rb +36 -0
  74. data/lib/rdf/vocab/wot.rb +21 -0
  75. data/lib/rdf/vocab/xhtml.rb +9 -0
  76. data/lib/rdf/vocab/xsd.rb +58 -0
  77. data/lib/rdf/vocab.rb +215 -0
  78. data/lib/rdf/writer.rb +475 -0
  79. data/lib/rdf.rb +192 -0
  80. metadata +173 -0
@@ -0,0 +1,731 @@
1
+ module RDF
2
+ ##
3
+ # An RDF list.
4
+ #
5
+ # @example Constructing a new list
6
+ # RDF::List[1, 2, 3]
7
+ #
8
+ # @since 0.2.3
9
+ class RDF::List
10
+ include RDF::Enumerable
11
+ include RDF::Resource
12
+
13
+ ##
14
+ # Constructs a new list from the given `values`.
15
+ #
16
+ # The list will be identified by a new autogenerated blank node, and
17
+ # backed by an initially empty in-memory graph.
18
+ #
19
+ # @example
20
+ # RDF::List[]
21
+ # RDF::List[*(1..10)]
22
+ # RDF::List[1, 2, 3]
23
+ # RDF::List["foo", "bar"]
24
+ # RDF::List["a", 1, "b", 2, "c", 3]
25
+ #
26
+ # @param [Array<RDF::Term>] values
27
+ # @return [RDF::List]
28
+ def self.[](*values)
29
+ self.new(nil, nil, values)
30
+ end
31
+
32
+ ##
33
+ # Initializes a newly-constructed list.
34
+ #
35
+ # @param [RDF::Resource] subject
36
+ # @param [RDF::Graph] graph
37
+ # @param [Array<RDF::Term>] values
38
+ # @yield [list]
39
+ # @yieldparam [RDF::List] list
40
+ def initialize(subject = nil, graph = nil, values = nil, &block)
41
+ @subject = subject || RDF::Node.new
42
+ @graph = graph || RDF::Graph.new
43
+
44
+ values.each { |value| self << value } unless values.nil? || values.empty?
45
+
46
+ if block_given?
47
+ case block.arity
48
+ when 1 then block.call(self)
49
+ else instance_eval(&block)
50
+ end
51
+ end
52
+ end
53
+
54
+ UNSET = Object.new.freeze # @private
55
+
56
+ # The canonical empty list.
57
+ NIL = RDF::List.new(RDF.nil).freeze
58
+
59
+ ##
60
+ # Validate the list ensuring that
61
+ # * rdf:rest values are all BNodes are nil
62
+ # * rdf:type, if it exists, is rdf:List
63
+ # * each subject has no properties other than single-valued rdf:first, rdf:rest
64
+ # other than for the first node in the list
65
+ # @return [Boolean]
66
+ def valid?
67
+ li = subject
68
+ while li != RDF.nil do
69
+ rest = nil
70
+ firsts = rests = 0
71
+ @graph.query(:subject => li) do |st|
72
+ case st.predicate
73
+ when RDF.type
74
+ # Be tollerant about rdf:type entries, as some OWL vocabularies use it excessively
75
+ when RDF.first
76
+ firsts += 1
77
+ when RDF.rest
78
+ rest = st.object
79
+ return false unless rest.node? || rest == RDF.nil
80
+ rests += 1
81
+ else
82
+ # First node may have other properties
83
+ return false unless li == subject
84
+ end
85
+ end
86
+ return false unless firsts == 1 && rests == 1
87
+ li = rest
88
+ end
89
+ true
90
+ end
91
+
92
+ ##
93
+ # Returns the subject term of this list.
94
+ #
95
+ # @attr_reader [RDF::Resource]
96
+ attr_reader :subject
97
+
98
+ ##
99
+ # Returns the underlying graph storing the statements that constitute
100
+ # this list.
101
+ #
102
+ # @attr_reader [RDF::Graph]
103
+ attr_reader :graph
104
+
105
+ ##
106
+ # Returns the set intersection of this list and `other`.
107
+ #
108
+ # The resulting list contains the elements common to both lists, with no
109
+ # duplicates.
110
+ #
111
+ # @example
112
+ # RDF::List[1, 2] & RDF::List[1, 2] #=> RDF::List[1, 2]
113
+ # RDF::List[1, 2] & RDF::List[2, 3] #=> RDF::List[2]
114
+ # RDF::List[1, 2] & RDF::List[3, 4] #=> RDF::List[]
115
+ #
116
+ # @param [RDF::List] other
117
+ # @return [RDF::List]
118
+ # @see http://ruby-doc.org/core-1.9/classes/Array.html#M000469
119
+ def &(other)
120
+ RDF::List[*(to_a & other.to_a)]
121
+ end
122
+
123
+ ##
124
+ # Returns the set union of this list and `other`.
125
+ #
126
+ # The resulting list contains the elements from both lists, with no
127
+ # duplicates.
128
+ #
129
+ # @example
130
+ # RDF::List[1, 2] | RDF::List[1, 2] #=> RDF::List[1, 2]
131
+ # RDF::List[1, 2] | RDF::List[2, 3] #=> RDF::List[1, 2, 3]
132
+ # RDF::List[1, 2] | RDF::List[3, 4] #=> RDF::List[1, 2, 3, 4]
133
+ #
134
+ # @param [RDF::List] other
135
+ # @return [RDF::List]
136
+ # @see http://ruby-doc.org/core-1.9/classes/Array.html#M000470
137
+ def |(other)
138
+ RDF::List[*(to_a | other.to_a)]
139
+ end
140
+
141
+ ##
142
+ # Returns the concatenation of this list and `other`.
143
+ #
144
+ # @example
145
+ # RDF::List[1, 2] + RDF::List[3, 4] #=> RDF::List[1, 2, 3, 4]
146
+ #
147
+ # @param [RDF::List] other
148
+ # @return [RDF::List]
149
+ # @see http://ruby-doc.org/core-1.9/classes/Array.html#M000466
150
+ def +(other)
151
+ RDF::List[*(to_a + other.to_a)]
152
+ end
153
+
154
+ ##
155
+ # Returns the difference between this list and `other`, removing any
156
+ # elements that appear in both lists.
157
+ #
158
+ # @example
159
+ # RDF::List[1, 2, 2, 3] - RDF::List[2] #=> RDF::List[1, 3]
160
+ #
161
+ # @param [RDF::List] other
162
+ # @return [RDF::List]
163
+ # @see http://ruby-doc.org/core-1.9/classes/Array.html#M000468
164
+ def -(other)
165
+ RDF::List[*(to_a - other.to_a)]
166
+ end
167
+
168
+ ##
169
+ # Returns either a repeated list or a string concatenation of the
170
+ # elements in this list.
171
+ #
172
+ # @overload *(times)
173
+ # Returns a new list built of `times` repetitions of this list.
174
+ #
175
+ # @example
176
+ # RDF::List[1, 2, 3] * 2 #=> RDF::List[1, 2, 3, 1, 2, 3]
177
+ #
178
+ # @param [Integer] times
179
+ # @return [RDF::List]
180
+ #
181
+ # @overload *(sep)
182
+ # Returns the string concatenation of the elements in this list
183
+ # separated by `sep`. Equivalent to `self.join(sep)`.
184
+ #
185
+ # @example
186
+ # RDF::List[1, 2, 3] * "," #=> "1,2,3"
187
+ #
188
+ # @param [String, #to_s] sep
189
+ # @return [RDF::List]
190
+ #
191
+ # @return [RDF::List]
192
+ # @see http://ruby-doc.org/core-1.9/classes/Array.html#M000467
193
+ def *(int_or_str)
194
+ case int_or_str
195
+ when Integer then RDF::List[*(to_a * int_or_str)]
196
+ else join(int_or_str.to_s)
197
+ end
198
+ end
199
+
200
+ ##
201
+ # Returns the element at `index`.
202
+ #
203
+ # @example
204
+ # RDF::List[1, 2, 3][0] #=> 1
205
+ #
206
+ # @param [Integer] index
207
+ # @return [RDF::Term]
208
+ # @see http://ruby-doc.org/core-1.9/classes/Array.html#M000417
209
+ def [](index)
210
+ at(index)
211
+ end
212
+
213
+ ##
214
+ # Appends an element to the tail of this list.
215
+ #
216
+ # @example
217
+ # RDF::List[] << 1 << 2 << 3 #=> RDF::List[1, 2, 3]
218
+ #
219
+ # @param [RDF::Term] value
220
+ # @return [RDF::List]
221
+ # @see http://ruby-doc.org/core-1.9/classes/Array.html#M000424
222
+ def <<(value)
223
+ value = case value
224
+ when nil then RDF.nil
225
+ when RDF::Value then value
226
+ when Array then RDF::List.new(nil, graph, value)
227
+ else value
228
+ end
229
+
230
+ if empty?
231
+ new_subject = subject
232
+ else
233
+ old_subject, new_subject = last_subject, RDF::Node.new
234
+ graph.delete([old_subject, RDF.rest, RDF.nil])
235
+ graph.insert([old_subject, RDF.rest, new_subject])
236
+ end
237
+
238
+ graph.insert([new_subject, RDF.type, RDF.List])
239
+ graph.insert([new_subject, RDF.first, value])
240
+ graph.insert([new_subject, RDF.rest, RDF.nil])
241
+
242
+ self
243
+ end
244
+
245
+ ##
246
+ # Compares this list to `other` for sorting purposes.
247
+ #
248
+ # @example
249
+ # RDF::List[1] <=> RDF::List[1] #=> 0
250
+ # RDF::List[1] <=> RDF::List[2] #=> -1
251
+ # RDF::List[2] <=> RDF::List[1] #=> 1
252
+ #
253
+ # @param [RDF::List] other
254
+ # @return [Integer]
255
+ # @see http://ruby-doc.org/core-1.9/classes/Array.html#M000461
256
+ def <=>(other)
257
+ to_a <=> other.to_a # TODO: optimize this
258
+ end
259
+
260
+ ##
261
+ # Returns `true` if this list is empty.
262
+ #
263
+ # @example
264
+ # RDF::List[].empty? #=> true
265
+ # RDF::List[1, 2, 3].empty? #=> false
266
+ #
267
+ # @return [Boolean]
268
+ # @see http://ruby-doc.org/core-1.9/classes/Array.html#M000434
269
+ def empty?
270
+ graph.query(:subject => subject, :predicate => RDF.first).empty?
271
+ end
272
+
273
+ ##
274
+ # Returns the length of this list.
275
+ #
276
+ # @example
277
+ # RDF::List[].length #=> 0
278
+ # RDF::List[1, 2, 3].length #=> 3
279
+ #
280
+ # @return [Integer]
281
+ # @see http://ruby-doc.org/core-1.9/classes/Array.html#M000433
282
+ def length
283
+ each.count
284
+ end
285
+
286
+ alias_method :size, :length
287
+
288
+ ##
289
+ # Returns the index of the first element equal to `value`, or `nil` if
290
+ # no match was found.
291
+ #
292
+ # @example
293
+ # RDF::List['a', 'b', 'c'].index('a') #=> 0
294
+ # RDF::List['a', 'b', 'c'].index('d') #=> nil
295
+ #
296
+ # @param [RDF::Term] value
297
+ # @return [Integer]
298
+ # @see http://ruby-doc.org/core-1.9/classes/Array.html#M000436
299
+ def index(value)
300
+ each.with_index do |v, i|
301
+ return i if v == value
302
+ end
303
+ return nil
304
+ end
305
+
306
+ ##
307
+ # Returns the element at `index`.
308
+ #
309
+ # @example
310
+ # RDF::List[1, 2, 3].at(0) #=> 1
311
+ #
312
+ # @return [RDF::Term]
313
+ # @see http://ruby-doc.org/core-1.9/classes/Array.html#M000462
314
+ def slice(*args)
315
+ case argc = args.size
316
+ when 2 then slice_with_start_and_length(*args)
317
+ when 1 then (arg = args.first).is_a?(Range) ? slice_with_range(arg) : at(arg)
318
+ when 0 then raise ArgumentError, "wrong number of arguments (0 for 1)"
319
+ else raise ArgumentError, "wrong number of arguments (#{argc} for 2)"
320
+ end
321
+ end
322
+
323
+ ##
324
+ # @private
325
+ def slice_with_start_and_length(start, length)
326
+ RDF::List[*to_a.slice(start, length)]
327
+ end
328
+
329
+ ##
330
+ # @private
331
+ def slice_with_range(range)
332
+ RDF::List[*to_a.slice(range)]
333
+ end
334
+
335
+ protected :slice_with_start_and_length
336
+ protected :slice_with_range
337
+
338
+ ##
339
+ # Returns the element at `index`.
340
+ #
341
+ # @example
342
+ # RDF::List[1, 2, 3].fetch(0) #=> 1
343
+ # RDF::List[1, 2, 3].fetch(4) #=> IndexError
344
+ # RDF::List[1, 2, 3].fetch(4, nil) #=> nil
345
+ # RDF::List[1, 2, 3].fetch(4) { |n| n*n } #=> 16
346
+ #
347
+ # @return [RDF::Term]
348
+ # @see http://ruby-doc.org/core-1.9/classes/Array.html#M000420
349
+ def fetch(index, default = UNSET, &block)
350
+ each.with_index do |v, i|
351
+ return v if i == index
352
+ end
353
+
354
+ case
355
+ when block_given? then block.call(index)
356
+ when !default.eql?(UNSET) then default
357
+ else raise IndexError, "index #{index} not in the list #{self.inspect}"
358
+ end
359
+ end
360
+
361
+ ##
362
+ # Returns the element at `index`.
363
+ #
364
+ # @example
365
+ # RDF::List[1, 2, 3].at(0) #=> 1
366
+ #
367
+ # @return [RDF::Term]
368
+ # @see http://ruby-doc.org/core-1.9/classes/Array.html#M000419
369
+ def at(index)
370
+ each.with_index do |v, i|
371
+ return v if i == index
372
+ end
373
+ return nil
374
+ end
375
+
376
+ alias_method :nth, :at
377
+
378
+ ##
379
+ # Returns the first element in this list.
380
+ #
381
+ # @example
382
+ # RDF::List[*(1..10)].first #=> 1
383
+ #
384
+ # @return [RDF::Term]
385
+ def first
386
+ graph.first_object(:subject => first_subject, :predicate => RDF.first)
387
+ end
388
+
389
+ ##
390
+ # Returns the second element in this list.
391
+ #
392
+ # @example
393
+ # RDF::List[*(1..10)].second #=> 2
394
+ #
395
+ # @return [RDF::Term]
396
+ def second
397
+ at(1)
398
+ end
399
+
400
+ ##
401
+ # Returns the third element in this list.
402
+ #
403
+ # @example
404
+ # RDF::List[*(1..10)].third #=> 3
405
+ #
406
+ # @return [RDF::Term]
407
+ def third
408
+ at(2)
409
+ end
410
+
411
+ ##
412
+ # Returns the fourth element in this list.
413
+ #
414
+ # @example
415
+ # RDF::List[*(1..10)].fourth #=> 4
416
+ #
417
+ # @return [RDF::Term]
418
+ def fourth
419
+ at(3)
420
+ end
421
+
422
+ ##
423
+ # Returns the fifth element in this list.
424
+ #
425
+ # @example
426
+ # RDF::List[*(1..10)].fifth #=> 5
427
+ #
428
+ # @return [RDF::Term]
429
+ def fifth
430
+ at(4)
431
+ end
432
+
433
+ ##
434
+ # Returns the sixth element in this list.
435
+ #
436
+ # @example
437
+ # RDF::List[*(1..10)].sixth #=> 6
438
+ #
439
+ # @return [RDF::Term]
440
+ def sixth
441
+ at(5)
442
+ end
443
+
444
+ ##
445
+ # Returns the seventh element in this list.
446
+ #
447
+ # @example
448
+ # RDF::List[*(1..10)].seventh #=> 7
449
+ #
450
+ # @return [RDF::Term]
451
+ def seventh
452
+ at(6)
453
+ end
454
+
455
+ ##
456
+ # Returns the eighth element in this list.
457
+ #
458
+ # @example
459
+ # RDF::List[*(1..10)].eighth #=> 8
460
+ #
461
+ # @return [RDF::Term]
462
+ def eighth
463
+ at(7)
464
+ end
465
+
466
+ ##
467
+ # Returns the ninth element in this list.
468
+ #
469
+ # @example
470
+ # RDF::List[*(1..10)].ninth #=> 9
471
+ #
472
+ # @return [RDF::Term]
473
+ def ninth
474
+ at(8)
475
+ end
476
+
477
+ ##
478
+ # Returns the tenth element in this list.
479
+ #
480
+ # @example
481
+ # RDF::List[*(1..10)].tenth #=> 10
482
+ #
483
+ # @return [RDF::Term]
484
+ def tenth
485
+ at(9)
486
+ end
487
+
488
+ ##
489
+ # Returns the last element in this list.
490
+ #
491
+ # @example
492
+ # RDF::List[1, 2, 3].last #=> 3
493
+ #
494
+ # @return [RDF::Term]
495
+ # @see http://ruby-doc.org/core-1.9/classes/Array.html#M000422
496
+ def last
497
+ graph.first_object(:subject => last_subject, :predicate => RDF.first)
498
+ end
499
+
500
+ ##
501
+ # Returns a list containing all but the first element of this list.
502
+ #
503
+ # @example
504
+ # RDF::List[1, 2, 3].rest #=> RDF::List[2, 3]
505
+ #
506
+ # @return [RDF::List]
507
+ def rest
508
+ (subject = rest_subject).eql?(RDF.nil) ? nil : self.class.new(subject, graph)
509
+ end
510
+
511
+ ##
512
+ # Returns a list containing the last element of this list.
513
+ #
514
+ # @example
515
+ # RDF::List[1, 2, 3].tail #=> RDF::List[3]
516
+ #
517
+ # @return [RDF::List]
518
+ def tail
519
+ (subject = last_subject).eql?(RDF.nil) ? nil : self.class.new(subject, graph)
520
+ end
521
+
522
+ ##
523
+ # Returns the first subject term constituting this list.
524
+ #
525
+ # This is equivalent to `subject`.
526
+ #
527
+ # @example
528
+ # RDF::List[1, 2, 3].first_subject #=> RDF::Node(...)
529
+ #
530
+ # @return [RDF::Resource]
531
+ def first_subject
532
+ subject
533
+ end
534
+
535
+ ##
536
+ # @example
537
+ # RDF::List[1, 2, 3].rest_subject #=> RDF::Node(...)
538
+ #
539
+ # @return [RDF::Resource]
540
+ def rest_subject
541
+ graph.first_object(:subject => subject, :predicate => RDF.rest)
542
+ end
543
+
544
+ ##
545
+ # Returns the last subject term constituting this list.
546
+ #
547
+ # @example
548
+ # RDF::List[1, 2, 3].last_subject #=> RDF::Node(...)
549
+ #
550
+ # @return [RDF::Resource]
551
+ def last_subject
552
+ each_subject.to_a.last # TODO: optimize this
553
+ end
554
+
555
+ ##
556
+ # Yields each subject term constituting this list.
557
+ #
558
+ # @example
559
+ # RDF::List[1, 2, 3].each_subject do |subject|
560
+ # puts subject.inspect
561
+ # end
562
+ #
563
+ # @return [Enumerator]
564
+ # @see RDF::Enumerable#each
565
+ def each_subject(&block)
566
+ return enum_subject unless block_given?
567
+
568
+ subject = self.subject
569
+ block.call(subject)
570
+
571
+ loop do
572
+ rest = graph.first_object(:subject => subject, :predicate => RDF.rest)
573
+ break if rest.nil? || rest.eql?(RDF.nil)
574
+ block.call(subject = rest)
575
+ end
576
+ end
577
+
578
+ ##
579
+ # Yields each element in this list.
580
+ #
581
+ # @example
582
+ # RDF::List[1, 2, 3].each do |value|
583
+ # puts value.inspect
584
+ # end
585
+ #
586
+ # @return [Enumerator]
587
+ # @see http://ruby-doc.org/core-1.9/classes/Enumerable.html
588
+ def each(&block)
589
+ return to_enum unless block_given?
590
+
591
+ each_subject do |subject|
592
+ if value = graph.first_object(:subject => subject, :predicate => RDF.first)
593
+ block.call(value) # FIXME
594
+ end
595
+ end
596
+ end
597
+
598
+ ##
599
+ # Yields each statement constituting this list.
600
+ #
601
+ # @example
602
+ # RDF::List[1, 2, 3].each_statement do |statement|
603
+ # puts statement.inspect
604
+ # end
605
+ #
606
+ # @return [Enumerator]
607
+ # @see RDF::Enumerable#each_statement
608
+ def each_statement(&block)
609
+ return enum_statement unless block_given?
610
+
611
+ each_subject do |subject|
612
+ graph.query(:subject => subject, &block)
613
+ end
614
+ end
615
+
616
+ ##
617
+ # Returns a string created by converting each element of this list into
618
+ # a string, separated by `sep`.
619
+ #
620
+ # @example
621
+ # RDF::List[1, 2, 3].join #=> "123"
622
+ # RDF::List[1, 2, 3].join(", ") #=> "1, 2, 3"
623
+ #
624
+ # @param [String] sep
625
+ # @return [String]
626
+ # @see http://ruby-doc.org/core-1.9/classes/Array.html#M000438
627
+ def join(sep = $,)
628
+ map(&:to_s).join(sep)
629
+ end
630
+
631
+ ##
632
+ # Returns the elements in this list in reversed order.
633
+ #
634
+ # @example
635
+ # RDF::List[1, 2, 3].reverse #=> RDF::List[3, 2, 1]
636
+ #
637
+ # @return [RDF::List]
638
+ # @see http://ruby-doc.org/core-1.9/classes/Array.html#M000439
639
+ def reverse
640
+ RDF::List[*to_a.reverse]
641
+ end
642
+
643
+ ##
644
+ # Returns the elements in this list in sorted order.
645
+ #
646
+ # @example
647
+ # RDF::List[2, 3, 1].sort #=> RDF::List[1, 2, 3]
648
+ #
649
+ # @return [RDF::List]
650
+ # @see http://ruby-doc.org/core-1.9/classes/Enumerable.html#M003038
651
+ def sort(&block)
652
+ RDF::List[*super]
653
+ end
654
+
655
+ ##
656
+ # Returns the elements in this list in sorted order.
657
+ #
658
+ # @example
659
+ # RDF::List[2, 3, 1].sort_by(&:to_i) #=> RDF::List[1, 2, 3]
660
+ #
661
+ # @return [RDF::List]
662
+ # @see http://ruby-doc.org/core-1.9/classes/Enumerable.html#M003039
663
+ def sort_by(&block)
664
+ RDF::List[*super]
665
+ end
666
+
667
+ ##
668
+ # Returns a new list with the duplicates in this list removed.
669
+ #
670
+ # @example
671
+ # RDF::List[1, 2, 2, 3].uniq #=> RDF::List[1, 2, 3]
672
+ #
673
+ # @return [RDF::List]
674
+ # @see http://ruby-doc.org/core-1.9/classes/Array.html#M000471
675
+ def uniq
676
+ RDF::List[*to_a.uniq]
677
+ end
678
+
679
+ ##
680
+ # Returns the elements in this list as an array.
681
+ #
682
+ # @example
683
+ # RDF::List[].to_a #=> []
684
+ # RDF::List[1, 2, 3].to_a #=> [1, 2, 3]
685
+ #
686
+ # @return [Array]
687
+ def to_a
688
+ each.to_a
689
+ end
690
+
691
+ ##
692
+ # Returns the elements in this list as a set.
693
+ #
694
+ # @example
695
+ # RDF::List[1, 2, 3].to_set #=> Set[1, 2, 3]
696
+ #
697
+ # @return [Set]
698
+ def to_set
699
+ require 'set' unless defined?(::Set)
700
+ each.to_set
701
+ end
702
+
703
+ ##
704
+ # Returns a string representation of this list.
705
+ #
706
+ # @example
707
+ # RDF::List[].to_s #=> "RDF::List[]"
708
+ # RDF::List[1, 2, 3].to_s #=> "RDF::List[1, 2, 3]"
709
+ #
710
+ # @return [String]
711
+ def to_s
712
+ 'RDF::List[' + join(', ') + ']'
713
+ end
714
+
715
+ ##
716
+ # Returns a developer-friendly representation of this list.
717
+ #
718
+ # @example
719
+ # RDF::List[].inspect #=> "#<RDF::List(_:g2163790380)>"
720
+ #
721
+ # @return [String]
722
+ def inspect
723
+ if self.equal?(NIL)
724
+ 'RDF::List::NIL'
725
+ else
726
+ #sprintf("#<%s:%#0x(%s)>", self.class.name, __id__, subject.to_s)
727
+ sprintf("#<%s:%#0x(%s)>", self.class.name, __id__, to_s) # FIXME
728
+ end
729
+ end
730
+ end
731
+ end