]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Core/Viz/Graph/GEXF.hs
[FEAT] Enabling Phylo 1 click
[gargantext.git] / src / Gargantext / Core / Viz / Graph / GEXF.hs
1 {-|
2 Module : Gargantext.Core.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.Core.Viz.Graph.GEXF
19 where
20
21 import Gargantext.Prelude
22 import qualified Data.HashMap.Lazy as HashMap
23 import qualified Gargantext.Prelude as P
24 import qualified Gargantext.Core.Viz.Graph.Types as G
25 import qualified Xmlbf as Xmlbf
26 import Prelude (error)
27
28 -- Converts to GEXF format
29 -- See https://gephi.org/gexf/format/
30 instance Xmlbf.ToXml G.Graph where
31 toXml (G.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.3")
39 , ("xmlns:viz", "http://gexf.net/1.3/viz")
40 , ("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
41 , ("xsi:schemaLocation", "http://gexf.net/1.3 http://gexf.net/1.3/gexf.xsd")
42 , ("version", "1.3") ]
43 meta = Xmlbf.element "meta" params $ creator <> desc
44 where
45 params = HashMap.fromList [ ("lastmodifieddate", "2020-03-13") ]
46 creator = Xmlbf.element "creator" HashMap.empty $ Xmlbf.text "Gargantext.org"
47 desc = Xmlbf.element "description" HashMap.empty $ Xmlbf.text "Gargantext gexf file"
48 graph :: [G.Node] -> [G.Edge] -> [Xmlbf.Node]
49 graph gn ge = Xmlbf.element "graph" params $ (nodes gn) <> (edges ge)
50 where
51 params = HashMap.fromList [ ("mode", "static")
52 , ("defaultedgetype", "directed") ]
53 nodes :: [G.Node] -> [Xmlbf.Node]
54 nodes gn = Xmlbf.element "nodes" HashMap.empty $ P.concatMap node' gn
55
56 node' :: G.Node -> [Xmlbf.Node]
57 node' (G.Node { node_id = nId, node_label = l, node_size = w}) =
58 Xmlbf.element "node" params (Xmlbf.element "viz:size" sizeParams [])
59 where
60 params = HashMap.fromList [ ("id", nId)
61 , ("label", l) ]
62 sizeParams = HashMap.fromList [ ("value", (cs . show) w) ]
63 edges :: [G.Edge] -> [Xmlbf.Node]
64 edges gn = Xmlbf.element "edges" HashMap.empty $ P.concatMap edge gn
65 edge :: G.Edge -> [Xmlbf.Node]
66 edge (G.Edge { edge_id = eId
67 , edge_source = es
68 , edge_target = et
69 , edge_weight = ew }) =
70 Xmlbf.element "edge" params []
71 where
72 params = HashMap.fromList [ ("id", eId)
73 , ("source", es)
74 , ("target", et)
75 , ("weight", (cs . show) ew)]
76
77 -- just to be able to derive a client for the entire gargantext API,
78 -- we however want to avoid sollicitating this instance
79 instance Xmlbf.FromXml G.Graph where
80 fromXml = error "FromXml Graph: not defined, just a placeholder"