Class: RDF::Query::Solutions
- Inherits:
-
Array
- Object
- Array
- RDF::Query::Solutions
- Defined in:
- lib/rdf/query/solutions.rb
Overview
An RDF basic graph pattern (BGP) query solution sequence.
Instance Method Summary (collapse)
-
- (Hash{Symbol => Array<RDF::Term>}) bindings
Returns hash of bindings from each solution.
-
- (Integer) count(&block)
Returns the number of matching query solutions.
-
- distinct
(also: #distinct!, #reduced, #reduced!)
Ensures that the solutions in this solution sequence are unique.
-
- filter(criteria = {}) {|solution| ... }
(also: #filter!)
Filters this solution sequence by the given
criteria. -
- (Boolean) have_variables?(variables)
(also: #has_variables?)
Returns
trueif this solution sequence contains bindings for any of the givenvariables. -
- limit(length)
(also: #limit!)
Limits the number of solutions in this solution sequence to a maximum of
length. -
- offset(start)
(also: #offset!)
Limits this solution sequence to bindings starting from the
startoffset in the overall solution sequence. -
- order(*variables) {|solution| ... }
(also: #order_by)
Reorders this solution sequence by the given
variables. -
- project(*variables)
(also: #select)
Restricts this solution sequence to the given
variablesonly. -
- (Array<Symbol>) variable_names
Returns an array of the distinct variable names used in this solution sequence.
Instance Method Details
- (Hash{Symbol => Array<RDF::Term>}) bindings
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
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/rdf/query/solutions.rb', line 68 def bindings bindings = {} each do |solution| solution.each do |key, value| bindings[key] ||= [] bindings[key] << value end end bindings end |
- (Integer) count - (Integer) count({ |solution| ... }) {|solution| ... }
Returns the number of matching query solutions.
59 60 61 |
# File 'lib/rdf/query/solutions.rb', line 59 def count(&block) super end |
- distinct Also known as: distinct!, reduced, reduced!
This method returns an undefined value.
Ensures that the solutions in this solution sequence are unique.
165 166 167 168 |
# File 'lib/rdf/query/solutions.rb', line 165 def distinct self.uniq! self end |
- filter(criteria = {}) {|solution| ... } Also known as: filter!
This method returns an undefined value.
Filters this solution sequence by the given criteria.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/rdf/query/solutions.rb', line 87 def filter(criteria = {}, &block) if block_given? self.reject! do |solution| !block.call(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| solution[name] == value end !results.all? end end self end |
- (Boolean) have_variables?(variables) Also known as: has_variables?
Returns true if this solution sequence contains bindings for any of
the given variables.
232 233 234 |
# File 'lib/rdf/query/solutions.rb', line 232 def have_variables?(variables) self.any? { |solution| solution.has_variables?(variables) } end |
- limit(length) Also known as: limit!
This method returns an undefined value.
Limits the number of solutions in this solution sequence to a maximum
of length.
197 198 199 200 201 202 203 204 205 |
# File 'lib/rdf/query/solutions.rb', line 197 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 |
- offset(start) Also known as: offset!
This method returns an undefined value.
Limits this solution sequence to bindings starting from the start
offset in the overall solution sequence.
180 181 182 183 184 185 186 |
# File 'lib/rdf/query/solutions.rb', line 180 def offset(start) case start = start.to_i when 0 then nil else self.slice!(0...start) end self end |
- order(*variables) {|solution| ... } Also known as: order_by
This method returns an undefined value.
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 <=>.
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/rdf/query/solutions.rb', line 121 def order(*variables, &block) if variables.empty? && !block_given? raise ArgumentError, "wrong number of arguments (0 for 1)" else self.sort! do |a, b| if block_given? block.call((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) Also known as: select
This method returns an undefined value.
Restricts this solution sequence to the given variables only.
148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/rdf/query/solutions.rb', line 148 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 self end |
- (Array<Symbol>) variable_names
Returns an array of the distinct variable names used in this solution sequence.
213 214 215 216 217 218 219 220 221 |
# File 'lib/rdf/query/solutions.rb', line 213 def variable_names variables = self.inject({}) do |result, solution| solution.each_name do |name| result[name] ||= true end result end variables.keys end |