Commit 597e9a73 authored by Przemyslaw Kaminski's avatar Przemyslaw Kaminski

[sigma.js] graph doesn't crash now

parent 84ca8906
...@@ -213,9 +213,13 @@ selectorWithSize _ _ = do ...@@ -213,9 +213,13 @@ selectorWithSize _ _ = do
performDiff :: Sigma.Sigma -> ST.SGraph -> Effect Unit performDiff :: Sigma.Sigma -> ST.SGraph -> Effect Unit
performDiff sigma g = do performDiff sigma g = do
if (Seq.null addEdges) && (Seq.null addNodes) && (Set.isEmpty removeEdges) && (Set.isEmpty removeNodes) then -- if (Seq.null addEdges) && (Seq.null addNodes) && (Set.isEmpty removeEdges) && (Set.isEmpty removeNodes) then
pure unit -- pure unit
else do -- else do
console.log2 "[performDiff] addNodes" addNodes
console.log2 "[performDiff] addEdges" $ A.fromFoldable addEdges
console.log2 "[performDiff] removeNodes" removeNodes
console.log2 "[performDiff] removeEdges" removeEdges
traverse_ (Graphology.addNode sigmaGraph) addNodes traverse_ (Graphology.addNode sigmaGraph) addNodes
traverse_ (Graphology.addEdge sigmaGraph) addEdges traverse_ (Graphology.addEdge sigmaGraph) addEdges
traverse_ (Graphology.removeEdge sigmaGraph) removeEdges traverse_ (Graphology.removeEdge sigmaGraph) removeEdges
......
...@@ -22,11 +22,23 @@ export function _mapNodes(g, fn) { ...@@ -22,11 +22,23 @@ export function _mapNodes(g, fn) {
}); });
} }
export function _forEachEdge(g, fn) {
return g.forEachEdge(function(name, attrs, source, target, sourceAttributes, targetAttributes, undirected) {
return fn({id: name,
source,
target,
...attrs});
});
}
// Almost the same as graphology.mapNodes but with a change that only // Almost the same as graphology.mapNodes but with a change that only
// 1 argument is passed: the whole node structure // 1 argument is passed: the whole node structure
// TODO Add source, target // https://graphology.github.io/iteration.html#mapedges
export function _mapEdges(g, fn) { export function _mapEdges(g, fn) {
return g.mapEdges(function(name, attrs) { return g.mapEdges(function(name, attrs, source, target, sourceAttributes, targetAttributes, undirected) {
return fn({id: name, ...attrs}); return fn({id: name,
source,
target,
...attrs});
}); });
} }
...@@ -8,6 +8,7 @@ module Gargantext.Hooks.Sigmax.Graphology where ...@@ -8,6 +8,7 @@ module Gargantext.Hooks.Sigmax.Graphology where
import Prelude import Prelude
import Data.Array as A import Data.Array as A
import Data.Function.Uncurried (Fn2, runFn2)
import Data.Sequence as Seq import Data.Sequence as Seq
import Data.Set as Set import Data.Set as Set
import Data.Traversable (traverse) import Data.Traversable (traverse)
...@@ -23,8 +24,9 @@ foreign import data Graph :: Type ...@@ -23,8 +24,9 @@ foreign import data Graph :: Type
foreign import _newGraph :: EffectFn1 Unit Graph foreign import _newGraph :: EffectFn1 Unit Graph
foreign import _addNode :: EffectFn3 Graph String (Record Types.Node) String foreign import _addNode :: EffectFn3 Graph String (Record Types.Node) String
foreign import _addEdge :: EffectFn4 Graph String String (Record Types.Edge) String foreign import _addEdge :: EffectFn4 Graph String String (Record Types.Edge) String
foreign import _mapNodes :: forall a. EffectFn2 Graph (Record Types.Node -> a) (Array a) foreign import _mapNodes :: forall a. Fn2 Graph (Record Types.Node -> a) (Array a)
foreign import _mapEdges :: forall a. EffectFn2 Graph (Record Types.Edge -> a) (Array a) foreign import _forEachEdge :: EffectFn2 Graph (Record Types.Edge -> Effect Unit) Unit
foreign import _mapEdges :: forall a. Fn2 Graph (Record Types.Edge -> a) (Array a)
newGraph :: Unit -> Effect Graph newGraph :: Unit -> Effect Graph
newGraph = runEffectFn1 _newGraph newGraph = runEffectFn1 _newGraph
...@@ -47,17 +49,18 @@ forEachNode :: Graph -> (Record Types.Node -> Effect Unit) -> Effect Unit ...@@ -47,17 +49,18 @@ forEachNode :: Graph -> (Record Types.Node -> Effect Unit) -> Effect Unit
-- TODO Check this: how does FFI translate function of two arguments -- TODO Check this: how does FFI translate function of two arguments
-- into PS \x y ? -- into PS \x y ?
forEachNode g fn = pure $ g ... "forEachNode" $ [\_ n -> fn n] forEachNode g fn = pure $ g ... "forEachNode" $ [\_ n -> fn n]
mapNodes :: Graph -> (Record Types.Node -> Record Types.Node) -> Effect (Array (Record Types.Node)) mapNodes :: forall a. Graph -> (Record Types.Node -> a) -> Array a
mapNodes = runEffectFn2 _mapNodes mapNodes = runFn2 _mapNodes
addEdge :: Graph -> Record Types.Edge -> Effect String addEdge :: Graph -> Record Types.Edge -> Effect String
addEdge g edge@{ source, target } = runEffectFn4 _addEdge g source target edge addEdge g edge@{ source, target } = runEffectFn4 _addEdge g source target edge
removeEdge :: Graph -> String -> Effect Unit removeEdge :: Graph -> String -> Effect Unit
removeEdge g eId = pure $ g ... "dropEdge" $ [eId] removeEdge g eId = pure $ g ... "dropEdge" $ [eId]
forEachEdge :: Graph -> (Record Types.Edge -> Effect Unit) -> Effect Unit forEachEdge :: Graph -> (Record Types.Edge -> Effect Unit) -> Effect Unit
forEachEdge g fn = pure $ g ... "forEachEdge" $ [\_ e -> fn e] forEachEdge = runEffectFn2 _forEachEdge
mapEdges :: Graph -> (Record Types.Edge -> Record Types.Edge) -> Effect (Array (Record Types.Edge)) --forEachEdge g fn = pure $ g ... "forEachEdge" $ [\_ e -> fn e]
mapEdges = runEffectFn2 _mapEdges mapEdges :: forall a. Graph -> (Record Types.Edge -> a) -> Array a
mapEdges = runFn2 _mapEdges
-- TODO Maybe our use of this function (`updateWithGraph`) in code is -- TODO Maybe our use of this function (`updateWithGraph`) in code is
-- too much. We convert Types.Graph into Graphology.Graph and then -- too much. We convert Types.Graph into Graphology.Graph and then
...@@ -99,20 +102,17 @@ edges_ g = g ... "edges" $ [] :: Array Types.EdgeId ...@@ -99,20 +102,17 @@ edges_ g = g ... "edges" $ [] :: Array Types.EdgeId
nodes_ :: Graph -> Array Types.NodeId nodes_ :: Graph -> Array Types.NodeId
nodes_ g = g ... "nodes" $ [] :: Array Types.NodeId nodes_ g = g ... "nodes" $ [] :: Array Types.NodeId
-- | Call `sigmaGraph.edges()` on a sigmajs graph instance. -- | `sigma.edges()` returns only edge keys, we need to map to get the full edge
edges :: Graph -> Effect (Seq.Seq (Record Types.Edge)) edges :: Graph -> Seq.Seq (Record Types.Edge)
edges g = do edges g = Seq.fromFoldable $ mapEdges g identity
edges' <- mapEdges g identity -- | `sigma.nodes()` returns only node keys, we need to map to get the full node
pure $ Seq.fromFoldable edges' nodes :: Graph -> Seq.Seq (Record Types.Node)
-- | Call `sigmaGraph.nodes()` on a sigmajs graph instance. nodes g = Seq.fromFoldable $ mapNodes g identity
nodes :: Graph -> Effect (Seq.Seq (Record Types.Node))
nodes g = do
nodes' <- mapNodes g identity
pure $ Seq.fromFoldable nodes'
-- | Fetch ids of graph edges in a sigmajs instance. -- | Fetch ids of graph edges in a sigmajs instance.
edgeIds :: Graph -> Types.EdgeIds edgeIds :: Graph -> Types.EdgeIds
edgeIds = Set.fromFoldable <<< edges_ -- auto-assigned edge ids are different from our edge ids
edgeIds g = Set.fromFoldable $ mapEdges g (\{ id } -> id) -- -<<< edges_
-- | Fetch ids of graph nodes in a sigmajs instance. -- | Fetch ids of graph nodes in a sigmajs instance.
nodeIds :: Graph -> Types.NodeIds nodeIds :: Graph -> Types.NodeIds
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment