You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Writer#write_statement now ensure that nodes are unique, and updates the node to have a new unique identifier if that identifier has already been written for a different node, taking into consider possible node duplication. Fixes#262.
Copy file name to clipboardExpand all lines: lib/rdf/writer.rb
+35-8
Original file line number
Diff line number
Diff line change
@@ -258,7 +258,7 @@ def to_sym
258
258
# @yieldreturn [void]
259
259
definitialize(output= $stdout,options={}, &block)
260
260
@output,@options=output,options.dup
261
-
@nodes,@node_id={},0
261
+
@nodes,@node_id,@node_id_map={},0,{}
262
262
263
263
ifblock_given?
264
264
write_prologue
@@ -409,16 +409,43 @@ def write_comment(text)
409
409
end
410
410
411
411
##
412
+
# Add a statement to the writer. This will check to ensure that the statement is complete (no nil terms) and is valid, if the `:validation` option is set.
413
+
#
414
+
# Additionally, it will de-duplicate BNode terms sharing a common identifier.
415
+
#
412
416
# @param [RDF::Statement] statement
413
417
# @return [self]
414
418
# @note logs error if attempting to write an invalid {RDF::Statement} or if canonicalizing a statement which cannot be canonicalized.
415
419
defwrite_statement(statement)
416
420
statement=statement.canonicalize!ifcanonicalize?
417
421
422
+
# Make sure BNodes in statement use unique identifiers
423
+
ifstatement.node?
424
+
terms=statement.to_quad.mapdo |term|
425
+
ifterm.is_a?(RDF::Node)
426
+
term=term.originalwhileterm.original
427
+
@nodes[term] ||= begin
428
+
# Account for duplicated nodes
429
+
@node_id_map[term.to_s] ||= term
430
+
if !@node_id_map[term.to_s].equal?(term)
431
+
# Rename node
432
+
term.make_unique!
433
+
@node_id_map[term.to_s]=term
434
+
end
435
+
end
436
+
else
437
+
term
438
+
end
439
+
end
440
+
statement=RDF::Statement.from(statement.to_quad)
441
+
end
442
+
418
443
ifstatement.incomplete?
419
444
log_error"Statement #{statement.inspect} is incomplete"
420
445
elsifvalidate? && statement.invalid?
421
446
log_error"Statement #{statement.inspect} is invalid"
0 commit comments