Sha256: 3997b7447f4b2982b432bba93f7a6130f159f1d86a1d466cb1d9d3e3923e22a6

Contents?: true

Size: 997 Bytes

Versions: 4

Compression:

Stored size: 997 Bytes

Contents

module RDF::N3::Algebra::Math
  ##
  # The object is calulated as the absolute value of the subject.
  #
  # @see https://www.w3.org/TR/xpath-functions/#func-abs
  class AbsoluteValue < RDF::N3::Algebra::ResourceOperator
    NAME = :mathAbsoluteValue
    URI = RDF::N3::Math.absoluteValue

    ##
    # The math:absoluteValue operator takes string or number and calculates its absolute value.
    #
    # @param [RDF::Term] resource
    # @param [:subject, :object] position
    # @return [RDF::Term]
    # @see RDF::N3::ResourceOperator#evaluate
    def resolve(resource, position:)
      case position
      when :subject
        return nil unless resource.literal?
        as_literal(resource.as_number.abs)
      when :object
        return nil unless resource.literal? || resource.variable?
        resource
      end
    end

    ##
    # Input is either the subject or object
    #
    # @return [RDF::Term]
    def input_operand
      RDF::N3::List.new(values: operands)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rdf-n3-3.3.0 lib/rdf/n3/algebra/math/absolute_value.rb
rdf-n3-3.2.1 lib/rdf/n3/algebra/math/absolute_value.rb
rdf-n3-3.2.0 lib/rdf/n3/algebra/math/absolute_value.rb
rdf-n3-3.1.2 lib/rdf/n3/algebra/math/absolute_value.rb