2 Module : Gargantext.Viz.Graph
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
12 {-# LANGUAGE NoImplicitPrelude #-}
13 {-# LANGUAGE TemplateHaskell #-}
14 {-# LANGUAGE DeriveGeneric #-}
16 module Gargantext.Viz.Graph
19 import GHC.IO (FilePath)
20 import GHC.Generics (Generic)
21 import Data.Aeson.TH (deriveJSON)
22 import qualified Data.Aeson as DA
24 import Data.ByteString.Lazy as DBL (readFile, writeFile)
26 import Data.Text (Text)
27 import qualified Text.Read as T
28 import qualified Data.Text as T
30 import Gargantext.Prelude
31 import Gargantext.Core.Utils.Prefix (unPrefix)
34 data TypeNode = Terms | Unknown
35 deriving (Show, Generic)
37 $(deriveJSON (unPrefix "") ''TypeNode)
39 data Attributes = Attributes { clust_default :: Int }
40 deriving (Show, Generic)
41 $(deriveJSON (unPrefix "") ''Attributes)
43 data Node = Node { node_size :: Int
44 , node_type :: TypeNode
47 , node_attributes :: Attributes
49 deriving (Show, Generic)
50 $(deriveJSON (unPrefix "node_") ''Node)
52 data Edge = Edge { edge_source :: Text
54 , edge_weight :: Double
57 deriving (Show, Generic)
58 $(deriveJSON (unPrefix "edge_") ''Edge)
60 data Graph = Graph { graph_nodes :: [Node]
61 , graph_edges :: [Edge]
63 deriving (Show, Generic)
64 $(deriveJSON (unPrefix "graph_") ''Graph)
65 -----------------------------------------------------------
66 -- Old Gargantext Version
68 data AttributesOld = AttributesOld { cl :: Int }
69 deriving (Show, Generic)
70 $(deriveJSON (unPrefix "") ''AttributesOld)
72 data NodeOld = NodeOld { no_id :: Int
73 , no_at :: AttributesOld
77 deriving (Show, Generic)
78 $(deriveJSON (unPrefix "no_") ''NodeOld)
80 data EdgeOld = EdgeOld { eo_s :: Int
84 deriving (Show, Generic)
85 $(deriveJSON (unPrefix "eo_") ''EdgeOld)
87 data GraphOld = GraphOld {
89 , go_nodes :: [NodeOld]
91 deriving (Show, Generic)
92 $(deriveJSON (unPrefix "go_") ''GraphOld)
94 ----------------------------------------------------------
97 graphOld2graph :: GraphOld -> Graph
98 graphOld2graph (GraphOld links nodes) = Graph (map nodeOld2node nodes) (zipWith linkOld2edge [1..] links)
100 nodeOld2node :: NodeOld -> Node
101 nodeOld2node (NodeOld no_id' (AttributesOld cl') no_s' no_lb')
102 = Node no_s' Terms (cs $ show no_id') no_lb' (Attributes cl')
104 linkOld2edge :: Int -> EdgeOld -> Edge
105 linkOld2edge n (EdgeOld eo_s' eo_t' eo_w') = Edge (cs $ show eo_s') (cs $ show eo_t') ((T.read $ T.unpack eo_w') :: Double) (cs $ show n)
108 graphOld2graphWithFiles :: FilePath -> FilePath -> IO ()
109 graphOld2graphWithFiles g1 g2 = do
110 -- GraphOld <- IO Fichier
111 graph <- DBL.readFile g1
112 let newGraph = case DA.decode graph :: Maybe GraphOld of
113 Nothing -> panic (T.pack "no graph")
116 DBL.writeFile g2 (DA.encode $ graphOld2graph newGraph)