Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added method to extract a subgraph for isomorphic comparison
- Loading branch information
Showing
3 changed files
with
141 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import rdflib | ||
import rdflib.compare | ||
import sotools | ||
|
||
expected_json = """{ | ||
"@context": { | ||
"@vocab":"https://example.net/" | ||
}, | ||
"@id":"./sub", | ||
"property_0": "literal_0", | ||
"property_1": ["literal_1-0", "literal_1-1"], | ||
"property_2": { | ||
"property_3":"Anonymous subgraph" | ||
} | ||
} | ||
""" | ||
|
||
test_json = """{ | ||
"@context": { | ||
"@vocab":"https://example.net/" | ||
}, | ||
"@id":"./parent", | ||
"sub":""" + expected_json + """ | ||
} | ||
""" | ||
|
||
# Load the full graph, setting the base to "https://example.net/" | ||
g_full = rdflib.Graph() | ||
g_full.parse(data=test_json, format="json-ld", publicID="https://example.net/") | ||
print("===\nFull:") | ||
print(g_full.serialize(format="nt").decode()) | ||
|
||
g_expected = rdflib.ConjunctiveGraph() | ||
g_expected.parse(data=expected_json, format="json-ld", publicID="https://example.net/") | ||
print("===\nExpected:") | ||
print(g_expected.serialize(format="nt").decode()) | ||
|
||
#Extract the subgraph that is the object of the subject "https://example.net/sub" | ||
g_sub = sotools.getSubgraph(g_full, rdflib.URIRef("https://example.net/sub")) | ||
print("===\nExtracted:") | ||
print(g_sub.serialize(format="nt").decode()) | ||
|
||
#Direct comparison of the graphs, will fail if there are BNodes | ||
print(f"Extracted subgraph is equal to the expected graph: {g_sub == g_expected}") | ||
|
||
# Use isomorphic comparison. This operations can be very expensive if either of | ||
# the grphs are poorly structured with lots of BNodes | ||
print((f"Extracted subgraph is isomorphic with the expected: " | ||
f"{rdflib.compare.isomorphic(g_sub, g_expected)}")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters