Class: SPARQL::Algebra::Operator::Table

Inherits:
SPARQL::Algebra::Operator show all
Includes:
Query
Defined in:
lib/sparql/algebra/operator/table.rb

Overview

The SPARQL Table operator.

This is used to provide inline values. Each row becomes a solution.

[28] ValuesClause ::= ( ‘VALUES’ DataBlock )?

[61] InlineData ::= ‘VALUES’ DataBlock

Examples:

SPARQL Grammar (ValuesClause)

PREFIX dc:   <http://purl.org/dc/elements/1.1/> 
PREFIX :     <http://example.org/book/> 
PREFIX ns:   <http://example.org/ns#> 
SELECT ?book ?title ?price {
   ?book dc:title ?title ;
         ns:price ?price .
}
VALUES ?book { :book1 }

SSE (ValuesClause)

(prefix ((dc: <http://purl.org/dc/elements/1.1/>)
         (: <http://example.org/book/>)
         (ns: <http://example.org/ns#>))
 (project (?book ?title ?price)
  (join
   (bgp (triple ?book dc:title ?title) (triple ?book ns:price ?price))
   (table (vars ?book) (row (?book :book1)))) ))

SPARQL Grammar (empty query no values)

SELECT * { } VALUES () { }

SSE (empty query no values)

(join (bgp) (table empty))

SPARQL Grammar (InlineData)

PREFIX dc:   <http://purl.org/dc/elements/1.1/> 
PREFIX :     <http://example.org/book/> 
PREFIX ns:   <http://example.org/ns#> 

SELECT ?book ?title ?price
{
   VALUES ?book { :book1 }
   ?book dc:title ?title ;
         ns:price ?price .
}

SSE (InlineData)

(prefix ((dc: <http://purl.org/dc/elements/1.1/>)
         (: <http://example.org/book/>)
         (ns: <http://example.org/ns#>))
 (project (?book ?title ?price)
  (join
   (table (vars ?book) (row (?book :book1)))
   (bgp (triple ?book dc:title ?title) (triple ?book ns:price ?price))) ))

empty table

(table unit)

See Also:

Constant Summary collapse

NAME =
[:table]

Constants inherited from SPARQL::Algebra::Operator

ARITY, IsURI, URI

Constants included from Expression

Expression::PATTERN_PARENTS

Instance Attribute Summary

Attributes included from Query

#solutions

Attributes inherited from SPARQL::Algebra::Operator

#operands

Instance Method Summary collapse

Methods included from Query

#each_solution, #empty?, #failed?, #graph_name=, #matched?, #query_yields_boolean?, #query_yields_solutions?, #query_yields_statements?, #unshift

Methods inherited from SPARQL::Algebra::Operator

#aggregate?, arity, #base_uri, base_uri, base_uri=, #bind, #boolean, #constant?, #deep_dup, #each_descendant, #eql?, #evaluatable?, evaluate, #executable?, #first_ancestor, for, #initialize, #inspect, #ndvars, #node?, #operand, #optimize, #optimize!, #parent, #parent=, #prefixes, prefixes, prefixes=, #rewrite, #to_binary, to_sparql, #to_sxp, #to_sxp_bin, #validate!, #variable?, #vars

Methods included from Expression

cast, #constant?, #evaluate, extension, extension?, extensions, for, #invalid?, new, #node?, open, #optimize, #optimize!, parse, register_extension, #to_sxp_bin, #valid?, #validate!, #variable?

Constructor Details

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

Instance Method Details

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

Returns solutions for each row

Parameters:

  • queryable (RDF::Queryable)

    the graph or repository to query

  • options (Hash{Symbol => Object})

    any additional keyword options

Yields:

  • (solution)

    each matching solution

Yield Parameters:

Yield Returns:

  • (void)

    ignored

Returns:

See Also:



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/sparql/algebra/operator/table.rb', line 82

def execute(queryable, **options, &block)
  @solutions = RDF::Query::Solutions()
  Array(operands[1..-1]).each do |row|
    next unless row.is_a?(Array)
    bindings = row[1..-1].inject({}) do |memo, (var, value)|
      memo[var.to_sym] = value unless value == :undef
      memo
    end
    @solutions << RDF::Query::Solution.new(bindings)
  end
  @solutions.variable_names = self.variables.keys
  @solutions.each(&block) if block_given?
  @solutions
end

#to_sparql(top_level: true, **options) ⇒ String

Returns a partial SPARQL grammar for this operator.

Parameters:

  • top_level (Boolean) (defaults to: true)

    (true) Treat this as a top-level, generating SELECT … WHERE {}

Returns:

  • (String)


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/sparql/algebra/operator/table.rb', line 113

def to_sparql(top_level: true, **options)
  str = "VALUES (#{Array(operands.first)[1..-1].map { |e| e.to_sparql(**options) }.join(' ')}) {\n"
  operands[1..-1].each do |row|
    line = '('
    row[1..-1].each do |col|
      v = col[1].to_sparql(**options)
      line << v + ' '
    end
    line = line.chomp(' ')
    line << ")\n"

    str << line
  end

  str << "}\n"
  top_level ? Operator.to_sparql(str, **options) : str
end

#variablesHash{Symbol => RDF::Query::Variable}

In-scope variables for a table are the variables operand

Returns:



101
102
103
104
# File 'lib/sparql/algebra/operator/table.rb', line 101

def variables
  in_scope = operands.first.is_a?(Array) ? operands.first[1..-1] : []
  in_scope.inject({}) {|memo, v| memo.merge(v.variables)}
end