Commits

Doug Burke committed 51e618b

Formatting: separated prefixes from bnode generation

Comments (0)

Files changed (3)

src/Swish/RDF/Formatter/Internal.hs

 
 -- | A generator for BNode labels.
 data NodeGenState = Ngs
-    { prefixes  :: NamespaceMap          -- TODO: why do we have prefixes in here? should be in parent state (if needed)
-    , nodeMap   :: NodeGenLookupMap
+    { nodeMap   :: NodeGenLookupMap
     , nodeGen   :: Word32
     }
 
 -- | Create an empty node generator.
 emptyNgs :: NodeGenState
-emptyNgs = Ngs
-    { prefixes  = M.empty
-    , nodeMap   = M.empty
-    , nodeGen   = 0
-    }
+emptyNgs = Ngs M.empty 0
 
 {-|
 Get the label text for the blank node, creating a new one

src/Swish/RDF/Formatter/N3.hs

     , objs      :: [RDFLabel]          -- for last property selected
     , formAvail :: FormulaMap RDFLabel
     , formQueue :: [(RDFLabel,RDFGraph)]
+    , prefixes  :: NamespaceMap
     , nodeGenSt :: NodeGenState
     , bNodesCheck   :: [RDFLabel]      -- these bNodes are not to be converted to '[..]' format
     , traceBuf  :: [String]
              
 type Formatter a = State N3FormatterState a
 
-emptyN3FS :: NodeGenState -> N3FormatterState
-emptyN3FS ngs = N3FS
+emptyN3FS :: NamespaceMap -> NodeGenState -> N3FormatterState
+emptyN3FS pmap ngs = N3FS
     { indent    = "\n"
     , lineBreak = False
     , graph     = emptyRDFGraph
     , objs      = []
     , formAvail = emptyFormulaMap
     , formQueue = []
+    , prefixes  = pmap
     , nodeGenSt = ngs
     , bNodesCheck   = []
     , traceBuf  = []
 setNgs ngs = modify $ \st -> st { nodeGenSt = ngs }
 
 getPrefixes :: Formatter NamespaceMap
-getPrefixes = prefixes `liftM` getNgs
+getPrefixes = prefixes `liftM` get
+
+setPrefixes :: NamespaceMap -> Formatter ()
+setPrefixes pmap = modify $ \st -> st { prefixes = pmap }
 
 getSubjs :: Formatter (SubjTree RDFLabel)
 getSubjs = subjs `liftM` get
   -> (B.Builder, NodeGenLookupMap, Word32, [String])
 formatGraphDiag indnt flag gr = 
   let fg  = formatGraph indnt " .\n" False flag gr
-      ngs = emptyNgs {
-        prefixes = M.empty,
-        nodeGen  = findMaxBnode gr
-        }
+      ngs = emptyNgs { nodeGen = findMaxBnode gr }
              
-      (out, fgs) = runState fg (emptyN3FS ngs)
+      (out, fgs) = runState fg (emptyN3FS emptyNamespaceMap ngs)
       ogs        = nodeGenSt fgs
   
   in (out, nodeMap ogs, nodeGen ogs, traceBuf fgs)
 
 insertFormula :: RDFGraph -> Formatter B.Builder
 insertFormula gr = do
+  pmap0 <- getPrefixes
   ngs0  <- getNgs
   ind   <- getIndent
   let grm = formatGraph (ind `mappend` "    ") "" True False
             (setNamespaces emptyNamespaceMap gr)
 
-      (f3str, fgs') = runState grm (emptyN3FS ngs0)
+      (f3str, fgs') = runState grm (emptyN3FS pmap0 ngs0)
 
   setNgs (nodeGenSt fgs')
+  setPrefixes (prefixes fgs')
   f4str <- nextLine " } "
   return $ mconcat [" { ",f3str, f4str]
 
 
 newState :: RDFGraph -> N3FormatterState -> N3FormatterState
 newState gr st = 
-    let ngs0 = nodeGenSt st
-        pre' = prefixes ngs0 `M.union` getNamespaces gr
-        ngs' = ngs0 { prefixes = pre' }
+    let pre' = prefixes st `M.union` getNamespaces gr
         (arcSubjs, bNodes) = processArcs gr
 
     in st  { graph     = gr
            , props     = []
            , objs      = []
            , formAvail = getFormulae gr
-           , nodeGenSt = ngs'
+           , prefixes  = pre'
            , bNodesCheck   = bNodes
            }
 

src/Swish/RDF/Formatter/Turtle.hs

 import Swish.RDF.Graph (
   RDFGraph, RDFLabel(..)
   , NamespaceMap
+  , emptyNamespaceMap
   , getNamespaces
   , emptyRDFGraph
   )
     , objs      :: [RDFLabel]          -- for last property selected
     -- , formAvail :: FormulaMap RDFLabel
     -- , formQueue :: [(RDFLabel,RDFGraph)]
+    , prefixes  :: NamespaceMap
     , nodeGenSt :: NodeGenState
     , bNodesCheck   :: [RDFLabel]      -- these bNodes are not to be converted to '[..]' format
     , traceBuf  :: [String]
     , objs      = []
     -- , formAvail = emptyFormulaMap
     -- , formQueue = []
+    , prefixes  = emptyNamespaceMap
     , nodeGenSt = ngs
     , bNodesCheck   = []
     , traceBuf  = []
 setNgs ngs = modify $ \st -> st { nodeGenSt = ngs }
 
 getPrefixes :: Formatter NamespaceMap
-getPrefixes = prefixes `liftM` getNgs
+getPrefixes = prefixes `liftM` get
 
 getSubjs :: Formatter (SubjTree RDFLabel)
 getSubjs = subjs `liftM` get
   -> (B.Builder, NodeGenLookupMap, Word32, [String])
 formatGraphDiag indnt flag gr = 
   let fg  = formatGraph indnt " .\n" False flag gr
-      ngs = emptyNgs {
-        prefixes = M.empty,
-        nodeGen  = findMaxBnode gr
-        }
+      ngs = emptyNgs { nodeGen = findMaxBnode gr }
              
       (out, fgs) = runState fg (emptyTFS ngs)
       ogs        = nodeGenSt fgs
 
 newState :: RDFGraph -> TurtleFormatterState -> TurtleFormatterState
 newState gr st = 
-    let ngs0 = nodeGenSt st
-        pre' = prefixes ngs0 `M.union` getNamespaces gr
-        ngs' = ngs0 { prefixes = pre' }
+    let pre' = prefixes st `M.union` getNamespaces gr
         (arcSubjs, bNodes) = processArcs gr
 
     in st  { graph     = gr
            , subjs     = arcSubjs
            , props     = []
            , objs      = []
-           , nodeGenSt = ngs'
+           , prefixes  = pre'
            , bNodesCheck   = bNodes
            }