class SPARQL::Algebra::Operator::Order

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 www.w3.org/TR/sparql11-query/#modOrderBy

Constants

NAME

Public Instance Methods

execute(queryable, **options, &block) click to toggle source

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 www.w3.org/TR/sparql11-query/#sparqlAlgebra

# File lib/sparql/algebra/operator/order.rb, line 34
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 = begin
          Operator::Compare.evaluate(a_eval, b_eval, order_by: true).to_s.to_i
        rescue TypeError
          # Type sError is effectively zero
          debug(options) {"(order) rescue(#{$!}): #{a_eval.inspect}, #{b_eval.inspect}"}
          RDF::Literal(0)
        end
        comp = -comp if op.is_a?(Operator::Desc)
        comp
      end if memo == 0
      memo
    end
  end
  @solutions.each(&block) if block_given?
  @solutions
end