Skip to content

Commit

Permalink
Add graph hashing method to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
TShapinsky committed Nov 16, 2023
1 parent c1bf14d commit 0341d15
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
20 changes: 20 additions & 0 deletions buildingmotif/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,3 +513,23 @@ def skip_uri(uri: URIRef) -> bool:
if uri.startswith(ns):
return True
return False


def hash_graph(graph: Graph) -> int:
"""
Returns a cryptographic hash of the graph contents
:param graph: graph to hash
:type graph: graph
:return: integer hash
:rtype: int
"""
# Copy graph to memory (improved performance if graph is backed by a DB store)
graph_prime = copy_graph(graph)

# nt is the best performing serialization format I tested.
# For medium-office-compiled it takes 0.03s vs 0.5 for ttl
graph_string = graph_prime.serialize(format="nt")

return hash(graph_string)
20 changes: 20 additions & 0 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
_param_name,
get_parameters,
get_template_parts_from_shape,
hash_graph,
replace_nodes,
rewrite_shape_graph,
skip_uri,
Expand Down Expand Up @@ -278,3 +279,22 @@ def test_skip_uri():
assert skip_uri(XSD.integer)
assert skip_uri(SH.NodeShape)
assert not skip_uri(BRICK.Sensor)


def test_hash():
graph = Graph()
graph.parse(data=PREAMBLE)

graph.add((MODEL["a"], A, BRICK["AHU"]))
before_hash = hash_graph(graph)

triple_to_add = (MODEL["b"], A, BRICK["Sensor"])
graph.add(triple_to_add)

after_hash = hash_graph(graph)
assert before_hash != after_hash, "Graph changed, but hashes did not"

graph.remove(triple_to_add)

after_hash = hash_graph(graph)
assert before_hash == after_hash, "Graph with same state resulted in different hash"

0 comments on commit 0341d15

Please sign in to comment.