{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
module Swish.RDF.Formatter.N3
( NodeGenLookupMap
, formatGraphAsText
, formatGraphAsLazyText
, formatGraphAsBuilder
, formatGraphIndent
, formatGraphDiag
)
where
import Swish.RDF.Formatter.Internal (NodeGenLookupMap, SubjTree, PredTree
, SLens(..)
, LabelContext(..)
, NodeGenState(..)
, changeState
, hasMore
, emptyNgs
, findMaxBnode
, processArcs
, quoteB
, formatScopedName
, formatPlainLit
, formatLangLit
, formatTypedLit
, insertList
, nextLine_
, mapBlankNode_
, formatPrefixes_
, formatGraph_
, formatSubjects_
, formatProperties_
, formatObjects_
, insertBnode_
, extractList_
)
import Swish.Namespace (ScopedName)
import Swish.RDF.Graph (
RDFGraph, RDFLabel(..),
NamespaceMap,
emptyNamespaceMap,
FormulaMap, emptyFormulaMap,
setNamespaces, getNamespaces,
getFormulae,
emptyRDFGraph
)
import Swish.RDF.Vocabulary (
rdfType,
rdfNil,
owlSameAs, logImplies
)
import Control.Monad (void)
import Control.Monad.State (State, modify, get, gets, put, runState)
import Data.Char (isDigit)
import Data.List (uncons)
import Data.Word (Word32)
#if (!defined(__GLASGOW_HASKELL__)) || (__GLASGOW_HASKELL__ < 710)
import Data.Monoid (Monoid(..))
#endif
import qualified Data.Map as M
import qualified Data.Text as T
import qualified Data.Text.Lazy as L
import qualified Data.Text.Lazy.Builder as B
data N3FormatterState = N3FS
{ N3FormatterState -> Builder
indent :: B.Builder
, N3FormatterState -> Bool
lineBreak :: Bool
, N3FormatterState -> RDFGraph
graph :: RDFGraph
, N3FormatterState -> SubjTree RDFLabel
subjs :: SubjTree RDFLabel
, N3FormatterState -> PredTree RDFLabel
props :: PredTree RDFLabel
, N3FormatterState -> [RDFLabel]
objs :: [RDFLabel]
, N3FormatterState -> FormulaMap RDFLabel
formAvail :: FormulaMap RDFLabel
, N3FormatterState -> [(RDFLabel, RDFGraph)]
formQueue :: [(RDFLabel,RDFGraph)]
, N3FormatterState -> NamespaceMap
prefixes :: NamespaceMap
, N3FormatterState -> NodeGenState
nodeGenSt :: NodeGenState
, N3FormatterState -> [RDFLabel]
bNodesCheck :: [RDFLabel]
, N3FormatterState -> [[Char]]
traceBuf :: [String]
}
type SL a = SLens N3FormatterState a
_lineBreak :: SL Bool
_lineBreak :: SL Bool
_lineBreak = forall a b. (a -> b) -> (a -> b -> a) -> SLens a b
SLens N3FormatterState -> Bool
lineBreak forall a b. (a -> b) -> a -> b
$ \N3FormatterState
a Bool
b -> N3FormatterState
a { lineBreak :: Bool
lineBreak = Bool
b }
_nodeGen :: SL NodeGenState
_nodeGen :: SL NodeGenState
_nodeGen = forall a b. (a -> b) -> (a -> b -> a) -> SLens a b
SLens N3FormatterState -> NodeGenState
nodeGenSt forall a b. (a -> b) -> a -> b
$ \N3FormatterState
a NodeGenState
b -> N3FormatterState
a { nodeGenSt :: NodeGenState
nodeGenSt = NodeGenState
b }
type Formatter a = State N3FormatterState a
updateState :: N3FormatterState -> SubjTree RDFLabel -> PredTree RDFLabel -> [RDFLabel] -> N3FormatterState
updateState :: N3FormatterState
-> SubjTree RDFLabel
-> PredTree RDFLabel
-> [RDFLabel]
-> N3FormatterState
updateState N3FormatterState
ost SubjTree RDFLabel
nsubjs PredTree RDFLabel
nprops [RDFLabel]
nobjs = N3FormatterState
ost { subjs :: SubjTree RDFLabel
subjs = SubjTree RDFLabel
nsubjs, props :: PredTree RDFLabel
props = PredTree RDFLabel
nprops, objs :: [RDFLabel]
objs = [RDFLabel]
nobjs }
emptyN3FS :: NamespaceMap -> NodeGenState -> N3FormatterState
emptyN3FS :: NamespaceMap -> NodeGenState -> N3FormatterState
emptyN3FS NamespaceMap
pmap NodeGenState
ngs = N3FS
{ indent :: Builder
indent = Builder
"\n"
, lineBreak :: Bool
lineBreak = Bool
False
, graph :: RDFGraph
graph = RDFGraph
emptyRDFGraph
, subjs :: SubjTree RDFLabel
subjs = []
, props :: PredTree RDFLabel
props = []
, objs :: [RDFLabel]
objs = []
, formAvail :: FormulaMap RDFLabel
formAvail = FormulaMap RDFLabel
emptyFormulaMap
, formQueue :: [(RDFLabel, RDFGraph)]
formQueue = []
, prefixes :: NamespaceMap
prefixes = NamespaceMap
pmap
, nodeGenSt :: NodeGenState
nodeGenSt = NodeGenState
ngs
, bNodesCheck :: [RDFLabel]
bNodesCheck = []
, traceBuf :: [[Char]]
traceBuf = []
}
setIndent :: B.Builder -> Formatter ()
setIndent :: Builder -> Formatter ()
setIndent Builder
ind = forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify forall a b. (a -> b) -> a -> b
$ \N3FormatterState
st -> N3FormatterState
st { indent :: Builder
indent = Builder
ind }
setLineBreak :: Bool -> Formatter ()
setLineBreak :: Bool -> Formatter ()
setLineBreak Bool
brk = forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify forall a b. (a -> b) -> a -> b
$ \N3FormatterState
st -> N3FormatterState
st { lineBreak :: Bool
lineBreak = Bool
brk }
setSubjs :: SubjTree RDFLabel -> Formatter ()
setSubjs :: SubjTree RDFLabel -> Formatter ()
setSubjs SubjTree RDFLabel
sl = forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify forall a b. (a -> b) -> a -> b
$ \N3FormatterState
st -> N3FormatterState
st { subjs :: SubjTree RDFLabel
subjs = SubjTree RDFLabel
sl }
setProps :: PredTree RDFLabel -> Formatter ()
setProps :: PredTree RDFLabel -> Formatter ()
setProps PredTree RDFLabel
ps = forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify forall a b. (a -> b) -> a -> b
$ \N3FormatterState
st -> N3FormatterState
st { props :: PredTree RDFLabel
props = PredTree RDFLabel
ps }
queueFormula :: RDFLabel -> Formatter ()
queueFormula :: RDFLabel -> Formatter ()
queueFormula RDFLabel
fn = do
N3FormatterState
st <- forall s (m :: * -> *). MonadState s m => m s
get
let fa :: FormulaMap RDFLabel
fa = N3FormatterState -> FormulaMap RDFLabel
formAvail N3FormatterState
st
_newState :: RDFGraph -> N3FormatterState
_newState RDFGraph
fv = N3FormatterState
st {
formAvail :: FormulaMap RDFLabel
formAvail = forall k a. Ord k => k -> Map k a -> Map k a
M.delete RDFLabel
fn FormulaMap RDFLabel
fa,
formQueue :: [(RDFLabel, RDFGraph)]
formQueue = (RDFLabel
fn,RDFGraph
fv) forall a. a -> [a] -> [a]
: N3FormatterState -> [(RDFLabel, RDFGraph)]
formQueue N3FormatterState
st
}
case forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup RDFLabel
fn FormulaMap RDFLabel
fa of
Maybe RDFGraph
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
Just RDFGraph
v -> forall (f :: * -> *) a. Functor f => f a -> f ()
void forall a b. (a -> b) -> a -> b
$ forall s (m :: * -> *). MonadState s m => s -> m ()
put forall a b. (a -> b) -> a -> b
$ RDFGraph -> N3FormatterState
_newState RDFGraph
v
extractFormula :: RDFLabel -> Formatter (Maybe RDFGraph)
RDFLabel
fn = do
N3FormatterState
st <- forall s (m :: * -> *). MonadState s m => m s
get
let (Maybe RDFGraph
rval, FormulaMap RDFLabel
nform) = forall k a.
Ord k =>
(k -> a -> Maybe a) -> k -> Map k a -> (Maybe a, Map k a)
M.updateLookupWithKey (\RDFLabel
_ RDFGraph
_ -> forall a. Maybe a
Nothing) RDFLabel
fn forall a b. (a -> b) -> a -> b
$ N3FormatterState -> FormulaMap RDFLabel
formAvail N3FormatterState
st
forall s (m :: * -> *). MonadState s m => s -> m ()
put forall a b. (a -> b) -> a -> b
$ N3FormatterState
st { formAvail :: FormulaMap RDFLabel
formAvail = FormulaMap RDFLabel
nform }
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe RDFGraph
rval
extractList :: LabelContext -> RDFLabel -> Formatter (Maybe [RDFLabel])
= forall a.
(a -> SubjTree RDFLabel)
-> (a -> PredTree RDFLabel)
-> (SubjTree RDFLabel -> State a ())
-> (PredTree RDFLabel -> State a ())
-> LabelContext
-> RDFLabel
-> State a (Maybe [RDFLabel])
extractList_ N3FormatterState -> SubjTree RDFLabel
subjs N3FormatterState -> PredTree RDFLabel
props SubjTree RDFLabel -> Formatter ()
setSubjs PredTree RDFLabel -> Formatter ()
setProps
formatGraphAsText :: RDFGraph -> T.Text
formatGraphAsText :: RDFGraph -> Text
formatGraphAsText = Text -> Text
L.toStrict forall b c a. (b -> c) -> (a -> b) -> a -> c
. RDFGraph -> Text
formatGraphAsLazyText
formatGraphAsLazyText :: RDFGraph -> L.Text
formatGraphAsLazyText :: RDFGraph -> Text
formatGraphAsLazyText = Builder -> Text
B.toLazyText forall b c a. (b -> c) -> (a -> b) -> a -> c
. RDFGraph -> Builder
formatGraphAsBuilder
formatGraphAsBuilder :: RDFGraph -> B.Builder
formatGraphAsBuilder :: RDFGraph -> Builder
formatGraphAsBuilder = Builder -> Bool -> RDFGraph -> Builder
formatGraphIndent Builder
"\n" Bool
True
formatGraphIndent ::
B.Builder
-> Bool
-> RDFGraph
-> B.Builder
formatGraphIndent :: Builder -> Bool -> RDFGraph -> Builder
formatGraphIndent Builder
indnt Bool
flag RDFGraph
gr =
let (Builder
res, NodeGenLookupMap
_, Word32
_, [[Char]]
_) = Builder
-> Bool
-> RDFGraph
-> (Builder, NodeGenLookupMap, Word32, [[Char]])
formatGraphDiag Builder
indnt Bool
flag RDFGraph
gr
in Builder
res
formatGraphDiag ::
B.Builder
-> Bool
-> RDFGraph
-> (B.Builder, NodeGenLookupMap, Word32, [String])
formatGraphDiag :: Builder
-> Bool
-> RDFGraph
-> (Builder, NodeGenLookupMap, Word32, [[Char]])
formatGraphDiag Builder
indnt Bool
flag RDFGraph
gr =
let fg :: Formatter Builder
fg = Builder -> Builder -> Bool -> Bool -> RDFGraph -> Formatter Builder
formatGraph Builder
indnt Builder
" .\n" Bool
False Bool
flag RDFGraph
gr
ngs :: NodeGenState
ngs = NodeGenState
emptyNgs { nodeGen :: Word32
nodeGen = RDFGraph -> Word32
findMaxBnode RDFGraph
gr }
(Builder
out, N3FormatterState
fgs) = forall s a. State s a -> s -> (a, s)
runState Formatter Builder
fg (NamespaceMap -> NodeGenState -> N3FormatterState
emptyN3FS NamespaceMap
emptyNamespaceMap NodeGenState
ngs)
ogs :: NodeGenState
ogs = N3FormatterState -> NodeGenState
nodeGenSt N3FormatterState
fgs
in (Builder
out, NodeGenState -> NodeGenLookupMap
nodeMap NodeGenState
ogs, NodeGenState -> Word32
nodeGen NodeGenState
ogs, N3FormatterState -> [[Char]]
traceBuf N3FormatterState
fgs)
formatGraph ::
B.Builder
-> B.Builder
-> Bool
-> Bool
-> RDFGraph
-> Formatter B.Builder
formatGraph :: Builder -> Builder -> Bool -> Bool -> RDFGraph -> Formatter Builder
formatGraph = forall a.
(Builder -> State a ())
-> (Bool -> State a ())
-> (RDFGraph -> a -> a)
-> (NamespaceMap -> State a Builder)
-> (a -> SubjTree RDFLabel)
-> State a Builder
-> Builder
-> Builder
-> Bool
-> Bool
-> RDFGraph
-> State a Builder
formatGraph_ Builder -> Formatter ()
setIndent Bool -> Formatter ()
setLineBreak RDFGraph -> N3FormatterState -> N3FormatterState
newState NamespaceMap -> Formatter Builder
formatPrefixes N3FormatterState -> SubjTree RDFLabel
subjs Formatter Builder
formatSubjects
formatPrefixes :: NamespaceMap -> Formatter B.Builder
formatPrefixes :: NamespaceMap -> Formatter Builder
formatPrefixes = forall a.
(Builder -> State a Builder) -> NamespaceMap -> State a Builder
formatPrefixes_ Builder -> Formatter Builder
nextLine
formatSubjects :: Formatter B.Builder
formatSubjects :: Formatter Builder
formatSubjects = forall a.
State a RDFLabel
-> (LabelContext -> RDFLabel -> State a Builder)
-> (a -> PredTree RDFLabel)
-> (RDFLabel -> Builder -> State a Builder)
-> (a -> SubjTree RDFLabel)
-> (Builder -> State a Builder)
-> State a Builder
formatSubjects_ Formatter RDFLabel
nextSubject LabelContext -> RDFLabel -> Formatter Builder
formatLabel N3FormatterState -> PredTree RDFLabel
props RDFLabel -> Builder -> Formatter Builder
formatProperties N3FormatterState -> SubjTree RDFLabel
subjs Builder -> Formatter Builder
nextLine
formatProperties :: RDFLabel -> B.Builder -> Formatter B.Builder
formatProperties :: RDFLabel -> Builder -> Formatter Builder
formatProperties = forall a.
(RDFLabel -> State a RDFLabel)
-> (LabelContext -> RDFLabel -> State a Builder)
-> (RDFLabel -> RDFLabel -> Builder -> State a Builder)
-> (a -> PredTree RDFLabel)
-> (Builder -> State a Builder)
-> RDFLabel
-> Builder
-> State a Builder
formatProperties_ RDFLabel -> Formatter RDFLabel
nextProperty LabelContext -> RDFLabel -> Formatter Builder
formatLabel RDFLabel -> RDFLabel -> Builder -> Formatter Builder
formatObjects N3FormatterState -> PredTree RDFLabel
props Builder -> Formatter Builder
nextLine
formatObjects :: RDFLabel -> RDFLabel -> B.Builder -> Formatter B.Builder
formatObjects :: RDFLabel -> RDFLabel -> Builder -> Formatter Builder
formatObjects = forall a.
(RDFLabel -> RDFLabel -> State a RDFLabel)
-> (LabelContext -> RDFLabel -> State a Builder)
-> (a -> [RDFLabel])
-> (Builder -> State a Builder)
-> RDFLabel
-> RDFLabel
-> Builder
-> State a Builder
formatObjects_ RDFLabel -> RDFLabel -> Formatter RDFLabel
nextObject LabelContext -> RDFLabel -> Formatter Builder
formatLabel N3FormatterState -> [RDFLabel]
objs Builder -> Formatter Builder
nextLine
insertFormula :: RDFGraph -> Formatter B.Builder
insertFormula :: RDFGraph -> Formatter Builder
insertFormula RDFGraph
gr = do
NamespaceMap
pmap0 <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets N3FormatterState -> NamespaceMap
prefixes
NodeGenState
ngs0 <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets N3FormatterState -> NodeGenState
nodeGenSt
Builder
ind <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets N3FormatterState -> Builder
indent
let grm :: Formatter Builder
grm = Builder -> Builder -> Bool -> Bool -> RDFGraph -> Formatter Builder
formatGraph (Builder
ind forall a. Monoid a => a -> a -> a
`mappend` Builder
" ") Builder
"" Bool
True Bool
False
(forall lb. NamespaceMap -> NSGraph lb -> NSGraph lb
setNamespaces NamespaceMap
emptyNamespaceMap RDFGraph
gr)
(Builder
f3str, N3FormatterState
fgs') = forall s a. State s a -> s -> (a, s)
runState Formatter Builder
grm (NamespaceMap -> NodeGenState -> N3FormatterState
emptyN3FS NamespaceMap
pmap0 NodeGenState
ngs0)
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify forall a b. (a -> b) -> a -> b
$ \N3FormatterState
st -> N3FormatterState
st { nodeGenSt :: NodeGenState
nodeGenSt = N3FormatterState -> NodeGenState
nodeGenSt N3FormatterState
fgs'
, prefixes :: NamespaceMap
prefixes = N3FormatterState -> NamespaceMap
prefixes N3FormatterState
fgs' }
Builder
f4str <- Builder -> Formatter Builder
nextLine Builder
" } "
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. Monoid a => [a] -> a
mconcat [Builder
" { ",Builder
f3str, Builder
f4str]
insertBnode :: LabelContext -> RDFLabel -> Formatter B.Builder
insertBnode :: LabelContext -> RDFLabel -> Formatter Builder
insertBnode LabelContext
SubjContext RDFLabel
lbl = do
Bool
flag <- forall a b. (a -> [b]) -> State a Bool
hasMore N3FormatterState -> PredTree RDFLabel
props
Builder
txt <- if Bool
flag
then (forall a. Monoid a => a -> a -> a
`mappend` Builder
"\n") forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` RDFLabel -> Builder -> Formatter Builder
formatProperties RDFLabel
lbl Builder
""
else forall (m :: * -> *) a. Monad m => a -> m a
return Builder
""
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. Monoid a => [a] -> a
mconcat [Builder
"[", Builder
txt, Builder
"]"]
insertBnode LabelContext
_ RDFLabel
lbl = forall a.
(a -> SubjTree RDFLabel)
-> (a -> PredTree RDFLabel)
-> (a -> [RDFLabel])
-> (a -> SubjTree RDFLabel -> PredTree RDFLabel -> [RDFLabel] -> a)
-> (RDFLabel -> Builder -> State a Builder)
-> RDFLabel
-> State a Builder
insertBnode_ N3FormatterState -> SubjTree RDFLabel
subjs N3FormatterState -> PredTree RDFLabel
props N3FormatterState -> [RDFLabel]
objs N3FormatterState
-> SubjTree RDFLabel
-> PredTree RDFLabel
-> [RDFLabel]
-> N3FormatterState
updateState RDFLabel -> Builder -> Formatter Builder
formatProperties RDFLabel
lbl
newState :: RDFGraph -> N3FormatterState -> N3FormatterState
newState :: RDFGraph -> N3FormatterState -> N3FormatterState
newState RDFGraph
gr N3FormatterState
st =
let pre' :: NamespaceMap
pre' = N3FormatterState -> NamespaceMap
prefixes N3FormatterState
st forall k a. Ord k => Map k a -> Map k a -> Map k a
`M.union` forall lb. NSGraph lb -> NamespaceMap
getNamespaces RDFGraph
gr
(SubjTree RDFLabel
arcSubjs, [RDFLabel]
bNodes) = RDFGraph -> (SubjTree RDFLabel, [RDFLabel])
processArcs RDFGraph
gr
in N3FormatterState
st { graph :: RDFGraph
graph = RDFGraph
gr
, subjs :: SubjTree RDFLabel
subjs = SubjTree RDFLabel
arcSubjs
, props :: PredTree RDFLabel
props = []
, objs :: [RDFLabel]
objs = []
, formAvail :: FormulaMap RDFLabel
formAvail = forall lb. NSGraph lb -> FormulaMap lb
getFormulae RDFGraph
gr
, prefixes :: NamespaceMap
prefixes = NamespaceMap
pre'
, bNodesCheck :: [RDFLabel]
bNodesCheck = [RDFLabel]
bNodes
}
getNext :: [a] -> (a, [a])
getNext :: forall a. [a] -> (a, [a])
getNext [a]
xs = case forall a. [a] -> Maybe (a, [a])
uncons [a]
xs of
Just (a
a, [a]
as) -> (a
a, [a]
as)
Maybe (a, [a])
Nothing -> forall a. HasCallStack => [Char] -> a
error [Char]
"Invariant broken: list is empty"
nextSubject :: Formatter RDFLabel
nextSubject :: Formatter RDFLabel
nextSubject =
forall a b. (a -> (b, a)) -> State a b
changeState forall a b. (a -> b) -> a -> b
$ \N3FormatterState
st ->
let ((RDFLabel
a,PredTree RDFLabel
b), SubjTree RDFLabel
sbs) = forall a. [a] -> (a, [a])
getNext (N3FormatterState -> SubjTree RDFLabel
subjs N3FormatterState
st)
nst :: N3FormatterState
nst = N3FormatterState
st { subjs :: SubjTree RDFLabel
subjs = SubjTree RDFLabel
sbs
, props :: PredTree RDFLabel
props = PredTree RDFLabel
b
, objs :: [RDFLabel]
objs = []
}
in (RDFLabel
a, N3FormatterState
nst)
nextProperty :: RDFLabel -> Formatter RDFLabel
nextProperty :: RDFLabel -> Formatter RDFLabel
nextProperty RDFLabel
_ =
forall a b. (a -> (b, a)) -> State a b
changeState forall a b. (a -> b) -> a -> b
$ \N3FormatterState
st ->
let ((RDFLabel
a,[RDFLabel]
b), PredTree RDFLabel
prs) = forall a. [a] -> (a, [a])
getNext (N3FormatterState -> PredTree RDFLabel
props N3FormatterState
st)
nst :: N3FormatterState
nst = N3FormatterState
st { props :: PredTree RDFLabel
props = PredTree RDFLabel
prs
, objs :: [RDFLabel]
objs = [RDFLabel]
b
}
in (RDFLabel
a, N3FormatterState
nst)
nextObject :: RDFLabel -> RDFLabel -> Formatter RDFLabel
nextObject :: RDFLabel -> RDFLabel -> Formatter RDFLabel
nextObject RDFLabel
_ RDFLabel
_ =
forall a b. (a -> (b, a)) -> State a b
changeState forall a b. (a -> b) -> a -> b
$ \N3FormatterState
st ->
let (RDFLabel
ob, [RDFLabel]
obs) = forall a. [a] -> (a, [a])
getNext (N3FormatterState -> [RDFLabel]
objs N3FormatterState
st)
nst :: N3FormatterState
nst = N3FormatterState
st { objs :: [RDFLabel]
objs = [RDFLabel]
obs }
in (RDFLabel
ob, N3FormatterState
nst)
nextLine :: B.Builder -> Formatter B.Builder
nextLine :: Builder -> Formatter Builder
nextLine = forall a.
(a -> Builder) -> SLens a Bool -> Builder -> State a Builder
nextLine_ N3FormatterState -> Builder
indent SL Bool
_lineBreak
specialTable :: [(ScopedName, String)]
specialTable :: [(ScopedName, [Char])]
specialTable =
[ (ScopedName
rdfType, [Char]
"a")
, (ScopedName
owlSameAs, [Char]
"=")
, (ScopedName
logImplies, [Char]
"=>")
, (ScopedName
rdfNil, [Char]
"()")
]
formatLabel :: LabelContext -> RDFLabel -> Formatter B.Builder
formatLabel :: LabelContext -> RDFLabel -> Formatter Builder
formatLabel LabelContext
lctxt lab :: RDFLabel
lab@(Blank (Char
_:[Char]
_)) = do
Maybe [RDFLabel]
mlst <- LabelContext -> RDFLabel -> Formatter (Maybe [RDFLabel])
extractList LabelContext
lctxt RDFLabel
lab
case Maybe [RDFLabel]
mlst of
Just [RDFLabel]
lst -> forall a.
(RDFLabel -> State a Builder) -> [RDFLabel] -> State a Builder
insertList (LabelContext -> RDFLabel -> Formatter Builder
formatLabel LabelContext
ObjContext) [RDFLabel]
lst
Maybe [RDFLabel]
Nothing -> do
Maybe RDFGraph
mfml <- RDFLabel -> Formatter (Maybe RDFGraph)
extractFormula RDFLabel
lab
case Maybe RDFGraph
mfml of
Just RDFGraph
fml -> RDFGraph -> Formatter Builder
insertFormula RDFGraph
fml
Maybe RDFGraph
Nothing -> do
[RDFLabel]
nb1 <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets N3FormatterState -> [RDFLabel]
bNodesCheck
if LabelContext
lctxt forall a. Eq a => a -> a -> Bool
/= LabelContext
PredContext Bool -> Bool -> Bool
&& RDFLabel
lab forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [RDFLabel]
nb1
then LabelContext -> RDFLabel -> Formatter Builder
insertBnode LabelContext
lctxt RDFLabel
lab
else RDFLabel -> Formatter Builder
formatNodeId RDFLabel
lab
formatLabel LabelContext
_ lab :: RDFLabel
lab@(Res ScopedName
sn) =
case forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup ScopedName
sn [(ScopedName, [Char])]
specialTable of
Just [Char]
txt -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Bool -> [Char] -> Builder
quoteB Bool
True [Char]
txt
Maybe [Char]
Nothing -> do
NamespaceMap
pr <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets N3FormatterState -> NamespaceMap
prefixes
RDFLabel -> Formatter ()
queueFormula RDFLabel
lab
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ ScopedName -> NamespaceMap -> Builder
formatScopedName ScopedName
sn NamespaceMap
pr
formatLabel LabelContext
_ (Lit Text
lit) = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Text -> Builder
formatPlainLit Text
lit
formatLabel LabelContext
_ (LangLit Text
lit LanguageTag
lcode) = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Text -> LanguageTag -> Builder
formatLangLit Text
lit LanguageTag
lcode
formatLabel LabelContext
_ (TypedLit Text
lit ScopedName
dtype) = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Bool -> Text -> ScopedName -> Builder
formatTypedLit Bool
True Text
lit ScopedName
dtype
formatLabel LabelContext
_ RDFLabel
lab = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ [Char] -> Builder
B.fromString forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> [Char]
show RDFLabel
lab
formatNodeId :: RDFLabel -> Formatter B.Builder
formatNodeId :: RDFLabel -> Formatter Builder
formatNodeId lab :: RDFLabel
lab@(Blank (Char
lnc:[Char]
_)) =
if Char -> Bool
isDigit Char
lnc then RDFLabel -> Formatter Builder
mapBlankNode RDFLabel
lab else forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ [Char] -> Builder
B.fromString forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> [Char]
show RDFLabel
lab
formatNodeId RDFLabel
other = forall a. HasCallStack => [Char] -> a
error forall a b. (a -> b) -> a -> b
$ [Char]
"formatNodeId not expecting a " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show RDFLabel
other
mapBlankNode :: RDFLabel -> Formatter B.Builder
mapBlankNode :: RDFLabel -> Formatter Builder
mapBlankNode = forall a. SLens a NodeGenState -> RDFLabel -> State a Builder
mapBlankNode_ SL NodeGenState
_nodeGen