Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
111 lines (93 loc) · 3.45 KB

File metadata and controls

111 lines (93 loc) · 3.45 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.