nadi_core 0.8.1

Core library for Nadi systems, for use by plugins
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
use crate::attrs::{AttrMap, HasAttributes};
use crate::eval::EvalErrorType;
use crate::expressions::RawExpr;
use crate::node::Node;
use crate::timeseries::{HasSeries, HasTimeSeries, SeriesMap, TsMap};
use abi_stable::std_types::{RDuration, Tuple2};
use abi_stable::{
    std_types::{
        RHashMap,
        ROption::{RNone, RSome},
        RString, RVec,
    },
    StableAbi,
};
use colored::Colorize;
use std::collections::{HashMap, HashSet};
use std::fmt::Debug;

// maybe I need a struct with loop, in_degree, etc flags instead?
#[repr(C)]
#[derive(StableAbi, Default, Clone, PartialEq)]
pub enum NetworkType {
    DAG,
    InTree,
    OutTree,
    WithLoop,
    #[default]
    Directed,
}

impl std::fmt::Display for NetworkType {
    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        match self {
            Self::DAG => write!(fmt, "DAG"),
            Self::InTree => write!(fmt, "InTree"),
            Self::OutTree => write!(fmt, "OutTree"),
            Self::WithLoop => write!(fmt, "WithLoop"),
            Self::Directed => write!(fmt, "Directed"),
        }
    }
}

/// Network is a collection of Nodes, with Connection information. The
/// connection information is saved in the nodes itself (`inputs` and
/// `output` variables), but they are assigned from the network.
///
/// The nadi system (lit, river system), is designed for the
/// connections between points along a river. Out of different types
/// of river networks possible, it can only handle non-branching
/// tributaries system, where each point can have zero to multiple
/// inputs, but can only have one output. Overall the system should
/// have a single output point. There can be branches in the river
/// itself in the physical sense as long as they converse before the
/// next point of interests. There cannot be node points that have
/// more than one path to reach another node in the representative
/// system.
///
/// Here is an example network file,
/// ```network
///     cannelton -> newburgh
///     newburgh -> evansville
///     evansville -> "jt-myers"
///     "jt-myers" -> "old-shawneetown"
///     "old-shawneetown" -> golconda
///     markland -> mcalpine
///     golconda -> smithland
/// ```
/// The basic form of network file can contain a connection per line,
/// the node names can either be identifier (alphanumeric+_) or a
/// quoted string (similar to [DOT format (graphviz
/// package)](https://graphviz.org/doc/info/lang.html)). Network file
/// without any connection format can be written as a node per line,
/// but those network can only call sequential functions, and not
/// input dependent ones.
///
/// Depending on the use cases, it can probably be applied to other
/// systems that are similar to a river system. Or even without the
/// connection information, the functions that are independent to each
/// other can be run in sequential order.
#[repr(C)]
#[derive(StableAbi, Default, Clone)]
pub struct Network {
    /// Type of the network
    ty: NetworkType,
    /// List of [`Node`]s based on their index
    pub(crate) nodes: RVec<RString>,
    /// List of [`Node`]s based on their order
    pub(crate) nodes_ord: RVec<RVec<RString>>,
    /// Map of node names to the [`Node`]
    pub(crate) nodes_map: RHashMap<RString, Node>,
    /// Network Attributes
    pub(crate) attributes: AttrMap,
    /// Network Series
    pub(crate) series: SeriesMap,
    /// Network TimeSeries
    pub(crate) timeseries: TsMap,
    /// Outlet [`Node`s] of the network
    pub(crate) outlets: RVec<Node>,
    /// network is ordered based on input topology
    pub(crate) ordered: bool,
}

impl std::fmt::Debug for Network {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Network")
            .field("nodes", &self.nodes)
            .field("attributes", &self.attributes)
            .field("outlets", &self.outlets.len())
            .field("ordered", &self.ordered)
            .finish()
    }
}

impl HasAttributes for Network {
    fn attr_map(&self) -> &AttrMap {
        &self.attributes
    }

    fn attr_map_mut(&mut self) -> &mut AttrMap {
        &mut self.attributes
    }
}

impl HasSeries for Network {
    fn series_map(&self) -> &SeriesMap {
        &self.series
    }

    fn series_map_mut(&mut self) -> &mut SeriesMap {
        &mut self.series
    }
}

impl HasTimeSeries for Network {
    fn ts_map(&self) -> &TsMap {
        &self.timeseries
    }

    fn ts_map_mut(&mut self) -> &mut TsMap {
        &mut self.timeseries
    }
}

impl Network {
    /// Type of the network
    pub fn ty(&self) -> &NetworkType {
        &self.ty
    }

    /// Iterator for the nodes in the network
    pub fn nodes(&self) -> impl Iterator<Item = &Node> {
        self.nodes.iter().map(|n| &self.nodes_map[n])
    }

    /// Iterator for the edges of the network
    pub fn edges(&self) -> impl Iterator<Item = (&Node, &Node)> + '_ {
        self.edges_ind().map(|(s, e)| {
            (
                &self.nodes_map[&self.nodes[s]],
                &self.nodes_map[&self.nodes[e]],
            )
        })
    }

    /// Leaf of the network (single node with no inputs)
    pub fn leaf(&self) -> Option<&Node> {
        let mut leaves = self.leaves();
        let lf = leaves.next()?;
        if leaves.next().is_some() {
            None
        } else {
            Some(lf)
        }
    }

    /// Iterator for the leaf nodes in the network
    pub fn leaves(&self) -> impl Iterator<Item = &Node> {
        self.nodes
            .iter()
            .filter(|n| {
                self.nodes_map[n.as_str()]
                    .try_lock()
                    .expect(&format!("mutex error: {:?} {}", file!(), line!()))
                    .is_leaf()
            })
            .map(|n| &self.nodes_map[n])
    }

    // Return Result<&Node, bool>, the bool is true if there are multiple roots, false if there are none, then make an EvalErrorType that takes VarType and this bool to generate better error message.
    /// Root of the network (single node with no output)
    pub fn root(&self) -> Option<&Node> {
        let mut roots = self.roots();
        let rt = roots.next()?;
        if roots.next().is_some() {
            None
        } else {
            Some(rt)
        }
    }

    /// Iterator for the root nodes in the network
    pub fn roots(&self) -> impl Iterator<Item = &Node> {
        self.nodes
            .iter()
            .filter(|n| {
                self.nodes_map[n.as_str()]
                    .try_lock()
                    .expect(&format!("mutex error: {:?} {}", file!(), line!()))
                    .is_root()
            })
            .map(|n| &self.nodes_map[n])
    }

    /// Append the edges from the list, making new nodes if necessary
    pub fn append_edges(&mut self, edges: &[(&str, &str)], _force: bool) -> Result<(), String> {
        for (start, end) in edges {
            if start == end {
                self.ty = NetworkType::WithLoop;
                // Maybe fix it after adding assertions
                // return Err(format!("Node {:?} has itself as the output", start));
                if !self.nodes_map.contains_key(*start) {
                    self.insert_node_by_name(start);
                }
                let inp = self.node_by_name(start).expect("Input just inserted");
                let out = inp.clone();
                inp.try_lock()
                    .unwrap_or_else(|| panic!("mutex error: {:?} {}", file!(), line!()))
                    .add_output(out.clone());
                inp.try_lock()
                    .unwrap_or_else(|| panic!("mutex error: {:?} {}", file!(), line!()))
                    .add_input(out.clone());
            } else {
                if !self.nodes_map.contains_key(*start) {
                    self.insert_node_by_name(start);
                }
                if !self.nodes_map.contains_key(*end) {
                    self.insert_node_by_name(end);
                }
                let inp = self.node_by_name(start).expect("Input just inserted");
                let out = self.node_by_name(end).expect("Output just inserted");
                inp.try_lock()
                    .unwrap_or_else(|| panic!("mutex error: {:?} {}", file!(), line!()))
                    .add_output(out.clone());
                out.try_lock()
                    .unwrap_or_else(|| panic!("mutex error: {:?} {}", file!(), line!()))
                    .add_input(inp.clone());
            }
        }
        self.flatten();
        Ok(())
    }

    pub fn position_nodes(&mut self) {
        todo!()
    }

    pub fn flatten(&mut self) {
        self.reorder();
        self.set_levels();
        self.nodes_map.values().for_each(|n| {
            let mut n = n
                .try_lock()
                .expect(&format!("mutex error: {:?} {}", file!(), line!()));
            let pos = (n.level() as f64, n.index() as f64);
            n.set_pos(pos)
        })
    }

    /// Create a network with given edges
    pub fn from_edges(edges: &[(&str, &str)], force: bool) -> Result<Self, String> {
        let mut network = Self::default();
        network.append_edges(edges, force)?;
        Ok(network)
    }

    /// Iterator of the edges with nodes' names
    pub fn edges_str(&self) -> impl Iterator<Item = (&str, &str)> + '_ {
        self.edges_ind()
            .map(|(s, e)| (self.nodes[s].as_str(), self.nodes[e].as_str()))
    }

    /// Iterator of the edges with node index
    pub fn edges_ind(&self) -> impl Iterator<Item = (usize, usize)> + '_ {
        self.nodes().filter_map(|n| {
            let n = n
                .try_lock()
                .expect(&format!("mutex error: {:?} {}", file!(), line!()));
            match n.output() {
                RSome(o) => Some((
                    n.index(),
                    o.try_lock()
                        .expect(&format!("mutex error: {:?} {}", file!(), line!()))
                        .index(),
                )),
                RNone => None,
            }
        })
    }

    /// Iterator of node names
    pub fn node_names(&self) -> impl Iterator<Item = &str> {
        self.nodes.iter().map(|n| n.as_str())
    }

    /// Nodes iterator in reverse order
    pub fn nodes_rev(&self) -> impl Iterator<Item = &Node> {
        self.nodes.iter().rev().map(|n| &self.nodes_map[n])
    }

    /// Number of nodes in the network
    pub fn nodes_count(&self) -> usize {
        self.nodes.len()
    }

    /// Insert a new node by its name
    pub fn insert_node_by_name(&mut self, name: &str) {
        if self.nodes_map.contains_key(name) {
            return;
        }
        let node = Node::new(self.nodes_count(), name);
        self.nodes_map.insert(name.into(), node);
        self.nodes.push(name.into());
    }

    /// Get a node by index
    pub fn node(&self, ind: usize) -> Option<&Node> {
        self.nodes.get(ind).map(|n| &self.nodes_map[n])
    }

    /// Get a node by name
    pub fn node_by_name(&self, name: &str) -> Option<&Node> {
        self.nodes_map.get(name)
    }

    /// Get a node by name (with error msg on failure)
    pub fn try_node_by_name(&self, name: &str) -> Result<&Node, EvalErrorType> {
        self.nodes_map
            .get(name)
            .ok_or_else(|| EvalErrorType::NodeNotFound(name.to_string()))
    }

    /// Get nodes in the given order
    pub fn nodes_order(&self, prop: &PropOrder) -> Vec<Node> {
        match prop {
            PropOrder::Auto | PropOrder::Sequential => self.nodes().cloned().collect(),
            PropOrder::OutputFirst => self
                .nodes_ord
                .iter()
                .rev()
                .flat_map(|n| n.iter())
                .map(|n| self.nodes_map[n].clone())
                .collect(),
            PropOrder::Inverse => self.nodes_rev().cloned().collect(),
            PropOrder::InputsFirst => self
                .nodes_ord
                .iter()
                .flat_map(|n| n.iter())
                .map(|n| self.nodes_map[n].clone())
                .collect(),
        }
    }

    /// Get nodes in the given order and selection
    pub fn nodes_select(
        &self,
        order: &PropOrder,
        prop: &PropNodes,
    ) -> Result<Vec<Node>, EvalErrorType> {
        match (prop, order) {
            (PropNodes::All, o) => Ok(self.nodes_order(o)),
            // preserve the list order if order not given explicitly
            (PropNodes::List(lst), PropOrder::Auto) => {
                let nodes: Vec<_> = lst.iter().map(|n| self.node_by_name(n.as_str())).collect();
                if nodes.iter().any(Option::is_none) {
                    let not_found: Vec<&str> = lst
                        .iter()
                        .zip(nodes)
                        .filter(|(_, o)| o.is_none())
                        .map(|(n, _)| n.as_str())
                        .collect();
                    Err(EvalErrorType::NodeNotFound(not_found.join(", ")))
                } else {
                    Ok(nodes.into_iter().filter_map(|o| o.cloned()).collect())
                }
            }
            (PropNodes::List(lst), o) => {
                let mut sel_lst: HashSet<&str> = lst.iter().map(|n| n.as_str()).collect();
                let res = self
                    .nodes_order(o)
                    .into_iter()
                    .filter(|n| sel_lst.remove(n.name()))
                    .collect();
                if sel_lst.is_empty() {
                    Ok(res)
                } else {
                    Err(EvalErrorType::NodeNotFound(
                        sel_lst.into_iter().collect::<Vec<&str>>().join(", "),
                    ))
                }
            }
            (PropNodes::Path(p), o) => self.nodes_path(o, p),
        }
    }

    /// Get a list of nodes in the given path
    pub fn nodes_path(
        &self,
        order: &PropOrder,
        path: &StrPath,
    ) -> Result<Vec<Node>, EvalErrorType> {
        if self.ty != NetworkType::InTree {
            return Err(EvalErrorType::NotImplementedError(
                "path between nodes only implemented for in-trees",
            ));
        }
        let start = self.try_node_by_name(path.start.as_str())?;
        let end = self.try_node_by_name(path.end.as_str())?;
        // we'll assume the network is indexed based on order, small
        // indices are closer to outlet; and resuffle the nodes
        let (start, end, flipped) = if start
            .try_lock()
            .expect(&format!("mutex error: {:?} {}", file!(), line!()))
            .index()
            > end.lock().index()
        {
            (start, end, false)
        } else {
            (end, start, true)
        };
        let mut curr = start.clone();
        let mut path_nodes = vec![];
        let start_name = self.nodes[start
            .try_lock()
            .expect(&format!("mutex error: {:?} {}", file!(), line!()))
            .index()]
        .as_str();
        let end_name = self.nodes[end
            .try_lock()
            .expect(&format!("mutex error: {:?} {}", file!(), line!()))
            .index()]
        .as_str();
        loop {
            path_nodes.push(curr.clone());
            if curr.name() == end_name {
                break;
            }
            let tmp = if let RSome(o) = curr
                .try_lock()
                .expect(&format!("mutex error: {:?} {}", file!(), line!()))
                .output()
            {
                o.clone()
            } else {
                // TODO: work with multiple output
                return Err(EvalErrorType::PathNotFound(
                    start_name.to_string(),
                    curr.name().to_string(),
                    end_name.to_string(),
                ));
            };
            curr = tmp;
        }
        match order {
            PropOrder::Auto if flipped => Ok(path_nodes.into_iter().rev().collect()),
            PropOrder::Auto => Ok(path_nodes),
            PropOrder::Sequential | PropOrder::OutputFirst => Ok(path_nodes),
            PropOrder::Inverse | PropOrder::InputsFirst => {
                Ok(path_nodes.into_iter().rev().collect())
            }
        }
    }

    pub fn leaf_nodes(&self) -> impl Iterator<Item = &Node> {
        self.nodes_map.iter().map(|n| n.1).filter(|n| {
            n.try_lock()
                .expect(&format!("mutex error: {:?} {}", file!(), line!()))
                .inputs()
                .is_empty()
        })
    }

    /// Calculate the weight of all nodes
    ///
    /// Value of weight signifies the number of all nodes (recursively)
    /// that are on the input side of the node
    pub fn calc_weights(&mut self) {
        let mut weights = HashMap::<RString, u64>::with_capacity(self.nodes.len());

        // ran into stackoverflow with the recursive pattern of calculation
        self.nodes_map.iter().for_each(|n| {
            if n.1
                .try_lock()
                .expect(&format!("mutex error: {:?} {}", file!(), line!()))
                .inputs()
                .is_empty()
            {
                weights.insert(n.0.clone(), 1);
            }
            let mut visited = HashSet::new();
            visited.insert(
                n.1.try_lock()
                    .expect(&format!("mutex error: {:?} {}", file!(), line!()))
                    .index(),
            );
            let mut n =
                n.1.try_lock_for(RDuration::from_secs(1))
                    .expect("Lock failed for node, maybe branched network")
                    .output()
                    .cloned();
            while let RSome(out) = n {
                *weights.entry(out.name().into()).or_insert(1) += 1;
                let o = out
                    .try_lock_for(RDuration::from_secs(1))
                    .expect("Lock failed for node, maybe branched network");
                if visited.contains(&o.index()) {
                    // looped back to the already visited node
                    // this needs to be here to prevent infinite loop
                    self.ty = NetworkType::WithLoop;
                    break;
                }
                visited.insert(o.index());
                n = o.output().cloned();
            }
        });

        for (node, ord) in weights {
            self.nodes_map[&node]
                .try_lock()
                .expect(&format!("mutex error: {:?} {}", file!(), line!()))
                .set_weight(ord);
        }
    }

    /// Calculate the order of all nodes; needs to be called after reorder
    ///
    /// Value of order signifies the number of all nodes (recursively)
    /// that are on the input side of the node
    pub fn calc_order(&mut self) {
        self.nodes_map.values().for_each(|n| {
            n.try_lock()
                .expect(&format!("mutex error: {:?} {}", file!(), line!()))
                .set_order(1)
        });

        let mut nodes_ord_map = HashMap::new();
        self.nodes.iter().enumerate().rev().for_each(|(i, n)| {
            let nobj = &self.nodes_map[n];
            let inputs: Vec<Node> = nobj
                .try_lock()
                .expect(&format!("mutex error: {:?} {}", file!(), line!()))
                .inputs()
                .to_vec();
            let ord = inputs
                .iter()
                // nodes that have some node loop back to them will have one (unset) value as order from them
                .map(|i| {
                    i.try_lock()
                        .expect(&format!("mutex error: {:?} {}", file!(), line!()))
                        .order()
                })
                .max()
                .unwrap_or(0);
            nobj.try_lock()
                .expect(&format!("mutex error: {:?} {}", file!(), line!()))
                .set_order(ord + 1);
            nodes_ord_map.insert(i, ord + 1);
        });

        let max_ord = *nodes_ord_map.values().max().unwrap_or(&0);
        let mut nodes_ord: Vec<RVec<RString>> = (0..=max_ord).map(|_| RVec::new()).collect();
        for (i, o) in nodes_ord_map {
            nodes_ord
                .get_mut(o as usize)
                .expect("max taken")
                .push(self.nodes[i].clone());
        }
        self.nodes_ord = nodes_ord
            .into_iter()
            .filter(|o| !o.is_empty())
            .collect::<Vec<_>>()
            .into();
    }

    /// Reorder the nodes in the network
    pub fn reorder(&mut self) {
        self.calc_weights();
        let weights: HashMap<_, _> = self
            .nodes_map
            .iter()
            .map(|n| {
                (
                    n.0.as_str(),
                    n.1.try_lock()
                        .expect(&format!("mutex error: {:?} {}", file!(), line!()))
                        .weight(),
                )
            })
            .collect();
        self.outlets = {
            let mut outlets: Vec<&str> = self
                .nodes_map
                .iter()
                .filter(|Tuple2(_, n)| {
                    n.try_lock()
                        .expect(&format!("mutex error: {:?} {}", file!(), line!()))
                        .is_root()
                })
                .map(|Tuple2(n, _)| n.as_str())
                .collect();
            outlets.sort_by(|a, b| weights[b].cmp(&weights[a]));
            outlets
                .into_iter()
                .map(|o| self.nodes_map[o].clone())
                .collect()
        };

        let mut visited: HashSet<String> = HashSet::new();
        let mut nodes_queue: Vec<String> = Vec::with_capacity(self.nodes.len());
        let mut new_nodes: Vec<String> = Vec::with_capacity(self.nodes.len());
        for o in self.outlets.iter().rev() {
            nodes_queue.push(o.name().to_string());
        }

        while let Some(curr) = nodes_queue.pop() {
            if visited.contains(&curr) {
                // basically to stop infinite loop, need to look
                // further how it impacts the values
                continue;
            }
            visited.insert(curr.clone());
            let inputs: Vec<Node> = self.nodes_map[curr.as_str()]
                .try_lock()
                .expect(&format!("mutex error: {:?} {}", file!(), line!()))
                .inputs()
                .to_vec();
            let mut inps: Vec<String> = inputs.iter().map(|i| i.name().to_string()).collect();

            inps.sort_by(|n1, n2| {
                weights[n2.as_str()]
                    .partial_cmp(&weights[n1.as_str()])
                    .unwrap()
            });
            // this just reorders the inputs, we don't change the input nodes
            self.nodes_map[curr.as_str()]
                .try_lock()
                .expect(&format!("mutex error: {:?} {}", file!(), line!()))
                .inputs = inps
                .iter()
                .map(|i| self.nodes_map[i.as_str()].clone())
                .collect();
            for c in inps {
                nodes_queue.push(c.clone());
            }
            new_nodes.push(curr);
        }
        let new_nodes: Vec<Node> = new_nodes
            .iter()
            .map(|n| self.nodes_map[n.as_str()].clone())
            .collect();
        if new_nodes.len() < self.nodes.len() {
            // todo, make the nodes into different groups?
            eprintln!(
                "Reorder not done, the nodes are not connected: {} connected out of {}",
                new_nodes.len(),
                self.nodes.len()
            );
            self.ordered = false;
            return;
        }
        self.nodes = new_nodes
            .iter()
            .map(|n| n.name().into())
            .collect::<Vec<RString>>()
            .into();
        self.reindex();
        self.calc_order();
        self.ordered = true;
        self.ty = match &self.ty {
            NetworkType::Directed => {
                let in_ord = self
                    .nodes_map
                    .values()
                    .map(|n| {
                        n.try_lock()
                            .expect(&format!("mutex error: {:?} {}", file!(), line!()))
                            .inputs()
                            .len()
                    })
                    .max()
                    .unwrap_or_default();
                let out_ord = self
                    .nodes_map
                    .values()
                    .map(|n| {
                        n.try_lock()
                            .expect(&format!("mutex error: {:?} {}", file!(), line!()))
                            .outputs()
                            .len()
                    })
                    .max()
                    .unwrap_or_default();
                // this is very rudimentary
                if out_ord < 2 {
                    NetworkType::InTree
                } else if in_ord < 2 {
                    NetworkType::OutTree
                } else {
                    NetworkType::DAG
                }
            }
            t => t.clone(),
        }
        // eprintln!("Exit Re Order");
    }

    /// reindex the nodes in the network
    pub fn reindex(&self) {
        for (i, n) in self.nodes().enumerate() {
            n.try_lock()
                .expect(&format!("mutex error: {:?} {}", file!(), line!()))
                .set_index(i);
        }
    }

    /// sets the levels for the nodes, 0 means it's the main branch and
    /// increasing number is for tributories level
    pub fn set_levels(&mut self) {
        fn recc_set(node: &Node, level: u64, visited: &mut HashSet<usize>) {
            let ind = node
                .try_lock()
                .expect(&format!("mutex error: {:?} {}", file!(), line!()))
                .index();
            if visited.contains(&ind) {
                // this will protect from infinite loop; not sure if
                // it is correct to skip it, or change the level to
                // something reasonable
                return;
            }
            visited.insert(ind);
            node.try_lock()
                .expect(&format!("mutex error: {:?} {}", file!(), line!()))
                .set_level(level);
            let inputs = node
                .try_lock()
                .expect(&format!("mutex error: {:?} {}", file!(), line!()))
                .inputs()
                .to_vec();
            match &inputs[..] {
                [] => (),
                [first, rest @ ..] => {
                    recc_set(first, level, visited);
                    for i in rest {
                        recc_set(i, level + 1, visited);
                    }
                }
            }
        }

        let mut visited: HashSet<usize> = HashSet::new();
        for o in &self.outlets {
            recc_set(o, 0, &mut visited);
        }
    }

    /// Remove a single node from the network
    ///
    /// This will remove the node, while making all the input nodes
    /// now goto the output node of the removed node. If it doesn't
    /// have a output node, and there is more than one input nodes,
    /// then the resulting network is no longer a directed tree, the
    /// function will print a warning.
    fn remove_node_single(&mut self, node: &Node) {
        // remove node from the network
        let (ind, outputs): (usize, Vec<Node>) = {
            let n = node
                .try_lock()
                .expect(&format!("mutex error: {:?} {}", file!(), line!()));
            let ind = n.index();
            self.nodes.remove(ind);
            self.nodes_map.remove(n.name());
            // make sure the block below doesn't hang for long
            (ind, n.outputs().to_vec())
        };

        // remove node from its output
        for out in &outputs {
            let pos = out
                .try_lock()
                .expect(&format!("mutex error: {:?} {}", file!(), line!()))
                .inputs()
                .iter()
                .position(|i| {
                    i.try_lock()
                        .expect(&format!("mutex error: {:?} {}", file!(), line!()))
                        .index()
                        == ind
                })
                .expect("Node should be in input list of output");
            out.try_lock()
                .expect(&format!("mutex error: {:?} {}", file!(), line!()))
                .inputs_mut()
                .remove(pos);
        }
        let inputs: Vec<Node> = node
            .try_lock()
            .expect(&format!("mutex error: {:?} {}", file!(), line!()))
            .inputs()
            .to_vec();

        // remove node from its inputs
        for inp in &inputs {
            let pos = inp
                .try_lock()
                .expect(&format!("mutex error: {:?} {}", file!(), line!()))
                .outputs()
                .iter()
                .position(|i| {
                    i.try_lock()
                        .expect(&format!("mutex error: {:?} {}", file!(), line!()))
                        .index()
                        == ind
                })
                .expect("Node should be in input list of output");
            inp.try_lock()
                .expect(&format!("mutex error: {:?} {}", file!(), line!()))
                .outputs_mut()
                .remove(pos);
        }

        // add new connections
        for inp in inputs {
            for out in &outputs {
                inp.try_lock()
                    .expect(&format!("mutex error: {:?} {}", file!(), line!()))
                    .add_output(out.clone());
                out.try_lock()
                    .expect(&format!("mutex error: {:?} {}", file!(), line!()))
                    .add_input(inp.clone());
            }
        }
        self.reindex();
    }

    /// Remove a single node from the network
    ///
    /// This will remove the node, while making all the input nodes
    /// now goto the output node of the removed node. If it doesn't
    /// have a output node, and there is more than one input nodes,
    /// then the resulting network is no longer a directed tree, the
    /// function will print a warning.
    pub fn remove_node(&mut self, node: &Node) {
        self.remove_node_single(node);
        // self.reorder();
        // self.set_levels();
    }

    // TODO: rewrite to make sure subset works properly
    /// Subset the network into a new network by removing a bunch of nodes
    pub fn subset(&mut self, filter: &[bool], keep: bool) -> Result<(), String> {
        let include_nodes: HashMap<String, Node> = self
            .nodes()
            .zip(self.node_names())
            .zip(filter)
            .filter(|(_, &f)| !(f ^ keep))
            .map(|((n, name), _)| (name.to_string(), n.clone()))
            .collect();
        include_nodes.values().for_each(|n| {
            n.try_lock()
                .expect(&format!("mutex error: {:?} {}", file!(), line!()))
                .unset_inputs();
        });
        for node in include_nodes.values() {
            let mut start = node.clone();
            loop {
                let out = start
                    .try_lock()
                    .expect(&format!("mutex error: {:?} {}", file!(), line!()))
                    .output()
                    .cloned();
                match out {
                    RNone => {
                        node.try_lock()
                            .expect(&format!("mutex error: {:?} {}", file!(), line!()))
                            .unset_outputs();
                        break;
                    }
                    RSome(o) => {
                        if include_nodes.contains_key(o.name()) {
                            let mut op = o.try_lock().expect(&format!(
                                "mutex error: {:?} {}",
                                file!(),
                                line!()
                            ));
                            op.add_input(node.clone());
                            node.try_lock()
                                .expect(&format!("mutex error: {:?} {}", file!(), line!()))
                                .unset_outputs();
                            node.try_lock()
                                .expect(&format!("mutex error: {:?} {}", file!(), line!()))
                                .add_output(o.clone());
                            break;
                        } else {
                            start = o.clone();
                        }
                    }
                }
            }
        }
        self.nodes = include_nodes.keys().map(|n| n.to_string().into()).collect();
        self.nodes_map = include_nodes
            .into_iter()
            .map(|(n, o)| (n.into(), o))
            .collect();
        self.reorder();
        self.set_levels();
        Ok(())
    }

    pub fn new_root(&mut self, node: Node) {
        node.try_lock()
            .expect(&format!("mutex error: {:?} {}", file!(), line!()))
            .unset_outputs();
        let mut nodes = Vec::with_capacity(self.nodes.len());
        let mut nodes_map = HashMap::with_capacity(self.nodes.len());
        fn register(n: &Node, nds: &mut Vec<RString>, nmp: &mut HashMap<RString, Node>) {
            let nm: RString = n.name().to_string().into();
            nds.push(nm.clone());
            nmp.insert(nm, n.clone());
            for i in n
                .try_lock()
                .expect(&format!("mutex error: {:?} {}", file!(), line!()))
                .inputs()
            {
                register(i, nds, nmp)
            }
        }
        register(&node, &mut nodes, &mut nodes_map);
        self.nodes = nodes.into();
        self.nodes_map = nodes_map.into();
        self.outlets = vec![node].into();
        // self.reorder();
        // self.set_levels();
    }

    /// get the connections in utf8 string to print in terminal
    pub fn connections_utf8(&self) -> Vec<String> {
        self.nodes()
            .map(|node| {
                let node =
                    node.try_lock()
                        .expect(&format!("mutex error: {:?} {}", file!(), line!()));
                let level = node.level();
                let par_level = node
                    .output()
                    .map(|n| {
                        n.try_lock()
                            .expect(&format!("mutex error: {:?} {}", file!(), line!()))
                            .level()
                    })
                    .unwrap_or(level);
                let _merge = level != par_level;

                let mut line = String::new();
                for _ in 0..level {
                    line.push_str("");
                }
                if level != par_level {
                    line.pop();
                    if node.inputs().is_empty() {
                        line.push_str("├──");
                    } else {
                        line.push_str("├──┐");
                    }
                } else if node.inputs().is_empty() {
                    line.push_str("");
                } else if node.output().is_none() {
                    line.push_str("");
                } else {
                    line.push_str("");
                }
                line
            })
            .collect()
    }

    /// get the connections in ascii string to print in terminal
    pub fn connections_ascii(&self) -> Vec<String> {
        self.nodes()
            .map(|node| {
                let node =
                    node.try_lock()
                        .expect(&format!("mutex error: {:?} {}", file!(), line!()));
                let level = node.level();
                let par_level = node
                    .output()
                    .map(|n| {
                        n.try_lock()
                            .expect(&format!("mutex error: {:?} {}", file!(), line!()))
                            .level()
                    })
                    .unwrap_or(level);
                let _merge = level != par_level;

                let mut line = String::new();
                for _ in 0..level {
                    line.push_str("  |");
                }
                if level != par_level {
                    line.pop();
                    line.push_str("|--*");
                // this is never needed as the first child is put in the same level
                // line.push_str("`--*");
                } else {
                    line.push_str("  *");
                }
                line
            })
            .collect()
    }
}

/// Path with start and end node
#[repr(C)]
#[derive(StableAbi, Debug, Hash, Default, Clone, Eq, PartialEq)]
pub struct StrPath {
    pub start: RString,
    pub end: RString,
}

impl std::fmt::Display for StrPath {
    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        write!(fmt, "{} -> {}", self.start, self.end)
    }
}

impl StrPath {
    /// new path
    pub fn new(start: RString, end: RString) -> Self {
        Self { start, end }
    }

    /// Make a string with colors to print in terminal
    pub fn to_colored_string(&self) -> String {
        format!(
            "{} -> {}",
            self.start.to_string().green(),
            self.end.to_string().green()
        )
    }
}

/// Propagation of the nodes in a network
#[derive(Debug, Default, Clone, PartialEq)]
pub struct Propagation {
    /// order of the nodes
    pub order: PropOrder,
    /// List or path of nodes
    pub nodes: PropNodes,
    /// Condition to evaluate for selection of nodes
    pub condition: PropCondition,
    /// start position of the propagation
    pub start: (usize, usize),
}

impl std::fmt::Display for Propagation {
    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        write!(fmt, "{}{}{}", self.order, self.nodes, self.condition)
    }
}

/// Propagation order for nodes in a network
#[derive(Debug, Default, Clone, PartialEq)]
pub enum PropOrder {
    /// Automatically based on context
    #[default]
    Auto,
    /// Sequential order (index: 0,1,2,...)
    Sequential,
    /// Inverse of the sequential
    Inverse,
    /// Input nodes before the output node
    InputsFirst,
    /// output node before the inputs node
    OutputFirst,
}

impl std::fmt::Display for PropOrder {
    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        match self {
            Self::Auto => Ok(()),
            Self::Sequential => write!(fmt, "<sequential>"),
            Self::Inverse => write!(fmt, "<inverse>"),
            Self::InputsFirst => write!(fmt, "<inputsfirst>"),
            Self::OutputFirst => write!(fmt, "<outputfirst>"),
        }
    }
}

/// List of nodes in a network
#[derive(Debug, Default, Clone, PartialEq)]
pub enum PropNodes {
    /// No selection (all nodes)
    #[default]
    All,
    /// List of nodes by their name
    List(RVec<RString>),
    /// Path between two nodes by their name
    Path(StrPath),
}

impl std::fmt::Display for PropNodes {
    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        match self {
            Self::All => Ok(()),
            Self::List(v) => write!(
                fmt,
                "[{}]",
                v.iter()
                    .map(|a| a.as_str())
                    .collect::<Vec<&str>>()
                    .join(", ")
            ),
            Self::Path(p) => write!(fmt, "[{}]", p),
        }
    }
}

/// Propagation condition for the nodes
#[derive(Debug, Default, Clone, PartialEq)]
pub enum PropCondition {
    /// No condition (all nodes)
    #[default]
    All,
    /// Expression to evaluate into a bool to check
    Expr(RawExpr),
    // TODO
    // Head(usize),
    // Tail(usize),
}

impl std::fmt::Display for PropCondition {
    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        match self {
            Self::All => Ok(()),
            Self::Expr(expr) => write!(fmt, "({})", expr),
        }
    }
}

/// Take any [`Node`] and create [`Network`] with it as the outlet.
impl From<Node> for Network {
    fn from(node: Node) -> Self {
        let mut net = Self::default();

        // todo: add the visited flag to avoid infinite loop here as well

        let mut nodes = vec![];
        fn insert_node(n: &Node, nodes: &mut Vec<Node>) {
            let ni = n
                .try_lock_for(RDuration::from_secs(1))
                .expect("Lock failed for node, maybe branched network");
            if ni.inputs().is_empty() {
                nodes.push(n.clone());
            } else {
                for i in ni.inputs() {
                    insert_node(i, nodes);
                }
                nodes.push(n.clone());
            }
        }
        insert_node(&node, &mut nodes);
        net.nodes_map = nodes
            .into_iter()
            .map(|n| (RString::from(n.name()), n))
            .collect::<HashMap<RString, Node>>()
            .into();
        net.nodes = net.nodes_map.keys().cloned().collect::<Vec<_>>().into();
        net.outlets = vec![node].into();
        // net.reorder();
        // net.set_levels();
        net
    }
}