2025-05-01 22:30:59 +00:00
|
|
|
/*
|
|
|
|
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 cmd
|
|
|
|
|
|
|
|
import (
|
2025-05-04 03:37:58 +00:00
|
|
|
"errors"
|
2025-05-02 05:04:00 +00:00
|
|
|
"fmt"
|
2025-05-01 22:30:59 +00:00
|
|
|
"os"
|
|
|
|
|
2025-05-09 04:32:59 +00:00
|
|
|
igraph "scm.dairydemon.net/filifa/gv2adj/cmd/internal/graph"
|
2025-05-07 02:24:28 +00:00
|
|
|
idot "scm.dairydemon.net/filifa/gv2adj/cmd/internal/graph/dot"
|
2025-05-02 01:00:45 +00:00
|
|
|
|
2025-05-01 22:30:59 +00:00
|
|
|
"github.com/spf13/cobra"
|
2025-05-02 01:00:45 +00:00
|
|
|
"gonum.org/v1/gonum/graph/encoding/dot"
|
2025-05-09 03:45:13 +00:00
|
|
|
dotfmt "gonum.org/v1/gonum/graph/formats/dot"
|
2025-05-02 05:04:00 +00:00
|
|
|
"gonum.org/v1/gonum/mat"
|
2025-05-01 22:30:59 +00:00
|
|
|
)
|
|
|
|
|
2025-05-02 01:00:45 +00:00
|
|
|
var file string
|
2025-05-01 22:30:59 +00:00
|
|
|
|
2025-05-04 02:42:09 +00:00
|
|
|
var matlabFmt bool
|
|
|
|
var pythonFmt bool
|
|
|
|
|
|
|
|
var oneline bool
|
|
|
|
|
2025-05-07 03:56:20 +00:00
|
|
|
var weightAttr string
|
|
|
|
|
2025-05-04 03:37:58 +00:00
|
|
|
var nodeOrder []string
|
|
|
|
|
2025-05-01 22:30:59 +00:00
|
|
|
// rootCmd represents the base command when called without any subcommands
|
|
|
|
var rootCmd = &cobra.Command{
|
2025-05-07 01:48:17 +00:00
|
|
|
Use: "gv2adj",
|
2025-05-10 02:09:23 +00:00
|
|
|
Short: "Compute adjacency matrices for Graphviz graphs",
|
|
|
|
Long: `Compute adjacency matrices (either weighted or unweighted) for Graphviz graphs (both directed and undirected).
|
2025-05-01 22:30:59 +00:00
|
|
|
|
2025-05-10 02:09:23 +00:00
|
|
|
Compute an unweighted adjacency matrix:
|
|
|
|
gv2adj -f <dot file>
|
|
|
|
|
|
|
|
Compute a weighted adjacency matrix, where weights are stored in the 'len' attribute of each edge:
|
|
|
|
gv2adj -f <dot file> --weight-attr len
|
|
|
|
`,
|
2025-05-01 22:30:59 +00:00
|
|
|
// Uncomment the following line if your bare application
|
|
|
|
// has an action associated with it:
|
2025-05-10 02:50:15 +00:00
|
|
|
RunE: parse,
|
2025-05-02 01:00:45 +00:00
|
|
|
}
|
|
|
|
|
2025-05-10 02:50:15 +00:00
|
|
|
func parse(cmd *cobra.Command, args []string) error {
|
2025-05-02 01:00:45 +00:00
|
|
|
data, err := os.ReadFile(file)
|
|
|
|
if err != nil {
|
2025-05-10 02:50:15 +00:00
|
|
|
return err
|
2025-05-02 01:00:45 +00:00
|
|
|
}
|
|
|
|
|
2025-05-09 03:45:13 +00:00
|
|
|
ast, err := dotfmt.ParseBytes(data)
|
|
|
|
if err != nil {
|
2025-05-10 02:50:15 +00:00
|
|
|
return err
|
2025-05-09 03:45:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
first := ast.Graphs[0]
|
|
|
|
var graph idot.DOTWeightedGraph
|
|
|
|
if first.Directed {
|
|
|
|
graph = idot.NewDOTDirectedGraph(weightAttr)
|
|
|
|
} else {
|
|
|
|
graph = idot.NewDOTUndirectedGraph(weightAttr)
|
|
|
|
}
|
|
|
|
|
2025-05-04 17:24:48 +00:00
|
|
|
err = dot.UnmarshalMulti(data, graph)
|
2025-05-02 01:00:45 +00:00
|
|
|
if err != nil {
|
2025-05-10 02:50:15 +00:00
|
|
|
return err
|
2025-05-02 01:00:45 +00:00
|
|
|
}
|
|
|
|
|
2025-05-09 04:32:59 +00:00
|
|
|
matrix, err := orderedAdjMatrix(graph.WeightedGraph)
|
2025-05-04 03:37:58 +00:00
|
|
|
if err != nil {
|
2025-05-10 02:50:15 +00:00
|
|
|
return err
|
2025-05-04 03:37:58 +00:00
|
|
|
}
|
|
|
|
|
2025-05-04 03:42:02 +00:00
|
|
|
outputMatrix(matrix)
|
2025-05-10 02:50:15 +00:00
|
|
|
return nil
|
2025-05-01 22:30:59 +00:00
|
|
|
}
|
|
|
|
|
2025-05-09 04:32:59 +00:00
|
|
|
func orderedAdjMatrix(g igraph.WeightedGraph) (*mat.Dense, error) {
|
|
|
|
matrix := igraph.AdjacencyMatrix(g)
|
2025-05-04 03:37:58 +00:00
|
|
|
if len(nodeOrder) == 0 {
|
|
|
|
return matrix, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeIndexes := make(map[string]int)
|
|
|
|
for i := 0; i < len(nodeOrder); i++ {
|
|
|
|
nodeIndexes[nodeOrder[i]] = i
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes := g.Nodes()
|
|
|
|
newOrder := make([]int, nodes.Len())
|
|
|
|
for nodes.Next() {
|
2025-05-07 02:24:28 +00:00
|
|
|
node := nodes.Node().(*idot.Node)
|
2025-05-04 03:37:58 +00:00
|
|
|
id := node.DOTID()
|
|
|
|
|
|
|
|
var ok bool
|
|
|
|
newOrder[node.ID()], ok = nodeIndexes[id]
|
|
|
|
if !ok {
|
2025-05-10 03:00:53 +00:00
|
|
|
return nil, errors.New("node '" + id + "' not found in given order")
|
2025-05-04 03:37:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
matrix.PermuteRows(newOrder, true)
|
|
|
|
matrix.PermuteCols(newOrder, true)
|
|
|
|
|
|
|
|
return matrix, nil
|
|
|
|
}
|
|
|
|
|
2025-05-04 03:42:02 +00:00
|
|
|
func outputMatrix(matrix mat.Matrix) {
|
|
|
|
fmtOptions := make([]mat.FormatOption, 0)
|
|
|
|
if matlabFmt {
|
|
|
|
fmtOptions = append(fmtOptions, mat.FormatMATLAB())
|
|
|
|
} else if pythonFmt {
|
|
|
|
fmtOptions = append(fmtOptions, mat.FormatPython())
|
|
|
|
}
|
|
|
|
|
|
|
|
out := mat.Formatted(matrix, fmtOptions...)
|
|
|
|
|
|
|
|
// for matlab and python formats, %#v outputs as matrix and %v is
|
|
|
|
// oneline, but for standard format, it's the opposite, so we xor
|
|
|
|
if (matlabFmt || pythonFmt) != oneline {
|
2025-05-10 04:04:36 +00:00
|
|
|
fmt.Printf("%#.6v\n", out)
|
2025-05-04 03:42:02 +00:00
|
|
|
} else {
|
2025-05-10 04:04:36 +00:00
|
|
|
fmt.Printf("%.6v\n", out)
|
2025-05-04 03:42:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-05-01 22:30:59 +00:00
|
|
|
// Execute adds all child commands to the root command and sets flags appropriately.
|
|
|
|
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
|
|
|
func Execute() {
|
|
|
|
err := rootCmd.Execute()
|
|
|
|
if err != nil {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2025-05-07 02:46:03 +00:00
|
|
|
rootCmd.Flags().StringVarP(&file, "file", "f", "", "dot file")
|
2025-05-02 01:00:45 +00:00
|
|
|
rootCmd.MarkFlagRequired("file")
|
2025-05-04 02:42:09 +00:00
|
|
|
|
|
|
|
rootCmd.Flags().BoolVar(&matlabFmt, "matlab", false, "format output as MATLAB array")
|
|
|
|
rootCmd.Flags().BoolVar(&pythonFmt, "python", false, "format output as python array")
|
|
|
|
rootCmd.MarkFlagsMutuallyExclusive("matlab", "python")
|
|
|
|
|
|
|
|
rootCmd.Flags().BoolVar(&oneline, "oneline", false, "output on one line")
|
2025-05-04 03:37:58 +00:00
|
|
|
|
2025-05-10 02:09:39 +00:00
|
|
|
rootCmd.Flags().StringVarP(&weightAttr, "weight-attr", "w", "", "edge attribute to use as weight")
|
2025-05-07 03:56:20 +00:00
|
|
|
|
2025-05-10 02:09:39 +00:00
|
|
|
rootCmd.Flags().StringSliceVarP(&nodeOrder, "order", "o", nil, "list the graph node names in the order they should be placed in the matrix")
|
2025-05-02 01:00:45 +00:00
|
|
|
}
|