Class: RDF::N3::Reader

Inherits:
Reader
  • Object
show all
Includes:
EBNF::LL1::Parser, Terminals, Util::Logger
Defined in:
lib/rdf/n3/reader.rb

Overview

TODO:

A Notation-3/Turtle parser in Ruby

N3 Parser, based in librdf version of predictiveParser.py Separate pass to create branch_table from n3-selectors.n3

This implementation only supports quickVars at the document scope.

Non-distinguished blank node variables are created as part of reasoning.

  • Formulae as RDF::Query representations

  • Formula expansion similar to SPARQL Construct

Defined Under Namespace

Classes: Recovery, SyntaxError

Constant Summary

Constants included from Terminals

Terminals::ANON, Terminals::BASE, Terminals::BLANK_NODE_LABEL, Terminals::DECIMAL, Terminals::DOUBLE, Terminals::ECHAR, Terminals::ESCAPE_CHAR4, Terminals::ESCAPE_CHAR8, Terminals::EXPONENT, Terminals::INTEGER, Terminals::IPLSTART, Terminals::IRIREF, Terminals::IRI_RANGE, Terminals::LANGTAG, Terminals::PERCENT, Terminals::PLX, Terminals::PNAME_LN, Terminals::PNAME_NS, Terminals::PN_CHARS, Terminals::PN_CHARS_BASE, Terminals::PN_CHARS_BODY, Terminals::PN_CHARS_U, Terminals::PN_LOCAL, Terminals::PN_LOCAL_BODY, Terminals::PN_LOCAL_ESC, Terminals::PN_PREFIX, Terminals::PREFIX, Terminals::QUICK_VAR_NAME, Terminals::STRING_LITERAL_LONG_QUOTE, Terminals::STRING_LITERAL_LONG_SINGLE_QUOTE, Terminals::STRING_LITERAL_QUOTE, Terminals::STRING_LITERAL_SINGLE_QUOTE, Terminals::UCHAR, Terminals::U_CHARS1, Terminals::U_CHARS2, Terminals::WS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input = $stdin, **options) {|reader| ... } ⇒ reader

Initializes the N3 reader instance.

Parameters:

  • input (IO, File, String) (defaults to: $stdin)

    the input stream to read

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • :base_uri (#to_s) — default: nil

    the base URI to use when resolving relative URIs (not supported by all readers)

  • :validate (Boolean) — default: false

    whether to validate the parsed statements and values

  • :canonicalize (Boolean) — default: false

    whether to canonicalize parsed literals and URIs.

  • :prefixes (Hash) — default: Hash.new

    the prefix mappings to use (not supported by all readers)

  • :list_terms (Hash) — default: false

    represent collections as an ‘RDF::Term`, rather than an rdf:first/rest ladder.

Yields:

  • (reader)

    ‘self`

Yield Parameters:

  • reader (RDF::Reader)

Yield Returns:

  • (void)

    ignored

Raises:

  • (Error)

    Raises RDF::ReaderError if validating and an error is found



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/rdf/n3/reader.rb', line 83

def initialize(input = $stdin, **options, &block)
  super do
    @options = {
      anon_base:  "b0",
      whitespace:  WS,
      depth: 0,
    }.merge(@options)
    @prod_stack = []

    @formulae = []
    @formula_nodes = {}
    @label_uniquifier = "0"
    @bnodes = {}
    @bn_labler = @options[:anon_base].dup
    @bn_mapper = {}
    @variables = {}

    if options[:base_uri]
      progress("base_uri") { base_uri.inspect}
      namespace(nil, iri(base_uri.to_s.match?(%r{[#/]$}) ? base_uri : "#{base_uri}#"))
    end

    # Prepopulate operator namespaces unless validating
    unless validate?
      namespace(:rdf, RDF.to_uri)
      namespace(:rdfs, RDF::RDFS.to_uri)
      namespace(:xsd, RDF::XSD.to_uri)
      namespace(:crypto, RDF::N3::Crypto.to_uri)
      namespace(:list, RDF::N3::List.to_uri)
      namespace(:log, RDF::N3::Log.to_uri)
      namespace(:math, RDF::N3::Math.to_uri)
      namespace(:rei, RDF::N3::Rei.to_uri)
      #namespace(:string, RDF::N3::String.to_uri)
      namespace(:time, RDF::N3::Time.to_uri)
    end
    progress("validate") {validate?.inspect}
    progress("canonicalize") {canonicalize?.inspect}

    @lexer = EBNF::LL1::Lexer.new(input, self.class.patterns, **@options)

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

Instance Attribute Details

#formula_nodesHash{RDF::Node => RDF::Graph} (readonly)

All nodes allocated to formulae

Returns:



40
41
42
# File 'lib/rdf/n3/reader.rb', line 40

def formula_nodes
  @formula_nodes
end

#formulaeArray<RDF::Node> (readonly)

Nodes used as Formulae graph names

Returns:



35
36
37
# File 'lib/rdf/n3/reader.rb', line 35

def formulae
  @formulae
end

#variablesHash{Symbol => RDF::Node} (readonly)

Allocated variables by formula

Returns:



45
46
47
# File 'lib/rdf/n3/reader.rb', line 45

def variables
  @variables
end

Class Method Details

.optionsObject

N3 Reader options



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rdf/n3/reader.rb', line 50

def self.options
  super + [
    RDF::CLI::Option.new(
      symbol: :list_terms,
      datatype: TrueClass,
      default: true,
      control: :checkbox,
      on: ["--list-terms CONTEXT"],
      description: "Use native collections (lists), not first/rest ladder.")
  ]
end

Instance Method Details

#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:



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/rdf/n3/reader.rb', line 141

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

    begin
      while (@lexer.first rescue true)
        read_n3Doc
      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

#each_triple {|subject, predicate, object| ... } ⇒ void

This method returns an undefined value.

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

Yields:

  • (subject, predicate, object)

Yield Parameters:

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


169
170
171
172
173
174
175
176
# File 'lib/rdf/n3/reader.rb', line 169

def each_triple
  if block_given?
    each_statement do |statement|
      yield(*statement.to_triple)
    end
  end
  enum_for(:each_triple)
end

#inspectObject



132
133
134
# File 'lib/rdf/n3/reader.rb', line 132

def inspect
  sprintf("#<%s:%#0x(%s)>", self.class.name, __id__, base_uri.to_s)
end