module Data.RDF.Graph.TList (TList) where
import Prelude hiding (pred)
import Control.DeepSeq (NFData)
import Data.Binary
import qualified Data.Map as Map
import Data.RDF.Namespace
import Data.RDF.Query
import Data.RDF.Types (RDF,Rdf(..),Triple,Node,Subject,Predicate,Object,NodeSelector,Triples,BaseUrl)
import Data.List (nub)
import GHC.Generics
data TList deriving (Generic)
instance Binary TList
instance NFData TList
data instance RDF TList = TListC (Triples, Maybe BaseUrl, PrefixMappings)
deriving (Generic,NFData)
instance Rdf TList where
baseUrl = baseUrl'
prefixMappings = prefixMappings'
addPrefixMappings = addPrefixMappings'
empty = empty'
mkRdf = mkRdf'
addTriple = addTriple'
removeTriple = removeTriple'
triplesOf = triplesOf'
uniqTriplesOf = uniqTriplesOf'
select = select'
query = query'
showGraph = showGraph'
showGraph' :: RDF TList -> [Char]
showGraph' gr = concatMap (\t -> show t ++ "\n") (expandTriples gr)
prefixMappings' :: RDF TList -> PrefixMappings
prefixMappings' (TListC(_, _, pms)) = pms
addPrefixMappings' :: RDF TList -> PrefixMappings -> Bool -> RDF TList
addPrefixMappings' (TListC(ts, baseURL, pms)) pms' replace =
let merge = if replace then flip mergePrefixMappings else mergePrefixMappings
in TListC(ts, baseURL, merge pms pms')
baseUrl' :: RDF TList -> Maybe BaseUrl
baseUrl' (TListC(_, baseURL, _)) = baseURL
empty' :: RDF TList
empty' = TListC([], Nothing, PrefixMappings Map.empty)
mkRdf' :: Triples -> Maybe BaseUrl -> PrefixMappings -> RDF TList
mkRdf' ts baseURL pms = TListC (ts, baseURL, pms)
addTriple' :: RDF TList -> Triple -> RDF TList
addTriple' (TListC (ts, bURL, preMapping)) t = TListC (t:ts,bURL,preMapping)
removeTriple' :: RDF TList -> Triple -> RDF TList
removeTriple' (TListC (ts, bURL, preMapping)) t =
TListC (newTs,bURL,preMapping)
where
newTs = filter (/= t) ts
triplesOf' :: RDF TList -> Triples
triplesOf' ((TListC(ts, _, _))) = ts
uniqTriplesOf' :: RDF TList -> Triples
uniqTriplesOf' = nub . expandTriples
select' :: RDF TList -> NodeSelector -> NodeSelector -> NodeSelector -> Triples
select' g s p o = filter (matchSelect s p o) $ triplesOf g
query' :: RDF TList -> Maybe Subject -> Maybe Predicate -> Maybe Object -> Triples
query' g s p o = filter (matchPattern s p o) $ triplesOf g
matchSelect :: NodeSelector -> NodeSelector -> NodeSelector -> Triple -> Bool
matchSelect s p o t =
match s (subjectOf t) && match p (predicateOf t) && match o (objectOf t)
where
match Nothing _ = True
match (Just fn) n = fn n
matchPattern :: Maybe Subject -> Maybe Predicate -> Maybe Object -> Triple -> Bool
matchPattern subj pred obj t = smatch t && pmatch t && omatch t
where
smatch trp = matchNode subj (subjectOf trp)
pmatch trp = matchNode pred (predicateOf trp)
omatch trp = matchNode obj (objectOf trp)
matchNode :: Maybe Node -> Node -> Bool
matchNode Nothing _ = True
matchNode (Just n1) n2 = n1 == n2