]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Viz/Graph/GEXF.hs
Merge branch 'dev-default-extensions' into dev
[gargantext.git] / src / Gargantext / Viz / Graph / GEXF.hs
1 {-|
2 Module : Gargantext.Viz.Graph
3 Description :
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
8 Portability : POSIX
9
10 -}
11
12
13 {-# OPTIONS_GHC -fno-warn-orphans #-}
14
15 {-# LANGUAGE OverloadedLists #-} -- allows to write Map and HashMap as lists
16 {-# LANGUAGE TypeOperators #-}
17
18 module Gargantext.Viz.Graph.GEXF
19 where
20
21 import Gargantext.Prelude
22 import Gargantext.Viz.Graph
23 import qualified Data.HashMap.Lazy as HashMap
24 import qualified Gargantext.Prelude as P
25 import qualified Gargantext.Viz.Graph as G
26 import qualified Xmlbf as Xmlbf
27
28 -- Converts to GEXF format
29 -- See https://gephi.org/gexf/format/
30 instance Xmlbf.ToXml Graph where
31 toXml (Graph { _graph_nodes = graphNodes
32 , _graph_edges = graphEdges }) = root graphNodes graphEdges
33 where
34 root :: [G.Node] -> [G.Edge] -> [Xmlbf.Node]
35 root gn ge =
36 Xmlbf.element "gexf" params $ meta <> (graph gn ge)
37 where
38 params = HashMap.fromList [ ("xmlns", "http://www.gexf.net/1.2draft")
39 , ("version", "1.2") ]
40 meta = Xmlbf.element "meta" params $ creator <> desc
41 where
42 params = HashMap.fromList [ ("lastmodifieddate", "2020-03-13") ]
43 creator = Xmlbf.element "creator" HashMap.empty $ Xmlbf.text "Gargantext.org"
44 desc = Xmlbf.element "description" HashMap.empty $ Xmlbf.text "Gargantext gexf file"
45 graph :: [G.Node] -> [G.Edge] -> [Xmlbf.Node]
46 graph gn ge = Xmlbf.element "graph" params $ (nodes gn) <> (edges ge)
47 where
48 params = HashMap.fromList [ ("mode", "static")
49 , ("defaultedgetype", "directed") ]
50 nodes :: [G.Node] -> [Xmlbf.Node]
51 nodes gn = Xmlbf.element "nodes" HashMap.empty $ P.concatMap node' gn
52
53 node' :: G.Node -> [Xmlbf.Node]
54 node' (G.Node { node_id = nId, node_label = l }) =
55 Xmlbf.element "node" params []
56 where
57 params = HashMap.fromList [ ("id", nId)
58 , ("label", l) ]
59 edges :: [G.Edge] -> [Xmlbf.Node]
60 edges gn = Xmlbf.element "edges" HashMap.empty $ P.concatMap edge gn
61 edge :: G.Edge -> [Xmlbf.Node]
62 edge (G.Edge { edge_id = eId, edge_source = es, edge_target = et }) =
63 Xmlbf.element "edge" params []
64 where
65 params = HashMap.fromList [ ("id", eId)
66 , ("source", es)
67 , ("target", et) ]