Class: SPARQL::Algebra::Operator::Avg

Inherits:
Unary show all
Includes:
Aggregate
Defined in:
vendor/bundler/ruby/2.5.0/bundler/gems/sparql-b66c70fb77a0/lib/sparql/algebra/operator/avg.rb

Overview

The SPARQL avg set function.

Examples:

(prefix ((: <http://www.example.org/>))
  (project (?avg)
    (extend ((?avg ?.0))
      (group () ((?.0 (avg ?o)))
        (bgp (triple ?s :dec ?o))))))

See Also:

Constant Summary collapse

NAME =
:avg

Constants inherited from Unary

Unary::ARITY

Constants inherited from SPARQL::Algebra::Operator

ARITY, IsURI, URI

Instance Attribute Summary

Attributes inherited from SPARQL::Algebra::Operator

#operands

Instance Method Summary collapse

Methods included from Aggregate

#aggregate, #replace_aggregate!, #replace_vars!

Methods inherited from Unary

#initialize

Methods inherited from SPARQL::Algebra::Operator

#aggregate?, arity, #base_uri, base_uri, base_uri=, #boolean, #constant?, #each_descendant, #eql?, #evaluatable?, evaluate, #executable?, #first_ancestor, for, #initialize, #inspect, #ndvars, #node?, #operand, #optimize, #parent, #parent=, prefixes, #prefixes, prefixes=, #rewrite, #to_binary, #to_sxp, #to_sxp_bin, #validate!, #variable?, #vars

Methods included from Expression

cast, #constant?, #evaluate, extension, extensions, for, #invalid?, new, #node?, open, #optimize, parse, register_extension, #to_sxp_bin, #valid?, #validate!, #variable?

Constructor Details

This class inherits a constructor from SPARQL::Algebra::Operator::Unary

Instance Method Details

#apply(enum) ⇒ RDF::Literal::Numeric

The Avg set function calculates the average value for an expression over a group. It is defined in terms of Sum and Count.

Parameters:

Returns:



25
26
27
28
29
30
31
32
33
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-b66c70fb77a0/lib/sparql/algebra/operator/avg.rb', line 25

def apply(enum)
  if enum.empty?
    RDF::Literal(0)
  elsif enum.flatten.all? {|n| n.is_a?(RDF::Literal::Numeric)}
    enum.flatten.reduce(:+) / RDF::Literal::Decimal.new(enum.length)
  else
    raise TypeError, "Averaging non-numeric types: #{enum.flatten}"
  end
end