Sha256: 7fce923df53174e645a448b457cebc2fb918681cce5ad18d34a69f38c023fff3

Contents?: true

Size: 1.3 KB

Versions: 25

Compression:

Stored size: 1.3 KB

Contents

module SPARQL; module Algebra
  class Operator
    ##
    # The SPARQL `sum` set function.
    #
    # @example
    #    (prefix ((: <http://www.example.org/>))
    #      (project (?sum)
    #        (extend ((?sum ?.0))
    #          (group () ((?.0 (sum ?o)))
    #            (bgp (triple ?s :dec ?o))))))
    #
    # @see http://www.w3.org/TR/sparql11-query/#defn_aggSum
    class Sum < Operator::Unary
      include Aggregate

      NAME = :sum

      ##
      # Sum is a SPARQL set function that will return the numeric value obtained by summing the values within the aggregate group. Type promotion happens as per the op:numeric-add function, applied transitively, (see definition below) so the value of SUM(?x), in an aggregate group where ?x has values 1 (integer), 2.0e0 (float), and 3.0 (decimal) will be 6.0 (float).
      #
      # @param  [Enumerable<Array<RDF::Term>>] enum
      #   enum of evaluated operand
      # @return [RDF::Literal::Numeric] The sum of the terms
      def apply(enum)
        if enum.empty?
          RDF::Literal(0)
        elsif enum.flatten.all? {|n| n.is_a?(RDF::Literal::Numeric)}
          enum.flatten.reduce(:+)
        else
          raise TypeError, "Averaging non-numeric types: #{enum.flatten}"
        end
      end
    end # Sum
  end # Operator
end; end # SPARQL::Algebra

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
sparql-3.0.2 lib/sparql/algebra/operator/sum.rb
sparql-3.0.1 lib/sparql/algebra/operator/sum.rb
sparql-3.0.0 lib/sparql/algebra/operator/sum.rb
sparql-2.2.2 lib/sparql/algebra/operator/sum.rb
sparql-2.2.1 lib/sparql/algebra/operator/sum.rb
sparql-2.2.0 lib/sparql/algebra/operator/sum.rb
sparql-2.1.0 lib/sparql/algebra/operator/sum.rb
sparql-2.0.0 lib/sparql/algebra/operator/sum.rb
sparql-2.0.0.beta2 lib/sparql/algebra/operator/sum.rb
sparql-1.99.1 lib/sparql/algebra/operator/sum.rb
sparql-2.0.0.beta1 lib/sparql/algebra/operator/sum.rb
sparql-1.1.9.1 lib/sparql/algebra/operator/sum.rb
sparql-1.99.0 lib/sparql/algebra/operator/sum.rb
sparql-1.1.9 lib/sparql/algebra/operator/sum.rb
sparql-1.1.8 lib/sparql/algebra/operator/sum.rb
sparql-1.1.7 lib/sparql/algebra/operator/sum.rb
sparql-1.1.6 lib/sparql/algebra/operator/sum.rb
sparql-1.1.5 lib/sparql/algebra/operator/sum.rb
sparql-1.1.4 lib/sparql/algebra/operator/sum.rb
sparql-1.1.3 lib/sparql/algebra/operator/sum.rb