Skip to content

Commit

Permalink
Improve support in flattening for @reverse with a blank node subject.
Browse files Browse the repository at this point in the history
  • Loading branch information
gkellogg committed Oct 21, 2013
1 parent 2a1bf31 commit 49492f4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
13 changes: 8 additions & 5 deletions lib/json/ld/flatten.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def generate_node_map(element,
list = nil)
depth do
debug("node_map") {"active_graph: #{active_graph}, element: #{element.inspect}"}
debug(" =>") {"active_subject: #{active_subject.inspect}, active_property: #{active_property.inspect}, list: #{list.inspect}"}
if element.is_a?(Array)
# If element is an array, process each entry in element recursively by passing item for element, node map, active graph, active subject, active property, and list.
element.map {|o|
Expand Down Expand Up @@ -90,7 +91,10 @@ def generate_node_map(element,
graph[id] ||= {'@id' => id}

# If active property is not null, perform the following steps:
if active_property
if node?(active_subject) || node_reference?(active_subject)
debug("node_map") {"#{active_subject.inspect} is an object, merge into #{graph[id].inspect}"}
merge_value(graph[id], active_property, active_subject)
elsif active_property
# Create a new JSON object reference consisting of a single member @id whose value is id.
reference = {'@id' => id}

Expand Down Expand Up @@ -125,13 +129,12 @@ def generate_node_map(element,
element.delete('@reverse').each do |property, values|
values.each do |value|
debug("node_map") {"@reverse(#{id}): #{value.inspect}"}
# If value has a property member, append referenced node to its value; otherwise create a property member whose value is an array containing referenced node.
merge_value(value, property, {'@id' => id})

# Recursively invoke this algorithm passing value for element, node map, and active graph.
generate_node_map(value,
node_map,
active_graph)
active_graph,
{'@id' => id},
property)
end
end
end
Expand Down
29 changes: 29 additions & 0 deletions spec/flatten_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,35 @@
}]
}),
:options => {}
},
"@reverse bnode issue (0045)" => {
:input => ::JSON.parse(%q{
{
"@context": {
"foo": "http://example.org/foo",
"bar": { "@reverse": "http://example.org/bar", "@type": "@id" }
},
"foo": "Foo",
"bar": [ "http://example.org/origin", "_:b0" ]
}
}),
:output => ::JSON.parse(%q{
[
{
"@id": "_:b0",
"http://example.org/foo": [ { "@value": "Foo" } ]
},
{
"@id": "_:b1",
"http://example.org/bar": [ { "@id": "_:b0" } ]
},
{
"@id": "http://example.org/origin",
"http://example.org/bar": [ { "@id": "_:b0" } ]
}
]
}),
:options => {}
}
}.each do |title, params|
it title do
Expand Down

0 comments on commit 49492f4

Please sign in to comment.