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
use super::{PhysicalGraph, TopologyGraph};
use crate::{
    core::{Accessability, EdgeId, NodeId, TransitEdge, TransitNode},
    operations::TransitNetworkModifier,
};
use geo::CoordNum;

pub mod repair;

/// Represents a transit network as a graph with transit nodes and edges.
///
/// The struct holds a physical graph and a topological graph which are lower-level representations of the network.
/// `TransitNetwork` provides a higher-level interface to the physical graph and topological graph.
///
/// The struct implements `TransitNetworkModifier` trait for modifying the underlying physical graph.
///
/// # Generics
///
/// `R`: Copyable trait bound. This represents the type of the data associated with the network's routes.
/// `T`: This represents the type of the coordinates used in the network. It's expected to implement `CoordNum` trait.
///
/// # Fields
///
/// * `physical_graph: PhysicalGraph<R, T>` - The physical graph representing the transit network.
/// * `topology_graph: TopologyGraph` - The topological graph representing the transit network.
#[derive(Debug, Clone)]
pub struct TransitNetwork<R: Copy, T: CoordNum> {
    /// The physical graph representing the transit network.
    pub physical_graph: PhysicalGraph<R, T>,
    /// The topological graph representing the transit network.
    pub topology_graph: TopologyGraph,
}

impl<R: Copy, T: CoordNum> PartialEq for TransitNetwork<R, T> {
    fn eq(&self, other: &Self) -> bool {
        self.physical_graph.graph.node_count() == other.physical_graph.graph.node_count()
            && self.topology_graph.graph.edge_count() == other.topology_graph.graph.edge_count()
    }
}

impl<R: Copy, T: CoordNum> TransitNetwork<R, T> {
    /// Constructs a new `TransitNetwork` with an empty `PhysicalGraph` and `TopologyGraph`.
    ///
    /// # Returns
    ///
    /// A new `TransitNetwork` instance.
    pub fn new() -> Self {
        TransitNetwork {
            physical_graph: PhysicalGraph::new(),
            topology_graph: TopologyGraph::new(),
        }
    }

    /// Returns a reference to the `TransitNode` with the given ID.
    pub fn get_edge_by_id(&self, edge_id: EdgeId) -> Option<&TransitEdge<T>> {
        self.physical_graph.get_transit_edge_by_id(edge_id)
    }
}

impl<R: Copy, T: CoordNum> Default for TransitNetwork<R, T> {
    fn default() -> Self {
        Self::new()
    }
}

/// Implementation of `TransitNetworkModifier` trait for `TransitNetwork`.
///
/// This implementation delegates the operations to the underlying physical graph.
impl<R: Copy, T: CoordNum> TransitNetworkModifier<R, T> for TransitNetwork<R, T> {
    /// Adds a `TransitNode` to the physical graph of the network.
    ///
    /// # Arguments
    ///
    /// * `node` - The `TransitNode` to be added to the network.
    ///
    /// # Returns
    ///
    /// * `NodeId` - The ID of the added node.
    fn add_node(&mut self, node: TransitNode<R>) -> NodeId {
        let node_id = node.id;
        self.physical_graph.add_transit_node(node);
        self.topology_graph.add_node(node_id);
        node_id
    }

    /// Adds a `TransitEdge` to the physical graph of the network.
    ///
    /// # Arguments
    ///
    /// * `edge` - The `TransitEdge` to be added to the network.
    fn add_edge(&mut self, edge: TransitEdge<T>) {
        self.physical_graph.add_transit_edge(edge.clone());
        self.topology_graph
            .add_edge(edge.id, edge.source, edge.target);
    }

    fn add_edge_with_accessibility(&mut self, edge: TransitEdge<T>, accessability: Accessability) {
        self.physical_graph.add_transit_edge(edge.clone());
        self.topology_graph.add_edge_with_accessibility(
            edge.id,
            edge.source,
            edge.target,
            accessability,
        );
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use geo::{coord, point, LineString};
    use petgraph::visit::IntoEdgeReferences;

    #[test]
    fn test_transit_network() {
        // Create a new TransitNetwork
        let mut network = TransitNetwork::new();

        // Define some nodes
        let node1 = TransitNode {
            id: 1,
            location: point!(x: 0.0, y: 0.0),
        };

        let node2 = TransitNode {
            id: 2,
            location: point!(x: 1.0, y: 1.0),
        };

        // Add nodes to the network
        let node1_id = network.add_node(node1);
        let node2_id = network.add_node(node2);

        // Check that the nodes were added successfully
        assert_eq!(node1_id, 1);
        assert_eq!(node2_id, 2);

        // Define an edge
        let edge = TransitEdge {
            id: 1,
            source: 1,
            target: 2,
            length: 1.0,
            path: LineString(vec![coord! {x: 0.0, y: 0.0}, coord! {x: 1.0, y: 1.0}]),
        };

        // Add edge to the network
        network.add_edge(edge);

        // Check that the edge was added successfully
        assert_eq!(network.physical_graph.graph.edge_count(), 1);

        // Check that the topology graph was populated correctly
        assert_eq!(network.topology_graph.graph.node_count(), 4);
        assert_eq!(network.topology_graph.graph.edge_count(), 2);
    }

    #[test]
    fn test_transit_network_edge_addition() {
        // Create a new TransitNetwork
        let mut network = TransitNetwork::new();

        // Define some nodes
        let node1 = TransitNode {
            id: 1,
            location: point!(x: 0.0, y: 0.0),
        };

        let node2 = TransitNode {
            id: 2,
            location: point!(x: 1.0, y: 1.0),
        };

        let node3 = TransitNode {
            id: 3,
            location: point!(x: 2.0, y: 2.0),
        };

        // Add nodes to the network
        network.add_node(node1);
        network.add_node(node2);
        network.add_node(node3);

        // Define edges
        let edge1 = TransitEdge {
            id: 1,
            source: 1,
            target: 2,
            length: 1.0,
            path: LineString(vec![coord! {x: 0.0, y: 0.0}, coord! {x: 1.0, y: 1.0}]),
        };

        let edge2 = TransitEdge {
            id: 2,
            source: 2,
            target: 3,
            length: 1.0,
            path: LineString(vec![coord! {x: 0.0, y: 0.0}, coord! {x: 2.0, y: 2.0}]),
        };

        // Add edges to the network
        network.add_edge(edge1);
        network.add_edge(edge2);

        // Check that the edges were added successfully
        assert_eq!(network.physical_graph.graph.edge_count(), 2);

        // Check that the topology graph was populated correctly
        assert_eq!(network.topology_graph.graph.node_count(), 6);
        assert_eq!(network.topology_graph.graph.edge_count(), 4);

        // Check that the topology edges were computed correctly
        let edge_ids: Vec<_> = network
            .topology_graph
            .graph
            .edge_references()
            .map(|edge| edge.weight().edge_id)
            .collect();
        assert_eq!(edge_ids, vec![1, 1, 2, 2]);
    }

    #[test]
    fn test_add_edge_with_accessibility() {
        // Create a new TransitNetwork

        let mut network = TransitNetwork::new();

        // Define some nodes
        let node0 = TransitNode {
            id: 0,
            location: point!(x: 0.0, y: 0.0),
        };

        let node1 = TransitNode {
            id: 1,
            location: point!(x: 1.0, y: 1.0),
        };

        let node2 = TransitNode {
            id: 2,
            location: point!(x: 2.0, y: 2.0),
        };

        let node3 = TransitNode {
            id: 3,
            location: point!(x: 3.0, y: 3.0),
        };

        let node4 = TransitNode {
            id: 4,
            location: point!(x: 4.0, y: 4.0),
        };

        // Add nodes to the network
        network.add_node(node0);
        network.add_node(node1);
        network.add_node(node2);
        network.add_node(node3);
        network.add_node(node4);

        // Define edges
        let edge01 = TransitEdge {
            id: 1,
            source: 0,
            target: 1,
            length: 1.0,
            path: LineString(vec![coord! {x: 0.0, y: 0.0}, coord! {x: 1.0, y: 1.0}]),
        };

        let edge14 = TransitEdge {
            id: 2,
            source: 1,
            target: 4,
            length: 1.0,
            path: LineString(vec![coord! {x: 1.0, y: 1.0}, coord! {x: 4.0, y: 4.0}]),
        };

        let edge12 = TransitEdge {
            id: 3,
            source: 1,
            target: 2,
            length: 1.0,
            path: LineString(vec![coord! {x: 1.0, y: 1.0}, coord! {x: 2.0, y: 2.0}]),
        };

        let edge13 = TransitEdge {
            id: 4,
            source: 1,
            target: 3,
            length: 1.0,
            path: LineString(vec![coord! {x: 1.0, y: 1.0}, coord! {x: 3.0, y: 3.0}]),
        };

        // Add edges to the network
        network.add_edge(edge01);
        network.add_edge(edge14);
        network.add_edge(edge12);
        network.add_edge(edge13);

        // Add edge with accessibility
        let edge40 = TransitEdge {
            id: 5,
            source: 4,
            target: 0,
            length: 1.0,
            path: LineString(vec![coord! {x: 4.0, y: 4.0}, coord! {x: 0.0, y: 0.0}]),
        };

        network.add_edge_with_accessibility(edge40, Accessability::ReachableNodes(vec![2, 3]));

        // Check that the edges were added successfully
        assert_eq!(network.physical_graph.graph.edge_count(), 5);

        // Check that the topology graph was populated correctly
        assert_eq!(network.topology_graph.graph.node_count(), 10);
        assert_eq!(network.topology_graph.graph.edge_count(), 10);

        // Check that the topology edges were computed correctly
        let edge_ids: Vec<_> = network
            .topology_graph
            .graph
            .edge_references()
            .map(|edge| edge.weight().edge_id)
            .collect();
        assert_eq!(edge_ids, vec![1, 1, 2, 2, 3, 3, 4, 4, 5, 5]);
    }

    #[test]
    fn test_default() {
        let network: TransitNetwork<u32, f64> = TransitNetwork::default();

        assert_eq!(network.physical_graph.graph.node_count(), 0);
        assert_eq!(network.physical_graph.graph.edge_count(), 0);
        assert_eq!(network.topology_graph.graph.node_count(), 0);
        assert_eq!(network.topology_graph.graph.edge_count(), 0);
    }
}