class SPARQL::Algebra::Operator::BNode

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.

@example

(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 www.w3.org/TR/sparql11-query/#func-bnode

Constants

NAME

Public Class Methods

new(literal = false, **options) click to toggle source

Initializes a new operator instance.

@param [RDF::Literal] literal (false) @param [Hash{Symbol => Object}] options

any additional options (see {Operator#initialize})

@raise [TypeError] if any operand is invalid

Calls superclass method SPARQL::Algebra::Operator::Unary::new
# File lib/sparql/algebra/operator/bnode.rb, line 33
def initialize(literal = false, **options)
  super
end

Public Instance Methods

apply(literal, bindings, **options) click to toggle source

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.

@param [RDF::Literal] literal (nil) @param [RDF::Query::Solution, []] bindings

a query solution containing zero or more variable bindings

@return [RDF::Node] @raise [TypeError] if the operand is not a simple literal or nil

# File lib/sparql/algebra/operator/bnode.rb, line 60
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) click to toggle source

Evaluates this operator using the given variable `bindings`.

@param [RDF::Query::Solution] bindings

a query solution containing zero or more variable bindings

@param [Hash{Symbol => Object}] options ({})

options passed from query

@return [RDF::Term]

# File lib/sparql/algebra/operator/bnode.rb, line 45
def evaluate(bindings, **options)
  args = operands.map { |operand| operand.evaluate(bindings, depth: options[:depth].to_i + 1, **options) }
  apply(args.first, bindings)
end
to_sxp_bin() click to toggle source

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

Remove the optional argument.

@return [Array] `self` @see openjena.org/wiki/SSE

# File lib/sparql/algebra/operator/bnode.rb, line 86
def to_sxp_bin
  [NAME] + operands.reject {|o| o == false}
end