Skip to content

Commit

Permalink
Don't make values of @included values of a property referencing the…
Browse files Browse the repository at this point in the history
… containing node.
  • Loading branch information
gkellogg committed Jul 31, 2019
1 parent 0687909 commit a807090
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 30 deletions.
4 changes: 1 addition & 3 deletions lib/json/ld/flatten.rb
Expand Up @@ -114,9 +114,7 @@ def create_node_map(element, graph_map,

if element['@included']
create_node_map(element.delete('@included'), graph_map,
active_subject: active_subject,
active_graph: active_graph,
active_property: active_property)
active_graph: active_graph)
end

element.keys.each do |property|
Expand Down
37 changes: 13 additions & 24 deletions lib/json/ld/to_rdf.rb
Expand Up @@ -15,7 +15,6 @@ module ToRDF
# @yieldparam [RDF::Statement] statement
# @return RDF::Resource the subject of this item
def item_to_rdf(item, graph_name: nil, &block)
included_subjects = []
# Just return value object as Term
if value?(item)
value, datatype = item.fetch('@value'), item.fetch('@type', nil)
Expand Down Expand Up @@ -49,10 +48,10 @@ def item_to_rdf(item, graph_name: nil, &block)

# Initialize literal as an RDF literal using value and datatype. If element has the key @language and datatype is xsd:string, then add the value associated with the @language key as the language of the object.
language = item.fetch('@language', nil)
return [RDF::Literal.new(value, datatype: datatype, language: language)]
return RDF::Literal.new(value, datatype: datatype, language: language)
elsif list?(item)
# If item is a list object, initialize list_results as an empty array, and object to the result of the List Conversion algorithm, passing the value associated with the @list key from item and list_results.
return [parse_list(item['@list'], graph_name: graph_name, &block)]
return parse_list(item['@list'], graph_name: graph_name, &block)
end

subject = item['@id'] ? as_resource(item['@id']) : node
Expand Down Expand Up @@ -82,15 +81,12 @@ def item_to_rdf(item, graph_name: nil, &block)
object = item_to_rdf(v, graph_name: graph_name, &block)
#log_debug("item_to_rdf") {"subject: #{object.to_ntriples rescue 'malformed rdf'}"}
# yield subject, prediate, and literal to results.
Array(object).each do |o|
yield RDF::Statement(o, predicate, subject, graph_name: graph_name)
end
yield RDF::Statement(object, predicate, subject, graph_name: graph_name)
end
end
when '@included'
predicate = as_resource(property) if property
included_subjects = values.inject([]) do |memo, v|
memo + item_to_rdf(v, graph_name: graph_name, &block)
values.each do |v|
item_to_rdf(v, graph_name: graph_name, &block)
end
when /^@/
# Otherwise, if @type is any other keyword, skip to the next property-values pair
Expand All @@ -111,19 +107,16 @@ def item_to_rdf(item, graph_name: nil, &block)
yield RDF::Statement(subject, predicate, object, graph_name: graph_name)
else
# Otherwise, item is a value object or a node definition. Generate object as the result of the Object Converstion algorithm passing item.
objects = item_to_rdf(v, graph_name: graph_name, &block)
object = item_to_rdf(v, graph_name: graph_name, &block)
#log_debug("item_to_rdf") {"object: #{object.to_ntriples rescue 'malformed rdf'}"}
# yield subject, prediate, and literal to results.
Array(objects).each do |object|
yield RDF::Statement(subject, predicate, object, graph_name: graph_name)
end
yield RDF::Statement(subject, predicate, object, graph_name: graph_name)
end
end
end
end

# Return subjects from @included, if any, as well as primary subject
included_subjects.unshift(subject)
subject
end

##
Expand All @@ -138,24 +131,20 @@ def item_to_rdf(item, graph_name: nil, &block)
def parse_list(list, graph_name: nil, &block)
#log_debug('parse_list') {"list: #{list.inspect}"}

objects = []
list.each do |list_item|
# Set first to the result of the Object Converstion algorithm passing item.
objects += item_to_rdf(list_item, graph_name: graph_name, &block)
end

last = objects.pop
last = list.pop
result = first_bnode = last ? node : RDF.nil

objects.each do |object|
list.each do |list_item|
# Set first to the result of the Object Converstion algorithm passing item.
object = item_to_rdf(list_item, graph_name: graph_name, &block)
yield RDF::Statement(first_bnode, RDF.first, object, graph_name: graph_name)
rest_bnode = node
yield RDF::Statement(first_bnode, RDF.rest, rest_bnode, graph_name: graph_name)
first_bnode = rest_bnode
end
if last
yield RDF::Statement(first_bnode, RDF.first, last, graph_name: graph_name)
object = item_to_rdf(last, graph_name: graph_name, &block)
yield RDF::Statement(first_bnode, RDF.first, object, graph_name: graph_name)
yield RDF::Statement(first_bnode, RDF.rest, RDF.nil, graph_name: graph_name)
end
result
Expand Down
3 changes: 1 addition & 2 deletions spec/flatten_spec.rb
Expand Up @@ -383,8 +383,7 @@
output: %([{
"@id": "_:b0",
"http://example.org/prop": [
{"@id": "_:b1"},
{"@id": "_:b2"}
{"@id": "_:b1"}
]
}, {
"@id": "_:b1",
Expand Down
3 changes: 2 additions & 1 deletion spec/to_rdf_spec.rb
Expand Up @@ -881,7 +881,8 @@
}
}),
output: %(
[<http://example.org/prop> [a <http://example.org/Foo>], [a <http://example.org/Bar>]] .
[<http://example.org/prop> [a <http://example.org/Foo>]] .
[a <http://example.org/Bar>] .
)
},
"json.api example": {
Expand Down

0 comments on commit a807090

Please sign in to comment.