Skip to content

Commit

Permalink
Finish 3.1.6
Browse files Browse the repository at this point in the history
  • Loading branch information
gkellogg committed Sep 4, 2020
2 parents 6325096 + 51b1daf commit a9e4452
Show file tree
Hide file tree
Showing 16 changed files with 460 additions and 167 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.1.5
3.1.6
30 changes: 15 additions & 15 deletions lib/rdf/model/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def self.[](*values)
# identified by `subject`, but will be invalid.
#
# @example add constructed list to existing graph
# l = RDF::List(nil, nil (1, 2, 3))
# l = RDF::List(values: (1, 2, 3))
# g = RDF::Graph.new << l
# g.count # => l.count
#
Expand All @@ -68,8 +68,8 @@ def initialize(subject: nil, graph: nil, values: nil, &block)
if first || values.length > 0
# Intantiate the list from values, and insert the first value using subject.
values.reverse_each {|value| self.unshift(value)}
graph.insert RDF::Statement(subject, RDF.first, first || RDF.nil)
graph.insert RDF::Statement(subject, RDF.rest, @subject)
@graph.insert RDF::Statement(subject, RDF.first, first || RDF.nil)
@graph.insert RDF::Statement(subject, RDF.rest, @subject)
end
@subject = subject
else
Expand Down Expand Up @@ -175,7 +175,7 @@ def ==(other)
# @return [RDF::List]
# @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-26
def &(other)
RDF::List[*(to_a & other.to_a)]
self.class.new(values: (to_a & other.to_a))
end

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

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

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

##
Expand Down Expand Up @@ -250,7 +250,7 @@ def -(other)
# @see http://ruby-doc.org/core-2.2.2/Array.html#method-i-2A
def *(int_or_str)
case int_or_str
when Integer then RDF::List[*(to_a * int_or_str)]
when Integer then self.class.new(values: (to_a * int_or_str))
else join(int_or_str.to_s)
end
end
Expand Down Expand Up @@ -538,13 +538,13 @@ def slice(*args)
##
# @private
def slice_with_start_and_length(start, length)
RDF::List[*to_a.slice(start, length)]
self.class.new(values: to_a.slice(start, length))
end

##
# @private
def slice_with_range(range)
RDF::List[*to_a.slice(range)]
self.class.new(values: to_a.slice(range))
end

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

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

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

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

##
Expand Down Expand Up @@ -964,7 +964,7 @@ def normalize_value(value)
case value
when nil then RDF.nil
when RDF::Value then value
when Array then RDF::List.new(subject: nil, graph: graph, values: value)
when Array then self.class.new(subject: nil, graph: graph, values: value)
else value
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/rdf/model/literal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ def self.new(value, language: nil, datatype: nil, lexical: nil, validate: false,
when ::Integer then RDF::Literal::Integer
when ::Float then RDF::Literal::Double
when ::BigDecimal then RDF::Literal::Decimal
when ::Rational then RDF::Literal::Double
when ::DateTime then RDF::Literal::DateTime
when ::Time then RDF::Literal::DateTime
when ::Date then RDF::Literal::Date
Expand Down
12 changes: 6 additions & 6 deletions lib/rdf/model/literal/decimal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ def abs
##
# 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.
#
# @return [RDF::Literal]
# @return [RDF::Literal::Integer]
def round
self.class.new(to_d.round)
RDF::Literal::Integer.new(to_d.round)
end

##
Expand All @@ -74,9 +74,9 @@ def round
# @example
# RDF::Literal(1).ceil #=> RDF::Literal(1)
#
# @return [RDF::Literal]
# @return [RDF::Literal::Integer]
def ceil
self.class.new(to_d.ceil)
RDF::Literal::Integer.new(to_d.ceil)
end

##
Expand All @@ -85,9 +85,9 @@ def ceil
# @example
# RDF::Literal(1).floor #=> RDF::Literal(1)
#
# @return [RDF::Literal]
# @return [RDF::Literal::Integer]
def floor
self.class.new(to_d.floor)
RDF::Literal::Integer.new(to_d.floor)
end

##
Expand Down
18 changes: 9 additions & 9 deletions lib/rdf/model/literal/double.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,33 +142,33 @@ def infinite?
end

##
# Returns the smallest number greater than or equal to `self`.
# Returns the smallest integer greater than or equal to `self`.
#
# @example
# RDF::Literal(1.2).ceil #=> RDF::Literal(2)
# RDF::Literal(-1.2).ceil #=> RDF::Literal(-1)
# RDF::Literal(2.0).ceil #=> RDF::Literal(2)
# RDF::Literal(-2.0).ceil #=> RDF::Literal(-2)
#
# @return [RDF::Literal]
# @return [RDF::Literal::Integer]
# @since 0.2.3
def ceil
self.class.new(to_f.ceil)
RDF::Literal::Integer.new(to_f.ceil)
end

##
# Returns the largest number less than or equal to `self`.
# Returns the largest integer less than or equal to `self`.
#
# @example
# RDF::Literal(1.2).floor #=> RDF::Literal(1)
# RDF::Literal(-1.2).floor #=> RDF::Literal(-2)
# RDF::Literal(2.0).floor #=> RDF::Literal(2)
# RDF::Literal(-2.0).floor #=> RDF::Literal(-2)
#
# @return [RDF::Literal]
# @return [RDF::Literal::Integer]
# @since 0.2.3
def floor
self.class.new(to_f.floor)
RDF::Literal::Integer.new(to_f.floor)
end

##
Expand All @@ -181,11 +181,11 @@ def abs
end

##
# 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.
# 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.
#
# @return [RDF::Literal]
# @return [RDF::Literal::Integer]
def round
self.class.new(to_f.round)
RDF::Literal::Integer.new(to_f.round)
end

##
Expand Down
34 changes: 34 additions & 0 deletions lib/rdf/model/literal/numeric.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,40 @@ def *(other)
end
end

##
# Exponent − Performs exponential (power) calculation on operators.
#
# Promotes values, as necessary, with the result type depending on the input values.
#
# @param [Literal::Numeric, #to_i, #to_f, #to_d] other
# @return [RDF::Literal::Numeric]
# @since 0.2.3
# @see https://www.w3.org/TR/xpath-functions/#func-math-pow
def **(other)
RDF::Literal(object ** (other.is_a?(RDF::Literal::Numeric) ? other.object : other))
rescue ZeroDivisionError
RDF::Literal::Double.new('INF')
end

##
# Exponent − Performs remainder of `self` divided by `other`.
#
# @param [Literal::Numeric, #to_i, #to_f, #to_d] other
# @return [RDF::Literal]
# @since 0.2.3
# @see https://www.w3.org/TR/xpath-functions/#func-numeric-mod
def %(other)
if self.class == Double || [Double, ::Float].include?(other.class)
self.class.new(to_f % other.to_f)
elsif ((self.class == RDF::Literal::Float || other.class == RDF::Literal::Float) rescue false)
self.class.new(to_f % other.to_f)
elsif self.class == Decimal || other.class == Decimal
self.class.new(to_d % (other.respond_to?(:to_d) ? other.to_d : BigDecimal(other.to_s)))
else
self.class.new(to_i % other.to_i)
end
end

##
# Returns the quotient of `self` divided by `other`.
#
Expand Down
9 changes: 8 additions & 1 deletion lib/rdf/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -247,15 +247,22 @@ def optimize(**options)
# Optimizes this query by reordering its constituent triple patterns
# according to their cost estimates.
#
# Optional patterns have greater cost than non-optional patterns so they will always come after non-optional patterns
#
# @param [Hash{Symbol => Object}] options
# any additional options for optimization
# @return [self]
# @see RDF::Query::Pattern#cost
# @since 0.3.0
def optimize!(**options)
@patterns.sort! do |a, b|
optional, required = @patterns.partition(&:optional?)
required.sort! do |a, b|
(a.cost || 0) <=> (b.cost || 0)
end
optional.sort! do |a, b|
(a.cost || 0) <=> (b.cost || 0)
end
@patterns = required + optional
self
end

Expand Down

0 comments on commit a9e4452

Please sign in to comment.