lib/sparql/algebra/operator/notexists.rb in sparql-3.1.8 vs lib/sparql/algebra/operator/notexists.rb in sparql-3.2.0

- old
+ new

@@ -3,17 +3,28 @@ ## # The SPARQL logical `exists` operator. # # There is a filter operator EXISTS that takes a graph pattern. EXISTS returns `true`/`false` depending on whether the pattern matches the dataset given the bindings in the current group graph pattern, the dataset and the active graph at this point in the query evaluation. No additional binding of variables occurs. The `NOT EXISTS` form translates into `fn:not(EXISTS{...})`. # - # @example - # (prefix ((ex: <http://www.example.org/>)) - # (filter (exists - # (filter (notexists (bgp (triple ?s ?p ex:o2))) - # (bgp (triple ?s ?p ex:o1)))) - # (bgp (triple ?s ?p ex:o)))) + # [126] NotExistsFunc ::= 'NOT' 'EXISTS' GroupGraphPattern # + # @example SPARQL Grammar + # PREFIX ex: <http://www.w3.org/2009/sparql/docs/tests/data-sparql11/negation#> + # SELECT ?animal { + # ?animal a ex:Animal + # FILTER NOT EXISTS { ?animal a ex:Insect } + # } + # + # @example SSE + # (prefix + # ((ex: <http://www.w3.org/2009/sparql/docs/tests/data-sparql11/negation#>)) + # (project (?animal) + # (filter + # (notexists + # (bgp (triple ?animal a ex:Insect))) + # (bgp (triple ?animal a ex:Animal)))) ) + # # @see https://www.w3.org/TR/sparql11-query/#func-abs # @see https://www.w3.org/TR/xpath-functions/#func-abs class NotExists < Operator::Unary include Evaluatable @@ -31,9 +42,22 @@ # @return [RDF::Literal::Boolean] `true` or `false` def evaluate(bindings, **options) solutions = RDF::Query::Solutions(bindings) queryable = options[:queryable] operand(0).execute(queryable, solutions: solutions, **options).empty? + end + + ## + # + # Returns a partial SPARQL grammar for this operator. + # + # @param [Boolean] top_level (true) + # Treat this as a top-level, generating SELECT ... WHERE {} + # @return [String] + def to_sparql(top_level: true, **options) + "NOT EXISTS {\n" + + operands.last.to_sparql(top_level: false, **options) + + "\n}" end end # NotExists end # Operator end; end # SPARQL::Algebra