[Fencommits] fenserve: start work on a library for treating data serialized in Storm as a functional data structure

Benja Fallenstein benja.fallenstein at gmail.com
Wed Apr 4 17:15:57 EEST 2007


Wed Apr  4 16:25:48 EEST 2007  Benja Fallenstein <benja.fallenstein at gmail.com>
  * start work on a library for treating data serialized in Storm as a functional data structure
diff -rN -u old-fenserve/StormData.hs new-fenserve/StormData.hs
--- old-fenserve/StormData.hs	1970-01-01 02:00:00.000000000 +0200
+++ new-fenserve/StormData.hs	2007-04-04 17:15:57.000000000 +0300
@@ -0,0 +1,81 @@
+{-# OPTIONS_GHC -fglasgow-exts #-}
+module StormData where
+
+-- Copyright (c) 2006-2007, Benja Fallenstein, Tuukka Hastrup
+-- This file is part of Fenfire.
+-- 
+-- Fenfire is free software; you can redistribute it and/or modify it under
+-- the terms of the GNU General Public License as published by
+-- the Free Software Foundation; either version 2 of the License, or
+-- (at your option) any later version.
+-- 
+-- Fenfire is distributed in the hope that it will be useful, but WITHOUT
+-- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+-- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
+-- Public License for more details.
+-- 
+-- You should have received a copy of the GNU General
+-- Public License along with Fenfire; if not, write to the Free
+-- Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+-- MA  02111-1307  USA
+
+import Storm
+import Fenfire.RDF
+import Fenfire.Utils
+import qualified Fenfire.Raptor as Raptor
+
+import Control.Monad
+
+import Data.ByteString (ByteString)
+import qualified Data.ByteString as ByteString
+
+import System.IO.Unsafe (unsafePerformIO)
+
+data (FromRDF a, ToRDF a) => StormRef a = StormRef Node
+-- possible system for the future:
+-- UnreadStormRef Node | ReadStormRef Node a | UnserializedStormRef a
+
+instance FromRDF (StormRef a) where
+    fromRDF _ n = StormRef n
+
+instance ToRDF (StormRef a) where
+    toRDF (StormRef n) = return n
+  
+bURI :: BlockId -> String
+bURI (BlockId bid) = "blk:/" ++ bid
+  
+bIRI :: BlockId -> Node
+bIRI bid = IRI $ bURI bid
+
+bID :: Node -> BlockId
+bID (IRI ('b':'l':'k':':':'/':s)) = BlockId $ takeWhile (/= '#') s
+bID node = error $ "Not a block IRI: " ++ show node
+                                        
+getNode :: StormMonad m => Node -> m ByteString
+getNode = getBlock . bID
+                
+getGraph :: StormMonad m => Node -> m Graph
+getGraph n = do bytes <- getNode n
+                let (ts, nss) = unsafePerformIO $ 
+                        Raptor.bytesToTriples "turtle" bytes (iriStr n)
+                return $ raptorToGraph ts nss (iriStr n)
+                
+readStormRef :: (FromRDF a, ToRDF a, StormMonad m) => StormRef a -> m a
+readStormRef (StormRef n) = do g <- getGraph n; return $ fromRDF g n
+
+showGraph :: Graph -> ByteString
+showGraph g = let (ts,nss) = graphToRaptor g; uri = iriStr $ defaultGraph g
+               in unsafePerformIO $ Raptor.triplesToBytes ts nss uri
+                 
+addGraph :: StormMonad m => Graph -> m Node
+addGraph g = liftM bIRI $ addBlock (showGraph g)
+
+newStormRef :: (FromRDF a, ToRDF a, StormMonad m) => a -> m (StormRef a)
+newStormRef value = do 
+    let (node, ts) = runToRDF "new:block" $ toRDF value
+    node' <- addGraph $ toGraph (IRI "new:block") ts
+    return $ StormRef $ changeBaseURI "new:block" (iriStr node') node
+
+modifyStormRef :: (FromRDF a, ToRDF a, StormMonad m) =>
+                  EndoM m a -> EndoM m (StormRef a)
+modifyStormRef f r = readStormRef r >>= f >>= newStormRef




More information about the Fencommits mailing list