graphfind_rs/petgraph/
pattern_graphs.rs

1use crate::pattern_matching::{PatternElement, PatternGraph};
2
3/// Defines an PatternGraph over an directed petgraph. Guarantees that
4/// our graph should always be directed.
5impl<NodeWeight, EdgeWeight> PatternGraph<NodeWeight, EdgeWeight>
6    for petgraph::graph::Graph<PatternElement<NodeWeight>, PatternElement<EdgeWeight>>
7{
8    /// Adds a hidden node to match, and returns the reference.
9    fn add_hidden_node<C>(&mut self, condition: C) -> Self::NodeRef
10    where
11        C: Fn(&NodeWeight) -> bool + 'static,
12    {
13        self.add_node(PatternElement::new(Box::new(condition), true))
14    }
15
16    /// Adds a visible node to match, and returns the reference.
17    fn add_node<C>(&mut self, condition: C) -> Self::NodeRef
18    where
19        C: Fn(&NodeWeight) -> bool + 'static,
20    {
21        self.add_node(PatternElement::new(Box::new(condition), false))
22    }
23
24    /// Adds a hidden/ignored edge to match, and returns the reference.
25    fn add_hidden_edge<C>(
26        &mut self,
27        from: Self::NodeRef,
28        to: Self::NodeRef,
29        condition: C,
30    ) -> Self::EdgeRef
31    where
32        C: Fn(&EdgeWeight) -> bool + 'static,
33    {
34        self.add_edge(from, to, PatternElement::new(Box::new(condition), true))
35    }
36
37    /// Adds an edge to match, and returns the reference.
38    fn add_edge<C>(&mut self, from: Self::NodeRef, to: Self::NodeRef, condition: C) -> Self::EdgeRef
39    where
40        C: Fn(&EdgeWeight) -> bool + 'static,
41    {
42        if !self.node_weight(from).unwrap().should_appear()
43            || !self.node_weight(to).unwrap().should_appear()
44        {
45            panic!("Must not refer to an edge that refers to nodes that cannot be referred!")
46        }
47        self.add_edge(from, to, PatternElement::new(Box::new(condition), false))
48    }
49}