Class: RDF::Query::Solution
- Inherits:
-
Object
- Object
- RDF::Query::Solution
- Includes:
- Enumerable
- Defined in:
- lib/rdf/query/solution.rb
Overview
An RDF query solution.
Instance Attribute Summary collapse
- #bindings ⇒ Object readonly
Instance Method Summary collapse
-
#[](name) ⇒ RDF::Term
Returns the value of the variable
name. -
#[]=(name, value) ⇒ RDF::Term
Binds or rebinds the variable
nameto the givenvalue. -
#bound?(name) ⇒ Boolean
Returns
trueif the variablenameis bound in this solution. -
#compatible?(other) ⇒ Boolean
Compatible Mappings.
-
#disjoint?(other) ⇒ Boolean
Disjoint mapping.
-
#dup ⇒ RDF::Statement
Duplicate solution, preserving patterns.
-
#each_binding {|name, value| ... } ⇒ Enumerator
(also: #each)
Enumerates over every variable binding in this solution.
-
#each_name {|name| ... } ⇒ Enumerator
(also: #each_key)
Enumerates over every variable name in this solution.
-
#each_value {|value| ... } ⇒ Enumerator
Enumerates over every variable value in this solution.
-
#each_variable {|variable| ... } ⇒ Enumerator
Enumerates over every variable in this solution.
-
#enum_binding ⇒ Enumerator<RDF::Resource>
Returns an enumerator for #each_binding.
-
#enum_name ⇒ Enumerator<RDF::Resource>
Returns an enumerator for #each_name.
-
#enum_value ⇒ Enumerator<RDF::Resource>
Returns an enumerator for #each_value.
-
#enum_variable ⇒ Enumerator<RDF::Resource>
Returns an enumerator for #each_variable.
-
#eql?(other) ⇒ Boolean
(also: #==)
Equivalence of solution.
-
#has_variables?(variables) ⇒ Boolean
Returns
trueif this solution contains bindings for any of the givenvariables. -
#hash ⇒ Integer
Integer hash of this solution.
-
#initialize(bindings = {}) {|solution| ... } ⇒ Solution
constructor
Initializes the query solution.
- #inspect ⇒ String
-
#isomorphic_with?(other) ⇒ Boolean
Isomorphic Mappings Two solution mappings u1 and u2 are isomorphic if, for every variable v in dom(u1) and in dom(u2), u1(v) = u2(v).
-
#merge(other) ⇒ RDF::Query::Solution
Merges the bindings from the given
otherquery solution with a copy of this one. -
#merge!(other) ⇒ void
Merges the bindings from the given
otherquery solution into this one, overwriting any existing ones having the same name. - #to_a ⇒ Array<Array(Symbol, RDF::Term)>
- #to_h ⇒ Hash{Symbol => RDF::Term}
-
#unbound?(name) ⇒ Boolean
Returns
trueif the variablenameis unbound in this solution.
Methods included from Enumerable
#dump, #each_graph, #each_object, #each_predicate, #each_quad, #each_statement, #each_subject, #each_term, #each_triple, #enum_graph, #enum_object, #enum_predicate, #enum_quad, #enum_statement, #enum_subject, #enum_term, #enum_triple, #graph_names, #has_graph?, #has_object?, #has_predicate?, #has_quad?, #has_statement?, #has_subject?, #has_term?, #has_triple?, #invalid?, #objects, #predicates, #project_graph, #quads, #statements, #subjects, #supports?, #terms, #to_set, #triples, #valid?, #validate!
Methods included from Util::Aliasing::LateBound
Methods included from Countable
Constructor Details
#initialize(bindings = {}) {|solution| ... } ⇒ Solution
Initializes the query solution.
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/rdf/query/solution.rb', line 39 def initialize(bindings = {}, &block) @bindings = bindings.to_h if block_given? case block.arity when 1 then block.call(self) else instance_eval(&block) end end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#binding(name) ⇒ RDF::Term (protected)
334 335 336 337 338 339 340 |
# File 'lib/rdf/query/solution.rb', line 334 def method_missing(name, *args, &block) if args.empty? && @bindings.has_key?(name.to_sym) @bindings[name.to_sym] else super # raises NoMethodError end end |
Instance Attribute Details
#bindings ⇒ Object (readonly)
51 52 53 |
# File 'lib/rdf/query/solution.rb', line 51 def bindings @bindings end |
Instance Method Details
#[](name) ⇒ RDF::Term
Returns the value of the variable name.
178 179 180 |
# File 'lib/rdf/query/solution.rb', line 178 def [](name) @bindings[name.to_sym] end |
#[]=(name, value) ⇒ RDF::Term
Binds or rebinds the variable name to the given value.
190 191 192 |
# File 'lib/rdf/query/solution.rb', line 190 def []=(name, value) @bindings[name.to_sym] = value.is_a?(RDF::Term) ? value : RDF::Literal(value) end |
#bound?(name) ⇒ Boolean
Returns true if the variable name is bound in this solution.
158 159 160 |
# File 'lib/rdf/query/solution.rb', line 158 def bound?(name) !unbound?(name) end |
#compatible?(other) ⇒ Boolean
Compatible Mappings
Two solution mappings u1 and u2 are compatible if, for every variable v in dom(u1) and in dom(u2), u1(v) = u2(v).
254 255 256 257 258 |
# File 'lib/rdf/query/solution.rb', line 254 def compatible?(other) @bindings.all? do |k, v| !other.to_h.has_key?(k) || other[k].eql?(v) end end |
#disjoint?(other) ⇒ Boolean
Disjoint mapping
A solution is disjoint with another solution if it shares no common variables in their domains.
268 269 270 271 272 |
# File 'lib/rdf/query/solution.rb', line 268 def disjoint?(other) @bindings.none? do |k, v| v && other.to_h.has_key?(k) && other[k].eql?(v) end end |
#dup ⇒ RDF::Statement
Duplicate solution, preserving patterns
241 242 243 |
# File 'lib/rdf/query/solution.rb', line 241 def dup merge({}) end |
#each_binding {|name, value| ... } ⇒ Enumerator Also known as: each
Enumerates over every variable binding in this solution.
60 61 62 63 |
# File 'lib/rdf/query/solution.rb', line 60 def each_binding(&block) @bindings.each(&block) if block_given? enum_binding end |
#each_name {|name| ... } ⇒ Enumerator Also known as: each_key
Enumerates over every variable name in this solution.
81 82 83 84 |
# File 'lib/rdf/query/solution.rb', line 81 def each_name(&block) @bindings.each_key(&block) if block_given? enum_name end |
#each_value {|value| ... } ⇒ Enumerator
Enumerates over every variable value in this solution.
102 103 104 105 |
# File 'lib/rdf/query/solution.rb', line 102 def each_value(&block) @bindings.each_value(&block) if block_given? enum_value end |
#each_variable {|variable| ... } ⇒ Enumerator
Enumerates over every variable in this solution.
134 135 136 137 138 139 140 141 |
# File 'lib/rdf/query/solution.rb', line 134 def each_variable if block_given? @bindings.each do |name, value| yield Variable.new(name, value) end end enum_variable end |
#enum_binding ⇒ Enumerator<RDF::Resource>
Returns an enumerator for #each_binding.
71 72 73 |
# File 'lib/rdf/query/solution.rb', line 71 def enum_binding enum_for(:each_binding) end |
#enum_name ⇒ Enumerator<RDF::Resource>
Returns an enumerator for #each_name.
92 93 94 |
# File 'lib/rdf/query/solution.rb', line 92 def enum_name enum_for(:each_name) end |
#enum_value ⇒ Enumerator<RDF::Resource>
Returns an enumerator for #each_value.
112 113 114 |
# File 'lib/rdf/query/solution.rb', line 112 def enum_value enum_for(:each_value) end |
#enum_variable ⇒ Enumerator<RDF::Resource>
Returns an enumerator for #each_variable.
148 149 150 |
# File 'lib/rdf/query/solution.rb', line 148 def enum_variable enum_for(:each_variable) end |
#eql?(other) ⇒ Boolean Also known as: ==
Equivalence of solution
309 310 311 |
# File 'lib/rdf/query/solution.rb', line 309 def eql?(other) other.is_a?(Solution) && @bindings.eql?(other.bindings) end |
#has_variables?(variables) ⇒ Boolean
Returns true if this solution contains bindings for any of the given variables.
124 125 126 |
# File 'lib/rdf/query/solution.rb', line 124 def has_variables?(variables) variables.any? { |variable| bound?(variable) } end |
#hash ⇒ Integer
Integer hash of this solution
303 304 305 |
# File 'lib/rdf/query/solution.rb', line 303 def hash @bindings.hash end |
#inspect ⇒ String
322 323 324 |
# File 'lib/rdf/query/solution.rb', line 322 def inspect sprintf("#<%s:%#0x(%s)>", self.class.name, __id__, @bindings.inspect) end |
#isomorphic_with?(other) ⇒ Boolean
Isomorphic Mappings Two solution mappings u1 and u2 are isomorphic if, for every variable v in dom(u1) and in dom(u2), u1(v) = u2(v).
282 283 284 285 286 |
# File 'lib/rdf/query/solution.rb', line 282 def isomorphic_with?(other) @bindings.all? do |k, v| !other.to_h.has_key?(k) || other[k].eql?(v) end end |
#merge(other) ⇒ RDF::Query::Solution
Merges the bindings from the given other query solution with a copy of this one.
234 235 236 |
# File 'lib/rdf/query/solution.rb', line 234 def merge(other) self.class.new(@bindings.dup).merge!(other) end |
#merge!(other) ⇒ void
This method returns an undefined value.
Merges the bindings from the given other query solution into this one, overwriting any existing ones having the same name.
## RDFStar (RDF*)
If merging a binding for a statement to a pattern, merge their embedded solutions.
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/rdf/query/solution.rb', line 207 def merge!(other) @bindings.merge!(other.to_h) do |key, v1, v2| # Don't merge a pattern over a statement # This happens because JOIN does a reverse merge, # and a pattern is set in v2. v2.is_a?(Pattern) ? v1 : v2 end # Merge bindings from patterns = [] @bindings.each do |k, v| if v.is_a?(Pattern) && other[k].is_a?(RDF::Statement) << v.solution(other[k]) end end # Merge embedded solutions .each {|soln| merge!(soln)} self end |
#to_a ⇒ Array<Array(Symbol, RDF::Term)>
290 291 292 |
# File 'lib/rdf/query/solution.rb', line 290 def to_a @bindings.to_a end |
#to_h ⇒ Hash{Symbol => RDF::Term}
296 297 298 |
# File 'lib/rdf/query/solution.rb', line 296 def to_h @bindings.dup end |
#unbound?(name) ⇒ Boolean
Returns true if the variable name is unbound in this solution.
168 169 170 |
# File 'lib/rdf/query/solution.rb', line 168 def unbound?(name) @bindings[name.to_sym].nil? end |