radiate_extensions/architects/node_collections/
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
use radiate::Valid;

use super::GraphIterator;
use crate::node::Node;
use crate::{
    node_collections, schema::collection_type::CollectionType, Direction, NodeCollection,
    NodeFactory, NodeRepairs, NodeType,
};

#[derive(Clone, PartialEq, Default)]
pub struct Graph<T>
where
    T: Clone + PartialEq,
{
    pub nodes: Vec<Node<T>>,
}

impl<T> Graph<T>
where
    T: Clone + PartialEq + Default,
{
    pub fn topological_iter(&self) -> impl Iterator<Item = &Node<T>> {
        GraphIterator::new(self)
    }

    pub fn set_cycles(mut self, indecies: Vec<usize>) -> Graph<T> {
        if indecies.is_empty() {
            let all_indices = self
                .get_nodes()
                .iter()
                .map(|node| node.index)
                .collect::<Vec<usize>>();

            return self.set_cycles(all_indices);
        }

        for idx in indecies {
            let node_cycles = node_collections::get_cycles(self.get_nodes(), idx);

            if node_cycles.is_empty() {
                let node = self.get_mut(idx);
                node.direction = Direction::Forward;
            } else {
                for cycle_idx in node_cycles {
                    let node = self.get_mut(cycle_idx);
                    node.direction = Direction::Backward;
                }
            }
        }

        self
    }
}

impl<T> NodeCollection<T> for Graph<T>
where
    T: Clone + PartialEq + Default,
{
    fn from_nodes(nodes: Vec<Node<T>>) -> Self {
        Graph { nodes }
    }

    fn get(&self, index: usize) -> &Node<T> {
        self.nodes.get(index).unwrap_or_else(|| {
            panic!(
                "Node index {} out of bounds. Graph has {} nodes.",
                index,
                self.nodes.len()
            )
        })
    }

    fn get_mut(&mut self, index: usize) -> &mut Node<T> {
        let length = self.nodes.len();
        self.nodes.get_mut(index).unwrap_or_else(|| {
            panic!(
                "Node index {} out of bounds. Graph has {} nodes.",
                index, length
            )
        })
    }

    fn get_nodes(&self) -> &[Node<T>] {
        &self.nodes
    }

    fn get_nodes_mut(&mut self) -> &mut [Node<T>] {
        &mut self.nodes
    }
}

impl<T> NodeRepairs<T> for Graph<T>
where
    T: Clone + PartialEq + Default,
{
    fn repair(&mut self, factory: Option<&NodeFactory<T>>) -> Self {
        let mut collection = self.clone().set_cycles(Vec::new());

        for node in collection.iter_mut() {
            node.collection_type = Some(CollectionType::Graph);

            if let Some(factory) = factory {
                let temp_node = factory.new_node(node.index, NodeType::Aggregate);

                if node.node_type() == &NodeType::Output && !node.outgoing().is_empty() {
                    node.node_type = NodeType::Aggregate;
                    node.value = temp_node.value.clone();
                } else if node.node_type() == &NodeType::Input && !node.incoming().is_empty() {
                    node.node_type = NodeType::Aggregate;
                    node.value = temp_node.value.clone();
                }
            }
        }

        collection
    }
}

impl<T> Valid for Graph<T>
where
    T: Clone + PartialEq + Default,
{
    fn is_valid(&self) -> bool {
        self.nodes.iter().all(|node| node.is_valid())
    }
}

impl<T> IntoIterator for Graph<T>
where
    T: Clone + PartialEq + Default,
{
    type Item = Node<T>;
    type IntoIter = std::vec::IntoIter<Node<T>>;

    fn into_iter(self) -> Self::IntoIter {
        self.nodes.into_iter()
    }
}

impl<T> std::fmt::Debug for Graph<T>
where
    T: Clone + PartialEq + Default + std::fmt::Debug,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Graph {{\n")?;
        for node in self.get_nodes() {
            write!(f, "  {:?},\n", node)?;
        }
        write!(f, "}}")
    }
}