Skip to content

Instantly share code, notes, and snippets.

@robstewart57
Created July 23, 2020 23:07
Show Gist options
  • Save robstewart57/66eafe6d1eca54e9743e211d1fcd8c6e to your computer and use it in GitHub Desktop.
Save robstewart57/66eafe6d1eca54e9743e211d1fcd8c6e to your computer and use it in GitHub Desktop.
A Haskell function that computes if two RDF graph structures are identical. Uses functions from the rdf4h library and the Automorphism.isIsomorphic function from the hgal library
-- | Compares the structure of two graphs and returns 'True' if their
-- graph structures are identical. This does not consider the nature
-- of each node in the graph, i.e. the URI text of 'UNode' nodes,
-- the generated index of a blank node, or the values in literal
-- nodes. Unsafe because it assumes IRI resolution will succeed, may
-- throw an 'IRIResolutionException` exception.
isGraphIsomorphic :: (Rdf a, Rdf b) => RDF a -> RDF b -> Bool
isGraphIsomorphic g1 g2 = Automorphism.isIsomorphic g1' g2'
where
g1' = rdfGraphToDataGraph g1
g2' = rdfGraphToDataGraph g2
rdfGraphToDataGraph :: Rdf c => RDF c -> Graph
rdfGraphToDataGraph g = dataGraph
where
triples = expandTriples g
triplesHashMap :: HashMap (Subject, Predicate) [Object]
triplesHashMap = HashMap.fromListWith (<>) [((s, p), [o]) | Triple s p o <- triples]
triplesGrouped :: [((Subject, Predicate), [Object])]
triplesGrouped = HashMap.toList triplesHashMap
(dataGraph, _, _) = (graphFromEdges . fmap (\((s, p), os) -> (s, p, os))) triplesGrouped
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment