2022-11-26 06:32:04 +00:00
|
|
|
package d2sequence
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"math"
|
2022-11-29 03:20:53 +00:00
|
|
|
"sort"
|
2022-11-26 06:32:04 +00:00
|
|
|
|
|
|
|
|
"oss.terrastruct.com/d2/d2graph"
|
|
|
|
|
"oss.terrastruct.com/d2/lib/geo"
|
|
|
|
|
"oss.terrastruct.com/d2/lib/go2"
|
|
|
|
|
"oss.terrastruct.com/d2/lib/label"
|
2022-11-29 18:11:42 +00:00
|
|
|
"oss.terrastruct.com/d2/lib/shape"
|
2022-11-26 06:32:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func Layout(ctx context.Context, g *d2graph.Graph) (err error) {
|
2022-11-28 22:01:22 +00:00
|
|
|
sd := &sequenceDiagram{
|
|
|
|
|
graph: g,
|
2022-11-29 01:06:37 +00:00
|
|
|
objectRank: make(map[*d2graph.Object]int),
|
2022-11-29 03:20:53 +00:00
|
|
|
objectDepth: make(map[*d2graph.Object]int),
|
2022-11-29 01:06:37 +00:00
|
|
|
minEdgeRank: make(map[*d2graph.Object]int),
|
|
|
|
|
maxEdgeRank: make(map[*d2graph.Object]int),
|
2022-11-28 22:01:22 +00:00
|
|
|
edgeYStep: MIN_EDGE_DISTANCE,
|
|
|
|
|
actorXStep: MIN_ACTOR_DISTANCE,
|
|
|
|
|
maxActorHeight: 0.,
|
|
|
|
|
}
|
2022-11-26 06:32:04 +00:00
|
|
|
|
2022-11-29 01:06:37 +00:00
|
|
|
sd.init()
|
2022-11-28 22:01:22 +00:00
|
|
|
sd.placeActors()
|
2022-11-29 18:08:11 +00:00
|
|
|
sd.placeActivationBoxes()
|
2022-11-29 01:06:37 +00:00
|
|
|
sd.routeEdges()
|
2022-11-29 02:23:51 +00:00
|
|
|
sd.addLifelineEdges()
|
2022-11-26 06:32:04 +00:00
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-28 22:01:22 +00:00
|
|
|
type sequenceDiagram struct {
|
|
|
|
|
graph *d2graph.Graph
|
|
|
|
|
|
2022-11-29 18:08:11 +00:00
|
|
|
edges []*d2graph.Edge
|
|
|
|
|
actors []*d2graph.Object
|
|
|
|
|
activations []*d2graph.Object
|
2022-11-29 01:06:37 +00:00
|
|
|
|
2022-11-29 18:08:11 +00:00
|
|
|
// can be either actors or activation boxes
|
2022-11-29 23:14:36 +00:00
|
|
|
// rank: left to right position of actors/activations
|
|
|
|
|
objectRank map[*d2graph.Object]int
|
|
|
|
|
// depth: the nested levels of a given actor/activation
|
2022-11-29 03:20:53 +00:00
|
|
|
objectDepth map[*d2graph.Object]int
|
2022-11-29 01:06:37 +00:00
|
|
|
|
|
|
|
|
// keep track of the first and last edge of a given actor
|
2022-11-29 23:14:36 +00:00
|
|
|
// the edge rank is the order in which it appears from top to bottom
|
2022-11-29 01:06:37 +00:00
|
|
|
minEdgeRank map[*d2graph.Object]int
|
|
|
|
|
maxEdgeRank map[*d2graph.Object]int
|
|
|
|
|
|
2022-11-28 22:01:22 +00:00
|
|
|
edgeYStep float64
|
|
|
|
|
actorXStep float64
|
|
|
|
|
maxActorHeight float64
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-29 01:06:37 +00:00
|
|
|
func (sd *sequenceDiagram) init() {
|
|
|
|
|
sd.edges = make([]*d2graph.Edge, len(sd.graph.Edges))
|
|
|
|
|
copy(sd.edges, sd.graph.Edges)
|
|
|
|
|
|
2022-11-29 03:20:53 +00:00
|
|
|
queue := make([]*d2graph.Object, len(sd.graph.Root.ChildrenArray))
|
|
|
|
|
copy(queue, sd.graph.Root.ChildrenArray)
|
|
|
|
|
for len(queue) > 0 {
|
|
|
|
|
obj := queue[0]
|
|
|
|
|
queue = queue[1:]
|
|
|
|
|
|
2022-11-29 18:11:42 +00:00
|
|
|
if sd.isActor(obj) {
|
2022-11-29 01:06:37 +00:00
|
|
|
sd.actors = append(sd.actors, obj)
|
2022-11-29 03:20:53 +00:00
|
|
|
sd.objectRank[obj] = len(sd.actors)
|
|
|
|
|
sd.objectDepth[obj] = 0
|
2022-11-29 23:14:36 +00:00
|
|
|
sd.maxActorHeight = math.Max(sd.maxActorHeight, obj.Height)
|
2022-11-29 18:11:42 +00:00
|
|
|
} else {
|
|
|
|
|
// activations boxes are always rectangles and have no labels
|
2022-11-29 03:20:53 +00:00
|
|
|
obj.Attributes.Label = d2graph.Scalar{Value: ""}
|
2022-11-29 18:11:42 +00:00
|
|
|
obj.Attributes.Shape = d2graph.Scalar{Value: shape.SQUARE_TYPE}
|
2022-11-29 18:08:11 +00:00
|
|
|
sd.activations = append(sd.activations, obj)
|
2022-11-29 03:20:53 +00:00
|
|
|
sd.objectRank[obj] = sd.objectRank[obj.Parent]
|
|
|
|
|
sd.objectDepth[obj] = sd.objectDepth[obj.Parent] + 1
|
2022-11-29 01:06:37 +00:00
|
|
|
}
|
2022-11-29 03:20:53 +00:00
|
|
|
|
|
|
|
|
queue = append(queue, obj.ChildrenArray...)
|
2022-11-29 01:06:37 +00:00
|
|
|
}
|
2022-11-29 03:20:53 +00:00
|
|
|
|
2022-11-29 01:06:37 +00:00
|
|
|
for rank, edge := range sd.edges {
|
2022-11-29 23:14:36 +00:00
|
|
|
sd.edgeYStep = math.Max(sd.edgeYStep, float64(edge.LabelDimensions.Height))
|
2022-11-29 01:06:37 +00:00
|
|
|
|
|
|
|
|
sd.setMinMaxEdgeRank(edge.Src, rank)
|
|
|
|
|
sd.setMinMaxEdgeRank(edge.Dst, rank)
|
|
|
|
|
|
|
|
|
|
// ensures that long labels, spanning over multiple actors, don't make for large gaps between actors
|
|
|
|
|
// by distributing the label length across the actors rank difference
|
|
|
|
|
rankDiff := math.Abs(float64(sd.objectRank[edge.Src]) - float64(sd.objectRank[edge.Dst]))
|
|
|
|
|
distributedLabelWidth := float64(edge.LabelDimensions.Width) / rankDiff
|
|
|
|
|
sd.actorXStep = math.Max(sd.actorXStep, distributedLabelWidth+HORIZONTAL_PAD)
|
|
|
|
|
}
|
2022-11-29 23:14:36 +00:00
|
|
|
|
|
|
|
|
sd.maxActorHeight += VERTICAL_PAD
|
|
|
|
|
sd.edgeYStep += VERTICAL_PAD
|
2022-11-29 01:06:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (sd *sequenceDiagram) setMinMaxEdgeRank(actor *d2graph.Object, rank int) {
|
|
|
|
|
if minRank, exists := sd.minEdgeRank[actor]; exists {
|
2022-11-29 18:11:42 +00:00
|
|
|
sd.minEdgeRank[actor] = go2.IntMin(minRank, rank)
|
2022-11-29 01:06:37 +00:00
|
|
|
} else {
|
|
|
|
|
sd.minEdgeRank[actor] = rank
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-29 18:11:42 +00:00
|
|
|
sd.maxEdgeRank[actor] = go2.IntMax(sd.maxEdgeRank[actor], rank)
|
2022-11-29 01:06:37 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-28 19:11:38 +00:00
|
|
|
// placeActors places actors bottom aligned, side by side
|
2022-11-28 22:01:22 +00:00
|
|
|
func (sd *sequenceDiagram) placeActors() {
|
2022-11-26 06:32:04 +00:00
|
|
|
x := 0.
|
2022-11-29 01:06:37 +00:00
|
|
|
for _, actors := range sd.actors {
|
2022-11-28 22:01:22 +00:00
|
|
|
yOffset := sd.maxActorHeight - actors.Height
|
2022-11-28 19:11:38 +00:00
|
|
|
actors.TopLeft = geo.NewPoint(x, yOffset)
|
2022-11-28 22:01:22 +00:00
|
|
|
x += actors.Width + sd.actorXStep
|
2022-11-28 19:11:38 +00:00
|
|
|
actors.LabelPosition = go2.Pointer(string(label.InsideMiddleCenter))
|
2022-11-26 06:32:04 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-29 18:08:11 +00:00
|
|
|
// addLifelineEdges adds a new edge for each actor in the graph that represents the its lifeline
|
2022-11-26 06:41:11 +00:00
|
|
|
// ┌──────────────┐
|
2022-11-28 19:11:38 +00:00
|
|
|
// │ actor │
|
2022-11-26 06:41:11 +00:00
|
|
|
// └──────┬───────┘
|
|
|
|
|
// │
|
|
|
|
|
// │ lifeline
|
|
|
|
|
// │
|
|
|
|
|
// │
|
2022-11-28 22:01:22 +00:00
|
|
|
func (sd *sequenceDiagram) addLifelineEdges() {
|
2022-11-29 01:06:37 +00:00
|
|
|
endY := sd.getEdgeY(len(sd.edges))
|
|
|
|
|
for _, actor := range sd.actors {
|
2022-11-28 19:11:38 +00:00
|
|
|
actorBottom := actor.Center()
|
|
|
|
|
actorBottom.Y = actor.TopLeft.Y + actor.Height
|
|
|
|
|
actorLifelineEnd := actor.Center()
|
|
|
|
|
actorLifelineEnd.Y = endY
|
2022-11-28 22:01:22 +00:00
|
|
|
sd.graph.Edges = append(sd.graph.Edges, &d2graph.Edge{
|
2022-11-26 06:32:04 +00:00
|
|
|
Attributes: d2graph.Attributes{
|
|
|
|
|
Style: d2graph.Style{
|
2022-11-28 19:32:56 +00:00
|
|
|
StrokeDash: &d2graph.Scalar{Value: "10"},
|
2022-11-28 19:11:38 +00:00
|
|
|
Stroke: actor.Attributes.Style.Stroke,
|
|
|
|
|
StrokeWidth: actor.Attributes.Style.StrokeWidth,
|
2022-11-26 06:32:04 +00:00
|
|
|
},
|
|
|
|
|
},
|
2022-11-28 19:11:38 +00:00
|
|
|
Src: actor,
|
2022-11-26 06:32:04 +00:00
|
|
|
SrcArrow: false,
|
|
|
|
|
Dst: &d2graph.Object{
|
2022-11-28 19:11:38 +00:00
|
|
|
ID: actor.ID + fmt.Sprintf("-lifeline-end-%d", go2.StringToIntHash(actor.ID+"-lifeline-end")),
|
2022-11-26 06:32:04 +00:00
|
|
|
},
|
|
|
|
|
DstArrow: false,
|
2022-11-28 19:11:38 +00:00
|
|
|
Route: []*geo.Point{actorBottom, actorLifelineEnd},
|
2022-11-26 06:32:04 +00:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-29 01:06:37 +00:00
|
|
|
|
2022-11-29 18:08:11 +00:00
|
|
|
// placeActivationBoxes places activation boxes over the object lifeline
|
2022-11-29 02:23:51 +00:00
|
|
|
// ┌──────────┐
|
|
|
|
|
// │ actor │
|
|
|
|
|
// └────┬─────┘
|
|
|
|
|
// ┌─┴──┐
|
|
|
|
|
// │ │
|
2022-11-29 18:08:11 +00:00
|
|
|
// activation
|
2022-11-29 02:23:51 +00:00
|
|
|
// │ │
|
|
|
|
|
// └─┬──┘
|
|
|
|
|
// │
|
|
|
|
|
// lifeline
|
|
|
|
|
// │
|
2022-11-29 18:08:11 +00:00
|
|
|
func (sd *sequenceDiagram) placeActivationBoxes() {
|
2022-11-29 18:29:15 +00:00
|
|
|
// quickly find the activation box center X
|
2022-11-29 01:06:37 +00:00
|
|
|
rankToX := make(map[int]float64)
|
|
|
|
|
for _, actor := range sd.actors {
|
|
|
|
|
rankToX[sd.objectRank[actor]] = actor.Center().X
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-29 18:29:15 +00:00
|
|
|
// places activation boxes from most to least nested
|
|
|
|
|
// the order is important because the only way a child activation box exists is if there'e an edge to it
|
|
|
|
|
// however, the parent activation might not have an edge to it and then its position is based on the child position
|
|
|
|
|
// or, there can be edge to it, but it comes after the child one meaning the top left position is still based on the child
|
|
|
|
|
// and not on its own edge
|
2022-11-29 18:08:11 +00:00
|
|
|
activationFromMostNested := make([]*d2graph.Object, len(sd.activations))
|
|
|
|
|
copy(activationFromMostNested, sd.activations)
|
|
|
|
|
sort.SliceStable(activationFromMostNested, func(i, j int) bool {
|
|
|
|
|
return sd.objectDepth[activationFromMostNested[i]] > sd.objectDepth[activationFromMostNested[j]]
|
2022-11-29 03:20:53 +00:00
|
|
|
})
|
2022-11-29 18:08:11 +00:00
|
|
|
for _, activation := range activationFromMostNested {
|
2022-11-29 18:29:15 +00:00
|
|
|
// finds the position based on children
|
2022-11-29 03:20:53 +00:00
|
|
|
minChildY := math.Inf(1)
|
|
|
|
|
maxChildY := math.Inf(-1)
|
2022-11-29 18:08:11 +00:00
|
|
|
for _, child := range activation.ChildrenArray {
|
2022-11-29 03:20:53 +00:00
|
|
|
minChildY = math.Min(minChildY, child.TopLeft.Y)
|
|
|
|
|
maxChildY = math.Max(maxChildY, child.TopLeft.Y+child.Height)
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-29 18:29:15 +00:00
|
|
|
// finds the position if there are edges to this activation box
|
2022-11-29 03:20:53 +00:00
|
|
|
minEdgeY := math.Inf(1)
|
2022-11-29 18:08:11 +00:00
|
|
|
if minRank, exists := sd.minEdgeRank[activation]; exists {
|
2022-11-29 03:20:53 +00:00
|
|
|
minEdgeY = sd.getEdgeY(minRank)
|
|
|
|
|
}
|
|
|
|
|
maxEdgeY := math.Inf(-1)
|
2022-11-29 18:08:11 +00:00
|
|
|
if maxRank, exists := sd.maxEdgeRank[activation]; exists {
|
2022-11-29 03:20:53 +00:00
|
|
|
maxEdgeY = sd.getEdgeY(maxRank)
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-29 18:29:15 +00:00
|
|
|
// if it is the same as the child top left, add some padding
|
2022-11-29 03:20:53 +00:00
|
|
|
minY := math.Min(minEdgeY, minChildY)
|
|
|
|
|
if minY == minChildY {
|
2022-11-29 18:08:11 +00:00
|
|
|
minY -= ACTIVATION_BOX_DEPTH_GROW_FACTOR
|
2022-11-29 20:57:07 +00:00
|
|
|
} else {
|
|
|
|
|
minY -= ACTIVATION_BOX_EDGE_PAD
|
2022-11-29 03:20:53 +00:00
|
|
|
}
|
|
|
|
|
maxY := math.Max(maxEdgeY, maxChildY)
|
|
|
|
|
if maxY == maxChildY {
|
2022-11-29 18:08:11 +00:00
|
|
|
maxY += ACTIVATION_BOX_DEPTH_GROW_FACTOR
|
2022-11-29 20:57:07 +00:00
|
|
|
} else {
|
|
|
|
|
maxY += ACTIVATION_BOX_EDGE_PAD
|
2022-11-29 03:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-29 23:14:36 +00:00
|
|
|
height := math.Max(maxY-minY, MIN_ACTIVATION_BOX_HEIGHT)
|
2022-11-29 18:11:42 +00:00
|
|
|
width := ACTIVATION_BOX_WIDTH + (float64(sd.objectDepth[activation]-1) * ACTIVATION_BOX_DEPTH_GROW_FACTOR)
|
2022-11-29 18:08:11 +00:00
|
|
|
x := rankToX[sd.objectRank[activation]] - (width / 2.)
|
|
|
|
|
activation.Box = geo.NewBox(geo.NewPoint(x, minY), width, height)
|
2022-11-29 01:06:37 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// routeEdges routes horizontal edges from Src to Dst
|
|
|
|
|
func (sd *sequenceDiagram) routeEdges() {
|
|
|
|
|
for rank, edge := range sd.edges {
|
2022-11-29 02:23:51 +00:00
|
|
|
isLeftToRight := edge.Src.TopLeft.X < edge.Dst.TopLeft.X
|
|
|
|
|
|
2022-11-29 18:29:15 +00:00
|
|
|
// finds the proper anchor point based on the edge direction
|
2022-11-29 02:23:51 +00:00
|
|
|
var startX, endX float64
|
|
|
|
|
if sd.isActor(edge.Src) {
|
|
|
|
|
startX = edge.Src.Center().X
|
|
|
|
|
} else if isLeftToRight {
|
|
|
|
|
startX = edge.Src.TopLeft.X + edge.Src.Width
|
|
|
|
|
} else {
|
|
|
|
|
startX = edge.Src.TopLeft.X
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if sd.isActor(edge.Dst) {
|
|
|
|
|
endX = edge.Dst.Center().X
|
|
|
|
|
} else if isLeftToRight {
|
|
|
|
|
endX = edge.Dst.TopLeft.X
|
|
|
|
|
} else {
|
|
|
|
|
endX = edge.Dst.TopLeft.X + edge.Dst.Width
|
|
|
|
|
}
|
2022-11-29 19:53:30 +00:00
|
|
|
|
|
|
|
|
if isLeftToRight {
|
|
|
|
|
startX += ACTIVATION_BOX_EDGE_PAD
|
|
|
|
|
endX -= ACTIVATION_BOX_EDGE_PAD
|
|
|
|
|
} else {
|
|
|
|
|
startX -= ACTIVATION_BOX_EDGE_PAD
|
|
|
|
|
endX += ACTIVATION_BOX_EDGE_PAD
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-29 02:23:51 +00:00
|
|
|
edgeY := sd.getEdgeY(rank)
|
|
|
|
|
edge.Route = []*geo.Point{
|
|
|
|
|
geo.NewPoint(startX, edgeY),
|
|
|
|
|
geo.NewPoint(endX, edgeY),
|
|
|
|
|
}
|
2022-11-29 01:06:37 +00:00
|
|
|
|
|
|
|
|
if edge.Attributes.Label.Value != "" {
|
|
|
|
|
if isLeftToRight {
|
|
|
|
|
edge.LabelPosition = go2.Pointer(string(label.OutsideTopCenter))
|
|
|
|
|
} else {
|
2022-11-29 18:29:15 +00:00
|
|
|
// the label will be placed above the edge because the orientation is based on the edge normal vector
|
2022-11-29 01:06:37 +00:00
|
|
|
edge.LabelPosition = go2.Pointer(string(label.OutsideBottomCenter))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (sd *sequenceDiagram) getEdgeY(rank int) float64 {
|
2022-11-29 02:23:51 +00:00
|
|
|
// +1 so that the first edge has the top padding for its label
|
2022-11-29 01:06:37 +00:00
|
|
|
return ((float64(rank) + 1.) * sd.edgeYStep) + sd.maxActorHeight
|
|
|
|
|
}
|
2022-11-29 02:23:51 +00:00
|
|
|
|
|
|
|
|
func (sd *sequenceDiagram) isActor(obj *d2graph.Object) bool {
|
|
|
|
|
return obj.Parent == sd.graph.Root
|
|
|
|
|
}
|