Class: RDF::TriG::Writer

Inherits:
RDF::Turtle::Writer show all
Includes:
StreamingWriter
Defined in:
vendor/bundler/ruby/2.5.0/bundler/gems/rdf-trig-7745d44512d1/lib/rdf/trig/writer.rb

Overview

A TriG serialiser

Note that the natural interface is to write a whole repository at a time. Writing statements or Triples will create a repository to add them to and then serialize the repository.

The writer will add prefix definitions, and use them for creating @prefix definitions, and minting QNames

Examples:

Obtaining a TriG writer class

RDF::Writer.for(:trig)         #=> RDF::TriG::Writer
RDF::Writer.for("etc/test.trig")
RDF::Writer.for(:file_name      => "etc/test.trig")
RDF::Writer.for(file_extension: "trig")
RDF::Writer.for(:content_type   => "application/trig")

Serializing RDF repo into an TriG file

RDF::TriG::Writer.open("etc/test.trig") do |writer|
  writer << repo
end

Serializing RDF statements into an TriG file

RDF::TriG::Writer.open("etc/test.trig") do |writer|
  repo.each_statement do |statement|
    writer << statement
  end
end

Serializing RDF statements into an TriG string

RDF::TriG::Writer.buffer do |writer|
  repo.each_statement do |statement|
    writer << statement
  end
end

Serializing RDF statements to a string in streaming mode

RDF::TriG::Writer.buffer(stream: true) do |writer|
  repo.each_statement do |statement|
    writer << statement
  end
end

Creating @base and @prefix definitions in output

RDF::TriG::Writer.buffer(base_uri: "http://example.com/", prefixes: {
    nil => "http://example.com/ns#",
    foaf: "http://xmlns.com/foaf/0.1/"}
) do |writer|
  repo.each_statement do |statement|
    writer << statement
  end
end

Author:

Instance Attribute Summary

Attributes inherited from RDF::Turtle::Writer

#graph

Attributes inherited from Writer

#options

Instance Method Summary collapse

Methods included from StreamingWriter

#stream_epilogue, #stream_statement

Methods inherited from RDF::Turtle::Writer

#format_literal, #format_node, #format_uri, #get_pname, #indent, options, #order_subjects, #predicate_order, #preprocess, #quoted, #reset, #sort_properties, #start_document, #top_classes, #write_triple

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 included from RDF::Turtle::StreamingWriter

#stream_epilogue, #stream_prologue, #stream_statement

Methods inherited from Writer

#base_uri, buffer, #canonicalize?, dump, each, #encoding, #escaped, #flush, for, format, #format_list, #format_literal, #format_node, #format_term, #format_uri, #node_id, open, options, #prefix, #prefixes, #prefixes=, #puts, #quoted, #to_sym, to_sym, #uri_for, #validate?, #write_comment, #write_statement, #write_triple, #write_triples

Methods included from Util::Aliasing::LateBound

#alias_method

Methods included from Writable

#<<, #insert, #insert_graph, #insert_reader, #insert_statement, #insert_statements, #writable?

Constructor Details

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

Initializes the TriG writer instance.

Parameters:

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

    the output stream

  • options (Hash{Symbol => Object}) (defaults to: {})

    any additional options

Options Hash (options):

  • :encoding (Encoding) — default: Encoding::UTF_8

    the encoding to use on the output stream (Ruby 1.9+)

  • :canonicalize (Boolean) — default: false

    whether to canonicalize literals when serializing

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

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

  • :base_uri (#to_s) — default: nil

    the base URI to use when constructing relative URIs

  • :max_depth (Integer) — default: 3

    Maximum depth for recursively defining resources, defaults to 3

  • :standard_prefixes (Boolean) — default: false

    Add standard prefixes to @prefixes, if necessary.

  • :stream (Boolean) — default: false

    Do not attempt to optimize graph presentation, suitable for streaming large repositories.

  • :default_namespace (String) — default: nil

    URI to use as default namespace, same as prefixes\[nil\]

Yields:

  • (writer)

    self

  • (writer)

Yield Parameters:

Yield Returns:

  • (void)


90
91
92
93
94
95
96
97
98
99
100
101
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-trig-7745d44512d1/lib/rdf/trig/writer.rb', line 90

def initialize(output = $stdout, options = {}, &block)
  super do
    # Set both @repo and @graph to a new repository.
    @repo = @graph = RDF::Repository.new
    if block_given?
      case block.arity
        when 0 then instance_eval(&block)
        else block.call(self)
      end
    end
  end
end

Instance Method Details

#blankNodePropertyList?(subject) ⇒ Boolean (protected)

Add additional constraint that the resource must be in a single graph

Returns:

  • (Boolean)


183
184
185
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-trig-7745d44512d1/lib/rdf/trig/writer.rb', line 183

def blankNodePropertyList?(subject)
  super && resource_in_single_graph?(subject)
end

#order_graphsObject (protected)

Order graphs for output



199
200
201
202
203
204
205
206
207
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-trig-7745d44512d1/lib/rdf/trig/writer.rb', line 199

def order_graphs
  log_debug("order_graphs") {@repo.graph_names.to_a.inspect}
  graph_names = @repo.graph_names.to_a.sort
  
  # include default graph, if necessary
  graph_names.unshift(nil) unless @repo.query(graph_name: false).to_a.empty?
  
  graph_names
end

#p_squared?(resource, position) ⇒ Boolean (protected)

Add additional constraint that the resource must be in a single graph

Returns:

  • (Boolean)


188
189
190
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-trig-7745d44512d1/lib/rdf/trig/writer.rb', line 188

def p_squared?(resource, position)
  super && resource_in_single_graph?(resource)
end

#preprocess_statement(statement) ⇒ Object (protected)

Perform any statement preprocessing required. This is used to perform reference counts and determine required prefixes.

Parameters:



212
213
214
215
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-trig-7745d44512d1/lib/rdf/trig/writer.rb', line 212

def preprocess_statement(statement)
  super
  get_pname(statement.graph_name) if statement.has_graph?
end

#resource_in_single_graph?(resource) ⇒ Boolean (protected)

Returns:

  • (Boolean)


192
193
194
195
196
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-trig-7745d44512d1/lib/rdf/trig/writer.rb', line 192

def resource_in_single_graph?(resource)
  graph_names = @repo.query(subject: resource).map(&:graph_name)
  graph_names += @repo.query(object: resource).map(&:graph_name)
  graph_names.uniq.length <= 1
end

#write_epilogue

This method returns an undefined value.

Outputs the TriG representation of all stored triples.



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-trig-7745d44512d1/lib/rdf/trig/writer.rb', line 137

def write_epilogue
  case
  when @options[:stream]
    stream_epilogue
  else
    @max_depth = @options[:max_depth] || 3
    @base_uri = RDF::URI(@options[:base_uri])

    reset

    log_debug {"serialize: repo: #{@repo.size}"}

    preprocess
    start_document

    order_graphs.each do |graph_name|
      log_depth do
        log_debug {"graph_name: #{graph_name.inspect}"}
        reset
        @options[:log_depth] = graph_name ? 1 : 0

        if graph_name
          @output.write("\n#{format_term(graph_name)} {")
        end

        # Restrict view to the particular graph
        @graph = @repo.project_graph(graph_name)

        # Pre-process statements again, but in the specified graph
        @graph.each {|st| preprocess_statement(st)}
        order_subjects.each do |subject|
          unless is_done?(subject)
            statement(subject)
          end
        end

        @output.puts("}") if graph_name
      end
    end
  end
  raise RDF::WriterError, "Errors found during processing" if log_statistics[:error]
end

#write_prologue

This method returns an undefined value.

Write out declarations



123
124
125
126
127
128
129
130
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-trig-7745d44512d1/lib/rdf/trig/writer.rb', line 123

def write_prologue
  case
  when @options[:stream]
    stream_prologue
  else
    super
  end
end

#write_quad(subject, predicate, object, graph_name)

This method returns an undefined value.

Adds a triple to be serialized

Parameters:



111
112
113
114
115
116
117
118
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-trig-7745d44512d1/lib/rdf/trig/writer.rb', line 111

def write_quad(subject, predicate, object, graph_name)
  statement = RDF::Statement.new(subject, predicate, object, graph_name: graph_name)
  if @options[:stream]
    stream_statement(statement)
  else
    @graph.insert(statement)
  end
end