Class: SPARQL::Algebra::Operator::Order

Inherits:
Binary show all
Includes:
Query
Defined in:
lib/sparql/algebra/operator/order.rb

Overview

The SPARQL GraphPattern order operator.

Examples:

(prefix ((foaf: <http://xmlns.com/foaf/0.1/>))
  (project (?name)
    (order ((asc ?name))
      (bgp (triple ?x foaf:name ?name)))))

See Also:

Constant Summary

NAME =
[:order]

Constants inherited from Binary

Binary::ARITY

Constants inherited from SPARQL::Algebra::Operator

ARITY

Instance Attribute Summary

Attributes included from Query

#solutions

Attributes inherited from SPARQL::Algebra::Operator

#operands, #options

Instance Method Summary (collapse)

Methods included from Query

#context=, #each_solution, #failed?, #matched?, #unshift, #variables

Methods inherited from Binary

#initialize

Methods inherited from SPARQL::Algebra::Operator

arity, base_uri, #base_uri, base_uri=, #boolean, #constant?, #eql?, #evaluatable?, evaluate, #executable?, for, #initialize, #inspect, #operand, prefixes, #prefixes, prefixes=, #to_sse, #to_sxp, #variable?

Methods included from Evaluatable

#apply, #evaluate, #memoize

Methods included from Expression

cast, #constant?, #evaluate, for, new, open, parse, #to_sse, #variable?

Constructor Details

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

Instance Method Details

- (RDF::Query::Solutions) execute(queryable, options = {})

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)



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/sparql/algebra/operator/order.rb', line 30

def execute(queryable, options = {})
  debug("Order", options)
  @solutions = operands.last.execute(queryable, options.merge(:depth => options[:depth].to_i + 1)).order do |a, b|
    operand(0).inject(false) do |memo, op|
      debug("=> #{op.inspect}", options)
      memo ||= begin
        comp = case op
        when RDF::Query::Variable
          a[op.to_sym] <=> b[op.to_sym]
        when Operator, Array
          a_eval, b_eval = op.evaluate(a), op.evaluate(b)
          if a_eval.nil?
            RDF::Literal(-1)
          elsif b_eval.nil?
            RDF::Literal(1)
          else
            Operator::Compare.evaluate(a_eval, b_eval)
          end
        else
          raise TypeError, "Unexpected order expression #{op.inspect}"
        end
        comp = -comp if op.is_a?(Operator::Desc)
        comp == 0 ? false : comp
      end
    end || 0  # They compare equivalently if there are no matches
  end
end

- (Union, RDF::Query) optimize

Returns an optimized version of this query.

Return optimized query



64
65
66
# File 'lib/sparql/algebra/operator/order.rb', line 64

def optimize
  operands = operands.map(&:optimize)
end