Skip to content

Commit 9754b84

Browse files
committed
Graph framing cleanup.
1 parent 73d2a5a commit 9754b84

File tree

2 files changed

+146
-37
lines changed

2 files changed

+146
-37
lines changed

lib/json/ld/api.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,10 +433,12 @@ def self.frame(input, frame, options = {})
433433
# Get framing nodes from expanded input, replacing Blank Node identifiers as necessary
434434
create_node_map(value, framing_state[:graphMap], graph: '@default')
435435

436-
# If Frame is {'@graph': {}] use only the default graph for matches
437-
if frame.has_key?('@graph') && frame['@graph'].empty?
436+
frame_keys = frame.keys.map {|k| context.expand_iri(k, vocab: true, quiet: true)}
437+
if frame_keys.include?('@graph')
438+
# If frame contains @graph, it matches the default graph.
438439
framing_state[:graph] = '@default'
439440
else
441+
# If frame does not contain @graph used the merged graph.
440442
framing_state[:graph] = '@merged'
441443
framing_state[:link]['@merged'] = {}
442444
framing_state[:graphMap]['@merged'] = merge_node_map_graphs(framing_state[:graphMap])

spec/frame_spec.rb

Lines changed: 142 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,67 @@
637637
}]
638638
}]
639639
})
640+
},
641+
"library" => {
642+
frame: %({
643+
"@context": {
644+
"dc": "http://purl.org/dc/elements/1.1/",
645+
"ex": "http://example.org/vocab#",
646+
"xsd": "http://www.w3.org/2001/XMLSchema#",
647+
"ex:contains": { "@type": "@id" }
648+
},
649+
"@type": "ex:Library",
650+
"ex:contains": {}
651+
}),
652+
input: %({
653+
"@context": {
654+
"dc": "http://purl.org/dc/elements/1.1/",
655+
"ex": "http://example.org/vocab#",
656+
"xsd": "http://www.w3.org/2001/XMLSchema#"
657+
},
658+
"@id": "http://example.org/library",
659+
"@type": "ex:Library",
660+
"dc:name": "Library",
661+
"ex:contains": {
662+
"@id": "http://example.org/library/the-republic",
663+
"@type": "ex:Book",
664+
"dc:creator": "Plato",
665+
"dc:title": "The Republic",
666+
"ex:contains": {
667+
"@id": "http://example.org/library/the-republic#introduction",
668+
"@type": "ex:Chapter",
669+
"dc:description": "An introductory chapter on The Republic.",
670+
"dc:title": "The Introduction"
671+
}
672+
}
673+
}),
674+
output: %({
675+
"@context": {
676+
"dc": "http://purl.org/dc/elements/1.1/",
677+
"ex": "http://example.org/vocab#",
678+
"xsd": "http://www.w3.org/2001/XMLSchema#",
679+
"ex:contains": { "@type": "@id" }
680+
},
681+
"@graph": [
682+
{
683+
"@id": "http://example.org/library",
684+
"@type": "ex:Library",
685+
"dc:name": "Library",
686+
"ex:contains": {
687+
"@id": "http://example.org/library/the-republic",
688+
"@type": "ex:Book",
689+
"dc:creator": "Plato",
690+
"dc:title": "The Republic",
691+
"ex:contains": {
692+
"@id": "http://example.org/library/the-republic#introduction",
693+
"@type": "ex:Chapter",
694+
"dc:description": "An introductory chapter on The Republic.",
695+
"dc:title": "The Introduction"
696+
}
697+
}
698+
}
699+
]
700+
})
640701
}
641702
}.each do |title, params|
642703
it title do
@@ -1018,37 +1079,7 @@
10181079

10191080
describe "named graphs" do
10201081
{
1021-
#"Frame default graph only" => {
1022-
# frame: %({
1023-
# "@context": {"@vocab": "urn:"},
1024-
# "@graph": {}
1025-
# }),
1026-
# input: %({
1027-
# "@context": {"@vocab": "urn:"},
1028-
# "@id": "urn:id-1",
1029-
# "@type": "Class",
1030-
# "term": "foo",
1031-
# "ref": {
1032-
# "@id": "urn:id-2",
1033-
# "@graph": {
1034-
# "@id": "urn:id-2",
1035-
# "term": "bar"
1036-
# }
1037-
# }
1038-
# }),
1039-
# output: %({
1040-
# "@context": {"@vocab": "urn:"},
1041-
# "@graph": [
1042-
# {
1043-
# "@id": "urn:id-1",
1044-
# "@type": "Class",
1045-
# "ref": {"@id": "urn:id-2"},
1046-
# "term": "foo"
1047-
# }
1048-
# ]
1049-
# })
1050-
#},
1051-
"Merge graphs" => {
1082+
"Merge graphs if no outer @graph is used" => {
10521083
frame: %({
10531084
"@context": {"@vocab": "urn:"},
10541085
"@type": "Class"
@@ -1075,7 +1106,7 @@
10751106
}]
10761107
})
10771108
},
1078-
"Preserve graphs" => {
1109+
"Preserve graphs if outer @graph is used" => {
10791110
frame: %({
10801111
"@context": {"@vocab": "urn:"},
10811112
"@type": "Class",
@@ -1085,7 +1116,8 @@
10851116
"@context": {"@vocab": "urn:"},
10861117
"@id": "urn:id-1",
10871118
"@type": "Class",
1088-
"merge": {
1119+
"preserve": {
1120+
"@id": "urn:gr-1",
10891121
"@graph": {
10901122
"@id": "urn:id-2",
10911123
"term": "data"
@@ -1097,8 +1129,8 @@
10971129
"@graph": [{
10981130
"@id": "urn:id-1",
10991131
"@type": "Class",
1100-
"merge": {
1101-
"@id": "_:b0",
1132+
"preserve": {
1133+
"@id": "urn:gr-1",
11021134
"@graph": [{
11031135
"@id": "urn:id-2",
11041136
"term": "data"
@@ -1205,6 +1237,81 @@
12051237
}]
12061238
})
12071239
},
1240+
"library" => {
1241+
frame: %({
1242+
"@context": {
1243+
"dc": "http://purl.org/dc/elements/1.1/",
1244+
"ex": "http://example.org/vocab#",
1245+
"xsd": "http://www.w3.org/2001/XMLSchema#",
1246+
"ex:contains": { "@type": "@id" }
1247+
},
1248+
"@type": "ex:Library",
1249+
"ex:contains": {
1250+
"@id": "http://example.org/graphs/books",
1251+
"@graph": {
1252+
"@type": "ex:Book"
1253+
}
1254+
}
1255+
}),
1256+
input: %({
1257+
"@context": {
1258+
"dc": "http://purl.org/dc/elements/1.1/",
1259+
"ex": "http://example.org/vocab#",
1260+
"xsd": "http://www.w3.org/2001/XMLSchema#",
1261+
"ex:contains": { "@type": "@id" }
1262+
},
1263+
"@id": "http://example.org/library",
1264+
"@type": "ex:Library",
1265+
"dc:name": "Library",
1266+
"ex:contains": {
1267+
"@id": "http://example.org/graphs/books",
1268+
"@graph": {
1269+
"@id": "http://example.org/library/the-republic",
1270+
"@type": "ex:Book",
1271+
"dc:creator": "Plato",
1272+
"dc:title": "The Republic",
1273+
"ex:contains": {
1274+
"@id": "http://example.org/library/the-republic#introduction",
1275+
"@type": "ex:Chapter",
1276+
"dc:description": "An introductory chapter on The Republic.",
1277+
"dc:title": "The Introduction"
1278+
}
1279+
}
1280+
}
1281+
}),
1282+
output: %({
1283+
"@context": {
1284+
"dc": "http://purl.org/dc/elements/1.1/",
1285+
"ex": "http://example.org/vocab#",
1286+
"xsd": "http://www.w3.org/2001/XMLSchema#",
1287+
"ex:contains": { "@type": "@id" }
1288+
},
1289+
"@graph": [
1290+
{
1291+
"@id": "http://example.org/library",
1292+
"@type": "ex:Library",
1293+
"dc:name": "Library",
1294+
"ex:contains": {
1295+
"@id": "http://example.org/graphs/books",
1296+
"@graph": [
1297+
{
1298+
"@id": "http://example.org/library/the-republic",
1299+
"@type": "ex:Book",
1300+
"dc:creator": "Plato",
1301+
"dc:title": "The Republic",
1302+
"ex:contains": {
1303+
"@id": "http://example.org/library/the-republic#introduction",
1304+
"@type": "ex:Chapter",
1305+
"dc:description": "An introductory chapter on The Republic.",
1306+
"dc:title": "The Introduction"
1307+
}
1308+
}
1309+
]
1310+
}
1311+
}
1312+
]
1313+
})
1314+
}
12081315
}.each do |title, params|
12091316
it title do
12101317
do_frame(params)

0 commit comments

Comments
 (0)