{-# LANGUAGE CPP #-}
module Swish.RDF.Query
( rdfQueryFind, rdfQueryFilter
, rdfQueryBack, rdfQueryBackFilter, rdfQueryBackModify
, rdfQueryInstance
, rdfQuerySubs, rdfQueryBackSubs
, rdfQuerySubsAll
, rdfQuerySubsBlank, rdfQueryBackSubsBlank
, rdfFindArcs, rdfSubjEq, rdfPredEq, rdfObjEq
, rdfFindPredVal, rdfFindPredInt, rdfFindValSubj
, rdfFindList
, allp
, anyp
, rdfQuerySubs2 )
where
import Swish.Datatype (DatatypeMap(..))
import Swish.VarBinding (VarBinding(..), VarBindingModify(..), VarBindingFilter(..))
import Swish.VarBinding (makeVarBinding, applyVarBinding, joinVarBindings)
import Swish.RDF.Graph
( Arc(..), LDGraph(..)
, arcSubj, arcPred, arcObj
, RDFLabel(..)
, isDatatyped, isBlank, isQueryVar
, getLiteralText, makeBlank
, RDFTriple
, RDFGraph
, allLabels, remapLabels
, resRdfFirst
, resRdfRest
, resRdfNil
, traverseNSGraph
)
import Swish.RDF.VarBinding (RDFVarBinding, RDFVarBindingFilter)
import Swish.RDF.VarBinding (nullRDFVarBinding)
import Swish.RDF.Datatype.XSD.MapInteger (mapXsdInteger)
import Swish.RDF.Vocabulary (xsdInteger, xsdNonNegInteger)
import Swish.Utils.ListHelpers (flist)
import Control.Monad (when)
import Control.Monad.State (State, runState, modify)
import Data.Maybe (mapMaybe, isJust, fromJust)
#if (!defined(__GLASGOW_HASKELL__)) || (__GLASGOW_HASKELL__ < 710)
import Data.Monoid (Monoid(..))
#endif
import qualified Data.Set as S
getTriples :: RDFGraph -> [RDFTriple]
getTriples :: RDFGraph -> [RDFTriple]
getTriples = Set RDFTriple -> [RDFTriple]
forall a. Set a -> [a]
S.toList (Set RDFTriple -> [RDFTriple])
-> (RDFGraph -> Set RDFTriple) -> RDFGraph -> [RDFTriple]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RDFGraph -> Set RDFTriple
forall (lg :: * -> *) lb. LDGraph lg lb => lg lb -> ArcSet lb
getArcs
rdfQueryFind ::
RDFGraph
-> RDFGraph
-> [RDFVarBinding]
rdfQueryFind :: RDFGraph -> RDFGraph -> [RDFVarBinding]
rdfQueryFind =
NodeQuery RDFLabel
-> RDFVarBinding -> [RDFTriple] -> RDFGraph -> [RDFVarBinding]
rdfQueryPrim1 NodeQuery RDFLabel
matchQueryVariable RDFVarBinding
nullRDFVarBinding ([RDFTriple] -> RDFGraph -> [RDFVarBinding])
-> (RDFGraph -> [RDFTriple])
-> RDFGraph
-> RDFGraph
-> [RDFVarBinding]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RDFGraph -> [RDFTriple]
getTriples
rdfQueryPrim1 ::
NodeQuery RDFLabel -> RDFVarBinding -> [Arc RDFLabel]
-> RDFGraph
-> [RDFVarBinding]
rdfQueryPrim1 :: NodeQuery RDFLabel
-> RDFVarBinding -> [RDFTriple] -> RDFGraph -> [RDFVarBinding]
rdfQueryPrim1 NodeQuery RDFLabel
_ RDFVarBinding
initv [] RDFGraph
_ = [RDFVarBinding
initv]
rdfQueryPrim1 NodeQuery RDFLabel
nodeq RDFVarBinding
initv (RDFTriple
qa:[RDFTriple]
qas) RDFGraph
tg =
let qam :: RDFTriple
qam = (RDFLabel -> RDFLabel) -> RDFTriple -> RDFTriple
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (RDFVarBinding -> RDFLabel -> RDFLabel
forall a. VarBinding a a -> a -> a
applyVarBinding RDFVarBinding
initv) RDFTriple
qa
newv :: [RDFVarBinding]
newv = NodeQuery RDFLabel -> RDFTriple -> RDFGraph -> [RDFVarBinding]
rdfQueryPrim2 NodeQuery RDFLabel
nodeq RDFTriple
qam RDFGraph
tg
in [[RDFVarBinding]] -> [RDFVarBinding]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
[ NodeQuery RDFLabel
-> RDFVarBinding -> [RDFTriple] -> RDFGraph -> [RDFVarBinding]
rdfQueryPrim1 NodeQuery RDFLabel
nodeq RDFVarBinding
v2 [RDFTriple]
qas RDFGraph
tg
| RDFVarBinding
v1 <- [RDFVarBinding]
newv
, let v2 :: RDFVarBinding
v2 = RDFVarBinding -> RDFVarBinding -> RDFVarBinding
forall a b.
(Ord a, Ord b) =>
VarBinding a b -> VarBinding a b -> VarBinding a b
joinVarBindings RDFVarBinding
initv RDFVarBinding
v1
]
rdfQueryPrim2 ::
NodeQuery RDFLabel -> Arc RDFLabel
-> RDFGraph
-> [RDFVarBinding]
rdfQueryPrim2 :: NodeQuery RDFLabel -> RDFTriple -> RDFGraph -> [RDFVarBinding]
rdfQueryPrim2 NodeQuery RDFLabel
nodeq RDFTriple
qa RDFGraph
tg =
(RDFTriple -> Maybe RDFVarBinding)
-> [RDFTriple] -> [RDFVarBinding]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (NodeQuery RDFLabel -> RDFTriple -> RDFTriple -> Maybe RDFVarBinding
getBinding NodeQuery RDFLabel
nodeq RDFTriple
qa) (Set RDFTriple -> [RDFTriple]
forall a. Set a -> [a]
S.toList (Set RDFTriple -> [RDFTriple]) -> Set RDFTriple -> [RDFTriple]
forall a b. (a -> b) -> a -> b
$ RDFGraph -> Set RDFTriple
forall (lg :: * -> *) lb. LDGraph lg lb => lg lb -> ArcSet lb
getArcs RDFGraph
tg)
rdfQueryFilter ::
RDFVarBindingFilter -> [RDFVarBinding] -> [RDFVarBinding]
rdfQueryFilter :: RDFVarBindingFilter -> [RDFVarBinding] -> [RDFVarBinding]
rdfQueryFilter RDFVarBindingFilter
qbf = (RDFVarBinding -> Bool) -> [RDFVarBinding] -> [RDFVarBinding]
forall a. (a -> Bool) -> [a] -> [a]
filter (RDFVarBindingFilter -> RDFVarBinding -> Bool
forall a b. VarBindingFilter a b -> VarBinding a b -> Bool
vbfTest RDFVarBindingFilter
qbf)
rdfQueryBack ::
RDFGraph
-> RDFGraph
-> [[RDFVarBinding]]
rdfQueryBack :: RDFGraph -> RDFGraph -> [[RDFVarBinding]]
rdfQueryBack RDFGraph
qg RDFGraph
tg =
let ga :: RDFGraph -> [RDFTriple]
ga = RDFGraph -> [RDFTriple]
getTriples
in NodeQuery RDFLabel
-> [RDFVarBinding]
-> [RDFTriple]
-> [RDFTriple]
-> [[RDFVarBinding]]
rdfQueryBack1 NodeQuery RDFLabel
matchQueryVariable [] (RDFGraph -> [RDFTriple]
ga RDFGraph
qg) (RDFGraph -> [RDFTriple]
ga RDFGraph
tg)
rdfQueryBack1 ::
NodeQuery RDFLabel -> [RDFVarBinding] -> [Arc RDFLabel] -> [Arc RDFLabel]
-> [[RDFVarBinding]]
rdfQueryBack1 :: NodeQuery RDFLabel
-> [RDFVarBinding]
-> [RDFTriple]
-> [RDFTriple]
-> [[RDFVarBinding]]
rdfQueryBack1 NodeQuery RDFLabel
_ [RDFVarBinding]
initv [RDFTriple]
_ [] = [[RDFVarBinding]
initv]
rdfQueryBack1 NodeQuery RDFLabel
nodeq [RDFVarBinding]
initv [RDFTriple]
qas (RDFTriple
ta:[RDFTriple]
tas) = [[[RDFVarBinding]]] -> [[RDFVarBinding]]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
[ NodeQuery RDFLabel
-> [RDFVarBinding]
-> [RDFTriple]
-> [RDFTriple]
-> [[RDFVarBinding]]
rdfQueryBack1 NodeQuery RDFLabel
nodeq (RDFVarBinding
nvRDFVarBinding -> [RDFVarBinding] -> [RDFVarBinding]
forall a. a -> [a] -> [a]
:[RDFVarBinding]
initv) [RDFTriple]
qas [RDFTriple]
tas
| RDFVarBinding
nv <- NodeQuery RDFLabel -> [RDFTriple] -> RDFTriple -> [RDFVarBinding]
rdfQueryBack2 NodeQuery RDFLabel
nodeq [RDFTriple]
qas RDFTriple
ta ]
rdfQueryBack2 ::
NodeQuery RDFLabel -> [Arc RDFLabel] -> Arc RDFLabel
-> [RDFVarBinding]
rdfQueryBack2 :: NodeQuery RDFLabel -> [RDFTriple] -> RDFTriple -> [RDFVarBinding]
rdfQueryBack2 NodeQuery RDFLabel
nodeq [RDFTriple]
qas RDFTriple
ta =
[ Maybe RDFVarBinding -> RDFVarBinding
forall a. HasCallStack => Maybe a -> a
fromJust Maybe RDFVarBinding
b | RDFTriple
qa <- [RDFTriple]
qas, let b :: Maybe RDFVarBinding
b = NodeQuery RDFLabel -> RDFTriple -> RDFTriple -> Maybe RDFVarBinding
getBinding NodeQuery RDFLabel
nodeq RDFTriple
qa RDFTriple
ta, Maybe RDFVarBinding -> Bool
forall a. Maybe a -> Bool
isJust Maybe RDFVarBinding
b ]
rdfQueryBackFilter ::
RDFVarBindingFilter -> [[RDFVarBinding]] -> [[RDFVarBinding]]
rdfQueryBackFilter :: RDFVarBindingFilter -> [[RDFVarBinding]] -> [[RDFVarBinding]]
rdfQueryBackFilter RDFVarBindingFilter
qbf = ([RDFVarBinding] -> Bool) -> [[RDFVarBinding]] -> [[RDFVarBinding]]
forall a. (a -> Bool) -> [a] -> [a]
filter ((RDFVarBinding -> Bool) -> [RDFVarBinding] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (RDFVarBindingFilter -> RDFVarBinding -> Bool
forall a b. VarBindingFilter a b -> VarBinding a b -> Bool
vbfTest RDFVarBindingFilter
qbf))
rdfQueryBackModify ::
VarBindingModify a b -> [[VarBinding a b]] -> [[VarBinding a b]]
rdfQueryBackModify :: VarBindingModify a b -> [[VarBinding a b]] -> [[VarBinding a b]]
rdfQueryBackModify VarBindingModify a b
qbm = ([VarBinding a b] -> [[VarBinding a b]])
-> [[VarBinding a b]] -> [[VarBinding a b]]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (VarBindingModify a b -> [VarBinding a b] -> [[VarBinding a b]]
forall a b.
VarBindingModify a b -> [VarBinding a b] -> [[VarBinding a b]]
rdfQueryBackModify1 VarBindingModify a b
qbm)
rdfQueryBackModify1 ::
VarBindingModify a b -> [VarBinding a b] -> [[VarBinding a b]]
rdfQueryBackModify1 :: VarBindingModify a b -> [VarBinding a b] -> [[VarBinding a b]]
rdfQueryBackModify1 VarBindingModify a b
qbm = (VarBinding a b -> [VarBinding a b])
-> [VarBinding a b] -> [[VarBinding a b]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (VarBindingModify a b -> [VarBinding a b] -> [VarBinding a b]
forall a b.
VarBindingModify a b -> [VarBinding a b] -> [VarBinding a b]
vbmApply VarBindingModify a b
qbm ([VarBinding a b] -> [VarBinding a b])
-> (VarBinding a b -> [VarBinding a b])
-> VarBinding a b
-> [VarBinding a b]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (VarBinding a b -> [VarBinding a b] -> [VarBinding a b]
forall a. a -> [a] -> [a]
:[]))
rdfQueryInstance :: RDFGraph -> RDFGraph -> [RDFVarBinding]
rdfQueryInstance :: RDFGraph -> RDFGraph -> [RDFVarBinding]
rdfQueryInstance =
NodeQuery RDFLabel
-> RDFVarBinding -> [RDFTriple] -> RDFGraph -> [RDFVarBinding]
rdfQueryPrim1 NodeQuery RDFLabel
matchQueryBnode RDFVarBinding
nullRDFVarBinding ([RDFTriple] -> RDFGraph -> [RDFVarBinding])
-> (RDFGraph -> [RDFTriple])
-> RDFGraph
-> RDFGraph
-> [RDFVarBinding]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RDFGraph -> [RDFTriple]
getTriples
type NodeQuery a = a -> a -> Maybe Bool
getBinding ::
NodeQuery RDFLabel -> Arc RDFLabel -> Arc RDFLabel
-> Maybe RDFVarBinding
getBinding :: NodeQuery RDFLabel -> RDFTriple -> RDFTriple -> Maybe RDFVarBinding
getBinding NodeQuery RDFLabel
nodeq (Arc RDFLabel
s1 RDFLabel
p1 RDFLabel
o1) (Arc RDFLabel
s2 RDFLabel
p2 RDFLabel
o2) =
[(RDFLabel, RDFLabel)]
-> [(RDFLabel, RDFLabel)] -> Maybe RDFVarBinding
makeBinding [(RDFLabel
s1,RDFLabel
s2),(RDFLabel
p1,RDFLabel
p2),(RDFLabel
o1,RDFLabel
o2)] []
where
makeBinding :: [(RDFLabel, RDFLabel)]
-> [(RDFLabel, RDFLabel)] -> Maybe RDFVarBinding
makeBinding [] [(RDFLabel, RDFLabel)]
bs = RDFVarBinding -> Maybe RDFVarBinding
forall a. a -> Maybe a
Just (RDFVarBinding -> Maybe RDFVarBinding)
-> RDFVarBinding -> Maybe RDFVarBinding
forall a b. (a -> b) -> a -> b
$ [(RDFLabel, RDFLabel)] -> RDFVarBinding
forall a b. (Ord a, Ord b) => [(a, b)] -> VarBinding a b
makeVarBinding [(RDFLabel, RDFLabel)]
bs
makeBinding (vr :: (RDFLabel, RDFLabel)
vr@(RDFLabel
v,RDFLabel
r):[(RDFLabel, RDFLabel)]
bvrs) [(RDFLabel, RDFLabel)]
bs =
case NodeQuery RDFLabel
nodeq RDFLabel
v RDFLabel
r of
Maybe Bool
Nothing -> Maybe RDFVarBinding
forall a. Maybe a
Nothing
Just Bool
False -> [(RDFLabel, RDFLabel)]
-> [(RDFLabel, RDFLabel)] -> Maybe RDFVarBinding
makeBinding [(RDFLabel, RDFLabel)]
bvrs [(RDFLabel, RDFLabel)]
bs
Just Bool
True -> [(RDFLabel, RDFLabel)]
-> [(RDFLabel, RDFLabel)] -> Maybe RDFVarBinding
makeBinding [(RDFLabel, RDFLabel)]
bvrs ((RDFLabel, RDFLabel)
vr(RDFLabel, RDFLabel)
-> [(RDFLabel, RDFLabel)] -> [(RDFLabel, RDFLabel)]
forall a. a -> [a] -> [a]
:[(RDFLabel, RDFLabel)]
bs)
matchQueryVariable :: NodeQuery RDFLabel
matchQueryVariable :: NodeQuery RDFLabel
matchQueryVariable (Var String
_) RDFLabel
_ = Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
True
matchQueryVariable RDFLabel
q RDFLabel
t
| RDFLabel
q RDFLabel -> RDFLabel -> Bool
forall a. Eq a => a -> a -> Bool
== RDFLabel
t = Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
False
| Bool
otherwise = Maybe Bool
forall a. Maybe a
Nothing
matchQueryBnode :: NodeQuery RDFLabel
matchQueryBnode :: NodeQuery RDFLabel
matchQueryBnode (Blank String
_) RDFLabel
_ = Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
True
matchQueryBnode RDFLabel
q RDFLabel
t
| RDFLabel
q RDFLabel -> RDFLabel -> Bool
forall a. Eq a => a -> a -> Bool
== RDFLabel
t = Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
False
| Bool
otherwise = Maybe Bool
forall a. Maybe a
Nothing
rdfQuerySubs :: [RDFVarBinding] -> RDFGraph -> [RDFGraph]
rdfQuerySubs :: [RDFVarBinding] -> RDFGraph -> [RDFGraph]
rdfQuerySubs [RDFVarBinding]
vars RDFGraph
gr =
((RDFGraph, [RDFLabel]) -> RDFGraph)
-> [(RDFGraph, [RDFLabel])] -> [RDFGraph]
forall a b. (a -> b) -> [a] -> [b]
map (RDFGraph, [RDFLabel]) -> RDFGraph
forall a b. (a, b) -> a
fst ([(RDFGraph, [RDFLabel])] -> [RDFGraph])
-> [(RDFGraph, [RDFLabel])] -> [RDFGraph]
forall a b. (a -> b) -> a -> b
$ ((RDFGraph, [RDFLabel]) -> Bool)
-> [(RDFGraph, [RDFLabel])] -> [(RDFGraph, [RDFLabel])]
forall a. (a -> Bool) -> [a] -> [a]
filter ([RDFLabel] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([RDFLabel] -> Bool)
-> ((RDFGraph, [RDFLabel]) -> [RDFLabel])
-> (RDFGraph, [RDFLabel])
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (RDFGraph, [RDFLabel]) -> [RDFLabel]
forall a b. (a, b) -> b
snd) ([(RDFGraph, [RDFLabel])] -> [(RDFGraph, [RDFLabel])])
-> [(RDFGraph, [RDFLabel])] -> [(RDFGraph, [RDFLabel])]
forall a b. (a -> b) -> a -> b
$ [RDFVarBinding] -> RDFGraph -> [(RDFGraph, [RDFLabel])]
rdfQuerySubsAll [RDFVarBinding]
vars RDFGraph
gr
rdfQueryBackSubs ::
[[RDFVarBinding]] -> RDFGraph -> [[(RDFGraph,[RDFLabel])]]
rdfQueryBackSubs :: [[RDFVarBinding]] -> RDFGraph -> [[(RDFGraph, [RDFLabel])]]
rdfQueryBackSubs [[RDFVarBinding]]
varss RDFGraph
gr = [ [RDFVarBinding] -> RDFGraph -> [(RDFGraph, [RDFLabel])]
rdfQuerySubsAll [RDFVarBinding]
v RDFGraph
gr | [RDFVarBinding]
v <- [[RDFVarBinding]]
varss ]
rdfQuerySubsAll :: [RDFVarBinding] -> RDFGraph -> [(RDFGraph,[RDFLabel])]
rdfQuerySubsAll :: [RDFVarBinding] -> RDFGraph -> [(RDFGraph, [RDFLabel])]
rdfQuerySubsAll [RDFVarBinding]
vars RDFGraph
gr = [ RDFVarBinding -> RDFGraph -> (RDFGraph, [RDFLabel])
rdfQuerySubs2 RDFVarBinding
v RDFGraph
gr | RDFVarBinding
v <- [RDFVarBinding]
vars ]
rdfQuerySubsBlank :: [RDFVarBinding] -> RDFGraph -> [RDFGraph]
rdfQuerySubsBlank :: [RDFVarBinding] -> RDFGraph -> [RDFGraph]
rdfQuerySubsBlank [RDFVarBinding]
vars RDFGraph
gr =
[ [RDFLabel]
-> [RDFLabel] -> (RDFLabel -> RDFLabel) -> RDFGraph -> RDFGraph
forall lb.
Label lb =>
[lb] -> [lb] -> (lb -> lb) -> NSGraph lb -> NSGraph lb
remapLabels [RDFLabel]
vs [RDFLabel]
bs RDFLabel -> RDFLabel
makeBlank RDFGraph
g
| RDFVarBinding
v <- [RDFVarBinding]
vars
, let (RDFGraph
g,[RDFLabel]
vs) = RDFVarBinding -> RDFGraph -> (RDFGraph, [RDFLabel])
rdfQuerySubs2 RDFVarBinding
v RDFGraph
gr
, let bs :: [RDFLabel]
bs = Set RDFLabel -> [RDFLabel]
forall a. Set a -> [a]
S.toList (Set RDFLabel -> [RDFLabel]) -> Set RDFLabel -> [RDFLabel]
forall a b. (a -> b) -> a -> b
$ (RDFLabel -> Bool) -> RDFGraph -> Set RDFLabel
forall lb. Label lb => (lb -> Bool) -> NSGraph lb -> Set lb
allLabels RDFLabel -> Bool
isBlank RDFGraph
g
]
rdfQueryBackSubsBlank :: [[RDFVarBinding]] -> RDFGraph -> [[RDFGraph]]
rdfQueryBackSubsBlank :: [[RDFVarBinding]] -> RDFGraph -> [[RDFGraph]]
rdfQueryBackSubsBlank [[RDFVarBinding]]
varss RDFGraph
gr = [ [RDFVarBinding] -> RDFGraph -> [RDFGraph]
rdfQuerySubsBlank [RDFVarBinding]
v RDFGraph
gr | [RDFVarBinding]
v <- [[RDFVarBinding]]
varss ]
rdfQuerySubs2 :: RDFVarBinding -> RDFGraph -> (RDFGraph, [RDFLabel])
rdfQuerySubs2 :: RDFVarBinding -> RDFGraph -> (RDFGraph, [RDFLabel])
rdfQuerySubs2 RDFVarBinding
varb RDFGraph
gr = (RDFGraph -> RDFGraph -> RDFGraph
forall (lg :: * -> *) lb.
(LDGraph lg lb, Ord lb) =>
lg lb -> lg lb -> lg lb
addGraphs RDFGraph
forall a. Monoid a => a
mempty RDFGraph
g, Set RDFLabel -> [RDFLabel]
forall a. Set a -> [a]
S.toList Set RDFLabel
vs)
where
(RDFGraph
g,Set RDFLabel
vs) = State (Set RDFLabel) RDFGraph
-> Set RDFLabel -> (RDFGraph, Set RDFLabel)
forall s a. State s a -> s -> (a, s)
runState ((RDFLabel -> StateT (Set RDFLabel) Identity RDFLabel)
-> RDFGraph -> State (Set RDFLabel) RDFGraph
forall (f :: * -> *) a.
(Applicative f, Ord a) =>
(a -> f a) -> NSGraph a -> f (NSGraph a)
traverseNSGraph (RDFVarBinding
-> RDFLabel -> StateT (Set RDFLabel) Identity RDFLabel
mapNode RDFVarBinding
varb) RDFGraph
gr) Set RDFLabel
forall a. Set a
S.empty
mapNode :: RDFVarBinding -> RDFLabel -> State (S.Set RDFLabel) RDFLabel
mapNode :: RDFVarBinding
-> RDFLabel -> StateT (Set RDFLabel) Identity RDFLabel
mapNode RDFVarBinding
varb RDFLabel
lab =
case RDFVarBinding -> RDFLabel -> Maybe RDFLabel
forall a b. VarBinding a b -> a -> Maybe b
vbMap RDFVarBinding
varb RDFLabel
lab of
Just RDFLabel
v -> RDFLabel -> StateT (Set RDFLabel) Identity RDFLabel
forall (m :: * -> *) a. Monad m => a -> m a
return RDFLabel
v
Maybe RDFLabel
Nothing -> Bool
-> StateT (Set RDFLabel) Identity ()
-> StateT (Set RDFLabel) Identity ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (RDFLabel -> Bool
isQueryVar RDFLabel
lab) ((Set RDFLabel -> Set RDFLabel) -> StateT (Set RDFLabel) Identity ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify (RDFLabel -> Set RDFLabel -> Set RDFLabel
forall a. Ord a => a -> Set a -> Set a
S.insert RDFLabel
lab)) StateT (Set RDFLabel) Identity ()
-> StateT (Set RDFLabel) Identity RDFLabel
-> StateT (Set RDFLabel) Identity RDFLabel
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> RDFLabel -> StateT (Set RDFLabel) Identity RDFLabel
forall (m :: * -> *) a. Monad m => a -> m a
return RDFLabel
lab
allp :: [a->Bool] -> a -> Bool
allp :: [a -> Bool] -> a -> Bool
allp [a -> Bool]
ps a
a = [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
and ([a -> Bool] -> a -> [Bool]
forall a b. [a -> b] -> a -> [b]
flist [a -> Bool]
ps a
a)
anyp :: [a->Bool] -> a -> Bool
anyp :: [a -> Bool] -> a -> Bool
anyp [a -> Bool]
ps a
a = [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
or ([a -> Bool] -> a -> [Bool]
forall a b. [a -> b] -> a -> [b]
flist [a -> Bool]
ps a
a)
rdfFindArcs :: (RDFTriple -> Bool) -> RDFGraph -> [RDFTriple]
rdfFindArcs :: (RDFTriple -> Bool) -> RDFGraph -> [RDFTriple]
rdfFindArcs RDFTriple -> Bool
p = Set RDFTriple -> [RDFTriple]
forall a. Set a -> [a]
S.toList (Set RDFTriple -> [RDFTriple])
-> (RDFGraph -> Set RDFTriple) -> RDFGraph -> [RDFTriple]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (RDFTriple -> Bool) -> Set RDFTriple -> Set RDFTriple
forall a. (a -> Bool) -> Set a -> Set a
S.filter RDFTriple -> Bool
p (Set RDFTriple -> Set RDFTriple)
-> (RDFGraph -> Set RDFTriple) -> RDFGraph -> Set RDFTriple
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RDFGraph -> Set RDFTriple
forall (lg :: * -> *) lb. LDGraph lg lb => lg lb -> ArcSet lb
getArcs
rdfSubjEq :: RDFLabel -> RDFTriple -> Bool
rdfSubjEq :: RDFLabel -> RDFTriple -> Bool
rdfSubjEq RDFLabel
s = (RDFLabel
sRDFLabel -> RDFLabel -> Bool
forall a. Eq a => a -> a -> Bool
==) (RDFLabel -> Bool) -> (RDFTriple -> RDFLabel) -> RDFTriple -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RDFTriple -> RDFLabel
forall lb. Arc lb -> lb
arcSubj
rdfPredEq :: RDFLabel -> RDFTriple -> Bool
rdfPredEq :: RDFLabel -> RDFTriple -> Bool
rdfPredEq RDFLabel
p = (RDFLabel
pRDFLabel -> RDFLabel -> Bool
forall a. Eq a => a -> a -> Bool
==) (RDFLabel -> Bool) -> (RDFTriple -> RDFLabel) -> RDFTriple -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RDFTriple -> RDFLabel
forall lb. Arc lb -> lb
arcPred
rdfObjEq :: RDFLabel -> RDFTriple -> Bool
rdfObjEq :: RDFLabel -> RDFTriple -> Bool
rdfObjEq RDFLabel
o = (RDFLabel
oRDFLabel -> RDFLabel -> Bool
forall a. Eq a => a -> a -> Bool
==) (RDFLabel -> Bool) -> (RDFTriple -> RDFLabel) -> RDFTriple -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RDFTriple -> RDFLabel
forall lb. Arc lb -> lb
arcObj
rdfFindPredVal ::
RDFLabel
-> RDFLabel
-> RDFGraph
-> [RDFLabel]
rdfFindPredVal :: RDFLabel -> RDFLabel -> RDFGraph -> [RDFLabel]
rdfFindPredVal RDFLabel
s RDFLabel
p = (RDFTriple -> RDFLabel) -> [RDFTriple] -> [RDFLabel]
forall a b. (a -> b) -> [a] -> [b]
map RDFTriple -> RDFLabel
forall lb. Arc lb -> lb
arcObj ([RDFTriple] -> [RDFLabel])
-> (RDFGraph -> [RDFTriple]) -> RDFGraph -> [RDFLabel]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (RDFTriple -> Bool) -> RDFGraph -> [RDFTriple]
rdfFindArcs ([RDFTriple -> Bool] -> RDFTriple -> Bool
forall a. [a -> Bool] -> a -> Bool
allp [RDFLabel -> RDFTriple -> Bool
rdfSubjEq RDFLabel
s,RDFLabel -> RDFTriple -> Bool
rdfPredEq RDFLabel
p])
rdfFindPredInt ::
RDFLabel
-> RDFLabel
-> RDFGraph -> [Integer]
rdfFindPredInt :: RDFLabel -> RDFLabel -> RDFGraph -> [Integer]
rdfFindPredInt RDFLabel
s RDFLabel
p = (RDFLabel -> Maybe Integer) -> [RDFLabel] -> [Integer]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe RDFLabel -> Maybe Integer
getint ([RDFLabel] -> [Integer])
-> (RDFGraph -> [RDFLabel]) -> RDFGraph -> [Integer]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (RDFLabel -> Bool) -> [RDFLabel] -> [RDFLabel]
forall a. (a -> Bool) -> [a] -> [a]
filter RDFLabel -> Bool
isint ([RDFLabel] -> [RDFLabel])
-> (RDFGraph -> [RDFLabel]) -> RDFGraph -> [RDFLabel]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RDFGraph -> [RDFLabel]
pvs
where
pvs :: RDFGraph -> [RDFLabel]
pvs = RDFLabel -> RDFLabel -> RDFGraph -> [RDFLabel]
rdfFindPredVal RDFLabel
s RDFLabel
p
isint :: RDFLabel -> Bool
isint = [RDFLabel -> Bool] -> RDFLabel -> Bool
forall a. [a -> Bool] -> a -> Bool
anyp
[ ScopedName -> RDFLabel -> Bool
isDatatyped ScopedName
xsdInteger
, ScopedName -> RDFLabel -> Bool
isDatatyped ScopedName
xsdNonNegInteger
]
getint :: RDFLabel -> Maybe Integer
getint = DatatypeMap Integer -> Text -> Maybe Integer
forall vt. DatatypeMap vt -> Text -> Maybe vt
mapL2V DatatypeMap Integer
mapXsdInteger (Text -> Maybe Integer)
-> (RDFLabel -> Text) -> RDFLabel -> Maybe Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RDFLabel -> Text
getLiteralText
rdfFindValSubj ::
RDFLabel
-> RDFLabel
-> RDFGraph
-> [RDFLabel]
rdfFindValSubj :: RDFLabel -> RDFLabel -> RDFGraph -> [RDFLabel]
rdfFindValSubj RDFLabel
p RDFLabel
o = (RDFTriple -> RDFLabel) -> [RDFTriple] -> [RDFLabel]
forall a b. (a -> b) -> [a] -> [b]
map RDFTriple -> RDFLabel
forall lb. Arc lb -> lb
arcSubj ([RDFTriple] -> [RDFLabel])
-> (RDFGraph -> [RDFTriple]) -> RDFGraph -> [RDFLabel]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (RDFTriple -> Bool) -> RDFGraph -> [RDFTriple]
rdfFindArcs ([RDFTriple -> Bool] -> RDFTriple -> Bool
forall a. [a -> Bool] -> a -> Bool
allp [RDFLabel -> RDFTriple -> Bool
rdfPredEq RDFLabel
p,RDFLabel -> RDFTriple -> Bool
rdfObjEq RDFLabel
o])
rdfFindList :: RDFGraph -> RDFLabel -> [RDFLabel]
rdfFindList :: RDFGraph -> RDFLabel -> [RDFLabel]
rdfFindList RDFGraph
gr RDFLabel
hd = [RDFLabel] -> [RDFLabel]
findhead ([RDFLabel] -> [RDFLabel]) -> [RDFLabel] -> [RDFLabel]
forall a b. (a -> b) -> a -> b
$ RDFGraph -> RDFLabel -> [RDFLabel]
rdfFindList RDFGraph
gr RDFLabel
findrest
where
findhead :: [RDFLabel] -> [RDFLabel]
findhead = ([RDFLabel] -> [RDFLabel])
-> [[RDFLabel] -> [RDFLabel]] -> [RDFLabel] -> [RDFLabel]
forall b. b -> [b] -> b
headOr ([RDFLabel] -> [RDFLabel] -> [RDFLabel]
forall a b. a -> b -> a
const []) ([[RDFLabel] -> [RDFLabel]] -> [RDFLabel] -> [RDFLabel])
-> [[RDFLabel] -> [RDFLabel]] -> [RDFLabel] -> [RDFLabel]
forall a b. (a -> b) -> a -> b
$
(RDFLabel -> [RDFLabel] -> [RDFLabel])
-> [RDFLabel] -> [[RDFLabel] -> [RDFLabel]]
forall a b. (a -> b) -> [a] -> [b]
map (:) (RDFLabel -> RDFLabel -> RDFGraph -> [RDFLabel]
rdfFindPredVal RDFLabel
hd RDFLabel
resRdfFirst RDFGraph
gr)
findrest :: RDFLabel
findrest = RDFLabel -> [RDFLabel] -> RDFLabel
forall b. b -> [b] -> b
headOr RDFLabel
resRdfNil (RDFLabel -> RDFLabel -> RDFGraph -> [RDFLabel]
rdfFindPredVal RDFLabel
hd RDFLabel
resRdfRest RDFGraph
gr)
headOr :: b -> [b] -> b
headOr = (b -> b -> b) -> b -> [b] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr b -> b -> b
forall a b. a -> b -> a
const