Class: SPARQL::Algebra::Operator::PathRange
- Inherits:
-
Ternary
- Object
- SPARQL::Algebra::Operator
- Ternary
- SPARQL::Algebra::Operator::PathRange
- Includes:
- Query
- Defined in:
- lib/sparql/algebra/operator/path_range.rb
Overview
The SPARQL Property Path pathRange
(NonCountingPath) operator.
Property path ranges allow specific path lenghts to be queried. The minimum range describes the minimum path length that will yield solutions, and the maximum range the maximum path that will be returned. A minumum of zero is similar to the path?
operator. The maximum range of *
is similar to the path*
or path+
operators.
For example, the two queries are functionally equivalent:
SELECT * WHERE {:a :p{1,2} :b}
SELECT * WHERE {:a (:p/:p?) :b}
[91] PathElt ::= PathPrimary PathMod? [93] PathMod ::= ‘*’ | ‘?’ | ‘+’ | ‘INTEGER? (‘,’ INTEGER?)? ‘’
Constant Summary collapse
- NAME =
:"pathRange"
Constants inherited from Ternary
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, accumulator: RDF::Query::Solutions.new, index: RDF::Literal(0), **options) {|solution| ... } ⇒ Object
Path with lower and upper bounds on lenghts:.
-
#initialize(min, max, path, **options) ⇒ PathRange
constructor
Initializes a new operator instance.
-
#to_sparql(**options) ⇒ String
Returns a partial SPARQL grammar for this operator.
Methods included from Query
#each_solution, #empty?, #failed?, #graph_name=, #matched?, #query_yields_boolean?, #query_yields_solutions?, #query_yields_statements?, #unshift, #variables
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, #to_sxp_bin, #validate!, #variable?, #variables, #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?, #validate!, #variable?
Constructor Details
#initialize(min, max, path, **options) ⇒ PathRange
Initializes a new operator instance.
75 76 77 78 79 |
# File 'lib/sparql/algebra/operator/path_range.rb', line 75 def initialize(min, max, path, **) raise ArgumentError, "expect min <= max {#{min},#{max}}" if max.is_a?(RDF::Literal::Integer) && max < min super end |
Instance Method Details
#execute(queryable, accumulator: RDF::Query::Solutions.new, index: RDF::Literal(0), **options) {|solution| ... } ⇒ Object
Path with lower and upper bounds on lenghts:
(path :a (pathRange 1 2 :p) :b) => (path)
100 101 102 103 104 105 106 107 108 109 110 111 112 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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/sparql/algebra/operator/path_range.rb', line 100 def execute(queryable, accumulator: RDF::Query::Solutions.new, index: RDF::Literal(0), **, &block) min, max, op = *operands subject, object = [:subject], [:object] debug() {"Path{#{min},#{max}} #{[subject, op, object].to_sse}"} path_mm = if min.zero? && max.is_a?(RDF::Literal::Integer) && max.zero? PathZero.new(op, **) else # Build up a sequence path_opt = nil seq_len = min.to_i if max.is_a?(RDF::Literal::Integer) # min to max is a sequence of optional sequences opt_len = (max - min).to_i while opt_len > 0 do path_opt = PathOpt.new(path_opt ? Seq.new(op, path_opt, **) : op, **) opt_len -= 1 end elsif seq_len > 0 path_opt = PathPlus.new(op, **) seq_len -= 1 else path_opt = PathStar.new(op, **) end # sequence ending in op, op+, op*, or path_opt path_seq = nil while seq_len > 0 do path_seq = if path_opt opt, path_opt = path_opt, nil Seq.new(op, opt, **) elsif path_seq Seq.new(op, path_seq, **) else op end seq_len -= 1 end path_seq || path_opt || op end debug() {"=> #{path_mm.to_sse}"} # After this, path_mm may just be the original op, which can be a term, not an operator. if path_mm.is_a?(RDF::Term) path_mm = RDF::Query.new do |q| q.pattern [subject, path_mm, object] end end solutions = path_mm.execute(queryable, **.merge(depth: [:depth].to_i + 1)).uniq debug() {"(path{#{min},#{max}})=> #{solutions.to_sxp}"} solutions.each(&block) if block_given? solutions end |
#to_sparql(**options) ⇒ String
Returns a partial SPARQL grammar for this operator.
165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/sparql/algebra/operator/path_range.rb', line 165 def to_sparql(**) min, max, path = operands "(#{path.to_sparql(**)})" + if max == :* "{#{min},}" elsif min == max "{#{min}}" else "{#{min},#{max}}" end end |