FSharpGephiStreamer


Using FSharpGephiStreamer's Grammar module to prepare nodes and edges for streaming

The Grammar module provides a short set of semantics that allow the conversion of any node/edge type to gephi readable objects.

Suppose you have the following types for your nodes and edges

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
/// Record type that represents a custom node 
type MyNode = {
    Id    : int
    Label : string
    Size  : float
    Data  : string
    }

/// Record type that represents a custom edge 
type MyEdge = {
    Id :     int
    Source : int
    Target : int
    Weight : float    
    }

The attributes currently supported are:

  • Size
  • Color
  • EdgeType
  • PositionX
  • PositionY
  • PositionZ
  • Label
  • UserDef

This means you have full control over the size, position, color ,and labels of the graph. You could even use your own layout algorithms without being reliant on the ones implemented in gephi.

All you need is a converter function that returns a list of Grammar attributes. The following example does exactly that for the label, size, color, and data of the MyNode type and the size, edge type, and color of the MyEdge type:

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
///converts a MyNode type to a list of grammar attributes
let nodeConverter (node:MyNode) =
    [
        Grammar.Attribute.Label node.Label; 
        Grammar.Attribute.Size  node.Size; 
        Grammar.Attribute.Color (Colors.Table.StatisticalGraphics24.getRandomColor()); 
        Grammar.Attribute.UserDef ("UserData",node.Data); 
    ]

///converts a MyEdge type to a list of grammar attributes
let edgeConverter (edge:MyEdge) =
    [
        Grammar.Attribute.Size  edge.Weight; 
        Grammar.Attribute.EdgeType  Grammar.EdgeDirection.Undirected;             
        Grammar.Attribute.Color Colors.Table.Office.grey ;      
    ]
namespace FSharpGephiStreamer
type MyNode =
  {Id: int;
   Label: string;
   Size: float;
   Data: string;}


 Record type that represents a custom node
MyNode.Id: int
Multiple items
val int : value:'T -> int (requires member op_Explicit)

--------------------
type int = int32

--------------------
type int<'Measure> = int
MyNode.Label: string
Multiple items
val string : value:'T -> string

--------------------
type string = System.String
MyNode.Size: float
Multiple items
val float : value:'T -> float (requires member op_Explicit)

--------------------
type float = System.Double

--------------------
type float<'Measure> = float
Multiple items
MyNode.Data: string

--------------------
namespace Microsoft.FSharp.Data
type MyEdge =
  {Id: int;
   Source: int;
   Target: int;
   Weight: float;}


 Record type that represents a custom edge
MyEdge.Id: int
MyEdge.Source: int
MyEdge.Target: int
MyEdge.Weight: float
val nodeConverter : node:MyNode -> Grammar.Attribute list


converts a MyNode type to a list of grammar attributes
val node : MyNode
module Grammar

from FSharpGephiStreamer
type Attribute =
  | Size of float
  | Color of Color
  | EdgeType of EdgeDirection
  | PositionX of float
  | PositionY of float
  | PositionZ of float
  | Label of string
  | LabelSize of float
  | LabelColor of Color
  | LabelVisible of bool
  ...
union case Grammar.Attribute.Label: string -> Grammar.Attribute
union case Grammar.Attribute.Size: float -> Grammar.Attribute
union case Grammar.Attribute.Color: Colors.Color -> Grammar.Attribute
module Colors

from FSharpGephiStreamer
module Table

from FSharpGephiStreamer.Colors
module StatisticalGraphics24

from FSharpGephiStreamer.Colors.Table
val getRandomColor : unit -> Colors.Color
union case Grammar.Attribute.UserDef: string * obj -> Grammar.Attribute
MyNode.Data: string
val edgeConverter : edge:MyEdge -> Grammar.Attribute list


converts a MyEdge type to a list of grammar attributes
val edge : MyEdge
union case Grammar.Attribute.EdgeType: Grammar.EdgeDirection -> Grammar.Attribute
type EdgeDirection =
  | Directed
  | Undirected
    static member convert : (EdgeDirection -> bool)
union case Grammar.EdgeDirection.Undirected: Grammar.EdgeDirection
module Office

from FSharpGephiStreamer.Colors.Table
val grey : Colors.Color
Fork me on GitHub