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
|
|
|
)
|
|
|
|
|
|
2022-11-30 22:01:16 +00:00
|
|
|
func Layout2(ctx context.Context, g *d2graph.Graph, layoutFn func(ctx context.Context, g *d2graph.Graph) error) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
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-30 20:09:29 +00:00
|
|
|
minMessageRank: make(map[*d2graph.Object]int),
|
|
|
|
|
maxMessageRank: make(map[*d2graph.Object]int),
|
|
|
|
|
messageYStep: MIN_MESSAGE_DISTANCE,
|
2022-11-28 22:01:22 +00:00
|
|
|
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 23:40:43 +00:00
|
|
|
sd.placeSpans()
|
2022-11-30 20:09:29 +00:00
|
|
|
sd.routeMessages()
|
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-30 20:09:29 +00:00
|
|
|
messages []*d2graph.Edge
|
|
|
|
|
actors []*d2graph.Object
|
|
|
|
|
spans []*d2graph.Object
|
2022-11-29 01:06:37 +00:00
|
|
|
|
2022-11-29 23:40:43 +00:00
|
|
|
// can be either actors or spans
|
|
|
|
|
// rank: left to right position of actors/spans (spans have the same rank as their parents)
|
2022-11-29 23:14:36 +00:00
|
|
|
objectRank map[*d2graph.Object]int
|
2022-11-29 01:06:37 +00:00
|
|
|
|
2022-11-30 20:09:29 +00:00
|
|
|
// keep track of the first and last message of a given actor/span
|
|
|
|
|
// the message rank is the order in which it appears from top to bottom
|
|
|
|
|
minMessageRank map[*d2graph.Object]int
|
|
|
|
|
maxMessageRank map[*d2graph.Object]int
|
2022-11-29 01:06:37 +00:00
|
|
|
|
2022-11-30 20:09:29 +00:00
|
|
|
messageYStep float64
|
2022-11-28 22:01:22 +00:00
|
|
|
actorXStep float64
|
|
|
|
|
maxActorHeight float64
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-29 01:06:37 +00:00
|
|
|
func (sd *sequenceDiagram) init() {
|
2022-11-30 20:09:29 +00:00
|
|
|
sd.messages = make([]*d2graph.Edge, len(sd.graph.Edges))
|
|
|
|
|
copy(sd.messages, sd.graph.Edges)
|
2022-11-29 01:06:37 +00:00
|
|
|
|
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)
|
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 {
|
2022-11-29 23:40:43 +00:00
|
|
|
// spans 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 23:17:47 +00:00
|
|
|
sd.spans = append(sd.spans, obj)
|
2022-11-29 03:20:53 +00:00
|
|
|
sd.objectRank[obj] = sd.objectRank[obj.Parent]
|
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-30 20:09:29 +00:00
|
|
|
for rank, message := range sd.messages {
|
|
|
|
|
sd.messageYStep = math.Max(sd.messageYStep, float64(message.LabelDimensions.Height))
|
2022-11-29 01:06:37 +00:00
|
|
|
|
2022-11-30 20:09:29 +00:00
|
|
|
sd.setMinMaxMessageRank(message.Src, rank)
|
|
|
|
|
sd.setMinMaxMessageRank(message.Dst, rank)
|
2022-11-29 01:06:37 +00:00
|
|
|
|
|
|
|
|
// 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
|
2022-11-30 20:09:29 +00:00
|
|
|
rankDiff := math.Abs(float64(sd.objectRank[message.Src]) - float64(sd.objectRank[message.Dst]))
|
|
|
|
|
distributedLabelWidth := float64(message.LabelDimensions.Width) / rankDiff
|
2022-11-29 01:06:37 +00:00
|
|
|
sd.actorXStep = math.Max(sd.actorXStep, distributedLabelWidth+HORIZONTAL_PAD)
|
|
|
|
|
}
|
2022-11-29 23:14:36 +00:00
|
|
|
|
|
|
|
|
sd.maxActorHeight += VERTICAL_PAD
|
2022-11-30 20:09:29 +00:00
|
|
|
sd.messageYStep += VERTICAL_PAD
|
2022-11-29 01:06:37 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-30 20:09:29 +00:00
|
|
|
func (sd *sequenceDiagram) setMinMaxMessageRank(actor *d2graph.Object, rank int) {
|
|
|
|
|
if minRank, exists := sd.minMessageRank[actor]; exists {
|
|
|
|
|
sd.minMessageRank[actor] = go2.IntMin(minRank, rank)
|
2022-11-29 01:06:37 +00:00
|
|
|
} else {
|
2022-11-30 20:09:29 +00:00
|
|
|
sd.minMessageRank[actor] = rank
|
2022-11-29 01:06:37 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-30 20:09:29 +00:00
|
|
|
sd.maxMessageRank[actor] = go2.IntMax(sd.maxMessageRank[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-30 20:09:29 +00:00
|
|
|
endY := sd.getMessageY(len(sd.messages))
|
2022-11-29 01:06:37 +00:00
|
|
|
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 23:40:43 +00:00
|
|
|
// placeSpans places spans over the object lifeline
|
2022-11-29 02:23:51 +00:00
|
|
|
// ┌──────────┐
|
|
|
|
|
// │ actor │
|
|
|
|
|
// └────┬─────┘
|
|
|
|
|
// ┌─┴──┐
|
|
|
|
|
// │ │
|
2022-11-29 23:17:47 +00:00
|
|
|
// |span|
|
2022-11-29 02:23:51 +00:00
|
|
|
// │ │
|
|
|
|
|
// └─┬──┘
|
|
|
|
|
// │
|
|
|
|
|
// lifeline
|
|
|
|
|
// │
|
2022-11-29 23:40:43 +00:00
|
|
|
func (sd *sequenceDiagram) placeSpans() {
|
|
|
|
|
// quickly find the span 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 23:40:43 +00:00
|
|
|
// places spans from most to least nested
|
2022-11-30 20:09:29 +00:00
|
|
|
// the order is important because the only way a child span exists is if there'e an message to it
|
|
|
|
|
// however, the parent span might not have a message to it and then its position is based on the child position
|
|
|
|
|
// or, there can be a message 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 message
|
2022-11-29 23:17:47 +00:00
|
|
|
spanFromMostNested := make([]*d2graph.Object, len(sd.spans))
|
|
|
|
|
copy(spanFromMostNested, sd.spans)
|
|
|
|
|
sort.SliceStable(spanFromMostNested, func(i, j int) bool {
|
2022-11-30 05:34:14 +00:00
|
|
|
return spanFromMostNested[i].Level() > spanFromMostNested[j].Level()
|
2022-11-29 03:20:53 +00:00
|
|
|
})
|
2022-11-29 23:17:47 +00:00
|
|
|
for _, span := range spanFromMostNested {
|
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 23:17:47 +00:00
|
|
|
for _, child := range span.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-30 20:09:29 +00:00
|
|
|
// finds the position if there are messages to this span
|
|
|
|
|
minMessageY := math.Inf(1)
|
|
|
|
|
if minRank, exists := sd.minMessageRank[span]; exists {
|
|
|
|
|
minMessageY = sd.getMessageY(minRank)
|
2022-11-29 03:20:53 +00:00
|
|
|
}
|
2022-11-30 20:09:29 +00:00
|
|
|
maxMessageY := math.Inf(-1)
|
|
|
|
|
if maxRank, exists := sd.maxMessageRank[span]; exists {
|
|
|
|
|
maxMessageY = sd.getMessageY(maxRank)
|
2022-11-29 03:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-29 18:29:15 +00:00
|
|
|
// if it is the same as the child top left, add some padding
|
2022-11-30 20:09:29 +00:00
|
|
|
minY := math.Min(minMessageY, minChildY)
|
2022-11-29 03:20:53 +00:00
|
|
|
if minY == minChildY {
|
2022-11-30 00:16:41 +00:00
|
|
|
minY -= SPAN_DEPTH_GROW_FACTOR
|
2022-11-29 20:57:07 +00:00
|
|
|
} else {
|
2022-11-30 20:09:29 +00:00
|
|
|
minY -= SPAN_MESSAGE_PAD
|
2022-11-29 03:20:53 +00:00
|
|
|
}
|
2022-11-30 20:09:29 +00:00
|
|
|
maxY := math.Max(maxMessageY, maxChildY)
|
2022-11-29 03:20:53 +00:00
|
|
|
if maxY == maxChildY {
|
2022-11-30 00:16:41 +00:00
|
|
|
maxY += SPAN_DEPTH_GROW_FACTOR
|
2022-11-29 20:57:07 +00:00
|
|
|
} else {
|
2022-11-30 20:09:29 +00:00
|
|
|
maxY += SPAN_MESSAGE_PAD
|
2022-11-29 03:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-29 23:40:43 +00:00
|
|
|
height := math.Max(maxY-minY, MIN_SPAN_HEIGHT)
|
2022-11-30 05:34:14 +00:00
|
|
|
// -2 because the actors count as level 1 making the first level span getting 2*SPAN_DEPTH_GROW_FACTOR
|
|
|
|
|
width := SPAN_WIDTH + (float64(span.Level()-2) * SPAN_DEPTH_GROW_FACTOR)
|
2022-11-29 23:17:47 +00:00
|
|
|
x := rankToX[sd.objectRank[span]] - (width / 2.)
|
|
|
|
|
span.Box = geo.NewBox(geo.NewPoint(x, minY), width, height)
|
2022-11-30 21:41:20 +00:00
|
|
|
span.ZIndex = 1
|
2022-11-29 01:06:37 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-30 20:09:29 +00:00
|
|
|
// routeMessages routes horizontal edges (messages) from Src to Dst
|
|
|
|
|
func (sd *sequenceDiagram) routeMessages() {
|
|
|
|
|
for rank, message := range sd.messages {
|
|
|
|
|
isLeftToRight := message.Src.TopLeft.X < message.Dst.TopLeft.X
|
2022-11-29 02:23:51 +00:00
|
|
|
|
2022-11-30 20:09:29 +00:00
|
|
|
// finds the proper anchor point based on the message direction
|
2022-11-29 02:23:51 +00:00
|
|
|
var startX, endX float64
|
2022-11-30 20:09:29 +00:00
|
|
|
if sd.isActor(message.Src) {
|
|
|
|
|
startX = message.Src.Center().X
|
2022-11-29 02:23:51 +00:00
|
|
|
} else if isLeftToRight {
|
2022-11-30 20:09:29 +00:00
|
|
|
startX = message.Src.TopLeft.X + message.Src.Width
|
2022-11-29 02:23:51 +00:00
|
|
|
} else {
|
2022-11-30 20:09:29 +00:00
|
|
|
startX = message.Src.TopLeft.X
|
2022-11-29 02:23:51 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-30 20:09:29 +00:00
|
|
|
if sd.isActor(message.Dst) {
|
|
|
|
|
endX = message.Dst.Center().X
|
2022-11-29 02:23:51 +00:00
|
|
|
} else if isLeftToRight {
|
2022-11-30 20:09:29 +00:00
|
|
|
endX = message.Dst.TopLeft.X
|
2022-11-29 02:23:51 +00:00
|
|
|
} else {
|
2022-11-30 20:09:29 +00:00
|
|
|
endX = message.Dst.TopLeft.X + message.Dst.Width
|
2022-11-29 02:23:51 +00:00
|
|
|
}
|
2022-11-29 19:53:30 +00:00
|
|
|
|
|
|
|
|
if isLeftToRight {
|
2022-11-30 20:09:29 +00:00
|
|
|
startX += SPAN_MESSAGE_PAD
|
|
|
|
|
endX -= SPAN_MESSAGE_PAD
|
2022-11-29 19:53:30 +00:00
|
|
|
} else {
|
2022-11-30 20:09:29 +00:00
|
|
|
startX -= SPAN_MESSAGE_PAD
|
|
|
|
|
endX += SPAN_MESSAGE_PAD
|
2022-11-29 19:53:30 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-30 20:09:29 +00:00
|
|
|
messageY := sd.getMessageY(rank)
|
|
|
|
|
message.Route = []*geo.Point{
|
|
|
|
|
geo.NewPoint(startX, messageY),
|
|
|
|
|
geo.NewPoint(endX, messageY),
|
2022-11-29 02:23:51 +00:00
|
|
|
}
|
2022-11-29 01:06:37 +00:00
|
|
|
|
2022-11-30 20:09:29 +00:00
|
|
|
if message.Attributes.Label.Value != "" {
|
2022-11-29 01:06:37 +00:00
|
|
|
if isLeftToRight {
|
2022-11-30 20:09:29 +00:00
|
|
|
message.LabelPosition = go2.Pointer(string(label.OutsideTopCenter))
|
2022-11-29 01:06:37 +00:00
|
|
|
} else {
|
2022-11-30 20:09:29 +00:00
|
|
|
// the label will be placed above the message because the orientation is based on the edge normal vector
|
|
|
|
|
message.LabelPosition = go2.Pointer(string(label.OutsideBottomCenter))
|
2022-11-29 01:06:37 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-30 20:09:29 +00:00
|
|
|
func (sd *sequenceDiagram) getMessageY(rank int) float64 {
|
|
|
|
|
// +1 so that the first message has the top padding for its label
|
|
|
|
|
return ((float64(rank) + 1.) * sd.messageYStep) + sd.maxActorHeight
|
2022-11-29 01:06:37 +00:00
|
|
|
}
|
2022-11-29 02:23:51 +00:00
|
|
|
|
|
|
|
|
func (sd *sequenceDiagram) isActor(obj *d2graph.Object) bool {
|
|
|
|
|
return obj.Parent == sd.graph.Root
|
|
|
|
|
}
|