whispervis/network.go

64 lines
1.3 KiB
Go
Raw Normal View History

2018-09-18 19:01:45 +03:00
//go:generate go-bindata data/
2018-09-18 17:35:16 +03:00
package main
import (
2018-09-18 19:01:45 +03:00
"bytes"
2018-09-18 17:35:16 +03:00
"fmt"
"github.com/divan/graphx/formats"
"github.com/divan/graphx/graph"
)
// Network represents network graph and information, used for
// for simulation and visualization.
type Network struct {
Name string
Description string
Data *graph.Graph
}
// LoadNetwork loads network information from the JSON file.
// JSON format is specified in graphx/formats package.
func LoadNetwork(file string) (*Network, error) {
desc := ""
2018-09-18 19:01:45 +03:00
content, err := Asset(file)
if err != nil {
return nil, fmt.Errorf("open bindata '%s': %v", file, err)
}
r := bytes.NewReader(content)
g, err := formats.FromD3JSONReader(r)
2018-09-18 17:35:16 +03:00
if err != nil {
return nil, fmt.Errorf("open '%s': %v", file, err)
}
return &Network{
2018-09-18 19:01:45 +03:00
Name: file,
2018-09-18 17:35:16 +03:00
Description: desc,
Data: g,
}, nil
}
2018-09-18 19:01:45 +03:00
// String implements Stringer for Network.
func (n *Network) String() string {
return fmt.Sprintf("[%s: %s] - %d nodes, %d links", n.Name, n.Description, n.NodesCount(), n.LinksCount())
}
// NodesCount returns number of the nodes in the network.
func (n *Network) NodesCount() int {
if n.Data == nil {
return 0
}
return len(n.Data.Nodes())
}
// LinksCount returns number of the links in the network.
func (n *Network) LinksCount() int {
if n.Data == nil {
return 0
}
return len(n.Data.Links())
}