Class: RDF::Query::Pattern

Inherits:
Statement show all
Defined in:
vendor/bundler/ruby/2.1.0/bundler/gems/rdf-24bf82278685/lib/rdf/query/pattern.rb,
vendor/bundler/ruby/2.1.0/bundler/gems/sparql-fb1fb1facc64/lib/sparql/algebra/extensions.rb

Overview

An RDF query pattern.

Since:

Instance Attribute Summary (collapse)

Attributes inherited from Statement

#context, #id, #object, #predicate, #subject

Instance Method Summary (collapse)

Methods inherited from Statement

#==, #===, #[], #[]=, add_entailment, #asserted?, #canonicalize, #canonicalize!, #entail, #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, #variable?

Methods included from Value

#anonymous?, #canonicalize, #canonicalize!, #constant?, #graph?, #inspect, #inspect!, #invalid?, #iri?, #list?, #literal?, #node?, #resource?, #statement?, #term?, #to_nquads, #to_ntriples, #to_rdf, #to_term, #type_error, #uri?, #validate!, #variable?

Constructor Details

- (Pattern) initialize(options = {}) - (Pattern) initialize(subject, predicate, object, options = {})

Note:

Statement treats symbols as interned Node instances, in a RDF::Query::Pattern, they are treated as Variable.

Returns a new instance of Pattern

Overloads:



38
39
40
# File 'vendor/bundler/ruby/2.1.0/bundler/gems/rdf-24bf82278685/lib/rdf/query/pattern.rb', line 38

def initialize(subject = nil, predicate = nil, object = nil, options = {})
  super
end

Instance Attribute Details

- (Numeric) cost

The estimated cost of this pattern (for query optimization).

Returns:

  • (Numeric)


62
63
64
# File 'vendor/bundler/ruby/2.1.0/bundler/gems/rdf-24bf82278685/lib/rdf/query/pattern.rb', line 62

def cost
  @cost
end

- (Hash) options (readonly)

Any additional options for this pattern.

Returns:



56
57
58
# File 'vendor/bundler/ruby/2.1.0/bundler/gems/rdf-24bf82278685/lib/rdf/query/pattern.rb', line 56

def options
  @options
end

Instance Method Details

- (self) bind(solution)

Binds the pattern to a solution, making it no longer variable if all variables are resolved to bound variables

Parameters:

Returns:

  • (self)


252
253
254
255
256
257
258
259
# File 'vendor/bundler/ruby/2.1.0/bundler/gems/rdf-24bf82278685/lib/rdf/query/pattern.rb', line 252

def bind(solution)
  self.to_quad.each_with_index do |term, index|
    if term && term.variable? && solution[term]
      self[index] = solution[term] 
    end
  end
  self
end

- (Integer) binding_count

Returns the number of bindings in this pattern.

Returns:



273
274
275
# File 'vendor/bundler/ruby/2.1.0/bundler/gems/rdf-24bf82278685/lib/rdf/query/pattern.rb', line 273

def binding_count
  bindings.size
end

- (Hash{Symbol => RDF::Term}) bindings

Returns all bindings in this pattern.

Returns:



281
282
283
284
285
286
287
288
# File 'vendor/bundler/ruby/2.1.0/bundler/gems/rdf-24bf82278685/lib/rdf/query/pattern.rb', line 281

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.

Returns:

  • (Boolean)

    true or false



265
266
267
# File 'vendor/bundler/ruby/2.1.0/bundler/gems/rdf-24bf82278685/lib/rdf/query/pattern.rb', line 265

def bindings?
  !bindings.empty?
end

- (Boolean) blank?

Returns true if this is a blank pattern, with all terms being nil.

Returns:

  • (Boolean)

    true or false

Since:

  • 0.3.0



69
70
71
# File 'vendor/bundler/ruby/2.1.0/bundler/gems/rdf-24bf82278685/lib/rdf/query/pattern.rb', line 69

def blank?
  subject.nil? && predicate.nil? && object.nil? && context.nil?
end

- (Boolean) bound?

Returns true if all variables in this pattern are bound.

Returns:

  • (Boolean)

    true or false



294
295
296
# File 'vendor/bundler/ruby/2.1.0/bundler/gems/rdf-24bf82278685/lib/rdf/query/pattern.rb', line 294

def bound?
  !variables.empty? && variables.values.all?(&:bound?)
end

- (Hash{Symbol => Variable}) bound_variables

Returns all bound variables in this pattern.

Returns:



302
303
304
# File 'vendor/bundler/ruby/2.1.0/bundler/gems/rdf-24bf82278685/lib/rdf/query/pattern.rb', line 302

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.

Examples:

Pattern.new(:s, :p, :o).execute(RDF::Repository.load('etc/doap.nt'))

Parameters:

Yields:

  • (statement)

    each matching statement

Yield Parameters:

  • statement (RDF::Statement)

    an RDF statement matching this pattern

Returns:

  • (Enumerator)

    an enumerator yielding matching statements

See Also:

Since:

  • 0.3.0



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'vendor/bundler/ruby/2.1.0/bundler/gems/rdf-24bf82278685/lib/rdf/query/pattern.rb', line 137

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.

Returns:

  • (Boolean)

    true or false

Since:

  • 0.3.0



78
79
80
81
82
83
# File 'vendor/bundler/ruby/2.1.0/bundler/gems/rdf-24bf82278685/lib/rdf/query/pattern.rb', line 78

def has_variables?
  subject.is_a?(Variable) ||
    predicate.is_a?(Variable) ||
    object.is_a?(Variable) ||
    context.is_a?(Variable)
end

- (Array<RDF::Query::Variable>) ndvars

Return the non-destinguished variables contained within this pattern

Returns:

Since:

  • 0.3.0



389
390
391
# File 'vendor/bundler/ruby/2.1.0/bundler/gems/sparql-fb1fb1facc64/lib/sparql/algebra/extensions.rb', line 389

def ndvars
  vars.reject(&:distinguished?)
end

- (Boolean) optional?

Returns true if this is an optional pattern.

Examples:

Pattern.new(:s, :p, :o).optional?                     #=> false
Pattern.new(:s, :p, :o, :optional => true).optional?  #=> true

Returns:

  • (Boolean)

    true or false

Since:

  • 0.3.0



95
96
97
# File 'vendor/bundler/ruby/2.1.0/bundler/gems/rdf-24bf82278685/lib/rdf/query/pattern.rb', line 95

def optional?
  !!options[: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.

Examples:

pattern = Pattern.new(:s, :p, :o)
solution = pattern.solution(statement)

pattern[:s] #=> statement.subject
pattern[:p] #=> statement.predicate
pattern[:o] #=> statement.object

Parameters:

Returns:

Since:

  • 0.3.0



186
187
188
189
190
191
192
193
# File 'vendor/bundler/ruby/2.1.0/bundler/gems/rdf-24bf82278685/lib/rdf/query/pattern.rb', line 186

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.

Returns:



326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'vendor/bundler/ruby/2.1.0/bundler/gems/rdf-24bf82278685/lib/rdf/query/pattern.rb', line 326

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

Returns:

Since:

  • 0.3.0



378
379
380
381
382
383
384
# File 'vendor/bundler/ruby/2.1.0/bundler/gems/sparql-fb1fb1facc64/lib/sparql/algebra/extensions.rb', line 378

def to_sxp_bin
  if has_context?
    [:quad, subject, predicate, object, context]
  else
    [:triple, subject, predicate, object]
  end
end

- (Boolean) unbound?

Returns true if all variables in this pattern are unbound.

Returns:

  • (Boolean)

    true or false



310
311
312
# File 'vendor/bundler/ruby/2.1.0/bundler/gems/rdf-24bf82278685/lib/rdf/query/pattern.rb', line 310

def unbound?
  !variables.empty? && variables.values.all?(&:unbound?)
end

- (Hash{Symbol => Variable}) unbound_variables

Returns all unbound variables in this pattern.

Returns:



318
319
320
# File 'vendor/bundler/ruby/2.1.0/bundler/gems/rdf-24bf82278685/lib/rdf/query/pattern.rb', line 318

def unbound_variables
  variables.reject { |name, variable| variable.bound? }
end

- (Boolean) valid?

Is this pattern composed only of valid components?

Returns:

  • (Boolean)

    true or false



103
104
105
106
107
108
109
110
# File 'vendor/bundler/ruby/2.1.0/bundler/gems/rdf-24bf82278685/lib/rdf/query/pattern.rb', line 103

def valid?
  (has_subject?   ? (subject.resource? || subject.variable?) && subject.valid? : true) && 
  (has_predicate? ? (predicate.uri? || predicate.variable?) && predicate.valid? : true) &&
  (has_object?    ? (object.term? || object.variable?) && object.valid? : true) &&
  (has_context?   ? (context.resource? || context.variable?) && context.valid? : true )
rescue NoMethodError
  false
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.

Returns:



221
222
223
224
225
226
227
228
# File 'vendor/bundler/ruby/2.1.0/bundler/gems/rdf-24bf82278685/lib/rdf/query/pattern.rb', line 221

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.

Examples:

Pattern.new(RDF::Node.new, :p, 123).variable_terms    #=> [:predicate]

Parameters:

  • name (Symbol, #to_sym) (defaults to: nil)

    an optional variable name

Returns:

Since:

  • 0.3.0



205
206
207
208
209
210
211
212
# File 'vendor/bundler/ruby/2.1.0/bundler/gems/rdf-24bf82278685/lib/rdf/query/pattern.rb', line 205

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.

Returns:



238
239
240
241
242
243
244
245
# File 'vendor/bundler/ruby/2.1.0/bundler/gems/rdf-24bf82278685/lib/rdf/query/pattern.rb', line 238

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

- (Array<RDF::Query::Variable>) vars

Return the variables contained within this pattern

Returns:

Since:

  • 0.3.0



396
397
398
# File 'vendor/bundler/ruby/2.1.0/bundler/gems/sparql-fb1fb1facc64/lib/sparql/algebra/extensions.rb', line 396

def vars
  variables.values
end