use std::collections::BTreeMap;
use relate_core::NodeId;
use relate_front::parse_and_lower;
use relate_ir::{
Clause, Direction, NodePattern, PathPattern, PatternElement, QuantifiedPathPattern, Query,
RelPattern,
};
use super::super::kernels::frontier::{run, FrontierRun, WalkBudget};
use super::super::kernels::frontier_tables::fresh_tables;
use super::super::kernels::nfa::{compile, FrontierNfa, Pool};
use super::super::kernels::rearival::RuleBuffers;
use crate::ctx::ExecCtx;
use crate::graph::Graph;
use crate::overlay::ExecGraph;
use crate::pattern_engine::PatternEngine;
pub(crate) fn patterns(cypher: &str) -> Vec<PathPattern> {
let query: Query = parse_and_lower(cypher).unwrap();
query
.parts
.iter()
.flat_map(|part| {
part.clauses.iter().filter_map(|clause| match clause {
Clause::Match { patterns, .. } => Some(patterns.as_slice()),
_ => None,
})
})
.flatten()
.cloned()
.collect()
}
pub(crate) fn named_node(name: &str) -> NodePattern {
NodePattern {
variable: Some(name.into()),
..NodePattern::anonymous()
}
}
pub(crate) fn one_hop_rel(types: &[&str]) -> RelPattern {
RelPattern {
variable: None,
types: types
.iter()
.map(|name| ((*name).into(), Default::default()))
.collect(),
type_expr: None,
direction: Direction::Both,
properties: None,
prop_keys: Vec::new(),
var_length: false,
min_hops: 1,
max_hops: Some(1),
where_clause: None,
quantified: false,
}
}
pub(crate) fn var_length_rel(types: &[&str], min: u32, max: Option<u32>) -> RelPattern {
RelPattern {
var_length: true,
min_hops: min,
max_hops: max,
..one_hop_rel(types)
}
}
pub(crate) fn quantified(
elements: Vec<PatternElement>,
min: u32,
max: Option<u32>,
) -> QuantifiedPathPattern {
QuantifiedPathPattern {
subpath_variable: None,
elements,
where_clause: None,
min,
max,
}
}
pub(crate) fn path_pattern(elements: Vec<PatternElement>) -> PathPattern {
PathPattern {
variable: None,
subpath_variable: None,
elements,
nodes: Vec::new(),
rels: Vec::new(),
where_clause: None,
mode: None,
selector: None,
is_shortest_path_function: false,
}
}
pub(crate) fn endpoint_nodes(run: &FrontierRun) -> Vec<NodeId> {
let mut nodes: Vec<NodeId> = run.endpoints.iter().map(|e| e.node).collect();
nodes.sort_unstable();
nodes
}
pub(crate) fn served_frontier_run(
cypher: &str,
graph: &Graph,
seed: NodeId,
pool: Pool,
) -> FrontierRun {
let nfa = compile(graph, &patterns(cypher)[0], PatternEngine::Auto)
.unwrap_or_else(|| panic!("{cypher} should compile"));
served_nfa_run(&nfa, graph, seed, pool)
}
pub(crate) fn served_nfa_run(
nfa: &FrontierNfa,
graph: &Graph,
seed: NodeId,
pool: Pool,
) -> FrontierRun {
let view = ExecGraph::read(graph);
run(
ExecCtx::new(&view, &BTreeMap::new(), None),
seed,
nfa,
WalkBudget::standard(),
pool,
&mut fresh_tables(nfa),
&mut RuleBuffers::new(),
)
.unwrap_or_else(|error| panic!("the frontier should run: {error}"))
.unwrap_served()
}