[Fencommits] fenfire-hs: add inserting and deleting to new graph impl

Benja Fallenstein benja.fallenstein at gmail.com
Tue Mar 20 19:09:04 EET 2007


Tue Mar 20 19:06:46 EET 2007  Benja Fallenstein <benja.fallenstein at gmail.com>
  * add inserting and deleting to new graph impl
diff -rN -u old-fenfire-hs/Fenfire/RDF.hs new-fenfire-hs/Fenfire/RDF.hs
--- old-fenfire-hs/Fenfire/RDF.hs	2007-03-20 19:08:58.000000000 +0200
+++ new-fenfire-hs/Fenfire/RDF.hs	2007-03-20 19:08:58.000000000 +0200
@@ -434,8 +434,14 @@
                                :*: Map (Any,  Any,  Any,  Any)  (Set Quad)
                                :*: HNil -- use some simple TH for this? :-)
                    }
+                   
+instance (Empty x, Empty xs) => Empty (HCons x xs) where 
+    empty = HCons empty empty
+instance Empty HNil where empty = HNil
+instance Empty Graph' where empty = Graph' "" empty empty
 
-simpleQuery pattern g = hOccursFst (graphViews g) Map.! pattern
+simpleQuery pattern g = 
+    Map.findWithDefault Set.empty pattern $ hOccursFst (graphViews g)
 
 -- We need an instance for each of these because the code GHC *generates*
 -- for each of these is different, even though *we* write the same thing
@@ -473,6 +479,11 @@
 instance (Show s, Show p, Show o, Pattern (s,p,o,Any) r) => 
          Pattern (s,p,o) r where
     query (s,p,o) = query (s,p,o,Any)
+    
+instance (Show s, Show p, Show o, Pattern (s,p,o) r, Pattern (o,p,s) r) =>
+         Pattern (s,p,Dir,o) r where
+    query (o,p,Neg,s) = query (s,p,o)
+    query (s,p,Pos,o) = query (s,p,o)
 
 instance Pattern pat (Set Quad) => Pattern pat (Set Triple) where
     query pat = Set.map quad2triple . query pat
@@ -495,3 +506,30 @@
 
 instance Pattern pat (Set Quad) => Pattern pat Bool where
     query pat = not . Set.null . (id :: Endo (Set Quad)) . query pat
+    
+    
+class (Ord a, Show a) => PatternSlot a where toPatternSlot :: Node -> a
+instance PatternSlot Node where toPatternSlot = id
+instance PatternSlot Any where toPatternSlot _ = Any
+
+newtype InsertQuad = InsertQuad Quad; newtype DeleteQuad = DeleteQuad Quad
+
+instance (PatternSlot s, PatternSlot p, PatternSlot o, PatternSlot g) => 
+         Apply InsertQuad (Map (s,p,o,g) (Set Quad)) 
+                          (Map (s,p,o,g) (Set Quad)) where
+    apply (InsertQuad (s,p,o,g)) =
+      updateWithDefault Set.empty (Set.insert (s,p,o,g))
+        (toPatternSlot s, toPatternSlot p, toPatternSlot o, toPatternSlot g)
+
+instance (PatternSlot s, PatternSlot p, PatternSlot o, PatternSlot g) => 
+         Apply DeleteQuad (Map (s,p,o,g) (Set Quad)) 
+                          (Map (s,p,o,g) (Set Quad)) where
+    apply (DeleteQuad (s,p,o,g)) =
+      updateWithDefault Set.empty (Set.delete (s,p,o,g))
+        (toPatternSlot s, toPatternSlot p, toPatternSlot o, toPatternSlot g)
+
+insertQuad :: Quad -> Endo Graph'
+insertQuad q g = g { graphViews = hMap (InsertQuad q) $ graphViews g }
+
+deleteQuad :: Quad -> Endo Graph'
+deleteQuad q g = g { graphViews = hMap (DeleteQuad q) $ graphViews g }
diff -rN -u old-fenfire-hs/Fenfire/Utils.hs new-fenfire-hs/Fenfire/Utils.hs
--- old-fenfire-hs/Fenfire/Utils.hs	2007-03-20 19:08:58.000000000 +0200
+++ new-fenfire-hs/Fenfire/Utils.hs	2007-03-20 19:08:58.000000000 +0200
@@ -32,6 +32,8 @@
 import qualified Data.Map as Map
 import Data.Maybe (fromMaybe)
 import Data.Monoid (Monoid(..))
+import Data.Set (Set)
+import qualified Data.Set as Set
 
 import qualified System.Time
 
@@ -85,6 +87,11 @@
 (&) = mappend
 
 
+class Empty a where empty :: a
+instance Empty (Set a)   where empty = Set.empty
+instance Empty (Map k v) where empty = Map.empty
+
+
 funzip :: Functor f => f (a,b) -> (f a, f b)
 funzip x = (fmap fst x, fmap snd x)
 




More information about the Fencommits mailing list