Class: RDF::Query

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rdf/query.rb,
lib/rdf/query/pattern.rb,
lib/rdf/query/solution.rb,
lib/rdf/query/variable.rb,
lib/rdf/query/solutions.rb,
lib/rdf/query/hash_pattern_normalizer.rb

Overview

An RDF basic graph pattern (BGP) query.

Named queries either match against a specifically named graph if the name is an RDF::Resource or bound RDF::Query::Variable. Names that are against unbound variables match either default or named graphs. The name of false will only match against the default graph.

Variable names cause the variable to be added to the solution set elements.

Examples:

Constructing a basic graph pattern query (1)

query = RDF::Query.new do
  pattern [:person, RDF.type,  FOAF.Person]
  pattern [:person, FOAF.name, :name]
  pattern [:person, FOAF.mbox, :email]
end

Constructing a basic graph pattern query (2)

query = RDF::Query.new({
  person: {
    RDF.type  => FOAF.Person,
    FOAF.name => :name,
    FOAF.mbox => :email,
  }
})

Executing a basic graph pattern query

graph = RDF::Graph.load('etc/doap.nt')
query.execute(graph).each do |solution|
  puts solution.inspect
end

Constructing and executing a query in one go (1)

solutions = RDF::Query.execute(graph) do
  pattern [:person, RDF.type, FOAF.Person]
end

Constructing and executing a query in one go (2)

solutions = RDF::Query.execute(graph, {
  person: {
    RDF.type => FOAF.Person,
  }
})

In this example, the default graph contains the names of the publishers of two named graphs. The triples in the named graphs are not visible in the default graph in this example.

# default graph
@prefix dc: <http://purl.org/dc/elements/1.1/

<http://example.org/bob>    dc:publisher  "Bob" .
<http://example.org/alice>  dc:publisher  "Alice" .

# Named graph: http://example.org/bob
@prefix foaf: <http://xmlns.com/foaf/0.1/> .

_:a foaf:name "Bob" .
_:a foaf:mbox <mailto:bob@oldcorp.example.org> .

# Named graph: http://example.org/alice
@prefix foaf: <http://xmlns.com/foaf/0.1/> .

_:a foaf:name "Alice" .
_:a foaf:mbox <mailto:alice@work.example.org> .

See Also:

Since:

  • 0.3.0

Defined Under Namespace

Classes: HashPatternNormalizer, Pattern, Solution, Solutions, Variable

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#contexts, #dump, #each_context, #each_graph, #each_object, #each_predicate, #each_quad, #each_subject, #each_triple, #enum_context, #enum_graph, #enum_object, #enum_predicate, #enum_quad, #enum_statement, #enum_subject, #enum_triple, #graph_names, #has_context?, #has_graph?, #has_object?, #has_predicate?, #has_quad?, #has_statement?, #has_subject?, #has_triple?, #invalid?, #method_missing, #objects, #predicates, #quads, #statements, #subjects, #supports?, #to_a, #to_hash, #to_set, #triples

Methods included from Util::Aliasing::LateBound

#alias_method

Methods included from Countable

#count

Constructor Details

#initialize(patterns = [], options = {}) {|query| ... } ⇒ Query #initialize(patterns, options = {}) {|query| ... } ⇒ Query

Initializes a new basic graph pattern query.

Overloads:

  • #initialize(patterns = [], options = {}) {|query| ... } ⇒ Query

    Parameters:

    • patterns (Array<RDF::Query::Pattern>) (defaults to: [])

      ...

    • options (Hash{Symbol => Object}) (defaults to: {})

      any additional keyword options

    Options Hash (options):

    • :solutions (RDF::Query::Solutions) — default: Solutions.new
    • :graph_name (RDF::Resource, RDF::Query::Variable, false) — default: nil

      Default graph name for matching against queryable. Named queries either match against a specifically named graphs if the name is an Resource or bound Variable. Names that are against unbound variables match either default or named graphs. The name of false will only match against the default graph.

    • :context (RDF::Resource, RDF::Query::Variable, false) — default: nil

      Alias for :graph_name. The :context option is deprecated in RDF.rb 2.0.

    • :name (RDF::Resource, RDF::Query::Variable, false) — default: nil

      Alias for :graph_name.

    Yields:

    • (query)

    Yield Parameters:

    Yield Returns:

    • (void)

      ignored

  • #initialize(patterns, options = {}) {|query| ... } ⇒ Query

    Parameters:

    • patterns (Hash{Object => Object})

      ...

    • options (Hash{Symbol => Object}) (defaults to: {})

      any additional keyword options

    Options Hash (options):

    • :solutions (RDF::Query::Solutions) — default: Solutions.new
    • :graph_name (RDF::Resource, RDF::Query::Variable, false) — default: nil

      Default graph name for matching against queryable. Named queries either match against a specifically named graphs if the name is an Resource or bound Variable. Names that are against unbound variables match either default or named graphs. The name of false will only match against the default graph.

    • :context (RDF::Resource, RDF::Query::Variable, false) — default: nil

      Alias for :graph_name. The :context option is deprecated in RDF.rb 2.0.

    • :name (RDF::Resource, RDF::Query::Variable, false) — default: nil

      Alias for :graph_name.

    Yields:

    • (query)

    Yield Parameters:

    Yield Returns:

    • (void)

      ignored

Since:

  • 0.3.0



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/rdf/query.rb', line 185

def initialize(*patterns, &block)
  @options  = patterns.last.is_a?(Hash) ? patterns.pop.dup : {}
  if @options.has_key?(:context)
    warn "[DEPRECATION] the :contexts option to Query#initialize is deprecated in RDF.rb 2.0, use :graph_name instead. Called from #{Gem.location_of_caller.join(':')}"
    @options[:graph_name] ||= options.delete(:context)
  end
  patterns << @options if patterns.empty?
  @variables = {}
  @solutions = Query::Solutions(@options.delete(:solutions))
  graph_name = @options.fetch(:graph_name, @options.fetch(:name, nil))
  @options.delete(:graph_name)
  @options.delete(:name)

  @patterns  = case patterns.first
    when Hash  then compile_hash_patterns(HashPatternNormalizer.normalize!(patterns.first.dup, @options))
    when Array then patterns.first
    else patterns
  end

  self.graph_name = graph_name

  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 in the class RDF::Enumerable

Instance Attribute Details

#optionsHash (readonly)

Any additional options for this query.

Returns:

  • (Hash)

Since:

  • 0.3.0



139
140
141
# File 'lib/rdf/query.rb', line 139

def options
  @options
end

#patternsArray<RDF::Query::Pattern> (readonly)

The patterns that constitute this query.

Returns:

Since:

  • 0.3.0



127
128
129
# File 'lib/rdf/query.rb', line 127

def patterns
  @patterns
end

#solutionsRDF::Query::Solutions (readonly)

The solution sequence for this query.

Returns:

Since:

  • 0.3.0



133
134
135
# File 'lib/rdf/query.rb', line 133

def solutions
  @solutions
end

#variablesHash{Symbol => RDF::Query::Variable} (readonly)

The variables used in this query.

Returns:

Since:

  • 0.3.0



121
122
123
# File 'lib/rdf/query.rb', line 121

def variables
  @variables
end

Class Method Details

.execute(queryable, patterns = {}, options = {}) {|query| ... } ⇒ RDF::Query::Solutions

Executes a query on the given queryable graph or repository.

Parameters:

  • queryable (RDF::Queryable)

    the graph or repository to query

  • patterns (Hash{Object => Object}) (defaults to: {})

    optional hash patterns to initialize the query with

  • options (Hash{Symbol => Object}) (defaults to: {})

    any additional keyword options (see #initialize)

Yields:

  • (query)

Yield Parameters:

Yield Returns:

  • (void)

    ignored

Returns:

See Also:

Since:

  • 0.3.0



92
93
94
# File 'lib/rdf/query.rb', line 92

def self.execute(queryable, patterns = {}, options = {}, &block)
  self.new(patterns, options, &block).execute(queryable, options)
end

.SolutionsSolutions .Solutions(solutions) ⇒ Solutions .Solutions(array) ⇒ Solutions .Solutions(*args) ⇒ Solutions

Cast values as Solutions

Overloads:

  • .SolutionsSolutions

    Returns Solutions.new()

    Returns:

  • .Solutions(solutions) ⇒ Solutions

    Returns the argument

    Returns:

  • .Solutions(array) ⇒ Solutions

    Returns the array extended with solutions

    Parameters:

    • array (Array)

    Returns:

    • (Solutions)

      returns the array extended with solutions

  • .Solutions(*args) ⇒ Solutions

    Returns new solutions including the arguments, which must each be a Solution

    Parameters:

    Returns:

    • (Solutions)

      returns new solutions including the arguments, which must each be a Solution

Since:

  • 0.3.0



111
112
113
114
115
# File 'lib/rdf/query.rb', line 111

def self.Solutions(*args)
  return args.first if args.length == 1 && args.first.is_a?(Solutions)
  args = args.first if args.first.is_a?(Array) && args.length == 1
  return Solutions.new(args)
end

Instance Method Details

#+(other) ⇒ RDF::Query

Add patterns from another query to form a new Query

Parameters:

Returns:

Since:

  • 0.3.0



413
414
415
# File 'lib/rdf/query.rb', line 413

def +(other)
  Query.new(self.patterns + other.patterns)
end

#<<(pattern)

This method returns an undefined value.

Appends the given query pattern to this query.

Parameters:

Since:

  • 0.3.0



220
221
222
223
# File 'lib/rdf/query.rb', line 220

def <<(pattern)
  @patterns << Pattern.from(pattern)
  self
end

#apply_context(context = options[:context]) ⇒ Object

Apply the context specified (or configured) to all patterns that have no context

Parameters:

Since:

  • 0.3.0



468
469
470
471
# File 'lib/rdf/query.rb', line 468

def apply_context(context = options[:context])
  warn "[DEPRECATION] Query#apply_context is deprecated in RDF.rb 2.0, use Query#apply_graph_name instead. Called from #{Gem.location_of_caller.join(':')}"
  apply_graph_name(context)
end

#apply_graph_name(graph_name = options[:graph_name]) ⇒ Object

Apply the graph name specified (or configured) to all patterns that have no graph name

Parameters:

Since:

  • 0.3.0



475
476
477
# File 'lib/rdf/query.rb', line 475

def apply_graph_name(graph_name = options[:graph_name])
  patterns.each {|pattern| pattern.graph_name = graph_name if pattern.graph_name.nil?} unless graph_name.nil?
end

#contextRDF::IRI, RDF::Query::Variable

Deprecated.

Use #graph_name instead.

Scope of this query, if any

Returns:

Since:

  • 0.3.0



455
456
457
458
# File 'lib/rdf/query.rb', line 455

def context
  warn "[DEPRECATION] Query#context is deprecated in RDF.rb 2.0, use Query#graph_name instead. Called from #{Gem.location_of_caller.join(':')}"
  graph_name
end

#context=(value) ⇒ RDF::IRI, RDF::Query::Variable

Deprecated.

Use #graph_name= instead.

Scope the query to named graphs matching value

Parameters:

Returns:

Since:

  • 0.3.0



440
441
442
443
# File 'lib/rdf/query.rb', line 440

def context=(value)
  warn "[DEPRECATION] Query#context= is deprecated in RDF.rb 2.0, use Query#graph_name= instead. Called from #{Gem.location_of_caller.join(':')}"
  self.graph_name = value
end

#default?Boolean

Is this query scoped to the default graph?

Returns:

  • (Boolean)

Since:

  • 0.3.0



425
426
427
# File 'lib/rdf/query.rb', line 425

def default?
  options[:graph_name] == false
end

#dupRDF::Query

Duplicate query, including patterns and solutions

Returns:

Since:

  • 0.3.0



528
529
530
531
532
# File 'lib/rdf/query.rb', line 528

def dup
  patterns = @patterns.map {|p| p.dup}
  patterns << @options.merge(solutions: @solutions.dup)
  Query.new(*patterns)
end

#each_solution {|solution| ... } ⇒ Enumerator Also known as: each

Enumerates over each matching query solution.

Yields:

  • (solution)

Yield Parameters:

Returns:

Since:

  • 0.3.0



509
510
511
# File 'lib/rdf/query.rb', line 509

def each_solution(&block)
  @solutions.each(&block)
end

#each_statement {|RDF::Query::Pattern| ... } ⇒ Enumerator

Enumerates over each statement (pattern).

Yields:

Yield Parameters:

  • pattern (::Query::Pattern)

Returns:

Since:

  • 0.3.0



520
521
522
523
# File 'lib/rdf/query.rb', line 520

def each_statement(&block)
  apply_graph_name
  patterns.each(&block)
end

#empty?Boolean

Query has no patterns

Returns:

  • (Boolean)

Since:

  • 0.3.0



498
499
500
# File 'lib/rdf/query.rb', line 498

def empty?
  patterns.empty?
end

#execute(queryable, options = {}) {|solution| ... } ⇒ RDF::Query::Solutions

Executes this query on the given queryable graph or repository.

Named queries either match against a specifically named graphs if the name is an RDF::Resource or bound RDF::Query::Variable. Names that are against unbound variables match either detault or named graphs. The name of false will only match against the default graph.

If the query nas no patterns, it returns a single empty solution as per SPARQL 1.1 Empty Group Pattern.

Parameters:

  • queryable (RDF::Queryable)

    the graph or repository to query

  • options (Hash{Symbol => Object}) (defaults to: {})

    any additional keyword options

Options Hash (options):

Yields:

  • (solution)

    each matching solution

Yield Parameters:

Yield Returns:

  • (void)

    ignored

Returns:

See Also:

Since:

  • 0.3.0



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/rdf/query.rb', line 302

def execute(queryable, options = {}, &block)
  validate!
  options = options.dup
  if options.has_key?(:context)
    warn "[DEPRECATION] the :contexts option to Query#execute is deprecated in RDF.rb 2.0, use :graph_name instead. Called from #{Gem.location_of_caller.join(':')}"
    options[:graph_name] ||= options.delete(:context)
  end

  # just so we can call #keys below without worrying
  options[:bindings] ||= {}

  # Use provided solutions to allow for query chaining
  # Otherwise, a quick empty solution simplifies the logic below; no special case for
  # the first pattern
  @solutions = Query::Solutions(options[:solutions] || Solution.new)

  # If there are no patterns, just return the empty solution
  if empty?
    @solutions.each(&block) if block_given?
    return @solutions
  end

  patterns = @patterns
  graph_name = options.fetch(:graph_name, options.fetch(:name, self.graph_name))

  # Add graph_name to pattern, if necessary
  unless graph_name.nil?
    if patterns.empty?
      patterns = [Pattern.new(nil, nil, nil, graph_name: graph_name)]
    else
      apply_graph_name(graph_name)
    end
  end

  patterns.each do |pattern|

    old_solutions, @solutions = @solutions, Query::Solutions()

    options[:bindings].each_key do |variable|
      if pattern.variables.include?(variable)
        unbound_solutions, old_solutions = old_solutions, Query::Solutions()
        options[:bindings][variable].each do |binding|
          unbound_solutions.each do |solution|
            old_solutions << solution.merge(variable => binding)
          end
        end
        options[:bindings].delete(variable)
      end
    end

    old_solutions.each do |solution|
      found_match = false
      pattern.execute(queryable, solution) do |statement|
        found_match = true
        @solutions << solution.merge(pattern.solution(statement))
      end
      # If this pattern was optional, and we didn't find any matches,
      # just copy it over as-is.
      if !found_match && pattern.optional?
        @solutions << solution
      end
    end

    #puts "solutions after #{pattern} are #{@solutions.to_a.inspect}"

    # It's important to abort failed queries quickly because later patterns
    # that can have constraints are often broad without them.
    # We have no solutions at all:
    return @solutions if @solutions.empty?

    if !pattern.optional?
      # We have no solutions for variables we should have solutions for:
      need_vars = pattern.variables.keys
      @solutions.each do |solution|
        break if need_vars.empty?
        need_vars -= solution.bindings.keys
      end
      return Query::Solutions() unless need_vars.empty?
    end
  end
  @solutions.each(&block) if block_given?
  @solutions
end

#failed?Boolean

Returns true if this query did not match when last executed.

When the solution sequence is empty, this method can be used to determine whether the query failed to match or not.

Returns:

  • (Boolean)

See Also:

Since:

  • 0.3.0



394
395
396
# File 'lib/rdf/query.rb', line 394

def failed?
  @solutions.empty?
end

#graph_nameRDF::IRI, RDF::Query::Variable

Scope of this query, if any

Returns:

Since:

  • 0.3.0



462
463
464
# File 'lib/rdf/query.rb', line 462

def graph_name
  options[:graph_name]
end

#graph_name=(value) ⇒ RDF::IRI, RDF::Query::Variable

Scope the query to named graphs matching value

Parameters:

Returns:

Since:

  • 0.3.0



448
449
450
# File 'lib/rdf/query.rb', line 448

def graph_name=(value)
  options[:graph_name] = value
end

#matched?Boolean

Returns true if this query matched when last executed.

When the solution sequence is empty, this method can be used to determine whether the query matched successfully or not.

Returns:

  • (Boolean)

See Also:

Since:

  • 0.3.0



406
407
408
# File 'lib/rdf/query.rb', line 406

def matched?
  !failed?
end

#named?Boolean

Is this query scoped to a named graph?

Returns:

  • (Boolean)

Since:

  • 0.3.0



419
420
421
# File 'lib/rdf/query.rb', line 419

def named?
  !!options[:graph_name]
end

#node?Boolean Also known as: has_blank_nodes?

Returns true if any pattern contains a blank node.

Returns:

  • (Boolean)

Since:

  • 2.0



492
493
494
# File 'lib/rdf/query.rb', line 492

def node?
  patterns.any?(&:node?) || graph_name && graph_name.node?
end

#optimize(options = {}) ⇒ RDF::Query

Returns an optimized copy of this query.

Parameters:

  • options (Hash{Symbol => Object}) (defaults to: {})

    any additional options for optimization

Returns:

Since:

  • 0.3.0



247
248
249
# File 'lib/rdf/query.rb', line 247

def optimize(options = {})
  self.dup.optimize!(options)
end

#optimize!(options = {}) ⇒ self

Optimizes this query by reordering its constituent triple patterns according to their cost estimates.

Parameters:

  • options (Hash{Symbol => Object}) (defaults to: {})

    any additional options for optimization

Returns:

  • (self)

See Also:

Since:

  • 0.3.0



260
261
262
263
264
265
# File 'lib/rdf/query.rb', line 260

def optimize!(options = {})
  @patterns.sort! do |a, b|
    (a.cost || 0) <=> (b.cost || 0)
  end
  self
end

#pattern(pattern, options = {})

This method returns an undefined value.

Appends the given query pattern to this query.

Parameters:

  • pattern (RDF::Query::Pattern)

    a triple query pattern

  • options (Hash{Symbol => Object}) (defaults to: {})

    any additional keyword options

Options Hash (options):

  • :optional (Boolean) — default: false

    whether this is an optional pattern

Since:

  • 0.3.0



235
236
237
238
# File 'lib/rdf/query.rb', line 235

def pattern(pattern, options = {})
  @patterns << Pattern.from(pattern, options)
  self
end

#unnamed?Boolean

Is this query unscoped? This indicates that it can return results from either a named graph or the default graph.

Returns:

  • (Boolean)

Since:

  • 0.3.0



432
433
434
# File 'lib/rdf/query.rb', line 432

def unnamed?
  options[:graph_name].nil?
end

#valid?Boolean

Determine if the URI is a valid according to RFC3987

Returns:

  • (Boolean)

    true or false

Since:

  • 0.3.9



539
540
541
542
543
# File 'lib/rdf/query.rb', line 539

def valid?
  !!validate!
rescue
  false
end

#validate!RDF::Query

Validate this query, making sure it can be executed by our query engine. This method is public so that it may be called by implementations of RDF::Queryable#query_execute that bypass our built-in query engine.

Returns:

Raises:

  • (ArgumentError)

    This query cannot be executed.

Since:

  • 0.3.0



552
553
554
555
556
557
558
559
560
561
562
563
564
# File 'lib/rdf/query.rb', line 552

def validate!
  # All patterns must be valid
  @patterns.each(&:validate!)

  # All optional patterns must appear after the regular patterns.
  if i = @patterns.find_index(&:optional?)
    unless @patterns[i..-1].all?(&:optional?)
      raise ArgumentError.new("Optional patterns must appear at end of query")
    end
  end

  self
end

#variable?Boolean

Returns true if any pattern contains a variable.

Returns:

  • (Boolean)

Since:

  • 0.3.0



483
484
485
# File 'lib/rdf/query.rb', line 483

def variable?
  patterns.any?(&:variable?) || graph_name && graph_name.variable?
end