Class: RDF::TriG::Reader

Inherits:
RDF::Turtle::Reader
  • Object
show all
Defined in:
lib/rdf/trig/reader.rb

Overview

A parser for the TriG

Leverages the Turtle reader

Instance Method Summary collapse

Instance Method Details

#add_statement(production, statement) ⇒ RDF::Statement

add a statement, object can be literal or URI or bnode

Parameters:

  • production (Symbol)
  • statement (RDF::Statement)

    the subject of the statement

Returns:

  • (RDF::Statement)

    Added statement

Raises:

  • (RDF::ReaderError)

    Checks parameter types and raises if they are incorrect if parsing mode is validate.



91
92
93
94
95
96
97
98
# File 'lib/rdf/trig/reader.rb', line 91

def add_statement(production, statement)
  error("Statement is invalid: #{statement.inspect.inspect}", production: produciton) if validate? && statement.invalid?
  statement.graph_name = @graph_name if @graph_name
  @callback.call(statement) if statement.subject &&
                               statement.predicate &&
                               statement.object &&
                               (validate? ? statement.valid? : true)
end

#each_quad {|subject, predicate, object, graph_name| ... } ⇒ void

This method returns an undefined value.

Iterates the given block for each RDF quad in the input.

Yields:

  • (subject, predicate, object, graph_name)

Yield Parameters:

  • subject (RDF::Resource)
  • predicate (RDF::URI)
  • object (RDF::Value)
  • graph_name (RDF::URI)


76
77
78
79
80
81
82
83
# File 'lib/rdf/trig/reader.rb', line 76

def each_quad(&block)
  if block_given?
    each_statement do |statement|
      block.call(*statement.to_quad)
    end
  end
  enum_for(:each_quad)
end

#each_statement {|statement| ... } ⇒ void

This method returns an undefined value.

Iterates the given block for each RDF statement in the input.

Yields:

  • (statement)

Yield Parameters:

  • statement (RDF::Statement)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rdf/trig/reader.rb', line 47

def each_statement(&block)
  if block_given?
    @recovering = false
    @callback = block

    begin
      while (@lexer.first rescue true)
        read_trigDoc
      end
    rescue EBNF::LL1::Lexer::Error, SyntaxError, EOFError, Recovery
      # Terminate loop if EOF found while recovering
    end

    if validate? && log_statistics[:error]
      raise RDF::ReaderError, "Errors found during processing"
    end
  end
  enum_for(:each_statement)
end