add argument for setting weight attribute
This commit is contained in:
@@ -28,17 +28,18 @@ import (
|
||||
// DOTDirectedGraph is a graph to unmarshal DOT graphs.
|
||||
type DOTDirectedGraph struct {
|
||||
*igraph.DirectedGraph
|
||||
WeightAttribute string
|
||||
}
|
||||
|
||||
// NewDOTDirectedGraph returns a graph with no nodes or edges.
|
||||
func NewDOTDirectedGraph() *DOTDirectedGraph {
|
||||
return &DOTDirectedGraph{DirectedGraph: igraph.NewDirectedGraph()}
|
||||
func NewDOTDirectedGraph(weightAttr string) *DOTDirectedGraph {
|
||||
return &DOTDirectedGraph{DirectedGraph: igraph.NewDirectedGraph(), WeightAttribute: weightAttr}
|
||||
}
|
||||
|
||||
// NewLine returns a DOT-aware weighted line.
|
||||
func (g *DOTDirectedGraph) NewLine(from, to graph.Node) graph.Line {
|
||||
e := g.DirectedGraph.NewWeightedLine(from, to, math.NaN()).(multi.WeightedLine)
|
||||
return &weightedLine{WeightedLine: e}
|
||||
return &weightedLine{WeightedLine: e, weightAttribute: g.WeightAttribute}
|
||||
}
|
||||
|
||||
// NewNode returns a DOT-aware Node.
|
||||
|
||||
@@ -46,6 +46,7 @@ func (n *Node) DOTID() string {
|
||||
// multi.WeightedLine, it allows for self-loops.
|
||||
type weightedLine struct {
|
||||
multi.WeightedLine
|
||||
weightAttribute string
|
||||
}
|
||||
|
||||
// SetAttribute enables storing the weight read from a DOT file. It errors if
|
||||
@@ -54,7 +55,7 @@ func (e *weightedLine) SetAttribute(attr encoding.Attribute) error {
|
||||
var err error
|
||||
|
||||
switch attr.Key {
|
||||
case "len":
|
||||
case e.weightAttribute:
|
||||
e.W, err = strconv.ParseFloat(attr.Value, 64)
|
||||
default:
|
||||
err = errors.New("unknown key:" + attr.Key)
|
||||
|
||||
Reference in New Issue
Block a user