Created
July 23, 2020 23:07
-
-
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
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
-- | 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