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

Inherits:
Unary show all
Includes:
Evaluatable
Defined in:
vendor/bundler/ruby/2.5.0/bundler/gems/sparql-b66c70fb77a0/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.

Examples:

(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

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=, #boolean, #constant?, #each_descendant, #eql?, #evaluatable?, evaluate, #executable?, #first_ancestor, for, #inspect, #ndvars, #node?, #operand, #optimize, #parent, #parent=, #prefixes, prefixes, prefixes=, #rewrite, #to_binary, #to_sxp, #validate!, #variable?, #vars

Methods included from Expression

cast, #constant?, extension, extensions, for, #invalid?, new, #node?, open, #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



33
34
35
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-b66c70fb77a0/lib/sparql/algebra/operator/bnode.rb', line 33

def initialize(literal = false, options = {})
  super
end

Instance Method Details

#apply(literal, bindings) ⇒ 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:

Returns:

Raises:

  • (TypeError)

    if the operand is not a simple literal or nil



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-b66c70fb77a0/lib/sparql/algebra/operator/bnode.rb', line 60

def apply(literal, bindings)
  @@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}) (defaults to: {})

    ({}) options passed from query

Returns:



45
46
47
48
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-b66c70fb77a0/lib/sparql/algebra/operator/bnode.rb', line 45

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_sxp_binArray

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

Remove the optional argument.

Returns:

See Also:



86
87
88
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-b66c70fb77a0/lib/sparql/algebra/operator/bnode.rb', line 86

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