Class: SPARQL::Algebra::Operator::Minus

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

Overview

The SPARQL GraphPattern minus operator.

[66] MinusGraphPattern ::= ‘MINUS’ GroupGraphPattern

Examples:

SPARQL Grammar

SELECT * { ?s ?p ?o MINUS { ?s ?q ?v } }

SSE

(minus
 (bgp
  (triple ?s ?p ?o))
 (bgp (triple ?s ?q ?v)))

SPARQL Grammar (inline filter)

PREFIX :    <http://example/>
SELECT (?s1 AS ?subset) (?s2 AS ?superset)
WHERE {
    ?s2 a :Set .
    ?s1 a :Set .
    FILTER(?s1 != ?s2)
    MINUS {
        ?s1 a :Set .
        ?s2 a :Set .
        FILTER(?s1 != ?s2)
    }
}

SSE (inline filter)

(prefix ((: <http://example/>))
 (project (?subset ?superset)
  (extend ((?subset ?s1) (?superset ?s2))
   (filter (!= ?s1 ?s2)
    (minus
     (bgp (triple ?s2 a :Set) (triple ?s1 a :Set))
     (filter (!= ?s1 ?s2)
      (bgp
       (triple ?s1 a :Set)
       (triple ?s2 a :Set))))))))

See Also:

Constant Summary collapse

NAME =
:minus

Constants inherited from Binary

Binary::ARITY

Constants inherited from SPARQL::Algebra::Operator

ARITY, IsURI, URI

Constants included from Expression

Expression::PATTERN_PARENTS

Instance Attribute Summary

Attributes included from Query

#solutions

Attributes inherited from SPARQL::Algebra::Operator

#operands

Instance Method Summary collapse

Methods included from Query

#each_solution, #empty?, #failed?, #graph_name=, #matched?, #query_yields_boolean?, #query_yields_solutions?, #query_yields_statements?, #unshift, #variables

Methods inherited from Binary

#initialize

Methods inherited from SPARQL::Algebra::Operator

#aggregate?, arity, #base_uri, base_uri, base_uri=, #bind, #boolean, #constant?, #deep_dup, #each_descendant, #eql?, #evaluatable?, evaluate, #executable?, #first_ancestor, for, #initialize, #inspect, #ndvars, #node?, #operand, #optimize, #parent, #parent=, #prefixes, prefixes, prefixes=, #rewrite, #to_binary, to_sparql, #to_sxp, #to_sxp_bin, #validate!, #variable?, #variables, #vars

Methods included from Expression

cast, #constant?, #evaluate, extension, extension?, extensions, for, #invalid?, new, #node?, open, #optimize, parse, register_extension, #to_sxp_bin, #valid?, #validate!, #variable?

Constructor Details

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

Instance Method Details

#execute(queryable, **options) {|solution| ... } ⇒ RDF::Query::Solutions

Executes each operand with queryable and performs the join operation by creating a new solution set containing the merge of all solutions from each set that are compatible with each other.

Parameters:

  • queryable (RDF::Queryable)

    the graph or repository to query

  • options (Hash{Symbol => Object})

    any additional keyword options

Yields:

  • (solution)

    each matching solution

Yield Parameters:

Yield Returns:

  • (void)

    ignored

Returns:

See Also:



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/sparql/algebra/operator/minus.rb', line 67

def execute(queryable, **options, &block)
  # Let Ω1 and Ω2 be multisets of solution mappings. We define:
  # 
  # Minus(Ω1, Ω2) = { μ | μ in Ω1 . ∀ μ' in Ω2, either μ and μ' are not compatible or dom(μ) and dom(μ') are disjoint }
  # 
  # card[Minus(Ω1, Ω2)](μ) = card[Ω1](μ)
  debug(options) {"Minus"}
  left = queryable.query(operand(0), **options.merge(depth: options[:depth].to_i + 1))
  debug(options) {"(minus left) #{left.inspect}"}
  right = queryable.query(operand(1), **options.merge(depth: options[:depth].to_i + 1))
  debug(options) {"(minus right) #{right.inspect}"}
  @solutions = left.minus(right)
  @solutions.each(&block) if block_given?
  @solutions
end

#optimize!(**options) ⇒ self

Optimizes this query.

Groups of one graph pattern (not a filter) become join(Z, A) and can be replaced by A. The empty graph pattern Z is the identity for join: Replace join(Z, A) by A Replace join(A, Z) by A

Returns:

  • (self)

See Also:



93
94
95
96
97
# File 'lib/sparql/algebra/operator/minus.rb', line 93

def optimize!(**options)
  ops = operands.map {|o| o.optimize(**options) }.select {|o| o.respond_to?(:empty?) && !o.empty?}
  @operands = ops
  self
end

#to_sparql(top_level: true, filter_ops: [], extensions: {}, **options) ⇒ String

Returns a partial SPARQL grammar for this operator.

Parameters:

  • extensions (Hash{String => Operator}) (defaults to: {})

    Variable bindings

  • filter_ops (Array<Operator>) (defaults to: [])

    ([]) Filter Operations

  • top_level (Boolean) (defaults to: true)

    (true) Treat this as a top-level, generating SELECT … WHERE {}

Returns:

  • (String)


110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/sparql/algebra/operator/minus.rb', line 110

def to_sparql(top_level: true, filter_ops: [], extensions: {}, **options)
  lhs, *rhs = operands
  str = "{\n" + lhs.to_sparql(top_level: false, extensions: {}, **options)

  # Any accrued filters go here.
  filter_ops.each do |op|
    str << "\nFILTER (#{op.to_sparql(**options)}) ."
  end

  rhs.each do |minus|
    str << "\nMINUS {\n"
    str << minus.to_sparql(top_level: false, extensions: {}, **options)
    str << "\n}"
  end
  str << "}"
  top_level ? Operator.to_sparql(str, extensions: extensions, **options) : str
end