2022-11-26 06:32:04 +00:00
|
|
|
package d2sequence
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"math"
|
|
|
|
|
|
|
|
|
|
"oss.terrastruct.com/d2/d2graph"
|
|
|
|
|
"oss.terrastruct.com/d2/lib/geo"
|
|
|
|
|
"oss.terrastruct.com/d2/lib/go2"
|
|
|
|
|
"oss.terrastruct.com/d2/lib/label"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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),
|
|
|
|
|
edgeRank: make(map[*d2graph.Edge]int),
|
|
|
|
|
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 01:06:37 +00:00
|
|
|
sd.placeLifespan()
|
|
|
|
|
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 01:06:37 +00:00
|
|
|
edges []*d2graph.Edge
|
|
|
|
|
actors []*d2graph.Object
|
|
|
|
|
lifespans []*d2graph.Object
|
|
|
|
|
|
|
|
|
|
// can be either actors or lifespans
|
|
|
|
|
objectRank map[*d2graph.Object]int
|
|
|
|
|
edgeRank map[*d2graph.Edge]int
|
|
|
|
|
|
|
|
|
|
// keep track of the first and last edge of a given actor
|
|
|
|
|
// needed for lifespan
|
|
|
|
|
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 intMin(a, b int) int {
|
|
|
|
|
return int(math.Min(float64(a), float64(b)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func intMax(a, b int) int {
|
|
|
|
|
return int(math.Max(float64(a), float64(b)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (sd *sequenceDiagram) init() {
|
|
|
|
|
sd.edges = make([]*d2graph.Edge, len(sd.graph.Edges))
|
|
|
|
|
copy(sd.edges, sd.graph.Edges)
|
|
|
|
|
|
|
|
|
|
for rank, actor := range sd.graph.Root.ChildrenArray {
|
|
|
|
|
sd.assignRank(actor, rank)
|
|
|
|
|
}
|
|
|
|
|
for _, obj := range sd.graph.Objects {
|
|
|
|
|
if obj.Parent == sd.graph.Root {
|
|
|
|
|
sd.actors = append(sd.actors, obj)
|
|
|
|
|
} else if obj != sd.graph.Root {
|
|
|
|
|
sd.lifespans = append(sd.lifespans, obj)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for rank, edge := range sd.edges {
|
|
|
|
|
sd.edgeRank[edge] = rank
|
|
|
|
|
if edge.Src.Parent == sd.graph.Root {
|
|
|
|
|
sd.maxActorHeight = math.Max(sd.maxActorHeight, edge.Src.Height+HORIZONTAL_PAD)
|
|
|
|
|
}
|
|
|
|
|
if edge.Dst.Parent == sd.graph.Root {
|
|
|
|
|
sd.maxActorHeight = math.Max(sd.maxActorHeight, edge.Dst.Height+HORIZONTAL_PAD)
|
|
|
|
|
}
|
|
|
|
|
sd.edgeYStep = math.Max(sd.edgeYStep, float64(edge.LabelDimensions.Height)+HORIZONTAL_PAD)
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (sd *sequenceDiagram) assignRank(actor *d2graph.Object, rank int) {
|
|
|
|
|
sd.objectRank[actor] = rank
|
|
|
|
|
for _, child := range actor.Children {
|
|
|
|
|
sd.assignRank(child, rank)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (sd *sequenceDiagram) setMinMaxEdgeRank(actor *d2graph.Object, rank int) {
|
|
|
|
|
if minRank, exists := sd.minEdgeRank[actor]; exists {
|
|
|
|
|
sd.minEdgeRank[actor] = intMin(minRank, rank)
|
|
|
|
|
} else {
|
|
|
|
|
sd.minEdgeRank[actor] = rank
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sd.maxEdgeRank[actor] = intMax(sd.maxEdgeRank[actor], rank)
|
|
|
|
|
}
|
|
|
|
|
|
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-28 19:11:38 +00:00
|
|
|
// addLifelineEdges adds a new edge for each actor in the graph that represents the
|
|
|
|
|
// edge below the actor showing its lifespan
|
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 02:23:51 +00:00
|
|
|
// placeLifespan places lifespan boxes over the object lifeline
|
|
|
|
|
// ┌──────────┐
|
|
|
|
|
// │ actor │
|
|
|
|
|
// └────┬─────┘
|
|
|
|
|
// ┌─┴──┐
|
|
|
|
|
// │ │
|
|
|
|
|
// lifespan
|
|
|
|
|
// │ │
|
|
|
|
|
// └─┬──┘
|
|
|
|
|
// │
|
|
|
|
|
// lifeline
|
|
|
|
|
// │
|
2022-11-29 01:06:37 +00:00
|
|
|
func (sd *sequenceDiagram) placeLifespan() {
|
|
|
|
|
rankToX := make(map[int]float64)
|
|
|
|
|
for _, actor := range sd.actors {
|
|
|
|
|
rankToX[sd.objectRank[actor]] = actor.Center().X
|
|
|
|
|
}
|
|
|
|
|
for _, lifespan := range sd.lifespans {
|
2022-11-29 01:09:34 +00:00
|
|
|
lifespan.Attributes.Label = d2graph.Scalar{Value: ""}
|
2022-11-29 01:06:37 +00:00
|
|
|
minRank := sd.minEdgeRank[lifespan]
|
|
|
|
|
maxRank := sd.maxEdgeRank[lifespan]
|
|
|
|
|
|
|
|
|
|
minY := sd.getEdgeY(minRank)
|
|
|
|
|
maxY := sd.getEdgeY(maxRank)
|
|
|
|
|
height := maxY - minY
|
2022-11-29 02:23:51 +00:00
|
|
|
height = math.Max(height, DEFAULT_LIFESPAN_BOX_HEIGHT)
|
2022-11-29 01:06:37 +00:00
|
|
|
x := rankToX[sd.objectRank[lifespan]] - (LIFESPAN_BOX_WIDTH / 2.)
|
|
|
|
|
lifespan.Box = geo.NewBox(geo.NewPoint(x, minY), LIFESPAN_BOX_WIDTH, height)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
|
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
|
|
|
|
|
}
|