module Swish.RDF.Datatype
( RDFDatatype
, RDFDatatypeVal
, RDFDatatypeMod
, RDFModifierFn, RDFApplyModifier
, makeRdfDtOpenVarBindingModify, makeRdfDtOpenVarBindingModifiers
, applyRDFDatatypeMod
, RDFDatatypeSub
, fromRDFLabel, toRDFLabel, makeDatatypedLiteral
)
where
import Swish.Datatype
( Datatype
, DatatypeVal(..)
, DatatypeMap(..)
, DatatypeMod(..), ModifierFn
, ApplyModifier
, DatatypeSub(..)
)
import Swish.Namespace (ScopedName)
import Swish.VarBinding (VarBindingModify(..))
import Swish.RDF.Graph
( RDFLabel(..)
, isDatatyped
, getLiteralText
, RDFGraph
)
import Swish.RDF.VarBinding (RDFVarBinding, RDFOpenVarBindingModify)
import Data.Maybe (fromMaybe, isJust, fromJust)
import qualified Data.Text as T
type RDFDatatype = Datatype RDFGraph RDFLabel RDFLabel
type RDFDatatypeVal vt = DatatypeVal RDFGraph vt RDFLabel RDFLabel
type RDFDatatypeMod vt = DatatypeMod vt RDFLabel RDFLabel
type RDFDatatypeSub supvt subvt = DatatypeSub RDFGraph RDFLabel RDFLabel supvt subvt
type RDFModifierFn = ModifierFn RDFLabel
type RDFApplyModifier = ApplyModifier RDFLabel RDFLabel
makeRdfDtOpenVarBindingModify ::
RDFDatatypeVal vt
-> RDFDatatypeMod vt
-> RDFOpenVarBindingModify
makeRdfDtOpenVarBindingModify :: RDFDatatypeVal vt -> RDFDatatypeMod vt -> RDFOpenVarBindingModify
makeRdfDtOpenVarBindingModify RDFDatatypeVal vt
dtval RDFDatatypeMod vt
dtmod =
RDFDatatypeMod vt -> ApplyModifier RDFLabel RDFLabel
forall vt lb vn. DatatypeMod vt lb vn -> ApplyModifier lb vn
dmAppf RDFDatatypeMod vt
dtmod (RDFDatatypeMod vt -> ScopedName
forall vt lb vn. DatatypeMod vt lb vn -> ScopedName
dmName RDFDatatypeMod vt
dtmod) ([ModifierFn RDFLabel] -> RDFOpenVarBindingModify)
-> [ModifierFn RDFLabel] -> RDFOpenVarBindingModify
forall a b. (a -> b) -> a -> b
$ (ModifierFn vt -> ModifierFn RDFLabel)
-> [ModifierFn vt] -> [ModifierFn RDFLabel]
forall a b. (a -> b) -> [a] -> [b]
map (RDFDatatypeVal vt -> ModifierFn vt -> ModifierFn RDFLabel
forall vt.
RDFDatatypeVal vt -> ModifierFn vt -> ModifierFn RDFLabel
makeRDFModifierFn RDFDatatypeVal vt
dtval) (RDFDatatypeMod vt -> [ModifierFn vt]
forall vt lb vn. DatatypeMod vt lb vn -> [ModifierFn vt]
dmModf RDFDatatypeMod vt
dtmod)
makeRdfDtOpenVarBindingModifiers ::
RDFDatatypeVal vt
-> [RDFOpenVarBindingModify]
makeRdfDtOpenVarBindingModifiers :: RDFDatatypeVal vt -> [RDFOpenVarBindingModify]
makeRdfDtOpenVarBindingModifiers RDFDatatypeVal vt
dtval =
(RDFDatatypeMod vt -> RDFOpenVarBindingModify)
-> [RDFDatatypeMod vt] -> [RDFOpenVarBindingModify]
forall a b. (a -> b) -> [a] -> [b]
map (RDFDatatypeVal vt -> RDFDatatypeMod vt -> RDFOpenVarBindingModify
forall vt.
RDFDatatypeVal vt -> RDFDatatypeMod vt -> RDFOpenVarBindingModify
makeRdfDtOpenVarBindingModify RDFDatatypeVal vt
dtval) (RDFDatatypeVal vt -> [RDFDatatypeMod vt]
forall ex vt lb vn.
DatatypeVal ex vt lb vn -> [DatatypeMod vt lb vn]
tvalMod RDFDatatypeVal vt
dtval)
applyRDFDatatypeMod ::
RDFDatatypeVal vt -> RDFDatatypeMod vt -> [RDFLabel] -> [RDFVarBinding]
-> [RDFVarBinding]
applyRDFDatatypeMod :: RDFDatatypeVal vt
-> RDFDatatypeMod vt
-> [RDFLabel]
-> [RDFVarBinding]
-> [RDFVarBinding]
applyRDFDatatypeMod RDFDatatypeVal vt
dtval RDFDatatypeMod vt
dtmod [RDFLabel]
lbs =
VarBindingModify RDFLabel RDFLabel
-> [RDFVarBinding] -> [RDFVarBinding]
forall a b.
VarBindingModify a b -> [VarBinding a b] -> [VarBinding a b]
vbmApply (RDFDatatypeVal vt -> RDFDatatypeMod vt -> RDFOpenVarBindingModify
forall vt.
RDFDatatypeVal vt -> RDFDatatypeMod vt -> RDFOpenVarBindingModify
makeRdfDtOpenVarBindingModify RDFDatatypeVal vt
dtval RDFDatatypeMod vt
dtmod [RDFLabel]
lbs)
makeRDFModifierFn ::
RDFDatatypeVal vt -> ModifierFn vt -> RDFModifierFn
makeRDFModifierFn :: RDFDatatypeVal vt -> ModifierFn vt -> ModifierFn RDFLabel
makeRDFModifierFn RDFDatatypeVal vt
dtval ModifierFn vt
fn [RDFLabel]
ivs =
let
ivals :: Maybe [vt]
ivals = (RDFLabel -> Maybe vt) -> [RDFLabel] -> Maybe [vt]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (RDFDatatypeVal vt -> RDFLabel -> Maybe vt
forall vt. RDFDatatypeVal vt -> RDFLabel -> Maybe vt
fromRDFLabel RDFDatatypeVal vt
dtval) [RDFLabel]
ivs
ovals :: [vt]
ovals | Maybe [vt] -> Bool
forall a. Maybe a -> Bool
isJust Maybe [vt]
ivals = ModifierFn vt
fn (Maybe [vt] -> [vt]
forall a. HasCallStack => Maybe a -> a
fromJust Maybe [vt]
ivals)
| Bool
otherwise = []
in
[RDFLabel] -> Maybe [RDFLabel] -> [RDFLabel]
forall a. a -> Maybe a -> a
fromMaybe [] (Maybe [RDFLabel] -> [RDFLabel]) -> Maybe [RDFLabel] -> [RDFLabel]
forall a b. (a -> b) -> a -> b
$ (vt -> Maybe RDFLabel) -> [vt] -> Maybe [RDFLabel]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (RDFDatatypeVal vt -> vt -> Maybe RDFLabel
forall vt. RDFDatatypeVal vt -> vt -> Maybe RDFLabel
toRDFLabel RDFDatatypeVal vt
dtval) [vt]
ovals
fromRDFLabel ::
RDFDatatypeVal vt -> RDFLabel -> Maybe vt
fromRDFLabel :: RDFDatatypeVal vt -> RDFLabel -> Maybe vt
fromRDFLabel RDFDatatypeVal vt
dtv RDFLabel
lab
| ScopedName -> RDFLabel -> Bool
isDatatyped ScopedName
dtnam RDFLabel
lab = DatatypeMap vt -> Text -> Maybe vt
forall vt. DatatypeMap vt -> Text -> Maybe vt
mapL2V DatatypeMap vt
dtmap (Text -> Maybe vt) -> Text -> Maybe vt
forall a b. (a -> b) -> a -> b
$ RDFLabel -> Text
getLiteralText RDFLabel
lab
| Bool
otherwise = Maybe vt
forall a. Maybe a
Nothing
where
dtnam :: ScopedName
dtnam = RDFDatatypeVal vt -> ScopedName
forall ex vt lb vn. DatatypeVal ex vt lb vn -> ScopedName
tvalName RDFDatatypeVal vt
dtv
dtmap :: DatatypeMap vt
dtmap = RDFDatatypeVal vt -> DatatypeMap vt
forall ex vt lb vn. DatatypeVal ex vt lb vn -> DatatypeMap vt
tvalMap RDFDatatypeVal vt
dtv
toRDFLabel :: RDFDatatypeVal vt -> vt -> Maybe RDFLabel
toRDFLabel :: RDFDatatypeVal vt -> vt -> Maybe RDFLabel
toRDFLabel RDFDatatypeVal vt
dtv =
(Text -> RDFLabel) -> Maybe Text -> Maybe RDFLabel
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (ScopedName -> Text -> RDFLabel
makeDatatypedLiteral ScopedName
dtnam) (Maybe Text -> Maybe RDFLabel)
-> (vt -> Maybe Text) -> vt -> Maybe RDFLabel
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DatatypeMap vt -> vt -> Maybe Text
forall vt. DatatypeMap vt -> vt -> Maybe Text
mapV2L DatatypeMap vt
dtmap
where
dtnam :: ScopedName
dtnam = RDFDatatypeVal vt -> ScopedName
forall ex vt lb vn. DatatypeVal ex vt lb vn -> ScopedName
tvalName RDFDatatypeVal vt
dtv
dtmap :: DatatypeMap vt
dtmap = RDFDatatypeVal vt -> DatatypeMap vt
forall ex vt lb vn. DatatypeVal ex vt lb vn -> DatatypeMap vt
tvalMap RDFDatatypeVal vt
dtv
makeDatatypedLiteral ::
ScopedName
-> T.Text
-> RDFLabel
makeDatatypedLiteral :: ScopedName -> Text -> RDFLabel
makeDatatypedLiteral = (Text -> ScopedName -> RDFLabel) -> ScopedName -> Text -> RDFLabel
forall a b c. (a -> b -> c) -> b -> a -> c
flip Text -> ScopedName -> RDFLabel
TypedLit