Skip to content

Commit

Permalink
Fix problems serializing list elements which are also the IRI subject…
Browse files Browse the repository at this point in the history
…s of statements.

Improve whitespace.
  • Loading branch information
gkellogg committed Jan 23, 2019
1 parent fb3599c commit e969c34
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 28 deletions.
27 changes: 18 additions & 9 deletions lib/rdf/turtle/writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,8 @@ def order_subjects
seen[st.object] if @lists.has_key?(st.object)
end

# List elements should not be targets for top-level serialization
list_elements = @lists.values.map(&:to_a).flatten.compact
# List elements which are bnodes should not be targets for top-level serialization
list_elements = @lists.values.map(&:to_a).flatten.select(&:node?).compact

# Sort subjects by resources over bnodes, ref_counts and the subject URI itself
recursable = (@subjects.keys - list_elements).
Expand Down Expand Up @@ -504,12 +504,15 @@ def do_list(l, position)
list = @lists[l]
log_debug("do_list") {list.inspect}
subject_done(RDF.nil)
index = 0
list.each_statement do |st|
next unless st.predicate == RDF.first
log_debug {" list this: #{st.subject} first: #{st.object}[#{position}]"}
@output.write(" ") if index > 0
path(st.object, position)
subject_done(st.subject)
position = :object
index += 1
end
end

Expand All @@ -519,7 +522,7 @@ def collection(node, position)
return false if position == :object && prop_count(node) > 0
#log_debug("collection") {"#{node.to_ntriples}, #{position}"}

@output.write(position == :subject ? "(" : " (")
@output.write("(")
log_depth {do_list(node, position)}
@output.write(')')
end
Expand All @@ -530,16 +533,20 @@ def blankNodePropertyList(resource, position)

log_debug("blankNodePropertyList") {resource.to_ntriples}
subject_done(resource)
@output.write(position == :subject ? "\n#{indent} [" : ' [')
@output.write(position == :subject ? "\n#{indent} [" : '[')
num_props = log_depth {predicateObjectList(resource, true)}
@output.write((num_props > 1 ? "\n#{indent}" : "") + (position == :object ? ']' : '] .'))
@output.write((num_props > 1 ? "\n#{indent(2)}" : "") + (position == :object ? ']' : '] .'))
true
end

# Default singular resource representation.
def p_term(resource, position)
#log_debug("p_term") {"#{resource.to_ntriples}, #{position}"}
l = (position == :subject ? "" : " ") + format_term(resource, options)
l = if resource == RDF.nil
"()"
else
format_term(resource, options)
end
@output.write(l)
end

Expand All @@ -562,7 +569,7 @@ def path(resource, position)
def predicate(resource)
log_debug("predicate") {resource.to_ntriples}
if resource == RDF.type
@output.write(" a")
@output.write("a")
else
path(resource, :predicate)
end
Expand Down Expand Up @@ -601,6 +608,7 @@ def predicateObjectList(subject, from_bpl = false)
begin
@output.write(";\n#{indent(2)}") if i > 0
predicate(RDF::URI.intern(prop))
@output.write(" ")
objectList(properties[prop])
end
end
Expand All @@ -611,8 +619,9 @@ def predicateObjectList(subject, from_bpl = false)
def triples(subject)
@output.write("\n#{indent}")
path(subject, :subject)
predicateObjectList(subject)
@output.write(" .")
@output.write(" ")
num_props = predicateObjectList(subject)
@output.write("#{num_props > 0 ? ' ' : ''}.")
true
end

Expand Down
46 changes: 27 additions & 19 deletions spec/writer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
},
"bare anon" => {
input: %(@prefix ex: <http://example.com/> . [ex:a ex:b] .),
regexp: [%r(^\s*\[ ex:a ex:b\] \.$)],
regexp: [%r(^\s*\[ex:a ex:b\] \.$)],
regexp_stream: [%r(_:\w+ ex:a ex:b \.$)]
},
"anon as subject" => {
Expand All @@ -106,7 +106,7 @@
},
"anon as object" => {
input: %(@prefix ex: <http://example.com/> . ex:a ex:b [ex:c ex:d] .),
regexp: [%r(^ex:a ex:b \[ ex:c ex:d\] \.$)],
regexp: [%r(^ex:a ex:b \[ex:c ex:d\] \.$)],
regexp_stream: []
},
"reuses BNode labels by default" => {
Expand Down Expand Up @@ -179,6 +179,14 @@
input: %(@prefix ex: <http://example.com/> . [ex:twoAnons ([a ex:mother] [a ex:father])] .),
regexp: [%r(\[\s*ex:twoAnons \(\s*\[\s*a ex:mother\s*\] \[\s*a ex:father\s*\]\)\] \.$)]
},
"list subjects": {
input: %(@prefix ex: <http://example.com/> . (ex:a ex:b) . ex:a a ex:Thing . ex:b a ex:Thing .),
regexp: [
%r(\(ex:a ex:b\) \.),
%r(ex:a a ex:Thing \.),
%r(ex:b a ex:Thing \.),
]
},
"owl:unionOf list": {
input: %(
@prefix ex: <http://example.com/> .
Expand Down Expand Up @@ -531,24 +539,24 @@
], canonicalize: true)
end
end
end

[
[0, "0.0e0"],
[10, "1.0e1"],
[-1, "-1.0e0"],
["0", "0.0e0"],
["10", "1.0e1"],
["-1", "-1.0e0"],
["1.0", "1.0e0"],
["0.1", "1.0e-1"],
["10.01", "1.001e1"],
["true", %{"true"^^<http://www.w3.org/2001/XMLSchema#double>}],
["false", %{"false"^^<http://www.w3.org/2001/XMLSchema#double>}],
["string", %{"string"^^<http://www.w3.org/2001/XMLSchema#double>}],
].each do |(l,r)|
it "serializes #{l.inspect} to #{r.inspect}" do
expect(subject.format_literal(RDF::Literal::Double.new(l))).to eql r
[
[0, "0.0e0"],
[10, "1.0e1"],
[-1, "-1.0e0"],
["0", "0.0e0"],
["10", "1.0e1"],
["-1", "-1.0e0"],
["1.0", "1.0e0"],
["0.1", "1.0e-1"],
["10.01", "1.001e1"],
["true", %{"true"^^<http://www.w3.org/2001/XMLSchema#double>}],
["false", %{"false"^^<http://www.w3.org/2001/XMLSchema#double>}],
["string", %{"string"^^<http://www.w3.org/2001/XMLSchema#double>}],
].each do |(l,r)|
it "serializes #{l.inspect} to #{r.inspect}" do
expect(subject.format_literal(RDF::Literal::Double.new(l))).to eql r
end
end
end

Expand Down

0 comments on commit e969c34

Please sign in to comment.