Sha256: e424e3d0ff290b66370f68bc0053e675ad53a74abf5a91cceeab709f35e28805

Contents?: true

Size: 1.72 KB

Versions: 6

Compression:

Stored size: 1.72 KB

Contents

module SPARQL; module Algebra
  class Operator
    ##
    # The SPARQL Property Path `alt` (Alternative Property Path) operator.
    #
    # @example
    #   (alt a b)
    #
    # @see http://www.w3.org/TR/sparql11-query/#defn_evalPP_alternative
    class Alt < Operator::Binary
      include Query
      
      NAME = :alt

      ##
      # Equivalent to:
      #
      #    (path x (alt :p :q) y)
      #     => (union (bgp (x :p y)) (bgp (x :q y)))
      #
      # @param  [RDF::Queryable] queryable
      #   the graph or repository to query
      # @param  [Hash{Symbol => Object}] options
      #   any additional keyword options
      # @option options [RDF::Term, RDF::Variable] :subject
      # @option options [RDF::Term, RDF::Variable] :object
      # @yield  [solution]
      #   each matching solution
      # @yieldparam  [RDF::Query::Solution] solution
      # @yieldreturn [void] ignored
      # @see    http://www.w3.org/TR/rdf-sparql-query/#sparqlAlgebra
      def execute(queryable, options = {}, &block)
        subject, object = options[:subject], options[:object]
        debug(options) {"Alt #{[subject, operands, object].to_sse}"}

        # Solutions where predicate exists
        qa = if operand(0).is_a?(RDF::Term)
          RDF::Query.new do |q|
            q.pattern [subject, operand(0), object]
          end
        else
          operand(0)
        end

        qb = if operand(1).is_a?(RDF::Term)
          RDF::Query.new do |q|
            q.pattern [subject, operand(1), object]
          end
        else
          operand(1)
        end

        query = Union.new(qa, qb)
        queryable.query(query, options.merge(depth: options[:depth].to_i + 1), &block)
      end
    end # Alt
  end # Operator
end; end # SPARQL::Algebra

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
sparql-1.99.1 lib/sparql/algebra/operator/alt.rb
sparql-1.1.9.1 lib/sparql/algebra/operator/alt.rb
sparql-1.99.0 lib/sparql/algebra/operator/alt.rb
sparql-1.1.9 lib/sparql/algebra/operator/alt.rb
sparql-1.1.8 lib/sparql/algebra/operator/alt.rb
sparql-1.1.7 lib/sparql/algebra/operator/alt.rb