Render explicit scenario lifetimes and preserve marker selection

This commit is contained in:
2026-09-11 05:25:04 -07:00
parent b853a1eca3
commit 52f6e176cd
15 changed files with 2231 additions and 97 deletions
+140 -39
View File
@@ -16,8 +16,6 @@ use oa_core::*;
use std::collections::BTreeMap;
pub(crate) mod creation;
pub(crate) mod scenario;
// Projection acceptance precedes the coordinated scenario renderer integration.
#[cfg(test)]
mod scenario_lifecycle;
pub(crate) mod state_canvas;
pub(crate) mod state_edit;
@@ -1092,6 +1090,7 @@ struct Scene {
rows: BTreeMap<Id, (Id, f32)>,
open_calls: Vec<Point>,
sequence_bounds: BTreeMap<Id, (f32, f32)>,
lifecycle: scenario_lifecycle::render::Rendering,
}
struct SequenceSlot {
slot: scenario::Slot,
@@ -1150,6 +1149,7 @@ impl Scene {
rows: BTreeMap::new(),
open_calls: vec![],
sequence_bounds: BTreeMap::new(),
lifecycle: scenario_lifecycle::render::Rendering::default(),
};
let mut lanes = p
.children(owner)
@@ -1236,7 +1236,17 @@ impl Scene {
}
let mut y = if scenario { 148. } else { 120. };
scene.occurrences(p, owner, scenario, &lane_x, &mut y, 0);
scene.size.height = (y + 45.).max(210.);
if scenario {
scene.lifecycle = scenario_lifecycle::render::Rendering::new(
p,
owner,
&lane_x,
&scene.rows,
&scene.sequence_bounds,
scene.size.height,
);
scene.lifecycle.align_messages(&mut scene.lines);
let calls: Vec<_> = scene
.rows
.keys()
@@ -1261,23 +1271,28 @@ impl Scene {
scene.activation_sequence(p, p.elements[&call].parent.unwrap(), call, x, None);
}
}
scene.size.height = (y + 45.).max(210.);
for (id, name, detail) in lanes {
let x = lane_x[&id];
scene.lines.insert(
0,
Line {
id,
points: vec![Point::new(x, 85.), Point::new(x, scene.size.height - 20.)],
label: String::new(),
label_at: Point::ORIGIN,
dashed: false,
},
);
if !scenario {
scene.lines.insert(
0,
Line {
id,
points: vec![Point::new(x, 85.), Point::new(x, scene.size.height - 20.)],
label: String::new(),
label_at: Point::ORIGIN,
dashed: false,
},
);
}
scene.boxes.push(BoxItem {
id,
rect: Rectangle::new(Point::new(x - 102., 15.), Size::new(204., 70.)),
title: name,
title: if scenario {
format!("Lane · {name}")
} else {
name
},
detail,
marker: None,
});
@@ -1319,6 +1334,16 @@ impl Scene {
let Some(SharedBody::Occurrence { body, .. }) = e.shared() else {
continue;
};
if scenario
&& matches!(body, OccurrenceBody::Exchange {
sender: Some(sender), receiver: Some(receiver),
message_kind: MessageKind::Create | MessageKind::Delete, ..
} if sender == receiver)
{
// Self lifecycle elbows and their caption need clearance from
// the preceding action/state/reference box, before recording rows.
*y += 16.;
}
if ordered {
let slot = scenario::Slot {
parent: owner,
@@ -1538,17 +1563,27 @@ impl Scene {
});
}
}
fn activation_segment(&mut self, call: Id, x: f32, top: f32, bottom: f32) {
if bottom <= top {
return;
fn activation_segment(
&mut self,
call: Id,
scope: Id,
receiver: Id,
x: f32,
top: f32,
bottom: f32,
) {
for span in self.lifecycle.alive_spans(receiver, scope, top, bottom) {
self.boxes.push(BoxItem {
id: call,
rect: Rectangle::new(
Point::new(x - 4., span.start),
Size::new(8., span.end - span.start),
),
title: String::new(),
detail: String::new(),
marker: None,
});
}
self.boxes.push(BoxItem {
id: call,
rect: Rectangle::new(Point::new(x - 4., top), Size::new(8., bottom - top)),
title: String::new(),
detail: String::new(),
marker: None,
});
}
/// Follow a single correlation through the ordered tree. Branches are drawn
/// as independent segments, never as one chronological stretch through ALT
@@ -1561,6 +1596,18 @@ impl Scene {
x: f32,
mut start: Option<f32>,
) -> bool {
let Some(SharedBody::Occurrence {
body:
OccurrenceBody::Exchange {
receiver: Some(receiver),
..
},
..
}) = p.elements[&call].shared()
else {
return false;
};
let receiver = *receiver;
let entered_open = start.is_some();
let mut split = false;
for id in p.elements[&owner].steps().into_iter().flatten() {
@@ -1574,6 +1621,21 @@ impl Scene {
continue;
};
match p.elements[id].shared() {
Some(SharedBody::Occurrence {
body:
OccurrenceBody::Exchange {
message_kind: MessageKind::Delete,
receiver: Some(deleted),
..
},
..
}) if *deleted == receiver => {
// This path cannot show activation beyond its local death.
// Keep the unresolved-call cue; a Delete is not a Reply.
self.activation_segment(call, owner, receiver, x, top, y);
self.open_calls.push(Point::new(x + 12., y - 16.));
start = None;
}
Some(SharedBody::Occurrence {
body:
OccurrenceBody::Exchange {
@@ -1583,14 +1645,19 @@ impl Scene {
},
..
}) if *correlation == call => {
self.activation_segment(call, x, top, y);
self.boxes.push(BoxItem {
id: call,
rect: Rectangle::new(Point::new(x - 7., y - 1.), Size::new(14., 2.)),
title: String::new(),
detail: String::new(),
marker: None,
});
self.activation_segment(call, owner, receiver, x, top, y);
for span in self.lifecycle.alive_spans(receiver, owner, y - 1., y + 1.) {
self.boxes.push(BoxItem {
id: call,
rect: Rectangle::new(
Point::new(x - 7., span.start),
Size::new(14., span.end - span.start),
),
title: String::new(),
detail: String::new(),
marker: None,
});
}
start = None;
}
Some(SharedBody::Occurrence {
@@ -1598,7 +1665,7 @@ impl Scene {
..
}) => {
split = true;
self.activation_segment(call, x, top, y);
self.activation_segment(call, owner, receiver, x, top, y);
let bottom = self
.boxes
.iter()
@@ -1633,7 +1700,7 @@ impl Scene {
} else {
top + 26.
};
self.activation_segment(call, x, top, bottom);
self.activation_segment(call, owner, receiver, x, top, bottom);
self.open_calls.push(Point::new(x + 8., bottom - 7.));
}
start.is_some()
@@ -1666,6 +1733,12 @@ impl Scene {
))
}
fn hit(&self, point: Point) -> Option<Id> {
if !point.x.is_finite() || !point.y.is_finite() {
return None;
}
if let Some(id) = self.lifecycle.marker_at(point) {
return Some(id);
}
self.boxes
.iter()
.rev()
@@ -1680,7 +1753,7 @@ impl Scene {
.iter()
.find(|line| {
!line.label.is_empty()
&& (Rectangle::new(
&& Rectangle::new(
line.label_at,
Size::new(
(line.label.chars().count() as f32 * 6.8 + 8.).min(225.),
@@ -1688,13 +1761,10 @@ impl Scene {
),
)
.contains(point)
|| line.points.windows(2).any(|p| {
crate::diagram::distance_to_segment(point, p[0], p[1])
< 8. / self.scale
}))
})
.map(|line| line.id)
})
.or_else(|| self.near_sequence_item(point))
.or_else(|| {
self.boxes
.iter()
@@ -1702,6 +1772,7 @@ impl Scene {
.find(|b| b.rect.contains(point) && b.title.is_empty())
.map(|b| b.id)
})
.or_else(|| self.lifecycle.lane_at(point, self.scale))
.or_else(|| self.lane_at(point))
.or_else(|| {
self.lines
@@ -1716,6 +1787,34 @@ impl Scene {
.map(|line| line.id)
})
}
fn near_sequence_item(&self, point: Point) -> Option<Id> {
if !self.scale.is_finite() || self.scale <= 0. {
return None;
}
let messages = self
.lines
.iter()
.filter(|line| !line.label.is_empty())
.map(|line| {
let distance = line
.points
.windows(2)
.map(|p| crate::diagram::distance_to_segment(point, p[0], p[1]))
.fold(f32::INFINITY, f32::min);
(line.id, distance)
});
// One distance comparison for both kinds of screen-sized hit tolerance.
// Dense minimum-Fit rows must not give the first message or last marker
// a larger hit priority than the visibly nearer item. UUID breaks ties.
self.lifecycle
.nearest_marker(point)
.into_iter()
.chain(messages)
.filter(|(_, distance)| *distance <= 8. / self.scale)
.min_by(|a, b| a.1.total_cmp(&b.1).then_with(|| a.0.cmp(&b.0)))
.map(|(id, _)| id)
}
}
impl canvas::Program<Message> for Scene {
type State = SequenceInput;
@@ -1956,6 +2055,7 @@ impl canvas::Program<Message> for Scene {
let mut f = Frame::new(renderer, bounds.size());
f.fill_rectangle(Point::ORIGIN, bounds.size(), Color::WHITE);
f.scale(self.scale);
self.lifecycle.draw_lanes(&mut f, self.selected);
for b in &self.boxes {
let outline = matches!(
b.marker,
@@ -2064,6 +2164,7 @@ impl canvas::Program<Message> for Scene {
..Default::default()
});
}
self.lifecycle.draw_markers(&mut f, self.selected);
if let Some((context, tool, editable)) = self.interaction {
let active = editable && state.context == Some(context) && state.tool == tool;
let point = cursor
@@ -2,6 +2,8 @@
use oa_core::{FragmentOperator, Id, Kind, MessageKind, OccurrenceBody, Project, SharedBody};
use std::collections::{BTreeMap, BTreeSet};
pub(super) mod render;
/// Maps already recorded by the sequence renderer. Rows carry their owning sequence.
pub(crate) struct Geometry<'a> {
pub rows: &'a BTreeMap<Id, (Id, f32)>,
@@ -0,0 +1,359 @@
//! Scene bridge for local lifetime geometry; no authoring or model mutations.
use super::{Geometry, LaneLifetime, Life, Marker, MarkerKind, Span, project_lifetimes};
use crate::{behavior::Line, style, views::short};
use iced::{
Color, Point, Rectangle, Size,
widget::canvas::{self, Frame, Path, Stroke, Text},
};
use oa_core::{Id, OccurrenceBody, Project, SharedBody};
use std::collections::BTreeMap;
#[derive(Debug, Clone, PartialEq)]
struct EventMarker {
event: Marker,
lifeline: Id,
center: Point,
capsule: Rectangle,
name: String,
sender_x: Option<f32>,
}
#[derive(Debug, Default, Clone, PartialEq)]
pub(in crate::behavior) struct Rendering {
// Complete rendering positions, including warning-admitted unassigned lanes.
// Scene.lanes independently retains the existing authoring eligibility rule.
lanes: BTreeMap<Id, f32>,
span: Option<Span>,
lifetimes: Vec<LaneLifetime>,
markers: Vec<EventMarker>,
}
impl Rendering {
pub(in crate::behavior) fn new(
project: &Project,
owner: Id,
lanes: &BTreeMap<Id, f32>,
rows: &BTreeMap<Id, (Id, f32)>,
insertion_scopes: &BTreeMap<Id, (f32, f32)>,
height: f32,
) -> Self {
let span = Span {
start: 85.,
end: height - 20.,
};
if !span.valid() {
return Self::default();
}
// Insertion scopes deliberately start later and end earlier. Empty and
// Create-only scenarios still have the full visible lifetime extent.
let mut scopes = insertion_scopes.clone();
for (scope, (start, end)) in &mut scopes {
if *scope != owner && *start == *end && start.is_finite() {
// An empty operand has 42 px between its insertion axis and
// frame end. Match the existing 26 px open-activation allowance.
*end = *start + 26.;
}
}
scopes.insert(owner, (span.start, span.end));
let lifetimes = project_lifetimes(
project,
owner,
&Geometry {
rows,
scopes: &scopes,
},
);
let lanes: BTreeMap<_, _> = lanes
.iter()
.filter(|(_, x)| x.is_finite())
.map(|(id, x)| (*id, *x))
.collect();
let mut markers = Vec::new();
for lane in &lifetimes {
let Some(&x) = lanes.get(&lane.lifeline) else {
continue;
};
for event in &lane.markers {
let sender_x = project
.elements
.get(&event.occurrence)
.and_then(|e| e.shared())
.and_then(|body| match body {
SharedBody::Occurrence {
body:
OccurrenceBody::Exchange {
sender: Some(sender),
..
},
..
} => lanes.get(sender).copied(),
_ => None,
});
markers.push(EventMarker {
event: *event,
lifeline: lane.lifeline,
center: Point::new(x, event.y),
capsule: Rectangle::new(
Point::new(x - 70., event.y - 14.),
Size::new(140., 28.),
),
name: project.named(lane.lifeline).to_owned(),
sender_x,
});
}
}
Self {
lanes,
span: Some(span),
lifetimes,
markers,
}
}
pub(in crate::behavior) fn align_messages(&self, lines: &mut [Line]) {
for marker in &self.markers {
let Some(sender) = marker.sender_x else {
continue;
};
let Some(line) = lines
.iter_mut()
.find(|line| line.id == marker.event.occurrence)
else {
continue;
};
let x = marker.center.x;
let y = marker.center.y;
let self_message = sender == x;
let end_x = match marker.event.kind {
MarkerKind::Birth if sender < x => marker.capsule.x,
MarkerKind::Birth => marker.capsule.x + marker.capsule.width,
MarkerKind::Death => x,
};
line.points = if self_message {
// The receiver is on the recorded occurrence row, including self
// lifecycle events. The elbow stays outside the birth capsule.
let elbow = x + 112.;
line.label_at = Point::new(x + 12., y - 52.);
vec![
Point::new(x, y - 24.),
Point::new(elbow, y - 24.),
Point::new(elbow, y),
Point::new(end_x, y),
]
} else {
if marker.event.kind == MarkerKind::Birth {
// Keep the label above the capsule, including right-to-left
// messages whose existing label starts beside the receiver.
line.label_at.y = y - 40.;
}
vec![Point::new(sender, y), Point::new(end_x, y)]
};
}
}
pub(in crate::behavior) fn alive_spans(
&self,
lifeline: Id,
scope: Id,
top: f32,
bottom: f32,
) -> Vec<Span> {
if !top.is_finite() || !bottom.is_finite() || bottom <= top {
return vec![];
}
self.lifetimes
.iter()
.find(|lane| lane.lifeline == lifeline)
.into_iter()
.flat_map(|lane| &lane.intervals)
.filter(|interval| interval.scope == scope && interval.life == Life::Alive)
.filter_map(|interval| {
let start = top.max(interval.span.start);
let end = bottom.min(interval.span.end);
(start < end).then_some(Span { start, end })
})
.collect()
}
pub(in crate::behavior) fn marker_at(&self, point: Point) -> Option<Id> {
self.markers
.iter()
.rev()
.find(|marker| match marker.event.kind {
MarkerKind::Birth => marker.capsule.contains(point),
MarkerKind::Death => marker_distance(marker, point) <= 1.25,
})
.map(|marker| marker.event.occurrence)
}
pub(in crate::behavior) fn nearest_marker(&self, point: Point) -> Option<(Id, f32)> {
if !point.x.is_finite() || !point.y.is_finite() {
return None;
}
// Exact visible shapes are tested first by Scene. A low-zoom tolerance
// must not outrank another marker's actual capsule, a header, or a label.
self.markers
.iter()
.map(|marker| (marker.event.occurrence, marker_distance(marker, point)))
.min_by(|a, b| a.1.total_cmp(&b.1).then_with(|| a.0.cmp(&b.0)))
}
pub(in crate::behavior) fn lane_at(&self, point: Point, scale: f32) -> Option<Id> {
let span = self.span?;
if !scale.is_finite() || scale <= 0. || point.y < span.start || point.y > span.end {
return None;
}
self.lanes
.iter()
.filter(|(_, x)| (**x - point.x).abs() < 12. / scale)
.min_by(|a, b| (a.1 - point.x).abs().total_cmp(&(b.1 - point.x).abs()))
.map(|(id, _)| *id)
}
pub(in crate::behavior) fn draw_lanes(&self, frame: &mut Frame, selected: Id) {
let Some(span) = self.span else {
return;
};
for (&id, &x) in &self.lanes {
frame.stroke(
&Path::line(Point::new(x, span.start), Point::new(x, span.end)),
Stroke {
line_dash: canvas::LineDash {
segments: &[2., 5.],
offset: 0.,
},
..Stroke::default()
.with_width(1.)
.with_color(if id == selected {
Color::from_rgb8(148, 190, 187)
} else {
Color::from_rgb8(214, 222, 222)
})
},
);
}
for lifetime in &self.lifetimes {
let Some(&x) = self.lanes.get(&lifetime.lifeline) else {
continue;
};
for interval in &lifetime.intervals {
match interval.life {
Life::Alive => frame.stroke(
&Path::line(
Point::new(x, interval.span.start),
Point::new(x, interval.span.end),
),
Stroke::default().with_width(2.).with_color(
if lifetime.lifeline == selected {
style::TEAL
} else {
Color::from_rgb8(91, 113, 113)
},
),
),
Life::Unresolved if interval.span.end - interval.span.start >= 16. => {
frame.fill_text(Text {
content: "?".into(),
position: Point::new(x + 9., interval.span.start + 2.),
size: 11.into(),
color: style::MUTED,
..Default::default()
});
}
_ => {}
}
}
}
if !self.lanes.is_empty() {
frame.fill_text(Text {
content:
"Dotted: editing guide · Solid: local lifetime · ?: lifetime depends on path"
.into(),
position: Point::new(28., span.end + 4.),
size: 10.into(),
color: style::MUTED,
..Default::default()
});
}
}
pub(in crate::behavior) fn draw_markers(&self, frame: &mut Frame, selected: Id) {
for marker in &self.markers {
let color = if selected == marker.event.occurrence || selected == marker.lifeline {
style::TEAL
} else {
style::INK
};
match marker.event.kind {
MarkerKind::Birth => {
let path = Path::rounded_rectangle(
marker.capsule.position(),
marker.capsule.size(),
6.into(),
);
frame.fill(&path, Color::from_rgb8(243, 248, 248));
frame.stroke(
&path,
Stroke::default().with_color(color).with_width(
if selected == marker.event.occurrence {
2.5
} else {
1.5
},
),
);
frame.fill_text(Text {
content: short(&marker.name, 18),
position: marker.capsule.position() + iced::Vector::new(8., 6.),
size: 12.into(),
color,
..Default::default()
});
}
MarkerKind::Death => {
for (a, b) in death_strokes(marker.center) {
frame.stroke(
&Path::line(a, b),
Stroke::default().with_color(color).with_width(2.5),
);
}
}
}
}
}
}
fn marker_distance(marker: &EventMarker, point: Point) -> f32 {
match marker.event.kind {
MarkerKind::Birth => {
let dx = (marker.capsule.x - point.x)
.max(point.x - marker.capsule.x - marker.capsule.width)
.max(0.);
let dy = (marker.capsule.y - point.y)
.max(point.y - marker.capsule.y - marker.capsule.height)
.max(0.);
dx.hypot(dy)
}
MarkerKind::Death => death_strokes(marker.center)
.iter()
.map(|(a, b)| crate::diagram::distance_to_segment(point, *a, *b))
.fold(f32::INFINITY, f32::min),
}
}
fn death_strokes(center: Point) -> [(Point, Point); 2] {
[
(
center + iced::Vector::new(-7., -7.),
center + iced::Vector::new(7., 7.),
),
(
center + iced::Vector::new(-7., 7.),
center + iced::Vector::new(7., -7.),
),
]
}
#[cfg(test)]
#[path = "render_tests.rs"]
mod tests;
@@ -0,0 +1,624 @@
use super::*;
use crate::{behavior::Scene, document::Document};
use oa_core::*;
struct Fixture {
project: Project,
owner: Id,
sender: Id,
receiver: Id,
work: Id,
exchange: Id,
}
impl Fixture {
fn new(layer: Layer) -> Self {
let mut project = Project::empty();
let kind = if layer == Layer::Oa {
Kind::Activity
} else {
Kind::Function
};
let a = project.add_element(kind, layer, "Request", None).unwrap();
let work = project.add_element(kind, layer, "Provide", None).unwrap();
let exchange = project.relate(
if layer == Layer::Oa {
RelationKind::Interaction
} else {
RelationKind::FunctionalExchange
},
"Service",
a,
work,
);
let owner = project
.add_element(Kind::Scenario, layer, "Lifecycle", None)
.unwrap();
let mut lane = |name, represented| {
let id = project
.add_element(Kind::Lifeline, layer, name, Some(owner))
.unwrap();
*project.elements.get_mut(&id).unwrap().shared_mut().unwrap() = SharedBody::Lifeline {
represented: Some(represented),
};
id
};
let sender = lane("Requester", a);
let receiver = lane("Provider", work);
Self {
project,
owner,
sender,
receiver,
work,
exchange,
}
}
fn occurrence(&mut self, parent: Id, name: &str, body: OccurrenceBody) -> Id {
let mut element = self
.project
.make_element(
Kind::Occurrence,
self.project.elements[&parent].layer(),
name,
Some(parent),
)
.unwrap();
let id = element.id;
*element.shared_mut().unwrap() = SharedBody::Occurrence {
body,
precondition: String::new(),
postcondition: String::new(),
};
self.project.elements.insert(id, element);
let parent = self.project.elements.get_mut(&parent).unwrap();
if let Some(steps) = parent.steps_mut() {
steps.push(id);
} else if let Some(SharedBody::Occurrence {
body: OccurrenceBody::Fragment { branches, .. },
..
}) = parent.shared_mut()
{
branches.push(id);
} else {
panic!("fixture parent must own ordered occurrences or operands");
}
id
}
fn message(&mut self, parent: Id, kind: MessageKind) -> Id {
self.occurrence(
parent,
"Message",
OccurrenceBody::Exchange {
exchange: Some(self.exchange),
sender: Some(self.sender),
receiver: Some(self.receiver),
message_kind: kind,
operation: None,
reply_to: None,
values: vec![],
},
)
}
fn fragment(&mut self, parent: Id, operator: FragmentOperator) -> Id {
self.occurrence(
parent,
"Paths",
OccurrenceBody::Fragment {
operator,
branches: vec![],
},
)
}
fn branch(&mut self, fragment: Id) -> Id {
self.occurrence(
fragment,
"Operand",
OccurrenceBody::Branch {
guard: "condition".into(),
occurrences: vec![],
},
)
}
fn document(&self) -> Document {
validate_candidate(&self.project).expect("ordinary core admission must succeed");
let mut bundle = source_project::format_bundle(&self.project);
for text in bundle.files.values_mut() {
*text = format!(
"// Retain exact authored CRLF and Unicode Ω\r\n{}",
text.replace('\n', "\r\n")
);
}
let document =
Document::from_bundle(bundle.clone(), source_project::ProjectLimits::default())
.unwrap();
assert_eq!(document.project(), &self.project);
assert_eq!(document.bundle(), &bundle);
document
}
fn scene(&self) -> Scene {
let document = self.document();
let before = document.snapshot();
let scene = Scene::sequence(document.project(), self.owner, self.owner);
assert_eq!(document.project(), &before.decoded.project);
assert_eq!(document.bundle(), &before.bundle);
assert_eq!(document.revision(), 0);
assert!(!document.can_undo() && !document.can_redo() && !document.dirty());
assert_eq!(
scene.lifecycle,
Scene::sequence(document.project(), self.owner, self.owner).lifecycle
);
assert!(
scene
.lines
.iter()
.flat_map(|line| &line.points)
.all(|p| p.x.is_finite() && p.y.is_finite())
);
scene
}
}
fn intervals(scene: &Scene, lifeline: Id, scope: Id) -> Vec<(f32, f32, Life)> {
scene
.lifecycle
.lifetimes
.iter()
.find(|lane| lane.lifeline == lifeline)
.unwrap()
.intervals
.iter()
.filter(|i| i.scope == scope)
.map(|i| (i.span.start, i.span.end, i.life))
.collect()
}
#[test]
fn real_scene_empty_and_create_only_use_visible_root_extents_not_insertion_bounds() {
for layer in [Layer::Oa, Layer::Sa] {
let mut f = Fixture::new(layer);
let empty = f.scene();
assert_eq!(empty.sequence_bounds[&f.owner], (106., 106.));
assert_eq!(
intervals(&empty, f.receiver, f.owner),
vec![(85., empty.size.height - 20., Life::Alive)]
);
assert!(empty.lines.is_empty(), "lane guides are not message lines");
let create = f.message(f.owner, MessageKind::Create);
let scene = f.scene();
let birth = scene.rows[&create].1;
assert_eq!(birth, 148.);
assert_eq!(scene.sequence_bounds[&f.owner], (106., 198.));
assert_eq!(
intervals(&scene, f.receiver, f.owner),
vec![
(85., birth, Life::Absent),
(birth, scene.size.height - 20., Life::Alive),
]
);
assert_eq!(
intervals(&scene, f.sender, f.owner),
vec![(85., scene.size.height - 20., Life::Alive)]
);
assert_eq!(scene.slots[0].y, 106.);
assert_eq!(
scene.lane_at(Point::new(scene.lanes[&f.receiver], 90.)),
Some(f.receiver)
);
}
}
#[test]
fn real_scene_birth_death_markers_align_endpoints_and_win_selection_without_mutating_source() {
for layer in [Layer::Oa, Layer::Sa] {
let mut f = Fixture::new(layer);
let create = f.message(f.owner, MessageKind::Create);
f.occurrence(
f.owner,
"Work",
OccurrenceBody::Action {
action: Some(f.work),
lifeline: Some(f.receiver),
},
);
let delete = f.message(f.owner, MessageKind::Delete);
let mut scene = f.scene();
let birth = scene.rows[&create].1;
let death = scene.rows[&delete].1;
assert_eq!(
intervals(&scene, f.receiver, f.owner),
vec![
(85., birth, Life::Absent),
(birth, death, Life::Alive),
(death, scene.size.height - 20., Life::Absent),
]
);
let capsule = scene
.lifecycle
.markers
.iter()
.find(|m| m.event.occurrence == create)
.unwrap()
.capsule;
let line = scene.lines.iter().find(|l| l.id == create).unwrap();
let expected_x = if scene.lanes[&f.sender] < scene.lanes[&f.receiver] {
capsule.x
} else {
capsule.x + capsule.width
};
assert_eq!(line.points.last(), Some(&Point::new(expected_x, birth)));
assert!(
line.label_at.y + 22. < capsule.y,
"message caption clears birth capsule"
);
let line = scene.lines.iter().find(|l| l.id == delete).unwrap();
assert_eq!(
line.points.last(),
Some(&Point::new(scene.lanes[&f.receiver], death))
);
for scale in [0.5, 1., 2.] {
scene.scale = scale;
assert_eq!(
scene.hit(Point::new(scene.lanes[&f.receiver], birth)),
Some(create)
);
assert_eq!(
scene.hit(Point::new(scene.lanes[&f.receiver], death)),
Some(delete)
);
assert_eq!(
scene.hit(Point::new(
scene.lanes[&f.receiver],
scene.size.height - 25.
)),
Some(f.receiver)
);
assert_eq!(
scene.hit(Point::new(scene.lanes[&f.receiver], 40.)),
Some(f.receiver)
);
}
}
}
#[test]
fn real_scene_branch_delete_clips_only_its_activation_and_keeps_unanswered_call_visible() {
for layer in [Layer::Oa, Layer::Sa] {
let mut f = Fixture::new(layer);
let call = f.message(f.owner, MessageKind::Call);
let fragment = f.fragment(f.owner, FragmentOperator::Alternative);
let left = f.branch(fragment);
let delete = f.message(left, MessageKind::Delete);
let right = f.branch(fragment);
let scene = f.scene();
let death = scene.rows[&delete].1;
let (left_start, left_end) = scene.sequence_bounds[&left];
let (right_start, _) = scene.sequence_bounds[&right];
let bars: Vec<_> = scene
.boxes
.iter()
.filter(|b| b.id == call && b.rect.width == 8.)
.map(|b| b.rect)
.collect();
assert!(
bars.iter()
.any(|r| r.y == left_start && r.y + r.height == death)
);
assert!(
!bars
.iter()
.any(|r| r.y < left_end && r.y + r.height > death && r.y >= left_start)
);
assert!(
bars.iter().any(|r| r.y == right_start && r.height == 26.),
"empty exclusive sibling retains its own open activation"
);
assert_eq!(
intervals(&scene, f.receiver, right),
vec![(right_start, right_start + 26., Life::Alive)]
);
let frame_end = scene.boxes.iter().find(|b| b.id == fragment).unwrap().rect;
assert!(
!bars.iter().any(|r| r.y >= frame_end.y + frame_end.height),
"uncertain post-fragment lifetime cannot carry a definite activation bar"
);
assert!(
scene
.open_calls
.iter()
.any(|p| p.y < death && p.y > left_start)
);
assert!(scene.open_calls.iter().any(|p| p.y > right_start));
assert_eq!(
intervals(&scene, f.receiver, left),
vec![
(left_start, death, Life::Alive),
(death, left_end, Life::Absent)
]
);
assert_eq!(
intervals(&scene, f.receiver, f.owner).last().unwrap().2,
Life::Unresolved
);
assert!(!f.project.elements.values().any(|e| matches!(
e.shared(),
Some(SharedBody::Occurrence {
body: OccurrenceBody::Exchange {
message_kind: MessageKind::Reply,
..
},
..
})
)));
}
}
#[test]
fn real_scene_unassigned_lanes_and_explicit_incomplete_receiver_keep_real_marker_identity() {
for layer in [Layer::Oa, Layer::Sa] {
let mut f = Fixture::new(layer);
*f.project
.elements
.get_mut(&f.receiver)
.unwrap()
.shared_mut()
.unwrap() = SharedBody::Lifeline { represented: None };
let create = f.message(f.owner, MessageKind::Create);
if let Some(SharedBody::Occurrence {
body: OccurrenceBody::Exchange { exchange, .. },
..
}) = f.project.elements.get_mut(&create).unwrap().shared_mut()
{
*exchange = None;
}
let scene = f.scene();
assert!(
!scene.lanes.contains_key(&f.receiver),
"authoring eligibility remains separate"
);
let x = scene.lifecycle.lanes[&f.receiver];
assert_eq!(scene.lifecycle.markers[0].lifeline, f.receiver);
assert_eq!(
scene.hit(Point::new(x, scene.rows[&create].1)),
Some(create)
);
assert_eq!(scene.hit(Point::new(x, 40.)), Some(f.receiver));
assert_eq!(
scene.hit(Point::new(x, scene.size.height - 25.)),
Some(f.receiver)
);
assert_eq!(scene.lane_at(Point::new(x, 90.)), None);
if let Some(SharedBody::Occurrence {
body: OccurrenceBody::Exchange {
receiver, exchange, ..
},
..
}) = f.project.elements.get_mut(&create).unwrap().shared_mut()
{
*receiver = None;
*exchange = Some(f.exchange);
}
let missing = f.scene();
assert!(
missing.lifecycle.markers.is_empty(),
"do not infer receiver from global exchange"
);
}
}
#[test]
fn real_scene_minimum_zoom_marker_halos_do_not_steal_adjacent_capsule_or_header_hits() {
let mut f = Fixture::new(Layer::Sa);
*f.project
.elements
.get_mut(&f.receiver)
.unwrap()
.shared_mut()
.unwrap() = SharedBody::Lifeline { represented: None };
let create = f.message(f.owner, MessageKind::Create);
let delete = f.message(f.owner, MessageKind::Delete);
for event in [create, delete] {
if let Some(SharedBody::Occurrence {
body: OccurrenceBody::Exchange { exchange, .. },
..
}) = f.project.elements.get_mut(&event).unwrap().shared_mut()
{
*exchange = None;
}
}
let mut scene = f.scene();
let x = scene.lifecycle.lanes[&f.receiver];
assert_eq!(scene.rows[&delete].1 - scene.rows[&create].1, 92.);
for scale in [0.05, 0.1, 0.5, 1., 2.] {
scene.scale = scale;
assert_eq!(
scene.hit(Point::new(x, scene.rows[&create].1)),
Some(create)
);
assert_eq!(
scene.hit(Point::new(x, scene.rows[&delete].1)),
Some(delete)
);
assert_eq!(scene.hit(Point::new(x, 40.)), Some(f.receiver));
if scale <= 0.1 {
for offset in [-25., -15., 15., 25.] {
assert_eq!(
scene.hit(Point::new(x, scene.rows[&delete].1 + offset)),
Some(delete),
"a nearby death halo must beat an earlier message's wider tolerance"
);
}
}
}
}
#[test]
fn real_scene_minimum_zoom_near_ordinary_message_wins_over_lifecycle_halos() {
let mut f = Fixture::new(Layer::Sa);
f.message(f.owner, MessageKind::Create);
let ordinary = f.message(f.owner, MessageKind::Asynchronous);
f.message(f.owner, MessageKind::Delete);
let mut scene = f.scene();
let x = (scene.lanes[&f.sender] + scene.lanes[&f.receiver]) / 2.;
for scale in [0.05, 0.1] {
scene.scale = scale;
assert_eq!(
scene.hit(Point::new(x, scene.rows[&ordinary].1 + 2.)),
Some(ordinary)
);
}
}
#[test]
fn real_scene_self_lifecycle_elbows_end_on_recorded_marker_rows() {
for layer in [Layer::Oa, Layer::Sa] {
for kind in [MessageKind::Create, MessageKind::Delete] {
let mut f = Fixture::new(layer);
let event = f.message(f.owner, kind);
if let Some(SharedBody::Occurrence {
body:
OccurrenceBody::Exchange {
exchange, sender, ..
},
..
}) = f.project.elements.get_mut(&event).unwrap().shared_mut()
{
*exchange = None;
*sender = Some(f.receiver);
}
let scene = f.scene();
let marker = &scene.lifecycle.markers[0];
let line = scene.lines.iter().find(|l| l.id == event).unwrap();
assert_eq!(line.points.len(), 4);
assert_eq!(line.points[0].y, marker.center.y - 24.);
assert_eq!(line.points[3].y, marker.center.y);
assert_eq!(
line.points[3].x,
if marker.event.kind == MarkerKind::Birth {
marker.capsule.x + marker.capsule.width
} else {
marker.center.x
}
);
assert!(line.points[1].x > marker.capsule.x + marker.capsule.width);
assert_eq!(scene.hit(marker.center), Some(event));
}
}
}
#[test]
fn real_scene_self_delete_caption_clears_the_preceding_action_box() {
let mut f = Fixture::new(Layer::Sa);
let action = f.occurrence(
f.owner,
"Finish work",
OccurrenceBody::Action {
action: Some(f.work),
lifeline: Some(f.receiver),
},
);
let delete = f.message(f.owner, MessageKind::Delete);
if let Some(SharedBody::Occurrence {
body: OccurrenceBody::Exchange {
exchange, sender, ..
},
..
}) = f.project.elements.get_mut(&delete).unwrap().shared_mut()
{
*exchange = None;
*sender = Some(f.receiver);
}
let scene = f.scene();
let action = scene.boxes.iter().find(|b| b.id == action).unwrap();
let line = scene.lines.iter().find(|l| l.id == delete).unwrap();
assert!(line.label_at.y > action.rect.y + action.rect.height);
assert!(line.label_at.y + 22. < line.points[0].y);
assert_eq!(line.points[3].y, scene.rows[&delete].1);
assert_eq!(
scene
.slots
.iter()
.find(|s| s.slot.before == Some(delete))
.unwrap()
.y,
scene.rows[&delete].1 - 42.
);
}
#[test]
fn real_scene_conditional_repeated_and_parallel_scopes_do_not_claim_definite_continuation() {
for operator in [
FragmentOperator::Alternative,
FragmentOperator::Optional,
FragmentOperator::Loop,
FragmentOperator::Parallel,
] {
let mut f = Fixture::new(Layer::Sa);
let fragment = f.fragment(f.owner, operator);
let first = f.branch(fragment);
let create = f.message(first, MessageKind::Create);
let scene = f.scene();
assert_eq!(scene.lifecycle.markers[0].event.occurrence, create);
assert_eq!(
intervals(&scene, f.receiver, f.owner).first().unwrap().2,
Life::Unresolved
);
assert_eq!(
intervals(&scene, f.receiver, f.owner).last().unwrap().2,
Life::Unresolved
);
assert!(
scene
.lifecycle
.lifetimes
.iter()
.find(|l| l.lifeline == f.sender)
.unwrap()
.intervals
.iter()
.all(|i| i.life == Life::Alive)
);
}
}
#[test]
fn real_scene_lifecycle_edit_undo_redo_restores_exact_source_and_changed_geometry() {
for layer in [Layer::Oa, Layer::Sa] {
let mut f = Fixture::new(layer);
let create = f.message(f.owner, MessageKind::Create);
let mut document = f.document();
let before = document.snapshot();
let geometry = Scene::sequence(document.project(), f.owner, create).lifecycle;
let mut edited = document.project().elements[&create].clone();
if let Some(SharedBody::Occurrence {
body: OccurrenceBody::Exchange { message_kind, .. },
..
}) = edited.shared_mut()
{
*message_kind = MessageKind::Asynchronous;
}
document
.execute(Command::EditElement(create, edited))
.unwrap();
let after = document.snapshot();
let changed = Scene::sequence(document.project(), f.owner, create).lifecycle;
assert_ne!(geometry, changed);
assert!(changed.markers.is_empty());
assert_ne!(before.bundle, after.bundle);
assert!(document.undo());
assert_eq!(document.project(), &before.decoded.project);
assert_eq!(document.bundle(), &before.bundle);
assert_eq!(
Scene::sequence(document.project(), f.owner, create).lifecycle,
geometry
);
assert!(!document.can_undo() && document.can_redo());
assert!(document.redo());
assert_eq!(document.project(), &after.decoded.project);
assert_eq!(document.bundle(), &after.bundle);
assert_eq!(
Scene::sequence(document.project(), f.owner, create).lifecycle,
changed
);
assert!(!document.can_redo());
}
}
+9 -1
View File
@@ -484,7 +484,7 @@ fn article(topic: Topic) -> Article {
const {
Article {
summary: "Use a scenario to tell a concrete behavioral story, including repeated messages, actions, state observations and guarded alternatives.",
keywords: "scenario sequence lifeline sender receiver fragment branch alternative loop guard parallel strict optional occurrence exchange action state interface operation allocation call reply unresolved",
keywords: "scenario sequence lifeline lifetime lifecycle birth death capsule sender receiver fragment branch alternative loop guard parallel strict optional occurrence exchange action state interface operation allocation call reply unresolved",
sections: &[
walkthrough(
"Create a scenario",
@@ -506,6 +506,14 @@ fn article(topic: Topic) -> Article {
"Inspect the scenario projection and Model checks. State exactly what each guard means, including units and assumptions where relevant.",
],
),
explain(
"Read lifeline creation and deletion",
"The Lane header and dotted guide identify a lifeline; the guide alone does not establish its lifetime. The stronger solid line shows where the lifeline is known to exist on this scenario path. Create starts that line at a named capsule; Delete ends it at an X. Select the capsule or X to edit that message occurrence. Select the header or guide to inspect the lifeline.",
),
explain(
"Distinguish uncertain lifetime from an unanswered call",
"A ? means lifetime depends on an alternative, optional, repeated or parallel path. Operands have independent lifetimes: Delete in one alternative does not delete a lifeline in another. Beyond such a fragment, the guide can continue without a definite solid line. This uncertainty is not by itself a model error. The … beside an activation marks a Call that remains unanswered on that path. A reused scenario's internal Create/Delete events appear when you choose Open definition; they do not create or delete the caller's lifelines.",
),
walkthrough(
"Invoke an interface operation",
&[