Class: RDF::Normalize::Writer

Inherits:
RDF::NQuads::Writer
  • Object
show all
Defined in:
lib/rdf/normalize/writer.rb

Overview

A RDF Graph normalization serialiser.

Normalizes the enumerated statements into normal form in the form of N-Quads.

Author:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output = $stdout, **options) {|writer| ... } ⇒ Writer

Initializes the writer instance.

Parameters:

  • output (IO, File) (defaults to: $stdout)

    the output stream

  • options (Hash{Symbol => Object})

    any additional options

Yields:

  • (writer)

    self

  • (writer)

Yield Parameters:

  • writer (RDF::Writer)
  • writer (RDF::Writer)

Yield Returns:

  • (void)


26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rdf/normalize/writer.rb', line 26

def initialize(output = $stdout, **options, &block)
  super do
    @repo = RDF::Repository.new
    if block_given?
      case block.arity
        when 0 then instance_eval(&block)
        else block.call(self)
      end
    end
  end
end

Instance Attribute Details

#repoRDF::Repository

Returns Repository of statements to serialized.

Returns:

  • (RDF::Repository)

    Repository of statements to serialized



12
13
14
# File 'lib/rdf/normalize/writer.rb', line 12

def repo
  @repo
end

Instance Method Details

#insert_statements(enumerable) (protected)

This method returns an undefined value.

Insert an Enumerable

Parameters:



74
75
76
# File 'lib/rdf/normalize/writer.rb', line 74

def insert_statements(enumerable)
  @repo = enumerable
end

#write_epilogue

This method returns an undefined value.

Outputs the Graph representation of all stored triples.



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rdf/normalize/writer.rb', line 55

def write_epilogue
  RDF::Normalize.new(@repo, **@options).
    statements.
    reject(&:variable?).
    map {|s| format_statement(s)}.
    sort.
    each do |line|
      puts line
    end
  super
end

#write_quad(subject, predicate, object, graph_name)

This method returns an undefined value.

Adds statements to the repository to be serialized in epilogue.

Parameters:

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


47
48
49
# File 'lib/rdf/normalize/writer.rb', line 47

def write_quad(subject, predicate, object, graph_name)
  @repo.insert(RDF::Statement(subject, predicate, object, graph_name: graph_name))
end