rdf 3.1.5 → 3.1.10

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.
@@ -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
  ##
@@ -65,7 +65,7 @@ class RDF::Query
65
65
  # the variable name
66
66
  # @param [RDF::Term] value
67
67
  # an optional variable value
68
- # @param [Boolean] distinguished (true) Also interpreted by leading '??' or '$$' in name.
68
+ # @param [Boolean] distinguished (true) Also interpreted by leading '?' or '$' in name. If non-distinguished, '??' or '$$'.
69
69
  # @param [Boolean] existential (true) Also interpreted by leading '$' in name
70
70
  def initialize(name = nil, value = nil, distinguished: nil, existential: nil)
71
71
  name = (name || "g#{__id__.to_i.abs}").to_s
@@ -246,6 +246,16 @@ class RDF::Query
246
246
  end
247
247
  end
248
248
 
249
+ ##
250
+ # Returns term if var is the same as this variable.
251
+ #
252
+ # @param [Symbol] var
253
+ # @param [RDF::Term] term
254
+ # @return [RDF::Term]
255
+ def var_values(var, term)
256
+ term if var == name
257
+ end
258
+
249
259
  ##
250
260
  # Returns a string representation of this variable.
251
261
  #
@@ -265,5 +275,6 @@ class RDF::Query
265
275
  prefix = distinguished? ? (existential? ? '$' : '?') : (existential? ? '$$' : '??')
266
276
  unbound? ? "#{prefix}#{name}" : "#{prefix}#{name}=#{value}"
267
277
  end
278
+ alias_method :to_base, :to_s
268
279
  end # Variable
269
280
  end # RDF::Query
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,19 +411,19 @@ 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
  []
410
418
  end
411
419
  ss.each do |s, ps|
412
420
  ps = if predicate.nil? || predicate.is_a?(RDF::Query::Variable)
413
- ps
414
- elsif ps.has_key?(predicate)
415
- { predicate => ps[predicate] }
416
- else
417
- []
418
- end
421
+ ps
422
+ elsif ps.key?(predicate)
423
+ { predicate => ps[predicate] }
424
+ else
425
+ []
426
+ end
419
427
  ps.each do |p, os|
420
428
  os.each do |o, object_options|
421
429
  next unless object.nil? || object.eql?(o)
@@ -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
@@ -207,10 +207,16 @@ module RDF
207
207
  end
208
208
 
209
209
  ##
210
- # @see RDF::Enumerable#has_statement?
211
- def has_statement?(statement)
212
- read_target.has_statement?(statement)
210
+ # @overload statement?
211
+ # Returns `false` indicating this is not an RDF::Statemenet.
212
+ # @return [Boolean]
213
+ # @see RDF::Value#statement?
214
+ # @overload statement?(statement)
215
+ # @see RDF::Enumerable#statement?
216
+ def statement?(statement = nil)
217
+ statement && read_target.has_statement?(statement)
213
218
  end
219
+ alias_method :has_statement?, :statement?
214
220
 
215
221
  ##
216
222
  # Returns a developer-friendly representation of this transaction.
@@ -17,6 +17,9 @@ module RDF; module Util
17
17
  # @see http://en.wikipedia.org/wiki/Weak_reference
18
18
  # @since 0.2.0
19
19
  class Cache
20
+ # The configured cache capacity.
21
+ attr_reader :capacity
22
+
20
23
  ##
21
24
  # @private
22
25
  def self.new(*args)
@@ -36,8 +39,8 @@ module RDF; module Util
36
39
 
37
40
  ##
38
41
  # @param [Integer] capacity
39
- def initialize(capacity = -1)
40
- @capacity = capacity
42
+ def initialize(capacity = nil)
43
+ @capacity = capacity || RDF.config.cache_size
41
44
  @cache ||= {}
42
45
  @index ||= {}
43
46
  end
@@ -50,9 +53,10 @@ module RDF; module Util
50
53
 
51
54
  ##
52
55
  # @return [Boolean]
53
- def has_capacity?
56
+ def capacity?
54
57
  @capacity.equal?(-1) || @capacity > @cache.size
55
58
  end
59
+ alias_method :has_capacity?, :capacity?
56
60
 
57
61
  ##
58
62
  # This implementation relies on `ObjectSpace#_id2ref` and performs
@@ -77,7 +81,7 @@ module RDF; module Util
77
81
  # @param [Object] value
78
82
  # @return [Object]
79
83
  def []=(key, value)
80
- if has_capacity?
84
+ if capacity?
81
85
  id = value.__id__
82
86
  @cache[key] = id
83
87
  @index[id] = key
@@ -106,7 +110,7 @@ module RDF; module Util
106
110
  class WeakRefCache < Cache
107
111
  ##
108
112
  # @param [Integer] capacity
109
- def initialize(capacity = -1)
113
+ def initialize(capacity = nil)
110
114
  require 'weakref' unless defined?(::WeakRef)
111
115
  super
112
116
  end
@@ -130,7 +134,7 @@ module RDF; module Util
130
134
  # @param [Object] value
131
135
  # @return [Object]
132
136
  def []=(key, value)
133
- if has_capacity?
137
+ if capacity?
134
138
  @cache[key] = WeakRef.new(value)
135
139
  end
136
140
  value
@@ -285,7 +285,7 @@ module RDF; module Util
285
285
 
286
286
  def respond_to_missing?(name, include_private = false)
287
287
  return true if
288
- [:fatal, :error, :warn, :info, :debug, :level, :sev_threshold]
288
+ %i(fatal error warn info debug level sev_threshold)
289
289
  .include?(name.to_sym)
290
290
  super
291
291
  end
@@ -263,7 +263,7 @@ module RDF
263
263
 
264
264
  def serialize_value(value, key, indent: "")
265
265
  if value.is_a?(Literal) && %w(: comment definition notation note editorialNote).include?(key.to_s)
266
- "%(#{value.to_s.gsub('(', '\(').gsub(')', '\)')}).freeze"
266
+ "#{value.to_s.inspect}.freeze"
267
267
  elsif value.is_a?(RDF::URI)
268
268
  "#{value.pname.inspect}.freeze"
269
269
  elsif value.is_a?(RDF::Vocabulary::Term)
@@ -430,7 +430,7 @@ module RDF
430
430
  # @param [#to_s] property
431
431
  # @return [RDF::URI]
432
432
  def [](property)
433
- if props.has_key?(property.to_sym)
433
+ if props.key?(property.to_sym)
434
434
  props[property.to_sym]
435
435
  else
436
436
  Term.intern([to_s, property.to_s].join(''), vocab: self, attributes: {})
@@ -508,7 +508,7 @@ module RDF
508
508
  #
509
509
  # @return [String]
510
510
  def to_s
511
- @@uris.has_key?(self) ? @@uris[self].to_s : super
511
+ @@uris.key?(self) ? @@uris[self].to_s : super
512
512
  end
513
513
 
514
514
  ##
@@ -731,7 +731,7 @@ module RDF
731
731
  # @param [#to_s] property
732
732
  # @return [URI]
733
733
  def [](property)
734
- Term.intern([to_s, property.to_s].join(''), vocab: self.class, attributes: {})
734
+ Term.intern([to_s, property.to_s].join(''), vocab: self, attributes: {})
735
735
  end
736
736
 
737
737
  ##
@@ -1153,7 +1153,7 @@ module RDF
1153
1153
  :onProperty, :someValuesFrom
1154
1154
  self.restriction?
1155
1155
  when :broader, :exactMatch, :hasTopConcept, :inScheme, :member, :narrower, :related
1156
- @attributes.has_key?(method)
1156
+ @attributes.key?(method)
1157
1157
  else
1158
1158
  super
1159
1159
  end
data/lib/rdf/writer.rb CHANGED
@@ -516,7 +516,7 @@ module RDF
516
516
  when RDF::Literal then format_literal(term, **options)
517
517
  when RDF::URI then format_uri(term, **options)
518
518
  when RDF::Node then format_node(term, **options)
519
- when RDF::Statement then format_rdfstar(term, **options)
519
+ when RDF::Statement then format_embTriple(term, **options)
520
520
  else nil
521
521
  end
522
522
  end
@@ -574,7 +574,7 @@ module RDF
574
574
  # @return [String]
575
575
  # @raise [NotImplementedError] unless implemented in subclass
576
576
  # @abstract
577
- def format_rdfstar(value, **options)
577
+ def format_embTriple(value, **options)
578
578
  raise NotImplementedError.new("#{self.class}#format_statement") # override in subclasses
579
579
  end
580
580