Class: SPARQL::Algebra::Operator::BNode
- 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 )
Constant Summary collapse
- NAME =
:bnode
Instance Method Summary collapse
-
#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.
-
#evaluate(bindings, **options) ⇒ RDF::Term
Evaluates this operator using the given variable
bindings. -
#initialize(literal = false, **options) ⇒ BNode
constructor
Initializes a new operator instance.
-
#to_sparql(**options) ⇒ String
Returns a partial SPARQL grammar for this operator.
-
#to_sxp_bin ⇒ Array
Returns the SPARQL S-Expression (SSE) representation of this expression.
Methods included from Evaluatable
#memoize, #replace_aggregate!, #replace_vars!
Constructor Details
#initialize(literal = false, **options) ⇒ BNode
Initializes a new operator instance.
47 48 49 |
# File 'lib/sparql/algebra/operator/bnode.rb', line 47 def initialize(literal = false, **) 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.
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, **) @@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.
59 60 61 62 |
# File 'lib/sparql/algebra/operator/bnode.rb', line 59 def evaluate(bindings, **) args = operands.map { |operand| operand.evaluate(bindings, **.merge(depth: [:depth].to_i + 1)) } apply(args.first, bindings) end |
#to_sparql(**options) ⇒ String
Returns a partial SPARQL grammar for this operator.
109 110 111 |
# File 'lib/sparql/algebra/operator/bnode.rb', line 109 def to_sparql(**) "BNODE(#{operands.last.to_sparql(**)})" end |