Class: SPARQL::Algebra::Operator::Graph

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

Overview

The SPARQL GraphPattern graph operator.

This is a wrapper to add a graph_name to the query, or an array of statements.

[58] GraphGraphPattern ::= ‘GRAPH’ VarOrIri GroupGraphPattern

Examples:

SPARQL Grammar (query)

PREFIX : <http://example/> 
SELECT * { 
    GRAPH ?g { ?s ?p ?o }
}

SSE

(prefix ((: <http://example/>))
 (graph ?g
  (bgp (triple ?s ?p ?o))))

SPARQL Grammar (named set of statements)

PREFIX : <http://example/> 
SELECT * { 
    GRAPH :g { :s :p :o }
}

SSE (named set of statements)

(prefix ((: <http://example/>))
 (graph :g
  (bgp (triple :s :p :o))))

SPARQL Grammar (syntax-graph-05.rq)

PREFIX : <http://example.org/>
SELECT *
WHERE
{
  :x :p :z
  GRAPH ?g { :x :b ?a . GRAPH ?g2 { :x :p ?x } }
}

SSE (syntax-graph-05.rq)

(prefix ((: <http://example.org/>))
 (join
  (bgp (triple :x :p :z))
  (graph ?g
   (join
    (bgp (triple :x :b ?a))
    (graph ?g2
     (bgp (triple :x :p ?x)))))))

SPARQL Grammar (pp06.rq)

prefix ex:	<http://www.example.org/schema#>
prefix in:	<http://www.example.org/instance#>

select ?x where {
  graph ?g {in:a ex:p1/ex:p2 ?x}
}

SSE (syntax-graph-05.rq)

(prefix ((ex: <http://www.example.org/schema#>)
         (in: <http://www.example.org/instance#>))
 (project (?x)
  (graph ?g
   (path in:a (seq ex:p1 ex:p2) ?x))))

See Also:

Constant Summary collapse

NAME =
[:graph]

Constants inherited from Binary

Binary::ARITY

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

Class Method Summary collapse

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, #variables

Methods inherited from Binary

#initialize

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=, #to_binary, to_sparql, #to_sxp, #to_sxp_bin, #validate!, #variable?, #variables, #vars

Methods included from Expression

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

Constructor Details

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

Class Method Details

.self.new(name, bgp) ⇒ RDF::Query .self.new(name, bgp) ⇒ RDF::Query .self.new(name, patterns) ⇒ RDF::Query

A graph is an RDF::Query with a graph_name. It can also be used as a container of statements or patterns, or other queryable operators (see GraphGraphPattern)

Overloads:

  • .self.new(name, bgp) ⇒ RDF::Query

    Parameters:

    • name (RDF::Resource)
    • patterns (RDF::Query)

      A sub-query (bgp)

  • .self.new(name, bgp) ⇒ RDF::Query

    Parameters:

    • name (RDF::Resource)
    • patterns (Operator)

      A sub-query (GraphGraphPattern)

  • .self.new(name, patterns) ⇒ RDF::Query

    Parameters:

Returns:



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/sparql/algebra/operator/graph.rb', line 87

def self.new(name, patterns, **options, &block)
  case patterns
  when RDF::Query
    # Record that the argument as a (bgp) for re-serialization back to SSE
    RDF::Query.new(*patterns.patterns, graph_name: name, &block)
  when Operator
    super
  else
    RDF::Query.new(*patterns, graph_name: name, as_container: true, &block)
  end
end

Instance Method Details

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

If the second operand is a Query operator: Executes this query on the given queryable graph or repository. Applies the given graph_name to the query, limiting the scope of the query to the specified graph, which may be an RDF::URI or RDF::Query::Variable.

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:



115
116
117
118
119
# File 'lib/sparql/algebra/operator/graph.rb', line 115

def execute(queryable, **options, &block)
  debug(options) {"Graph #{operands.first}"}
  graph_name, query = operands.first, operands.last
  @solutions = queryable.query(query, graph_name: graph_name, **options, &block)
end

#rewrite(&block) ⇒ SPARQL::Algebra::Expression

Don’t do any more rewriting

Returns:



124
125
126
# File 'lib/sparql/algebra/operator/graph.rb', line 124

def rewrite(&block)
  self
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)


135
136
137
138
139
140
141
# File 'lib/sparql/algebra/operator/graph.rb', line 135

def to_sparql(top_level: true, **options)
  query = operands.last.to_sparql(top_level: false, **options)
  # Paths don't automatically get braces.
  query = "{\n#{query}\n}" unless query.start_with?('{')
  str = "GRAPH #{operands.first.to_sparql(**options)} " + query
  top_level ? Operator.to_sparql(str, **options) : str
end