Rename edges -> messages

This commit is contained in:
Júlio César Batista 2022-11-30 12:09:29 -08:00
parent 7f7977eb8b
commit a86bfc0b9b
No known key found for this signature in database
GPG key ID: 10C4B861BF314878
3 changed files with 91 additions and 91 deletions

View file

@ -3,22 +3,22 @@ package d2sequence
// leaves at least 25 units of space on the left/right when computing the space required between actors // leaves at least 25 units of space on the left/right when computing the space required between actors
const HORIZONTAL_PAD = 50. const HORIZONTAL_PAD = 50.
// leaves at least 25 units of space on the top/bottom when computing the space required between edges // leaves at least 25 units of space on the top/bottom when computing the space required between messages
const VERTICAL_PAD = 50. const VERTICAL_PAD = 50.
const MIN_ACTOR_DISTANCE = 200. const MIN_ACTOR_DISTANCE = 200.
// min vertical distance between edges // min vertical distance between messages
const MIN_EDGE_DISTANCE = 100. const MIN_MESSAGE_DISTANCE = 100.
// default size // default size
const SPAN_WIDTH = 20. const SPAN_WIDTH = 20.
// small pad so that edges don't touch lifelines and spans // small pad so that messages don't touch lifelines and spans
const SPAN_EDGE_PAD = 5. const SPAN_MESSAGE_PAD = 5.
// as the spans start getting nested, their size grows // as the spans start getting nested, their size grows
const SPAN_DEPTH_GROW_FACTOR = 10. const SPAN_DEPTH_GROW_FACTOR = 10.
// when a span has a single edge // when a span has a single messages
const MIN_SPAN_HEIGHT = MIN_EDGE_DISTANCE / 2. const MIN_SPAN_HEIGHT = MIN_MESSAGE_DISTANCE / 2.

View file

@ -17,9 +17,9 @@ func Layout(ctx context.Context, g *d2graph.Graph) (err error) {
sd := &sequenceDiagram{ sd := &sequenceDiagram{
graph: g, graph: g,
objectRank: make(map[*d2graph.Object]int), objectRank: make(map[*d2graph.Object]int),
minEdgeRank: make(map[*d2graph.Object]int), minMessageRank: make(map[*d2graph.Object]int),
maxEdgeRank: make(map[*d2graph.Object]int), maxMessageRank: make(map[*d2graph.Object]int),
edgeYStep: MIN_EDGE_DISTANCE, messageYStep: MIN_MESSAGE_DISTANCE,
actorXStep: MIN_ACTOR_DISTANCE, actorXStep: MIN_ACTOR_DISTANCE,
maxActorHeight: 0., maxActorHeight: 0.,
} }
@ -27,7 +27,7 @@ func Layout(ctx context.Context, g *d2graph.Graph) (err error) {
sd.init() sd.init()
sd.placeActors() sd.placeActors()
sd.placeSpans() sd.placeSpans()
sd.routeEdges() sd.routeMessages()
sd.addLifelineEdges() sd.addLifelineEdges()
return nil return nil
@ -36,27 +36,27 @@ func Layout(ctx context.Context, g *d2graph.Graph) (err error) {
type sequenceDiagram struct { type sequenceDiagram struct {
graph *d2graph.Graph graph *d2graph.Graph
edges []*d2graph.Edge messages []*d2graph.Edge
actors []*d2graph.Object actors []*d2graph.Object
spans []*d2graph.Object spans []*d2graph.Object
// can be either actors or spans // can be either actors or spans
// rank: left to right position of actors/spans (spans have the same rank as their parents) // rank: left to right position of actors/spans (spans have the same rank as their parents)
objectRank map[*d2graph.Object]int objectRank map[*d2graph.Object]int
// keep track of the first and last edge of a given actor // keep track of the first and last message of a given actor/span
// the edge rank is the order in which it appears from top to bottom // the message rank is the order in which it appears from top to bottom
minEdgeRank map[*d2graph.Object]int minMessageRank map[*d2graph.Object]int
maxEdgeRank map[*d2graph.Object]int maxMessageRank map[*d2graph.Object]int
edgeYStep float64 messageYStep float64
actorXStep float64 actorXStep float64
maxActorHeight float64 maxActorHeight float64
} }
func (sd *sequenceDiagram) init() { func (sd *sequenceDiagram) init() {
sd.edges = make([]*d2graph.Edge, len(sd.graph.Edges)) sd.messages = make([]*d2graph.Edge, len(sd.graph.Edges))
copy(sd.edges, sd.graph.Edges) copy(sd.messages, sd.graph.Edges)
queue := make([]*d2graph.Object, len(sd.graph.Root.ChildrenArray)) queue := make([]*d2graph.Object, len(sd.graph.Root.ChildrenArray))
copy(queue, sd.graph.Root.ChildrenArray) copy(queue, sd.graph.Root.ChildrenArray)
@ -79,31 +79,31 @@ func (sd *sequenceDiagram) init() {
queue = append(queue, obj.ChildrenArray...) queue = append(queue, obj.ChildrenArray...)
} }
for rank, edge := range sd.edges { for rank, message := range sd.messages {
sd.edgeYStep = math.Max(sd.edgeYStep, float64(edge.LabelDimensions.Height)) sd.messageYStep = math.Max(sd.messageYStep, float64(message.LabelDimensions.Height))
sd.setMinMaxEdgeRank(edge.Src, rank) sd.setMinMaxMessageRank(message.Src, rank)
sd.setMinMaxEdgeRank(edge.Dst, rank) sd.setMinMaxMessageRank(message.Dst, rank)
// ensures that long labels, spanning over multiple actors, don't make for large gaps between actors // 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 // by distributing the label length across the actors rank difference
rankDiff := math.Abs(float64(sd.objectRank[edge.Src]) - float64(sd.objectRank[edge.Dst])) rankDiff := math.Abs(float64(sd.objectRank[message.Src]) - float64(sd.objectRank[message.Dst]))
distributedLabelWidth := float64(edge.LabelDimensions.Width) / rankDiff distributedLabelWidth := float64(message.LabelDimensions.Width) / rankDiff
sd.actorXStep = math.Max(sd.actorXStep, distributedLabelWidth+HORIZONTAL_PAD) sd.actorXStep = math.Max(sd.actorXStep, distributedLabelWidth+HORIZONTAL_PAD)
} }
sd.maxActorHeight += VERTICAL_PAD sd.maxActorHeight += VERTICAL_PAD
sd.edgeYStep += VERTICAL_PAD sd.messageYStep += VERTICAL_PAD
} }
func (sd *sequenceDiagram) setMinMaxEdgeRank(actor *d2graph.Object, rank int) { func (sd *sequenceDiagram) setMinMaxMessageRank(actor *d2graph.Object, rank int) {
if minRank, exists := sd.minEdgeRank[actor]; exists { if minRank, exists := sd.minMessageRank[actor]; exists {
sd.minEdgeRank[actor] = go2.IntMin(minRank, rank) sd.minMessageRank[actor] = go2.IntMin(minRank, rank)
} else { } else {
sd.minEdgeRank[actor] = rank sd.minMessageRank[actor] = rank
} }
sd.maxEdgeRank[actor] = go2.IntMax(sd.maxEdgeRank[actor], rank) sd.maxMessageRank[actor] = go2.IntMax(sd.maxMessageRank[actor], rank)
} }
// placeActors places actors bottom aligned, side by side // placeActors places actors bottom aligned, side by side
@ -126,7 +126,7 @@ func (sd *sequenceDiagram) placeActors() {
// │ // │
// │ // │
func (sd *sequenceDiagram) addLifelineEdges() { func (sd *sequenceDiagram) addLifelineEdges() {
endY := sd.getEdgeY(len(sd.edges)) endY := sd.getMessageY(len(sd.messages))
for _, actor := range sd.actors { for _, actor := range sd.actors {
actorBottom := actor.Center() actorBottom := actor.Center()
actorBottom.Y = actor.TopLeft.Y + actor.Height actorBottom.Y = actor.TopLeft.Y + actor.Height
@ -171,10 +171,10 @@ func (sd *sequenceDiagram) placeSpans() {
} }
// places spans from most to least nested // places spans from most to least nested
// the order is important because the only way a child span exists is if there'e an edge to it // 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 an edge to it and then its position is based on the child position // 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 edge to it, but it comes after the child one meaning the top left position is still based on the child // 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 edge // and not on its own message
spanFromMostNested := make([]*d2graph.Object, len(sd.spans)) spanFromMostNested := make([]*d2graph.Object, len(sd.spans))
copy(spanFromMostNested, sd.spans) copy(spanFromMostNested, sd.spans)
sort.SliceStable(spanFromMostNested, func(i, j int) bool { sort.SliceStable(spanFromMostNested, func(i, j int) bool {
@ -189,28 +189,28 @@ func (sd *sequenceDiagram) placeSpans() {
maxChildY = math.Max(maxChildY, child.TopLeft.Y+child.Height) maxChildY = math.Max(maxChildY, child.TopLeft.Y+child.Height)
} }
// finds the position if there are edges to this span // finds the position if there are messages to this span
minEdgeY := math.Inf(1) minMessageY := math.Inf(1)
if minRank, exists := sd.minEdgeRank[span]; exists { if minRank, exists := sd.minMessageRank[span]; exists {
minEdgeY = sd.getEdgeY(minRank) minMessageY = sd.getMessageY(minRank)
} }
maxEdgeY := math.Inf(-1) maxMessageY := math.Inf(-1)
if maxRank, exists := sd.maxEdgeRank[span]; exists { if maxRank, exists := sd.maxMessageRank[span]; exists {
maxEdgeY = sd.getEdgeY(maxRank) maxMessageY = sd.getMessageY(maxRank)
} }
// if it is the same as the child top left, add some padding // if it is the same as the child top left, add some padding
minY := math.Min(minEdgeY, minChildY) minY := math.Min(minMessageY, minChildY)
if minY == minChildY { if minY == minChildY {
minY -= SPAN_DEPTH_GROW_FACTOR minY -= SPAN_DEPTH_GROW_FACTOR
} else { } else {
minY -= SPAN_EDGE_PAD minY -= SPAN_MESSAGE_PAD
} }
maxY := math.Max(maxEdgeY, maxChildY) maxY := math.Max(maxMessageY, maxChildY)
if maxY == maxChildY { if maxY == maxChildY {
maxY += SPAN_DEPTH_GROW_FACTOR maxY += SPAN_DEPTH_GROW_FACTOR
} else { } else {
maxY += SPAN_EDGE_PAD maxY += SPAN_MESSAGE_PAD
} }
height := math.Max(maxY-minY, MIN_SPAN_HEIGHT) height := math.Max(maxY-minY, MIN_SPAN_HEIGHT)
@ -221,57 +221,57 @@ func (sd *sequenceDiagram) placeSpans() {
} }
} }
// routeEdges routes horizontal edges from Src to Dst // routeMessages routes horizontal edges (messages) from Src to Dst
func (sd *sequenceDiagram) routeEdges() { func (sd *sequenceDiagram) routeMessages() {
for rank, edge := range sd.edges { for rank, message := range sd.messages {
isLeftToRight := edge.Src.TopLeft.X < edge.Dst.TopLeft.X isLeftToRight := message.Src.TopLeft.X < message.Dst.TopLeft.X
// finds the proper anchor point based on the edge direction // finds the proper anchor point based on the message direction
var startX, endX float64 var startX, endX float64
if sd.isActor(edge.Src) { if sd.isActor(message.Src) {
startX = edge.Src.Center().X startX = message.Src.Center().X
} else if isLeftToRight { } else if isLeftToRight {
startX = edge.Src.TopLeft.X + edge.Src.Width startX = message.Src.TopLeft.X + message.Src.Width
} else { } else {
startX = edge.Src.TopLeft.X startX = message.Src.TopLeft.X
} }
if sd.isActor(edge.Dst) { if sd.isActor(message.Dst) {
endX = edge.Dst.Center().X endX = message.Dst.Center().X
} else if isLeftToRight { } else if isLeftToRight {
endX = edge.Dst.TopLeft.X endX = message.Dst.TopLeft.X
} else { } else {
endX = edge.Dst.TopLeft.X + edge.Dst.Width endX = message.Dst.TopLeft.X + message.Dst.Width
} }
if isLeftToRight { if isLeftToRight {
startX += SPAN_EDGE_PAD startX += SPAN_MESSAGE_PAD
endX -= SPAN_EDGE_PAD endX -= SPAN_MESSAGE_PAD
} else { } else {
startX -= SPAN_EDGE_PAD startX -= SPAN_MESSAGE_PAD
endX += SPAN_EDGE_PAD endX += SPAN_MESSAGE_PAD
} }
edgeY := sd.getEdgeY(rank) messageY := sd.getMessageY(rank)
edge.Route = []*geo.Point{ message.Route = []*geo.Point{
geo.NewPoint(startX, edgeY), geo.NewPoint(startX, messageY),
geo.NewPoint(endX, edgeY), geo.NewPoint(endX, messageY),
} }
if edge.Attributes.Label.Value != "" { if message.Attributes.Label.Value != "" {
if isLeftToRight { if isLeftToRight {
edge.LabelPosition = go2.Pointer(string(label.OutsideTopCenter)) message.LabelPosition = go2.Pointer(string(label.OutsideTopCenter))
} else { } else {
// the label will be placed above the edge because the orientation is based on the edge normal vector // the label will be placed above the message because the orientation is based on the edge normal vector
edge.LabelPosition = go2.Pointer(string(label.OutsideBottomCenter)) message.LabelPosition = go2.Pointer(string(label.OutsideBottomCenter))
} }
} }
} }
} }
func (sd *sequenceDiagram) getEdgeY(rank int) float64 { func (sd *sequenceDiagram) getMessageY(rank int) float64 {
// +1 so that the first edge has the top padding for its label // +1 so that the first message has the top padding for its label
return ((float64(rank) + 1.) * sd.edgeYStep) + sd.maxActorHeight return ((float64(rank) + 1.) * sd.messageYStep) + sd.maxActorHeight
} }
func (sd *sequenceDiagram) isActor(obj *d2graph.Object) bool { func (sd *sequenceDiagram) isActor(obj *d2graph.Object) bool {

View file

@ -92,19 +92,19 @@ func TestBasicSequenceDiagram(t *testing.T) {
} }
if edge.Src.TopLeft.X < edge.Dst.TopLeft.X { if edge.Src.TopLeft.X < edge.Dst.TopLeft.X {
// left to right // left to right
if edge.Route[0].X != edge.Src.Center().X+SPAN_EDGE_PAD { if edge.Route[0].X != edge.Src.Center().X+SPAN_MESSAGE_PAD {
t.Fatalf("expected edge[%d] x to be at the actor center", i) t.Fatalf("expected edge[%d] x to be at the actor center", i)
} }
if edge.Route[1].X != edge.Dst.Center().X-SPAN_EDGE_PAD { if edge.Route[1].X != edge.Dst.Center().X-SPAN_MESSAGE_PAD {
t.Fatalf("expected edge[%d] x to be at the actor center", i) t.Fatalf("expected edge[%d] x to be at the actor center", i)
} }
} else { } else {
if edge.Route[0].X != edge.Src.Center().X-SPAN_EDGE_PAD { if edge.Route[0].X != edge.Src.Center().X-SPAN_MESSAGE_PAD {
t.Fatalf("expected edge[%d] x to be at the actor center", i) t.Fatalf("expected edge[%d] x to be at the actor center", i)
} }
if edge.Route[1].X != edge.Dst.Center().X+SPAN_EDGE_PAD { if edge.Route[1].X != edge.Dst.Center().X+SPAN_MESSAGE_PAD {
t.Fatalf("expected edge[%d] x to be at the actor center", i) t.Fatalf("expected edge[%d] x to be at the actor center", i)
} }
} }
@ -208,8 +208,8 @@ func TestSpansSequenceDiagram(t *testing.T) {
t.Fatalf("expected a.t1 and b.t1 to have the same height, got %.5f and %.5f", a_t1.Height, b_t1.Height) t.Fatalf("expected a.t1 and b.t1 to have the same height, got %.5f and %.5f", a_t1.Height, b_t1.Height)
} }
// Y diff of the 2 first edges // Y diff of the 2 first messages
expectedHeight := g.Edges[1].Route[0].Y - g.Edges[0].Route[0].Y + (2 * SPAN_EDGE_PAD) expectedHeight := g.Edges[1].Route[0].Y - g.Edges[0].Route[0].Y + (2 * SPAN_MESSAGE_PAD)
if a_t1.Height != expectedHeight { if a_t1.Height != expectedHeight {
t.Fatalf("expected a.t1 height to be %.5f, got %.5f", expectedHeight, a_t1.Height) t.Fatalf("expected a.t1 height to be %.5f, got %.5f", expectedHeight, a_t1.Height)
} }
@ -231,20 +231,20 @@ func TestSpansSequenceDiagram(t *testing.T) {
if a_t1.TopLeft.Y != b_t1.TopLeft.Y { if a_t1.TopLeft.Y != b_t1.TopLeft.Y {
t.Fatal("expected a.t1 and b.t1 to be placed at the same Y") t.Fatal("expected a.t1 and b.t1 to be placed at the same Y")
} }
if a_t1.TopLeft.Y != g.Edges[0].Route[0].Y-SPAN_EDGE_PAD { if a_t1.TopLeft.Y != g.Edges[0].Route[0].Y-SPAN_MESSAGE_PAD {
t.Fatal("expected a.t1 to be placed at the same Y of the first edge") t.Fatal("expected a.t1 to be placed at the same Y of the first message")
} }
// check routes // check routes
if g.Edges[0].Route[0].X != a_t1.TopLeft.X+a_t1.Width+SPAN_EDGE_PAD { if g.Edges[0].Route[0].X != a_t1.TopLeft.X+a_t1.Width+SPAN_MESSAGE_PAD {
t.Fatal("expected the first edge to start on a.t1 top right X") t.Fatal("expected the first message to start on a.t1 top right X")
} }
if g.Edges[0].Route[1].X != b_t1.TopLeft.X-SPAN_EDGE_PAD { if g.Edges[0].Route[1].X != b_t1.TopLeft.X-SPAN_MESSAGE_PAD {
t.Fatal("expected the first edge to end on b.t1 top left X") t.Fatal("expected the first message to end on b.t1 top left X")
} }
if g.Edges[2].Route[1].X != b.Center().X-SPAN_EDGE_PAD { if g.Edges[2].Route[1].X != b.Center().X-SPAN_MESSAGE_PAD {
t.Fatal("expected the third edge to end on b.t1 center X") t.Fatal("expected the third message to end on b.t1 center X")
} }
} }