Permalink
Browse files

* Moved parseFile and parseURL to ParserUtils

Ignore-this: 46fe52414443249936a3cfb5433ce299
* Moved all RDF implementations to the Data.RDF namespace (since they have nothing to do with text)
* Some hlint recommended changes

darcs-hash:20110115085607-1786f-7fefcbe6b7fa2443e6a08f9f1af61ef3609c7717.gz
  • Loading branch information...
1 parent 93a94da commit 8a0993308f5a3995b468895210e597a84edfb2dd @amccausl committed Jan 15, 2011
View
@@ -37,8 +37,8 @@ flag hpc
library
exposed-modules: Data.RDF
, Data.RDF.Namespace
- , Text.RDF.RDF4H.MGraph
- , Text.RDF.RDF4H.TriplesGraph
+ , Data.RDF.MGraph
+ , Data.RDF.TriplesGraph
, Text.RDF.RDF4H.NTriplesParser
, Text.RDF.RDF4H.NTriplesSerializer
, Text.RDF.RDF4H.TurtleParser
@@ -100,8 +100,8 @@ executable test-rdf4h
, HUnit >= 1.2.2.1 && < 1.3
other-modules: Data.RDF
, Data.RDF.Namespace
- , Text.RDF.RDF4H.MGraph
- , Text.RDF.RDF4H.TriplesGraph
+ , Data.RDF.MGraph
+ , Data.RDF.TriplesGraph
, Text.RDF.RDF4H.NTriplesParser
, Text.RDF.RDF4H.NTriplesSerializer
, Text.RDF.RDF4H.TurtleParser
View
@@ -4,7 +4,6 @@
--
module Data.RDF (
- parseFile, parseURL,
-- * Serializing RDF
RdfSerializer(hWriteG, writeG, hWriteH, writeH, hWriteTs, writeTs, hWriteT, writeT, hWriteN, writeN),
-- * RDF graph
@@ -35,6 +34,9 @@ import Data.RDF.Utils
import Data.ByteString.Lazy.Char8(ByteString)
import qualified Data.ByteString.Lazy.Char8 as B
import Data.List
+import Data.Ord (comparing)
+
+import qualified Data.Map as Map
import System.IO
@@ -83,7 +85,7 @@ class RDF rdf where
-- |Return an empty RDF graph.
empty :: rdf
--- empty = mkRdf [] Nothing (PrefixMappings Map.Empty)
+ empty = mkRdf [] Nothing (PrefixMappings Map.empty)
-- |Return a graph containing all the given triples. Handling of duplicates
-- in the input depend on the particular graph implementation.
@@ -115,13 +117,13 @@ class RDF rdf where
-- particular graph implementation for more information.
select :: rdf -> NodeSelector -> NodeSelector -> NodeSelector -> Triples
select rdf funcS funcP funcO = filter (select' funcS funcP funcO) (triplesOf rdf)
- where select' (Just fS) (Just fP) (Just fO) = \(Triple s p o) -> (fS s) && (fP p) && (fO o)
- select' (Just fS) (Just fP) Nothing = \(Triple s p _) -> (fS s) && (fP p)
- select' (Just fS) Nothing (Just fO) = \(Triple s _ o) -> (fS s) && (fO o)
- select' (Just fS) Nothing Nothing = \(Triple s _ _) -> (fS s)
- select' Nothing (Just fP) (Just fO) = \(Triple _ p o) -> (fP p) && (fO o)
- select' Nothing (Just fP) Nothing = \(Triple _ p _) -> (fP p)
- select' Nothing Nothing (Just fO) = \(Triple _ _ o) -> (fO o)
+ where select' (Just fS) (Just fP) (Just fO) = \(Triple s p o) -> fS s && fP p && fO o
+ select' (Just fS) (Just fP) Nothing = \(Triple s p _) -> fS s && fP p
+ select' (Just fS) Nothing (Just fO) = \(Triple s _ o) -> fS s && fO o
+ select' (Just fS) Nothing Nothing = \(Triple s _ _) -> fS s
+ select' Nothing (Just fP) (Just fO) = \(Triple _ p o) -> fP p && fO o
+ select' Nothing (Just fP) Nothing = \(Triple _ p _) -> fP p
+ select' Nothing Nothing (Just fO) = \(Triple _ _ o) -> fO o
select' Nothing Nothing Nothing = \(Triple _ _ _) -> True
-- |Return the triples in the graph that match the given pattern, where
@@ -138,23 +140,7 @@ class RDF rdf where
query :: rdf -> Maybe Subject -> Maybe Predicate -> Maybe Object -> Triples
query rdf s p o = select rdf (toNodeSelector s) (toNodeSelector p) (toNodeSelector o)
where toNodeSelector Nothing = Nothing
- toNodeSelector (Just node) = Just (\x -> x == node)
-
--- * Shared functions for use by parsing functions
-
-parseFile :: forall rdf. (RDF rdf)
- => (ByteString -> Either ParseFailure rdf)
- -> FilePath
- -> IO (Either ParseFailure rdf)
-parseFile parser file = do str <- readFile file
- return (parser (s2b str))
-
--- TODO: use proper function
-parseURL :: forall rdf. (RDF rdf)
- => (ByteString -> Either ParseFailure rdf)
- -> String
- -> IO (Either ParseFailure rdf)
-parseURL = parseFile
+ toNodeSelector (Just node) = Just (== node)
-- |An RdfSerializer is a serializer of RDF to some particular output format, such as
-- NTriples or Turtle.
@@ -349,33 +335,30 @@ instance Eq Node where
-- of '(BNodeGen 44)' and '(BNodeGen 3)' is that of the values, or
-- 'compare 44 3', GT.
instance Ord Node where
- compare n1 n2 = compareNode n1 n2
-
-compareNode :: Node -> Node -> Ordering
-compareNode (UNode fs1) (UNode fs2) = compareFS fs1 fs2
-compareNode (UNode _) _ = LT
-compareNode (BNode fs1) (BNode fs2) = compareFS fs1 fs2
-compareNode (BNode _) (UNode _) = GT
-compareNode (BNode _) _ = LT
-compareNode (BNodeGen i1) (BNodeGen i2) = compare i1 i2
-compareNode (BNodeGen _) (LNode _) = LT
-compareNode (BNodeGen _) _ = GT
-compareNode (LNode (PlainL bs1)) (LNode (PlainL bs2)) = compare bs1 bs2
-compareNode (LNode (PlainL _)) (LNode _) = LT
-compareNode (LNode (PlainLL bs1 bs1')) (LNode (PlainLL bs2 bs2')) =
- case compare bs1' bs2' of
- EQ -> compare bs1 bs2
- LT -> LT
- GT -> GT
-compareNode (LNode (PlainLL _ _)) (LNode (PlainL _)) = GT
-compareNode (LNode (PlainLL _ _)) (LNode _) = LT
-compareNode (LNode (TypedL bs1 fs1)) (LNode (TypedL bs2 fs2)) =
- case compare fs1 fs2 of
- EQ -> compare bs1 bs2
- LT -> LT
- GT -> GT
-compareNode (LNode (TypedL _ _)) (LNode _) = GT
-compareNode (LNode _) _ = GT
+ compare (UNode fs1) (UNode fs2) = compareFS fs1 fs2
+ compare (UNode _) _ = LT
+ compare (BNode fs1) (BNode fs2) = compareFS fs1 fs2
+ compare (BNode _) (UNode _) = GT
+ compare (BNode _) _ = LT
+ compare (BNodeGen i1) (BNodeGen i2) = compare i1 i2
+ compare (BNodeGen _) (LNode _) = LT
+ compare (BNodeGen _) _ = GT
+ compare (LNode (PlainL bs1)) (LNode (PlainL bs2)) = compare bs1 bs2
+ compare (LNode (PlainL _)) (LNode _) = LT
+ compare (LNode (PlainLL bs1 bs1')) (LNode (PlainLL bs2 bs2')) =
+ case compare bs1' bs2' of
+ EQ -> compare bs1 bs2
+ LT -> LT
+ GT -> GT
+ compare (LNode (PlainLL _ _)) (LNode (PlainL _)) = GT
+ compare (LNode (PlainLL _ _)) (LNode _) = LT
+ compare (LNode (TypedL bs1 fs1)) (LNode (TypedL bs2 fs2)) =
+ case compare fs1 fs2 of
+ EQ -> compare bs1 bs2
+ LT -> LT
+ GT -> GT
+ compare (LNode (TypedL _ _)) (LNode _) = GT
+ compare (LNode _) _ = GT
-- |Two triples are equal iff their respective subjects, predicates, and objects
-- are equal.
@@ -385,14 +368,7 @@ instance Eq Triple where
-- |The ordering of triples is based on that of the subject, predicate, and object
-- of the triple, in that order.
instance Ord Triple where
- (Triple s1 p1 o1) `compare` (Triple s2 p2 o2) =
- case compareNode s1 s2 of
- EQ -> case compareNode p1 p2 of
- EQ -> compareNode o1 o2
- LT -> LT
- GT -> GT
- GT -> GT
- LT -> LT
+ compare = comparing (\(Triple s p o) -> (s, p, o))
-- |Two 'LValue' values are equal iff they are of the same type and all fields are
-- equal.
@@ -408,25 +384,21 @@ instance Eq LValue where
-- literal value second, and '(TypedL literalValue datatypeUri)' being ordered
-- by datatype first and literal value second.
instance Ord LValue where
- compare l1 l2 = compareLValue l1 l2
-
-{-# INLINE compareLValue #-}
-compareLValue :: LValue -> LValue -> Ordering
-compareLValue (PlainL bs1) (PlainL bs2) = compare bs1 bs2
-compareLValue (PlainL _) _ = LT
-compareLValue _ (PlainL _) = GT
-compareLValue (PlainLL bs1 bs1') (PlainLL bs2 bs2') =
- case compare bs1' bs2' of
- EQ -> compare bs1 bs2
- GT -> GT
- LT -> LT
-compareLValue (PlainLL _ _) _ = LT
-compareLValue _ (PlainLL _ _) = GT
-compareLValue (TypedL l1 t1) (TypedL l2 t2) =
- case compareFS t1 t2 of
- EQ -> compare l1 l2
- GT -> GT
- LT -> LT
+ compare (PlainL bs1) (PlainL bs2) = compare bs1 bs2
+ compare (PlainL _) _ = LT
+ compare _ (PlainL _) = GT
+ compare (PlainLL bs1 bs1') (PlainLL bs2 bs2') =
+ case compare bs1' bs2' of
+ EQ -> compare bs1 bs2
+ GT -> GT
+ LT -> LT
+ compare (PlainLL _ _) _ = LT
+ compare _ (PlainLL _ _) = GT
+ compare (TypedL l1 t1) (TypedL l2 t2) =
+ case compareFS t1 t2 of
+ EQ -> compare l1 l2
+ GT -> GT
+ LT -> LT
-- String representations of the various data types; generally NTriples-like.
File renamed without changes.
@@ -1,6 +1,6 @@
-- |A simple graph implementation backed by 'Data.Map'.
-module Text.RDF.RDF4H.MGraph (
+module Data.RDF.MGraph (
MGraph,
empty,
mkRdf,
@@ -151,10 +151,10 @@ q1 Nothing p o spoMap = Set.unions $ map (q2 p o) $ Map.toList spoMap
q2 :: Maybe Node -> Maybe Node -> (Node, Map Node (Set Node)) -> Set (Node, Node, Node)
q2 (Just p) o (s, pmap) =
- case p `Map.member` pmap of
- False -> Set.empty
- True -> Set.map (\(p', o') -> (s, p', o')) $
+ if p `Map.member` pmap
+ then Set.map (\(p', o') -> (s, p', o')) $
q3 o (p, Map.findWithDefault undefined p pmap)
+ else Set.empty
q2 Nothing o (s, pmap) = Set.map (\(x,y) -> (s,x,y)) $ Set.unions $ map (q3 o) opmaps
where opmaps ::[(Node, Set Node)]
opmaps = Map.toList pmap
@@ -79,7 +79,7 @@ instance Show PrefixMappings where
-- worth optimizing yet.
show (PrefixMappings pmap) = printf "PrefixMappings [%s]" mappingsStr
where showPM = show . PrefixMapping
- mappingsStr = concat $ List.intersperse ", " $ map showPM (Map.toList pmap)
+ mappingsStr = List.intercalate ", " (map showPM (Map.toList pmap))
-- |Perform a left-biased merge of the two sets of prefix mappings.
mergePrefixMappings :: PrefixMappings -> PrefixMappings -> PrefixMappings
@@ -8,7 +8,7 @@
-- functions of this graph (select, query) remove duplicates from their
-- result triples (but triplesOf does not) since it is usually cheap
-- to do so.
-module Text.RDF.RDF4H.TriplesGraph (
+module Data.RDF.TriplesGraph (
TriplesGraph,
empty,
mkRdf,
View
@@ -11,6 +11,7 @@ import qualified Data.ByteString.Lazy as BL
import Data.ByteString.Lazy.Char8(ByteString)
import qualified Data.ByteString.Lazy.Char8 as B
+import Data.Ord (comparing)
import Data.Map(Map)
import qualified Data.Map as Map
@@ -60,14 +61,14 @@ equalFS fs1 fs2 = uniq fs1 == uniq fs2
-- and are otherwise ordered using the natural ordering of 'ByteString' in the
-- internal (reversed) representation.
instance Ord FastString where
- compare fs1 fs2 = compareFS fs1 fs2
+ compare = compareFS
{-# INLINE compareFS #-}
compareFS :: FastString -> FastString -> Ordering
compareFS fs1 fs2 =
- case uniq fs1 == uniq fs2 of
- True -> EQ
- False -> compare (value fs1) (value fs2)
+ if uniq fs1 == uniq fs2
+ then EQ
+ else comparing value fs1 fs2
-- |A convenience function for converting from a bytestring to a string.
{-# INLINE b2s #-}
View
@@ -1,7 +1,8 @@
module Main where
import Data.RDF
-import Text.RDF.RDF4H.TriplesGraph
+import Data.RDF.TriplesGraph
+import Text.RDF.RDF4H.ParserUtils (parseFile, parseURL)
import Text.RDF.RDF4H.NTriplesParser (parseNTriplesRDF)
import Text.RDF.RDF4H.TurtleParser (parseTurtleRDF)
import Text.RDF.RDF4H.NTriplesSerializer
@@ -27,9 +28,9 @@ import Text.Printf(hPrintf)
main :: IO ()
main =
do (opts, args) <- (getArgs >>= compilerOpts)
- when (elem Help opts)
+ when (Help `elem` opts)
(putStrLn (usageInfo header options) >> exitWith ExitSuccess)
- when (elem Version opts)
+ when (Version `elem` opts)
(putStrLn version >> exitWith ExitSuccess)
when (null args)
(ioError (userError ("\n\n" ++ "INPUT-URI required\n\n" ++ usageInfo header options)))
@@ -39,7 +40,7 @@ main =
outputFormat = getWithDefault (OutputFormat "ntriples") opts
inputBaseUri = getInputBaseUri inputUri args opts
outputBaseUri = getWithDefault (OutputBaseUri inputBaseUri) opts
- unless (outputFormat == "ntriples" || outputFormat == "turtle")
+ unless (outputFormat `elem` ["ntriples", "turtle"])
(hPrintf stderr ("'" ++ outputFormat ++ "' is not a valid output format. Supported output formats are: ntriples, turtle\n") >> exitWith (ExitFailure 1))
when debug
(hPrintf stderr " INPUT-URI: %s\n\n" inputUri >>
@@ -56,13 +57,13 @@ main =
>>= \(res :: Either ParseFailure TriplesGraph) -> write outputFormat docUri emptyPms res
("turtle", False) -> (if inputUri /= "-"
then parseFile (parseTurtleRDF mInputUri docUri) inputUri
- else B.getContents >>= return . (parseTurtleRDF mInputUri docUri))
+ else fmap (parseTurtleRDF mInputUri docUri) B.getContents)
>>= \(res :: Either ParseFailure TriplesGraph) -> write outputFormat docUri emptyPms res
("ntriples", True) -> parseURL parseNTriplesRDF inputUri
>>= \(res :: Either ParseFailure TriplesGraph) -> write outputFormat Nothing emptyPms res
("ntriples", False) -> (if inputUri /= "-"
then parseFile parseNTriplesRDF inputUri
- else B.getContents >>= return . parseNTriplesRDF)
+ else fmap parseNTriplesRDF B.getContents)
>>= \(res :: Either ParseFailure TriplesGraph) -> write outputFormat Nothing emptyPms res
(str , _ ) -> putStrLn ("Invalid format: " ++ str) >> exitFailure
@@ -85,9 +86,9 @@ write format docUri pms res =
-- arg is silently discarded.
getInputBaseUri :: String -> [String] -> [Flag] -> String
getInputBaseUri inputUri args flags =
- case (null $ tail args) of
- True -> getWithDefault (InputBaseUri inputUri) flags
- False -> getWithDefault (InputBaseUri (head $ tail args)) flags
+ if null $ tail args
+ then getWithDefault (InputBaseUri inputUri) flags
+ else getWithDefault (InputBaseUri (head $ tail args)) flags
-- Determine if the bytestring represents a URI, which is currently
-- decided solely by checking for a colon in the string.
@@ -20,8 +20,9 @@ import qualified Data.ByteString.Lazy.Char8 as B
import Data.RDF
import Data.RDF.Utils()
-import Text.RDF.RDF4H.TriplesGraph()
-import Text.RDF.RDF4H.MGraph()
+import Data.RDF.TriplesGraph()
+import Data.RDF.MGraph()
+import Text.RDF.RDF4H.ParserUtils (parseFile, parseURL)
import Text.RDF.RDF4H.NTriplesParser (parseNTriplesRDF)
import Text.RDF.RDF4H.TurtleParser (parseTurtleRDF)
import Text.RDF.RDF4H.NTriplesSerializer()
@@ -46,7 +47,7 @@ parseTurtleString :: forall rdf. (RDF rdf) => Maybe String -> Maybe String -> By
parseTurtleString baseUrl docUri = _parse (mkTurtleParser baseUrl docUri)
mkTurtleParser :: forall rdf. (RDF rdf)
- => Maybe String -> Maybe String -> (ByteString -> Either ParseFailure rdf)
+ => Maybe String -> Maybe String -> ByteString -> Either ParseFailure rdf
mkTurtleParser b d = parseTurtleRDF ((BaseUrl . B.pack) `fmap` b) (B.pack `fmap` d)
-- |Load an NTriples file from the filesystem.
@@ -67,7 +68,7 @@ parseNTriplesString = _parse parseNTriplesRDF
-- |Print a list of triples to stdout; useful for debugging and interactive use.
printTriples :: Triples -> IO ()
-printTriples = mapM_ (putStrLn . show)
+printTriples = mapM_ print
-- Load a graph using the given parseFunc, parser, and the location (filesystem path
-- or HTTP URL), calling error with the 'ParseFailure' message if unable to load
Oops, something went wrong.

0 comments on commit 8a09933

Please sign in to comment.