2022-11-26 06:32:04 +00:00
|
|
|
package d2sequence
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2022-12-01 00:11:02 +00:00
|
|
|
"strings"
|
2022-11-26 06:32:04 +00:00
|
|
|
|
|
|
|
|
"oss.terrastruct.com/d2/d2graph"
|
2022-12-01 00:11:02 +00:00
|
|
|
"oss.terrastruct.com/d2/d2target"
|
2022-11-26 06:32:04 +00:00
|
|
|
"oss.terrastruct.com/d2/lib/geo"
|
2022-12-02 17:11:02 +00:00
|
|
|
"oss.terrastruct.com/d2/lib/label"
|
|
|
|
|
"oss.terrastruct.com/util-go/go2"
|
2022-11-26 06:32:04 +00:00
|
|
|
)
|
|
|
|
|
|
2022-12-02 01:11:41 +00:00
|
|
|
// Layout identifies and performs layout on sequence diagrams within a graph
|
2022-12-01 21:27:53 +00:00
|
|
|
// first, it traverses the graph from Root and once it finds an object of shape `sequence_diagram`
|
|
|
|
|
// it replaces the children with a rectangle with id `sequence_diagram`, collects all edges coming to this node and
|
|
|
|
|
// flag the edges to be removed. Then, using the children and the edges, it lays out the sequence diagram and
|
|
|
|
|
// sets the dimensions of the rectangle `sequence_diagram` rectangle.
|
|
|
|
|
// Once all nodes were processed, it continues to run the layout engine without the sequence diagram nodes and edges.
|
|
|
|
|
// Then it restores all objects with their proper layout engine and sequence diagram positions
|
2022-12-01 19:47:27 +00:00
|
|
|
func Layout(ctx context.Context, g *d2graph.Graph, layout func(ctx context.Context, g *d2graph.Graph) error) error {
|
2022-12-01 21:39:48 +00:00
|
|
|
// flag objects to keep to avoid having to flag all descendants of sequence diagram to be removed
|
|
|
|
|
objectsToKeep := make(map[*d2graph.Object]struct{})
|
2022-12-01 21:27:53 +00:00
|
|
|
// edges flagged to be removed (these are internal edges of the sequence diagrams)
|
2022-12-01 00:11:02 +00:00
|
|
|
edgesToRemove := make(map[*d2graph.Edge]struct{})
|
2022-12-01 21:27:53 +00:00
|
|
|
// store the sequence diagram related to a given node
|
2022-12-02 02:08:34 +00:00
|
|
|
sequenceDiagrams := make(map[string]*sequenceDiagram)
|
2022-12-01 21:27:53 +00:00
|
|
|
// keeps the reference of the children of a given node
|
2022-12-02 02:08:34 +00:00
|
|
|
childrenReplacement := make(map[string][]*d2graph.Object)
|
2022-12-01 00:11:02 +00:00
|
|
|
|
2022-12-02 17:11:02 +00:00
|
|
|
// starts in root and traverses all descendants
|
2022-12-02 02:08:34 +00:00
|
|
|
queue := make([]*d2graph.Object, 1, len(g.Objects))
|
2022-12-01 00:11:02 +00:00
|
|
|
queue[0] = g.Root
|
|
|
|
|
for len(queue) > 0 {
|
|
|
|
|
obj := queue[0]
|
|
|
|
|
queue = queue[1:]
|
|
|
|
|
|
2022-12-01 21:39:48 +00:00
|
|
|
objectsToKeep[obj] = struct{}{}
|
2022-12-01 00:11:02 +00:00
|
|
|
if obj.Attributes.Shape.Value == d2target.ShapeSequenceDiagram {
|
|
|
|
|
// clean current children and keep a backup to restore them later
|
|
|
|
|
obj.Children = make(map[string]*d2graph.Object)
|
2022-12-02 02:08:34 +00:00
|
|
|
children := obj.ChildrenArray
|
2022-12-01 00:11:02 +00:00
|
|
|
obj.ChildrenArray = nil
|
|
|
|
|
|
2022-12-02 17:11:02 +00:00
|
|
|
// find the edges that belong to this sequence diagram
|
2022-12-01 21:27:53 +00:00
|
|
|
var edges []*d2graph.Edge
|
2022-12-01 00:11:02 +00:00
|
|
|
for _, edge := range g.Edges {
|
2022-12-01 21:27:53 +00:00
|
|
|
// both Src and Dst must be inside the sequence diagram
|
2022-12-01 00:11:02 +00:00
|
|
|
if strings.HasPrefix(edge.Src.AbsID(), obj.AbsID()) && strings.HasPrefix(edge.Dst.AbsID(), obj.AbsID()) {
|
|
|
|
|
edgesToRemove[edge] = struct{}{}
|
2022-12-01 21:27:53 +00:00
|
|
|
edges = append(edges, edge)
|
2022-12-01 00:11:02 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-02 02:08:34 +00:00
|
|
|
sd := newSequenceDiagram(children, edges)
|
2022-12-01 00:11:02 +00:00
|
|
|
sd.layout()
|
2022-12-02 17:11:02 +00:00
|
|
|
obj.Width = sd.getWidth()
|
|
|
|
|
obj.Height = sd.getHeight()
|
|
|
|
|
sequenceDiagrams[obj.AbsID()] = sd
|
|
|
|
|
childrenReplacement[obj.AbsID()] = children
|
2022-12-01 00:11:02 +00:00
|
|
|
} else {
|
2022-12-01 21:27:53 +00:00
|
|
|
// only move to children if the parent is not a sequence diagram
|
2022-12-01 00:11:02 +00:00
|
|
|
queue = append(queue, obj.ChildrenArray...)
|
|
|
|
|
}
|
2022-11-28 22:01:22 +00:00
|
|
|
}
|
2022-11-26 06:32:04 +00:00
|
|
|
|
2022-12-01 21:27:53 +00:00
|
|
|
// removes the edges
|
2022-12-02 02:08:34 +00:00
|
|
|
layoutEdges := make([]*d2graph.Edge, 0, len(g.Edges)-len(edgesToRemove))
|
2022-12-01 00:11:02 +00:00
|
|
|
for _, edge := range g.Edges {
|
|
|
|
|
if _, exists := edgesToRemove[edge]; !exists {
|
2022-12-02 02:08:34 +00:00
|
|
|
layoutEdges = append(layoutEdges, edge)
|
2022-12-01 00:11:02 +00:00
|
|
|
}
|
|
|
|
|
}
|
2022-12-02 02:08:34 +00:00
|
|
|
g.Edges = layoutEdges
|
2022-12-01 00:11:02 +00:00
|
|
|
|
2022-12-02 17:11:02 +00:00
|
|
|
// done this way (by flagging objects) instead of appending while going through the `queue`
|
2022-12-01 21:39:48 +00:00
|
|
|
// because appending in that order would change the order of g.Objects which
|
|
|
|
|
// could lead to layout changes (as the order of the objects might be important for the underlying engine)
|
2022-12-02 02:08:34 +00:00
|
|
|
layoutObjects := make([]*d2graph.Object, 0, len(objectsToKeep))
|
2022-12-01 21:39:48 +00:00
|
|
|
for _, obj := range g.Objects {
|
|
|
|
|
if _, exists := objectsToKeep[obj]; exists {
|
2022-12-02 02:08:34 +00:00
|
|
|
layoutObjects = append(layoutObjects, obj)
|
2022-12-01 21:39:48 +00:00
|
|
|
}
|
|
|
|
|
}
|
2022-12-02 02:08:34 +00:00
|
|
|
g.Objects = layoutObjects
|
2022-12-01 21:27:53 +00:00
|
|
|
|
2022-12-02 01:11:41 +00:00
|
|
|
if g.Root.Attributes.Shape.Value == d2target.ShapeSequenceDiagram {
|
|
|
|
|
// don't need to run the layout engine if the root is a sequence diagram
|
|
|
|
|
g.Root.ChildrenArray[0].TopLeft = geo.NewPoint(0, 0)
|
|
|
|
|
} else if err := layout(ctx, g); err != nil {
|
2022-12-01 00:11:02 +00:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-02 02:08:34 +00:00
|
|
|
// restores objects
|
|
|
|
|
for _, obj := range g.Objects {
|
|
|
|
|
if _, exists := sequenceDiagrams[obj.AbsID()]; !exists {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2022-12-02 17:11:02 +00:00
|
|
|
obj.LabelPosition = go2.Pointer(string(label.InsideTopCenter))
|
2022-12-01 21:27:53 +00:00
|
|
|
// shift the sequence diagrams as they are always placed at (0, 0)
|
2022-12-02 17:11:02 +00:00
|
|
|
sequenceDiagrams[obj.AbsID()].shift(obj.TopLeft)
|
2022-12-01 21:27:53 +00:00
|
|
|
|
|
|
|
|
// restore children
|
2022-12-02 17:11:02 +00:00
|
|
|
obj.Children = make(map[string]*d2graph.Object)
|
|
|
|
|
for _, child := range childrenReplacement[obj.AbsID()] {
|
|
|
|
|
obj.Children[child.ID] = child
|
2022-12-01 00:11:02 +00:00
|
|
|
}
|
2022-12-02 17:11:02 +00:00
|
|
|
obj.ChildrenArray = childrenReplacement[obj.AbsID()]
|
2022-12-01 21:27:53 +00:00
|
|
|
|
|
|
|
|
// add lifeline edges
|
2022-12-02 17:11:02 +00:00
|
|
|
g.Edges = append(g.Edges, sequenceDiagrams[obj.AbsID()].lifelines...)
|
|
|
|
|
g.Edges = append(g.Edges, sequenceDiagrams[obj.AbsID()].messages...)
|
|
|
|
|
g.Objects = append(g.Objects, sequenceDiagrams[obj.AbsID()].actors...)
|
|
|
|
|
g.Objects = append(g.Objects, sequenceDiagrams[obj.AbsID()].spans...)
|
2022-12-01 00:11:02 +00:00
|
|
|
}
|
2022-11-26 06:32:04 +00:00
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|