2 Module : Gargantext.Core.Viz.Graph
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
13 {-# OPTIONS_GHC -fno-warn-orphans #-}
15 {-# LANGUAGE OverloadedLists #-} -- allows to write Map and HashMap as lists
16 {-# LANGUAGE TypeOperators #-}
18 module Gargantext.Core.Viz.Graph.GEXF
21 import Gargantext.Prelude
22 import Gargantext.Core.Viz.Graph
23 import qualified Data.HashMap.Lazy as HashMap
24 import qualified Gargantext.Prelude as P
25 import qualified Gargantext.Core.Viz.Graph as G
26 import qualified Xmlbf as Xmlbf
27 import Prelude (error)
29 -- Converts to GEXF format
30 -- See https://gephi.org/gexf/format/
31 instance Xmlbf.ToXml Graph where
32 toXml (Graph { _graph_nodes = graphNodes
33 , _graph_edges = graphEdges }) = root graphNodes graphEdges
35 root :: [G.Node] -> [G.Edge] -> [Xmlbf.Node]
37 Xmlbf.element "gexf" params $ meta <> (graph gn ge)
39 params = HashMap.fromList [ ("xmlns", "http://www.gexf.net/1.2draft")
40 , ("version", "1.2") ]
41 meta = Xmlbf.element "meta" params $ creator <> desc
43 params = HashMap.fromList [ ("lastmodifieddate", "2020-03-13") ]
44 creator = Xmlbf.element "creator" HashMap.empty $ Xmlbf.text "Gargantext.org"
45 desc = Xmlbf.element "description" HashMap.empty $ Xmlbf.text "Gargantext gexf file"
46 graph :: [G.Node] -> [G.Edge] -> [Xmlbf.Node]
47 graph gn ge = Xmlbf.element "graph" params $ (nodes gn) <> (edges ge)
49 params = HashMap.fromList [ ("mode", "static")
50 , ("defaultedgetype", "directed") ]
51 nodes :: [G.Node] -> [Xmlbf.Node]
52 nodes gn = Xmlbf.element "nodes" HashMap.empty $ P.concatMap node' gn
54 node' :: G.Node -> [Xmlbf.Node]
55 node' (G.Node { node_id = nId, node_label = l, node_size = w}) =
56 Xmlbf.element "node" params []
58 params = HashMap.fromList [ ("id", nId)
60 , ("size", (cs . show) w)]
61 edges :: [G.Edge] -> [Xmlbf.Node]
62 edges gn = Xmlbf.element "edges" HashMap.empty $ P.concatMap edge gn
63 edge :: G.Edge -> [Xmlbf.Node]
64 edge (G.Edge { edge_id = eId, edge_source = es, edge_target = et }) =
65 Xmlbf.element "edge" params []
67 params = HashMap.fromList [ ("id", eId)
71 -- just to be able to derive a client for the entire gargantext API,
72 -- we however want to avoid sollicitating this instance
73 instance Xmlbf.FromXml Graph where
74 fromXml = error "FromXml Graph: not defined, just a placeholder"