rdf 3.1.5 → 3.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3024b8471983ae96eef2df1b95ef2fc1d72bba082d43e3567c9e8c30970cb6db
4
- data.tar.gz: 3104532cffa328bb403fe3403deb30188ceb00a119b8f4e1dbccc7c01fd69712
3
+ metadata.gz: 4a2f065f2ef318b5183512ee7cbe816d3c86cf497a1822d886d1a5cb12ae7c7c
4
+ data.tar.gz: b51e9e8d120f8441da1cc4478a4a8bf470010cd8009eef992fb91d39fce2aeea
5
5
  SHA512:
6
- metadata.gz: 2b62e3081278305f44f09c7aa77d3143a5110779bd1963e702ffde2ffacae8fa4ee0abcb861441675ea70c409f33270e7f4a2e0cf45c66c4956188bebef6ae80
7
- data.tar.gz: 23674e4180517963dd92f26ac4d40c7c3eea672be96e64f8d719ff94c084ef229e5e21473d1d4dd0336fbb8ae3ad2c3b200ff1f7da6b7a352ead2ff98d4dd83c
6
+ metadata.gz: efe71f0a3278321dacffe85afac3b9fad8fd9c0be2736689ced8fcfa27164cf1f9a0221438c851fe8d61f375889ab6ca124f026a6a675b64b38792aa5c9f7e91
7
+ data.tar.gz: c9228398f6a2e3236bdfc878bbe083713648acee8e26c72a1bed358deea5041420667167976607b2942f24b026ab3d6aae03c046632ca41e6fa9760f997d752e
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.1.5
1
+ 3.1.6
@@ -45,7 +45,7 @@ module RDF
45
45
  # identified by `subject`, but will be invalid.
46
46
  #
47
47
  # @example add constructed list to existing graph
48
- # l = RDF::List(nil, nil (1, 2, 3))
48
+ # l = RDF::List(values: (1, 2, 3))
49
49
  # g = RDF::Graph.new << l
50
50
  # g.count # => l.count
51
51
  #
@@ -68,8 +68,8 @@ module RDF
68
68
  if first || values.length > 0
69
69
  # Intantiate the list from values, and insert the first value using subject.
70
70
  values.reverse_each {|value| self.unshift(value)}
71
- graph.insert RDF::Statement(subject, RDF.first, first || RDF.nil)
72
- graph.insert RDF::Statement(subject, RDF.rest, @subject)
71
+ @graph.insert RDF::Statement(subject, RDF.first, first || RDF.nil)
72
+ @graph.insert RDF::Statement(subject, RDF.rest, @subject)
73
73
  end
74
74
  @subject = subject
75
75
  else
@@ -175,7 +175,7 @@ module RDF
175
175
  # @return [RDF::List]
176
176
  # @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-26
177
177
  def &(other)
178
- RDF::List[*(to_a & other.to_a)]
178
+ self.class.new(values: (to_a & other.to_a))
179
179
  end
180
180
 
181
181
  ##
@@ -193,7 +193,7 @@ module RDF
193
193
  # @return [RDF::List]
194
194
  # @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-7C
195
195
  def |(other)
196
- RDF::List[*(to_a | other.to_a)]
196
+ self.class.new(values: (to_a | other.to_a))
197
197
  end
198
198
 
199
199
  ##
@@ -206,7 +206,7 @@ module RDF
206
206
  # @return [RDF::List]
207
207
  # @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-2B
208
208
  def +(other)
209
- RDF::List[*(to_a + other.to_a)]
209
+ self.class.new(values: (to_a + other.to_a))
210
210
  end
211
211
 
212
212
  ##
@@ -220,7 +220,7 @@ module RDF
220
220
  # @return [RDF::List]
221
221
  # @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-2D
222
222
  def -(other)
223
- RDF::List[*(to_a - other.to_a)]
223
+ self.class.new(values: (to_a - other.to_a))
224
224
  end
225
225
 
226
226
  ##
@@ -250,7 +250,7 @@ module RDF
250
250
  # @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-2A
251
251
  def *(int_or_str)
252
252
  case int_or_str
253
- when Integer then RDF::List[*(to_a * int_or_str)]
253
+ when Integer then self.class.new(values: (to_a * int_or_str))
254
254
  else join(int_or_str.to_s)
255
255
  end
256
256
  end
@@ -538,13 +538,13 @@ module RDF
538
538
  ##
539
539
  # @private
540
540
  def slice_with_start_and_length(start, length)
541
- RDF::List[*to_a.slice(start, length)]
541
+ self.class.new(values: to_a.slice(start, length))
542
542
  end
543
543
 
544
544
  ##
545
545
  # @private
546
546
  def slice_with_range(range)
547
- RDF::List[*to_a.slice(range)]
547
+ self.class.new(values: to_a.slice(range))
548
548
  end
549
549
 
550
550
  protected :slice_with_start_and_length
@@ -851,7 +851,7 @@ module RDF
851
851
  # @return [RDF::List]
852
852
  # @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-reverse
853
853
  def reverse
854
- RDF::List[*to_a.reverse]
854
+ self.class.new(values: to_a.reverse)
855
855
  end
856
856
 
857
857
  ##
@@ -863,7 +863,7 @@ module RDF
863
863
  # @return [RDF::List]
864
864
  # @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-sort
865
865
  def sort(&block)
866
- RDF::List[*super]
866
+ self.class.new(values: super)
867
867
  end
868
868
 
869
869
  ##
@@ -875,7 +875,7 @@ module RDF
875
875
  # @return [RDF::List]
876
876
  # @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-sort_by
877
877
  def sort_by(&block)
878
- RDF::List[*super]
878
+ self.class.new(values: super)
879
879
  end
880
880
 
881
881
  ##
@@ -887,7 +887,7 @@ module RDF
887
887
  # @return [RDF::List]
888
888
  # @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-uniq
889
889
  def uniq
890
- RDF::List[*to_a.uniq]
890
+ self.class.new(values: to_a.uniq)
891
891
  end
892
892
 
893
893
  ##
@@ -964,7 +964,7 @@ module RDF
964
964
  case value
965
965
  when nil then RDF.nil
966
966
  when RDF::Value then value
967
- when Array then RDF::List.new(subject: nil, graph: graph, values: value)
967
+ when Array then self.class.new(subject: nil, graph: graph, values: value)
968
968
  else value
969
969
  end
970
970
  end
@@ -118,6 +118,7 @@ module RDF
118
118
  when ::Integer then RDF::Literal::Integer
119
119
  when ::Float then RDF::Literal::Double
120
120
  when ::BigDecimal then RDF::Literal::Decimal
121
+ when ::Rational then RDF::Literal::Double
121
122
  when ::DateTime then RDF::Literal::DateTime
122
123
  when ::Time then RDF::Literal::DateTime
123
124
  when ::Date then RDF::Literal::Date
@@ -63,9 +63,9 @@ module RDF; class Literal
63
63
  ##
64
64
  # Returns the number with no fractional part that is closest to the argument. If there are two such numbers, then the one that is closest to positive infinity is returned. An error is raised if arg is not a numeric value.
65
65
  #
66
- # @return [RDF::Literal]
66
+ # @return [RDF::Literal::Integer]
67
67
  def round
68
- self.class.new(to_d.round)
68
+ RDF::Literal::Integer.new(to_d.round)
69
69
  end
70
70
 
71
71
  ##
@@ -74,9 +74,9 @@ module RDF; class Literal
74
74
  # @example
75
75
  # RDF::Literal(1).ceil #=> RDF::Literal(1)
76
76
  #
77
- # @return [RDF::Literal]
77
+ # @return [RDF::Literal::Integer]
78
78
  def ceil
79
- self.class.new(to_d.ceil)
79
+ RDF::Literal::Integer.new(to_d.ceil)
80
80
  end
81
81
 
82
82
  ##
@@ -85,9 +85,9 @@ module RDF; class Literal
85
85
  # @example
86
86
  # RDF::Literal(1).floor #=> RDF::Literal(1)
87
87
  #
88
- # @return [RDF::Literal]
88
+ # @return [RDF::Literal::Integer]
89
89
  def floor
90
- self.class.new(to_d.floor)
90
+ RDF::Literal::Integer.new(to_d.floor)
91
91
  end
92
92
 
93
93
  ##
@@ -142,7 +142,7 @@ module RDF; class Literal
142
142
  end
143
143
 
144
144
  ##
145
- # Returns the smallest number greater than or equal to `self`.
145
+ # Returns the smallest integer greater than or equal to `self`.
146
146
  #
147
147
  # @example
148
148
  # RDF::Literal(1.2).ceil #=> RDF::Literal(2)
@@ -150,14 +150,14 @@ module RDF; class Literal
150
150
  # RDF::Literal(2.0).ceil #=> RDF::Literal(2)
151
151
  # RDF::Literal(-2.0).ceil #=> RDF::Literal(-2)
152
152
  #
153
- # @return [RDF::Literal]
153
+ # @return [RDF::Literal::Integer]
154
154
  # @since 0.2.3
155
155
  def ceil
156
- self.class.new(to_f.ceil)
156
+ RDF::Literal::Integer.new(to_f.ceil)
157
157
  end
158
158
 
159
159
  ##
160
- # Returns the largest number less than or equal to `self`.
160
+ # Returns the largest integer less than or equal to `self`.
161
161
  #
162
162
  # @example
163
163
  # RDF::Literal(1.2).floor #=> RDF::Literal(1)
@@ -165,10 +165,10 @@ module RDF; class Literal
165
165
  # RDF::Literal(2.0).floor #=> RDF::Literal(2)
166
166
  # RDF::Literal(-2.0).floor #=> RDF::Literal(-2)
167
167
  #
168
- # @return [RDF::Literal]
168
+ # @return [RDF::Literal::Integer]
169
169
  # @since 0.2.3
170
170
  def floor
171
- self.class.new(to_f.floor)
171
+ RDF::Literal::Integer.new(to_f.floor)
172
172
  end
173
173
 
174
174
  ##
@@ -181,11 +181,11 @@ module RDF; class Literal
181
181
  end
182
182
 
183
183
  ##
184
- # Returns the number with no fractional part that is closest to the argument. If there are two such numbers, then the one that is closest to positive infinity is returned. An error is raised if arg is not a numeric value.
184
+ # Returns the integer with no fractional part that is closest to the argument. If there are two such numbers, then the one that is closest to positive infinity is returned. An error is raised if arg is not a numeric value.
185
185
  #
186
- # @return [RDF::Literal]
186
+ # @return [RDF::Literal::Integer]
187
187
  def round
188
- self.class.new(to_f.round)
188
+ RDF::Literal::Integer.new(to_f.round)
189
189
  end
190
190
 
191
191
  ##
@@ -123,6 +123,40 @@ module RDF; class Literal
123
123
  end
124
124
  end
125
125
 
126
+ ##
127
+ # Exponent − Performs exponential (power) calculation on operators.
128
+ #
129
+ # Promotes values, as necessary, with the result type depending on the input values.
130
+ #
131
+ # @param [Literal::Numeric, #to_i, #to_f, #to_d] other
132
+ # @return [RDF::Literal::Numeric]
133
+ # @since 0.2.3
134
+ # @see https://www.w3.org/TR/xpath-functions/#func-math-pow
135
+ def **(other)
136
+ RDF::Literal(object ** (other.is_a?(RDF::Literal::Numeric) ? other.object : other))
137
+ rescue ZeroDivisionError
138
+ RDF::Literal::Double.new('INF')
139
+ end
140
+
141
+ ##
142
+ # Exponent − Performs remainder of `self` divided by `other`.
143
+ #
144
+ # @param [Literal::Numeric, #to_i, #to_f, #to_d] other
145
+ # @return [RDF::Literal]
146
+ # @since 0.2.3
147
+ # @see https://www.w3.org/TR/xpath-functions/#func-numeric-mod
148
+ def %(other)
149
+ if self.class == Double || [Double, ::Float].include?(other.class)
150
+ self.class.new(to_f % other.to_f)
151
+ elsif ((self.class == RDF::Literal::Float || other.class == RDF::Literal::Float) rescue false)
152
+ self.class.new(to_f % other.to_f)
153
+ elsif self.class == Decimal || other.class == Decimal
154
+ self.class.new(to_d % (other.respond_to?(:to_d) ? other.to_d : BigDecimal(other.to_s)))
155
+ else
156
+ self.class.new(to_i % other.to_i)
157
+ end
158
+ end
159
+
126
160
  ##
127
161
  # Returns the quotient of `self` divided by `other`.
128
162
  #
@@ -247,15 +247,22 @@ module RDF
247
247
  # Optimizes this query by reordering its constituent triple patterns
248
248
  # according to their cost estimates.
249
249
  #
250
+ # Optional patterns have greater cost than non-optional patterns so they will always come after non-optional patterns
251
+ #
250
252
  # @param [Hash{Symbol => Object}] options
251
253
  # any additional options for optimization
252
254
  # @return [self]
253
255
  # @see RDF::Query::Pattern#cost
254
256
  # @since 0.3.0
255
257
  def optimize!(**options)
256
- @patterns.sort! do |a, b|
258
+ optional, required = @patterns.partition(&:optional?)
259
+ required.sort! do |a, b|
260
+ (a.cost || 0) <=> (b.cost || 0)
261
+ end
262
+ optional.sort! do |a, b|
257
263
  (a.cost || 0) <=> (b.cost || 0)
258
264
  end
265
+ @patterns = required + optional
259
266
  self
260
267
  end
261
268
 
@@ -181,16 +181,10 @@ module RDF; class Query
181
181
 
182
182
  # No, some terms actually refer to the same variable...
183
183
  else
184
- # Figure out which terms refer to the same variable:
185
- terms = variables.each_key.find do |name|
186
- terms = variable_terms(name)
187
- break terms if terms.size > 1
188
- end
184
+ # Considering embedding, figure out if variables that may appear more than once resolve to the same value.
185
+ vars = variables.keys
189
186
  queryable.query(query).select do |statement|
190
- # Only yield those matching statements where the variable
191
- # constraint is also satisfied:
192
- # FIXME: `Array#uniq` uses `#eql?` and `#hash`, not `#==`
193
- if terms.map { |term| statement.send(term) }.uniq.size.equal?(1)
187
+ if vars.all? {|var| self.var_values(var, statement).uniq.size == 1}
194
188
  yield statement if block_given?
195
189
  true
196
190
  end
@@ -220,8 +214,8 @@ module RDF; class Query
220
214
  solution[predicate.to_sym] = statement.predicate if predicate.is_a?(Variable)
221
215
  solution[object.to_sym] = statement.object if object.is_a?(Variable)
222
216
  solution[graph_name.to_sym] = statement.graph_name if graph_name.is_a?(Variable)
223
- solution.merge!(subject.solution(statement.subject)) if subject.is_a?(Pattern)
224
- solution.merge!(object.solution(statement.object)) if object.is_a?(Pattern)
217
+ solution.merge!(subject.solution(statement.subject)) if subject.respond_to?(:solution)
218
+ solution.merge!(object.solution(statement.object)) if object.respond_to?(:solution)
225
219
  end
226
220
  end
227
221
 
@@ -234,8 +228,11 @@ module RDF; class Query
234
228
  # @param [Symbol, #to_sym] name
235
229
  # an optional variable name
236
230
  # @return [Array<Symbol>]
231
+ # @deprecated use {#var_values} instead
237
232
  # @since 0.3.0
238
233
  def variable_terms(name = nil)
234
+ warn "[DEPRECATION] RDF::Query::Pattern#variable_terms is deprecated and will be removed in a future version.\n" +
235
+ "Called from #{Gem.location_of_caller.join(':')}"
239
236
  terms = []
240
237
  terms << :subject if subject.is_a?(Variable) && (!name || name.eql?(subject.name))
241
238
  terms << :predicate if predicate.is_a?(Variable) && (!name || name.eql?(predicate.name))
@@ -244,6 +241,20 @@ module RDF; class Query
244
241
  terms
245
242
  end
246
243
 
244
+ ##
245
+ # Returns all values the statement in the same pattern position
246
+ #
247
+ # @param [Symbol] var
248
+ # @param [RDF::Statement] statement
249
+ # @return [Array<RDF::Term>]
250
+ def var_values(var, statement)
251
+ [:subject, :predicate, :object, :graph_name].map do |position|
252
+ po = self.send(position)
253
+ so = statement.send(position)
254
+ po.var_values(var, so) if po.respond_to?(:var_values)
255
+ end.flatten.compact
256
+ end
257
+
247
258
  ##
248
259
  # Returns the number of variables in this pattern.
249
260
  #
@@ -254,7 +265,7 @@ module RDF; class Query
254
265
  def variable_count
255
266
  [subject, predicate, object, graph_name].inject(0) do |memo, term|
256
267
  memo += (term.is_a?(Variable) ? 1 :
257
- (term.is_a?(Pattern) ? term.variable_count : 0))
268
+ (term.respond_to?(:variable_count) ? term.variable_count : 0))
258
269
  end
259
270
  end
260
271
  alias_method :cardinality, :variable_count
@@ -311,9 +322,9 @@ module RDF; class Query
311
322
  def bindings
312
323
  bindings = {}
313
324
  bindings.merge!(subject.bindings) if subject && subject.variable?
314
- bindings.merge!(predicate.bindings) if predicate.is_a?(Variable)
325
+ bindings.merge!(predicate.bindings) if predicate && predicate.variable?
315
326
  bindings.merge!(object.bindings) if object && object.variable?
316
- bindings.merge!(graph_name.bindings) if graph_name.is_a?(Variable)
327
+ bindings.merge!(graph_name.bindings) if graph_name && graph_name.variable?
317
328
  bindings
318
329
  end
319
330
 
@@ -354,24 +365,7 @@ module RDF; class Query
354
365
  #
355
366
  # @return [String]
356
367
  def to_s
357
- StringIO.open do |buffer| # FIXME in RDF::Statement
358
- buffer << 'OPTIONAL ' if optional?
359
- buffer << [subject, predicate, object].map do |r|
360
- if r.is_a?(RDF::Query::Variable)
361
- r.to_s
362
- elsif r.is_a?(RDF::Query::Pattern)
363
- "<<#{r.to_s[0..-3]}>>"
364
- else
365
- RDF::NTriples.serialize(r)
366
- end
367
- end.join(" ")
368
- buffer << case graph_name
369
- when nil, false then " ."
370
- when Variable then " #{graph_name.to_s} ."
371
- else " #{RDF::NTriples.serialize(graph_name)} ."
372
- end
373
- buffer.string
374
- end
368
+ (optional? ? 'OPTIONAL ' : '') + super
375
369
  end
376
370
  end # Pattern
377
371
  end; end # RDF::Query
@@ -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
@@ -410,12 +410,12 @@ module RDF
410
410
  end
411
411
  ss.each do |s, ps|
412
412
  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
413
+ ps
414
+ elsif ps.has_key?(predicate)
415
+ { predicate => ps[predicate] }
416
+ else
417
+ []
418
+ end
419
419
  ps.each do |p, os|
420
420
  os.each do |o, object_options|
421
421
  next unless object.nil? || object.eql?(o)
@@ -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)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rdf
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.5
4
+ version: 3.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arto Bendiken
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2020-08-06 00:00:00.000000000 Z
13
+ date: 2020-09-04 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: link_header