Sha256: 0808f967d25d3151535f333a5eae93ce200c3e570a300f029b0b6e6900913638

Contents?: true

Size: 1.43 KB

Versions: 7

Compression:

Stored size: 1.43 KB

Contents

module SPARQL; module Algebra
  class Operator
    ##
    # The SPARQL logical `abs` operator.
    #
    # [121] BuiltInCall ::= ... | 'ABS' '(' Expression ')'
    #
    # @example SPARQL Grammar
    #   PREFIX : <http://example.org/>
    #   SELECT * WHERE {
    #     ?s :num ?num
    #     FILTER(ABS(?num) >= 2)
    #   }
    #
    # @example SSE
    #   (prefix ((: <http://example.org/>))
    #    (filter (>= (abs ?num) 2)
    #     (bgp (triple ?s :num ?num))))
    #
    # @see https://www.w3.org/TR/sparql11-query/#func-abs
    # @see https://www.w3.org/TR/xpath-functions/#func-abs
    class Abs < Operator::Unary
      include Evaluatable

      NAME = [:abs]

      ##
      # Returns the absolute value of `arg`. An error is raised if `arg` is not a numeric value.
      # 
      # @param  [RDF::Literal] operand
      #   the operand
      # @return [RDF::Literal] literal of same type
      # @raise  [TypeError] if the operand is not a numeric value
      def apply(operand, **options)
        case operand
          when RDF::Literal::Numeric then operand.abs
          else raise TypeError, "expected an RDF::Literal::Numeric, but got #{operand.inspect}"
        end
      end

      ##
      #
      # Returns a partial SPARQL grammar for this operator.
      #
      # @return [String]
      def to_sparql(**options)
        "ABS(#{operands.first.to_sparql(**options)})"
      end
    end # Abs
  end # Operator
end; end # SPARQL::Algebra

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
sparql-3.3.1 lib/sparql/algebra/operator/abs.rb
sparql-3.3.0 lib/sparql/algebra/operator/abs.rb
sparql-3.2.6 lib/sparql/algebra/operator/abs.rb
sparql-3.2.5 lib/sparql/algebra/operator/abs.rb
sparql-3.2.4 lib/sparql/algebra/operator/abs.rb
sparql-3.2.3 lib/sparql/algebra/operator/abs.rb
sparql-3.2.1 lib/sparql/algebra/operator/abs.rb