Class: SPARQL::Algebra::Operator::BNode

Inherits:
Unary show all
Includes:
Evaluatable
Defined in:
lib/sparql/algebra/operator/bnode.rb

Overview

The SPARQL bnode operator.

The BNODE function constructs a blank node that is distinct from all blank nodes in the dataset being queried and distinct from all blank nodes created by calls to this constructor for other query solutions. If the no argument form is used, every call results in a distinct blank node. If the form with a simple literal is used, every call results in distinct blank nodes for different simple literals, and the same blank node for calls with the same simple literal within expressions for one solution mapping.

[121] BuiltInCall ::= … | ‘BNODE’ ( ‘(’ Expression ‘)’ | NIL )

Examples:

SPARQL Grammar

PREFIX : <http://example.org/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?s1 ?s2 (BNODE(?s1) AS ?b1) (BNODE(?s2) AS ?b2)
WHERE {
  ?a :str ?s1 .
  ?b :str ?s2 .
  FILTER (?a = :s1 || ?a = :s3)
  FILTER (?b = :s1 || ?b = :s3)
}

SSE

(prefix
 ((: <http://example.org/>) (xsd: <http://www.w3.org/2001/XMLSchema#>))
 (project (?s1 ?s2 ?b1 ?b2)
  (extend
   ((?b1 (bnode ?s1)) (?b2 (bnode ?s2)))
   (filter
    (exprlist
     (|| (= ?a :s1) (= ?a :s3))
     (|| (= ?b :s1) (= ?b :s3)))
    (bgp (triple ?a :str ?s1) (triple ?b :str ?s2))) )) )

See Also:

Constant Summary collapse

NAME =
:bnode

Constants inherited from Unary

Unary::ARITY

Constants inherited from SPARQL::Algebra::Operator

ARITY, IsURI, URI

Constants included from Expression

Expression::PATTERN_PARENTS

Instance Attribute Summary

Attributes inherited from SPARQL::Algebra::Operator

#operands

Instance Method Summary collapse

Methods included from Evaluatable

#memoize, #replace_aggregate!, #replace_vars!

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, #inspect, #ndvars, #node?, #operand, #optimize, #optimize!, #parent, #parent=, #prefixes, prefixes, prefixes=, #rewrite, #to_binary, to_sparql, #to_sxp, #validate!, #variable?, #variables, #vars

Methods included from Expression

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

Constructor Details

#initialize(literal = false, **options) ⇒ BNode

Initializes a new operator instance.

Parameters:

Raises:

  • (TypeError)

    if any operand is invalid



47
48
49
# File 'lib/sparql/algebra/operator/bnode.rb', line 47

def initialize(literal = false, **options)
  super
end

Instance Method Details

#apply(literal, bindings, **options) ⇒ RDF::Node

The BNODE function constructs a blank node that is distinct from all blank nodes in the dataset being queried and distinct from all blank nodes created by calls to this constructor for other query solutions. If the no argument form is used, every call results in a distinct blank node. If the form with a simple literal is used, every call results in distinct blank nodes for different simple literals, and the same blank node for calls with the same simple literal within expressions for one solution mapping.

This functionality is compatible with the treatment of blank nodes in SPARQL CONSTRUCT templates.

Parameters:

  • literal (RDF::Literal)

    (nil)

  • bindings (RDF::Query::Solution, #[])

    a query solution containing zero or more variable bindings

Returns:

  • (RDF::Node)

Raises:

  • (TypeError)

    if the operand is not a simple literal or nil



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/sparql/algebra/operator/bnode.rb', line 74

def apply(literal, bindings, **options)
  @@bnode_base ||= "b0"
  @@bindings ||= bindings
  @@bnodes ||= {}

  if literal == RDF::Literal::FALSE
    l, @@bnode_base = @@bnode_base, @@bnode_base.succ
    RDF::Node.new(l)
  else
    raise TypeError, "expected an simple literal, but got #{literal.inspect}" unless literal.literal? && literal.simple?
    # Return the same BNode if used with the same binding
    @@bnodes, @@bindings = {}, bindings unless @@bindings == bindings
    @@bnodes[literal.to_s.to_sym] ||= begin
      l, @@bnode_base = @@bnode_base, @@bnode_base.succ
      RDF::Node.new(l)
    end
  end
end

#evaluate(bindings, **options) ⇒ RDF::Term

Evaluates this operator using the given variable bindings.

Parameters:

  • bindings (RDF::Query::Solution)

    a query solution containing zero or more variable bindings

  • options (Hash{Symbol => Object})

    ({}) options passed from query

Returns:



59
60
61
62
# File 'lib/sparql/algebra/operator/bnode.rb', line 59

def evaluate(bindings, **options)
  args = operands.map { |operand| operand.evaluate(bindings, **options.merge(depth: options[:depth].to_i + 1)) }
  apply(args.first, bindings)
end

#to_sparql(**options) ⇒ String

Returns a partial SPARQL grammar for this operator.

Returns:

  • (String)


109
110
111
# File 'lib/sparql/algebra/operator/bnode.rb', line 109

def to_sparql(**options)
  "BNODE(#{operands.last.to_sparql(**options)})"
end

#to_sxp_binArray

Returns the SPARQL S-Expression (SSE) representation of this expression.

Remove the optional argument.

Returns:

See Also:



100
101
102
# File 'lib/sparql/algebra/operator/bnode.rb', line 100

def to_sxp_bin
  [NAME] + operands.reject {|o| o == false}
end