dol 0.8.1

DOL (Design Ontology Language) - A declarative specification language for ontology-first development
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
//! Network Topology Module
//!
//! Hyphal-inspired network topology algorithms for distributed coordination.
//!
//! This module provides graph-based network representation with support for:
//! - Node management (exploration tips, transport nodes, hubs)
//! - Edge connections with capacity and latency metrics
//! - Anastomosis (fusion) operations for network consolidation
//! - Dijkstra's shortest path algorithm for message routing
//! - Network topology metrics

use std::collections::{HashMap, HashSet};

/// Node identifier in the hyphal network.
///
/// Each node represents a point in the distributed network, which can be
/// an active exploration tip, a transport segment, or a fusion hub.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct NodeId(pub u64);

impl NodeId {
    /// Create a new NodeId from a u64 value.
    pub fn new(id: u64) -> Self {
        Self(id)
    }

    /// Get the underlying u64 value.
    pub fn value(&self) -> u64 {
        self.0
    }
}

/// Edge between nodes in the hyphal network.
///
/// Represents a hyphal segment connecting two nodes with defined
/// transport capacity and communication latency.
#[derive(Debug, Clone)]
pub struct Edge {
    /// Source node of the edge
    pub source: NodeId,
    /// Target node of the edge
    pub target: NodeId,
    /// Transport capacity (bandwidth) of the edge
    pub capacity: f64,
    /// Communication latency of the edge
    pub latency: f64,
}

impl Edge {
    /// Create a new edge with the given parameters.
    pub fn new(source: NodeId, target: NodeId, capacity: f64, latency: f64) -> Self {
        Self {
            source,
            target,
            capacity,
            latency,
        }
    }

    /// Create an edge with default latency calculated from capacity.
    pub fn with_capacity(source: NodeId, target: NodeId, capacity: f64) -> Self {
        Self {
            source,
            target,
            capacity,
            latency: 1.0 / capacity,
        }
    }
}

/// Network topology representing a hyphal graph structure.
///
/// The topology maintains:
/// - A set of all nodes in the network
/// - A list of directed edges connecting nodes
/// - A set of active exploration tips
#[derive(Debug, Clone)]
pub struct Topology {
    /// All nodes in the network
    pub nodes: HashSet<NodeId>,
    /// All edges connecting nodes
    pub edges: Vec<Edge>,
    /// Active exploration tips (growing nodes)
    pub active_tips: HashSet<NodeId>,
}

impl Default for Topology {
    fn default() -> Self {
        Self::new()
    }
}

impl Topology {
    /// Create a new empty topology.
    pub fn new() -> Self {
        Self {
            nodes: HashSet::new(),
            edges: vec![],
            active_tips: HashSet::new(),
        }
    }

    /// Add a node as an exploration tip.
    ///
    /// The node is added to both the node set and the active tips set.
    pub fn add_tip(&mut self, id: NodeId) {
        self.nodes.insert(id);
        self.active_tips.insert(id);
    }

    /// Add a node without making it an active tip.
    pub fn add_node(&mut self, id: NodeId) {
        self.nodes.insert(id);
    }

    /// Remove a node from active tips (make it dormant).
    pub fn deactivate_tip(&mut self, id: NodeId) {
        self.active_tips.remove(&id);
    }

    /// Connect two nodes with an edge.
    ///
    /// Both nodes are added to the network if not already present.
    pub fn connect(&mut self, source: NodeId, target: NodeId, capacity: f64) {
        self.nodes.insert(source);
        self.nodes.insert(target);
        self.edges.push(Edge {
            source,
            target,
            capacity,
            latency: 1.0 / capacity,
        });
    }

    /// Connect two nodes with a bidirectional edge.
    pub fn connect_bidirectional(&mut self, a: NodeId, b: NodeId, capacity: f64) {
        self.connect(a, b, capacity);
        self.connect(b, a, capacity);
    }

    /// Get neighbors of a node (nodes reachable via outgoing edges).
    pub fn neighbors(&self, node: NodeId) -> Vec<NodeId> {
        self.edges
            .iter()
            .filter(|e| e.source == node)
            .map(|e| e.target)
            .collect()
    }

    /// Get all edges from a node.
    pub fn edges_from(&self, node: NodeId) -> Vec<&Edge> {
        self.edges.iter().filter(|e| e.source == node).collect()
    }

    /// Get all edges to a node.
    pub fn edges_to(&self, node: NodeId) -> Vec<&Edge> {
        self.edges.iter().filter(|e| e.target == node).collect()
    }

    /// Perform anastomosis (fuse nearby tips).
    ///
    /// When two exploration tips merge, they create a stronger connection
    /// and one becomes inactive. Returns the surviving node if fusion succeeds.
    pub fn fuse_tips(&mut self, a: NodeId, b: NodeId) -> Option<NodeId> {
        if !self.active_tips.contains(&a) || !self.active_tips.contains(&b) {
            return None;
        }

        // Create fusion edge with higher capacity
        self.edges.push(Edge {
            source: a,
            target: b,
            capacity: 2.0, // Fused edges have higher capacity
            latency: 0.5,
        });

        // Create reverse edge for bidirectional connectivity
        self.edges.push(Edge {
            source: b,
            target: a,
            capacity: 2.0,
            latency: 0.5,
        });

        // One tip becomes inactive (absorbed into the network)
        self.active_tips.remove(&b);

        Some(a)
    }

    /// Branch a tip into two new tips.
    ///
    /// Creates two new exploration tips from an existing tip,
    /// connecting them to the parent node.
    pub fn branch_tip(
        &mut self,
        tip: NodeId,
        left_id: NodeId,
        right_id: NodeId,
        capacity: f64,
    ) -> (NodeId, NodeId) {
        // Add new nodes as tips
        self.nodes.insert(left_id);
        self.nodes.insert(right_id);
        self.active_tips.insert(left_id);
        self.active_tips.insert(right_id);

        // Deactivate the parent tip
        self.active_tips.remove(&tip);

        // Connect parent to children
        self.edges.push(Edge {
            source: tip,
            target: left_id,
            capacity,
            latency: 1.0 / capacity,
        });
        self.edges.push(Edge {
            source: tip,
            target: right_id,
            capacity,
            latency: 1.0 / capacity,
        });

        (left_id, right_id)
    }

    /// Find shortest path using Dijkstra's algorithm.
    ///
    /// Uses edge latency as the distance metric.
    /// Returns None if no path exists.
    pub fn shortest_path(&self, from: NodeId, to: NodeId) -> Option<Vec<NodeId>> {
        if !self.nodes.contains(&from) || !self.nodes.contains(&to) {
            return None;
        }

        if from == to {
            return Some(vec![from]);
        }

        let mut distances: HashMap<NodeId, f64> = HashMap::new();
        let mut previous: HashMap<NodeId, NodeId> = HashMap::new();
        let mut unvisited: HashSet<NodeId> = self.nodes.clone();

        distances.insert(from, 0.0);

        while !unvisited.is_empty() {
            // Find minimum distance node among unvisited
            let current = unvisited
                .iter()
                .filter(|n| distances.contains_key(n))
                .min_by(|a, b| {
                    let da = distances.get(a).unwrap_or(&f64::INFINITY);
                    let db = distances.get(b).unwrap_or(&f64::INFINITY);
                    da.partial_cmp(db).unwrap_or(std::cmp::Ordering::Equal)
                })
                .copied();

            let current = match current {
                Some(c) => c,
                None => break, // No reachable nodes remaining
            };

            if current == to {
                // Reconstruct path
                let mut path = vec![to];
                let mut curr = to;
                while let Some(&prev) = previous.get(&curr) {
                    path.push(prev);
                    curr = prev;
                }
                path.reverse();
                return Some(path);
            }

            unvisited.remove(&current);
            let current_dist = *distances.get(&current).unwrap_or(&f64::INFINITY);

            // Update neighbors
            for edge in &self.edges {
                if edge.source == current && unvisited.contains(&edge.target) {
                    let alt = current_dist + edge.latency;
                    if alt < *distances.get(&edge.target).unwrap_or(&f64::INFINITY) {
                        distances.insert(edge.target, alt);
                        previous.insert(edge.target, current);
                    }
                }
            }
        }

        None
    }

    /// Check if there is any path between two nodes.
    pub fn is_connected(&self, from: NodeId, to: NodeId) -> bool {
        self.shortest_path(from, to).is_some()
    }

    /// Check if the entire network is connected (all nodes reachable from any node).
    pub fn is_fully_connected(&self) -> bool {
        if self.nodes.is_empty() {
            return true;
        }

        let start = *self.nodes.iter().next().unwrap();
        self.nodes.iter().all(|&n| self.is_connected(start, n))
    }

    /// Prune edges below a capacity threshold.
    pub fn prune(&mut self, threshold: f64) {
        self.edges.retain(|e| e.capacity >= threshold);
    }

    /// Calculate network metrics.
    pub fn metrics(&self) -> TopologyMetrics {
        let node_count = self.nodes.len();
        let edge_count = self.edges.len();
        let active_tip_count = self.active_tips.len();

        let total_capacity: f64 = self.edges.iter().map(|e| e.capacity).sum();
        let avg_capacity = if edge_count > 0 {
            total_capacity / edge_count as f64
        } else {
            0.0
        };

        let total_latency: f64 = self.edges.iter().map(|e| e.latency).sum();
        let avg_latency = if edge_count > 0 {
            total_latency / edge_count as f64
        } else {
            0.0
        };

        // Calculate density (ratio of edges to possible edges)
        let density = if node_count > 1 {
            edge_count as f64 / (node_count * (node_count - 1)) as f64
        } else {
            0.0
        };

        TopologyMetrics {
            node_count,
            edge_count,
            active_tip_count,
            avg_capacity,
            avg_latency,
            density,
        }
    }
}

/// Network topology metrics.
#[derive(Debug, Clone)]
pub struct TopologyMetrics {
    /// Total number of nodes
    pub node_count: usize,
    /// Total number of edges
    pub edge_count: usize,
    /// Number of active exploration tips
    pub active_tip_count: usize,
    /// Average edge capacity
    pub avg_capacity: f64,
    /// Average edge latency
    pub avg_latency: f64,
    /// Graph density (edges / possible edges)
    pub density: f64,
}

impl TopologyMetrics {
    /// Check if the network has exploration capacity.
    pub fn has_explorers(&self) -> bool {
        self.active_tip_count > 0
    }

    /// Check if the network is sparse.
    pub fn is_sparse(&self) -> bool {
        self.density < 0.3
    }

    /// Check if the network is dense.
    pub fn is_dense(&self) -> bool {
        self.density > 0.7
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_basic_topology() {
        let mut topo = Topology::new();
        topo.add_tip(NodeId(1));
        topo.add_tip(NodeId(2));
        topo.connect(NodeId(1), NodeId(2), 1.0);

        assert_eq!(topo.nodes.len(), 2);
        assert_eq!(topo.edges.len(), 1);
        assert_eq!(topo.active_tips.len(), 2);
    }

    #[test]
    fn test_shortest_path_simple() {
        let mut topo = Topology::new();
        topo.add_tip(NodeId(1));
        topo.add_tip(NodeId(2));
        topo.add_tip(NodeId(3));
        topo.connect(NodeId(1), NodeId(2), 1.0);
        topo.connect(NodeId(2), NodeId(3), 1.0);

        let path = topo.shortest_path(NodeId(1), NodeId(3));
        assert_eq!(path, Some(vec![NodeId(1), NodeId(2), NodeId(3)]));
    }

    #[test]
    fn test_shortest_path_same_node() {
        let mut topo = Topology::new();
        topo.add_tip(NodeId(1));

        let path = topo.shortest_path(NodeId(1), NodeId(1));
        assert_eq!(path, Some(vec![NodeId(1)]));
    }

    #[test]
    fn test_shortest_path_no_path() {
        let mut topo = Topology::new();
        topo.add_tip(NodeId(1));
        topo.add_tip(NodeId(2));
        // No edge connecting them

        let path = topo.shortest_path(NodeId(1), NodeId(2));
        assert_eq!(path, None);
    }

    #[test]
    fn test_shortest_path_prefers_lower_latency() {
        let mut topo = Topology::new();
        topo.add_tip(NodeId(1));
        topo.add_tip(NodeId(2));
        topo.add_tip(NodeId(3));

        // Direct path with high latency
        topo.edges.push(Edge {
            source: NodeId(1),
            target: NodeId(3),
            capacity: 0.1,
            latency: 10.0,
        });

        // Indirect path with lower total latency
        topo.connect(NodeId(1), NodeId(2), 1.0); // latency = 1.0
        topo.connect(NodeId(2), NodeId(3), 1.0); // latency = 1.0

        let path = topo.shortest_path(NodeId(1), NodeId(3));
        // Should prefer the indirect path (total latency 2.0 < 10.0)
        assert_eq!(path, Some(vec![NodeId(1), NodeId(2), NodeId(3)]));
    }

    #[test]
    fn test_anastomosis() {
        let mut topo = Topology::new();
        topo.add_tip(NodeId(1));
        topo.add_tip(NodeId(2));

        let fused = topo.fuse_tips(NodeId(1), NodeId(2));
        assert!(fused.is_some());
        assert_eq!(fused, Some(NodeId(1)));
        assert_eq!(topo.active_tips.len(), 1);
        assert!(topo.active_tips.contains(&NodeId(1)));
        assert!(!topo.active_tips.contains(&NodeId(2)));
        assert_eq!(topo.edges.len(), 2); // Bidirectional edges
    }

    #[test]
    fn test_anastomosis_requires_active_tips() {
        let mut topo = Topology::new();
        topo.add_tip(NodeId(1));
        topo.add_node(NodeId(2)); // Not an active tip

        let fused = topo.fuse_tips(NodeId(1), NodeId(2));
        assert!(fused.is_none());
    }

    #[test]
    fn test_branch_tip() {
        let mut topo = Topology::new();
        topo.add_tip(NodeId(1));

        let (left, right) = topo.branch_tip(NodeId(1), NodeId(2), NodeId(3), 1.0);

        assert_eq!(left, NodeId(2));
        assert_eq!(right, NodeId(3));
        assert_eq!(topo.nodes.len(), 3);
        assert_eq!(topo.edges.len(), 2);
        assert!(!topo.active_tips.contains(&NodeId(1)));
        assert!(topo.active_tips.contains(&NodeId(2)));
        assert!(topo.active_tips.contains(&NodeId(3)));
    }

    #[test]
    fn test_neighbors() {
        let mut topo = Topology::new();
        topo.add_tip(NodeId(1));
        topo.add_tip(NodeId(2));
        topo.add_tip(NodeId(3));
        topo.connect(NodeId(1), NodeId(2), 1.0);
        topo.connect(NodeId(1), NodeId(3), 1.0);

        let neighbors = topo.neighbors(NodeId(1));
        assert_eq!(neighbors.len(), 2);
        assert!(neighbors.contains(&NodeId(2)));
        assert!(neighbors.contains(&NodeId(3)));
    }

    #[test]
    fn test_prune() {
        let mut topo = Topology::new();
        topo.add_tip(NodeId(1));
        topo.add_tip(NodeId(2));
        topo.add_tip(NodeId(3));
        topo.connect(NodeId(1), NodeId(2), 0.5);
        topo.connect(NodeId(2), NodeId(3), 1.5);

        topo.prune(1.0);

        assert_eq!(topo.edges.len(), 1);
        assert_eq!(topo.edges[0].source, NodeId(2));
        assert_eq!(topo.edges[0].target, NodeId(3));
    }

    #[test]
    fn test_metrics() {
        let mut topo = Topology::new();
        topo.add_tip(NodeId(1));
        topo.add_tip(NodeId(2));
        topo.add_tip(NodeId(3));
        topo.connect(NodeId(1), NodeId(2), 1.0);
        topo.connect(NodeId(2), NodeId(3), 2.0);

        let metrics = topo.metrics();
        assert_eq!(metrics.node_count, 3);
        assert_eq!(metrics.edge_count, 2);
        assert_eq!(metrics.active_tip_count, 3);
        assert!((metrics.avg_capacity - 1.5).abs() < 0.001);
    }

    #[test]
    fn test_bidirectional_connection() {
        let mut topo = Topology::new();
        topo.add_tip(NodeId(1));
        topo.add_tip(NodeId(2));
        topo.connect_bidirectional(NodeId(1), NodeId(2), 1.0);

        assert_eq!(topo.edges.len(), 2);

        // Can traverse both directions
        let path1 = topo.shortest_path(NodeId(1), NodeId(2));
        let path2 = topo.shortest_path(NodeId(2), NodeId(1));
        assert!(path1.is_some());
        assert!(path2.is_some());
    }
}