-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraph.rs
396 lines (351 loc) Β· 16.1 KB
/
graph.rs
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
use crate::{Connection, InputId, Node, NodeId, OutputId};
use std::{
collections::{HashMap, LinkedList},
fmt,
};
/// Processing graph consisting of nodes and connections.
pub struct Graph<N: Node> {
/// Connections in graph.
connections: Vec<Connection>,
/// Internal counter for next node id.
next_node_id: NodeId,
/// Nodes in graph, indexed by unique id.
nodes: HashMap<NodeId, N>,
/// Node processing order (result of topologial sort).
processing_order: LinkedList<NodeId>,
}
impl<N: Node> Graph<N> {
/// Creates new empty graph.
pub fn new() -> Self {
Graph {
connections: Vec::new(),
next_node_id: NodeId(0),
nodes: HashMap::new(),
processing_order: LinkedList::new(),
}
}
/// Adds a connection to the graph.
pub fn add_connection(&mut self, connection: Connection) -> Result<Connection, GraphError> {
// Validate connection and check whether input is free.
let connection = self.validate_connection(connection)?;
if self
.connections
.iter()
.find(|c| c.target_node == connection.target_node && c.target_input == connection.target_input)
.is_some()
{
return Err(GraphError::InputAlreadyConnected(connection.target_node, connection.target_input));
}
// Add connection, update processing order (check for undelayed cycles).
self.connections.push(connection);
match self.calc_processing_order() {
Err(error) => {
// Revert change (most likely an undelayed cycle was introduced).
self.connections.pop();
Err(error)
}
Ok(order) => {
self.processing_order = order;
Ok(connection)
}
}
}
/// Adds a node to the graph.
pub fn add_node(&mut self, node: N) -> NodeId {
let id = self.next_node_id;
self.nodes.insert(id, node);
self.processing_order = self.calc_processing_order().unwrap();
self.next_node_id.0 += 1;
id
}
/// Determines processing order (new topological sorting, can fail due to undelayed cycles).
fn calc_processing_order(&self) -> Result<LinkedList<NodeId>, GraphError> {
// Calculate in-degree of nodes.
let mut in_degree: HashMap<NodeId, usize> = HashMap::new();
for &node in self.nodes.keys() {
in_degree.insert(node, 0);
}
for connection in self.connections.iter() {
// Nodes do not depend on nodes that introduce delay.
if !self.get_node(connection.source_node).unwrap().delayed_processing() {
in_degree.entry(connection.target_node).and_modify(|d| *d += 1);
}
}
// Find nodes with in-degree 0.
let mut queue: LinkedList<NodeId> = LinkedList::new();
for (&node, °ree) in in_degree.iter() {
if degree == 0 {
queue.push_back(node);
}
}
// Topological sort.
let mut order: LinkedList<NodeId> = LinkedList::new();
let mut delayed: LinkedList<NodeId> = LinkedList::new();
while !queue.is_empty() {
let node = queue.pop_front().unwrap();
if !self.get_node(node).unwrap().delayed_processing() {
// Reduce in-degree of connected nodes, add to queue once in-degree == 0.
for connection in self.connections.iter() {
if connection.source_node == node {
in_degree.entry(connection.target_node).and_modify(|d| *d -= 1);
if *in_degree.get(&connection.target_node).unwrap() == 0 {
queue.push_back(connection.target_node);
}
}
}
order.push_back(node);
} else {
delayed.push_back(node);
}
}
// Nodes that introduce delay are processed after all other nodes.
while !delayed.is_empty() {
order.push_back(delayed.pop_front().unwrap());
}
// Number of nodes in order won't match if an undelayed cycle exists.
if order.len() != self.nodes.len() {
return Err(GraphError::CycleWithoutDelay);
}
Ok(order)
}
/// Returns a node by id.
pub fn get_node(&self, id: NodeId) -> Result<&N, GraphError> {
self.nodes.get(&id).ok_or(GraphError::NodeNotExists(id))
}
/// Returns a mutable node by id.
pub fn get_node_mut(&mut self, id: NodeId) -> Result<&mut N, GraphError> {
self.nodes.get_mut(&id).ok_or(GraphError::NodeNotExists(id))
}
/// Returns iterator over nodes.
pub fn iter_nodes(&self) -> impl Iterator<Item = (&NodeId, &N)> {
self.nodes.iter()
}
/// Returns mutable iterator over nodes.
pub fn iter_nodes_mut(&mut self) -> impl Iterator<Item = (&NodeId, &mut N)> {
self.nodes.iter_mut()
}
/// Processes nodes in graph.
pub fn process(&mut self) {
// First pass.
for &node in self.processing_order.iter() {
// Populate inputs.
for connection in self.connections.iter() {
if connection.target_node == node {
let value = self.nodes.get(&connection.source_node).unwrap().get_output(connection.source_output);
self.nodes.get_mut(&connection.target_node).unwrap().set_input(connection.target_input, value);
}
}
// Process non-delayed nodes.
let node = self.nodes.get_mut(&node).unwrap();
if !node.delayed_processing() {
node.process();
}
}
// Second pass.
for &node in self.processing_order.iter() {
// Process delayed nodes.
let node = self.nodes.get_mut(&node).unwrap();
if node.delayed_processing() {
node.process();
}
}
}
/// Removes a connection.
pub fn remove_connection(&mut self, connection: Connection) -> Result<Connection, GraphError> {
if self.connections.contains(&connection) {
self.connections.retain(|&c| c != connection);
self.processing_order = self.calc_processing_order().unwrap();
Ok(connection)
} else {
Err(GraphError::ConnectionNotExists(connection))
}
}
/// Removes a node by id.
pub fn remove_node(&mut self, id: NodeId) -> Result<N, GraphError> {
let node = self.nodes.remove(&id).ok_or(GraphError::NodeNotExists(id))?;
self.connections = self.connections.iter().cloned().filter(|&c| self.validate_connection(c).is_ok()).collect();
self.processing_order = self.calc_processing_order().unwrap();
Ok(node)
}
/// Validates a connection (whether nodes and input/output exist).
fn validate_connection(&self, connection: Connection) -> Result<Connection, GraphError> {
let source = self.get_node(connection.source_node)?;
let target = self.get_node(connection.target_node)?;
if !source.list_outputs().contains(&connection.source_output) {
return Err(GraphError::OutputNotExists(connection.source_node, connection.source_output));
}
if !target.list_inputs().contains(&connection.target_input) {
return Err(GraphError::InputNotExists(connection.target_node, connection.target_input));
}
Ok(connection)
}
}
/// Graph error type.
#[derive(PartialEq)]
pub enum GraphError {
ConnectionNotExists(Connection),
CycleWithoutDelay,
InputAlreadyConnected(NodeId, InputId),
InputNotExists(NodeId, InputId),
NodeNotExists(NodeId),
OutputNotExists(NodeId, OutputId),
}
impl fmt::Debug for GraphError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
GraphError::ConnectionNotExists(connection) => write!(f, "{:?} does not exist in graph.", connection),
GraphError::CycleWithoutDelay => write!(f, "Graph contains a cycle without delay."),
GraphError::InputAlreadyConnected(node, input) => {
write!(f, "Input with id {} on node with id {} is already connected.", input.0, node.0)
}
GraphError::InputNotExists(node, input) => {
write!(f, "Input with id {} does not exist on node with id {}.", input.0, node.0)
}
GraphError::NodeNotExists(node) => write!(f, "Node with id {} does not exist in graph.", node.0),
GraphError::OutputNotExists(node, output) => {
write!(f, "Output with id {} does not exist on node with id {}.", output.0, node.0)
}
}
}
}
/// Unit tests.
#[cfg(test)]
mod tests {
use super::*;
use crate::nodes;
#[test]
fn add_connection() {
let mut graph: Graph<Box<dyn Node>> = Graph::new();
let node0 = graph.add_node(Box::from(nodes::Variable::new(1.0)));
let node1 = graph.add_node(Box::from(nodes::Variable::new(2.0)));
assert_eq!(graph.connections.len(), 0);
graph.add_connection(Connection::new(node0, OutputId(0), node1, InputId(0))).unwrap();
assert_eq!(graph.connections.len(), 1);
// Invalid connections.
assert_eq!(
graph.add_connection(Connection::new(node0, OutputId(0), node1, InputId(0))),
Err(GraphError::InputAlreadyConnected(node1, InputId(0)))
);
assert_eq!(
graph.add_connection(Connection::new(node1, OutputId(0), node0, InputId(0))),
Err(GraphError::CycleWithoutDelay)
);
assert_eq!(
graph.add_connection(Connection::new(node0, OutputId(0), node1, InputId(1))),
Err(GraphError::InputNotExists(node1, InputId(1)))
);
assert_eq!(
graph.add_connection(Connection::new(NodeId(2), OutputId(0), node1, InputId(0))),
Err(GraphError::NodeNotExists(NodeId(2)))
);
assert_eq!(
graph.add_connection(Connection::new(node0, OutputId(1), node1, InputId(0))),
Err(GraphError::OutputNotExists(node0, OutputId(1)))
);
assert_eq!(graph.connections.len(), 1);
}
#[test]
fn add_node() {
let mut graph: Graph<Box<dyn Node>> = Graph::new();
assert_eq!(graph.nodes.len(), 0);
let node0 = graph.add_node(Box::from(nodes::Variable::new(1.0)));
assert_eq!(graph.nodes.len(), 1);
assert_eq!(node0, NodeId(0));
let node1 = graph.add_node(Box::from(nodes::Variable::new(2.0)));
assert_eq!(graph.nodes.len(), 2);
assert_eq!(node1, NodeId(1));
}
#[test]
fn processing_order() {
let mut graph: Graph<Box<dyn Node>> = Graph::new();
let var0 = graph.add_node(Box::from(nodes::Variable::new(1.0)));
let var1 = graph.add_node(Box::from(nodes::Variable::new(2.0)));
graph.add_connection(Connection::new(var0, OutputId(0), var1, InputId(0))).unwrap();
assert_eq!(graph.processing_order.iter().cloned().collect::<Vec<NodeId>>(), vec![var0, var1]);
let add2 = graph.add_node(Box::from(nodes::Addition::new()));
graph.add_connection(Connection::new(var0, OutputId(0), add2, InputId(0))).unwrap();
graph.add_connection(Connection::new(var1, OutputId(0), add2, InputId(1))).unwrap();
assert_eq!(graph.processing_order.iter().cloned().collect::<Vec<NodeId>>(), vec![var0, var1, add2]);
let var3 = graph.add_node(Box::from(nodes::Variable::new(3.0)));
graph.add_connection(Connection::new(var3, OutputId(0), var0, InputId(0))).unwrap();
assert_eq!(graph.processing_order.iter().cloned().collect::<Vec<NodeId>>(), vec![var3, var0, var1, add2]);
let delay4 = graph.add_node(Box::from(nodes::Delay::new()));
let add5 = graph.add_node(Box::from(nodes::Addition::new()));
graph.add_connection(Connection::new(add5, OutputId(0), delay4, InputId(0))).unwrap();
graph.add_connection(Connection::new(delay4, OutputId(0), add5, InputId(0))).unwrap();
graph.add_connection(Connection::new(add2, OutputId(0), add5, InputId(1))).unwrap();
assert_eq!(
graph.processing_order.iter().cloned().collect::<Vec<NodeId>>(),
vec![var3, var0, var1, add2, add5, delay4]
);
}
#[test]
fn get_node() {
let mut graph: Graph<Box<dyn Node>> = Graph::new();
assert_eq!(graph.get_node(NodeId(0)).err(), Some(GraphError::NodeNotExists(NodeId(0))));
assert_eq!(graph.get_node_mut(NodeId(0)).err(), Some(GraphError::NodeNotExists(NodeId(0))));
graph.add_node(Box::from(nodes::Variable::new(1.0)));
assert_eq!(graph.get_node(NodeId(0)).map(|n| n.get_output(OutputId(0))), Ok(1.0));
assert_eq!(graph.get_node_mut(NodeId(0)).map(|n| n.get_output(OutputId(0))), Ok(1.0));
}
#[test]
fn iter_node() {
let mut graph: Graph<Box<dyn Node>> = Graph::new();
let var0 = graph.add_node(Box::from(nodes::Variable::new(1.0)));
let var1 = graph.add_node(Box::from(nodes::Variable::new(2.0)));
let var2 = graph.add_node(Box::from(nodes::Variable::new(3.0)));
assert!(graph.iter_nodes().find(|(&id, _)| id == var0).is_some());
assert!(graph.iter_nodes().find(|(&id, _)| id == var1).is_some());
assert!(graph.iter_nodes_mut().find(|(&id, _)| id == var2).is_some());
}
#[test]
fn process() {
let mut graph: Graph<Box<dyn Node>> = Graph::new();
let var0 = graph.add_node(Box::from(nodes::Variable::new(1.0)));
let add1 = graph.add_node(Box::from(nodes::Addition::new()));
let del2 = graph.add_node(Box::from(nodes::Delay::new()));
graph.add_connection(Connection::new(var0, OutputId(0), add1, InputId(0))).unwrap();
graph.add_connection(Connection::new(add1, OutputId(0), del2, InputId(0))).unwrap();
graph.add_connection(Connection::new(del2, OutputId(0), add1, InputId(1))).unwrap();
assert_eq!(graph.processing_order.iter().cloned().collect::<Vec<NodeId>>(), vec![var0, add1, del2]);
assert_eq!(graph.get_node(add1).unwrap().get_output(OutputId(0)), 0.0);
graph.process();
assert_eq!(graph.get_node(add1).unwrap().get_output(OutputId(0)), 1.0);
graph.process();
assert_eq!(graph.get_node(add1).unwrap().get_output(OutputId(0)), 2.0);
graph.process();
assert_eq!(graph.get_node(add1).unwrap().get_output(OutputId(0)), 3.0);
}
#[test]
fn remove_connection() {
let mut graph: Graph<Box<dyn Node>> = Graph::new();
let node0 = graph.add_node(Box::from(nodes::Variable::new(1.0)));
let node1 = graph.add_node(Box::from(nodes::Variable::new(2.0)));
let node2 = graph.add_node(Box::from(nodes::Variable::new(3.0)));
let conn0 = Connection::new(node2, OutputId(0), node1, InputId(0));
let conn1 = Connection::new(node1, OutputId(0), node0, InputId(0));
graph.add_connection(conn0).unwrap();
graph.add_connection(conn1).unwrap();
assert_eq!(graph.connections.len(), 2);
assert_eq!(graph.remove_connection(conn1), Ok(conn1));
assert_eq!(graph.connections.len(), 1);
assert_eq!(graph.remove_connection(conn1), Err(GraphError::ConnectionNotExists(conn1)));
}
#[test]
fn remove_node() {
let mut graph: Graph<Box<dyn Node>> = Graph::new();
let node0 = graph.add_node(Box::from(nodes::Variable::new(1.0)));
let node1 = graph.add_node(Box::from(nodes::Variable::new(2.0)));
let node2 = graph.add_node(Box::from(nodes::Variable::new(3.0)));
assert_eq!(graph.nodes.len(), 3);
let conn0 = Connection::new(node2, OutputId(0), node1, InputId(0));
let conn1 = Connection::new(node1, OutputId(0), node0, InputId(0));
graph.add_connection(conn0).unwrap();
graph.add_connection(conn1).unwrap();
assert_eq!(graph.connections.len(), 2);
assert_eq!(graph.remove_node(node1).map(|n| n.get_output(OutputId(0))), Ok(2.0));
assert_eq!(graph.nodes.len(), 2);
assert_eq!(graph.connections.len(), 0);
assert_eq!(graph.remove_node(node1).err(), Some(GraphError::NodeNotExists(node1)));
}
}