Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added skeleton RDF comparer, updated tests to use "isIsomorphic" for …

…comparison.

Ignore-this: c88d10bc8bfa82f77a2ef80dccacfa6a

darcs-hash:20110129212117-1786f-ffd133cd78659d772909c56e26b8186929c3df8e.gz
  • Loading branch information...
commit a9cb31a59e376e4d6245c407cd1d5aed3783f37a 1 parent 3a01750
@amccausl authored
View
23 src/Data/RDF.hs
@@ -22,6 +22,7 @@ module Data.RDF (
PrefixMappings(PrefixMappings), toPMList, PrefixMapping(PrefixMapping),
NodeSelector, isUNode, isBNode, isLNode,
equalSubjects, equalPredicates, equalObjects,
+ isIsomorphic,
subjectOf, predicateOf, objectOf,
Subject, Predicate, Object,
FastString(uniq,value),mkFastString,
@@ -505,3 +506,25 @@ fromEither res =
removeDupes :: Triples -> Triples
removeDupes = map head . group . sort
+-- |This determines if two RDF representations are equal regardless of blank
+-- nodc names, triple order and prefixes. In math terms, this is the \simeq
+-- latex operator, or ~=
+isIsomorphic :: forall rdf1 rdf2. (RDF rdf1, RDF rdf2) => rdf1 -> rdf2 -> Bool
+isIsomorphic g1 g2 = normalize g1 == normalize g2
+ where normalize :: forall rdf. (RDF rdf) => rdf -> Triples
+ normalize = sort . nub . expandTriples
+
+-- |Expand the triples in a graph with the prefix map and base URL for that
+-- graph.
+expandTriples :: (RDF rdf) => rdf -> Triples
+expandTriples rdf = expandTriples' [] (baseUrl rdf) (prefixMappings rdf) (triplesOf rdf)
+
+expandTriples' :: Triples -> Maybe BaseUrl -> PrefixMappings -> Triples -> Triples
+expandTriples' acc _ _ [] = acc
+expandTriples' acc baseUrl prefixMappings (t:rest) = expandTriples' (normalize baseUrl prefixMappings t : acc) baseUrl prefixMappings rest
+ where normalize baseUrl prefixMappings = expandPrefixes prefixMappings . expandBaseUrl baseUrl
+ expandBaseUrl (Just baseUrl) triple = triple
+ expandBaseUrl Nothing triple = triple
+ expandPrefixes prefixMappings triple = triple
+
+-- expand prefix map into triples, sort triples, recursively generate a blank node mapping
View
1  src/Data/RDF/TriplesGraph.hs
@@ -42,7 +42,6 @@ import qualified Data.Map as Map
--
-- * 'query' : O(n)
newtype TriplesGraph = TriplesGraph (Triples, Maybe BaseUrl, PrefixMappings)
- deriving (Eq)
instance RDF TriplesGraph where
baseUrl = baseUrl'
View
24 testsuite/tests/Text/RDF/RDF4H/XmlParser_Test.hs
@@ -26,7 +26,8 @@ import Data.RDF.TriplesGraph_Test
import Text.RDF.RDF4H.XmlParser
-tests = [ testGroup "XmlParser:parseXmlRDF" [ testCase "example07" test_parseXmlRDF_example07
+tests = [ testGroup "XmlParser:parseXmlRDF" [ testCase "simpleStriping" test_simpleStriping
+ , testCase "example07" test_parseXmlRDF_example07
-- , testCase "example08" test_parseXmlRDF_example08
-- , testCase "example09" test_parseXmlRDF_example09
-- , testCase "example10" test_parseXmlRDF_example10
@@ -45,8 +46,21 @@ tests = [ testGroup "XmlParser:parseXmlRDF" [ testCase "example07" test_parseXml
]
mkTextNode = lnode . plainL . s2b
-testParse exRDF ex = (case parseXmlRDF Nothing Nothing (s2b exRDF) of Right (result :: TriplesGraph) -> result) @?= ex
-
+testParse exRDF ex = assertBool ("expected: " ++ show ex ++ "but got: " ++ show parsed) (isIsomorphic (parsed :: TriplesGraph) (ex :: TriplesGraph))
+ where parsed = case parseXmlRDF Nothing Nothing (s2b exRDF) of Right result -> result
+
+test_simpleStriping = testParse
+ "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\
+ \ xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\
+ \<rdf:Description rdf:about=\"http://www.w3.org/TR/rdf-syntax-grammar\">\
+ \<dc:title>RDF/XML Syntax Specification (Revised)</dc:title>\
+ \</rdf:Description>\
+ \</rdf:RDF>"
+ ( mkRdf [ Triple ((unode . s2b) "http://www.w3.org/TR/rdf-syntax-grammar") ((unode . s2b) "dc:title") (mkTextNode "RDF/XML Syntax Specification (Revised)") ]
+ Nothing
+ ( PrefixMappings (Map.fromList [ (s2b "dc", s2b "http://purl.org/dc/elements/1.1/")
+ , (s2b "rdf", s2b "http://www.w3.org/1999/02/22-rdf-syntax-ns#") ]) )
+ )
example07 = "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\
\ xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\
@@ -69,8 +83,8 @@ test_parseXmlRDF_example07 = testParse example07
]
Nothing
( PrefixMappings (Map.fromList [ (s2b "dc", s2b "http://purl.org/dc/elements/1.1/")
- , (s2b "ex", s2b "http://example.org/stuff/1.0/")
- , (s2b "rdf", s2b "http://www.w3.org/1999/02/22-rdf-syntax-ns#") ]) )
+ , (s2b "ex", s2b "http://example.org/stuff/1.0/")
+ , (s2b "rdf", s2b "http://www.w3.org/1999/02/22-rdf-syntax-ns#") ]) )
)
example08 = "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\
Please sign in to comment.
Something went wrong with that request. Please try again.