Skip to content

Commit c2fb0a2

Browse files
committed
Keep triples generated through nested collections.
1 parent 4d4cc1b commit c2fb0a2

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

lib/ld/patch/parser.rb

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ class Parser
231231

232232
current[:resource] = current[:collection].subject
233233
end
234+
235+
(input[:triples] ||= []).concat(current[:triples]) if current[:triples]
234236
input[:subject] = current[:resource] || current[:iri]
235237
end
236238

@@ -269,7 +271,7 @@ class Parser
269271
end
270272

271273
production(:blankNodePropertyList) do |input, current, callback|
272-
input[:resource] = current[:subject]
274+
input[:subject] = input[:resource] = current[:subject]
273275
(input[:triples] ||= []).concat(current[:triples]) if current[:triples]
274276
end
275277

@@ -282,6 +284,7 @@ class Parser
282284
production(:collection) do |input, current, callback|
283285
# Create an RDF list
284286
objects = current[:object_list]
287+
(input[:triples] ||= []).concat(current[:triples]) if current[:triples]
285288
input[:collection] = RDF::List[*objects]
286289
end
287290

@@ -545,7 +548,11 @@ def bnode(id = nil)
545548
@options[:anon_base] = @options[:anon_base].succ
546549
end
547550
# Don't use provided ID to avoid aliasing issues when re-serializing the graph, when the bnode identifiers are re-used
548-
(@bnode_cache ||= {})[id.to_s] ||= RDF::Node.new
551+
(@bnode_cache ||= {})[id.to_s] ||= begin
552+
new_bnode = RDF::Node.new
553+
new_bnode.lexical = "_:#{id}"
554+
new_bnode
555+
end
549556
end
550557
end
551558

@@ -589,4 +596,14 @@ def literal(value, options = {})
589596
RDF::Literal.new(value, options.merge(validate: validate?))
590597
end
591598
end
592-
end
599+
end
600+
601+
602+
# Update RDF::Node to set lexical representation of BNode
603+
##
604+
# Extensions for RDF::URI
605+
class RDF::Node
606+
# Original lexical value of this URI to allow for round-trip serialization.
607+
def lexical=(value); @lexical = value; end
608+
def lexical; @lexical; end
609+
end

spec/algebra_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,18 @@
140140
<http://example/#> <http://example.org/vocab#preferredLanguages> _:a .
141141
)
142142
},
143+
"nested_collection" => {
144+
data: %(<http://example.org/something> <http://example.org/completely> <http://example.org/different> .),
145+
patch: %(Add {<http://a.example/s> <http://a.example/p> ((1)) .} .),
146+
result: %(
147+
<http://a.example/s> <http://a.example/p> _:outerEl1 .
148+
_:outerEl1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> _:innerEl1 .
149+
_:innerEl1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> "1"^^<http://www.w3.org/2001/XMLSchema#integer> .
150+
_:innerEl1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> .
151+
_:outerEl1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> .
152+
<http://example.org/something> <http://example.org/completely> <http://example.org/different> .
153+
)
154+
}
143155
}.each do |name, props|
144156
it name do
145157
graph = RDF::Graph.new << RDF::NTriples::Reader.new(props[:data])

0 commit comments

Comments
 (0)