Class: RDF::Turtle::Writer
- Inherits:
-
Writer
- Object
- Writer
- RDF::Turtle::Writer
- Includes:
- StreamingWriter, Util::Logger
- Defined in:
- lib/rdf/turtle/writer.rb
Overview
A Turtle serialiser
Note that the natural interface is to write a whole graph at a time. Writing statements or Triples will create a graph to add them to and then serialize the graph.
The writer will add prefix definitions, and use them for creating @prefix definitions, and minting QNames
Instance Attribute Summary collapse
-
#graph ⇒ Graph
Graph of statements serialized.
Class Method Summary collapse
-
.options ⇒ Object
Writer options.
Instance Method Summary collapse
-
#format_literal(literal, **options) ⇒ String
Returns the N-Triples representation of a literal.
-
#format_node(node, **options) ⇒ String
Returns the Turtle representation of a blank node.
-
#format_quotedTriple(statement, **options) ⇒ String
Returns an embedded triple.
-
#format_uri(uri, **options) ⇒ String
Returns the Turtle representation of a URI reference.
-
#get_pname(resource) ⇒ String?
Return a PName for the URI, or nil.
-
#initialize(output = $stdout, **options) {|writer| ... } ⇒ Writer
constructor
Initializes the Turtle writer instance.
-
#sort_properties(properties) ⇒ Array<String>
Take a hash from predicate uris to lists of values.
-
#write_epilogue ⇒ void
Outputs the Turtle representation of all stored triples.
-
#write_prologue ⇒ void
Write out declarations.
-
#write_triple(subject, predicate, object) ⇒ void
Adds a triple to be serialized.
Methods included from StreamingWriter
#stream_epilogue, #stream_prologue, #stream_statement
Constructor Details
#initialize(output = $stdout, **options) {|writer| ... } ⇒ Writer
Initializes the Turtle writer instance.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/rdf/turtle/writer.rb', line 126 def initialize(output = $stdout, **, &block) @graph = RDF::Graph.new @uri_to_pname = {} @uri_to_prefix = {} = {literal_shorthand: true}.merge() super do reset if block_given? case block.arity when 0 then instance_eval(&block) else block.call(self) end end end end |
Instance Attribute Details
#graph ⇒ Graph
Returns Graph of statements serialized.
64 65 66 |
# File 'lib/rdf/turtle/writer.rb', line 64 def graph @graph end |
Class Method Details
.options ⇒ Object
Writer options
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/rdf/turtle/writer.rb', line 69 def self. super + [ RDF::CLI::Option.new( symbol: :max_depth, datatype: Integer, on: ["--max-depth DEPTH"], description: "Maximum depth for recursively defining resources, defaults to 3.") {true}, RDF::CLI::Option.new( symbol: :stream, datatype: TrueClass, on: ["--stream"], description: "Do not attempt to optimize graph presentation, suitable for streaming large graphs.") {true}, RDF::CLI::Option.new( symbol: :default_namespace, datatype: RDF::URI, on: ["--default-namespace URI", :REQUIRED], description: "URI to use as default namespace, same as prefixes.") {|arg| RDF::URI(arg)}, RDF::CLI::Option.new( symbol: :literal_shorthand, datatype: FalseClass, on: ["--no-literal-shorthand"], description: "Do not attempt to use Literal shorthands fo numbers and boolean values.") {false}, ] end |
Instance Method Details
#format_literal(literal, **options) ⇒ String
Returns the N-Triples representation of a literal.
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/rdf/turtle/writer.rb', line 276 def format_literal(literal, **) case literal when RDF::Literal case @options[:literal_shorthand] && literal.valid? ? literal.datatype : false when RDF::XSD.boolean %w(true false).include?(literal.value) ? literal.value : literal.canonicalize.to_s when RDF::XSD.integer literal.value.match?(/^[\+\-]?\d+$/) && !canonicalize? ? literal.value : literal.canonicalize.to_s when RDF::XSD.decimal literal.value.match?(/^[\+\-]?\d+\.\d+?$/) && !canonicalize? ? literal.value : literal.canonicalize.to_s when RDF::XSD.double in_form = case literal.value when /[\+\-]?\d+\.\d*E[\+\-]?\d+$/i then true when /[\+\-]?\.\d+E[\+\-]?\d+$/i then true when /[\+\-]?\d+E[\+\-]?\d+$/i then true else false end && !canonicalize? in_form ? literal.value : literal.canonicalize.to_s.sub('E', 'e').to_s else text = quoted(literal.value) text << "@#{literal.language}" if literal.language? text << "--#{literal.direction}" if literal.direction? text << "^^#{format_uri(literal.datatype)}" if literal.datatype? text end else quoted(literal.to_s) end end |
#format_node(node, **options) ⇒ String
Returns the Turtle representation of a blank node.
327 328 329 |
# File 'lib/rdf/turtle/writer.rb', line 327 def format_node(node, **) [:unique_bnodes] ? node.to_unique_base : node.to_base end |
#format_quotedTriple(statement, **options) ⇒ String
Returns an embedded triple.
337 338 339 340 |
# File 'lib/rdf/turtle/writer.rb', line 337 def format_quotedTriple(statement, **) log_debug("rdfstar") {"#{statement.to_ntriples}"} "<<%s %s %s>>" % statement.to_a.map { |value| format_term(value, **) } end |
#format_uri(uri, **options) ⇒ String
Returns the Turtle representation of a URI reference.
315 316 317 318 319 |
# File 'lib/rdf/turtle/writer.rb', line 315 def format_uri(uri, **) md = uri.relativize(base_uri) log_debug("relativize") {"#{uri.to_ntriples} => #{md.inspect}"} if md != uri.to_s md != uri.to_s ? "<#{md}>" : (get_pname(uri) || "<#{uri}>") end |
#get_pname(resource) ⇒ String?
Return a PName for the URI, or nil. Adds namespace of PName to defined prefixes
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/rdf/turtle/writer.rb', line 207 def get_pname(resource) case resource when RDF::Node return [:unique_bnodes] ? resource.to_unique_base : resource.to_base when RDF::URI uri = resource.to_s else return nil end pname = case when @uri_to_pname.key?(uri) return @uri_to_pname[uri] when u = @uri_to_prefix.keys.sort_by {|uu| uu.length}.reverse.detect {|uu| uri.index(uu.to_s) == 0} # Use a defined prefix prefix = @uri_to_prefix[u] unless u.to_s.empty? prefix(prefix, u) unless u.to_s.empty? log_debug("get_pname") {"add prefix #{prefix.inspect} => #{u}"} # Escape suffix, as necessary RDF::URI(uri).pname(prefixes: {prefix => u}) end when @options[:standard_prefixes] && vocab = RDF::Vocabulary.each.to_a.detect {|v| uri.index(v.to_uri.to_s) == 0} prefix = vocab.__name__.to_s.split('::').last.downcase @uri_to_prefix[vocab.to_uri.to_s] = prefix prefix(prefix, vocab.to_uri) # Define for output log_debug("get_pname") {"add standard prefix #{prefix.inspect} => #{vocab.to_uri}"} RDF::URI(uri).pname(prefixes: {prefix => vocab.to_uri}) else nil end # Make sure pname is a valid pname if pname md = Terminals::PNAME_LN.match(pname) || Terminals::PNAME_NS.match(pname) pname = nil unless md.to_s.length == pname.length end @uri_to_pname[uri] = pname end |
#sort_properties(properties) ⇒ Array<String>
Take a hash from predicate uris to lists of values. Sort the lists of values. Return a sorted list of properties.
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/rdf/turtle/writer.rb', line 252 def sort_properties(properties) # Make sorted list of properties prop_list = [] predicate_order.each do |prop| next unless properties[prop.to_s] prop_list << prop.to_s end properties.keys.sort.each do |prop| next if prop_list.include?(prop.to_s) prop_list << prop.to_s end log_debug("sort_properties") {prop_list.join(', ')} prop_list end |
#write_epilogue ⇒ void
This method returns an undefined value.
Outputs the Turtle representation of all stored triples.
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/rdf/turtle/writer.rb', line 174 def write_epilogue case when @options[:stream] stream_epilogue else @max_depth = @options[:max_depth] || 3 self.reset log_debug("\nserialize") {"graph: #{@graph.size}"} preprocess start_document # Remove lists that are referenced and have non-list properties; # these are legal, but can't be serialized as lists @lists.reject! do |node, list| ref_count(node) > 0 && prop_count(node) > 0 end order_subjects.each do |subject| unless is_done?(subject) statement(subject) end end end super end |
#write_prologue ⇒ void
This method returns an undefined value.
Write out declarations
160 161 162 163 164 165 166 167 |
# File 'lib/rdf/turtle/writer.rb', line 160 def write_prologue case when @options[:stream] stream_prologue else end super end |
#write_triple(subject, predicate, object) ⇒ void
This method returns an undefined value.
Adds a triple to be serialized
148 149 150 151 152 153 154 155 |
# File 'lib/rdf/turtle/writer.rb', line 148 def write_triple(subject, predicate, object) statement = RDF::Statement.new(subject, predicate, object) if @options[:stream] stream_statement(statement) else @graph.insert(statement) end end |