Class: RDF::N3::Reader

Inherits:
Reader show all
Includes:
Meta, Parser, Util::Logger
Defined in:
vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/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 uses distinguished variables for both universal and explicit existential variables (defined with @forSome). Variables created from blank nodes are non-distinguished. Distinguished existential variables are tracked using $, internally, as the RDF query_pattern logic looses details of the variable definition in solutions, where the variable is represented using a symbol.

  • Formulae as RDF::Query representations
  • Formula expansion similar to SPARQL Construct

Constant Summary collapse

N3_KEYWORDS =
%w(a is of has keywords prefix base true false forSome forAny)

Constants included from Parser

Parser::NOT_NAME_CHARS, Parser::NOT_QNAME_CHARS, Parser::R_MLSTRING, Parser::R_WHITESPACE, Parser::SINGLE_CHARACTER_SELECTORS, Parser::START

Constants included from Meta

Meta::BARENAME_START, Meta::BARENAME_TAIL, Meta::BRANCHES, Meta::REGEXPS

Constants included from Util::Logger

Util::Logger::IOWrapper

Instance Attribute Summary collapse

Attributes inherited from Reader

#options

Attributes included from Enumerable

#existentials, #universals

Instance Method Summary collapse

Methods included from Parser

#abbr, #buffer, #consume, #depth, #dump_stack, #error, #get_token, #parse, #readline, #test, #token, #whitespace

Methods included from Util::Logger

#log_debug, #log_depth, #log_error, #log_fatal, #log_info, #log_recover, #log_recovering?, #log_statistics, #log_warn, #logger

Methods inherited from Reader

#base_uri, #canonicalize?, #close, each, #encoding, #fail_object, #fail_predicate, #fail_subject, for, format, #intern?, #lineno, open, options, #prefix, #prefixes, #prefixes=, #read_statement, #read_triple, #rewind, #to_sym, to_sym, #valid?, #validate?

Methods included from Util::Aliasing::LateBound

#alias_method

Methods included from Enumerable

add_entailment, #contain?, #dump, #each_graph, #each_object, #each_predicate, #each_quad, #each_subject, #each_term, #entail, #enum_graph, #enum_object, #enum_predicate, #enum_quad, #enum_statement, #enum_subject, #enum_term, #enum_triple, #graph_names, #has_graph?, #has_object?, #has_predicate?, #has_quad?, #has_statement?, #has_subject?, #has_term?, #has_triple?, #invalid?, #method_missing, #objects, #predicates, #project_graph, #quads, #respond_to_missing?, #statements, #subjects, #supports?, #terms, #to_a, #to_h, #to_set, #triples, #valid?, #validate!

Methods included from Countable

#count, #empty?

Methods included from Isomorphic

#bijection_to, #isomorphic_with?

Methods included from Readable

#readable?

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) (defaults to: {})

    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

  • :intern (Boolean) — default: true

    whether to intern all parsed URIs

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

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

Yields:

  • (reader)

    self

Yield Parameters:

Yield Returns:

  • (void)

    ignored

Raises:

  • (Error)

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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 53

def initialize(input = $stdin, options = {}, &block)
  super do
    input.rewind if input.respond_to?(:rewind)
    @input = input.respond_to?(:read) ? input : StringIO.new(input.to_s)
    @lineno = 0
    readline  # Prime the pump

    @memo = {}
    @keyword_mode = false
    @keywords = %w(a is of this has).map(&:freeze).freeze
    @productions = []
    @prod_data = []

    @branches = BRANCHES # Get from meta class
    @regexps = REGEXPS # Get from meta class

    @formulae = []      # Nodes used as Formulae graph names
    @formulae_nodes = {}
    @label_uniquifier ||= "#{Random.new_seed}_000000"
    @bnodes = {}  # allocated bnodes by formula
    @variables = {}  # allocated variables by formula

    if options[:base_uri]
      log_info("@uri") { base_uri.inspect}
      namespace(nil, uri("#{base_uri}#"))
    end

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

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RDF::Enumerable

Instance Attribute Details

#formulaeArray<RDF::Node> (readonly)

The Blank nodes allocated for formula

Returns:



30
31
32
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 30

def formulae
  @formulae
end

Instance Method Details

#booleanToken(prod, tok) ⇒ Object (protected)



172
173
174
175
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 172

def booleanToken(prod, tok)
  lit = RDF::Literal.new(tok.delete("@"), datatype: RDF::XSD.boolean, validate: validate?, canonicalize: canonicalize?)
  add_prod_data(:literal, lit)
end

#declarationFinishObject (protected)



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 194

def declarationFinish
  decl = @prod_data.pop
  case decl[:prod]
  when "@prefix"
    uri = process_uri(decl[:explicituri])
    namespace(decl[:prefix], uri)
  when "@base"
    # Base, set or update document URI
    uri = decl[:explicituri]
    options[:base_uri] = process_uri(uri)

    # The empty prefix "" is by default , bound to "#" -- the local namespace of the file.
    # The parser behaves as though there were a
    #   @prefix : <#>.
    # just before the file.
    # This means that <#foo> can be written :foo and using @keywords one can reduce that to foo.

    namespace(nil, uri.match(/[\/\#]$/) ? base_uri : process_uri("#{uri}#"))
    log_debug("declarationFinish[@base]", depth: depth) {"@base=#{base_uri}"}
  when "@keywords"
    log_debug("declarationFinish[@keywords]", depth: depth) {@keywords.inspect}
    # Keywords are handled in tokenizer and maintained in @keywords array
    if (@keywords & N3_KEYWORDS) != @keywords
      error("Undefined keywords used: #{(@keywords - N3_KEYWORDS).to_sentence}") if validate?
    end
    @userkeys = true
  else
    error("declarationFinish: FIXME #{decl.inspect}")
  end
end

#declarationStart(prod) ⇒ Object (protected)



177
178
179
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 177

def declarationStart(prod)
  @prod_data << {}
end

#declarationToken(prod, tok) ⇒ Object (protected)



181
182
183
184
185
186
187
188
189
190
191
192
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 181

def declarationToken(prod, tok)
  case prod
  when "@prefix", "@base", "@keywords"
    add_prod_data(:prod, prod)
  when "prefix"
    add_prod_data(:prefix, tok[0..-2])
  when "explicituri"
    add_prod_data(:explicituri, tok[1..-2])
  else
    add_prod_data(prod.to_sym, tok)
  end
end

#documentStart(prod) ⇒ Object (protected)

Document start, instantiate



226
227
228
229
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 226

def documentStart(prod)
  @formulae.push(nil)
  @prod_data << {}
end

#dtlangToken(prod, tok) ⇒ Object (protected)



231
232
233
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 231

def dtlangToken(prod, tok)
  add_prod_data(:langcode, tok) if prod == "langcode"
end

#each_statement {|statement| ... }

This method returns an undefined value.

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

Yields:

  • (statement)

Yield Parameters:



112
113
114
115
116
117
118
119
120
121
122
123
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 112

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

    parse(START.to_sym)

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

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

This method returns an undefined value.

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

Yields:

  • (subject, predicate, object)

Yield Parameters:



133
134
135
136
137
138
139
140
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 133

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

#existentialFinishObject (protected)

Apart from the set of statements, a formula also has a set of URIs of symbols which are universally quantified, and a set of URIs of symbols which are existentially quantified. Variables are then in general symbols which have been quantified.

Here we allocate a variable (making up a name) and record with the defining formula. Quantification is done when the formula is completed against all in-scope variables



245
246
247
248
249
250
251
252
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 245

def existentialFinish
  pd = @prod_data.pop
  forSome = Array(pd[:symbol])
  forSome.each do |term|
    var = univar(term, existential: true)
    add_var_to_formula(@formulae.last, term, var)
  end
end

#existentialStart(prod) ⇒ Object (protected)



235
236
237
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 235

def existentialStart(prod)
  @prod_data << {}
end

#expressionFinishObject (protected)

Process path items, and push on the last object for parent processing



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 259

def expressionFinish
  expression = @prod_data.pop

  # If we're in teh middle of a pathtail, append
  if @prod_data.last[:pathtail] && expression[:pathitem] && expression[:pathtail]
    path_list = [expression[:pathitem]] + expression[:pathtail]
    log_debug("expressionFinish(pathtail)", depth: depth) {"set pathtail to #{path_list.inspect}"}
    @prod_data.last[:pathtail] = path_list

    dir_list = [expression[:direction]] if expression[:direction]
    dir_list += expression[:directiontail] if expression[:directiontail]
    @prod_data.last[:directiontail] = dir_list if dir_list
  elsif expression[:pathitem] && expression[:pathtail]
    add_prod_data(:expression, process_path(expression))
  elsif expression[:pathitem]
    add_prod_data(:expression, expression[:pathitem])
  else
    error("expressionFinish: FIXME #{expression.inspect}")
  end
end

#expressionStart(prod) ⇒ Object (protected)



254
255
256
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 254

def expressionStart(prod)
  @prod_data << {}
end

#inspectObject



103
104
105
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 103

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

#literalFinishObject (protected)



289
290
291
292
293
294
295
296
297
298
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 289

def literalFinish
  lit = @prod_data.pop
  content = RDF::NTriples.unescape(lit[:string])
  language = lit[:langcode] if lit[:langcode]
  language = language.downcase if language && canonicalize?
  datatype = lit[:symbol]

  lit = RDF::Literal.new(content, language: language, datatype: datatype, validate: validate?, canonicalize: canonicalize?)
  add_prod_data(:literal, lit)
end

#literalStart(prod) ⇒ Object (protected)



280
281
282
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 280

def literalStart(prod)
  @prod_data << {}
end

#literalToken(prod, tok) ⇒ Object (protected)



284
285
286
287
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 284

def literalToken(prod, tok)
  tok = tok[0, 3] == '"""' ? tok[3..-4] : tok[1..-2]
  add_prod_data(:string, tok)
end

#objectFinishObject (protected)



304
305
306
307
308
309
310
311
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 304

def objectFinish
  object = @prod_data.pop
  if object[:expression]
    add_prod_data(:object, object[:expression])
  else
    error("objectFinish: FIXME #{object.inspect}")
  end
end

#objectStart(prod) ⇒ Object (protected)



300
301
302
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 300

def objectStart(prod)
  @prod_data << {}
end

#onFinishObject (protected)

End of production



153
154
155
156
157
158
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 153

def onFinish
  prod = @productions.pop()
  handler = "#{prod}Finish".to_sym
  log_info("#{handler}(#{respond_to?(handler, true)})", depth: depth) {"#{prod}: #{@prod_data.last.inspect}"}
  send(handler) if respond_to?(handler, true)
end

#onStart(prod) ⇒ Object (protected)

Start of production



144
145
146
147
148
149
150
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 144

def onStart(prod)
  handler = "#{prod}Start".to_sym
  log_info("#{handler}(#{respond_to?(handler, true)})", prod, depth: depth)
  @productions << prod
  send(handler, prod) if respond_to?(handler, true)

end

#onToken(prod, tok) ⇒ Object (protected)

Process of a token



161
162
163
164
165
166
167
168
169
170
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 161

def onToken(prod, tok)
  unless @productions.empty?
    parentProd = @productions.last
    handler = "#{parentProd}Token".to_sym
    log_info("#{handler}(#{respond_to?(handler, true)})", depth: depth) {"#{prod}, #{tok}: #{@prod_data.last.inspect}"}
    send(handler, prod, tok) if respond_to?(handler, true)
  else
    error("Token has no parent production")
  end
end

#pathitemFinishObject (protected)



369
370
371
372
373
374
375
376
377
378
379
380
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 369

def pathitemFinish
  pathitem = @prod_data.pop
  if pathitem[:pathlist]
    error("pathitemFinish(pathlist): FIXME #{pathitem.inspect}")
  elsif pathitem[:propertylist]
    error("pathitemFinish(propertylist): FIXME #{pathitem.inspect}")
  elsif pathitem[:symbol] || pathitem[:literal]
    add_prod_data(:pathitem, pathitem[:symbol] || pathitem[:literal])
  else
    error("pathitemFinish: FIXME #{pathitem.inspect}")
  end
end

#pathitemStart(prod) ⇒ Object (protected)



313
314
315
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 313

def pathitemStart(prod)
  @prod_data << {}
end

#pathitemToken(prod, tok) ⇒ Object (protected)



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 317

def pathitemToken(prod, tok)
  case prod
  when "numericliteral"
    nl = RDF::NTriples.unescape(tok)
    datatype = case nl
    when /e/i then RDF::XSD.double
    when /\./ then RDF::XSD.decimal
    else RDF::XSD.integer
    end

    lit = RDF::Literal.new(nl, datatype: datatype, validate: validate?, canonicalize: canonicalize?)
    add_prod_data(:literal, lit)
  when "quickvariable"
    # There is a also a shorthand syntax ?x which is the same as :x except that it implies that x is
    # universally quantified not in the formula but in its parent formula
    uri = process_qname(tok.sub('?', ':'))
    var = uri.variable? ? uri : univar(uri)
    add_var_to_formula(@formulae[-2], uri, var)
    # Also add var to this formula
    add_var_to_formula(@formulae.last, uri, var)

    add_prod_data(:symbol, var)
  when "boolean"
    lit = RDF::Literal.new(tok.delete("@"), datatype: RDF::XSD.boolean, validate: validate?, canonicalize: canonicalize?)
    add_prod_data(:literal, lit)
  when "[", "("
    # Push on state for content of blank node
    @prod_data << {}
  when "]", ")"
    # Construct
    symbol = process_anonnode(@prod_data.pop)
    add_prod_data(:symbol, symbol)
  when "{"
    # A new formula, push on a node as a named graph
    node = RDF::Node.new(".form_#{unique_label}")
    @formulae << node
    @formulae_nodes[node] = true

    # Promote variables defined on the earlier formula to this formula
    @variables[node] = {}
    @variables[@formulae[-2]].each do |name, var|
      @variables[node][name] = var
    end
  when "}"
    # Pop off the formula
    formula = @formulae.pop
    add_prod_data(:symbol, formula)
  else
    error("pathitemToken(#{prod}, #{tok}): FIXME")
  end
end

#pathlistFinishObject (protected)



386
387
388
389
390
391
392
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 386

def pathlistFinish
  pathlist = @prod_data.pop
  # Flatten propertylist into an array
  expr = @prod_data.last.delete(:expression)
  add_prod_data(:pathlist, expr) if expr
  add_prod_data(:pathlist, pathlist[:pathlist]) if pathlist[:pathlist]
end

#pathlistStart(prod) ⇒ Object (protected)



382
383
384
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 382

def pathlistStart(prod)
  @prod_data << {pathlist: []}
end

#pathtailFinishObject (protected)



407
408
409
410
411
412
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 407

def pathtailFinish
  pathtail = @prod_data.pop
  add_prod_data(:pathtail, pathtail[:pathtail])
  add_prod_data(:direction, pathtail[:direction]) if pathtail[:direction]
  add_prod_data(:directiontail, pathtail[:directiontail]) if pathtail[:directiontail]
end

#pathtailStart(prod) ⇒ Object (protected)



394
395
396
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 394

def pathtailStart(prod)
  @prod_data << {pathtail: []}
end

#pathtailToken(prod, tok) ⇒ Object (protected)



398
399
400
401
402
403
404
405
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 398

def pathtailToken(prod, tok)
  case tok
  when "!", "."
    add_prod_data(:direction, :forward)
  when "^"
    add_prod_data(:direction, :reverse)
  end
end

#propertylistFinishObject (protected)



418
419
420
421
422
423
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 418

def propertylistFinish
  propertylist = @prod_data.pop
  # Flatten propertylist into an array
  ary = [propertylist, propertylist.delete(:propertylist)].flatten.compact
  @prod_data.last[:propertylist] = ary
end

#propertylistStart(prod) ⇒ Object (protected)



414
415
416
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 414

def propertylistStart(prod)
  @prod_data << {}
end

#simpleStatementFinishObject (protected)

Completion of Simple Statement, all productions include :subject, and :propertyList



430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 430

def simpleStatementFinish
  statement = @prod_data.pop

  subject = statement[:subject]
  properties = Array(statement[:propertylist])
  properties.each do |p|
    predicate = p[:verb]
    next unless predicate
    log_debug("simpleStatementFinish(pred)", depth: depth) {predicate.to_s}
    error(%(Illegal statment: "#{predicate}" missing object)) unless p.has_key?(:object)
    objects = Array(p[:object])
    objects.each do |object|
      if p[:invert]
        add_statement("simpleStatementFinish", object, predicate, subject)
      else
        add_statement("simpleStatementFinish", subject, predicate, object)
      end
    end
  end
end

#simpleStatementStart(prod) ⇒ Object (protected)



425
426
427
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 425

def simpleStatementStart(prod)
  @prod_data << {}
end

#subjectFinishObject (protected)



455
456
457
458
459
460
461
462
463
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 455

def subjectFinish
  subject = @prod_data.pop

  if subject[:expression]
    add_prod_data(:subject, subject[:expression])
  else
    error("unknown expression type")
  end
end

#subjectStart(prod) ⇒ Object (protected)



451
452
453
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 451

def subjectStart(prod)
  @prod_data << {}
end

#symbolToken(prod, tok) ⇒ Object (protected)



465
466
467
468
469
470
471
472
473
474
475
476
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 465

def symbolToken(prod, tok)
  term = case prod
  when 'explicituri'
    process_uri(tok[1..-2])
  when 'qname'
    process_qname(tok)
  else
    error("symbolToken(#{prod}, #{tok}): FIXME #{term.inspect}")
  end

  add_prod_data(:symbol, term)
end

#universalFinishObject (protected)

Apart from the set of statements, a formula also has a set of URIs of symbols which are universally quantified, and a set of URIs of symbols which are existentially quantified. Variables are then in general symbols which have been quantified.

Here we allocate a variable (making up a name) and record with the defining formula. Quantification is done when the formula is completed against all in-scope variables



488
489
490
491
492
493
494
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 488

def universalFinish
  pd = @prod_data.pop
  forAll = Array(pd[:symbol])
  forAll.each do |term|
    add_var_to_formula(@formulae.last, term, univar(term))
  end
end

#universalStart(prod) ⇒ Object (protected)



478
479
480
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 478

def universalStart(prod)
  @prod_data << {}
end

#verbFinishObject (protected)



522
523
524
525
526
527
528
529
530
531
532
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 522

def verbFinish
  verb = @prod_data.pop
  if verb[:expression]
    error("Literal may not be used as a predicate") if verb[:expression].is_a?(RDF::Literal)
    error("Formula may not be used as a peredicate") if @formulae_nodes.has_key?(verb[:expression])
    add_prod_data(:verb, verb[:expression])
    add_prod_data(:invert, true) if verb[:invert]
  else
    error("verbFinish: FIXME #{verb.inspect}")
  end
end

#verbStart(prod) ⇒ Object (protected)



496
497
498
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 496

def verbStart(prod)
  @prod_data << {}
end

#verbToken(prod, tok) ⇒ Object (protected)



500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/reader.rb', line 500

def verbToken(prod, tok)
  term = case prod
  when '<='
    add_prod_data(:expression, RDF::N3::Log.implies)
    add_prod_data(:invert, true)
  when '=>'
    add_prod_data(:expression, RDF::N3::Log.implies)
  when '='
    add_prod_data(:expression, RDF::OWL.sameAs)
  when '@a'
    add_prod_data(:expression, RDF.type)
  when '@has', "@of"
    # Syntactic sugar
  when '@is'
    add_prod_data(:invert, true)
  else
    error("verbToken(#{prod}, #{tok}): FIXME #{term.inspect}")
  end

  add_prod_data(:symbol, term)
end