Skip to content

Commit a9e4452

Browse files
committed
Finish 3.1.6
2 parents 6325096 + 51b1daf commit a9e4452

16 files changed

Lines changed: 460 additions & 167 deletions

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.1.5
1+
3.1.6

lib/rdf/model/list.rb

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def self.[](*values)
4545
# identified by `subject`, but will be invalid.
4646
#
4747
# @example add constructed list to existing graph
48-
# l = RDF::List(nil, nil (1, 2, 3))
48+
# l = RDF::List(values: (1, 2, 3))
4949
# g = RDF::Graph.new << l
5050
# g.count # => l.count
5151
#
@@ -68,8 +68,8 @@ def initialize(subject: nil, graph: nil, values: nil, &block)
6868
if first || values.length > 0
6969
# Intantiate the list from values, and insert the first value using subject.
7070
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)
7373
end
7474
@subject = subject
7575
else
@@ -175,7 +175,7 @@ def ==(other)
175175
# @return [RDF::List]
176176
# @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-26
177177
def &(other)
178-
RDF::List[*(to_a & other.to_a)]
178+
self.class.new(values: (to_a & other.to_a))
179179
end
180180

181181
##
@@ -193,7 +193,7 @@ def &(other)
193193
# @return [RDF::List]
194194
# @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-7C
195195
def |(other)
196-
RDF::List[*(to_a | other.to_a)]
196+
self.class.new(values: (to_a | other.to_a))
197197
end
198198

199199
##
@@ -206,7 +206,7 @@ def |(other)
206206
# @return [RDF::List]
207207
# @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-2B
208208
def +(other)
209-
RDF::List[*(to_a + other.to_a)]
209+
self.class.new(values: (to_a + other.to_a))
210210
end
211211

212212
##
@@ -220,7 +220,7 @@ def +(other)
220220
# @return [RDF::List]
221221
# @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-2D
222222
def -(other)
223-
RDF::List[*(to_a - other.to_a)]
223+
self.class.new(values: (to_a - other.to_a))
224224
end
225225

226226
##
@@ -250,7 +250,7 @@ def -(other)
250250
# @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-2A
251251
def *(int_or_str)
252252
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))
254254
else join(int_or_str.to_s)
255255
end
256256
end
@@ -538,13 +538,13 @@ def slice(*args)
538538
##
539539
# @private
540540
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))
542542
end
543543

544544
##
545545
# @private
546546
def slice_with_range(range)
547-
RDF::List[*to_a.slice(range)]
547+
self.class.new(values: to_a.slice(range))
548548
end
549549

550550
protected :slice_with_start_and_length
@@ -851,7 +851,7 @@ def join(sep = $,)
851851
# @return [RDF::List]
852852
# @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-reverse
853853
def reverse
854-
RDF::List[*to_a.reverse]
854+
self.class.new(values: to_a.reverse)
855855
end
856856

857857
##
@@ -863,7 +863,7 @@ def reverse
863863
# @return [RDF::List]
864864
# @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-sort
865865
def sort(&block)
866-
RDF::List[*super]
866+
self.class.new(values: super)
867867
end
868868

869869
##
@@ -875,7 +875,7 @@ def sort(&block)
875875
# @return [RDF::List]
876876
# @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-sort_by
877877
def sort_by(&block)
878-
RDF::List[*super]
878+
self.class.new(values: super)
879879
end
880880

881881
##
@@ -887,7 +887,7 @@ def sort_by(&block)
887887
# @return [RDF::List]
888888
# @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-uniq
889889
def uniq
890-
RDF::List[*to_a.uniq]
890+
self.class.new(values: to_a.uniq)
891891
end
892892

893893
##
@@ -964,7 +964,7 @@ def normalize_value(value)
964964
case value
965965
when nil then RDF.nil
966966
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)
968968
else value
969969
end
970970
end

lib/rdf/model/literal.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ def self.new(value, language: nil, datatype: nil, lexical: nil, validate: false,
118118
when ::Integer then RDF::Literal::Integer
119119
when ::Float then RDF::Literal::Double
120120
when ::BigDecimal then RDF::Literal::Decimal
121+
when ::Rational then RDF::Literal::Double
121122
when ::DateTime then RDF::Literal::DateTime
122123
when ::Time then RDF::Literal::DateTime
123124
when ::Date then RDF::Literal::Date

lib/rdf/model/literal/decimal.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ def abs
6363
##
6464
# 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.
6565
#
66-
# @return [RDF::Literal]
66+
# @return [RDF::Literal::Integer]
6767
def round
68-
self.class.new(to_d.round)
68+
RDF::Literal::Integer.new(to_d.round)
6969
end
7070

7171
##
@@ -74,9 +74,9 @@ def round
7474
# @example
7575
# RDF::Literal(1).ceil #=> RDF::Literal(1)
7676
#
77-
# @return [RDF::Literal]
77+
# @return [RDF::Literal::Integer]
7878
def ceil
79-
self.class.new(to_d.ceil)
79+
RDF::Literal::Integer.new(to_d.ceil)
8080
end
8181

8282
##
@@ -85,9 +85,9 @@ def ceil
8585
# @example
8686
# RDF::Literal(1).floor #=> RDF::Literal(1)
8787
#
88-
# @return [RDF::Literal]
88+
# @return [RDF::Literal::Integer]
8989
def floor
90-
self.class.new(to_d.floor)
90+
RDF::Literal::Integer.new(to_d.floor)
9191
end
9292

9393
##

lib/rdf/model/literal/double.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,33 +142,33 @@ def infinite?
142142
end
143143

144144
##
145-
# Returns the smallest number greater than or equal to `self`.
145+
# Returns the smallest integer greater than or equal to `self`.
146146
#
147147
# @example
148148
# RDF::Literal(1.2).ceil #=> RDF::Literal(2)
149149
# RDF::Literal(-1.2).ceil #=> RDF::Literal(-1)
150150
# RDF::Literal(2.0).ceil #=> RDF::Literal(2)
151151
# RDF::Literal(-2.0).ceil #=> RDF::Literal(-2)
152152
#
153-
# @return [RDF::Literal]
153+
# @return [RDF::Literal::Integer]
154154
# @since 0.2.3
155155
def ceil
156-
self.class.new(to_f.ceil)
156+
RDF::Literal::Integer.new(to_f.ceil)
157157
end
158158

159159
##
160-
# Returns the largest number less than or equal to `self`.
160+
# Returns the largest integer less than or equal to `self`.
161161
#
162162
# @example
163163
# RDF::Literal(1.2).floor #=> RDF::Literal(1)
164164
# RDF::Literal(-1.2).floor #=> RDF::Literal(-2)
165165
# RDF::Literal(2.0).floor #=> RDF::Literal(2)
166166
# RDF::Literal(-2.0).floor #=> RDF::Literal(-2)
167167
#
168-
# @return [RDF::Literal]
168+
# @return [RDF::Literal::Integer]
169169
# @since 0.2.3
170170
def floor
171-
self.class.new(to_f.floor)
171+
RDF::Literal::Integer.new(to_f.floor)
172172
end
173173

174174
##
@@ -181,11 +181,11 @@ def abs
181181
end
182182

183183
##
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.
185185
#
186-
# @return [RDF::Literal]
186+
# @return [RDF::Literal::Integer]
187187
def round
188-
self.class.new(to_f.round)
188+
RDF::Literal::Integer.new(to_f.round)
189189
end
190190

191191
##

lib/rdf/model/literal/numeric.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,40 @@ def *(other)
123123
end
124124
end
125125

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+
126160
##
127161
# Returns the quotient of `self` divided by `other`.
128162
#

lib/rdf/query.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,15 +247,22 @@ def optimize(**options)
247247
# Optimizes this query by reordering its constituent triple patterns
248248
# according to their cost estimates.
249249
#
250+
# Optional patterns have greater cost than non-optional patterns so they will always come after non-optional patterns
251+
#
250252
# @param [Hash{Symbol => Object}] options
251253
# any additional options for optimization
252254
# @return [self]
253255
# @see RDF::Query::Pattern#cost
254256
# @since 0.3.0
255257
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|
257263
(a.cost || 0) <=> (b.cost || 0)
258264
end
265+
@patterns = required + optional
259266
self
260267
end
261268

0 commit comments

Comments
 (0)