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
use crate::node::{Node, NodeTrait};
#[derive(Clone, Debug, PartialEq)]
pub struct GraphOptions {
pub min_nodes: usize,
pub max_nodes: usize,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Graph {
pub nodes: Vec<Node>,
pub options: GraphOptions,
}
pub trait GraphOptionsTrait {
fn new() -> Self;
fn validate_graphoptions(self) -> Self;
}
impl GraphOptionsTrait for GraphOptions {
fn new() -> Self {
GraphOptions {
min_nodes: 3,
max_nodes: 5,
}
}
fn validate_graphoptions(self) -> Self {
if self.min_nodes < 3 {
panic!(
"Minimum number of nodes must be at least 3. Current value: {min_nodes}",
min_nodes = self.min_nodes
);
}
if self.max_nodes < self.min_nodes {
panic!(
"Maximum number of nodes must be greater than or equal to the minimum number of nodes. Current values: min_nodes = {min_nodes}, max_nodes = {max_nodes}",
min_nodes = self.min_nodes,
max_nodes = self.max_nodes
);
}
self
}
}
pub trait GraphTrait<N> {
// Creates a new empty graph
fn new_empty(options: Option<GraphOptions>) -> Graph;
// Adds a (validated) node to the graph
fn add_node(self, node: N) -> Self;
// Creates a graph from a vector of (validated) nodes
fn from_nodes(nodes: Vec<N>) -> Self;
// Validate Graph
// fn validate_graph(self) -> Self;
// Create a random graph with with min and max number of nodes
// fn create_random_graph(min_nodes: usize, max_nodes: usize) -> Self;
}
impl GraphTrait<Node> for Graph {
fn new_empty(options: Option<GraphOptions>) -> Graph {
let opts = match options {
Some(options) => options.validate_graphoptions(),
None => GraphOptions::new(),
};
Graph {
nodes: Vec::new(),
options: opts,
}
}
fn add_node(mut self, node: Node) -> Self {
node.validate_node();
self.nodes.push(node);
self
}
fn from_nodes(nodes: Vec<Node>) -> Self {
for node in &nodes {
node.validate_node();
}
Graph {
nodes,
options: GraphOptions::new(),
}
}
// fn create_random_graph(min_nodes: usize, max_nodes: usize) -> Graph {
// }
// fn validate_graph(mut self) -> Self {
// }
}
#[cfg(test)]
mod tests {
use super::*;
use crate::node::NodeKind;
// use proptest::prelude::*;
// proptest! {
// // Test that any valid options (min_nodes >= 3, max_nodes >= min_nodes) successfully create a Graph
// #[test]
// fn test_prop_valid_graph_options(
// min_nodes in 3..100_usize,
// extra in 0..50_usize
// ) {
// let max_nodes = min_nodes + extra;
// println!("Testing with min_nodes = {}, max_nodes = {}", min_nodes, max_nodes); // <-- Add this
// let options = GraphOptions { min_nodes, max_nodes };
// let graph = Graph::new_empty(Some(options.clone()));
// assert_eq!(graph.options.min_nodes, min_nodes);
// assert_eq!(graph.options.max_nodes, max_nodes);
// assert_eq!(graph.nodes.len(), 0);
// }
// }
#[test]
fn test_new_empty_without_options() {
let graph = Graph::new_empty(None);
assert_eq!(graph.nodes.len(), 0);
assert_eq!(graph.options.min_nodes, 3);
assert_eq!(graph.options.max_nodes, 5);
}
#[test]
fn test_new_empty_with_options() {
let options = GraphOptions {
min_nodes: 4,
max_nodes: 6,
};
let graph = Graph::new_empty(Some(options.clone()));
assert_eq!(graph.nodes.len(), 0);
assert_eq!(graph.options.min_nodes, options.min_nodes);
assert_eq!(graph.options.max_nodes, options.max_nodes);
}
#[test]
fn test_add_node() {
let graph = Graph::new_empty(None);
let node = Node {
id: 0,
kind: NodeKind::Input,
inputs: None,
};
let graph = graph.add_node(node.clone());
assert_eq!(graph.nodes.len(), 1);
assert_eq!(graph.nodes[0], node);
}
#[test]
fn test_from_nodes() {
let nodes = vec![
Node {
id: 0,
kind: NodeKind::Input,
inputs: None,
},
Node {
id: 1,
kind: NodeKind::Hidden,
inputs: Some(vec![0]),
},
Node {
id: 2,
kind: NodeKind::Output,
inputs: Some(vec![1]),
},
];
let graph = Graph::from_nodes(nodes.clone());
assert_eq!(graph.nodes.len(), 3);
assert_eq!(graph.nodes[0].id, 0);
assert_eq!(graph.nodes[1].kind, NodeKind::Hidden);
assert_eq!(graph.nodes[2].inputs, Some(vec![1]));
}
}