xgraph 2.0.0

A comprehensive Rust library providing efficient graph algorithms for solving real-world problems in social network analysis, transportation optimization, recommendation systems, and more
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
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
//! Module for CSV input/output operations on heterogeneous multigraphs.
//!
//! This module provides functionality to serialize and deserialize `HeterogeneousGraph` instances
//! to and from CSV files. It supports saving and loading node and edge data along with their
//! attributes in a structured format. The implementation is feature-gated under `hgraph`.
//!
//! ## CSV Format
//! - **Nodes CSV**: Columns include `node_id`, `data`, and dynamically determined attribute keys.
//! - **Edges CSV**: Columns include `edge_id`, `from`, `to`, `weight`, `data`, and dynamic attributes.
//!
//! ## Features
//! - Available only when the `hgraph` feature is enabled in `Cargo.toml`:
//!   ```toml
//!   [dependencies.xgraph]
//!   version = "x.y.z"
//!   features = ["hgraph"]
//!   ```
//!
//! ## Error Handling
//! Operations return custom `Result` types with detailed errors instead of panicking, allowing
//! users to handle issues gracefully.

use std::fs::File;
use std::io::{self, BufRead, BufReader, Write};

#[cfg(feature = "hgraph")]
use crate::hgraph::h_graph::HeterogeneousGraph;

// --- Error Handling Additions ---

/// Error type for CSV input/output operations.
///
/// Represents various failure modes that can occur during graph serialization or deserialization.
#[cfg(feature = "hgraph")]
#[derive(Debug)]
pub enum CsvIoError {
    /// An I/O error occurred while reading or writing files.
    IoError(io::Error),
    /// The CSV file is malformed or missing required headers.
    InvalidFormat(String),
    /// Failed to parse a field (e.g., node data, weight, or edge data).
    ParseError {
        /// The field that failed to parse.
        field: String,
        /// The value that could not be parsed.
        value: String,
        /// The underlying parse error details.
        details: String,
    },
}

#[cfg(feature = "hgraph")]
impl std::fmt::Display for CsvIoError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            CsvIoError::IoError(e) => write!(f, "I/O error: {}", e),
            CsvIoError::InvalidFormat(msg) => write!(f, "Invalid CSV format: {}", msg),
            CsvIoError::ParseError {
                field,
                value,
                details,
            } => write!(f, "Failed to parse {} '{}': {}", field, value, details),
        }
    }
}

#[cfg(feature = "hgraph")]
impl std::error::Error for CsvIoError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            CsvIoError::IoError(e) => Some(e),
            _ => None,
        }
    }
}

#[cfg(feature = "hgraph")]
impl From<io::Error> for CsvIoError {
    fn from(err: io::Error) -> Self {
        CsvIoError::IoError(err)
    }
}

/// Result type alias for CSV I/O operations.
///
/// Wraps operation results to provide detailed error information via `CsvIoError`.
#[cfg(feature = "hgraph")]
pub type Result<T> = std::result::Result<T, CsvIoError>;

// --- Trait Definition ---

/// A trait for CSV input/output operations on heterogeneous multigraphs.
///
/// This trait defines methods to save a graph to CSV files and load a graph from CSV files.
/// It supports saving and loading node and edge data along with their attributes in a structured format.
///
/// # Type Parameters
/// - `W`: The weight type of the graph edges (e.g., `u32`, `f64`).
/// - `N`: The node data type, must implement `NodeType`.
/// - `E`: The edge data type, must implement `EdgeType`.
///
/// # Requirements
/// - For `save_to_csv`: `W`, `N`, and `E` must implement `Display`.
/// - For `load_from_csv`: `W`, `N`, and `E` must implement `FromStr` and `Default`, with debuggable parse errors.
///
/// # Features
/// This trait is only available when the `hgraph` feature is enabled.
#[cfg(feature = "hgraph")]
pub trait CsvIO<W, N, E> {
    /// Saves the graph to CSV files.
    ///
    /// Writes the graph's nodes and edges to two separate CSV files:
    /// - `nodes_file`: Contains node IDs, their data (via `as_string`), and attributes as columns.
    /// - `edges_file`: Contains edge IDs, source and target node IDs, weights, edge data (via `as_string`), and attributes as columns.
    ///
    /// Attributes are dynamically determined from the graph and written as additional columns.
    /// Missing attributes for a node or edge are represented as empty strings.
    ///
    /// # Arguments
    /// - `nodes_file`: The path to the file where nodes will be saved.
    /// - `edges_file`: The path to the file where edges will be saved.
    ///
    /// # Returns
    /// A `Result<()>` indicating success or failure with a `CsvIoError` on error.
    ///
    /// # Examples
    /// ```rust
    /// #[cfg(feature = "hgraph")]
    /// {
    ///     use crate::xgraph::hgraph::h_graph::HeterogeneousGraph;
    ///     use crate::xgraph::hgraph::hcsv_io::CsvIO;
    ///     let mut graph = HeterogeneousGraph::<f64, String, String>::new(false);
    ///     let n1 = graph.add_node("Alice".to_string());
    ///     let n2 = graph.add_node("Bob".to_string());
    ///     graph.add_edge(n1, n2, 1.5, "friend".to_string()).unwrap();
    ///     match graph.save_to_csv("nodes.csv", "edges.csv") {
    ///         Ok(()) => println!("Graph saved successfully"),
    ///         Err(e) => println!("Failed to save graph: {}", e),
    ///     }
    /// }
    /// ```
    fn save_to_csv(&self, nodes_file: &str, edges_file: &str) -> Result<()>
    where
        W: Copy + PartialEq + std::fmt::Display,
        N: Clone + std::fmt::Debug + std::fmt::Display,
        E: Clone + std::fmt::Debug + std::fmt::Display;

    /// Loads a graph from CSV files.
    ///
    /// Reads a graph from two CSV files:
    /// - `nodes_file`: Contains node IDs, data, and attributes.
    /// - `edges_file`: Contains edge IDs, source and target node IDs, weights, edge data, and attributes.
    ///
    /// The node and edge data are parsed from the CSV using `FromStr`. Attributes are loaded dynamically based on the CSV headers.
    ///
    /// # Arguments
    /// - `nodes_file`: The path to the file containing nodes.
    /// - `edges_file`: The path to the file containing edges.
    /// - `directed`: A boolean indicating whether the loaded graph should be directed.
    ///
    /// # Returns
    /// A `Result<Self>` containing the loaded graph or a `CsvIoError` on failure.
    ///
    /// # Examples
    /// ```rust
    /// #[cfg(feature = "hgraph")]
    /// {
    ///     use crate::xgraph::hgraph::h_graph::HeterogeneousGraph;
    ///     use crate::xgraph::hgraph::hcsv_io::CsvIO;
    ///     match HeterogeneousGraph::<f64, String, String>::load_from_csv("nodes.csv", "edges.csv", false) {
    ///         Ok(graph) => println!("Loaded graph with {} nodes", graph.nodes.len()),
    ///         Err(e) => println!("Failed to load graph: {}", e),
    ///     }
    /// }
    /// ```
    fn load_from_csv(nodes_file: &str, edges_file: &str, directed: bool) -> Result<Self>
    where
        Self: Sized,
        W: Copy + PartialEq + Default + std::str::FromStr,
        N: Clone + std::fmt::Debug + std::str::FromStr,
        E: Clone + std::fmt::Debug + Default + std::str::FromStr,
        <W as std::str::FromStr>::Err: std::fmt::Debug,
        <N as std::str::FromStr>::Err: std::fmt::Debug,
        <E as std::str::FromStr>::Err: std::fmt::Debug;
}

// --- Implementation ---

#[cfg(feature = "hgraph")]
impl<W, N, E> CsvIO<W, N, E> for HeterogeneousGraph<W, N, E>
where
    W: Copy + PartialEq + std::fmt::Debug,
    N: crate::hgraph::h_node::NodeType,
    E: crate::hgraph::h_edge::EdgeType + Default,
{
    fn save_to_csv(&self, nodes_file: &str, edges_file: &str) -> Result<()>
    where
        W: std::fmt::Display,
        N: std::fmt::Display,
        E: std::fmt::Display,
    {
        // --- Save Nodes ---
        let mut nodes_writer = File::create(nodes_file).map_err(CsvIoError::IoError)?;

        // Collect and sort unique node attribute keys
        let mut node_attrs: Vec<String> = self
            .nodes
            .iter()
            .flat_map(|(_, node)| node.attributes.keys())
            .collect::<std::collections::HashSet<_>>()
            .into_iter()
            .cloned()
            .collect();
        node_attrs.sort();

        // Write header
        writeln!(nodes_writer, "node_id,data,{}", node_attrs.join(","))
            .map_err(CsvIoError::IoError)?;

        // Write node data and attributes
        for (id, node) in self.nodes.iter() {
            let attrs: Vec<String> = node_attrs
                .iter()
                .map(|key| {
                    node.attributes
                        .get(key)
                        .map_or("".to_string(), |v| v.to_string())
                })
                .collect();
            writeln!(
                nodes_writer,
                "{},{},{}",
                id,
                node.data.as_string(),
                attrs.join(",")
            )
            .map_err(CsvIoError::IoError)?;
        }

        // --- Save Edges ---
        let mut edges_writer = File::create(edges_file).map_err(CsvIoError::IoError)?;

        // Collect and sort unique edge attribute keys
        let mut edge_attrs: Vec<String> = self
            .edges
            .iter()
            .flat_map(|(_, edge)| edge.attributes.keys())
            .collect::<std::collections::HashSet<_>>()
            .into_iter()
            .cloned()
            .collect();
        edge_attrs.sort();

        // Write header
        writeln!(
            edges_writer,
            "edge_id,from,to,weight,data,{}",
            edge_attrs.join(",")
        )
        .map_err(CsvIoError::IoError)?;

        // Write edge data and attributes
        for (id, edge) in self.edges.iter() {
            let attrs: Vec<String> = edge_attrs
                .iter()
                .map(|key| {
                    edge.attributes
                        .get(key)
                        .map_or("".to_string(), |v| v.to_string())
                })
                .collect();
            writeln!(
                edges_writer,
                "{},{},{},{},{},{}",
                id,
                edge.from,
                edge.to,
                edge.weight,
                edge.data.as_string(),
                attrs.join(",")
            )
            .map_err(CsvIoError::IoError)?;
        }

        Ok(())
    }

    fn load_from_csv(nodes_file: &str, edges_file: &str, directed: bool) -> Result<Self>
    where
        Self: Sized,
        W: Default + std::str::FromStr,
        N: std::str::FromStr,
        E: std::str::FromStr,
        <W as std::str::FromStr>::Err: std::fmt::Debug,
        <N as std::str::FromStr>::Err: std::fmt::Debug,
        <E as std::str::FromStr>::Err: std::fmt::Debug,
    {
        let mut graph = HeterogeneousGraph::new(directed);

        // --- Load Nodes ---
        let nodes_reader = BufReader::new(File::open(nodes_file).map_err(CsvIoError::IoError)?);
        let mut lines = nodes_reader.lines();

        // Parse header
        let header = lines
            .next()
            .ok_or(CsvIoError::InvalidFormat("Empty nodes file".to_string()))?
            .map_err(CsvIoError::IoError)?;
        let attr_keys: Vec<&str> = header.split(',').skip(2).collect();
        if header.is_empty() || !header.starts_with("node_id,data") {
            return Err(CsvIoError::InvalidFormat(
                "Nodes CSV must start with 'node_id,data'".to_string(),
            ));
        }

        // Parse nodes
        for (line_num, line) in lines.enumerate() {
            let line = line.map_err(CsvIoError::IoError)?;
            let parts: Vec<&str> = line.split(',').collect();
            if parts.len() < 2 {
                return Err(CsvIoError::InvalidFormat(format!(
                    "Line {} in nodes CSV has too few fields",
                    line_num + 2
                )));
            }
            let _node_id: usize = parts[0].parse().unwrap_or_default(); // Ignored, new ID assigned
            let data = parts[1].parse().map_err(|e| CsvIoError::ParseError {
                field: "node data".to_string(),
                value: parts[1].to_string(),
                details: format!("{:?}", e),
            })?;
            let node = graph.add_node(data);

            // Load attributes
            for (key, value) in attr_keys.iter().zip(parts.iter().skip(2)) {
                if !value.is_empty() {
                    graph.set_node_attribute(node, key.to_string(), value.to_string());
                }
            }
        }

        // --- Load Edges ---
        let edges_reader = BufReader::new(File::open(edges_file).map_err(CsvIoError::IoError)?);
        let mut lines = edges_reader.lines();

        // Parse header
        let header = lines
            .next()
            .ok_or(CsvIoError::InvalidFormat("Empty edges file".to_string()))?
            .map_err(CsvIoError::IoError)?;
        let attr_keys: Vec<&str> = header.split(',').skip(5).collect();
        if header.is_empty() || !header.starts_with("edge_id,from,to,weight,data") {
            return Err(CsvIoError::InvalidFormat(
                "Edges CSV must start with 'edge_id,from,to,weight,data'".to_string(),
            ));
        }

        // Parse edges
        for (line_num, line) in lines.enumerate() {
            let line = line.map_err(CsvIoError::IoError)?;
            let parts: Vec<&str> = line.split(',').collect();
            if parts.len() < 5 {
                return Err(CsvIoError::InvalidFormat(format!(
                    "Line {} in edges CSV has too few fields",
                    line_num + 2
                )));
            }
            let _edge_id: usize = parts[0].parse().unwrap_or_default(); // Ignored, new ID assigned
            let from: usize = parts[1].parse().unwrap_or_default();
            let to: usize = parts[2].parse().unwrap_or_default();
            let weight = parts[3].parse().map_err(|e| CsvIoError::ParseError {
                field: "weight".to_string(),
                value: parts[3].to_string(),
                details: format!("{:?}", e),
            })?;
            let data = parts[4].parse().map_err(|e| CsvIoError::ParseError {
                field: "edge data".to_string(),
                value: parts[4].to_string(),
                details: format!("{:?}", e),
            })?;

            let edge_id = graph.add_edge(from, to, weight, data).unwrap_or_default(); // Assuming add_edge returns Result<usize, _>

            // Load attributes
            for (key, value) in attr_keys.iter().zip(parts.iter().skip(5)) {
                if !value.is_empty() {
                    graph.set_edge_attribute(edge_id, key.to_string(), value.to_string());
                }
            }
        }

        Ok(graph)
    }
}

// --- Tests ---

#[cfg(test)]
#[cfg(feature = "hgraph")]
mod tests {
    use super::*;
    use crate::hgraph::h_edge::EdgeType;
    use crate::hgraph::h_node::NodeType;
    use std::fmt;
    use std::str::FromStr;

    /// A simple node type for testing, representing a labeled entity.
    #[derive(Clone, Eq, PartialEq, Hash, Debug)]
    struct TestNode(String);

    impl NodeType for TestNode {
        fn as_string(&self) -> String {
            self.0.clone()
        }
    }

    impl fmt::Display for TestNode {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(f, "{}", self.0)
        }
    }

    impl FromStr for TestNode {
        type Err = String;
        fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
            Ok(TestNode(s.to_string()))
        }
    }

    /// A simple edge type for testing, representing a connection type.
    #[derive(Clone, Debug, Default)]
    struct TestEdge(String);

    impl EdgeType for TestEdge {
        fn as_string(&self) -> String {
            self.0.clone()
        }
    }

    impl fmt::Display for TestEdge {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(f, "{}", self.0)
        }
    }

    impl FromStr for TestEdge {
        type Err = String;
        fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
            Ok(TestEdge(s.to_string()))
        }
    }

    /// Tests basic CSV save and load functionality for a simple graph.
    #[test]
    fn test_csv_io_basic() {
        let mut graph = HeterogeneousGraph::<u32, TestNode, TestEdge>::new(false);
        let n1 = graph.add_node(TestNode("A".to_string()));
        let n2 = graph.add_node(TestNode("B".to_string()));
        let edge_id = graph
            .add_edge(n1, n2, 1, TestEdge("edge".to_string()))
            .unwrap();
        graph.set_node_attribute(n1, "color".to_string(), "red".to_string());
        graph.set_edge_attribute(edge_id, "type".to_string(), "road".to_string());

        assert!(graph
            .save_to_csv("test_nodes_basic.csv", "test_edges_basic.csv")
            .is_ok());
        let loaded_graph = HeterogeneousGraph::<u32, TestNode, TestEdge>::load_from_csv(
            "test_nodes_basic.csv",
            "test_edges_basic.csv",
            false,
        )
        .unwrap();

        assert_eq!(graph.nodes.len(), loaded_graph.nodes.len());
        assert_eq!(graph.edges.len(), loaded_graph.edges.len());
        assert_eq!(
            graph.get_node_data(n1).unwrap().as_string(),
            loaded_graph.get_node_data(0).unwrap().as_string()
        );
        assert_eq!(
            graph.get_edge_data_by_id(edge_id).unwrap().as_string(),
            loaded_graph.get_edge_data_by_id(0).unwrap().as_string()
        );
    }

    /// Tests handling of multiple edges between the same nodes (multigraph behavior).
    #[test]
    fn test_csv_io_multigraph() {
        let mut graph = HeterogeneousGraph::<f64, TestNode, TestEdge>::new(true);
        let n1 = graph.add_node(TestNode("X".to_string()));
        let n2 = graph.add_node(TestNode("Y".to_string()));
        let edge1 = graph
            .add_edge(n1, n2, 1.5, TestEdge("friend".to_string()))
            .unwrap();
        let edge2 = graph
            .add_edge(n1, n2, 2.0, TestEdge("colleague".to_string()))
            .unwrap();
        graph.set_node_attribute(n1, "type".to_string(), "person".to_string());
        graph.set_edge_attribute(edge1, "strength".to_string(), "strong".to_string());
        graph.set_edge_attribute(edge2, "strength".to_string(), "weak".to_string());

        assert!(graph
            .save_to_csv("test_nodes_multi.csv", "test_edges_multi.csv")
            .is_ok());
        let loaded_graph = HeterogeneousGraph::<f64, TestNode, TestEdge>::load_from_csv(
            "test_nodes_multi.csv",
            "test_edges_multi.csv",
            true,
        )
        .unwrap();

        assert_eq!(graph.nodes.len(), loaded_graph.nodes.len());
        assert_eq!(graph.edges.len(), loaded_graph.edges.len());
        assert_eq!(loaded_graph.get_edges_between(0, 1).len(), 2);
    }

    /// Tests error handling for invalid CSV files.
    #[test]
    fn test_csv_io_invalid_file() {
        let result = HeterogeneousGraph::<u32, TestNode, TestEdge>::load_from_csv(
            "nonexistent_nodes.csv",
            "nonexistent_edges.csv",
            false,
        );
        assert!(matches!(result, Err(CsvIoError::IoError(_))));

        std::fs::write("empty_nodes.csv", "").unwrap();
        let result = HeterogeneousGraph::<u32, TestNode, TestEdge>::load_from_csv(
            "empty_nodes.csv",
            "nonexistent_edges.csv",
            false,
        );
        assert!(matches!(result, Err(CsvIoError::InvalidFormat(_))));
    }
}