ebi_objects/traits/
graphable.rs

1use anyhow::Result;
2use layout::{
3    adt::dag::NodeHandle,
4    core::{base::Orientation, color::Color, geometry::Point, style::StyleAttr},
5    std_shapes::{
6        render::get_shape_size,
7        shapes::{Arrow, Element},
8    },
9    topo::layout::VisualGraph,
10};
11
12pub trait Graphable {
13    fn to_dot(&self) -> Result<VisualGraph>;
14}
15
16pub fn create_place(graph: &mut VisualGraph, label: &str) -> NodeHandle {
17    let shape = layout::std_shapes::shapes::ShapeKind::Circle(label.to_string());
18    let look = StyleAttr::simple();
19    let orientation = Orientation::LeftToRight;
20    let size = get_shape_size(orientation, &shape, look.font_size, true);
21    let node = Element::create(shape, look, orientation, size);
22    return graph.add_node(node);
23}
24
25pub fn create_transition(graph: &mut VisualGraph, label: &str, xlabel: &str) -> NodeHandle {
26    let shape = layout::std_shapes::shapes::ShapeKind::Box(label.to_string() + "\n" + xlabel);
27    let look = StyleAttr::simple();
28    let orientation = Orientation::LeftToRight;
29    let size = get_shape_size(orientation, &shape, look.font_size, false);
30    let node = Element::create(shape, look, orientation, size);
31    return graph.add_node(node);
32}
33
34pub fn create_silent_transition(graph: &mut VisualGraph, xlabel: &str) -> NodeHandle {
35    let shape = layout::std_shapes::shapes::ShapeKind::Box(xlabel.to_string());
36    let mut look = StyleAttr::simple();
37    look.fill_color = Some(Color::fast("grey"));
38    let orientation = Orientation::LeftToRight;
39    let size = Point::new(20., 30.);
40    let node = Element::create(shape, look, orientation, size);
41    return graph.add_node(node);
42}
43
44pub fn create_gateway(graph: &mut VisualGraph, label: &str) -> NodeHandle {
45    let shape = layout::std_shapes::shapes::ShapeKind::Box(label.to_string());
46    let mut look = StyleAttr::simple();
47    look.fill_color = Some(Color::new(0xa1cff3));
48    let orientation = Orientation::LeftToRight;
49    let size = Point::new(20., 20.);
50    let node = Element::create(shape, look, orientation, size);
51    return graph.add_node(node);
52}
53
54pub fn create_dot(graph: &mut VisualGraph) -> NodeHandle {
55    let shape = layout::std_shapes::shapes::ShapeKind::Circle("".to_string());
56    let look = StyleAttr::simple();
57    let orientation = Orientation::LeftToRight;
58    let size = Point::new(5., 5.);
59    let node = Element::create(shape, look, orientation, size);
60    return graph.add_node(node);
61}
62
63pub fn create_edge(graph: &mut VisualGraph, from: &NodeHandle, to: &NodeHandle, label: &str) {
64    let arrow = Arrow::simple(label);
65    return graph.add_edge(arrow, *from, *to);
66}