Skip to content

Commit 53eb315

Browse files
committed
Enhance graph visualization with new edge types and CLI improvements
- Added support for different edge types (Structure, Call, DataFlow) in L5X graph. - Updated SVG renderer to handle new edge styles and markers. - Refactored CLI to allow selection of graph types and improved argument parsing. - Introduced example command for generating sample graphs without L5X files. - Enhanced error handling and user feedback for missing input files.
1 parent 85932e8 commit 53eb315

6 files changed

Lines changed: 402 additions & 91 deletions

File tree

‎Cargo.lock‎

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎plcviz/Cargo.toml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@ rust-version = "1.70"
1616
[dependencies]
1717
l5x = "0.3"
1818
layout-rs = "0.1"
19+
regex = "1"
20+
clap = { version = "4.5", features = ["derive"] }
1921

2022
[dev-dependencies]

‎plcviz/src/config.rs‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Visualization configuration
22
3+
use std::str::FromStr;
4+
35
/// Type of graph to generate
46
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
57
pub enum GraphType {
@@ -8,10 +10,37 @@ pub enum GraphType {
810
Structure,
911
/// Call graph (Routine → Routine via JSR, AOI calls)
1012
CallGraph,
13+
/// Data flow graph (Tag read/write relationships)
14+
DataFlow,
1115
/// Combined structure + calls
1216
Combined,
1317
}
1418

19+
impl FromStr for GraphType {
20+
type Err = String;
21+
22+
fn from_str(s: &str) -> Result<Self, Self::Err> {
23+
match s.to_lowercase().as_str() {
24+
"structure" | "struct" | "s" => Ok(GraphType::Structure),
25+
"call" | "callgraph" | "calls" | "c" => Ok(GraphType::CallGraph),
26+
"dataflow" | "data" | "flow" | "d" => Ok(GraphType::DataFlow),
27+
"combined" | "all" | "a" => Ok(GraphType::Combined),
28+
_ => Err(format!("Unknown graph type: '{}'. Valid: structure, call, dataflow, combined", s)),
29+
}
30+
}
31+
}
32+
33+
impl GraphType {
34+
pub fn description(&self) -> &'static str {
35+
match self {
36+
GraphType::Structure => "Containment hierarchy (Programs → Routines)",
37+
GraphType::CallGraph => "Call graph (JSR, AOI calls)",
38+
GraphType::DataFlow => "Data flow (Tag read/write)",
39+
GraphType::Combined => "Combined structure + calls",
40+
}
41+
}
42+
}
43+
1544
/// What elements to include in the graph
1645
#[derive(Debug, Clone)]
1746
pub struct ElementFilter {

‎plcviz/src/graph/l5x_graph.rs‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,25 @@ impl L5xNodeType {
7979
}
8080
}
8181

82+
/// Type of edge
83+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
84+
pub enum EdgeType {
85+
/// Containment edge (Program → Routine)
86+
#[default]
87+
Structure,
88+
/// Call edge (JSR call between routines)
89+
Call,
90+
/// Data flow edge (tag read/write)
91+
DataFlow,
92+
}
93+
8294
/// An edge in the L5X graph
8395
#[derive(Debug, Clone)]
8496
pub struct L5xEdge {
8597
pub from: String,
8698
pub to: String,
8799
pub label: Option<String>,
100+
pub edge_type: EdgeType,
88101
}
89102

90103
impl Default for L5xGraph {
@@ -150,15 +163,17 @@ impl L5xGraph {
150163
from: from.to_string(),
151164
to: to.to_string(),
152165
label: None,
166+
edge_type: EdgeType::Call,
153167
});
154168
}
155169

156-
/// Add a labeled edge
170+
/// Add a labeled edge (structure/containment by default)
157171
pub fn add_edge(&mut self, from: &str, to: &str, label: Option<&str>) {
158172
self.edges.push(L5xEdge {
159173
from: from.to_string(),
160174
to: to.to_string(),
161175
label: label.map(|s| s.to_string()),
176+
edge_type: EdgeType::Structure,
162177
});
163178
}
164179

@@ -197,8 +212,17 @@ impl L5xGraph {
197212
// Add edges
198213
for edge in &self.edges {
199214
if let (Some(&from_h), Some(&to_h)) = (handles.get(&edge.from), handles.get(&edge.to)) {
200-
let label = edge.label.as_deref().unwrap_or("");
201-
let arrow = Arrow::simple(label);
215+
// Encode edge type in label prefix for renderer to parse
216+
let type_prefix = match edge.edge_type {
217+
EdgeType::Structure => "__STRUCT__",
218+
EdgeType::Call => "__CALL__",
219+
EdgeType::DataFlow => "__DATA__",
220+
};
221+
let label = match &edge.label {
222+
Some(l) => format!("{}{}", type_prefix, l),
223+
None => type_prefix.to_string(),
224+
};
225+
let arrow = Arrow::simple(&label);
202226
vg.add_edge(arrow, from_h, to_h);
203227
}
204228
}

‎plcviz/src/graph/renderer.rs‎

Lines changed: 69 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,20 @@ impl SvgRenderer {
6565
));
6666

6767
// Defs for arrow markers
68-
// refX=10 positions arrow tip at exact endpoint, but we want gap
69-
// markerWidth=8, markerHeight=6 for smaller arrows
7068
svg.push_str(
7169
r##"<defs>
7270
<marker id="arrowhead" markerWidth="8" markerHeight="6" refX="8" refY="3" orient="auto">
7371
<polygon points="0 0, 8 3, 0 6" fill="#333"/>
7472
</marker>
73+
<marker id="arrowhead-struct" markerWidth="8" markerHeight="6" refX="8" refY="3" orient="auto">
74+
<polygon points="0 0, 8 3, 0 6" fill="#757575"/>
75+
</marker>
76+
<marker id="arrowhead-call" markerWidth="8" markerHeight="6" refX="8" refY="3" orient="auto">
77+
<polygon points="0 0, 8 3, 0 6" fill="#1565C0"/>
78+
</marker>
79+
<marker id="arrowhead-data" markerWidth="8" markerHeight="6" refX="8" refY="3" orient="auto">
80+
<polygon points="0 0, 8 3, 0 6" fill="#2E7D32"/>
81+
</marker>
7582
</defs>
7683
"##,
7784
);
@@ -86,6 +93,50 @@ impl SvgRenderer {
8693
svg.push_str("</svg>\n");
8794
svg
8895
}
96+
97+
/// Draw an arrow path with specific style
98+
fn draw_arrow_with_style(
99+
&mut self,
100+
path: &[(Point, Point)],
101+
stroke: &str,
102+
dash: &str,
103+
has_head: bool,
104+
marker_id: &str,
105+
line_width: usize,
106+
) {
107+
let marker = if has_head {
108+
format!(r#" marker-end="url(#{})""#, marker_id)
109+
} else {
110+
String::new()
111+
};
112+
113+
// Build path data - cubic bezier curves
114+
let mut d = String::new();
115+
116+
// First segment: M start C control1, control2, end
117+
d.push_str(&format!("M {:.1},{:.1}", path[0].0.x, path[0].0.y));
118+
119+
if path.len() >= 2 {
120+
d.push_str(&format!(
121+
" C {:.1},{:.1} {:.1},{:.1} {:.1},{:.1}",
122+
path[0].1.x, path[0].1.y, path[1].0.x, path[1].0.y, path[1].1.x, path[1].1.y
123+
));
124+
}
125+
126+
// Additional segments use S (smooth curve)
127+
for point in path.iter().skip(2) {
128+
d.push_str(&format!(
129+
" S {:.1},{:.1} {:.1},{:.1}",
130+
point.0.x, point.0.y, point.1.x, point.1.y
131+
));
132+
}
133+
134+
self.content.push_str(&format!(
135+
r#"<path d="{}" fill="none" stroke="{}"{}{} stroke-width="{}"/>
136+
"#,
137+
d, stroke, dash, marker, line_width
138+
));
139+
}
89140
}
90141

91142
impl Default for SvgRenderer {
@@ -178,7 +229,7 @@ impl RenderBackend for SvgRenderer {
178229
head: (bool, bool),
179230
look: &StyleAttr,
180231
_properties: Option<String>,
181-
_text: &str,
232+
text: &str,
182233
) {
183234
if path.is_empty() {
184235
return;
@@ -188,44 +239,24 @@ impl RenderBackend for SvgRenderer {
188239
self.grow(p1.x.max(p2.x), p1.y.max(p2.y));
189240
}
190241

191-
let stroke = Self::color_to_svg(&look.line_color);
192-
let dash = if dashed {
193-
r#" stroke-dasharray="5,3""#
194-
} else {
195-
""
196-
};
197-
let marker = if head.1 {
198-
r#" marker-end="url(#arrowhead)""#
242+
// Parse edge type from text prefix and set color/style accordingly
243+
let (stroke, dash, marker_id) = if text.starts_with("__CALL__") {
244+
// Call edges: solid blue with blue arrow
245+
("#1565C0", "", "arrowhead-call")
246+
} else if text.starts_with("__DATA__") {
247+
// Data flow edges: dashed green
248+
("#2E7D32", r#" stroke-dasharray="5,3""#, "arrowhead-data")
249+
} else if text.starts_with("__STRUCT__") {
250+
// Structure edges: gray dashed
251+
("#757575", r#" stroke-dasharray="4,2""#, "arrowhead-struct")
199252
} else {
200-
""
253+
// Default: black solid
254+
let stroke = Self::color_to_svg(&look.line_color);
255+
let dash = if dashed { r#" stroke-dasharray="5,3""# } else { "" };
256+
return self.draw_arrow_with_style(path, &stroke, dash, head.1, "arrowhead", look.line_width);
201257
};
202258

203-
// Build path data - cubic bezier curves
204-
let mut d = String::new();
205-
206-
// First segment: M start C control1, control2, end
207-
d.push_str(&format!("M {:.1},{:.1}", path[0].0.x, path[0].0.y));
208-
209-
if path.len() >= 2 {
210-
d.push_str(&format!(
211-
" C {:.1},{:.1} {:.1},{:.1} {:.1},{:.1}",
212-
path[0].1.x, path[0].1.y, path[1].0.x, path[1].0.y, path[1].1.x, path[1].1.y
213-
));
214-
}
215-
216-
// Additional segments use S (smooth curve)
217-
for point in path.iter().skip(2) {
218-
d.push_str(&format!(
219-
" S {:.1},{:.1} {:.1},{:.1}",
220-
point.0.x, point.0.y, point.1.x, point.1.y
221-
));
222-
}
223-
224-
self.content.push_str(&format!(
225-
r#"<path d="{}" fill="none" stroke="{}"{}{} stroke-width="{}"/>
226-
"#,
227-
d, stroke, dash, marker, look.line_width
228-
));
259+
self.draw_arrow_with_style(path, stroke, dash, head.1, marker_id, look.line_width);
229260
}
230261

231262
fn create_clip(&mut self, _xy: Point, _size: Point, _rounded_px: usize) -> ClipHandle {

0 commit comments

Comments
 (0)