Class: RDF::Query::Solutions
- Includes:
- SPARQL::Results
- Defined in:
- vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/query/solutions.rb,
vendor/bundler/ruby/3.3.0/bundler/gems/rdf-spec-4f36efa2dbbc/lib/rdf/spec/inspects.rb,
vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/extensions.rb,
vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/algebra/extensions.rb
Overview
Extensions for RDF::Query::Solutions
.
Constant Summary
Constants included from SPARQL::Results
Instance Method Summary collapse
-
#==(other) ⇒ Object
Equals of solution.
-
#bindings ⇒ Hash{Symbol => Array<RDF::Term>}
Returns hash of bindings from each solution.
-
#count(&block) ⇒ Integer
Returns the number of matching query solutions.
-
#distinct ⇒ self
(also: #distinct!, #reduced, #reduced!)
Ensures that the solutions in this solution sequence are unique.
-
#dup ⇒ RDF::Query::Solutions
Duplicates each solution.
-
#eql?(other) ⇒ Boolean
Equivalence of solution.
-
#filter(expression = {}) {|solution| ... }
(also: #filter!)
Filters this solution sequence by the given
criteria
. -
#filter_without_expression {|solution| ... } ⇒ self
Filters this solution sequence by the given
criteria
. - #inspect ⇒ Object
-
#limit(length) ⇒ self
(also: #limit!)
Limits the number of solutions in this solution sequence to a maximum of
length
. -
#merge(other) ⇒ RDF::Query::Solutions
Merge solutions in
other
into a new solutions instance. -
#minus(other) ⇒ RDF::Query::Solutions
Difference between solution sets, from SPARQL 1.1.
-
#offset(start) ⇒ self
(also: #offset!)
Limits this solution sequence to bindings starting from the
start
offset in the overall solution sequence. -
#order(*variables) {|solution| ... } ⇒ self
(also: #order_by)
Reorders this solution sequence by the given
variables
. -
#project(*variables) ⇒ self
(also: #select)
Restricts this solution sequence to the given
variables
only. -
#variable?(*args) ⇒ Object
(also: #variables?, #have_variables?)
the given
variables
. -
#variable_names ⇒ Array<Symbol>
Returns an array of the distinct variable names used in this solution sequence.
-
#variable_names=(vars) ⇒ Array<Symbol>
Sets variable names used in these solutions.
Methods included from SPARQL::Results
#to_csv, #to_html, #to_json, #to_tsv, #to_xml
Methods inherited from Array
#aggregate?, #bind, #c14nxl, #constant?, #deep_dup, #evaluatable?, #executable?, #execute, #ndvars, #node?, #opt_sort, #optimize, #replace_aggregate!, #replace_vars!, #to_s_with_c14nxl, #to_sparql, #to_sxp, #to_sxp_bin, #valid?, #validate!, #variables, #vars
Instance Method Details
#==(other) ⇒ Object
Equals of solution
315 316 317 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/query/solutions.rb', line 315 def ==(other) super && (!other.respond_to?(:variable_names) || variable_names.eql?(other.variable_names)) end |
#bindings ⇒ Hash{Symbol => Array<RDF::Term>}
Returns hash of bindings from each solution. Each bound variable will have an array of bound values representing those from each solution, where a given solution will have just a single value for each bound variable
119 120 121 122 123 124 125 126 127 128 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/query/solutions.rb', line 119 def bindings bindings = {} each do |solution| solution.each do |key, value| bindings[key] ||= [] bindings[key] << value end end bindings end |
#count ⇒ Integer #count({ |solution| ... }) {|solution| ... } ⇒ Integer
Returns the number of matching query solutions.
59 60 61 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/query/solutions.rb', line 59 def count(&block) super end |
#distinct ⇒ self Also known as: distinct!, reduced, reduced!
Ensures that the solutions in this solution sequence are unique.
264 265 266 267 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/query/solutions.rb', line 264 def distinct self.uniq! self end |
#dup ⇒ RDF::Query::Solutions
Duplicates each solution.
133 134 135 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/query/solutions.rb', line 133 def dup RDF::Query::Solutions.new(self.compact.map(&:dup)) end |
#eql?(other) ⇒ Boolean
Equivalence of solution
309 310 311 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/query/solutions.rb', line 309 def eql?(other) super && (!other.respond_to?(:variable_names) || variable_names.eql?(other.variable_names)) end |
#filter(expression = {}) {|solution| ... } Also known as: filter!
This method returns an undefined value.
Filters this solution sequence by the given criteria
.
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/query/solutions.rb', line 160 def filter(criteria = {}) @variable_names = nil if block_given? self.reject! do |solution| !yield(solution.is_a?(Solution) ? solution : Solution.new(solution)) end else self.reject! do |solution| solution = solution.is_a?(Solution) ? solution : Solution.new(solution) results = criteria.map do |name, value| case value when Array then value.any? {|v| solution[name] == v} when Regexp then solution[name].to_s.match(value) else solution[name] == value end end !results.all? end end self end |
#filter_without_expression {|solution| ... } ⇒ self
Filters this solution sequence by the given criteria
.
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/algebra/extensions.rb', line 627 def filter(criteria = {}) @variable_names = nil if block_given? self.reject! do |solution| !yield(solution.is_a?(Solution) ? solution : Solution.new(solution)) end else self.reject! do |solution| solution = solution.is_a?(Solution) ? solution : Solution.new(solution) results = criteria.map do |name, value| case value when Array then value.any? {|v| solution[name] == v} when Regexp then solution[name].to_s.match(value) else solution[name] == value end end !results.all? end end self end |
#inspect ⇒ Object
37 38 39 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-spec-4f36efa2dbbc/lib/rdf/spec/inspects.rb', line 37 def inspect string = "vars: #{variable_names.join(",")}\n#{to_a.inspect}" end |
#limit(length) ⇒ self Also known as: limit!
Limits the number of solutions in this solution sequence to a maximum
of length
.
296 297 298 299 300 301 302 303 304 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/query/solutions.rb', line 296 def limit(length) length = length.to_i raise ArgumentError, "expected zero or a positive integer, got #{length}" if length < 0 case length when 0 then self.clear else self.slice!(length..-1) if length < self.size end self end |
#merge(other) ⇒ RDF::Query::Solutions
Merge solutions in other
into a new solutions instance. Each solution in other
is merged into those solutions in self
that are compatible.
142 143 144 145 146 147 148 149 150 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/query/solutions.rb', line 142 def merge(other) other ||= RDF::Query::Solutions() return other if self.empty? return self if other.empty? RDF::Query::Solutions(self.map do |s1| other.map { |s2| s2.merge(s1) if s2.compatible?(s1) } end.flatten.compact) end |
#minus(other) ⇒ RDF::Query::Solutions
Difference between solution sets, from SPARQL 1.1.
The minus
operation on solutions returns those solutions which either have no compatible solution in other
, or the solution domains are disjoint.
191 192 193 194 195 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/query/solutions.rb', line 191 def minus(other) self.dup.filter! do |soln| !other.any? {|soln2| soln.compatible?(soln2) && !soln.disjoint?(soln2)} end end |
#offset(start) ⇒ self Also known as: offset!
Limits this solution sequence to bindings starting from the start
offset in the overall solution sequence.
279 280 281 282 283 284 285 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/query/solutions.rb', line 279 def offset(start) case start = start.to_i when 0 then nil else self.slice!(0...start) end self end |
#order(*variables) {|solution| ... } ⇒ self Also known as: order_by
Reorders this solution sequence by the given variables
.
Variables may be symbols or Variable instances.
A variable may also be a Procedure/Lambda, compatible with ::Enumerable#sort
.
This takes two arguments (solutions) and returns -1, 0, or 1 equivalently to <=>.
If called with a block, variables are ignored, and the block is invoked with pairs of solutions. The block is expected to return -1, 0, or 1 equivalently to <=>.
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/query/solutions.rb', line 213 def order(*variables) if variables.empty? && !block_given? raise ArgumentError, "wrong number of arguments (0 for 1)" else self.sort! do |a, b| if block_given? yield((a.is_a?(Solution) ? a : Solution.new(a)), (b.is_a?(Solution) ? b : Solution.new(b))) else # Try each variable until a difference is found. variables.inject(nil) do |memo, v| memo || begin comp = v.is_a?(Proc) ? v.call(a, b) : (v = v.to_sym; a[v] <=> b[v]) comp == 0 ? false : comp end end || 0 end end end self end |
#project(*variables) ⇒ self Also known as: select
Restricts this solution sequence to the given variables
only.
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/query/solutions.rb', line 240 def project(*variables) if variables.empty? raise ArgumentError, "wrong number of arguments (0 for 1)" else variables.map!(&:to_sym) self.each do |solution| solution.bindings.delete_if { |k, v| !variables.include?(k.to_sym) } end end # Make sure variable_names are ordered by projected variables projected_vars, vars = variables.map(&:to_sym), variable_names vars = variable_names # Maintain projected order, and add any non-projected variables @variable_names = (projected_vars & vars) + (vars - projected_vars) self end |
#variable? ⇒ Boolean #variable?(variables) ⇒ Object Also known as: variables?, have_variables?
the given variables
.
@param [Array
102 103 104 105 106 107 108 109 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/query/solutions.rb', line 102 def variable?(*args) case args.length when 0 then false when 1 self.any? { |solution| solution.variables?(args.first) } else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)") end end |
#variable_names ⇒ Array<Symbol>
Returns an array of the distinct variable names used in this solution sequence.
68 69 70 71 72 73 74 75 76 77 78 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/query/solutions.rb', line 68 def variable_names @variable_names ||= begin variables = self.inject({}) do |result, solution| solution.each_name do |name| result[name] ||= true end result end variables.keys end end |
#variable_names=(vars) ⇒ Array<Symbol>
Sets variable names used in these solutions. If not set, the default is determined by the variables used in each solution.
85 86 87 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/query/solutions.rb', line 85 def variable_names=(vars) @variable_names = vars.map(&:to_sym) end |