parse differently depending on graph type
This commit is contained in:
parent
339fdc0bcf
commit
2013e26fb9
|
@ -23,6 +23,7 @@ import (
|
||||||
|
|
||||||
type WeightedGraph interface {
|
type WeightedGraph interface {
|
||||||
graph.Weighted
|
graph.Weighted
|
||||||
|
graph.WeightedMultigraph
|
||||||
graph.WeightedMultigraphBuilder
|
graph.WeightedMultigraphBuilder
|
||||||
|
|
||||||
WeightedEdges() graph.WeightedEdges
|
WeightedEdges() graph.WeightedEdges
|
||||||
|
|
|
@ -41,7 +41,7 @@ func NewDOTUndirectedGraph(weightAttr string) DOTWeightedGraph {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewLine returns a DOT-aware weighted line.
|
// NewLine returns a DOT-aware weighted line.
|
||||||
func (g *DOTWeightedGraph) NewLine(from, to graph.Node) graph.Line {
|
func (g DOTWeightedGraph) NewLine(from, to graph.Node) graph.Line {
|
||||||
var defaultWeight float64
|
var defaultWeight float64
|
||||||
if g.WeightAttribute == "" {
|
if g.WeightAttribute == "" {
|
||||||
defaultWeight = 1
|
defaultWeight = 1
|
||||||
|
@ -54,11 +54,11 @@ func (g *DOTWeightedGraph) NewLine(from, to graph.Node) graph.Line {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewNode returns a DOT-aware Node.
|
// NewNode returns a DOT-aware Node.
|
||||||
func (g *DOTWeightedGraph) NewNode() graph.Node {
|
func (g DOTWeightedGraph) NewNode() graph.Node {
|
||||||
return &Node{Node: g.WeightedGraph.NewNode()}
|
return &Node{Node: g.WeightedGraph.NewNode()}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetLine adds a DOT-aware weighted line to the graph.
|
// SetLine adds a DOT-aware weighted line to the graph.
|
||||||
func (g *DOTWeightedGraph) SetLine(e graph.Line) {
|
func (g DOTWeightedGraph) SetLine(e graph.Line) {
|
||||||
g.WeightedGraph.SetWeightedLine(e.(*weightedLine))
|
g.WeightedGraph.SetWeightedLine(e.(*weightedLine))
|
||||||
}
|
}
|
||||||
|
|
17
cmd/root.go
17
cmd/root.go
|
@ -25,6 +25,7 @@ import (
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"gonum.org/v1/gonum/graph/encoding/dot"
|
"gonum.org/v1/gonum/graph/encoding/dot"
|
||||||
|
dotfmt "gonum.org/v1/gonum/graph/formats/dot"
|
||||||
"gonum.org/v1/gonum/mat"
|
"gonum.org/v1/gonum/mat"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -60,7 +61,19 @@ func parse(cmd *cobra.Command, args []string) {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
graph := idot.NewDOTDirectedGraph(weightAttr)
|
ast, err := dotfmt.ParseBytes(data)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
first := ast.Graphs[0]
|
||||||
|
var graph idot.DOTWeightedGraph
|
||||||
|
if first.Directed {
|
||||||
|
graph = idot.NewDOTDirectedGraph(weightAttr)
|
||||||
|
} else {
|
||||||
|
graph = idot.NewDOTUndirectedGraph(weightAttr)
|
||||||
|
}
|
||||||
|
|
||||||
err = dot.UnmarshalMulti(data, graph)
|
err = dot.UnmarshalMulti(data, graph)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
@ -74,7 +87,7 @@ func parse(cmd *cobra.Command, args []string) {
|
||||||
outputMatrix(matrix)
|
outputMatrix(matrix)
|
||||||
}
|
}
|
||||||
|
|
||||||
func orderedAdjMatrix(g *idot.DOTDirectedGraph) (*mat.Dense, error) {
|
func orderedAdjMatrix(g idot.DOTWeightedGraph) (*mat.Dense, error) {
|
||||||
matrix := g.AdjacencyMatrix()
|
matrix := g.AdjacencyMatrix()
|
||||||
if len(nodeOrder) == 0 {
|
if len(nodeOrder) == 0 {
|
||||||
return matrix, nil
|
return matrix, nil
|
||||||
|
|
Loading…
Reference in New Issue