class YARSPG::Reader

A parser for YARS-PG.

Parses into RDF*, taking liberties with the meaning of a Property Graph.

Issues:

* Is it an error to parse data outside of a declared section? (warning?)
* Node lables are treated like types
* Node properties are treated like statements on that node
* Node string annotations are treated like statements with the predicate created as a document fragment.
* Edge labels and Edge ids are ignored.
* The `graph_name` of each edge is used only for properties and annotations, the edges themselves are in the default graph (per RDF* semantics).
* Node and Edge schemas are ignored.

Constants

PartialStatement
SECTION_ORDERS

Public Class Methods

new(input = nil, **options, &block) click to toggle source

Initializes a new reader instance.

This assumes that strings are interpreted as document-relative fragments.

@param [String, to_s] input @param [Hash{Symbol => Object}] options @option options [Hash] :prefixes (Hash.new)

the prefix mappings to use (for acessing intermediate parser productions)

@option options [#to_s] :base_uri (nil)

the base URI to use when resolving relative URIs (for acessing intermediate parser productions)

@option options [Boolean] :validate (false)

whether to validate the parsed statements and values. If not validating,
the parser will attempt to recover from errors.

@option options [Logger, write, <<] :logger

Record error/info/debug output

@return [YARSPG::Reader]

Calls superclass method
# File lib/yarspg/reader.rb, line 331
def initialize(input = nil, **options, &block)
  super do
    @options[:base_uri] = RDF::URI(base_uri || "")
    @options[:logger] = false unless @options.has_key?(:logger)
    log_debug("base IRI") {base_uri.inspect}

    if block_given?
      case block.arity
        when 0 then instance_eval(&block)
        else block.call(self)
      end
    end
  end
end

Public Instance Methods

each_statement(&block) click to toggle source

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

@yield [statement] @yieldparam [RDF::Statement] statement @return [void]

# File lib/yarspg/reader.rb, line 356
def each_statement(&block)
  if block_given?
    log_recover
    @callback = block

    begin
      parse(@input, :yarspg, YARSPG::Meta::RULES, **@options) do |context, *data|
        case context
        when :base_uri
          @options[:base_uri] = data.first
        when :statement
          loc = data.shift
          @callback.call(RDF::Statement.from(data))
        end
      end
    rescue EBNF::PEG::Parser::Error
      # Terminate loop if Errors found while parsing
    end

    if validate? && log_statistics[:error]
      raise RDF::ReaderError, "Errors found during processing"
    end
  end
  enum_for(:each_statement)
end
each_triple(&block) click to toggle source

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

@yield [subject, predicate, object] @yieldparam [RDF::Resource] subject @yieldparam [RDF::URI] predicate @yieldparam [RDF::Value] object @return [void]

# File lib/yarspg/reader.rb, line 390
def each_triple(&block)
  if block_given?
    each_statement do |statement|
      block.call(*statement.to_triple)
    end
  end
  enum_for(:each_triple)
end
emit_statements(subject, partials, graph_name) { |subject, predicate, object.subject, graph_name| ... } click to toggle source

Emit statements, accounting for lists

# File lib/yarspg/reader.rb, line 400
def emit_statements(subject, partials, graph_name)
  partials.each do |partial|
    if partial.object.list?
      yield(subject, partial.predicate, partial.object.subject, graph_name)
      partial.object.each_statement do |st|
        yield(st.subject, st.predicate, st.object, graph_name)
      end
    else
      yield(subject, partial.predicate, partial.object, graph_name)
    end
  end
end
inspect() click to toggle source
# File lib/yarspg/reader.rb, line 346
def inspect
  sprintf("#<%s:%#0x(%s)>", self.class.name, __id__, base_uri.to_s)
end