use std::collections::BTreeMap;
use relate_core::Value;
use relate_ir::Direction;
use crate::exec::test_support::{build, try_exec_cypher_params, Builder};
use crate::graph::Graph;
use crate::shortest::test_support::counting_expands;
use super::route::{take_anchor_arms, AnchorArm};
use super::{take_route_runs, with_forced_gpm};
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub(super) struct Counts {
pub expands: u64,
pub route_runs: u64,
pub arms: Vec<AnchorArm>,
}
pub(super) fn counting<T>(f: impl FnOnce() -> T) -> (T, Counts) {
let _ = take_route_runs();
let _ = take_anchor_arms();
let (out, expands) = counting_expands(f);
let counts = Counts {
expands,
route_runs: take_route_runs(),
arms: take_anchor_arms(),
};
(out, counts)
}
pub(super) fn plain(n: usize, rels: &[(usize, usize)]) -> Graph {
let rels: Vec<_> = rels.iter().map(|&(a, b)| (a, b, "R")).collect();
build(&vec![&[][..]; n], &rels).graph
}
pub(super) fn labelled(x: usize, n: usize, rels: &[(usize, usize)]) -> Graph {
let labels: Vec<&[&str]> = (0..n)
.map(|i| if i < x { &["X"][..] } else { &["Y"][..] })
.collect();
let rels: Vec<_> = rels.iter().map(|&(a, b)| (a, b, "R")).collect();
build(&labels, &rels).graph
}
pub(super) fn shortcut() -> Graph {
plain(5, &[(0, 1), (1, 2), (2, 3), (0, 4), (4, 3), (3, 1)])
}
pub(super) fn self_loops(loops: usize) -> Graph {
let b = Builder::new();
let m = b.node("m", &["M"]);
for i in 1..=loops {
b.rel(m, m, "B", &[("i", Value::Int(i as i64))]);
}
b.done()
}
pub(super) fn run(g: &Graph, q: &str) -> (Vec<Vec<Value>>, Counts) {
run_params(g, q, &BTreeMap::new())
}
pub(super) fn run_params(
g: &Graph,
q: &str,
params: &BTreeMap<String, Value>,
) -> (Vec<Vec<Value>>, Counts) {
counting(|| try_exec_cypher_params(g, q, params).unwrap().rows)
}
pub(super) fn on_route(g: &Graph, q: &str) -> Vec<Vec<Value>> {
let (rows, counts) = run(g, q);
assert!(counts.route_runs > 0, "expected the BFS route for {q}");
assert!(counts.expands > 0, "the route expanded nothing for {q}");
sorted(rows)
}
pub(super) fn gpm(g: &Graph, q: &str) -> Vec<Vec<Value>> {
gpm_params(g, q, &BTreeMap::new())
}
pub(super) fn gpm_params(g: &Graph, q: &str, params: &BTreeMap<String, Value>) -> Vec<Vec<Value>> {
sorted(with_forced_gpm(|| {
try_exec_cypher_params(g, q, params).unwrap().rows
}))
}
pub(super) fn sorted(mut rows: Vec<Vec<Value>>) -> Vec<Vec<Value>> {
rows.sort_by(|a, b| {
let mut cells = a.iter().zip(b).map(|(x, y)| x.order_by_cmp(y));
cells.find(|o| o.is_ne()).unwrap_or(a.len().cmp(&b.len()))
});
rows
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(super) struct Selector {
pub(super) open: &'static str,
pub(super) close: &'static str,
pub(super) all: bool,
}
#[rustfmt::skip]
pub(super) const SELECTORS: [Selector; 4] = [
Selector { open: "ANY SHORTEST ", close: "", all: false },
Selector { open: "SHORTEST 1 ", close: "", all: false },
Selector { open: "SHORTEST 1 GROUP ", close: "", all: true },
Selector { open: "ALL SHORTEST ", close: "", all: true },
];
pub(super) const DIRS: [Direction; 3] = [Direction::Outgoing, Direction::Incoming, Direction::Both];
pub(super) trait DirectionExt {
fn arrows(self) -> (&'static str, &'static str);
fn step(self, at: usize, rel: (usize, usize)) -> Option<usize>;
}
impl DirectionExt for Direction {
fn arrows(self) -> (&'static str, &'static str) {
match self {
Direction::Outgoing => ("-", "->"),
Direction::Incoming => ("<-", "-"),
Direction::Both => ("-", "-"),
}
}
fn step(self, at: usize, rel: (usize, usize)) -> Option<usize> {
let (from, to) = rel;
match self {
Direction::Outgoing => (from == at).then_some(to),
Direction::Incoming => (to == at).then_some(from),
Direction::Both if from == at => Some(to),
Direction::Both => (to == at).then_some(from),
}
}
}