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
/*
 * Copyright [2022] [Kevin Velasco]
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

use crate::graph::node::Node;

use edge::Edge;
use std::collections::{HashMap, HashSet};
use std::fmt::Display;

use dot::{Id, Nodes};
use std::rc::Rc;

use crate::extract::{Edges, ExtractData, Label, Query};

use crate::graph::traversal::GraphTraversal;
use crate::identify::{Identifiable, Identity};

pub mod edge;
pub mod node;

pub mod traversal;

pub type Graph<T> = ConcreteGraph<T>;

pub trait GraphDefinition {
    type Id: Identity;
    type Label: Display + Clone;
    type EdgeMeta;
    type NodeData: 'static;

    fn build_graph<V, Q>(source: &Q) -> ConcreteGraph<Self>
    where
        Q: Query<Self::Id, V>,
        V: Identifiable<Self::Id>
            + Edges<Self::Id, Self::EdgeMeta>
            + ExtractData<Self::NodeData>
            + Label<Label = Self::Label>,
        Self: Sized,
    {
        let source = source.create_index();

        let mut top_level_edges = source.all().into_iter().map(|value| value.get_id());

        let mut output = HashMap::new();
        let mut queue: Vec<Self::Id> = vec![];
        let mut current: Option<Self::Id> = top_level_edges.next();
        let mut seen_set = HashSet::new();
        while let Some(current_identity) = current.take() {
            // get next edges
            if let Some(next) = queue.pop() {
                current = Some(next);
            } else if let Some(id) = top_level_edges.next() {
                current = Some(id);
            } else {
                current = None;
            }

            if seen_set.contains(&current_identity) {
                continue;
            } else {
                seen_set.insert(current_identity.clone());
            }

            let value: &V = source
                .query(&current_identity)
                .expect("Created a value that doesn't exist in the set");
            let current_node = output
                .entry(current_identity.clone() as Self::Id)
                .or_insert_with(|| {
                    Node::new(
                        current_identity.clone(),
                        value.label(),
                        value.extract_data(),
                    )
                })
                .clone();

            let edge_iterator = value.edges();

            for edge in edge_iterator {
                // add the value to the queue
                queue.push(edge.sink.clone());

                let node = output
                    .entry(edge.sink.clone() as Self::Id)
                    .or_insert_with(|| {
                        let data = source.query(&edge.sink);

                        assert!(
                            data.is_some(),
                            "Created node that does not exist {}",
                            edge.sink.clone()
                        );
                        let data = data.unwrap();
                        Node::new(edge.sink, data.label(), data.extract_data())
                    })
                    .clone();

                current_node.insert_edge(node.clone(), edge.meta);
            }
        }
        output.into_values().collect()
    }
}

pub struct SimpleGraphDefinition;
impl GraphDefinition for SimpleGraphDefinition {
    type Id = i32;
    type Label = Self::Id;
    type EdgeMeta = ();
    type NodeData = ();
}

trait AbstractGraph {
    type Definition: GraphDefinition;
}

pub struct ConcreteGraph<T>
where
    T: GraphDefinition,
{
    pub(crate) nodes: HashMap<T::Id, Node<T>>,
}

impl<T> ConcreteGraph<T>
where
    T: GraphDefinition,
{
    pub fn all_nodes(&self) -> impl Iterator<Item = Node<T>> + '_ {
        self.nodes.values().cloned()
    }

    // all of the edges in the entire graph
    pub fn all_edges(&self) -> impl Iterator<Item = Edge<T>> + '_ {
        self.nodes
            .values()
            .flat_map(|node| node.get_edges().into_iter())
    }

    pub fn order(&self) -> usize {
        self.nodes.len()
    }
}

impl<T: GraphDefinition> Default for ConcreteGraph<T> {
    fn default() -> Self {
        Self {
            nodes: Default::default(),
        }
    }
}

impl<T> FromIterator<Node<T>> for ConcreteGraph<T>
where
    T: GraphDefinition,
{
    fn from_iter<I: IntoIterator<Item = Node<T>>>(iter: I) -> Self {
        let mut map = HashMap::default();

        for node in iter {
            map.insert(node.get_id(), node);
        }

        Self { nodes: map }
    }
}

impl<'a, T: GraphDefinition> IntoIterator for &'a ConcreteGraph<T> {
    type Item = Node<T>;
    type IntoIter = <Vec<Self::Item> as IntoIterator>::IntoIter;

    fn into_iter(self) -> Self::IntoIter {
        let nodes: Vec<_> = self.nodes.values().cloned().collect();
        nodes.into_iter()
    }
}

impl<T> Query<T::Id, Node<T>> for ConcreteGraph<T>
where
    T: GraphDefinition,
{
    fn query(&self, identifier: &T::Id) -> Option<&Node<T>> {
        self.nodes.get(identifier)
    }

    fn all(&self) -> Vec<&Node<T>> {
        self.nodes.values().collect()
    }
}

impl<T> ConcreteGraph<T>
where
    T: GraphDefinition,
{
    // takes the current nodes and makes every child point to it's parent and vv
    pub fn invert(&self) -> Inverted<T> {
        let edges = self.all_nodes(); // a graph always has edges.

        let mut output_graph = Self::default();

        for existing_node in edges {
            let node = output_graph
                .nodes
                .entry(existing_node.get_id())
                .or_insert_with(|| existing_node.new_derived())
                .clone();

            existing_node.with_edges_iter(|edge| {
                for child in edge {
                    let child_node = output_graph
                        .nodes
                        .entry(child.target.get_id())
                        .or_insert_with(|| child.target.new_derived())
                        .clone();

                    child_node.derive_edge(node.clone(), &child);
                }
            });
        }

        Inverted {
            graph: output_graph,
        }
    }
}

pub struct Inverted<T>
where
    T: GraphDefinition,
{
    graph: ConcreteGraph<T>,
}

impl<T> Inverted<T>
where
    T: GraphDefinition,
{
    pub fn inner(&self) -> &ConcreteGraph<T> {
        &self.graph
    }

    pub fn map_project(mut self, traversal: GraphTraversal<T>) -> Self {
        self.graph = traversal.project_into_graph(&self.graph);
        self
    }
}

impl Graph<SimpleGraphDefinition> {
    pub fn insert_node(&mut self, node_id: <SimpleGraphDefinition as GraphDefinition>::Id) {
        self.nodes
            .entry(node_id.clone())
            .or_insert_with(|| Node::<SimpleGraphDefinition>::new_bare(node_id));
    }

    pub fn insert_edge(
        &mut self,
        from: <SimpleGraphDefinition as GraphDefinition>::Id,
        to: <SimpleGraphDefinition as GraphDefinition>::Id,
    ) {
        let node = self
            .nodes
            .entry(from.clone())
            .or_insert_with(|| Node::<SimpleGraphDefinition>::new_bare(from))
            .clone();
        let target = self
            .nodes
            .entry(to.clone())
            .or_insert_with(|| Node::<SimpleGraphDefinition>::new_bare(to))
            .clone();
        node.insert_edge(target, Rc::new(()));
    }
}

#[cfg(test)]
mod test {
    type SimpleGraph = Graph<SimpleGraphDefinition>;

    use crate::graph::node::Node;
    use crate::graph::{Graph, GraphDefinition, SimpleGraphDefinition};
    use std::collections::HashSet;

    use crate::extract::{Edge, Edges, Label, Query};
    use crate::identify::Identifiable;

    type Id = i32;

    struct Datastore {
        store: Vec<Data>,
    }

    struct Data {
        id: Id,
        edges: Vec<Id>,
    }

    impl Label for Data {
        type Label = Id;
        fn label(&self) -> Self::Label {
            self.id
        }
    }

    impl Identifiable<Id> for Data {
        fn get_id(&self) -> Id {
            self.id.clone()
        }
    }

    impl Query<Id, Data> for Datastore {
        fn query(&self, identifier: &Id) -> Option<&Data> {
            self.store.iter().find(|data| data.id == *identifier)
        }

        fn all(&self) -> Vec<&Data> {
            self.store.iter().collect()
        }
    }

    impl<'a> Edges<Id, ()> for Data {
        fn next_edge(&self, previous_edge_index: Option<usize>) -> Option<Edge<Id, ()>> {
            let next_idx = previous_edge_index.map(|e| e + 1).unwrap_or_default();
            let edge = self.edges.get(next_idx)?;
            Some(Edge::new(self.get_id(), edge.clone(), next_idx, ()))
        }
    }

    type TestGraph = Graph<SimpleGraph>;
    #[test]
    fn graph_can_be_linked_together() {
        let store = Datastore {
            store: vec![
                Data {
                    id: (1),
                    edges: vec![(2), (3)],
                },
                Data {
                    id: (2),
                    edges: vec![(4)],
                },
                Data {
                    id: (3),
                    edges: vec![(4)],
                },
                Data {
                    id: (4),
                    edges: vec![(1)],
                },
            ],
        };

        let graph = SimpleGraphDefinition::build_graph(&store);

        let node_edge_compare = |node: &Node<SimpleGraphDefinition>| {
            let mut coll = vec![];
            node.with_edges_iter(|edges| {
                coll.extend(edges.cloned().map(|edge| edge.target.get_id()))
            });
            coll
        };

        let one: Node<SimpleGraphDefinition> = graph
            .into_iter()
            .find(|node: &Node<SimpleGraphDefinition>| node.get_id() == (1))
            .unwrap();

        assert_eq!(node_edge_compare(&one), vec![(2), (3)]);
        let three = one.get_edge_ref(1).unwrap().target;

        assert_eq!(node_edge_compare(&three), vec![(4)]);
        let two = one.get_edge_ref(0).unwrap().target;
        assert_eq!(node_edge_compare(&two), vec![(4)]);
        let four = two.get_edge_ref(0).unwrap().target;
        let alt_four = three.get_edge_ref(0).unwrap().target;
        assert_eq!(node_edge_compare(&alt_four), node_edge_compare(&four));

        assert_eq!(node_edge_compare(&four), vec![(1)]);
        assert_eq!(node_edge_compare(&alt_four), vec![(1)]);
    }

    #[test]
    fn graph_can_be_inverted() {
        let store = Datastore {
            store: vec![
                Data {
                    id: (1),
                    edges: vec![(2), (3)],
                },
                Data {
                    id: (2),
                    edges: vec![(4)],
                },
                Data {
                    id: (3),
                    edges: vec![(4)],
                },
                Data {
                    id: (4),
                    edges: vec![(1)],
                },
            ],
        };

        let graph = SimpleGraphDefinition::build_graph(&store);
        let graph = graph.invert();

        let one = graph
            .graph
            .into_iter()
            .find(|node| node.get_id() == (1))
            .unwrap();

        let node_edge_compare = |node: &Node<SimpleGraphDefinition>| {
            let mut coll = vec![];
            node.with_edges_iter(|edges| {
                coll.extend(edges.cloned().map(|edge| edge.target.get_id()))
            });
            coll
        };

        assert_eq!(node_edge_compare(&one), vec![(4)]);

        let four = one.get_edge_ref(0).unwrap().target;
        let three = four.get_edge_ref(1).unwrap().target;
        let two = four.get_edge_ref(0).unwrap().target;

        assert_eq!(node_edge_compare(&three), vec![(1)]);
        assert_eq!(node_edge_compare(&two), vec![(1)]);

        let alt_one = two.get_edge_ref(0).unwrap().target;
        assert_eq!(node_edge_compare(&alt_one), node_edge_compare(&one));

        assert_eq!(
            HashSet::from_iter(node_edge_compare(&four).into_iter()),
            HashSet::from([2, 3])
        );
        assert_eq!(node_edge_compare(&alt_one), vec![(4)]);
    }
}
impl<'a, T: GraphDefinition> dot::GraphWalk<'a, Node<T>, Edge<T>> for ConcreteGraph<T> {
    fn nodes(&'a self) -> Nodes<'a, Node<T>> {
        let nodes = self.all_nodes();
        Nodes::Owned(nodes.collect())
    }

    fn edges(&'a self) -> dot::Edges<'a, Edge<T>> {
        dot::Edges::Owned(self.all_edges().collect())
    }

    fn source(&'a self, edge: &Edge<T>) -> Node<T> {
        edge.origin.clone()
    }

    fn target(&'a self, edge: &Edge<T>) -> Node<T> {
        edge.target.clone()
    }
}

impl<'a, T: GraphDefinition> dot::Labeller<'a, Node<T>, Edge<T>> for ConcreteGraph<T> {
    fn graph_id(&'a self) -> Id<'a> {
        Id::new("webpack_stats").unwrap()
    }

    fn node_id(&'a self, n: &Node<T>) -> Id<'a> {
        Id::new(n.get_id().escaped()).unwrap()
    }
}