use std::collections::HashMap;
use std::path::Path;
use std::sync::{Arc, Mutex};
use anyhow::Result;
use crate::graph::*;
use crate::properties;
use crate::NodeType;
#[derive(Clone, Debug)]
pub struct GraphSpy<G: SwhGraph> {
pub graph: G,
pub history: Arc<Mutex<Vec<(&'static str, String)>>>,
}
impl<G: SwhGraph> GraphSpy<G> {
pub fn new(graph: G) -> Self {
GraphSpy {
graph,
history: Arc::new(Mutex::new(Vec::new())),
}
}
#[inline(always)]
fn record<Args: std::fmt::Debug>(&self, method: &'static str, arguments: Args) {
self.history
.lock()
.unwrap()
.push((method, format!("{arguments:?}")));
}
}
impl<G: SwhGraph> SwhGraph for GraphSpy<G> {
#[inline(always)]
fn path(&self) -> &Path {
self.record("path", ());
self.graph.path()
}
#[inline(always)]
fn is_transposed(&self) -> bool {
self.record("is_transposed", ());
self.graph.is_transposed()
}
#[inline(always)]
fn num_nodes(&self) -> usize {
self.record("num_nodes", ());
self.graph.num_nodes()
}
#[inline(always)]
fn has_node(&self, node_id: NodeId) -> bool {
self.record("has_node", (node_id,));
self.graph.has_node(node_id)
}
#[inline(always)]
fn num_arcs(&self) -> u64 {
self.record("num_arcs", ());
self.graph.num_arcs()
}
fn num_nodes_by_type(&self) -> Result<HashMap<NodeType, usize>> {
self.record("num_nodes_by_type", ());
self.graph.num_nodes_by_type()
}
fn num_arcs_by_type(&self) -> Result<HashMap<(NodeType, NodeType), usize>> {
self.record("num_arcs_by_type", ());
self.graph.num_arcs_by_type()
}
#[inline(always)]
fn has_arc(&self, src_node_id: NodeId, dst_node_id: NodeId) -> bool {
self.record("has_arc", (src_node_id, dst_node_id));
self.graph.has_arc(src_node_id, dst_node_id)
}
}
impl<G: SwhForwardGraph> SwhForwardGraph for GraphSpy<G> {
type Successors<'succ>
= <G as SwhForwardGraph>::Successors<'succ>
where
Self: 'succ;
#[inline(always)]
fn successors(&self, node_id: NodeId) -> Self::Successors<'_> {
self.record("successors", (node_id,));
self.graph.successors(node_id)
}
#[inline(always)]
fn outdegree(&self, node_id: NodeId) -> usize {
self.record("outdegree", (node_id,));
self.graph.outdegree(node_id)
}
}
impl<G: SwhLabeledForwardGraph> SwhLabeledForwardGraph for GraphSpy<G> {
type LabeledArcs<'arc>
= <G as SwhLabeledForwardGraph>::LabeledArcs<'arc>
where
Self: 'arc;
type LabeledSuccessors<'succ>
= <G as SwhLabeledForwardGraph>::LabeledSuccessors<'succ>
where
Self: 'succ;
fn untyped_labeled_successors(&self, node_id: NodeId) -> Self::LabeledSuccessors<'_> {
self.record("untyped_labeled_successors", (node_id,));
self.graph.untyped_labeled_successors(node_id)
}
}
impl<G: SwhBackwardGraph> SwhBackwardGraph for GraphSpy<G> {
type Predecessors<'succ>
= <G as SwhBackwardGraph>::Predecessors<'succ>
where
Self: 'succ;
fn predecessors(&self, node_id: NodeId) -> Self::Predecessors<'_> {
self.record("predecessors", (node_id,));
self.graph.predecessors(node_id)
}
fn indegree(&self, node_id: NodeId) -> usize {
self.record("indegree", (node_id,));
self.graph.indegree(node_id)
}
}
impl<G: SwhLabeledBackwardGraph> SwhLabeledBackwardGraph for GraphSpy<G> {
type LabeledArcs<'arc>
= <G as SwhLabeledBackwardGraph>::LabeledArcs<'arc>
where
Self: 'arc;
type LabeledPredecessors<'succ>
= <G as SwhLabeledBackwardGraph>::LabeledPredecessors<'succ>
where
Self: 'succ;
fn untyped_labeled_predecessors(&self, node_id: NodeId) -> Self::LabeledPredecessors<'_> {
self.record("untyped_labeled_predecessors", (node_id,));
self.graph.untyped_labeled_predecessors(node_id)
}
}
impl<G: SwhGraphWithProperties> SwhGraphWithProperties for GraphSpy<G> {
type Maps = <G as SwhGraphWithProperties>::Maps;
type Timestamps = <G as SwhGraphWithProperties>::Timestamps;
type Persons = <G as SwhGraphWithProperties>::Persons;
type Contents = <G as SwhGraphWithProperties>::Contents;
type Strings = <G as SwhGraphWithProperties>::Strings;
type LabelNames = <G as SwhGraphWithProperties>::LabelNames;
fn properties(
&self,
) -> &properties::SwhGraphProperties<
Self::Maps,
Self::Timestamps,
Self::Persons,
Self::Contents,
Self::Strings,
Self::LabelNames,
> {
self.record("properties", ());
self.graph.properties()
}
}