lib/sparql/algebra/operator/graph.rb in sparql-3.2.0 vs lib/sparql/algebra/operator/graph.rb in sparql-3.2.1

- old
+ new

@@ -5,31 +5,66 @@ # # This is a wrapper to add a `graph_name` to the query, or an array of statements. # # [58] GraphGraphPattern ::= 'GRAPH' VarOrIri GroupGraphPattern # - # @example SPARQL Grammar + # @example SPARQL Grammar (query) # PREFIX : <http://example/> # SELECT * { # GRAPH ?g { ?s ?p ?o } # } # # @example SSE # (prefix ((: <http://example/>)) # (graph ?g # (bgp (triple ?s ?p ?o)))) # - # @example of a query + # @example SPARQL Grammar (named set of statements) + # PREFIX : <http://example/> + # SELECT * { + # GRAPH :g { :s :p :o } + # } + # + # @example SSE (named set of statements) # (prefix ((: <http://example/>)) + # (graph :g + # (bgp (triple :s :p :o)))) + # + # @example 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 } } + # } + # + # @example SSE (syntax-graph-05.rq) + # (prefix ((: <http://example.org/>)) + # (join + # (bgp (triple :x :p :z)) # (graph ?g - # (bgp (triple ?s ?p ?o)))) + # (join + # (bgp (triple :x :b ?a)) + # (graph ?g2 + # (bgp (triple :x :p ?x))))))) # - # @example named set of statements - # (prefix ((: <http://example/>)) - # (graph :g - # ((triple :s :p :o)))) + # @example 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} + # } # + # @example 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 https://www.w3.org/TR/sparql11-query/#sparqlAlgebra class Graph < Operator::Binary include Query NAME = [:graph] @@ -86,9 +121,24 @@ ## # Don't do any more rewriting # @return [SPARQL::Algebra::Expression] `self` def rewrite(&block) self + end + + ## + # + # Returns a partial SPARQL grammar for this operator. + # + # @param [Boolean] top_level (true) + # Treat this as a top-level, generating SELECT ... WHERE {} + # @return [String] + 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 end # Graph end # Operator end; end # SPARQL::Algebra