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
use crate::RegulatoryGraph;

/// To consider two regulatory graphs equivalent, we generally assume that they have the same
/// number of variables, with the same names, and stored in the same order. Furthermore, they
/// also need to have the same regulations, however, these do not have a specific order that
/// needs to be preserved. The reason why we enforce variable order and not regulation order
/// is that `VariableId` objects should be compatible across equivalent graphs, but there is no
/// `RegulationId` or a similar requirement.
impl PartialEq for RegulatoryGraph {
    fn eq(&self, other: &Self) -> bool {
        if self.variables != other.variables {
            return false;
        }
        // Check that every regulation present in `self` is equivalent in `other`.
        for self_reg in self.regulations() {
            if let Some(other_reg) = other.find_regulation(self_reg.regulator, self_reg.target) {
                if self_reg != other_reg {
                    return false;
                }
            } else {
                return false;
            }
        }

        // And symmetrically that every regulation in `other` is equivalent in `self`.
        for other_reg in other.regulations() {
            if let Some(self_reg) = self.find_regulation(other_reg.regulator, other_reg.target) {
                if self_reg != other_reg {
                    return false;
                }
            } else {
                return false;
            }
        }

        true
    }
}

impl Eq for RegulatoryGraph {}

#[cfg(test)]
mod tests {
    use crate::RegulatoryGraph;

    #[test]
    fn test_regulation_order_equivalence() {
        let mut a = RegulatoryGraph::new(vec!["a".to_string(), "b".to_string()]);
        let mut b = RegulatoryGraph::new(vec!["a".to_string(), "b".to_string()]);

        a.add_string_regulation("a -> b").unwrap();
        a.add_string_regulation("b -| a").unwrap();

        b.add_string_regulation("b -| a").unwrap();
        b.add_string_regulation("a -> b").unwrap();

        assert_eq!(a, b);
    }
}