rdf 3.1.7 → 3.1.12

Sign up to get free protection for your applications and to get access to all the features.
@@ -57,11 +57,21 @@ module RDF
57
57
  end
58
58
 
59
59
  ##
60
- # Returns `true` if `self` is a {RDF::Term}.
60
+ # @overload term?
61
+ # Returns `true` if `self` is a {RDF::Term}.
61
62
  #
62
- # @return [Boolean]
63
- def term?
64
- true
63
+ # @return [Boolean]
64
+ # @overload term?(name)
65
+ # Returns `true` if `self` contains the given RDF subject term.
66
+ #
67
+ # @param [RDF::Resource] value
68
+ # @return [Boolean]
69
+ def term?(*args)
70
+ case args.length
71
+ when 0 then true
72
+ when 1 then false
73
+ else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)")
74
+ end
65
75
  end
66
76
 
67
77
  ##
@@ -72,6 +82,14 @@ module RDF
72
82
  self
73
83
  end
74
84
 
85
+ ##
86
+ # Returns an array including just itself.
87
+ #
88
+ # @return [Array<RDF::Value>]
89
+ def terms
90
+ [self]
91
+ end
92
+
75
93
  ##
76
94
  # Returns the base representation of this term.
77
95
  #
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)
@@ -419,7 +416,7 @@ module RDF
419
416
  # @see http://tools.ietf.org/html/rfc3986#section-5.2.2
420
417
  # @see http://tools.ietf.org/html/rfc3986#section-5.2.3
421
418
  def join(*uris)
422
- 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)}
423
420
 
424
421
  uris.each do |uri|
425
422
  uri = RDF::URI.new(uri) unless uri.is_a?(RDF::URI)
@@ -583,7 +580,7 @@ module RDF
583
580
  else
584
581
  RDF::URI.new(
585
582
  **object.merge(path: '/').
586
- keep_if {|k, v| [:scheme, :authority, :path].include?(k)})
583
+ keep_if {|k, v| %i(scheme authority path).include?(k)})
587
584
  end
588
585
  end
589
586
 
@@ -591,13 +588,14 @@ module RDF
591
588
  # Returns `true` if this URI is hierarchical and it's path component isn't equal to `/`.
592
589
  #
593
590
  # @example
594
- # RDF::URI('http://example.org/').has_parent? #=> false
595
- # 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
596
593
  #
597
594
  # @return [Boolean] `true` or `false`
598
- def has_parent?
595
+ def parent?
599
596
  !root?
600
597
  end
598
+ alias_method :has_parent?, :parent?
601
599
 
602
600
  ##
603
601
  # Returns a copy of this URI with the path component ascended to the
@@ -1125,7 +1123,7 @@ module RDF
1125
1123
  # @param [String, #to_s] value
1126
1124
  # @return [RDF::URI] self
1127
1125
  def authority=(value)
1128
- 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)}
1129
1127
  object[:authority] = (value.to_s.dup.force_encoding(Encoding::UTF_8) if value)
1130
1128
  user; password; userinfo; host; port
1131
1129
  @value = nil
@@ -1155,7 +1153,7 @@ module RDF
1155
1153
  # @param [String, #to_s] value
1156
1154
  # @return [RDF::URI] self
1157
1155
  def userinfo=(value)
1158
- object.delete_if {|k, v| [:user, :password, :authority].include?(k)}
1156
+ object.delete_if {|k, v| %i(user password authority).include?(k)}
1159
1157
  object[:userinfo] = (value.to_s.dup.force_encoding(Encoding::UTF_8) if value)
1160
1158
  user; password; authority
1161
1159
  @value = nil
@@ -29,19 +29,38 @@ module RDF
29
29
  # @see RDF::Statement
30
30
  module Value
31
31
  ##
32
- # Returns `true` if `self` is a {RDF::Graph}.
32
+ # @overload graph?
33
+ # Returns `true` if `self` is a {RDF::Graph}.
33
34
  #
34
- # @return [Boolean]
35
- def graph?
36
- false
35
+ # @return [Boolean]
36
+ # @overload graph?(name)
37
+ # Returns `true` if `self` contains the given RDF graph_name.
38
+ #
39
+ # @param [RDF::Resource, false] graph_name
40
+ # Use value `false` to query for the default graph_name
41
+ # @return [Boolean]
42
+ def graph?(*args)
43
+ case args.length
44
+ when 0, 1 then false
45
+ else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)")
46
+ end
37
47
  end
38
48
 
39
49
  ##
40
- # Is this a {RDF::Statement}?
50
+ # @overload statement?
51
+ # Returns `true` if `self` is a {RDF::Statement}.
41
52
  #
42
- # @return [Boolean]
43
- def statement?
44
- false
53
+ # @return [Boolean]
54
+ # @overload statement?(statement)
55
+ # Returns `true` if `self` contains the given {RDF::Statement}.
56
+ #
57
+ # @param [RDF::Statement] statement
58
+ # @return [Boolean]
59
+ def statement?(*args)
60
+ case args.length
61
+ when 0, 1 then false
62
+ else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)")
63
+ end
45
64
  end
46
65
 
47
66
  ##
@@ -53,11 +72,20 @@ module RDF
53
72
  end
54
73
 
55
74
  ##
56
- # Is this a {RDF::Term}?
75
+ # @overload term?
76
+ # Returns `true` if `self` is a {RDF::Term}.
57
77
  #
58
- # @return [Boolean]
59
- def term?
60
- false
78
+ # @return [Boolean]
79
+ # @overload term?(name)
80
+ # Returns `true` if `self` contains the given RDF subject term.
81
+ #
82
+ # @param [RDF::Resource] value
83
+ # @return [Boolean]
84
+ def term?(*args)
85
+ case args.length
86
+ when 0, 1 then false
87
+ else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)")
88
+ end
61
89
  end
62
90
 
63
91
  ##
@@ -103,12 +131,21 @@ module RDF
103
131
  end
104
132
 
105
133
  ##
106
- # Is this a {RDF::Query::Variable}, or does it contain a variable?
134
+ # @overload variable?
135
+ # Returns `true` if `self` is a {RDF::Query::Variable}, or does it contain a variable?
107
136
  #
108
- # @return [Boolean]
137
+ # @return [Boolean]
138
+ # @overload variable?(variable)
139
+ # Returns `true` if `self` contains the given variable.
140
+ #
141
+ # @param [RDF::Resource] value
142
+ # @return [Boolean]
109
143
  # @since 0.1.7
110
- def variable?
111
- false
144
+ def variable?(*args)
145
+ case args.length
146
+ when 0, 1 then false
147
+ else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)")
148
+ end
112
149
  end
113
150
 
114
151
  ##
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
@@ -443,11 +443,22 @@ module RDF
443
443
  end
444
444
 
445
445
  ##
446
- # Returns `true` if any pattern contains a variable.
446
+ # @overload variable?
447
+ # Returns `true` if any pattern contains a variable.
447
448
  #
448
- # @return [Boolean]
449
- def variable?
450
- !variables.empty?
449
+ # @return [Boolean]
450
+ # @overload variable?(variables)
451
+ # Returns `true` if any pattern contains any of the variables.
452
+ #
453
+ # @param [Array<Symbol, #to_sym>] variables
454
+ # @return [Boolean]
455
+ def variable?(*args)
456
+ case args.length
457
+ when 0 then !variables.empty?
458
+ when 1
459
+ patterns.any? {|p| p.variable?(*args)}
460
+ else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)")
461
+ end
451
462
  end
452
463
  alias_method :variables?, :variable?
453
464
  alias_method :has_variables?, :variable?
@@ -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)
@@ -114,16 +114,27 @@ class RDF::Query
114
114
  end
115
115
 
116
116
  ##
117
- # Returns `true` if this solution contains bindings for any of the given
117
+ # @overload variable?
118
+ # Returns `false`.
119
+ #
120
+ # @return [Boolean]
121
+ # @overload variable?(variables)
122
+ # Returns `true` if this solution contains bindings for any of the given
118
123
  # `variables`.
119
124
  #
120
- # @param [Array<Symbol, #to_sym>] variables
121
- # an array of variables to check
122
- # @return [Boolean] `true` or `false`
125
+ # @param [Array<Symbol, #to_sym>] variables
126
+ # @return [Boolean]
123
127
  # @since 0.3.0
124
- def has_variables?(variables)
125
- variables.any? { |variable| bound?(variable) }
128
+ def variable?(*args)
129
+ case args.length
130
+ when 0 then false
131
+ when 1
132
+ args.first.any? { |variable| bound?(variable) }
133
+ else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)")
134
+ end
126
135
  end
136
+ alias_method :variables?, :variable?
137
+ alias_method :has_variables?, :variable?
127
138
 
128
139
  ##
129
140
  # Enumerates over every variable in this solution.
@@ -253,7 +264,7 @@ class RDF::Query
253
264
  # @see http://www.w3.org/TR/2013/REC-sparql11-query-20130321/#defn_algCompatibleMapping
254
265
  def compatible?(other)
255
266
  @bindings.all? do |k, v|
256
- !other.to_h.has_key?(k) || other[k].eql?(v)
267
+ !other.to_h.key?(k) || other[k].eql?(v)
257
268
  end
258
269
  end
259
270
 
@@ -267,7 +278,7 @@ class RDF::Query
267
278
  # @see http://www.w3.org/TR/2013/REC-sparql11-query-20130321/#defn_algMinus
268
279
  def disjoint?(other)
269
280
  @bindings.none? do |k, v|
270
- v && other.to_h.has_key?(k) && other[k].eql?(v)
281
+ v && other.to_h.key?(k) && other[k].eql?(v)
271
282
  end
272
283
  end
273
284
 
@@ -281,7 +292,7 @@ class RDF::Query
281
292
  # @return [Boolean]
282
293
  def isomorphic_with?(other)
283
294
  @bindings.all? do |k, v|
284
- !other.to_h.has_key?(k) || other[k].eql?(v)
295
+ !other.to_h.key?(k) || other[k].eql?(v)
285
296
  end
286
297
  end
287
298
 
@@ -332,7 +343,7 @@ class RDF::Query
332
343
  # @param [Symbol] name
333
344
  # @return [RDF::Term]
334
345
  def method_missing(name, *args, &block)
335
- if args.empty? && @bindings.has_key?(name.to_sym)
346
+ if args.empty? && @bindings.key?(name.to_sym)
336
347
  @bindings[name.to_sym]
337
348
  else
338
349
  super # raises NoMethodError
@@ -342,7 +353,7 @@ class RDF::Query
342
353
  ##
343
354
  # @return [Boolean]
344
355
  def respond_to_missing?(name, include_private = false)
345
- @bindings.has_key?(name.to_sym) || super
356
+ @bindings.key?(name.to_sym) || super
346
357
  end
347
358
 
348
359
  ##