Sha256: 889d4b8c9061c4e2aa6c8ad5e0334b70a0f3cf686cfa4d2ebf7b9c650af43fc0

Contents?: true

Size: 2 KB

Versions: 5

Compression:

Stored size: 2 KB

Contents

module SPARQL; module Algebra
  class Operator
    ##
    # The SPARQL GraphPattern `order` operator.
    #
    # @example
    #   (prefix ((foaf: <http://xmlns.com/foaf/0.1/>))
    #     (project (?name)
    #       (order ((asc ?name))
    #         (bgp (triple ?x foaf:name ?name)))))
    #
    # @see https://www.w3.org/TR/sparql11-query/#modOrderBy
    class Order < Operator::Binary
      include Query
      
      NAME = [:order]

      ##
      # Executes this query on the given `queryable` graph or repository.
      # Orders a solution set returned by executing operand(1) using
      # an array of expressions and/or variables specified in operand(0)
      #
      # @param  [RDF::Queryable] queryable
      #   the graph or repository to query
      # @param  [Hash{Symbol => Object}] options
      #   any additional keyword options
      # @yield  [solution]
      #   each matching solution
      # @yieldparam  [RDF::Query::Solution] solution
      # @yieldreturn [void] ignored
      # @return [RDF::Query::Solutions]
      #   the resulting solution sequence
      # @see    https://www.w3.org/TR/sparql11-query/#sparqlAlgebra
      def execute(queryable, **options, &block)

        debug(options) {"Order"}
        @solutions = queryable.query(operands.last, depth: options[:depth].to_i + 1, **options).order do |a, b|
          operand(0).inject(0) do |memo, op|
            debug(options) {"(order) #{op.inspect}"}
            memo = begin
              a_eval = op.evaluate(a, queryable: queryable, depth: options[:depth].to_i + 1, **options) rescue nil
              b_eval = op.evaluate(b, queryable: queryable, depth: options[:depth].to_i + 1, **options) rescue nil
              comp = Operator::Compare.evaluate(a_eval, b_eval).to_i
              comp = -comp if op.is_a?(Operator::Desc)
              comp
            end if memo == 0
            memo
          end
        end
        @solutions.each(&block) if block_given?
        @solutions
      end
    end # Order
  end # Operator
end; end # SPARQL::Algebra

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
sparql-3.1.6 lib/sparql/algebra/operator/order.rb
sparql-3.1.5 lib/sparql/algebra/operator/order.rb
sparql-3.1.4 lib/sparql/algebra/operator/order.rb
sparql-3.1.3 lib/sparql/algebra/operator/order.rb
sparql-3.1.2 lib/sparql/algebra/operator/order.rb