Class: SPARQL::Algebra::Operator::Construct

Inherits:
Binary show all
Includes:
Query
Defined in:
lib/sparql/algebra/operator/construct.rb

Overview

The SPARQL GraphPattern construct operator.

Examples:

(prefix ((rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>)
         (foaf: <http://xmlns.com/foaf/0.1/>))
  (construct ((triple ?s ?p ?o))
    (project (?s ?p ?o)
      (bgp (triple ?s ?p ?o)))))

See Also:

Constant Summary

NAME =
[:construct]

Constants inherited from Binary

Binary::ARITY

Constants inherited from SPARQL::Algebra::Operator

ARITY

Instance Attribute Summary

Attributes included from Query

#solutions

Attributes inherited from SPARQL::Algebra::Operator

#operands, #options

Instance Method Summary (collapse)

Methods included from Query

#context=, #each_solution, #failed?, #matched?, #unshift, #variables

Methods inherited from Binary

#initialize

Methods inherited from SPARQL::Algebra::Operator

arity, base_uri, #base_uri, base_uri=, #boolean, #constant?, #eql?, #evaluatable?, evaluate, #executable?, for, #initialize, #inspect, #operand, prefixes, #prefixes, prefixes=, #to_sse, #to_sxp, #variable?

Methods included from Evaluatable

#apply, #evaluate, #memoize

Methods included from Expression

cast, #constant?, #evaluate, for, new, open, parse, #to_sse, #variable?

Constructor Details

This class inherits a constructor from SPARQL::Algebra::Operator::Binary

Instance Method Details

- (RDF::Query::Solutions) execute(queryable, options = {})

Executes this query on the given RDF::Queryable object. Binds variables to the array of patterns in the first operand and returns the resulting RDF::Graph object

If any such instantiation produces a triple containing an unbound variable or an illegal RDF construct, such as an RDF::Literal in subject or predicate position, then that triple is not included in the output RDF graph. The graph template can contain triples with no variables (known as ground or explicit triples), and these also appear in the output RDF graph returned by the CONSTRUCT query form.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/sparql/algebra/operator/construct.rb', line 35

def execute(queryable, options = {})
  debug("Construct #{operands.first}, #{options.inspect}", options)
  graph = RDF::Graph.new
  patterns = operands.first
  query = operands.last

  query.execute(queryable, options.merge(:depth => options[:depth].to_i + 1)).each do |solution|
    debug("=> Apply #{solution.inspect} to BGP", options)
    
    # Create a mapping from BNodes within the pattern list to newly constructed BNodes
    nodes = {}
    patterns.each do |pattern|
      terms = {}
      [:subject, :predicate, :object].each do |r|
        terms[r] = case o = pattern.send(r)
        when RDF::Node            then nodes[o] ||= RDF::Node.new
        when RDF::Query::Variable then solution[o]
        else                           o
        end
      end
      
      statement = RDF::Statement.from(terms)

      # Sanity checking on statement
      if statement.subject.nil? || statement.predicate.nil? || statement.object.nil? ||
         statement.subject.literal? || statement.predicate.literal?
        debug("==> skip #{statement.inspect}", options)
        next
      end

      debug("==> add #{statement.inspect}", options)
      graph << statement
    end
  end
  
  debug("=> #{graph.dump(:n3)}", options)
  graph
end

- (Union, RDF::Query) optimize

Returns an optimized version of this query.

Return optimized query



80
81
82
# File 'lib/sparql/algebra/operator/construct.rb', line 80

def optimize
  operands = operands.map(&:optimize)
end