Class: SPARQL::Algebra::Operator::Extend
- Inherits:
-
Binary
- Object
- SPARQL::Algebra::Operator
- Binary
- SPARQL::Algebra::Operator::Extend
- Includes:
- Query
- Defined in:
- lib/sparql/algebra/operator/extend.rb
Overview
The SPARQL Extend
operator.
Extends a solution
[60] Bind ::= ‘BIND’ ‘(’ Expression ‘AS’ Var ‘)’
Constant Summary collapse
- NAME =
[:extend]
Constants inherited from Binary
Constants inherited from SPARQL::Algebra::Operator
Constants included from Expression
Instance Attribute Summary
Attributes included from Query
Attributes inherited from SPARQL::Algebra::Operator
Instance Method Summary collapse
-
#execute(queryable, **options) {|solution| ... } ⇒ RDF::Query::Solutions
Let μ be a solution mapping, Ω a multiset of solution mappings, var a variable and expr be an expression, then we define:.
-
#to_sparql(**options) ⇒ String
Returns a partial SPARQL grammar for this operator.
-
#validate! ⇒ Object
The variable introduced by the BIND clause must not have been used in the group graph pattern up to the point of use in BIND.
-
#variables ⇒ Hash{Symbol => RDF::Query::Variable}
The variables used in the extension.
Methods included from Query
#each_solution, #empty?, #failed?, #graph_name=, #matched?, #query_yields_boolean?, #query_yields_solutions?, #query_yields_statements?, #unshift
Methods inherited from Binary
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, #initialize, #inspect, #ndvars, #node?, #operand, #optimize, #optimize!, #parent, #parent=, #prefixes, prefixes, prefixes=, #rewrite, #to_binary, to_sparql, #to_sxp, #to_sxp_bin, #variable?, #vars
Methods included from Expression
cast, #constant?, #evaluate, extension, extension?, extensions, for, #invalid?, new, #node?, open, #optimize, #optimize!, parse, register_extension, #to_sxp_bin, #valid?, #variable?
Constructor Details
This class inherits a constructor from SPARQL::Algebra::Operator::Binary
Instance Method Details
#execute(queryable, **options) {|solution| ... } ⇒ RDF::Query::Solutions
Let μ be a solution mapping, Ω a multiset of solution mappings, var a variable and expr be an expression, then we define:
Extend(μ, var, expr) = μ ∪ { (var,value) | var not in dom(μ) and value = expr(μ) }
Extend(μ, var, expr) = μ if var not in dom(μ) and expr(μ) is an error
Extend is undefined when var in dom(μ).
Extend(Ω, var, expr) = { Extend(μ, var, expr) | μ in Ω }
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/sparql/algebra/operator/extend.rb', line 83 def execute(queryable, **, &block) debug() {"Extend"} @solutions = operand(1).execute(queryable, **.merge(depth: [:depth].to_i + 1)) @solutions.each do |solution| # Re-bind to bindings, if defined, as they might not be found in solution [:bindings].each_binding do |name, value| solution[name] = value if operands.first.variables.include?(name) end if [:bindings] && operands.first.respond_to?(:variables) debug() {"===> soln #{solution.to_h.inspect}"} operand(0).each do |(var, expr)| begin val = expr.evaluate(solution, queryable: queryable, **.merge(depth: [:depth].to_i + 1)) debug() {"===> + #{var} => #{val.inspect}"} val = val.dup.bind(solution) if val.is_a?(RDF::Query::Pattern) solution.bindings[var.to_sym] = val rescue TypeError => e # Evaluates to error, ignore debug() {"===> #{var} error: #{e.}"} end end end @solutions.each(&block) if block_given? @solutions end |
#to_sparql(**options) ⇒ String
Returns a partial SPARQL grammar for this operator.
Extracts bindings.
158 159 160 161 162 163 164 165 166 167 |
# File 'lib/sparql/algebra/operator/extend.rb', line 158 def to_sparql(**) extensions = operands.first.inject({}) do |memo, (as, expression)| # Use string/name of variable "as" to aid in later matching memo.merge(as.to_s => expression) end # Merge any inherited extensions from options extensions = .delete(:extensions).merge(extensions) if .key?(:extensions) operands.last.to_sparql(extensions: extensions, **) end |
#validate! ⇒ Object
The variable introduced by the BIND clause must not have been used in the group graph pattern up to the point of use in BIND
Also, variables used in a binding expression must be projected by the query.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/sparql/algebra/operator/extend.rb', line 113 def validate! bind_vars = operand(0).map(&:first).map(&:name) query_vars = operand(1).variables.keys unless (bind_vars.compact & query_vars.compact).empty? raise ArgumentError, "bound variable used in query: #{(bind_vars.compact & query_vars.compact).to_sse}" end # Special case for group variables if operands.last.is_a?(Group) bind_expr_vars = operand(0).map(&:last).variables.keys group_vars = operands.last.variables.keys group_internal_vars = operands.last.internal_variables.keys bind_expr_vars.each do |v| raise ArgumentError, "extension expression uses variable not in scope: #{v}" if group_internal_vars.include?(v) && !group_vars.include?(v) end end super end |
#variables ⇒ Hash{Symbol => RDF::Query::Variable}
The variables used in the extension. Includes extended variables.
144 145 146 147 148 149 |
# File 'lib/sparql/algebra/operator/extend.rb', line 144 def variables operands.first. map(&:first). map(&:variables). inject(operands.last.variables) {|memo, h| memo.merge(h)} end |