Module: SPARQL::Grammar

Defined in:
vendor/bundler/ruby/2.5.0/bundler/gems/sparql-7b34b4cf091a/lib/sparql/grammar.rb,
vendor/bundler/ruby/2.5.0/bundler/gems/sparql-7b34b4cf091a/lib/sparql/grammar/terminals11.rb,
vendor/bundler/ruby/2.5.0/bundler/gems/sparql-7b34b4cf091a/lib/sparql/grammar/parser11.rb

Overview

A SPARQL grammar for RDF.rb.

Representation

The parser natively generates native SPARQL S-Expressions (SSE), a hierarch of SPARQL::Algebra::Operator instances which can be executed against a queryable object, such as a Repository identically to RDF::Query.

Other elements within the hierarchy are generated using RDF objects, such as RDF::URI, RDF::Node, RDF::Literal, and RDF::Query.

See Parser for a full listing of algebra operations and RDF objects generated by the parser.

The native SSE representation may be serialized to a textual representation of SSE as serialized general S-Expressions (SXP). The SXP generated closely follows that of OpenJena ARQ, which is intended principally for running the SPARQL rules. Additionally, SSE is generated for CONSTRUCT, ASK, DESCRIBE and FROM operators.

SXP is generated by serializing the parser result as follows:

sse = SPARQL::Grammar.parse("SELECT * WHERE { ?s ?p ?o }")
sxp = sse.to_sxp

The following examples illustrate SPARQL transformations:

SPARQL:

SELECT * WHERE { ?a ?b ?c }

SXP:

(bgp (triple ?a ?b ?c))

SPARQL:

SELECT * FROM <a> WHERE { ?a ?b ?c }

SXP:

(dataset (<a>) (bgp (triple ?a ?b ?c)))

SPARQL:

SELECT * FROM NAMED <a> WHERE { ?a ?b ?c }

SXP:

(dataset ((named <a>)) (bgp (triple ?a ?b ?c)))

SPARQL:

SELECT DISTINCT * WHERE {?a ?b ?c}

SXP:

(distinct (bgp (triple ?a ?b ?c)))

SPARQL:

SELECT ?a ?b WHERE {?a ?b ?c}

SXP:

(project (?a ?b) (bgp (triple ?a ?b ?c)))

SPARQL:

CONSTRUCT {?a ?b ?c} WHERE {?a ?b ?c FILTER (?a)}

SXP:

(construct ((triple ?a ?b ?c)) (filter ?a (bgp (triple ?a ?b ?c))))

SPARQL:

SELECT * WHERE {<a> <b> <c> OPTIONAL {<d> <e> <f>}}

SXP:

(leftjoin (bgp (triple <a> <b> <c>)) (bgp (triple <d> <e> <f>)))

SPARQL:

SELECT * WHERE {<a> <b> <c> {<d> <e> <f>}}

SXP:

(join (bgp (triple <a> <b> <c>)) (bgp (triple <d> <e> <f>)))

SPARQL:

PREFIX : <http://example/> 

SELECT * 
{ 
   { ?s ?p ?o }
  UNION
   { GRAPH ?g { ?s ?p ?o } }
}

SXP:

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

SPARQL:

LOAD <etc/doap.ttl>

SXP:

(update (load <etc/doap.ttl>))

SPARQL:

PREFIX     : <http://example.org/> 

INSERT {
        ?s ?p "q"
}
USING :g1
USING :g2
WHERE {
        ?s ?p ?o
}

SXP:

(prefix ((: <http://example.org/>))
  (update
    (modify
      (using (:g1 :g2)
        (bgp (triple ?s ?p ?o)))
      (insert ((triple ?s ?p "q"))))))

Implementation Notes

The parser is driven through a rules table contained in lib/sparql/grammar/meta.rb. This includes branch rules to indicate productions to be taken based on a current production.

The meta.rb file is generated from etc/sparql11.bnf using the ebnf gem.

ebnf --ll1 Query --format rb \
  --mod-name SPARQL::Grammar::Meta \
  --output lib/sparql/grammar/meta.rb \
  etc/sparql11.bnf

Defined Under Namespace

Modules: Meta, Terminals Classes: Parser

Class Method Summary collapse

Class Method Details

.open(filename, options = {}) {|reader| ... } ⇒ Object

Parses input from the given file name or URL.

Parameters:

  • filename (String, #to_s)
  • options (Hash{Symbol => Object}) (defaults to: {})

    any additional options (see RDF::Reader#initialize and RDF::Format.for)

Options Hash (options):

  • :format (Symbol) — default: :ntriples

Yields:

  • (reader)

Yield Parameters:

  • reader (RDF::Reader)

Yield Returns:

  • (void)

    ignored

Raises:

  • (RDF::FormatError)

    if no reader found for the specified format



193
194
195
196
197
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-7b34b4cf091a/lib/sparql/grammar.rb', line 193

def self.open(filename, options = {}, &block)
  RDF::Util::File.open_file(filename, options) do |file|
    self.parse(file, options, &block)
  end
end

.parse(query, options = {}, &block) ⇒ Parser

Parse the given SPARQL query string.

Examples:

result = SPARQL::Grammar.parse("SELECT * WHERE { ?s ?p ?o }")

Parameters:

  • query (IO, StringIO, Lexer, Array, String, #to_s)

    Query may be an array of lexed tokens, a lexer, or a string or open file.

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

Returns:

Raises:

  • (Parser::Error)

    on invalid input



178
179
180
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-7b34b4cf091a/lib/sparql/grammar.rb', line 178

def self.parse(query, options = {}, &block)
  Parser.new(query, options).parse(options[:update] ? :UpdateUnit : :QueryUnit)
end

.tokenize(query, options = {}) {|lexer| ... } ⇒ Lexer

Tokenizes the given SPARQL query string.

Examples:

lexer = SPARQL::Grammar.tokenize("SELECT * WHERE { ?s ?p ?o }")
lexer.each_token do |token|
  puts token.inspect
end

Parameters:

Yields:

  • (lexer)

Yield Parameters:

  • lexer (Lexer)

Returns:

  • (Lexer)

Raises:

  • (Lexer::Error)

    on invalid input



228
229
230
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-7b34b4cf091a/lib/sparql/grammar.rb', line 228

def self.tokenize(query, options = {}, &block)
  Lexer.tokenize(query, options, &block)
end

.valid?(query, options = {}) ⇒ Boolean

Returns true if the given SPARQL query string is valid.

Examples:

SPARQL::Grammar.valid?("SELECT ?s WHERE { ?s ?p ?o }")  #=> true
SPARQL::Grammar.valid?("SELECT s WHERE { ?s ?p ?o }")   #=> false

Parameters:

Returns:

  • (Boolean)


209
210
211
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-7b34b4cf091a/lib/sparql/grammar.rb', line 209

def self.valid?(query, options = {})
  Parser.new(query, options).valid?
end