mirror of
https://github.com/gonum/gonum.git
synced 2025-10-06 15:47:01 +08:00
@@ -208,26 +208,13 @@ func applyPortsToEdge(from ast.Vertex, to *ast.Edge, edge graph.Edge) {
|
|||||||
// addEdgeStmt adds the given edge statement to the graph.
|
// addEdgeStmt adds the given edge statement to the graph.
|
||||||
func (gen *generator) addEdgeStmt(dst encoding.Builder, stmt *ast.EdgeStmt) {
|
func (gen *generator) addEdgeStmt(dst encoding.Builder, stmt *ast.EdgeStmt) {
|
||||||
fs := gen.addVertex(dst, stmt.From)
|
fs := gen.addVertex(dst, stmt.From)
|
||||||
ts := gen.addEdge(dst, stmt.To)
|
ts := gen.addEdge(dst, stmt.To, stmt.Attrs)
|
||||||
for _, f := range fs {
|
for _, f := range fs {
|
||||||
for _, t := range ts {
|
for _, t := range ts {
|
||||||
edge := dst.NewEdge(f, t)
|
edge := dst.NewEdge(f, t)
|
||||||
dst.SetEdge(edge)
|
dst.SetEdge(edge)
|
||||||
applyPortsToEdge(stmt.From, stmt.To, edge)
|
applyPortsToEdge(stmt.From, stmt.To, edge)
|
||||||
|
addEdgeAttrs(edge, stmt.Attrs)
|
||||||
e, ok := edge.(encoding.AttributeSetter)
|
|
||||||
if !ok {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
for _, attr := range stmt.Attrs {
|
|
||||||
a := encoding.Attribute{
|
|
||||||
Key: attr.Key,
|
|
||||||
Value: attr.Val,
|
|
||||||
}
|
|
||||||
if err := e.SetAttribute(a); err != nil {
|
|
||||||
panic(fmt.Errorf("unable to unmarshal edge DOT attribute (%s=%s)", a.Key, a.Value))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -250,18 +237,19 @@ func (gen *generator) addVertex(dst encoding.Builder, v ast.Vertex) []graph.Node
|
|||||||
}
|
}
|
||||||
|
|
||||||
// addEdge adds the given edge to the graph, and returns its set of nodes.
|
// addEdge adds the given edge to the graph, and returns its set of nodes.
|
||||||
func (gen *generator) addEdge(dst encoding.Builder, to *ast.Edge) []graph.Node {
|
func (gen *generator) addEdge(dst encoding.Builder, to *ast.Edge, attrs []*ast.Attr) []graph.Node {
|
||||||
if !gen.directed && to.Directed {
|
if !gen.directed && to.Directed {
|
||||||
panic(fmt.Errorf("directed edge to %v in undirected graph", to.Vertex))
|
panic(fmt.Errorf("directed edge to %v in undirected graph", to.Vertex))
|
||||||
}
|
}
|
||||||
fs := gen.addVertex(dst, to.Vertex)
|
fs := gen.addVertex(dst, to.Vertex)
|
||||||
if to.To != nil {
|
if to.To != nil {
|
||||||
ts := gen.addEdge(dst, to.To)
|
ts := gen.addEdge(dst, to.To, attrs)
|
||||||
for _, f := range fs {
|
for _, f := range fs {
|
||||||
for _, t := range ts {
|
for _, t := range ts {
|
||||||
edge := dst.NewEdge(f, t)
|
edge := dst.NewEdge(f, t)
|
||||||
dst.SetEdge(edge)
|
dst.SetEdge(edge)
|
||||||
applyPortsToEdge(to.Vertex, to.To, edge)
|
applyPortsToEdge(to.Vertex, to.To, edge)
|
||||||
|
addEdgeAttrs(edge, attrs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -318,3 +306,20 @@ func (gen *generator) isInSubgraph() bool {
|
|||||||
func (gen *generator) appendSubgraphNode(n graph.Node) {
|
func (gen *generator) appendSubgraphNode(n graph.Node) {
|
||||||
gen.subNodes = append(gen.subNodes, n)
|
gen.subNodes = append(gen.subNodes, n)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// addEdgeAttrs adds the attributes to the given edge.
|
||||||
|
func addEdgeAttrs(edge graph.Edge, attrs []*ast.Attr) {
|
||||||
|
e, ok := edge.(encoding.AttributeSetter)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, attr := range attrs {
|
||||||
|
a := encoding.Attribute{
|
||||||
|
Key: attr.Key,
|
||||||
|
Value: attr.Val,
|
||||||
|
}
|
||||||
|
if err := e.SetAttribute(a); err != nil {
|
||||||
|
panic(fmt.Errorf("unable to unmarshal edge DOT attribute (%s=%s)", a.Key, a.Value))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user