-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample.hs
More file actions
111 lines (93 loc) · 3.45 KB
/
Example.hs
File metadata and controls
111 lines (93 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
module Example.Types where
import Control.Exception (finally)
import Control.Monad (forM_, void)
import Data.Aeson
import Data.Aeson.GADT.TH
import Data.Constraint.Extras.TH
import qualified Data.Dependent.Map as DMap
import Data.Dependent.Sum (EqTag (..), (==>))
import Data.Functor.Identity (Identity (..))
import Data.GADT.Compare.TH
import Data.Maybe
import Data.Text (Text)
import qualified Data.Text as T
import qualified Algebra.Graph.Labelled.AdjacencyMap as LAM
import Data.Acid
import Data.Acid.Advanced
import Data.PropertyGraph
import Data.PropertyGraph.Acid
import Data.PropertyGraph.Labelled
-- | Vertex property types
data VP a where
VP_Title :: VP Text
VP_Desc :: VP Text
VP_Ctx :: VP ()
VP_Hidden :: VP Bool
-- | Edge property types
data EP a where
-- | Whether this edge indicates the sorting sequence
EP_Sequence :: EP ()
-- | Whether this edge indicates a vertex dependency
EP_Dependency :: EP ()
type ExampleGraph = PropertyGraph VP EP Integer
type ExampleGraphEdit r = PropertyGraphEdit Integer VP EP r
isHidden :: Ord a => a -> PropertyGraph VP ep a -> Bool
isHidden v g = fromMaybe False $ getVertexProperty VP_Hidden v g
isCtx :: Ord a => a -> PropertyGraph VP ep a -> Bool
isCtx v = isJust . getVertexProperty VP_Ctx v
getTitle :: (Ord a, Show a) => a -> PropertyGraph VP ep a -> Text
getTitle v = fromMaybe ("Untitled:" <> T.pack (show v)) . getVertexProperty VP_Title v
getDesc :: Ord a => a -> PropertyGraph VP ep a -> Maybe Text
getDesc = getVertexProperty VP_Desc
deriving instance (ToJSON v, ToJSONKey v) => ToJSON (PropertyGraph VP EP v)
deriving instance (FromJSON v, FromJSONKey v, Ord v) => FromJSON (PropertyGraph VP EP v)
instance EqTag EP Identity where
eqTagged EP_Sequence EP_Sequence = (==)
eqTagged EP_Dependency EP_Dependency = (==)
eqTagged _ _ = (\_ _ -> False)
deriveGEq ''VP
deriveGCompare ''VP
deriveArgDict ''VP
deriveJSONGADT ''VP
deriveGEq ''EP
deriveGCompare ''EP
deriveArgDict ''EP
deriveJSONGADT ''EP
instance IsAcidic ExampleGraph where
acidEvents =
[ UpdateEvent runSomeLabelledGraphEdit jsonMethodSerialiser
, QueryEvent runSomeLabelledGraphView jsonMethodSerialiser
]
main :: IO ()
main = do
st <- openLocalState (LAM.empty :: ExampleGraph)
f st `finally` closeAcidState st
where
f st = do
-- Clear the graph
void $ runEdit st PropertyGraphEdit_ClearAll
-- Add our first vertex with given properties
v1 <- runEdit st $ PropertyGraphEdit_AddVertex $ DMap.fromList [VP_Title ==> "First vertex title"]
-- Add another
v2 <- runEdit st $ PropertyGraphEdit_AddVertex $ DMap.fromList [VP_Title ==> "Second vertext"]
-- Connect the two
void $ runEdit st $ PropertyGraphEdit_AddEdge v1 v2 $ DMap.fromList [EP_Dependency ==> ()]
-- Print the title of all vertices in the graph
g <- runView st PropertyGraphView_All
let vs :: [Integer] = unLabel <$> LAM.vertexList g
forM_ vs $ \v -> print =<< runView st (PropertyGraphView_GetVertexProperty v VP_Title)