@@ -211,19 +211,45 @@ def count_blank_node_identifiers_internal(input, results)
211211 end
212212 end
213213
214+ ##
215+ # Prune BNode identifiers recursively
216+ #
217+ # @param [Array, Hash] input
218+ # @param [Array<String>] bnodes_to_clear
219+ # @return [Array, Hash]
220+ def prune_bnodes ( input , bnodes_to_clear )
221+ result = case input
222+ when Array
223+ # If, after replacement, an array contains only the value null remove the value, leaving an empty array.
224+ input . map { |o | prune_bnodes ( o , bnodes_to_clear ) } . compact
225+ when Hash
226+ output = Hash . new
227+ input . each do |key , value |
228+ if context . expand_iri ( key ) == '@id' && bnodes_to_clear . include? ( value )
229+ # Don't add this to output, as it is pruned as being superfluous
230+ else
231+ output [ key ] = prune_bnodes ( value , bnodes_to_clear )
232+ end
233+ end
234+ output
235+ else
236+ input
237+ end
238+ result
239+ end
240+
214241 ##
215242 # Replace @preserve keys with the values, also replace @null with null.
216243 #
217244 # Optionally, remove BNode identifiers only used once.
218245 #
219246 # @param [Array, Hash] input
220- # @param [Array<String>] bnodes_to_clear
221247 # @return [Array, Hash]
222- def cleanup_preserve ( input , bnodes_to_clear )
248+ def cleanup_preserve ( input )
223249 result = case input
224250 when Array
225251 # If, after replacement, an array contains only the value null remove the value, leaving an empty array.
226- v = input . map { |o | cleanup_preserve ( o , bnodes_to_clear ) } . compact
252+ v = input . map { |o | cleanup_preserve ( o ) } . compact
227253
228254 # If the array contains a single member, which is itself an array, use that value as the result
229255 ( v . length == 1 && v . first . is_a? ( Array ) ) ? v . first : v
@@ -232,11 +258,9 @@ def cleanup_preserve(input, bnodes_to_clear)
232258 input . each do |key , value |
233259 if key == '@preserve'
234260 # replace all key-value pairs where the key is @preserve with the value from the key-pair
235- output = cleanup_preserve ( value , bnodes_to_clear )
236- elsif context . expand_iri ( key ) == '@id' && bnodes_to_clear . include? ( value )
237- # Don't add this to output, as it is pruned as being superfluous
261+ output = cleanup_preserve ( value )
238262 else
239- v = cleanup_preserve ( value , bnodes_to_clear )
263+ v = cleanup_preserve ( value )
240264
241265 # Because we may have added a null value to an array, we need to clean that up, if we possible
242266 v = v . first if v . is_a? ( Array ) && v . length == 1 && !context . as_array? ( key )
0 commit comments