Skip to content

Commit 959453f

Browse files
committed
Update smcat rendering to latest runtime interface.
1 parent 3d8c091 commit 959453f

File tree

3 files changed

+134
-66
lines changed

3 files changed

+134
-66
lines changed

frame_runtime/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ pub mod event;
6464
pub mod history;
6565
pub mod info;
6666
pub mod live;
67+
pub mod smcat;
6768
pub mod transition;
68-
// pub mod smcat;
6969

7070
/// The synchronized/thread-safe flavor of the runtime interface. Include this module if you
7171
/// compiled your Frame spec with `thread_safe = true`. Including this module will include the
@@ -76,6 +76,7 @@ pub mod sync {
7676
pub use crate::history::*;
7777
pub use crate::info::*;
7878
pub use crate::live::sync::*;
79+
pub use crate::smcat::sync::*;
7980
pub use crate::transition::sync::*;
8081
}
8182

@@ -88,5 +89,6 @@ pub mod unsync {
8889
pub use crate::history::*;
8990
pub use crate::info::*;
9091
pub use crate::live::unsync::*;
92+
pub use crate::smcat::unsync::*;
9193
pub use crate::transition::unsync::*;
9294
}

frame_runtime/src/smcat.rs

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
//! compatibility with new types that may be added.
77
88
use crate::info::*;
9-
use crate::live::*;
109
use std::fmt;
1110

1211
/// Style options for smcat states.
@@ -211,17 +210,7 @@ impl Renderer {
211210
self.render_common(machine_info, None, None)
212211
}
213212

214-
/// Generate an smcat diagram from a snapshot of a running state machine. Depending on the
215-
/// style configuration, this can be expected to highlight the running state, most recent
216-
/// transition, etc. Eventually, it may show the current values of variables.
217-
pub fn render_live(&self, machine: &dyn MachineInstance) -> String {
218-
let machine_info = machine.info();
219-
let active_state = machine.state().info().name;
220-
let last_transition = machine.event_monitor().last_transition().map(|t| t.info.id);
221-
self.render_common(machine_info, Some(active_state), last_transition)
222-
}
223-
224-
fn render_common(
213+
pub fn render_common(
225214
&self,
226215
machine_info: &MachineInfo,
227216
active_state: Option<&'static str>,
@@ -291,3 +280,57 @@ impl Renderer {
291280
));
292281
}
293282
}
283+
284+
/// Definitions specific to the synchronized/thread-safe interface.
285+
pub mod sync {
286+
pub use super::*;
287+
use crate::event::sync::*;
288+
use crate::live::sync::*;
289+
290+
impl Renderer {
291+
/// Generate an smcat diagram from a snapshot of a running state machine implementing the
292+
/// synchronized runtime interface. Depending on the style configuration, this can be
293+
/// expected to highlight the running state, most recent transition, etc. Eventually, it
294+
/// may show the current values of variables.
295+
pub fn render_live_sync<'a>(
296+
&self,
297+
machine: &dyn Machine<StatePtr, EventMonitor<'a>>,
298+
) -> String {
299+
let machine_info = machine.info();
300+
let active_state = machine.state().info().name;
301+
let last_transition = machine
302+
.event_monitor()
303+
.transition_history()
304+
.newest()
305+
.map(|t| t.info.id);
306+
self.render_common(machine_info, Some(active_state), last_transition)
307+
}
308+
}
309+
}
310+
311+
/// Definitions specific to the unsynchronized interface.
312+
pub mod unsync {
313+
pub use super::*;
314+
use crate::event::unsync::*;
315+
use crate::live::unsync::*;
316+
317+
impl Renderer {
318+
/// Generate an smcat diagram from a snapshot of a running state machine implementing the
319+
/// unsynchronized runtime interface. Depending on the style configuration, this can be
320+
/// expected to highlight the running state, most recent transition, etc. Eventually, it
321+
/// may show the current values of variables.
322+
pub fn render_live_unsync<'a>(
323+
&self,
324+
machine: &dyn Machine<StatePtr, EventMonitor<'a>>,
325+
) -> String {
326+
let machine_info = machine.info();
327+
let active_state = machine.state().info().name;
328+
let last_transition = machine
329+
.event_monitor()
330+
.transition_history()
331+
.newest()
332+
.map(|t| t.info.id);
333+
self.render_common(machine_info, Some(active_state), last_transition)
334+
}
335+
}
336+
}

frame_runtime/tests/demo.rs

Lines changed: 76 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -521,57 +521,80 @@ mod tests {
521521
assert!(!outgoing[0].is_change_state());
522522
}
523523

524-
// use indoc::indoc;
525-
// const SMCAT_STATIC: &str = indoc! {r#"
526-
// initial,
527-
// Init,
528-
// Foo,
529-
// Bar;
530-
// initial => Init;
531-
// Init -> Foo : " Init:> ";
532-
// Foo -> Bar : " next "; Bar -> Foo [color="grey"] : " next ";
533-
// "#};
534-
// const SMCAT_LIVE_1: &str = indoc! {r#"
535-
// initial,
536-
// Init,
537-
// Foo [active color="red"],
538-
// Bar;
539-
// initial => Init;
540-
// Init -> Foo [color="red" width=2] : " Init:> ";
541-
// Foo -> Bar : " next ";
542-
// Bar -> Foo [color="grey"] : " next ";
543-
// "#};
544-
// const SMCAT_LIVE_2: &str = indoc! {r#"
545-
// initial,
546-
// Init,
547-
// Foo,
548-
// Bar [active color="red"];
549-
// initial => Init;
550-
// Init -> Foo : " Init:> ";
551-
// Foo -> Bar [color="red" width=2] : " next ";
552-
// Bar -> Foo [color="grey"] : " next ";
553-
// "#};
554-
// const SMCAT_LIVE_3: &str = indoc! {r#"
555-
// initial,
556-
// Init,
557-
// Foo [active color="red"],
558-
// Bar;
559-
// initial => Init;
560-
// Init -> Foo : " Init:> ";
561-
// Foo -> Bar : " next ";
562-
// Bar -> Foo [color="pink" width=2] : " next ";
563-
// "#};
564-
//
565-
// #[test]
566-
// fn smcat_renderer() {
567-
// let smcat = smcat::Renderer::new(Box::new(smcat::SimpleStyle));
568-
// assert_eq!(smcat.render_static(info::machine()), SMCAT_STATIC);
569-
//
570-
// let mut sm = unsync::Demo::new();
571-
// assert_eq!(smcat.render_live(&sm), SMCAT_LIVE_1);
572-
// sm.next();
573-
// assert_eq!(smcat.render_live(&sm), SMCAT_LIVE_2);
574-
// sm.next();
575-
// assert_eq!(smcat.render_live(&sm), SMCAT_LIVE_3);
576-
// }
524+
use indoc::indoc;
525+
const SMCAT_STATIC: &str = indoc! {r#"
526+
initial,
527+
Init,
528+
Foo,
529+
Bar;
530+
initial => Init;
531+
Init -> Foo : " Init:> ";
532+
Foo -> Bar : " next ";
533+
Bar -> Foo [color="grey"] : " next ";
534+
"#};
535+
const SMCAT_LIVE_1: &str = indoc! {r#"
536+
initial,
537+
Init,
538+
Foo [active color="red"],
539+
Bar;
540+
initial => Init;
541+
Init -> Foo [color="red" width=2] : " Init:> ";
542+
Foo -> Bar : " next ";
543+
Bar -> Foo [color="grey"] : " next ";
544+
"#};
545+
const SMCAT_LIVE_2: &str = indoc! {r#"
546+
initial,
547+
Init,
548+
Foo,
549+
Bar [active color="red"];
550+
initial => Init;
551+
Init -> Foo : " Init:> ";
552+
Foo -> Bar [color="red" width=2] : " next ";
553+
Bar -> Foo [color="grey"] : " next ";
554+
"#};
555+
const SMCAT_LIVE_3: &str = indoc! {r#"
556+
initial,
557+
Init,
558+
Foo [active color="red"],
559+
Bar;
560+
initial => Init;
561+
Init -> Foo : " Init:> ";
562+
Foo -> Bar : " next ";
563+
Bar -> Foo [color="pink" width=2] : " next ";
564+
"#};
565+
566+
#[test]
567+
fn smcat_render_static() {
568+
use frame_runtime::smcat::*;
569+
let smcat = Renderer::new(Box::new(SimpleStyle));
570+
assert_eq!(smcat.render_static(super::info::machine()), SMCAT_STATIC);
571+
}
572+
573+
#[test]
574+
fn smcat_render_live_sync() {
575+
use crate::demo::sync::*;
576+
use frame_runtime::smcat::sync::*;
577+
let smcat = Renderer::new(Box::new(SimpleStyle));
578+
579+
let mut sm = Demo::new();
580+
assert_eq!(smcat.render_live_sync(&sm), SMCAT_LIVE_1);
581+
sm.next();
582+
assert_eq!(smcat.render_live_sync(&sm), SMCAT_LIVE_2);
583+
sm.next();
584+
assert_eq!(smcat.render_live_sync(&sm), SMCAT_LIVE_3);
585+
}
586+
587+
#[test]
588+
fn smcat_render_live_unsync() {
589+
use crate::demo::unsync::*;
590+
use frame_runtime::smcat::unsync::*;
591+
let smcat = Renderer::new(Box::new(SimpleStyle));
592+
593+
let mut sm = Demo::new();
594+
assert_eq!(smcat.render_live_unsync(&sm), SMCAT_LIVE_1);
595+
sm.next();
596+
assert_eq!(smcat.render_live_unsync(&sm), SMCAT_LIVE_2);
597+
sm.next();
598+
assert_eq!(smcat.render_live_unsync(&sm), SMCAT_LIVE_3);
599+
}
577600
}

0 commit comments

Comments
 (0)