rdf 3.1.6 → 3.1.11

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.
@@ -72,6 +72,14 @@ module RDF
72
72
  self
73
73
  end
74
74
 
75
+ ##
76
+ # Returns an array including just itself.
77
+ #
78
+ # @return [Array<RDF::Value>]
79
+ def terms
80
+ [self]
81
+ end
82
+
75
83
  ##
76
84
  # Returns the base representation of this term.
77
85
  #
data/lib/rdf/model/uri.rb CHANGED
@@ -27,11 +27,6 @@ module RDF
27
27
  class URI
28
28
  include RDF::Resource
29
29
 
30
- ##
31
- # Defines the maximum number of interned URI references that can be held
32
- # cached in memory at any one time.
33
- CACHE_SIZE = -1 # unlimited by default
34
-
35
30
  # IRI components
36
31
  UCSCHAR = Regexp.compile(<<-EOS.gsub(/\s+/, ''))
37
32
  [\\u00A0-\\uD7FF]|[\\uF900-\\uFDCF]|[\\uFDF0-\\uFFEF]|
@@ -117,11 +112,13 @@ module RDF
117
112
  ).freeze
118
113
 
119
114
  ##
115
+ # Cache size may be set through {RDF.config} using `uri_cache_size`.
116
+ #
120
117
  # @return [RDF::Util::Cache]
121
118
  # @private
122
119
  def self.cache
123
120
  require 'rdf/util/cache' unless defined?(::RDF::Util::Cache)
124
- @cache ||= RDF::Util::Cache.new(CACHE_SIZE)
121
+ @cache ||= RDF::Util::Cache.new(RDF.config.uri_cache_size)
125
122
  end
126
123
 
127
124
  ##
@@ -229,13 +226,13 @@ module RDF
229
226
  @value.dup.force_encoding(Encoding::UTF_8) if @value.encoding != Encoding::UTF_8
230
227
  @value.freeze
231
228
  else
232
- %w(
229
+ %i(
233
230
  scheme
234
231
  user password userinfo
235
232
  host port authority
236
233
  path query fragment
237
- ).map(&:to_sym).each do |meth|
238
- if options.has_key?(meth)
234
+ ).each do |meth|
235
+ if options.key?(meth)
239
236
  self.send("#{meth}=".to_sym, options[meth])
240
237
  else
241
238
  self.send(meth)
@@ -311,12 +308,22 @@ module RDF
311
308
  # @param [#to_s] base_uri
312
309
  # @return [RDF::URI]
313
310
  def relativize(base_uri)
314
- if base_uri.to_s.end_with?("/", "#") &&
311
+ if self.to_s.start_with?(base_uri.to_s) && %w(# ?).include?(self.to_s[base_uri.to_s.length, 1]) ||
312
+ base_uri.to_s.end_with?("/", "#") &&
315
313
  self.to_s.start_with?(base_uri.to_s)
316
- RDF::URI(self.to_s[base_uri.to_s.length..-1])
314
+ return RDF::URI(self.to_s[base_uri.to_s.length..-1])
317
315
  else
318
- self
316
+ # Create a list of parents, for which this IRI may be relative.
317
+ u = RDF::URI(base_uri)
318
+ iri_set = u.to_s.end_with?('/') ? [u.to_s] : []
319
+ iri_set << u.to_s while (u = u.parent)
320
+ iri_set.each_with_index do |bb, index|
321
+ next unless self.to_s.start_with?(bb)
322
+ rel = "../" * index + self.to_s[bb.length..-1]
323
+ return rel.empty? ? "./" : rel
324
+ end
319
325
  end
326
+ self
320
327
  end
321
328
 
322
329
  ##
@@ -409,7 +416,7 @@ module RDF
409
416
  # @see http://tools.ietf.org/html/rfc3986#section-5.2.2
410
417
  # @see http://tools.ietf.org/html/rfc3986#section-5.2.3
411
418
  def join(*uris)
412
- joined_parts = object.dup.delete_if {|k, v| [:user, :password, :host, :port].include?(k)}
419
+ joined_parts = object.dup.delete_if {|k, v| %i(user password host port).include?(k)}
413
420
 
414
421
  uris.each do |uri|
415
422
  uri = RDF::URI.new(uri) unless uri.is_a?(RDF::URI)
@@ -573,7 +580,7 @@ module RDF
573
580
  else
574
581
  RDF::URI.new(
575
582
  **object.merge(path: '/').
576
- keep_if {|k, v| [:scheme, :authority, :path].include?(k)})
583
+ keep_if {|k, v| %i(scheme authority path).include?(k)})
577
584
  end
578
585
  end
579
586
 
@@ -581,13 +588,14 @@ module RDF
581
588
  # Returns `true` if this URI is hierarchical and it's path component isn't equal to `/`.
582
589
  #
583
590
  # @example
584
- # RDF::URI('http://example.org/').has_parent? #=> false
585
- # RDF::URI('http://example.org/path/').has_parent? #=> true
591
+ # RDF::URI('http://example.org/').parent? #=> false
592
+ # RDF::URI('http://example.org/path/').parent? #=> true
586
593
  #
587
594
  # @return [Boolean] `true` or `false`
588
- def has_parent?
595
+ def parent?
589
596
  !root?
590
597
  end
598
+ alias_method :has_parent?, :parent?
591
599
 
592
600
  ##
593
601
  # Returns a copy of this URI with the path component ascended to the
@@ -1115,7 +1123,7 @@ module RDF
1115
1123
  # @param [String, #to_s] value
1116
1124
  # @return [RDF::URI] self
1117
1125
  def authority=(value)
1118
- object.delete_if {|k, v| [:user, :password, :host, :port, :userinfo].include?(k)}
1126
+ object.delete_if {|k, v| %i(user password host port userinfo).include?(k)}
1119
1127
  object[:authority] = (value.to_s.dup.force_encoding(Encoding::UTF_8) if value)
1120
1128
  user; password; userinfo; host; port
1121
1129
  @value = nil
@@ -1145,7 +1153,7 @@ module RDF
1145
1153
  # @param [String, #to_s] value
1146
1154
  # @return [RDF::URI] self
1147
1155
  def userinfo=(value)
1148
- object.delete_if {|k, v| [:user, :password, :authority].include?(k)}
1156
+ object.delete_if {|k, v| %i(user password authority).include?(k)}
1149
1157
  object[:userinfo] = (value.to_s.dup.force_encoding(Encoding::UTF_8) if value)
1150
1158
  user; password; authority
1151
1159
  @value = nil
data/lib/rdf/nquads.rb CHANGED
@@ -69,9 +69,9 @@ module RDF
69
69
 
70
70
  begin
71
71
  unless blank? || read_comment
72
- subject = read_uriref || read_node || read_rdfstar || fail_subject
72
+ subject = read_uriref || read_node || read_embTriple || fail_subject
73
73
  predicate = read_uriref(intern: true) || fail_predicate
74
- object = read_uriref || read_node || read_literal || read_rdfstar || fail_object
74
+ object = read_uriref || read_node || read_literal || read_embTriple || fail_object
75
75
  graph_name = read_uriref || read_node
76
76
  if validate? && !read_eos
77
77
  log_error("Expected end of statement (found: #{current_line.inspect})", lineno: lineno, exception: RDF::ReaderError)
@@ -213,7 +213,7 @@ module RDF::NTriples
213
213
  begin
214
214
  read_statement
215
215
  rescue RDF::ReaderError
216
- value = read_uriref || read_node || read_literal || read_rdfstar
216
+ value = read_uriref || read_node || read_literal || read_embTriple
217
217
  log_recover
218
218
  value
219
219
  end
@@ -229,9 +229,9 @@ module RDF::NTriples
229
229
 
230
230
  begin
231
231
  unless blank? || read_comment
232
- subject = read_uriref || read_node || read_rdfstar || fail_subject
232
+ subject = read_uriref || read_node || read_embTriple || fail_subject
233
233
  predicate = read_uriref(intern: true) || fail_predicate
234
- object = read_uriref || read_node || read_literal || read_rdfstar || fail_object
234
+ object = read_uriref || read_node || read_literal || read_embTriple || fail_object
235
235
 
236
236
  if validate? && !read_eos
237
237
  log_error("Expected end of statement (found: #{current_line.inspect})", lineno: lineno, exception: RDF::ReaderError)
@@ -247,11 +247,11 @@ module RDF::NTriples
247
247
 
248
248
  ##
249
249
  # @return [RDF::Statement]
250
- def read_rdfstar
250
+ def read_embTriple
251
251
  if @options[:rdfstar] && match(ST_START)
252
- subject = read_uriref || read_node || read_rdfstar || fail_subject
252
+ subject = read_uriref || read_node || read_embTriple || fail_subject
253
253
  predicate = read_uriref(intern: true) || fail_predicate
254
- object = read_uriref || read_node || read_literal || read_rdfstar || fail_object
254
+ object = read_uriref || read_node || read_literal || read_embTriple || fail_object
255
255
  if !match(ST_END)
256
256
  log_error("Expected end of statement (found: #{current_line.inspect})", lineno: lineno, exception: RDF::ReaderError)
257
257
  end
@@ -227,7 +227,7 @@ module RDF::NTriples
227
227
  # @param [RDF::Statement] statement
228
228
  # @param [Hash{Symbol => Object}] options ({})
229
229
  # @return [String]
230
- def format_rdfstar(statement, **options)
230
+ def format_embTriple(statement, **options)
231
231
  "<<%s %s %s>>" % statement.to_a.map { |value| format_term(value, **options) }
232
232
  end
233
233
  ##
@@ -309,8 +309,8 @@ module RDF::NTriples
309
309
  when RDF::Literal
310
310
  # Note, escaping here is more robust than in Term
311
311
  text = quoted(escaped(literal.value))
312
- text << "@#{literal.language}" if literal.has_language?
313
- text << "^^<#{uri_for(literal.datatype)}>" if literal.has_datatype?
312
+ text << "@#{literal.language}" if literal.language?
313
+ text << "^^<#{uri_for(literal.datatype)}>" if literal.datatype?
314
314
  text
315
315
  else
316
316
  quoted(escaped(literal.to_s))
data/lib/rdf/query.rb CHANGED
@@ -255,7 +255,7 @@ module RDF
255
255
  # @see RDF::Query::Pattern#cost
256
256
  # @since 0.3.0
257
257
  def optimize!(**options)
258
- optional, required = @patterns.partition(&:optional?)
258
+ optional, required = @patterns.uniq.partition(&:optional?)
259
259
  required.sort! do |a, b|
260
260
  (a.cost || 0) <=> (b.cost || 0)
261
261
  end
@@ -85,13 +85,13 @@ module RDF; class Query
85
85
  #
86
86
  # @return [Boolean] `true` or `false`
87
87
  # @since 0.3.0
88
- def has_variables?
88
+ def variables?
89
89
  subject && subject.variable? ||
90
90
  predicate && predicate.variable? ||
91
91
  object && object.variable? ||
92
92
  graph_name && graph_name.variable?
93
93
  end
94
- alias_method :variables?, :has_variables?
94
+ alias_method :has_variables?, :variables?
95
95
 
96
96
  ##
97
97
  # Returns `true` if this is an optional pattern.
@@ -111,10 +111,10 @@ module RDF; class Query
111
111
  #
112
112
  # @return [Boolean] `true` or `false`
113
113
  def valid?
114
- (has_subject? ? (subject.resource? || subject.variable?) && subject.valid? : true) &&
115
- (has_predicate? ? (predicate.uri? || predicate.variable?) && predicate.valid? : true) &&
116
- (has_object? ? (object.term? || object.variable?) && object.valid? : true) &&
117
- (has_graph? ? (graph_name.resource? || graph_name.variable?) && graph_name.valid? : true)
114
+ (subject? ? (subject.resource? || subject.variable?) && subject.valid? : true) &&
115
+ (predicate? ? (predicate.uri? || predicate.variable?) && predicate.valid? : true) &&
116
+ (object? ? (object.term? || object.variable?) && object.valid? : true) &&
117
+ (graph? ? (graph_name.resource? || graph_name.variable?) && graph_name.valid? : true)
118
118
  rescue NoMethodError
119
119
  false
120
120
  end
@@ -248,7 +248,7 @@ module RDF; class Query
248
248
  # @param [RDF::Statement] statement
249
249
  # @return [Array<RDF::Term>]
250
250
  def var_values(var, statement)
251
- [:subject, :predicate, :object, :graph_name].map do |position|
251
+ %i(subject predicate object graph_name).map do |position|
252
252
  po = self.send(position)
253
253
  so = statement.send(position)
254
254
  po.var_values(var, so) if po.respond_to?(:var_values)
@@ -121,9 +121,11 @@ class RDF::Query
121
121
  # an array of variables to check
122
122
  # @return [Boolean] `true` or `false`
123
123
  # @since 0.3.0
124
- def has_variables?(variables)
124
+ def variable?(variables)
125
125
  variables.any? { |variable| bound?(variable) }
126
126
  end
127
+ alias_method :variables?, :variable?
128
+ alias_method :has_variables?, :variable?
127
129
 
128
130
  ##
129
131
  # Enumerates over every variable in this solution.
@@ -253,7 +255,7 @@ class RDF::Query
253
255
  # @see http://www.w3.org/TR/2013/REC-sparql11-query-20130321/#defn_algCompatibleMapping
254
256
  def compatible?(other)
255
257
  @bindings.all? do |k, v|
256
- !other.to_h.has_key?(k) || other[k].eql?(v)
258
+ !other.to_h.key?(k) || other[k].eql?(v)
257
259
  end
258
260
  end
259
261
 
@@ -267,7 +269,7 @@ class RDF::Query
267
269
  # @see http://www.w3.org/TR/2013/REC-sparql11-query-20130321/#defn_algMinus
268
270
  def disjoint?(other)
269
271
  @bindings.none? do |k, v|
270
- v && other.to_h.has_key?(k) && other[k].eql?(v)
272
+ v && other.to_h.key?(k) && other[k].eql?(v)
271
273
  end
272
274
  end
273
275
 
@@ -281,7 +283,7 @@ class RDF::Query
281
283
  # @return [Boolean]
282
284
  def isomorphic_with?(other)
283
285
  @bindings.all? do |k, v|
284
- !other.to_h.has_key?(k) || other[k].eql?(v)
286
+ !other.to_h.key?(k) || other[k].eql?(v)
285
287
  end
286
288
  end
287
289
 
@@ -332,7 +334,7 @@ class RDF::Query
332
334
  # @param [Symbol] name
333
335
  # @return [RDF::Term]
334
336
  def method_missing(name, *args, &block)
335
- if args.empty? && @bindings.has_key?(name.to_sym)
337
+ if args.empty? && @bindings.key?(name.to_sym)
336
338
  @bindings[name.to_sym]
337
339
  else
338
340
  super # raises NoMethodError
@@ -342,7 +344,7 @@ class RDF::Query
342
344
  ##
343
345
  # @return [Boolean]
344
346
  def respond_to_missing?(name, include_private = false)
345
- @bindings.has_key?(name.to_sym) || super
347
+ @bindings.key?(name.to_sym) || super
346
348
  end
347
349
 
348
350
  ##
@@ -84,11 +84,13 @@ module RDF; class Query
84
84
  # @param [Array<Symbol, #to_sym>] variables
85
85
  # an array of variables to check
86
86
  # @return [Boolean] `true` or `false`
87
- # @see RDF::Query::Solution#has_variables?
87
+ # @see RDF::Query::Solution#variable?
88
88
  # @see RDF::Query#execute
89
- def have_variables?(variables)
90
- self.any? { |solution| solution.has_variables?(variables) }
89
+ def variable?(variables)
90
+ self.any? { |solution| solution.variables?(variables) }
91
91
  end
92
+ alias_method :variables?, :variable?
93
+ alias_method :have_variables?, :variable?
92
94
  alias_method :has_variables?, :have_variables?
93
95
 
94
96
  ##
data/lib/rdf/reader.rb CHANGED
@@ -160,10 +160,10 @@ module RDF
160
160
  end,
161
161
  RDF::CLI::Option.new(
162
162
  symbol: :rdfstar,
163
- control: :select,
164
- datatype: [:PG, :SA],
165
- on: ["--rdf-star MODE"],
166
- description: "Parse RDF*, either in Property Graph mode (PG) or Separate Assertions mode (SA).") {|arg| arg.to_sym},
163
+ datatype: TrueClass,
164
+ control: :checkbox,
165
+ on: ["--rdfstar"],
166
+ description: "Parse RDF*."),
167
167
  RDF::CLI::Option.new(
168
168
  symbol: :validate,
169
169
  datatype: TrueClass,
@@ -276,10 +276,8 @@ module RDF
276
276
  # the encoding of the input stream
277
277
  # @param [Boolean] intern (true)
278
278
  # whether to intern all parsed URIs
279
- # @param [:PG, :SA] rdfstar (nil)
279
+ # @param [Boolean] rdfstar (false)
280
280
  # support parsing RDF* statement resources.
281
- # If `:PG`, referenced statements are also emitted.
282
- # If `:SA`, referenced statements are not emitted.
283
281
  # @param [Hash] prefixes (Hash.new)
284
282
  # the prefix mappings to use (not supported by all readers)
285
283
  # @param [Hash{Symbol => Object}] options
@@ -295,7 +293,7 @@ module RDF
295
293
  encoding: Encoding::UTF_8,
296
294
  intern: true,
297
295
  prefixes: Hash.new,
298
- rdfstar: nil,
296
+ rdfstar: false,
299
297
  validate: false,
300
298
  **options,
301
299
  &block)
@@ -401,9 +399,6 @@ module RDF
401
399
  # Statements are yielded in the order that they are read from the input
402
400
  # stream.
403
401
  #
404
- # If the `rdfstar` option is `:PG` and triples include
405
- # embedded statements, they are also enumerated.
406
- #
407
402
  # @overload each_statement
408
403
  # @yield [statement]
409
404
  # each statement
@@ -423,7 +418,6 @@ module RDF
423
418
  loop do
424
419
  st = read_statement
425
420
  block.call(st)
426
- each_pg_statement(st, &block) if options[:rdfstar] == :PG
427
421
  end
428
422
  rescue EOFError
429
423
  rewind rescue nil
@@ -441,9 +435,6 @@ module RDF
441
435
  # Triples are yielded in the order that they are read from the input
442
436
  # stream.
443
437
  #
444
- # If the `rdfstar` option is `:PG` and triples include
445
- # embedded statements, they are also enumerated.
446
- #
447
438
  # @overload each_triple
448
439
  # @yield [subject, predicate, object]
449
440
  # each triple
@@ -464,10 +455,6 @@ module RDF
464
455
  loop do
465
456
  triple = read_triple
466
457
  block.call(*triple)
467
- if options[:rdfstar] == :PG
468
- block.call(*triple[0].to_a) if triple[0].is_a?(Statement)
469
- block.call(*triple[2].to_a) if triple[2].is_a?(Statement)
470
- end
471
458
  end
472
459
  rescue EOFError
473
460
  rewind rescue nil
@@ -20,7 +20,7 @@ module RDF
20
20
  # repository.count
21
21
  #
22
22
  # @example Checking whether a repository contains a specific statement
23
- # repository.has_statement?(statement)
23
+ # repository.statement?(statement)
24
24
  #
25
25
  # @example Enumerating statements in a repository
26
26
  # repository.each_statement { |statement| statement.inspect! }
@@ -69,7 +69,7 @@ module RDF
69
69
  #
70
70
  # @example Transational read from a repository
71
71
  # repository.transaction do |tx|
72
- # tx.has_statement?(statement)
72
+ # tx.statement?(statement)
73
73
  # tx.query([:s, :p, :o])
74
74
  # end
75
75
  #
@@ -288,10 +288,11 @@ module RDF
288
288
 
289
289
  ##
290
290
  # @private
291
- # @see RDF::Enumerable#has_graph?
292
- def has_graph?(graph)
293
- @data.has_key?(graph)
291
+ # @see RDF::Enumerable#graph?
292
+ def graph?(graph)
293
+ @data.key?(graph)
294
294
  end
295
+ alias_method :has_graph?, :graph?
295
296
 
296
297
  ##
297
298
  # @private
@@ -312,12 +313,19 @@ module RDF
312
313
  enum_graph
313
314
  end
314
315
 
316
+
315
317
  ##
316
- # @private
317
- # @see RDF::Enumerable#has_statement?
318
- def has_statement?(statement)
319
- has_statement_in?(@data, statement)
318
+ # @overload statement?
319
+ # Returns `false` indicating this is not an RDF::Statemenet.
320
+ # @return [Boolean]
321
+ # @see RDF::Value#statement?
322
+ # @overload statement?(statement)
323
+ # @private
324
+ # @see RDF::Enumerable#statement?
325
+ def statement?(statement = nil)
326
+ statement && statement_in?(@data, statement)
320
327
  end
328
+ alias_method :has_statement?, :statement?
321
329
 
322
330
  ##
323
331
  # @private
@@ -389,7 +397,7 @@ module RDF
389
397
  predicate = pattern.predicate
390
398
  object = pattern.object
391
399
 
392
- cs = snapshot.has_key?(graph_name) ? { graph_name => snapshot[graph_name] } : snapshot
400
+ cs = snapshot.key?(graph_name) ? { graph_name => snapshot[graph_name] } : snapshot
393
401
 
394
402
  cs.each do |c, ss|
395
403
  next unless graph_name.nil? ||
@@ -403,7 +411,7 @@ module RDF
403
411
  ss.keys.select {|s| s.statement? && subject.eql?(s)}.inject({}) do |memo, st|
404
412
  memo.merge(st => ss[st])
405
413
  end
406
- elsif ss.has_key?(subject)
414
+ elsif ss.key?(subject)
407
415
  { subject => ss[subject] }
408
416
  else
409
417
  []
@@ -411,7 +419,7 @@ module RDF
411
419
  ss.each do |s, ps|
412
420
  ps = if predicate.nil? || predicate.is_a?(RDF::Query::Variable)
413
421
  ps
414
- elsif ps.has_key?(predicate)
422
+ elsif ps.key?(predicate)
415
423
  { predicate => ps[predicate] }
416
424
  else
417
425
  []
@@ -468,16 +476,17 @@ module RDF
468
476
 
469
477
  ##
470
478
  # @private
471
- # @see #has_statement
472
- def has_statement_in?(data, statement)
479
+ # @see #statement?
480
+ def statement_in?(data, statement)
473
481
  s, p, o, g = statement.to_quad
474
482
  g ||= DEFAULT_GRAPH
475
483
 
476
- data.has_key?(g) &&
477
- data[g].has_key?(s) &&
478
- data[g][s].has_key?(p) &&
479
- data[g][s][p].has_key?(o)
484
+ data.key?(g) &&
485
+ data[g].key?(s) &&
486
+ data[g][s].key?(p) &&
487
+ data[g][s][p].key?(o)
480
488
  end
489
+ alias_method :has_statement_in?, :statement_in?
481
490
 
482
491
  ##
483
492
  # @private
@@ -485,7 +494,7 @@ module RDF
485
494
  def insert_to(data, statement)
486
495
  raise ArgumentError, "Statement #{statement.inspect} is incomplete" if statement.incomplete?
487
496
 
488
- unless has_statement_in?(data, statement)
497
+ unless statement_in?(data, statement)
489
498
  s, p, o, c = statement.to_quad
490
499
  c ||= DEFAULT_GRAPH
491
500
 
@@ -504,7 +513,7 @@ module RDF
504
513
  # @private
505
514
  # @return [Hamster::Hash] a new, updated hamster hash
506
515
  def delete_from(data, statement)
507
- if has_statement_in?(data, statement)
516
+ if statement_in?(data, statement)
508
517
  s, p, o, g = statement.to_quad
509
518
  g = DEFAULT_GRAPH unless supports?(:graph_name)
510
519
  g ||= DEFAULT_GRAPH