{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
module Swish.RDF.ClassRestrictionRule
( ClassRestriction(..), ClassRestrictionFn
, makeDatatypeRestriction, makeDatatypeRestrictionFn
, makeRDFClassRestrictionRules
, makeRDFDatatypeRestrictionRules
, falseGraph, falseGraphStr
)
where
import Swish.Datatype (DatatypeVal(..), DatatypeRel(..), DatatypeRelFn)
import Swish.Namespace (Namespace, ScopedName, namespaceToBuilder)
import Swish.Rule (Rule(..), bwdCheckInference)
import Swish.VarBinding (VarBinding(..))
import Swish.RDF.Graph
( RDFLabel(..)
, getScopedName
, RDFGraph
, getArcs
, merge
, toRDFGraph, emptyRDFGraph
, Arc(..)
, resRdfType
, resRdfdMaxCardinality
)
import Swish.RDF.Datatype (RDFDatatypeVal, fromRDFLabel, toRDFLabel)
import Swish.RDF.Ruleset (RDFRule, makeRDFGraphFromN3Builder)
import Swish.RDF.Query
( rdfQueryFind
, rdfFindValSubj, rdfFindPredVal, rdfFindPredInt
, rdfFindList
)
import Swish.RDF.VarBinding (RDFVarBinding)
import Swish.RDF.Vocabulary (namespaceRDFD)
#if (!defined(__GLASGOW_HASKELL__)) || (__GLASGOW_HASKELL__ < 808)
import Control.Applicative ((<$>))
#endif
import Data.List (delete, nub, subsequences)
import Data.Maybe (fromJust, fromMaybe, mapMaybe)
import Data.Ord.Partial (minima, maxima, partCompareEq, partComparePair, partCompareListMaybe, partCompareListSubset)
#if (!defined(__GLASGOW_HASKELL__)) || (__GLASGOW_HASKELL__ < 710)
import Data.Monoid (Monoid (..))
#endif
import qualified Data.Map as M
import qualified Data.Set as S
import qualified Data.Text.Lazy.Builder as B
type ClassRestrictionFn = [Maybe RDFLabel] -> Maybe [[RDFLabel]]
data ClassRestriction = ClassRestriction
{ ClassRestriction -> ScopedName
crName :: ScopedName
, ClassRestriction -> ClassRestrictionFn
crFunc :: ClassRestrictionFn
}
instance Eq ClassRestriction where
ClassRestriction
cr1 == :: ClassRestriction -> ClassRestriction -> Bool
== ClassRestriction
cr2 = ClassRestriction -> ScopedName
crName ClassRestriction
cr1 forall a. Eq a => a -> a -> Bool
== ClassRestriction -> ScopedName
crName ClassRestriction
cr2
instance Show ClassRestriction where
show :: ClassRestriction -> String
show ClassRestriction
cr = String
"ClassRestriction:" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show (ClassRestriction -> ScopedName
crName ClassRestriction
cr)
makeDatatypeRestriction ::
RDFDatatypeVal vt -> DatatypeRel vt -> ClassRestriction
makeDatatypeRestriction :: forall vt. RDFDatatypeVal vt -> DatatypeRel vt -> ClassRestriction
makeDatatypeRestriction RDFDatatypeVal vt
dtv DatatypeRel vt
dtrel = ClassRestriction
{ crName :: ScopedName
crName = forall vt. DatatypeRel vt -> ScopedName
dtRelName DatatypeRel vt
dtrel
, crFunc :: ClassRestrictionFn
crFunc = forall vt.
RDFDatatypeVal vt -> DatatypeRelFn vt -> ClassRestrictionFn
makeDatatypeRestrictionFn RDFDatatypeVal vt
dtv (forall vt. DatatypeRel vt -> DatatypeRelFn vt
dtRelFunc DatatypeRel vt
dtrel)
}
makeDatatypeRestrictionFn ::
RDFDatatypeVal vt -> DatatypeRelFn vt -> ClassRestrictionFn
makeDatatypeRestrictionFn :: forall vt.
RDFDatatypeVal vt -> DatatypeRelFn vt -> ClassRestrictionFn
makeDatatypeRestrictionFn RDFDatatypeVal vt
dtv DatatypeRelFn vt
dtrelfn =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe [vt] -> Maybe [RDFLabel]
toLabels) forall b c a. (b -> c) -> (a -> b) -> a -> c
. DatatypeRelFn vt
dtrelfn forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map Maybe RDFLabel -> Maybe vt
frLabel
where
frLabel :: Maybe RDFLabel -> Maybe vt
frLabel Maybe RDFLabel
Nothing = forall a. Maybe a
Nothing
frLabel (Just RDFLabel
l) = forall vt. RDFDatatypeVal vt -> RDFLabel -> Maybe vt
fromRDFLabel RDFDatatypeVal vt
dtv RDFLabel
l
toLabels :: [vt] -> Maybe [RDFLabel]
toLabels = forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM vt -> Maybe RDFLabel
toLabel
toLabel :: vt -> Maybe RDFLabel
toLabel = forall vt. RDFDatatypeVal vt -> vt -> Maybe RDFLabel
toRDFLabel RDFDatatypeVal vt
dtv
mkPrefix :: Namespace -> B.Builder
mkPrefix :: Namespace -> Builder
mkPrefix = Namespace -> Builder
namespaceToBuilder
ruleQuery :: RDFGraph
ruleQuery :: RDFGraph
ruleQuery = Builder -> RDFGraph
makeRDFGraphFromN3Builder forall a b. (a -> b) -> a -> b
$
forall a. Monoid a => [a] -> a
mconcat
[ Namespace -> Builder
mkPrefix Namespace
namespaceRDFD
, Builder
" ?c a rdfd:GeneralRestriction ; "
, Builder
" rdfd:onProperties ?p ; "
, Builder
" rdfd:constraint ?r . "
]
falseGraph :: RDFGraph
falseGraph :: RDFGraph
falseGraph = Builder -> RDFGraph
makeRDFGraphFromN3Builder forall a b. (a -> b) -> a -> b
$
Namespace -> Builder
mkPrefix Namespace
namespaceRDFD forall a. Monoid a => a -> a -> a
`mappend` Builder
falseGraphStr
falseGraphStr :: B.Builder
falseGraphStr :: Builder
falseGraphStr = Builder
"_:a rdfd:false _:b . "
makeRDFClassRestrictionRules :: [ClassRestriction] -> RDFGraph -> [RDFRule]
makeRDFClassRestrictionRules :: [ClassRestriction] -> RDFGraph -> [RDFRule]
makeRDFClassRestrictionRules [ClassRestriction]
crs RDFGraph
gr =
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe RDFVarBinding -> Maybe RDFRule
constructRule (RDFGraph -> [RDFVarBinding]
queryForRules RDFGraph
gr)
where
queryForRules :: RDFGraph -> [RDFVarBinding]
queryForRules = RDFGraph -> RDFGraph -> [RDFVarBinding]
rdfQueryFind RDFGraph
ruleQuery
constructRule :: RDFVarBinding -> Maybe RDFRule
constructRule = [ClassRestriction] -> RDFGraph -> RDFVarBinding -> Maybe RDFRule
makeRestrictionRule1 [ClassRestriction]
crs RDFGraph
gr
makeRestrictionRule1 ::
[ClassRestriction] -> RDFGraph -> RDFVarBinding -> Maybe RDFRule
makeRestrictionRule1 :: [ClassRestriction] -> RDFGraph -> RDFVarBinding -> Maybe RDFRule
makeRestrictionRule1 [ClassRestriction]
crs RDFGraph
gr RDFVarBinding
vb =
Maybe ClassRestriction
-> RDFLabel -> [RDFLabel] -> [Int] -> Maybe RDFRule
makeRestrictionRule2 Maybe ClassRestriction
rn RDFLabel
c [RDFLabel]
ps [Int]
cs
where
c :: RDFLabel
c = forall a. a -> Maybe a -> a
fromMaybe RDFLabel
NoNode forall a b. (a -> b) -> a -> b
$ forall a b. VarBinding a b -> a -> Maybe b
vbMap RDFVarBinding
vb (String -> RDFLabel
Var String
"c")
p :: RDFLabel
p = forall a. a -> Maybe a -> a
fromMaybe RDFLabel
NoNode forall a b. (a -> b) -> a -> b
$ forall a b. VarBinding a b -> a -> Maybe b
vbMap RDFVarBinding
vb (String -> RDFLabel
Var String
"p")
r :: RDFLabel
r = forall a. a -> Maybe a -> a
fromMaybe RDFLabel
NoNode forall a b. (a -> b) -> a -> b
$ forall a b. VarBinding a b -> a -> Maybe b
vbMap RDFVarBinding
vb (String -> RDFLabel
Var String
"r")
cs :: [Int]
cs = forall a. (a -> Bool) -> [a] -> [a]
filter (forall a. Ord a => a -> a -> Bool
> Int
0) forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall a. Num a => Integer -> a
fromInteger forall a b. (a -> b) -> a -> b
$
RDFLabel -> RDFLabel -> RDFGraph -> [Integer]
rdfFindPredInt RDFLabel
c RDFLabel
resRdfdMaxCardinality RDFGraph
gr
ps :: [RDFLabel]
ps = RDFGraph -> RDFLabel -> [RDFLabel]
rdfFindList RDFGraph
gr RDFLabel
p
rn :: Maybe ClassRestriction
rn = forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup (RDFLabel -> ScopedName
getScopedName RDFLabel
r) forall a b. (a -> b) -> a -> b
$ forall k a. Ord k => [(k, a)] -> Map k a
M.fromList forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (\ClassRestriction
cr -> (ClassRestriction -> ScopedName
crName ClassRestriction
cr, ClassRestriction
cr)) [ClassRestriction]
crs
makeRestrictionRule2 ::
Maybe ClassRestriction -> RDFLabel -> [RDFLabel] -> [Int]
-> Maybe RDFRule
makeRestrictionRule2 :: Maybe ClassRestriction
-> RDFLabel -> [RDFLabel] -> [Int] -> Maybe RDFRule
makeRestrictionRule2 (Just ClassRestriction
restriction) cls :: RDFLabel
cls@(Res ScopedName
cname) [RDFLabel]
props [Int]
cs =
forall a. a -> Maybe a
Just RDFRule
restrictionRule
where
restrictionRule :: RDFRule
restrictionRule = Rule
{ ruleName :: ScopedName
ruleName = ScopedName
cname
, fwdApply :: [RDFGraph] -> [RDFGraph]
fwdApply = ClassRestriction
-> RDFLabel -> [RDFLabel] -> [Int] -> [RDFGraph] -> [RDFGraph]
fwdApplyRestriction ClassRestriction
restriction RDFLabel
cls [RDFLabel]
props [Int]
cs
, bwdApply :: RDFGraph -> [[RDFGraph]]
bwdApply = ClassRestriction
-> RDFLabel -> [RDFLabel] -> [Int] -> RDFGraph -> [[RDFGraph]]
bwdApplyRestriction ClassRestriction
restriction RDFLabel
cls [RDFLabel]
props [Int]
cs
, checkInference :: [RDFGraph] -> RDFGraph -> Bool
checkInference = forall ex. Eq ex => Rule ex -> [ex] -> ex -> Bool
bwdCheckInference RDFRule
restrictionRule
}
makeRestrictionRule2 Maybe ClassRestriction
_ RDFLabel
_ [RDFLabel]
_ [Int]
_ = forall a. Maybe a
Nothing
fwdApplyRestriction ::
ClassRestriction -> RDFLabel -> [RDFLabel] -> [Int] -> [RDFGraph]
-> [RDFGraph]
fwdApplyRestriction :: ClassRestriction
-> RDFLabel -> [RDFLabel] -> [Int] -> [RDFGraph] -> [RDFGraph]
fwdApplyRestriction ClassRestriction
restriction RDFLabel
cls [RDFLabel]
props [Int]
cs [RDFGraph]
antgrs =
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [RDFGraph
falseGraph] forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat Maybe [[RDFGraph]]
newgrs
where
ris :: [RDFLabel]
ris = forall a. Eq a => [a] -> [a]
nub forall a b. (a -> b) -> a -> b
$ RDFLabel -> RDFLabel -> RDFGraph -> [RDFLabel]
rdfFindValSubj RDFLabel
resRdfType RDFLabel
cls RDFGraph
antgr
antgr :: RDFGraph
antgr = if forall (t :: * -> *) a. Foldable t => t a -> Bool
null [RDFGraph]
antgrs then RDFGraph
emptyRDFGraph else forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldl1 forall lb. Label lb => NSGraph lb -> NSGraph lb -> NSGraph lb
merge [RDFGraph]
antgrs
newgr :: RDFLabel -> Maybe [RDFGraph]
newgr :: RDFLabel -> Maybe [RDFGraph]
newgr RDFLabel
ri = ClassRestriction
-> RDFLabel -> [RDFLabel] -> [Int] -> RDFGraph -> Maybe [RDFGraph]
fwdApplyRestriction1 ClassRestriction
restriction RDFLabel
ri [RDFLabel]
props [Int]
cs RDFGraph
antgr
newgrs :: Maybe [[RDFGraph]]
newgrs :: Maybe [[RDFGraph]]
newgrs = forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM RDFLabel -> Maybe [RDFGraph]
newgr [RDFLabel]
ris
fwdApplyRestriction1 ::
ClassRestriction -> RDFLabel -> [RDFLabel] -> [Int] -> RDFGraph
-> Maybe [RDFGraph]
fwdApplyRestriction1 :: ClassRestriction
-> RDFLabel -> [RDFLabel] -> [Int] -> RDFGraph -> Maybe [RDFGraph]
fwdApplyRestriction1 ClassRestriction
restriction RDFLabel
ci [RDFLabel]
props [Int]
cs RDFGraph
antgr =
if Bool
grConsistent then forall a. a -> Maybe a
Just [RDFGraph]
newgrs else forall a. Maybe a
Nothing
where
(Bool
grConsistent,[[RDFLabel]]
_,[([Maybe RDFLabel], [[RDFLabel]])]
_,[[Maybe RDFLabel]]
sts) = ClassRestriction
-> RDFLabel
-> [RDFLabel]
-> [Int]
-> RDFGraph
-> (Bool, [[RDFLabel]], [([Maybe RDFLabel], [[RDFLabel]])],
[[Maybe RDFLabel]])
applyRestriction ClassRestriction
restriction RDFLabel
ci [RDFLabel]
props [Int]
cs RDFGraph
antgr
nts :: [[RDFLabel]]
nts :: [[RDFLabel]]
nts = forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence [[Maybe RDFLabel]]
sts
newarcs :: Set (Arc RDFLabel)
newarcs = forall a. Ord a => [a] -> Set a
S.fromList [forall lb. lb -> lb -> lb -> Arc lb
Arc RDFLabel
ci RDFLabel
p RDFLabel
v | [RDFLabel]
vs <- [[RDFLabel]]
nts, (RDFLabel
p,RDFLabel
v) <- forall a b. [a] -> [b] -> [(a, b)]
zip [RDFLabel]
props [RDFLabel]
vs ]
forall a. Ord a => Set a -> Set a -> Set a
`S.difference` forall (lg :: * -> *) lb. LDGraph lg lb => lg lb -> ArcSet lb
getArcs RDFGraph
antgr
newgrs :: [RDFGraph]
newgrs = if forall a. Set a -> Bool
S.null Set (Arc RDFLabel)
newarcs then [] else [Set (Arc RDFLabel) -> RDFGraph
toRDFGraph Set (Arc RDFLabel)
newarcs]
bwdApplyRestriction ::
ClassRestriction -> RDFLabel -> [RDFLabel] -> [Int] -> RDFGraph
-> [[RDFGraph]]
bwdApplyRestriction :: ClassRestriction
-> RDFLabel -> [RDFLabel] -> [Int] -> RDFGraph -> [[RDFGraph]]
bwdApplyRestriction ClassRestriction
restriction RDFLabel
cls [RDFLabel]
props [Int]
cs RDFGraph
congr =
forall a. a -> Maybe a -> a
fromMaybe [[RDFGraph
falseGraph]] Maybe [[RDFGraph]]
newgrs
where
ris :: [RDFLabel]
ris = RDFLabel -> RDFLabel -> RDFGraph -> [RDFLabel]
rdfFindValSubj RDFLabel
resRdfType RDFLabel
cls RDFGraph
congr
newgr :: RDFLabel -> Maybe [[RDFGraph]]
newgr :: RDFLabel -> Maybe [[RDFGraph]]
newgr RDFLabel
ri = ClassRestriction
-> RDFLabel
-> RDFLabel
-> [RDFLabel]
-> [Int]
-> RDFGraph
-> Maybe [[RDFGraph]]
bwdApplyRestriction1 ClassRestriction
restriction RDFLabel
cls RDFLabel
ri [RDFLabel]
props [Int]
cs RDFGraph
congr
newgrs :: Maybe [[RDFGraph]]
newgrs :: Maybe [[RDFGraph]]
newgrs = forall a b. (a -> b) -> [a] -> [b]
map forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM RDFLabel -> Maybe [[RDFGraph]]
newgr [RDFLabel]
ris
bwdApplyRestriction1 ::
ClassRestriction -> RDFLabel -> RDFLabel -> [RDFLabel] -> [Int] -> RDFGraph
-> Maybe [[RDFGraph]]
bwdApplyRestriction1 :: ClassRestriction
-> RDFLabel
-> RDFLabel
-> [RDFLabel]
-> [Int]
-> RDFGraph
-> Maybe [[RDFGraph]]
bwdApplyRestriction1 ClassRestriction
restriction RDFLabel
cls RDFLabel
ci [RDFLabel]
props [Int]
cs RDFGraph
congr =
if Bool
grConsistent then forall a. a -> Maybe a
Just [[RDFGraph]]
grss else forall a. Maybe a
Nothing
where
(Bool
grConsistent,[[RDFLabel]]
pvs,[([Maybe RDFLabel], [[RDFLabel]])]
cts,[[Maybe RDFLabel]]
_) =
ClassRestriction
-> RDFLabel
-> [RDFLabel]
-> [Int]
-> RDFGraph
-> (Bool, [[RDFLabel]], [([Maybe RDFLabel], [[RDFLabel]])],
[[Maybe RDFLabel]])
applyRestriction ClassRestriction
restriction RDFLabel
ci [RDFLabel]
props [Int]
cs RDFGraph
congr
fts :: [[RDFLabel]]
fts :: [[RDFLabel]]
fts = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap forall a b. (a, b) -> b
snd [([Maybe RDFLabel], [[RDFLabel]])]
cts
pts :: [([Maybe RDFLabel],[RDFLabel])]
pts :: [([Maybe RDFLabel], [RDFLabel])]
pts = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (ClassRestriction -> [RDFLabel] -> [([Maybe RDFLabel], [RDFLabel])]
deriveTuple ClassRestriction
restriction) [[RDFLabel]]
fts
dtss :: [[[Maybe RDFLabel]]]
dtss :: [[[Maybe RDFLabel]]]
dtss = forall a. Eq a => [[a]] -> [([Maybe a], [a])] -> [[[Maybe a]]]
coverSets [[RDFLabel]]
pvs [([Maybe RDFLabel], [RDFLabel])]
pts
ftss :: [[[Maybe RDFLabel]]]
ftss :: [[[Maybe RDFLabel]]]
ftss = forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. (\[[Maybe RDFLabel]]
t -> forall a b. (a -> [b] -> [b]) -> [[a]] -> [[b]] -> Bool
coversVals forall a. Eq a => Maybe a -> [a] -> [a]
deleteMaybe [[Maybe RDFLabel]]
t [[RDFLabel]]
pvs)) [[[Maybe RDFLabel]]]
dtss
grss :: [[RDFGraph]]
grss :: [[RDFGraph]]
grss = forall a b. (a -> b) -> [a] -> [b]
map ( [Arc RDFLabel] -> [RDFGraph]
makeGraphs forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[Maybe RDFLabel]] -> [Arc RDFLabel]
newArcs ) [[[Maybe RDFLabel]]]
ftss
newArcs :: [[Maybe RDFLabel]] -> [Arc RDFLabel]
newArcs [[Maybe RDFLabel]]
dts =
[ forall lb. lb -> lb -> lb -> Arc lb
Arc RDFLabel
ci RDFLabel
p RDFLabel
v | [Maybe RDFLabel]
mvs <- [[Maybe RDFLabel]]
dts, (RDFLabel
p,Just RDFLabel
v) <- forall a b. [a] -> [b] -> [(a, b)]
zip [RDFLabel]
props [Maybe RDFLabel]
mvs ]
makeGraphs :: [Arc RDFLabel] -> [RDFGraph]
makeGraphs = forall a b. (a -> b) -> [a] -> [b]
map (Set (Arc RDFLabel) -> RDFGraph
toRDFGraph forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Ord a => [a] -> Set a
S.fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. a -> [a] -> [a]
:[])) forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall lb. lb -> lb -> lb -> Arc lb
Arc RDFLabel
ci RDFLabel
resRdfType RDFLabel
cls forall a. a -> [a] -> [a]
:)
deriveTuple ::
ClassRestriction -> [RDFLabel]
-> [([Maybe RDFLabel], [RDFLabel])]
deriveTuple :: ClassRestriction -> [RDFLabel] -> [([Maybe RDFLabel], [RDFLabel])]
deriveTuple ClassRestriction
restriction [RDFLabel]
ft =
forall a b. (a -> b) -> [a] -> [b]
map (forall {b} {a}. b -> a -> (a, b)
tosnd [RDFLabel]
ft) forall a b. (a -> b) -> a -> b
$ forall a. PartCompare a -> [a] -> [a]
minima forall a. Eq a => [Maybe a] -> [Maybe a] -> Maybe Ordering
partCompareListMaybe forall a b. (a -> b) -> a -> b
$ forall a. (a -> Bool) -> [a] -> [a]
filter [Maybe RDFLabel] -> Bool
derives [[Maybe RDFLabel]]
partials
where
partials :: [[Maybe RDFLabel]]
partials = forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (\RDFLabel
x -> [forall a. Maybe a
Nothing, forall a. a -> Maybe a
Just RDFLabel
x]) [RDFLabel]
ft
derives :: [Maybe RDFLabel] -> Bool
derives = ([[RDFLabel]
ft] forall a. Eq a => a -> a -> Bool
==) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. HasCallStack => Maybe a -> a
fromJust forall b c a. (b -> c) -> (a -> b) -> a -> c
. ClassRestriction -> ClassRestrictionFn
crFunc ClassRestriction
restriction
tosnd :: b -> a -> (a, b)
tosnd = forall a b c. (a -> b -> c) -> b -> a -> c
flip (,)
applyRestriction ::
ClassRestriction -> RDFLabel -> [RDFLabel] -> [Int] -> RDFGraph
-> ( Bool
, [[RDFLabel]]
, [([Maybe RDFLabel],[[RDFLabel]])]
, [[Maybe RDFLabel]]
)
applyRestriction :: ClassRestriction
-> RDFLabel
-> [RDFLabel]
-> [Int]
-> RDFGraph
-> (Bool, [[RDFLabel]], [([Maybe RDFLabel], [[RDFLabel]])],
[[Maybe RDFLabel]])
applyRestriction ClassRestriction
restriction RDFLabel
ci [RDFLabel]
props [Int]
cs RDFGraph
gr =
(forall a b. (a -> [b] -> [b]) -> [[a]] -> [[b]] -> Bool
coversVals forall a. Eq a => Maybe a -> [a] -> [a]
deleteMaybe [[Maybe RDFLabel]]
sts [[RDFLabel]]
pvs Bool -> Bool -> Bool
&& Bool
cardinalityOK, [[RDFLabel]]
pvs, [([Maybe RDFLabel], [[RDFLabel]])]
cts, [[Maybe RDFLabel]]
sts )
where
pvs :: [[RDFLabel]]
pvs :: [[RDFLabel]]
pvs = [ RDFLabel -> RDFLabel -> RDFGraph -> [RDFLabel]
rdfFindPredVal RDFLabel
ci RDFLabel
p RDFGraph
gr | RDFLabel
p <- [RDFLabel]
props ]
pts :: [[Maybe RDFLabel]]
pts :: [[Maybe RDFLabel]]
pts = forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall a. [a] -> [Maybe a]
allJustAndNothing [[RDFLabel]]
pvs
rts :: [Maybe [[RDFLabel]]]
rts :: [Maybe [[RDFLabel]]]
rts = forall a b. (a -> b) -> [a] -> [b]
map (ClassRestriction -> ClassRestrictionFn
crFunc ClassRestriction
restriction) [[Maybe RDFLabel]]
pts
cts :: [([Maybe RDFLabel],[[RDFLabel]])]
cts :: [([Maybe RDFLabel], [[RDFLabel]])]
cts = forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe forall a b. (a, Maybe b) -> Maybe (a, b)
tupleConv (forall a b. [a] -> [b] -> [(a, b)]
zip [[Maybe RDFLabel]]
pts [Maybe [[RDFLabel]]]
rts)
tupleConv :: (a, Maybe b) -> Maybe (a,b)
tupleConv :: forall a b. (a, Maybe b) -> Maybe (a, b)
tupleConv (a
a, Just b
b) = forall a. a -> Maybe a
Just (a
a,b
b)
tupleConv (a, Maybe b)
_ = forall a. Maybe a
Nothing
mts :: [[Maybe RDFLabel]]
mts = forall a b. (a -> b) -> [a] -> [b]
map forall a. ([Maybe a], [[a]]) -> [Maybe a]
mostOneValue [([Maybe RDFLabel], [[RDFLabel]])]
cts
sts :: [[Maybe RDFLabel]]
sts :: [[Maybe RDFLabel]]
sts = forall a. PartCompare a -> [a] -> [a]
maxima forall a. Eq a => [Maybe a] -> [Maybe a] -> Maybe Ordering
partCompareListMaybe [[Maybe RDFLabel]]
mts
cardinalityOK :: Bool
cardinalityOK = forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Int]
cs Bool -> Bool -> Bool
|| forall (t :: * -> *) a. Foldable t => t a -> Int
length [[Maybe RDFLabel]]
sts forall a. Ord a => a -> a -> Bool
<= forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
minimum [Int]
cs
allJustAndNothing :: [a] -> [Maybe a]
allJustAndNothing :: forall a. [a] -> [Maybe a]
allJustAndNothing [a]
as = forall a. Maybe a
Nothingforall a. a -> [a] -> [a]
:forall a b. (a -> b) -> [a] -> [b]
map forall a. a -> Maybe a
Just [a]
as
mostOneValue :: ([Maybe a],[[a]]) -> [Maybe a]
mostOneValue :: forall a. ([Maybe a], [[a]]) -> [Maybe a]
mostOneValue ([Maybe a]
_,[[a]
movs]) = forall a b. (a -> b) -> [a] -> [b]
map forall a. a -> Maybe a
Just [a]
movs
mostOneValue ([Maybe a]
imvs,[[a]]
_) = [Maybe a]
imvs
coverSets :: (Eq a) => [[a]] -> [([Maybe a],[a])] -> [[[Maybe a]]]
coverSets :: forall a. Eq a => [[a]] -> [([Maybe a], [a])] -> [[[Maybe a]]]
coverSets [[a]]
pvs [([Maybe a], [a])]
dts =
forall a. PartCompare a -> [a] -> [a]
minima forall a. Eq a => [a] -> [a] -> Maybe Ordering
partCompareListSubset forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst) [[([Maybe a], [a])]]
ctss
where
ctss :: [[([Maybe a], [a])]]
ctss = forall a. (a -> Bool) -> [a] -> [a]
filter forall {a}. [(a, [a])] -> Bool
coverspvs forall a b. (a -> b) -> a -> b
$ forall a. [a] -> [a]
tail forall a b. (a -> b) -> a -> b
$ forall a. [a] -> [[a]]
subsequences [([Maybe a], [a])]
cts
cts :: [([Maybe a], [a])]
cts = forall a. PartCompare a -> [a] -> [a]
minima (forall a b.
PartCompare a
-> PartCompare b -> (a, b) -> (a, b) -> Maybe Ordering
partComparePair forall a. Eq a => [Maybe a] -> [Maybe a] -> Maybe Ordering
partCompareListMaybe forall a. Eq a => PartCompare a
partCompareEq) [([Maybe a], [a])]
dts
coverspvs :: [(a, [a])] -> Bool
coverspvs [(a, [a])]
cs = forall a b. (a -> [b] -> [b]) -> [[a]] -> [[b]] -> Bool
coversVals forall a. Eq a => a -> [a] -> [a]
delete (forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> b
snd [(a, [a])]
cs) [[a]]
pvs
coversVals :: (a->[b]->[b]) -> [[a]] -> [[b]] -> Bool
coversVals :: forall a b. (a -> [b] -> [b]) -> [[a]] -> [[b]] -> Bool
coversVals a -> [b] -> [b]
dropVal [[a]]
ts [[b]]
vss =
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all forall (t :: * -> *) a. Foldable t => t a -> Bool
null) (forall a b. (a -> b -> b) -> b -> [a] -> [b]
scanr [a] -> [[b]] -> [[b]]
dropUsed [[b]]
vss [[a]]
ts)
where
dropUsed :: [a] -> [[b]] -> [[b]]
dropUsed [] [] = []
dropUsed (a
a:[a]
as) ([b]
bs:[[b]]
bss) = a -> [b] -> [b]
dropVal a
a [b]
bs forall a. a -> [a] -> [a]
: [a] -> [[b]] -> [[b]]
dropUsed [a]
as [[b]]
bss
dropUsed [a]
_ [[b]]
_ = forall a. HasCallStack => String -> a
error String
"coversVals.dropUsed: list length mismatch"
deleteMaybe :: (Eq a) => Maybe a -> [a] -> [a]
deleteMaybe :: forall a. Eq a => Maybe a -> [a] -> [a]
deleteMaybe Maybe a
Nothing [a]
as = [a]
as
deleteMaybe (Just a
a) [a]
as = forall a. Eq a => a -> [a] -> [a]
delete a
a [a]
as
makeRDFDatatypeRestrictionRules :: RDFDatatypeVal vt -> RDFGraph -> [RDFRule]
makeRDFDatatypeRestrictionRules :: forall vt. RDFDatatypeVal vt -> RDFGraph -> [RDFRule]
makeRDFDatatypeRestrictionRules RDFDatatypeVal vt
dtval =
[ClassRestriction] -> RDFGraph -> [RDFRule]
makeRDFClassRestrictionRules [ClassRestriction]
dcrs
where
dcrs :: [ClassRestriction]
dcrs = forall a b. (a -> b) -> [a] -> [b]
map (forall vt. RDFDatatypeVal vt -> DatatypeRel vt -> ClassRestriction
makeDatatypeRestriction RDFDatatypeVal vt
dtval) (forall ex vt lb vn. DatatypeVal ex vt lb vn -> [DatatypeRel vt]
tvalRel RDFDatatypeVal vt
dtval)