Sha256: 805bf84388a92620ad156318b8732770d89dccfbcb71841ff73aafa27fd2cc4a

Contents?: true

Size: 1.86 KB

Versions: 1

Compression:

Stored size: 1.86 KB

Contents

module SPARQL; module Algebra
  class Operator
    ##
    # The SPARQL Property Path `alt` (Alternative Property Path) operator.
    #
    # Let P and Q be property path expressions.
    #
    #     eval(Path(X, alt(P,Q), Y)) = Union(eval(Path(X, P, Y)), eval(Path(X, Q, Y)))
    #
    # @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/sparql11-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, depth: options[:depth].to_i + 1, **options, &block)
      end
    end # Alt
  end # Operator
end; end # SPARQL::Algebra

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sparql-3.1.0 lib/sparql/algebra/operator/alt.rb