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
{-# LANGUAGE TypeApplications #-}
{-| Module : Graph.Tools
Description :
Copyright : (c) CNRS, Alexandre Delanoë
License : AGPL + CECILL v3
Maintainer : alexandre+dev@delanoe.org
Stability : experimental
Portability : POSIX
-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Graph.Tools.Import where
import Data.Csv
import Data.Text (pack, splitOn, unpack)
import Data.Vector hiding (map, uniq)
import Prelude (read)
import Protolude
import Graph.Types
import Graph.Tools.Random
import Graph.Tools.CSV
import Graph.FGL
import qualified Data.Matrix.Sparse.Static as SMatrix
import qualified Data.IntMap as Dict
import qualified Data.Map as Map
import qualified Data.ByteString.Lazy as BL
import qualified Data.Graph.Inductive as Graph
import qualified Data.List as List
import qualified Data.Set as Set
import qualified Data.Vector as Vector
import Data.Reflection
import qualified Data.IntMap.Strict as IntMap
------------------------------------------------------------------------
data GetGraph = WithFile { filepath :: FilePath }
| Random Int
data GraphData = LightGraph { lightGraph :: Graph () () }
| LabelledGraph { labelledGraph :: Graph [Text] Double }
deriving (Show)
getGraph :: GetGraph -> IO GraphData
getGraph (Random n) = reifyNat (fromIntegral n) $ \(pn :: Proxy n) ->
randomAdjacency @n
>>= \m -> pure $ LightGraph
$ mkGraphUfromEdges
$ List.map (\(x,y,_) -> (x,y))
$ SMatrix.toList m
getGraph (WithFile fp) = do
g <- readFileGraph CillexGraph fp
pure $ LabelledGraph g
getUnlabGraph :: GetGraph -> IO (Dict [Text], Graph () ())
getUnlabGraph gg = getUnlabGraph' <$> getGraph gg
getUnlabGraph' :: GraphData -> (Dict [Text], Graph () ())
getUnlabGraph' (LightGraph g) = (Dict.empty, g)
getUnlabGraph' (LabelledGraph g) = (dico, Graph.unlab g)
where dico = IntMap.fromList (Graph.labNodes g)