Compare commits
3 Commits
dev
...
feat/refac
Author | SHA1 | Date | |
---|---|---|---|
6970e59413
|
|||
57a3a371eb
|
|||
c1191de661
|
@ -1,119 +1,156 @@
|
|||||||
#import "/src/cetz.typ": canvas, draw
|
#import "/src/cetz.typ" as cetz: canvas, draw
|
||||||
#import "utils.typ": get-participants-i, get-style, normalize-units
|
#import "utils.typ": get-participants-i, get-style, normalize-units, is-elmt
|
||||||
#import "group.typ"
|
#import "../group.typ"
|
||||||
#import "participant.typ"
|
#import "../participant.typ"
|
||||||
#import participant: PAR-SPECIALS
|
#import participant: PAR-SPECIALS
|
||||||
#import "sequence.typ"
|
#import "../sequence.typ"
|
||||||
#import "separator.typ"
|
#import "../separator.typ"
|
||||||
#import "sync.typ"
|
#import "../sync.typ"
|
||||||
#import "consts.typ": *
|
#import "../consts.typ": *
|
||||||
#import "note.typ" as note: get-note-box
|
#import "../note.typ" as note: get-note-box
|
||||||
|
|
||||||
#let DEBUG-INVISIBLE = false
|
#let DEBUG-INVISIBLE = false
|
||||||
|
|
||||||
#let get-columns-width(participants, elements) = {
|
#let init-lifelines(participants) = {
|
||||||
participants = participants.map(p => {
|
return participants.map(p => {
|
||||||
p.insert("lifeline-lvl", 0)
|
p.insert("lifeline-lvl", 0)
|
||||||
p.insert("max-lifelines", 0)
|
p.insert("max-lifelines", 0)
|
||||||
p
|
p
|
||||||
})
|
})
|
||||||
let pars-i = get-participants-i(participants)
|
}
|
||||||
let cells = ()
|
|
||||||
|
|
||||||
// Unwrap syncs
|
#let unwrap-syncs(elements) = {
|
||||||
let i = 0
|
let i = 0
|
||||||
while i < elements.len() {
|
while i < elements.len() {
|
||||||
let elmt = elements.at(i)
|
let elmt = elements.at(i)
|
||||||
if elmt.type == "sync" {
|
if elmt.type == "sync" {
|
||||||
elements = elements.slice(0, i + 1) + elmt.elmts + elements.slice(i + 1)
|
elements = (
|
||||||
|
elements.slice(0, i + 1) +
|
||||||
|
elmt.elmts +
|
||||||
|
elements.slice(i + 1)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
i += 1
|
i += 1
|
||||||
}
|
}
|
||||||
|
return elements
|
||||||
|
}
|
||||||
|
|
||||||
// Compute max lifeline levels
|
#let seq-update-lifelines(participants, pars-i, seq) = {
|
||||||
|
let participants = participants
|
||||||
|
let com = if seq.comment == none {""} else {seq.comment}
|
||||||
|
let i1 = pars-i.at(seq.p1)
|
||||||
|
let i2 = pars-i.at(seq.p2)
|
||||||
|
let cell = (
|
||||||
|
elmt: seq,
|
||||||
|
i1: calc.min(i1, i2),
|
||||||
|
i2: calc.max(i1, i2),
|
||||||
|
cell: box(com, inset: 3pt)
|
||||||
|
)
|
||||||
|
|
||||||
|
if seq.disable-src or seq.destroy-src {
|
||||||
|
let p = participants.at(i1)
|
||||||
|
p.lifeline-lvl -= 1
|
||||||
|
participants.at(i1) = p
|
||||||
|
}
|
||||||
|
if seq.disable-dst {
|
||||||
|
let p = participants.at(i2)
|
||||||
|
p.lifeline-lvl -= 1
|
||||||
|
participants.at(i2) = p
|
||||||
|
}
|
||||||
|
if seq.enable-dst {
|
||||||
|
let p = participants.at(i2)
|
||||||
|
p.lifeline-lvl += 1
|
||||||
|
p.max-lifelines = calc.max(p.max-lifelines, p.lifeline-lvl)
|
||||||
|
participants.at(i2) = p
|
||||||
|
}
|
||||||
|
|
||||||
|
return (participants, cell)
|
||||||
|
}
|
||||||
|
|
||||||
|
#let evt-update-lifelines(participants, pars-i, evt) = {
|
||||||
|
let par-name = evt.participant
|
||||||
|
let i = pars-i.at(par-name)
|
||||||
|
let par = participants.at(i)
|
||||||
|
if evt.event == "disable" or evt.event == "destroy" {
|
||||||
|
par.lifeline-lvl -= 1
|
||||||
|
|
||||||
|
} else if evt.event == "enable" {
|
||||||
|
par.lifeline-lvl += 1
|
||||||
|
par.max-lifelines = calc.max(par.max-lifelines, par.lifeline-lvl)
|
||||||
|
}
|
||||||
|
participants.at(i) = par
|
||||||
|
return participants
|
||||||
|
}
|
||||||
|
|
||||||
|
#let note-get-cell(note) = {
|
||||||
|
let (p1, p2) = (none, none)
|
||||||
|
let cell = none
|
||||||
|
if note.side == "left" {
|
||||||
|
p1 = "["
|
||||||
|
p2 = note.pos
|
||||||
|
cell = get-note-box(note)
|
||||||
|
} else if note.side == "right" {
|
||||||
|
p1 = note.pos
|
||||||
|
p2 = "]"
|
||||||
|
cell = get-note-box(note)
|
||||||
|
} else if note.side == "over" and note.aligned-with != none {
|
||||||
|
let box1 = get-note-box(note)
|
||||||
|
let box2 = get-note-box(note.aligned-with)
|
||||||
|
let m1 = measure(box1)
|
||||||
|
let m2 = measure(box2)
|
||||||
|
cell = box(
|
||||||
|
width: (m1.width + m2.width) / 2,
|
||||||
|
height: calc.max(m1.height, m2.height)
|
||||||
|
)
|
||||||
|
p1 = note.pos
|
||||||
|
p2 = note.aligned-with.pos
|
||||||
|
} else {
|
||||||
|
return none
|
||||||
|
}
|
||||||
|
|
||||||
|
let i1 = pars-i.at(p1)
|
||||||
|
let i2 = pars-i.at(p2)
|
||||||
|
cell = (
|
||||||
|
elmt: note,
|
||||||
|
i1: calc.min(i1, i2),
|
||||||
|
i2: calc.max(i1, i2),
|
||||||
|
cell: cell
|
||||||
|
)
|
||||||
|
|
||||||
|
return cell
|
||||||
|
}
|
||||||
|
|
||||||
|
#let compute-max-lifeline-levels(participants, elements, pars-i) = {
|
||||||
|
let cells = ()
|
||||||
for elmt in elements {
|
for elmt in elements {
|
||||||
if elmt.type == "seq" {
|
if elmt.type == "seq" {
|
||||||
let com = if elmt.comment == none {""} else {elmt.comment}
|
let cell
|
||||||
let i1 = pars-i.at(elmt.p1)
|
(participants, cell) = seq-update-lifelines(
|
||||||
let i2 = pars-i.at(elmt.p2)
|
participants,
|
||||||
cells.push(
|
pars-i,
|
||||||
(
|
elmt
|
||||||
elmt: elmt,
|
|
||||||
i1: calc.min(i1, i2),
|
|
||||||
i2: calc.max(i1, i2),
|
|
||||||
cell: box(com, inset: 3pt)
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
cells.push(cell)
|
||||||
if elmt.disable-src or elmt.destroy-src {
|
|
||||||
let p = participants.at(i1)
|
|
||||||
p.lifeline-lvl -= 1
|
|
||||||
participants.at(i1) = p
|
|
||||||
}
|
|
||||||
if elmt.disable-dst {
|
|
||||||
let p = participants.at(i2)
|
|
||||||
p.lifeline-lvl -= 1
|
|
||||||
participants.at(i2) = p
|
|
||||||
}
|
|
||||||
if elmt.enable-dst {
|
|
||||||
let p = participants.at(i2)
|
|
||||||
p.lifeline-lvl += 1
|
|
||||||
p.max-lifelines = calc.max(p.max-lifelines, p.lifeline-lvl)
|
|
||||||
participants.at(i2) = p
|
|
||||||
}
|
|
||||||
} else if elmt.type == "evt" {
|
} else if elmt.type == "evt" {
|
||||||
let par-name = elmt.participant
|
participants = evt-update-lifelines(
|
||||||
let i = pars-i.at(par-name)
|
participants,
|
||||||
let par = participants.at(i)
|
pars-i,
|
||||||
if elmt.event == "disable" or elmt.event == "destroy" {
|
elmt
|
||||||
par.lifeline-lvl -= 1
|
)
|
||||||
|
|
||||||
} else if elmt.event == "enable" {
|
|
||||||
par.lifeline-lvl += 1
|
|
||||||
par.max-lifelines = calc.max(par.max-lifelines, par.lifeline-lvl)
|
|
||||||
}
|
|
||||||
participants.at(i) = par
|
|
||||||
|
|
||||||
} else if elmt.type == "note" {
|
} else if elmt.type == "note" {
|
||||||
let (p1, p2) = (none, none)
|
let cell = note-get-cell(elmt)
|
||||||
let cell = none
|
if cell != none {
|
||||||
if elmt.side == "left" {
|
cells.push(cell)
|
||||||
p1 = "["
|
|
||||||
p2 = elmt.pos
|
|
||||||
cell = get-note-box(elmt)
|
|
||||||
} else if elmt.side == "right" {
|
|
||||||
p1 = elmt.pos
|
|
||||||
p2 = "]"
|
|
||||||
cell = get-note-box(elmt)
|
|
||||||
} else if elmt.side == "over" {
|
|
||||||
if elmt.aligned-with != none {
|
|
||||||
let box1 = get-note-box(elmt)
|
|
||||||
let box2 = get-note-box(elmt.aligned-with)
|
|
||||||
let m1 = measure(box1)
|
|
||||||
let m2 = measure(box2)
|
|
||||||
cell = box(width: (m1.width + m2.width) / 2, height: calc.max(m1.height, m2.height))
|
|
||||||
p1 = elmt.pos
|
|
||||||
p2 = elmt.aligned-with.pos
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if p1 != none and p2 != none and cell != none {
|
|
||||||
let i1 = pars-i.at(p1)
|
|
||||||
let i2 = pars-i.at(p2)
|
|
||||||
cells.push(
|
|
||||||
(
|
|
||||||
elmt: elmt,
|
|
||||||
i1: calc.min(i1, i2),
|
|
||||||
i2: calc.max(i1, i2),
|
|
||||||
cell: cell
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compute column widths
|
return (participants, elements, cells)
|
||||||
// Compute minimum widths for participant names and shapes
|
}
|
||||||
|
|
||||||
|
/// Compute minimum widths for participant names and shapes
|
||||||
|
#let participants-min-col-widths(participants) = {
|
||||||
let widths = ()
|
let widths = ()
|
||||||
for i in range(participants.len() - 1) {
|
for i in range(participants.len() - 1) {
|
||||||
let p1 = participants.at(i)
|
let p1 = participants.at(i)
|
||||||
@ -124,11 +161,15 @@
|
|||||||
let w2 = m2.width
|
let w2 = m2.width
|
||||||
widths.push(w1 / 2pt + w2 / 2pt + PAR-SPACE)
|
widths.push(w1 / 2pt + w2 / 2pt + PAR-SPACE)
|
||||||
}
|
}
|
||||||
|
return widths
|
||||||
|
}
|
||||||
|
|
||||||
// Compute minimum width for over notes
|
/// Compute minimum width for over notes
|
||||||
for n in elements.filter(e => (e.type == "note" and
|
#let notes-min-col-widths(elements, widths, pars-i) = {
|
||||||
e.side == "over" and
|
let widths = widths
|
||||||
type(e.pos) == str)) {
|
let notes = elements.filter(e => e.type == "note")
|
||||||
|
for n in notes.filter(e => (e.side == "over" and
|
||||||
|
type(e.pos) == str)) {
|
||||||
|
|
||||||
let m = note.get-size(n)
|
let m = note.get-size(n)
|
||||||
let i = pars-i.at(n.pos)
|
let i = pars-i.at(n.pos)
|
||||||
@ -146,8 +187,12 @@
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return widths
|
||||||
|
}
|
||||||
|
|
||||||
// Compute minimum width for simple sequences (spanning 1 column)
|
/// Compute minimum width for simple sequences (spanning 1 column)
|
||||||
|
#let simple-seq-min-col-widths(cells, widths) = {
|
||||||
|
let widths = widths
|
||||||
for cell in cells.filter(c => c.i2 - c.i1 == 1) {
|
for cell in cells.filter(c => c.i2 - c.i1 == 1) {
|
||||||
let m = measure(cell.cell)
|
let m = measure(cell.cell)
|
||||||
widths.at(cell.i1) = calc.max(
|
widths.at(cell.i1) = calc.max(
|
||||||
@ -155,9 +200,14 @@
|
|||||||
m.width / 1pt + COMMENT-PAD
|
m.width / 1pt + COMMENT-PAD
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
return widths
|
||||||
|
}
|
||||||
|
|
||||||
// Compute minimum width for self sequences
|
/// Compute minimum width for self sequences
|
||||||
for cell in cells.filter(c => c.elmt.type == "seq" and c.i1 == c.i2) {
|
#let self-seq-min-col-widths(cells, widths) = {
|
||||||
|
let widths = widths
|
||||||
|
for cell in cells.filter(c => (c.elmt.type == "seq" and
|
||||||
|
c.i1 == c.i2)) {
|
||||||
let m = measure(cell.cell)
|
let m = measure(cell.cell)
|
||||||
let i = cell.i1
|
let i = cell.i1
|
||||||
if cell.elmt.flip {
|
if cell.elmt.flip {
|
||||||
@ -170,72 +220,112 @@
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return widths
|
||||||
|
}
|
||||||
|
|
||||||
// Compute remaining widths for longer sequences (spanning multiple columns)
|
/// Compute remaining widths for longer sequences (spanning multiple columns)
|
||||||
|
#let long-seq-min-col-widths(cells, widths) = {
|
||||||
|
let widths = widths
|
||||||
let multicol-cells = cells.filter(c => c.i2 - c.i1 > 1)
|
let multicol-cells = cells.filter(c => c.i2 - c.i1 > 1)
|
||||||
multicol-cells = multicol-cells.sorted(key: c => {
|
multicol-cells = multicol-cells.sorted(key: c => {
|
||||||
c.i1 * 1000 + c.i2
|
c.i1 * 1000 + c.i2
|
||||||
})
|
})
|
||||||
for cell in multicol-cells {
|
for cell in multicol-cells {
|
||||||
let m = measure(cell.cell)
|
let m = measure(cell.cell)
|
||||||
|
let width = (
|
||||||
|
m.width / 1pt +
|
||||||
|
COMMENT-PAD -
|
||||||
|
widths.slice(cell.i1, cell.i2 - 1).sum()
|
||||||
|
)
|
||||||
widths.at(cell.i2 - 1) = calc.max(
|
widths.at(cell.i2 - 1) = calc.max(
|
||||||
widths.at(cell.i2 - 1),
|
widths.at(cell.i2 - 1), width
|
||||||
m.width / 1pt + COMMENT-PAD - widths.slice(cell.i1, cell.i2 - 1).sum()
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
return widths
|
||||||
|
}
|
||||||
|
|
||||||
// Add lifeline widths
|
/// Add lifeline widths
|
||||||
for (i, w) in widths.enumerate() {
|
#let col-widths-add-lifelines(participants, widths) = {
|
||||||
|
return widths.enumerate().map(((i, w)) => {
|
||||||
let p1 = participants.at(i)
|
let p1 = participants.at(i)
|
||||||
let p2 = participants.at(i + 1)
|
let p2 = participants.at(i + 1)
|
||||||
let w = w + p1.max-lifelines * LIFELINE-W / 2
|
w += p1.max-lifelines * LIFELINE-W / 2
|
||||||
if p2.max-lifelines != 0 {
|
if p2.max-lifelines != 0 {
|
||||||
w += LIFELINE-W / 2
|
w += LIFELINE-W / 2
|
||||||
}
|
}
|
||||||
widths.at(i) = w
|
return w
|
||||||
}
|
})
|
||||||
|
}
|
||||||
|
|
||||||
for elmt in elements {
|
#let process-col-elements(elements, widths, pars-i) = {
|
||||||
if elmt.type == "col" {
|
let widths = widths
|
||||||
let i1 = pars-i.at(elmt.p1)
|
let cols = elements.filter(e => e.type == "col")
|
||||||
let i2 = pars-i.at(elmt.p2)
|
for col in cols {
|
||||||
if calc.abs(i1 - i2) != 1 {
|
let i1 = pars-i.at(col.p1)
|
||||||
let i-min = calc.min(i1, i2)
|
let i2 = pars-i.at(col.p2)
|
||||||
let i-max = calc.max(i1, i2)
|
if calc.abs(i1 - i2) != 1 {
|
||||||
let others = pars-i.pairs()
|
let i-min = calc.min(i1, i2)
|
||||||
.sorted(key: p => p.last())
|
let i-max = calc.max(i1, i2)
|
||||||
.slice(i-min + 1, i-max)
|
let others = pars-i.pairs()
|
||||||
.map(p => "'" + p.first() + "'")
|
.sorted(key: p => p.last())
|
||||||
.join(", ")
|
.slice(i-min + 1, i-max)
|
||||||
panic(
|
.map(p => "'" + p.first() + "'")
|
||||||
"Column participants must be consecutive (participants (" +
|
.join(", ")
|
||||||
others +
|
panic(
|
||||||
") are in between)"
|
"Column participants must be consecutive (participants (" +
|
||||||
)
|
others +
|
||||||
}
|
") are in between)"
|
||||||
let i = calc.min(i1, i2)
|
)
|
||||||
|
|
||||||
if elmt.width != auto {
|
|
||||||
widths.at(i) = normalize-units(elmt.width)
|
|
||||||
}
|
|
||||||
|
|
||||||
let width = widths.at(i)
|
|
||||||
width = calc.max(width, normalize-units(elmt.min-width))
|
|
||||||
if elmt.max-width != none {
|
|
||||||
width = calc.min(width, normalize-units(elmt.max-width))
|
|
||||||
}
|
|
||||||
widths.at(i) = width + normalize-units(elmt.margin)
|
|
||||||
}
|
}
|
||||||
}
|
let i = calc.min(i1, i2)
|
||||||
|
|
||||||
|
let width = widths.at(i)
|
||||||
|
|
||||||
|
if col.width != auto {
|
||||||
|
width = normalize-units(col.width)
|
||||||
|
}
|
||||||
|
|
||||||
|
width = calc.max(
|
||||||
|
width,
|
||||||
|
normalize-units(col.min-width)
|
||||||
|
)
|
||||||
|
if col.max-width != none {
|
||||||
|
width = calc.min(
|
||||||
|
width,
|
||||||
|
normalize-units(col.max-width)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
widths.at(i) = width + normalize-units(col.margin)
|
||||||
|
}
|
||||||
|
return widths
|
||||||
|
}
|
||||||
|
|
||||||
|
#let compute-columns-width(participants, elements, pars-i) = {
|
||||||
|
elements = elements.filter(is-elmt)
|
||||||
|
elements = unwrap-syncs(elements)
|
||||||
|
|
||||||
|
let cells
|
||||||
|
(participants, elements, cells) = compute-max-lifeline-levels(participants, elements, pars-i)
|
||||||
|
|
||||||
|
let widths = participants-min-col-widths(participants)
|
||||||
|
widths = notes-min-col-widths(elements, widths, pars-i)
|
||||||
|
widths = simple-seq-min-col-widths(cells, widths)
|
||||||
|
widths = self-seq-min-col-widths(cells, widths)
|
||||||
|
widths = long-seq-min-col-widths(cells, widths)
|
||||||
|
widths = col-widths-add-lifelines(participants, widths)
|
||||||
|
widths = process-col-elements(elements, widths, pars-i)
|
||||||
return widths
|
return widths
|
||||||
}
|
}
|
||||||
|
|
||||||
#let render(participants, elements) = context canvas(length: 1pt, {
|
#let render(participants, elements) = context canvas(length: 1pt, {
|
||||||
|
let participants = participants
|
||||||
|
let elements = elements
|
||||||
|
|
||||||
let shapes = ()
|
let shapes = ()
|
||||||
|
participants = init-lifelines(participants)
|
||||||
let pars-i = get-participants-i(participants)
|
let pars-i = get-participants-i(participants)
|
||||||
|
|
||||||
let widths = get-columns-width(participants, elements)
|
let widths = compute-columns-width(participants, elements, pars-i)
|
||||||
|
|
||||||
// Compute each column's X position
|
// Compute each column's X position
|
||||||
let x-pos = (0,)
|
let x-pos = (0,)
|
||||||
@ -267,8 +357,11 @@
|
|||||||
|
|
||||||
// Draw elemnts
|
// Draw elemnts
|
||||||
for elmt in elements {
|
for elmt in elements {
|
||||||
|
if not is-elmt(elmt) {
|
||||||
|
shapes.push(elmt)
|
||||||
|
|
||||||
// Sequences
|
// Sequences
|
||||||
if elmt.type == "seq" {
|
} else if elmt.type == "seq" {
|
||||||
let shps
|
let shps
|
||||||
(y, lifelines, shps) = draw-seq(elmt, y, lifelines)
|
(y, lifelines, shps) = draw-seq(elmt, y, lifelines)
|
||||||
shapes += shps
|
shapes += shps
|
317
src/core/setup.typ
Normal file
317
src/core/setup.typ
Normal file
@ -0,0 +1,317 @@
|
|||||||
|
#import "utils.typ": is-elmt, get-group-span
|
||||||
|
#import "../participant.typ": _exists as par-exists, _par
|
||||||
|
|
||||||
|
#let flatten-group(elmts, i) = {
|
||||||
|
let group = elmts.at(i)
|
||||||
|
elmts.at(i) = group
|
||||||
|
return (
|
||||||
|
elmts.slice(0, i + 1) +
|
||||||
|
group.elmts +
|
||||||
|
((
|
||||||
|
type: "grp-end",
|
||||||
|
start-i: i
|
||||||
|
),) +
|
||||||
|
elmts.slice(i+1)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#let update-group-children(elmts, i) = {
|
||||||
|
let elmts = elmts
|
||||||
|
let group-end = elmts.at(i)
|
||||||
|
|
||||||
|
elmts.at(group-end.start-i).elmts = elmts.slice(group-end.start-i + 1, i)
|
||||||
|
return elmts
|
||||||
|
}
|
||||||
|
|
||||||
|
#let convert-return(elmts, i, activation-history) = {
|
||||||
|
if activation-history.len() == 0 {
|
||||||
|
panic("Cannot return if no lifeline is activated")
|
||||||
|
}
|
||||||
|
let elmts = elmts
|
||||||
|
let activation-history = activation-history
|
||||||
|
let ret = elmts.at(i)
|
||||||
|
let seq = activation-history.pop()
|
||||||
|
elmts.at(i) = _seq(
|
||||||
|
seq.p2, seq.p1,
|
||||||
|
comment: ret.comment,
|
||||||
|
disable-src: true,
|
||||||
|
dashed: true
|
||||||
|
).first()
|
||||||
|
return (elmts, activation-history)
|
||||||
|
}
|
||||||
|
|
||||||
|
#let unwrap-containers(elmts) = {
|
||||||
|
let elmts = elmts
|
||||||
|
let i = 0
|
||||||
|
let activation-history = ()
|
||||||
|
|
||||||
|
// Flatten groups + convert returns
|
||||||
|
while i < elmts.len() {
|
||||||
|
let elmt = elmts.at(i)
|
||||||
|
if not is-elmt(elmt) {
|
||||||
|
i += 1
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if elmt.type == "grp" {
|
||||||
|
elmts = flatten-group(elmts, i)
|
||||||
|
|
||||||
|
} else if elmt.type == "seq" {
|
||||||
|
if elmt.enable-dst {
|
||||||
|
activation-history.push(elmt)
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if elmt.type == "evt" {
|
||||||
|
if elmt.event == "enable" {
|
||||||
|
for elmt2 in elmts.slice(0, i).rev() {
|
||||||
|
if elmt2.type == "seq" {
|
||||||
|
activation-history.push(elmt2)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if elmt.type == "ret" {
|
||||||
|
(elmts, activation-history) = convert-return(elmts, i, activation-history)
|
||||||
|
}
|
||||||
|
i += 1
|
||||||
|
}
|
||||||
|
|
||||||
|
return (elmts, activation-history)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#let prepare-seq-participants(ctx, seq) = {
|
||||||
|
let ctx = ctx
|
||||||
|
if not par-exists(ctx.participants, seq.p1) {
|
||||||
|
ctx.participants.push(_par(seq.p1).first())
|
||||||
|
}
|
||||||
|
if not par-exists(ctx.participants, seq.p2) {
|
||||||
|
ctx.participants.push(_par(
|
||||||
|
seq.p2,
|
||||||
|
from-start: not seq.create-dst
|
||||||
|
).first())
|
||||||
|
|
||||||
|
} else if seq.create-dst {
|
||||||
|
let i = ctx.participants.position(p => p.name == seq.p2)
|
||||||
|
ctx.participants.at(i).from-start = false
|
||||||
|
}
|
||||||
|
|
||||||
|
let p1 = seq.p1
|
||||||
|
let p2 = seq.p2
|
||||||
|
if seq.p1 == "?" {
|
||||||
|
p1 = "?" + seq.p2
|
||||||
|
}
|
||||||
|
if seq.p2 == "?" {
|
||||||
|
p2 = seq.p1 + "?"
|
||||||
|
}
|
||||||
|
ctx.linked.push(p1)
|
||||||
|
ctx.linked.push(p2)
|
||||||
|
ctx.last-seq = (
|
||||||
|
elmt: seq,
|
||||||
|
i: ctx.i,
|
||||||
|
p1: p1,
|
||||||
|
p2: p2
|
||||||
|
)
|
||||||
|
return ctx
|
||||||
|
}
|
||||||
|
|
||||||
|
#let prepare-note-participants(ctx, note) = {
|
||||||
|
let ctx = ctx
|
||||||
|
let note = note
|
||||||
|
note.insert(
|
||||||
|
"linked",
|
||||||
|
note.pos == none and note.side != "across"
|
||||||
|
)
|
||||||
|
if note.pos == none and note.side != "across" {
|
||||||
|
let names = ctx.participants.map(p => p.name)
|
||||||
|
let i1 = names.position(n => n == ctx.last-seq.p1)
|
||||||
|
let i2 = names.position(n => n == ctx.last-seq.p2)
|
||||||
|
let pars = (
|
||||||
|
(i1, ctx.last-seq.p1),
|
||||||
|
(i2, ctx.last-seq.p2)
|
||||||
|
).sorted(key: p => p.first())
|
||||||
|
|
||||||
|
if note.side == "left" {
|
||||||
|
note.pos = pars.first().last()
|
||||||
|
} else if note.side == "right" {
|
||||||
|
note.pos = pars.last().last()
|
||||||
|
}
|
||||||
|
|
||||||
|
let seq = last-seq.note
|
||||||
|
seq.insert("linked-note", note)
|
||||||
|
ctx.elmts.at(last-seq.i) = seq
|
||||||
|
}
|
||||||
|
if note.aligned {
|
||||||
|
let n = last-note.note
|
||||||
|
n.aligned-with = note
|
||||||
|
ctx.elmts.at(last-note.i) = n
|
||||||
|
}
|
||||||
|
if note.side == "left" {
|
||||||
|
ctx.linked.push("[")
|
||||||
|
} else if note.side == "right" {
|
||||||
|
ctx.linked.push("]")
|
||||||
|
}
|
||||||
|
|
||||||
|
let pars = none
|
||||||
|
if type(note.pos) == str {
|
||||||
|
pars = (note.pos,)
|
||||||
|
} else if type(note.pos) == array {
|
||||||
|
pars = note.pos
|
||||||
|
}
|
||||||
|
if pars != none {
|
||||||
|
for par in pars {
|
||||||
|
if not par-exists(ctx.participants, par) {
|
||||||
|
participants.push(_par(par).first())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.elmts.at(ctx.i) = note
|
||||||
|
|
||||||
|
ctx.last-note = (
|
||||||
|
elmt: note,
|
||||||
|
i: ctx.i
|
||||||
|
)
|
||||||
|
|
||||||
|
return ctx
|
||||||
|
}
|
||||||
|
|
||||||
|
#let prepare-evt-participants(ctx, evt) = {
|
||||||
|
let par = evt.participant
|
||||||
|
if not par-exists(ctx.participants, par) {
|
||||||
|
let p = _par(
|
||||||
|
par,
|
||||||
|
from-start: evt.event != "create"
|
||||||
|
).first()
|
||||||
|
ctx.participants.push(p)
|
||||||
|
|
||||||
|
} else if evt.event == "create" {
|
||||||
|
let i = ctx.participants.position(p => p.name == par)
|
||||||
|
ctx.participants.at(i).from-start = false
|
||||||
|
}
|
||||||
|
return ctx
|
||||||
|
}
|
||||||
|
|
||||||
|
#let normalize-special-participants(elmt) = {
|
||||||
|
if elmt.p1 == "?" {
|
||||||
|
elmt.p1 = "?" + elmt.p2
|
||||||
|
} else if elmt.p2 == "?" {
|
||||||
|
elmt.p2 = elmt.p1 + "?"
|
||||||
|
}
|
||||||
|
return elmt
|
||||||
|
}
|
||||||
|
|
||||||
|
#let prepare-participants(elmts) = {
|
||||||
|
let ctx = (
|
||||||
|
linked: (),
|
||||||
|
last-seq: none,
|
||||||
|
last-note: none,
|
||||||
|
participants: (),
|
||||||
|
elmts: elmts,
|
||||||
|
i: 0
|
||||||
|
)
|
||||||
|
|
||||||
|
for (i, elmt) in ctx.elmts.enumerate() {
|
||||||
|
ctx.i = i
|
||||||
|
if not is-elmt(elmt) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if elmt.type == "par" {
|
||||||
|
ctx.participants.push(elmt)
|
||||||
|
|
||||||
|
} else if elmt.type == "seq" {
|
||||||
|
ctx = prepare-seq-participants(ctx, elmt)
|
||||||
|
|
||||||
|
} else if elmt.type == "note" {
|
||||||
|
ctx = prepare-note-participants(ctx, elmt)
|
||||||
|
|
||||||
|
} else if elmt.type == "evt" {
|
||||||
|
ctx = prepare-evt-participants(ctx, elmt)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ctx.linked = ctx.linked.dedup()
|
||||||
|
|
||||||
|
let pars = ctx.participants
|
||||||
|
let participants = ()
|
||||||
|
|
||||||
|
if "[" in ctx.linked {
|
||||||
|
participants.push(_par("[", invisible: true).first())
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i, p) in pars.enumerate() {
|
||||||
|
let before = _par(
|
||||||
|
"?" + p.name,
|
||||||
|
invisible: true
|
||||||
|
).first()
|
||||||
|
let after = _par(
|
||||||
|
p.name + "?",
|
||||||
|
invisible: true
|
||||||
|
).first()
|
||||||
|
|
||||||
|
if before.name in ctx.linked {
|
||||||
|
if participants.len() == 0 or not participants.last().name.ends-with("?") {
|
||||||
|
participants.push(before)
|
||||||
|
} else {
|
||||||
|
participants.insert(-1, before)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
participants.push(p)
|
||||||
|
|
||||||
|
if after.name in ctx.linked {
|
||||||
|
participants.push(after)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if "]" in ctx.linked {
|
||||||
|
participants.push(_par(
|
||||||
|
"]",
|
||||||
|
invisible: true
|
||||||
|
).first())
|
||||||
|
}
|
||||||
|
|
||||||
|
return (ctx.elmts, participants)
|
||||||
|
}
|
||||||
|
|
||||||
|
#let finalize-setup(elmts, participants) = {
|
||||||
|
for (i, p) in participants.enumerate() {
|
||||||
|
p.insert("i", i)
|
||||||
|
participants.at(i) = p
|
||||||
|
}
|
||||||
|
|
||||||
|
let containers = ()
|
||||||
|
|
||||||
|
for (i, elmt) in elmts.enumerate() {
|
||||||
|
if not is-elmt(elmt) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if elmt.type == "seq" {
|
||||||
|
elmts.at(i) = normalize-special-participants(elmt)
|
||||||
|
} else if elmt.type == "grp-end" {
|
||||||
|
// Put back elements in group because they might have changed
|
||||||
|
elmts = update-group-children(elmts, i)
|
||||||
|
} else if elmt.type in ("grp", "alt") {
|
||||||
|
containers.push(i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compute groups spans (horizontal)
|
||||||
|
for i in containers {
|
||||||
|
let elmt = elmts.at(i)
|
||||||
|
let (min-i, max-i) = get-group-span(participants, elmt)
|
||||||
|
elmts.at(i).insert("min-i", min-i)
|
||||||
|
elmts.at(i).insert("max-i", max-i)
|
||||||
|
}
|
||||||
|
|
||||||
|
return (elmts, participants)
|
||||||
|
}
|
||||||
|
|
||||||
|
#let setup(elements) = {
|
||||||
|
let (elmts, activation-history) = unwrap-containers(elements)
|
||||||
|
|
||||||
|
let participants
|
||||||
|
(elmts, participants) = prepare-participants(elmts)
|
||||||
|
|
||||||
|
return finalize-setup(elmts, participants)
|
||||||
|
}
|
@ -1,3 +1,13 @@
|
|||||||
|
#let is-elmt(elmt) = {
|
||||||
|
if type(elmt) != dictionary {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if "type" not in elmt {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
#let normalize-units(value) = {
|
#let normalize-units(value) = {
|
||||||
if type(value) == int or type(value) == float {
|
if type(value) == int or type(value) == float {
|
||||||
return value
|
return value
|
215
src/diagram.typ
215
src/diagram.typ
@ -1,8 +1,10 @@
|
|||||||
#import "utils.typ": get-group-span, fit-canvas
|
#import "core/utils.typ": fit-canvas
|
||||||
#import "renderer.typ": render
|
#import "core/renderer.typ": render
|
||||||
#import "participant.typ" as participant: _par, PAR-SPECIALS
|
#import "participant.typ" as participant: _par, PAR-SPECIALS
|
||||||
#import "sequence.typ": _seq
|
#import "sequence.typ": _seq
|
||||||
|
|
||||||
|
#import "core/setup.typ": setup
|
||||||
|
|
||||||
#let _gap(size: 20) = {
|
#let _gap(size: 20) = {
|
||||||
return ((
|
return ((
|
||||||
type: "gap",
|
type: "gap",
|
||||||
@ -36,214 +38,7 @@
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
let participants = ()
|
let (elmts, participants) = setup(elements)
|
||||||
let elmts = elements
|
|
||||||
let i = 0
|
|
||||||
|
|
||||||
let activation-history = ()
|
|
||||||
|
|
||||||
// Flatten groups + convert returns
|
|
||||||
while i < elmts.len() {
|
|
||||||
let elmt = elmts.at(i)
|
|
||||||
if elmt.type == "grp" {
|
|
||||||
elmt.elmts = elmt.elmts.map(e => {
|
|
||||||
if e.type == "seq" {
|
|
||||||
if e.p1 == "?" {
|
|
||||||
e.p1 = "?" + e.p2
|
|
||||||
} else if e.p2 == "?" {
|
|
||||||
e.p2 = e.p1 + "?"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
e
|
|
||||||
})
|
|
||||||
elmts.at(i) = elmt
|
|
||||||
elmts = (
|
|
||||||
elmts.slice(0, i + 1) +
|
|
||||||
elmt.elmts +
|
|
||||||
((
|
|
||||||
type: "grp-end",
|
|
||||||
start-i: i
|
|
||||||
),) +
|
|
||||||
elmts.slice(i+1)
|
|
||||||
)
|
|
||||||
} else if elmt.type == "grp-end" {
|
|
||||||
// Put back elements in group because they might have changed
|
|
||||||
elmts.at(elmt.start-i).elmts = elmts.slice(elmt.start-i + 1, i)
|
|
||||||
|
|
||||||
} else if elmt.type == "seq" {
|
|
||||||
if elmt.enable-dst {
|
|
||||||
activation-history.push(elmt)
|
|
||||||
}
|
|
||||||
} else if elmt.type == "evt" {
|
|
||||||
if elmt.event == "enable" {
|
|
||||||
for elmt2 in elmts.slice(0, i).rev() {
|
|
||||||
if elmt2.type == "seq" {
|
|
||||||
activation-history.push(elmt2)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if elmt.type == "ret" {
|
|
||||||
if activation-history.len() == 0 {
|
|
||||||
panic("Cannot return if no lifeline is activated")
|
|
||||||
}
|
|
||||||
let seq = activation-history.pop()
|
|
||||||
elmts.at(i) = _seq(
|
|
||||||
seq.p2, seq.p1,
|
|
||||||
comment: elmt.comment,
|
|
||||||
disable-src: true,
|
|
||||||
dashed: true
|
|
||||||
).first()
|
|
||||||
}
|
|
||||||
i += 1
|
|
||||||
}
|
|
||||||
|
|
||||||
// List participants
|
|
||||||
let linked = ()
|
|
||||||
let last-seq = none
|
|
||||||
let last-note = none
|
|
||||||
for (i, elmt) in elmts.enumerate() {
|
|
||||||
if elmt.type == "par" {
|
|
||||||
participants.push(elmt)
|
|
||||||
} else if elmt.type == "seq" {
|
|
||||||
if not participant._exists(participants, elmt.p1) {
|
|
||||||
participants.push(_par(elmt.p1).first())
|
|
||||||
}
|
|
||||||
if not participant._exists(participants, elmt.p2) {
|
|
||||||
let par = _par(elmt.p2, from-start: not elmt.create-dst).first()
|
|
||||||
participants.push(par)
|
|
||||||
|
|
||||||
} else if elmt.create-dst {
|
|
||||||
let i = participants.position(p => p.name == elmt.p2)
|
|
||||||
participants.at(i).from-start = false
|
|
||||||
}
|
|
||||||
|
|
||||||
let p1 = elmt.p1
|
|
||||||
let p2 = elmt.p2
|
|
||||||
if elmt.p1 == "?" {
|
|
||||||
p1 = "?" + elmt.p2
|
|
||||||
}
|
|
||||||
if elmt.p2 == "?" {
|
|
||||||
p2 = elmt.p1 + "?"
|
|
||||||
}
|
|
||||||
linked.push(p1)
|
|
||||||
linked.push(p2)
|
|
||||||
last-seq = (
|
|
||||||
elmt: elmt,
|
|
||||||
i: i,
|
|
||||||
p1: p1,
|
|
||||||
p2: p2
|
|
||||||
)
|
|
||||||
} else if elmt.type == "note" {
|
|
||||||
elmt.insert("linked", elmt.pos == none and elmt.side != "across")
|
|
||||||
if elmt.pos == none and elmt.side != "across" {
|
|
||||||
let names = participants.map(p => p.name)
|
|
||||||
let i1 = names.position(n => n == last-seq.p1)
|
|
||||||
let i2 = names.position(n => n == last-seq.p2)
|
|
||||||
let pars = ((i1, last-seq.p1), (i2, last-seq.p2)).sorted(key: p => p.first())
|
|
||||||
if elmt.side == "left" {
|
|
||||||
elmt.pos = pars.first().last()
|
|
||||||
} else if elmt.side == "right" {
|
|
||||||
elmt.pos = pars.last().last()
|
|
||||||
}
|
|
||||||
|
|
||||||
let seq = last-seq.elmt
|
|
||||||
seq.insert("linked-note", elmt)
|
|
||||||
elmts.at(last-seq.i) = seq
|
|
||||||
}
|
|
||||||
if elmt.aligned {
|
|
||||||
let n = last-note.elmt
|
|
||||||
n.aligned-with = elmt
|
|
||||||
elmts.at(last-note.i) = n
|
|
||||||
}
|
|
||||||
elmts.at(i) = elmt
|
|
||||||
if elmt.side == "left" {
|
|
||||||
linked.push("[")
|
|
||||||
} else if elmt.side == "right" {
|
|
||||||
linked.push("]")
|
|
||||||
}
|
|
||||||
|
|
||||||
let pars = none
|
|
||||||
if type(elmt.pos) == str {
|
|
||||||
pars = (elmt.pos,)
|
|
||||||
} else if type(elmt.pos) == array {
|
|
||||||
pars = elmt.pos
|
|
||||||
}
|
|
||||||
if pars != none {
|
|
||||||
for par in pars {
|
|
||||||
if not participant._exists(participants, par) {
|
|
||||||
participants.push(_par(par).first())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
last-note = (
|
|
||||||
elmt: elmt,
|
|
||||||
i: i
|
|
||||||
)
|
|
||||||
} else if elmt.type == "evt" {
|
|
||||||
let par = elmt.participant
|
|
||||||
if not participant._exists(participants, par) {
|
|
||||||
let p = _par(par, from-start: elmt.event != "create").first()
|
|
||||||
participants.push(p)
|
|
||||||
|
|
||||||
} else if elmt.event == "create" {
|
|
||||||
let i = participants.position(p => p.name == par)
|
|
||||||
participants.at(i).from-start = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
linked = linked.dedup()
|
|
||||||
|
|
||||||
let pars = participants
|
|
||||||
participants = ()
|
|
||||||
|
|
||||||
if "[" in linked {
|
|
||||||
participants.push(_par("[", invisible: true).first())
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i, p) in pars.enumerate() {
|
|
||||||
let before = _par("?" + p.name, invisible: true).first()
|
|
||||||
let after = _par(p.name + "?", invisible: true).first()
|
|
||||||
|
|
||||||
if before.name in linked {
|
|
||||||
if participants.len() == 0 or not participants.last().name.ends-with("?") {
|
|
||||||
participants.push(before)
|
|
||||||
} else {
|
|
||||||
participants.insert(-1, before)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
participants.push(p)
|
|
||||||
|
|
||||||
if after.name in linked {
|
|
||||||
participants.push(after)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if "]" in linked {
|
|
||||||
participants.push(_par("]", invisible: true).first())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add index to participant
|
|
||||||
for (i, p) in participants.enumerate() {
|
|
||||||
p.insert("i", i)
|
|
||||||
participants.at(i) = p
|
|
||||||
}
|
|
||||||
|
|
||||||
// Compute groups spans (horizontal)
|
|
||||||
for (i, elmt) in elmts.enumerate() {
|
|
||||||
if elmt.type == "grp" or elmt.type == "alt" {
|
|
||||||
let (min-i, max-i) = get-group-span(participants, elmt)
|
|
||||||
elmts.at(i).insert("min-i", min-i)
|
|
||||||
elmts.at(i).insert("max-i", max-i)
|
|
||||||
} else if elmt.type == "seq" {
|
|
||||||
if elmt.p1 == "?" {
|
|
||||||
elmts.at(i).p1 = "?" + elmt.p2
|
|
||||||
} else if elmt.p2 == "?" {
|
|
||||||
elmts.at(i).p2 = elmt.p1 + "?"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let canvas = render(participants, elmts)
|
let canvas = render(participants, elmts)
|
||||||
fit-canvas(canvas, width: width)
|
fit-canvas(canvas, width: width)
|
||||||
|
Reference in New Issue
Block a user