gv2adj/cmd/internal/graph/dot/nodes_edges.go

66 lines
1.6 KiB
Go
Raw Normal View History

/*
Copyright © 2025 filifa
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package dot
import (
"errors"
"strconv"
"gonum.org/v1/gonum/graph"
"gonum.org/v1/gonum/graph/encoding"
"gonum.org/v1/gonum/graph/multi"
)
// Node is a DOT-aware graph.Node.
type Node struct {
graph.Node
dotID string
}
// SetDOTID sets the node's DOT ID. It enables storing the node name read from
// a DOT file.
func (n *Node) SetDOTID(id string) {
n.dotID = id
}
// DOTID returns the node's DOT ID.
func (n *Node) DOTID() string {
return n.dotID
}
2025-05-04 17:24:48 +00:00
// weightedLine is a DOT-aware multi.WeightedLine. By being a
2025-05-07 02:46:03 +00:00
// multi.WeightedLine, it allows for self-loops.
2025-05-04 17:24:48 +00:00
type weightedLine struct {
multi.WeightedLine
weightAttribute string
}
// SetAttribute enables storing the weight read from a DOT file. It errors if
2025-05-04 17:24:48 +00:00
// an attribute is read that can't be stored in a weightedLine.
func (e *weightedLine) SetAttribute(attr encoding.Attribute) error {
var err error
switch attr.Key {
case e.weightAttribute:
e.W, err = strconv.ParseFloat(attr.Value, 64)
default:
err = errors.New("unknown key:" + attr.Key)
}
return err
}