Class: RDF::Query::Pattern
- Inherits:
- Statement show all
- Defined in:
- vendor/bundler/ruby/2.0.0/bundler/gems/rdf-fa5fd9674f9a/lib/rdf/query/pattern.rb,
vendor/bundler/ruby/2.0.0/bundler/gems/sparql-da60321cd7bc/lib/sparql/algebra/extensions.rb
Overview
An RDF query pattern.
Instance Attribute Summary (collapse)
-
- (Numeric) cost
The estimated cost of this pattern (for query optimization).
-
- (Hash) options
readonly
Any additional options for this pattern.
Attributes inherited from Statement
#context, #id, #object, #predicate, #subject
Instance Method Summary (collapse)
-
- (Integer) binding_count
Returns the number of bindings in this pattern.
-
- (Hash{Symbol => RDF::Term}) bindings
Returns all bindings in this pattern.
-
- (Boolean) bindings?
Returns
trueif this pattern contains bindings. -
- (Boolean) blank?
Returns
trueif this is a blank pattern, with all terms beingnil. -
- (Boolean) bound?
Returns
trueif all variables in this pattern are bound. -
- (Hash{Symbol => Variable}) bound_variables
Returns all bound variables in this pattern.
-
- (Enumerator) execute(queryable, bindings = {}) {|statement| ... }
Executes this query pattern on the given
queryableobject. -
- (Boolean) has_variables?
(also: #variables?)
Returns
trueif this pattern contains any variables. -
- (Pattern) initialize(subject = nil, predicate = nil, object = nil, options = {})
constructor
A new instance of Pattern.
-
- (Boolean) optional?
Returns
trueif this is an optional pattern. -
- (RDF::Query::Solution) solution(statement)
Returns a query solution constructed by binding any variables in this pattern with the corresponding terms in the given
statement. -
- (String) to_s
Returns a string representation of this pattern.
-
- (Array) to_sxp_bin
Transform Query Pattern into an SXP.
-
- (Boolean) unbound?
Returns
trueif all variables in this pattern are unbound. -
- (Hash{Symbol => Variable}) unbound_variables
Returns all unbound variables in this pattern.
-
- (Integer) variable_count
(also: #cardinality, #arity)
Returns the number of variables in this pattern.
-
- (Array<Symbol>) variable_terms(name = nil)
Returns the variable terms in this pattern.
-
- (Hash{Symbol => Variable}) variables
Returns all variables in this pattern.
Methods inherited from Statement
#==, #===, #[], #[]=, #asserted?, #eql?, #has_blank_nodes?, #has_context?, #has_graph?, #has_object?, #has_predicate?, #has_subject?, #inferred?, #invalid?, #quoted?, #reified, #statement?, #to_hash, #to_quad, #to_triple, #valid?, #variable?
Methods included from Value
#anonymous?, #constant?, #graph?, #inspect, #inspect!, #invalid?, #iri?, #list?, #literal?, #node?, #resource?, #statement?, #term?, #to_nquads, #to_ntriples, #to_rdf, #type_error, #uri?, #valid?, #validate!, #variable?
Constructor Details
- (Pattern) initialize(options = {}) - (Pattern) initialize(subject, predicate, object, options = {})
A new instance of Pattern
35 36 37 |
# File 'vendor/bundler/ruby/2.0.0/bundler/gems/rdf-fa5fd9674f9a/lib/rdf/query/pattern.rb', line 35 def initialize(subject = nil, predicate = nil, object = nil, = {}) super end |
Instance Attribute Details
- (Numeric) cost
The estimated cost of this pattern (for query optimization).
59 60 61 |
# File 'vendor/bundler/ruby/2.0.0/bundler/gems/rdf-fa5fd9674f9a/lib/rdf/query/pattern.rb', line 59 def cost @cost end |
- (Hash) options (readonly)
Any additional options for this pattern.
53 54 55 |
# File 'vendor/bundler/ruby/2.0.0/bundler/gems/rdf-fa5fd9674f9a/lib/rdf/query/pattern.rb', line 53 def @options end |
Instance Method Details
- (Integer) binding_count
Returns the number of bindings in this pattern.
243 244 245 |
# File 'vendor/bundler/ruby/2.0.0/bundler/gems/rdf-fa5fd9674f9a/lib/rdf/query/pattern.rb', line 243 def binding_count bindings.size end |
- (Hash{Symbol => RDF::Term}) bindings
Returns all bindings in this pattern.
251 252 253 254 255 256 257 258 |
# File 'vendor/bundler/ruby/2.0.0/bundler/gems/rdf-fa5fd9674f9a/lib/rdf/query/pattern.rb', line 251 def bindings bindings = {} bindings.merge!(subject.bindings) if subject.is_a?(Variable) bindings.merge!(predicate.bindings) if predicate.is_a?(Variable) bindings.merge!(object.bindings) if object.is_a?(Variable) bindings.merge!(context.bindings) if context.is_a?(Variable) bindings end |
- (Boolean) bindings?
Returns true if this pattern contains bindings.
235 236 237 |
# File 'vendor/bundler/ruby/2.0.0/bundler/gems/rdf-fa5fd9674f9a/lib/rdf/query/pattern.rb', line 235 def bindings? !bindings.empty? end |
- (Boolean) blank?
Returns true if this is a blank pattern, with all terms being nil.
66 67 68 |
# File 'vendor/bundler/ruby/2.0.0/bundler/gems/rdf-fa5fd9674f9a/lib/rdf/query/pattern.rb', line 66 def blank? subject.nil? && predicate.nil? && object.nil? && context.nil? end |
- (Boolean) bound?
Returns true if all variables in this pattern are bound.
264 265 266 |
# File 'vendor/bundler/ruby/2.0.0/bundler/gems/rdf-fa5fd9674f9a/lib/rdf/query/pattern.rb', line 264 def bound? !variables.empty? && variables.values.all?(&:bound?) end |
- (Hash{Symbol => Variable}) bound_variables
Returns all bound variables in this pattern.
272 273 274 |
# File 'vendor/bundler/ruby/2.0.0/bundler/gems/rdf-fa5fd9674f9a/lib/rdf/query/pattern.rb', line 272 def bound_variables variables.reject { |name, variable| variable.unbound? } end |
- (Enumerator) execute(queryable, bindings = {}) {|statement| ... }
Executes this query pattern on the given queryable object.
Values are matched using using Queryable#query_pattern.
If the optional bindings are given, variables will be substituted with their values
when executing the query.
To match triples only in the default context, set context to false.
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 |
# File 'vendor/bundler/ruby/2.0.0/bundler/gems/rdf-fa5fd9674f9a/lib/rdf/query/pattern.rb', line 121 def execute(queryable, bindings = {}, &block) query = { :subject => subject.is_a?(Variable) && bindings[subject.to_sym] ? bindings[subject.to_sym] : subject, :predicate => predicate.is_a?(Variable) && bindings[predicate.to_sym] ? bindings[predicate.to_sym] : predicate, :object => object.is_a?(Variable) && bindings[object.to_sym] ? bindings[object.to_sym] : object, :context => context.is_a?(Variable) && bindings[context.to_sym] ? bindings[context.to_sym] : context, }.delete_if{|k,v| v.nil?} # Do all the variable terms refer to distinct variables? variables = self.variables if variable_count == variables.size # If so, we can just let the repository implementation handle # everything and yield matching statements directly: queryable.query(query, &block) # No, some terms actually refer to the same variable... else # Figure out which terms refer to the same variable: terms = variables.each_key.find do |name| terms = variable_terms(name) break terms if terms.size > 1 end queryable.query(query) do |statement| # Only yield those matching statements where the variable # constraint is also satisfied: # FIXME: `Array#uniq` uses `#eql?` and `#hash`, not `#==` if matches = terms.map { |term| statement.send(term) }.uniq.size.equal?(1) block.call(statement) end end end end |
- (Boolean) has_variables? Also known as: variables?
Returns true if this pattern contains any variables.
75 76 77 78 79 80 |
# File 'vendor/bundler/ruby/2.0.0/bundler/gems/rdf-fa5fd9674f9a/lib/rdf/query/pattern.rb', line 75 def has_variables? subject.is_a?(Variable) || predicate.is_a?(Variable) || object.is_a?(Variable) || context.is_a?(Variable) end |
- (Boolean) optional?
Returns true if this is an optional pattern.
92 93 94 |
# File 'vendor/bundler/ruby/2.0.0/bundler/gems/rdf-fa5fd9674f9a/lib/rdf/query/pattern.rb', line 92 def optional? !![:optional] end |
- (RDF::Query::Solution) solution(statement)
Returns a query solution constructed by binding any variables in this
pattern with the corresponding terms in the given statement.
170 171 172 173 174 175 176 177 |
# File 'vendor/bundler/ruby/2.0.0/bundler/gems/rdf-fa5fd9674f9a/lib/rdf/query/pattern.rb', line 170 def solution(statement) RDF::Query::Solution.new do |solution| solution[subject.to_sym] = statement.subject if subject.is_a?(Variable) solution[predicate.to_sym] = statement.predicate if predicate.is_a?(Variable) solution[object.to_sym] = statement.object if object.is_a?(Variable) solution[context.to_sym] = statement.context if context.is_a?(Variable) end end |
- (String) to_s
Returns a string representation of this pattern.
296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'vendor/bundler/ruby/2.0.0/bundler/gems/rdf-fa5fd9674f9a/lib/rdf/query/pattern.rb', line 296 def to_s StringIO.open do |buffer| # FIXME in RDF::Statement buffer << 'OPTIONAL ' if optional? buffer << [subject, predicate, object].map do |r| r.is_a?(RDF::Query::Variable) ? r.to_s : RDF::NTriples.serialize(r) end.join(" ") buffer << case context when nil, false then " ." when Variable then " #{context.to_s} ." else " #{RDF::NTriples.serialize(context)} ." end buffer.string end end |
- (Array) to_sxp_bin
Transform Query Pattern into an SXP
175 176 177 |
# File 'vendor/bundler/ruby/2.0.0/bundler/gems/sparql-da60321cd7bc/lib/sparql/algebra/extensions.rb', line 175 def to_sxp_bin [:triple, subject, predicate, object] end |
- (Boolean) unbound?
Returns true if all variables in this pattern are unbound.
280 281 282 |
# File 'vendor/bundler/ruby/2.0.0/bundler/gems/rdf-fa5fd9674f9a/lib/rdf/query/pattern.rb', line 280 def unbound? !variables.empty? && variables.values.all?(&:unbound?) end |
- (Hash{Symbol => Variable}) unbound_variables
Returns all unbound variables in this pattern.
288 289 290 |
# File 'vendor/bundler/ruby/2.0.0/bundler/gems/rdf-fa5fd9674f9a/lib/rdf/query/pattern.rb', line 288 def unbound_variables variables.reject { |name, variable| variable.bound? } end |
- (Integer) variable_count Also known as: cardinality, arity
Returns the number of variables in this pattern.
Note: this does not count distinct variables, and will therefore e.g. return 3 even if two terms are actually the same variable.
205 206 207 208 209 210 211 212 |
# File 'vendor/bundler/ruby/2.0.0/bundler/gems/rdf-fa5fd9674f9a/lib/rdf/query/pattern.rb', line 205 def variable_count count = 0 count += 1 if subject.is_a?(Variable) count += 1 if predicate.is_a?(Variable) count += 1 if object.is_a?(Variable) count += 1 if context.is_a?(Variable) count end |
- (Array<Symbol>) variable_terms(name = nil)
Returns the variable terms in this pattern.
189 190 191 192 193 194 195 196 |
# File 'vendor/bundler/ruby/2.0.0/bundler/gems/rdf-fa5fd9674f9a/lib/rdf/query/pattern.rb', line 189 def variable_terms(name = nil) terms = [] terms << :subject if subject.is_a?(Variable) && (!name || name.eql?(subject.name)) terms << :predicate if predicate.is_a?(Variable) && (!name || name.eql?(predicate.name)) terms << :object if object.is_a?(Variable) && (!name || name.eql?(object.name)) terms << :context if context.is_a?(Variable) && (!name || name.eql?(context.name)) terms end |
- (Hash{Symbol => Variable}) variables
Returns all variables in this pattern.
Note: this returns a hash containing distinct variables only.
222 223 224 225 226 227 228 229 |
# File 'vendor/bundler/ruby/2.0.0/bundler/gems/rdf-fa5fd9674f9a/lib/rdf/query/pattern.rb', line 222 def variables variables = {} variables.merge!(subject.variables) if subject.is_a?(Variable) variables.merge!(predicate.variables) if predicate.is_a?(Variable) variables.merge!(object.variables) if object.is_a?(Variable) variables.merge!(context.variables) if context.is_a?(Variable) variables end |