modality-lang 0.1.6

Modality language lib
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
use crate::ast::{Model, Graph, Transition, Property, PropertySign};

/// Generate a Mermaid state diagram from a Modality model
pub fn generate_mermaid_diagram(model: &Model) -> String {
    let mut diagram = String::new();
    diagram.push_str("stateDiagram-v2\n");
    
    for (_graph_idx, graph) in model.graphs.iter().enumerate() {
        if model.graphs.len() > 1 {
            diagram.push_str(&format!("    state {} {{\n", graph.name));
        }
        
        // Add all nodes first with graph prefix if multiple graphs
        let mut nodes = std::collections::HashSet::new();
        for transition in &graph.transitions {
            nodes.insert(&transition.from);
            nodes.insert(&transition.to);
        }
        
        for node in nodes {
            if model.graphs.len() > 1 {
                diagram.push_str(&format!("        {}.{} : {}\n", graph.name, node, node));
            } else {
                diagram.push_str(&format!("        {}\n", node));
            }
        }
        
        // Add all transitions within this graph only
        for transition in &graph.transitions {
            let edge_label = if transition.properties.is_empty() {
                String::new()
            } else {
                let props: Vec<String> = transition.properties
                    .iter()
                    .map(|prop| {
                        let sign = match prop.sign {
                            PropertySign::Plus => "+",
                            PropertySign::Minus => "-",
                        };
                        format!("{}{}", sign, prop.name)
                    })
                    .collect();
                format!(" : {}", props.join(" "))
            };
            
            if model.graphs.len() > 1 {
                diagram.push_str(&format!("        {}.{} --> {}.{}{}\n", 
                    graph.name, transition.from, graph.name, transition.to, edge_label));
            } else {
                diagram.push_str(&format!("        {} --> {}{}\n", 
                    transition.from, transition.to, edge_label));
            }
        }
        
        if model.graphs.len() > 1 {
            diagram.push_str("    }\n");
        }
    }
    
    diagram
}

/// Generate a Mermaid state diagram from multiple models
pub fn generate_mermaid_diagrams(models: &[Model]) -> String {
    let mut diagrams = String::new();
    
    for (i, model) in models.iter().enumerate() {
        diagrams.push_str(&format!("%% Model {}: {}\n", i + 1, model.name));
        diagrams.push_str(&generate_mermaid_diagram(model));
        if i < models.len() - 1 {
            diagrams.push_str("\n");
        }
    }
    
    diagrams
}

/// Generate a Mermaid state diagram with custom styling
pub fn generate_mermaid_diagram_with_styling(model: &Model) -> String {
    let mut diagram = String::new();
    diagram.push_str("stateDiagram-v2\n");
    
    // Add styling
    diagram.push_str("    classDef default fill:#f9f9f9,stroke:#333,stroke-width:2px\n");
    diagram.push_str("    classDef start fill:#d4edda,stroke:#155724,stroke-width:2px\n");
    diagram.push_str("    classDef end fill:#f8d7da,stroke:#721c24,stroke-width:2px\n");
    diagram.push_str("    classDef property fill:#fff3cd,stroke:#856404,stroke-width:2px\n");
    diagram.push_str("    classDef current fill:#e3f2fd,stroke:#1976d2,stroke-width:3px\n");
    
    for (_graph_idx, graph) in model.graphs.iter().enumerate() {
        if model.graphs.len() > 1 {
            diagram.push_str(&format!("    state {} {{\n", graph.name));
        }
        
        // Add all nodes first with graph prefix if multiple graphs
        let mut nodes = std::collections::HashSet::new();
        for transition in &graph.transitions {
            nodes.insert(&transition.from);
            nodes.insert(&transition.to);
        }
        
        for node in nodes {
            if model.graphs.len() > 1 {
                diagram.push_str(&format!("        {}.{} : {}\n", graph.name, node, node));
            } else {
                diagram.push_str(&format!("        {}\n", node));
            }
        }
        
        // Add all transitions within this graph only
        for transition in &graph.transitions {
            let edge_label = if transition.properties.is_empty() {
                String::new()
            } else {
                let props: Vec<String> = transition.properties
                    .iter()
                    .map(|prop| {
                        let sign = match prop.sign {
                            PropertySign::Plus => "+",
                            PropertySign::Minus => "-",
                        };
                        format!("{}{}", sign, prop.name)
                    })
                    .collect();
                format!(" : {}", props.join(" "))
            };
            
            if model.graphs.len() > 1 {
                diagram.push_str(&format!("        {}.{} --> {}.{}{}\n", 
                    graph.name, transition.from, graph.name, transition.to, edge_label));
            } else {
                diagram.push_str(&format!("        {} --> {}{}\n", 
                    transition.from, transition.to, edge_label));
            }
        }
        
        if model.graphs.len() > 1 {
            diagram.push_str("    }\n");
        }
    }
    
    // Apply default styling to all nodes
    diagram.push_str("    class * default\n");
    
    diagram
}

/// Generate a Mermaid state diagram with current state highlighting
pub fn generate_mermaid_diagram_with_state(model: &Model) -> String {
    let mut diagram = String::new();
    diagram.push_str("stateDiagram-v2\n");
    
    // Add styling for current states
    diagram.push_str("    classDef current fill:#e3f2fd,stroke:#1976d2,stroke-width:3px\n");
    
    for (_graph_idx, graph) in model.graphs.iter().enumerate() {
        if model.graphs.len() > 1 {
            diagram.push_str(&format!("    state {} {{\n", graph.name));
        }
        
        // Add all nodes first with graph prefix if multiple graphs
        let mut nodes = std::collections::HashSet::new();
        for transition in &graph.transitions {
            nodes.insert(&transition.from);
            nodes.insert(&transition.to);
        }
        
        // Check if this graph has current state information
        let empty_vec = Vec::<String>::new();
        let current_nodes = if let Some(state) = &model.state {
            state.iter()
                .find(|s| s.graph_name == graph.name)
                .map(|s| &s.current_nodes)
                .unwrap_or(&empty_vec)
        } else {
            &empty_vec
        };
        
        for node in nodes {
            let node_name = if model.graphs.len() > 1 {
                format!("{}.{}", graph.name, node)
            } else {
                node.to_string()
            };
            
            if model.graphs.len() > 1 {
                diagram.push_str(&format!("        {}.{} : {}\n", graph.name, node, node));
            } else {
                diagram.push_str(&format!("        {}\n", node));
            }
        }
        
        // Add all transitions within this graph only
        for transition in &graph.transitions {
            let edge_label = if transition.properties.is_empty() {
                String::new()
            } else {
                let props: Vec<String> = transition.properties
                    .iter()
                    .map(|prop| {
                        let sign = match prop.sign {
                            PropertySign::Plus => "+",
                            PropertySign::Minus => "-",
                        };
                        format!("{}{}", sign, prop.name)
                    })
                    .collect();
                format!(" : {}", props.join(" "))
            };
            
            if model.graphs.len() > 1 {
                diagram.push_str(&format!("        {}.{} --> {}.{}{}\n", 
                    graph.name, transition.from, graph.name, transition.to, edge_label));
            } else {
                diagram.push_str(&format!("        {} --> {}{}\n", 
                    transition.from, transition.to, edge_label));
            }
        }
        
        if model.graphs.len() > 1 {
            diagram.push_str("    }\n");
        }
    }
    
    // Apply current state styling
    if let Some(state) = &model.state {
        for graph_state in state {
            for node in &graph_state.current_nodes {
                let node_name = if model.graphs.len() > 1 {
                    format!("{}.{}", graph_state.graph_name, node)
                } else {
                    node.to_string()
                };
                diagram.push_str(&format!("    class {} current\n", node_name));
            }
        }
    }
    
    diagram
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ast::{Model, Graph, Transition, Property, PropertySign};

    #[test]
    fn test_generate_simple_diagram() {
        let mut model = Model::new("TestModel".to_string());
        let mut graph = Graph::new("g1".to_string());
        
        let transition = Transition::new("n1".to_string(), "n2".to_string());
        graph.add_transition(transition);
        
        model.add_graph(graph);
        
        let diagram = generate_mermaid_diagram(&model);
        
        assert!(diagram.contains("stateDiagram-v2"));
        assert!(diagram.contains("n1"));
        assert!(diagram.contains("n2"));
        assert!(diagram.contains("n1 --> n2"));
    }

    #[test]
    fn test_generate_diagram_with_properties() {
        let mut model = Model::new("TestModel".to_string());
        let mut graph = Graph::new("g1".to_string());
        
        let mut transition = Transition::new("n1".to_string(), "n2".to_string());
        transition.add_property(Property::new(PropertySign::Plus, "blue".to_string()));
        transition.add_property(Property::new(PropertySign::Minus, "red".to_string()));
        graph.add_transition(transition);
        
        model.add_graph(graph);
        
        let diagram = generate_mermaid_diagram(&model);
        
        assert!(diagram.contains("n1 --> n2 : +blue -red"));
    }

    #[test]
    fn test_generate_multiple_graphs_diagram() {
        let mut model = Model::new("TestModel".to_string());
        
        // First graph
        let mut graph1 = Graph::new("g1".to_string());
        let transition1 = Transition::new("n1".to_string(), "n2".to_string());
        graph1.add_transition(transition1);
        model.add_graph(graph1);
        
        // Second graph
        let mut graph2 = Graph::new("g2".to_string());
        let transition2 = Transition::new("a".to_string(), "b".to_string());
        graph2.add_transition(transition2);
        model.add_graph(graph2);
        
        let diagram = generate_mermaid_diagram(&model);
        
        assert!(diagram.contains("state g1 {"));
        assert!(diagram.contains("state g2 {"));
        assert!(diagram.contains("g1.n1 : n1"));
        assert!(diagram.contains("g1.n2 : n2"));
        assert!(diagram.contains("g2.a : a"));
        assert!(diagram.contains("g2.b : b"));
        assert!(diagram.contains("g1.n1 --> g1.n2"));
        assert!(diagram.contains("g2.a --> g2.b"));
        
        // Verify graphs are isolated - no transitions between graphs
        assert!(!diagram.contains("g1.n1 --> g2.a"));
        assert!(!diagram.contains("g2.a --> g1.n1"));
    }

    #[test]
    fn test_graph_isolation() {
        let mut model = Model::new("TestModel".to_string());
        
        // Graph 1 with nodes n1, n2
        let mut graph1 = Graph::new("g1".to_string());
        let transition1 = Transition::new("n1".to_string(), "n2".to_string());
        graph1.add_transition(transition1);
        model.add_graph(graph1);
        
        // Graph 2 with nodes a, b
        let mut graph2 = Graph::new("g2".to_string());
        let transition2 = Transition::new("a".to_string(), "b".to_string());
        graph2.add_transition(transition2);
        model.add_graph(graph2);
        
        let diagram = generate_mermaid_diagram(&model);
        
        // Verify each graph only contains its own nodes and transitions
        let lines: Vec<&str> = diagram.lines().collect();
        
        // Find the g1 state block
        let mut in_g1 = false;
        let mut g1_nodes = std::collections::HashSet::new();
        let mut g1_transitions = std::collections::HashSet::new();
        
        // Find the g2 state block
        let mut in_g2 = false;
        let mut g2_nodes = std::collections::HashSet::new();
        let mut g2_transitions = std::collections::HashSet::new();
        
        for line in lines {
            let trimmed = line.trim();
            if trimmed == "state g1 {" {
                in_g1 = true;
                in_g2 = false;
            } else if trimmed == "state g2 {" {
                in_g1 = false;
                in_g2 = true;
            } else if trimmed == "}" {
                in_g1 = false;
                in_g2 = false;
            } else if in_g1 && !trimmed.is_empty() && !trimmed.starts_with("state ") {
                if trimmed.contains("-->") {
                    g1_transitions.insert(trimmed.to_string());
                } else {
                    g1_nodes.insert(trimmed.to_string());
                }
            } else if in_g2 && !trimmed.is_empty() && !trimmed.starts_with("state ") {
                if trimmed.contains("-->") {
                    g2_transitions.insert(trimmed.to_string());
                } else {
                    g2_nodes.insert(trimmed.to_string());
                }
            }
        }
        
        // Verify g1 only contains g1.n1, g1.n2 and their transition
        assert!(g1_nodes.contains("g1.n1 : n1"));
        assert!(g1_nodes.contains("g1.n2 : n2"));
        assert!(!g1_nodes.contains("g2.a : a"));
        assert!(!g1_nodes.contains("g2.b : b"));
        assert!(g1_transitions.iter().any(|t| t.contains("g1.n1 --> g1.n2")));
        assert!(!g1_transitions.iter().any(|t| t.contains("g2.a") || t.contains("g2.b")));
        
        // Verify g2 only contains g2.a, g2.b and their transition
        assert!(g2_nodes.contains("g2.a : a"));
        assert!(g2_nodes.contains("g2.b : b"));
        assert!(!g2_nodes.contains("g1.n1 : n1"));
        assert!(!g2_nodes.contains("g1.n2 : n2"));
        assert!(g2_transitions.iter().any(|t| t.contains("g2.a --> g2.b")));
        assert!(!g2_transitions.iter().any(|t| t.contains("g1.n1") || t.contains("g1.n2")));
    }

    #[test]
    fn test_single_graph_no_prefix() {
        let mut model = Model::new("TestModel".to_string());
        let mut graph = Graph::new("g1".to_string());
        
        let transition = Transition::new("n1".to_string(), "n2".to_string());
        graph.add_transition(transition);
        
        model.add_graph(graph);
        
        let diagram = generate_mermaid_diagram(&model);
        
        // Single graph should not use prefixes
        assert!(diagram.contains("n1"));
        assert!(diagram.contains("n2"));
        assert!(diagram.contains("n1 --> n2"));
        assert!(!diagram.contains("g1.n1"));
        assert!(!diagram.contains("g1.n2"));
    }

    #[test]
    fn test_generate_diagram_with_state() {
        use crate::ast::GraphState;
        
        let mut model = Model::new("TestModel".to_string());
        let mut graph = Graph::new("g1".to_string());
        
        let transition = Transition::new("n1".to_string(), "n2".to_string());
        graph.add_transition(transition);
        model.add_graph(graph);
        
        // Add state information
        let state = vec![
            GraphState::new("g1".to_string(), vec!["n1".to_string(), "n2".to_string()])
        ];
        model.set_state(state);
        
        let diagram = generate_mermaid_diagram_with_state(&model);
        
        // Should contain current state styling
        assert!(diagram.contains("classDef current fill:#e3f2fd,stroke:#1976d2,stroke-width:3px"));
        assert!(diagram.contains("class n1 current"));
        assert!(diagram.contains("class n2 current"));
    }

    #[test]
    fn test_generate_diagram_with_multiple_graphs_and_state() {
        use crate::ast::GraphState;
        
        let mut model = Model::new("TestModel".to_string());
        
        // First graph
        let mut graph1 = Graph::new("g1".to_string());
        let transition1 = Transition::new("n1".to_string(), "n2".to_string());
        graph1.add_transition(transition1);
        model.add_graph(graph1);
        
        // Second graph
        let mut graph2 = Graph::new("g2".to_string());
        let transition2 = Transition::new("a".to_string(), "b".to_string());
        graph2.add_transition(transition2);
        model.add_graph(graph2);
        
        // Add state information
        let state = vec![
            GraphState::new("g1".to_string(), vec!["n1".to_string()]),
            GraphState::new("g2".to_string(), vec!["a".to_string(), "b".to_string()])
        ];
        model.set_state(state);
        
        let diagram = generate_mermaid_diagram_with_state(&model);
        
        // Should contain current state styling with prefixed names
        assert!(diagram.contains("classDef current fill:#e3f2fd,stroke:#1976d2,stroke-width:3px"));
        assert!(diagram.contains("class g1.n1 current"));
        assert!(diagram.contains("class g2.a current"));
        assert!(diagram.contains("class g2.b current"));
    }
}