crates/kleene-datalog/tests/count_without_rows.rs
//! Counts over an edge pattern with labels on its ends and parallel edges (a counted
//! input): a count weighed by the parallel edges, and an aggregate over the counted relation
//! weighed by its count (which a run without proof reads from the counting rule's body),
//! against a plain loop over the edge list, at 1 and 8 threads.
use std::collections::BTreeMap;
use kleene_datalog::{Aggregate, Engine, Facts, Inputs, Program, Reduce};
use kleene_sparse::{CpuBackend, CpuDevice};
/// Edges with parallel ones: 0->1 three times, 1->2, 2->0 twice, 2->3, 3->3 twice, 4->0,
/// 1->4.
const EDGES: [(u64, u64); 11] = [
(0, 1),
(0, 1),
(0, 1),
(1, 2),
(2, 0),
(2, 0),
(2, 3),
(3, 3),
(3, 3),
(4, 0),
(1, 4),
];
/// The nodes with the label (4 has none).
const LABELLED: [u64; 4] = [0, 1, 2, 3];
#[tokio::test]
async fn a_count_over_labelled_parallel_edges_is_right() {
let mut p = Program::new();
let e = p.input_counted::<3>("e");
let l = p.input::<1>("l");
let matches = p.derived_counted::<3>("matches");
let total = p.derived::<1>("total");
let fan = p.derived::<3>("fan");
let [a, b, k, x, n, c, s] = ["A", "B", "K", "X", "N", "C", "S"].map(|v| p.var(v));
// matches(A, B, X) :- X = count() by (A, B): l(A), e(A, B, K), l(B).
p.count_rule(
matches.at([a, b, x]),
[l.at([a]), e.at([a, b, k]), l.at([b])],
)
.unwrap();
// total(X) :- X = count(): l(A), e(A, B, K), l(B), each K times.
p.aggregate(
total.at([x]),
Aggregate::many([], [Reduce::count(x)]).times([k]),
[l.at([a]), e.at([a, b, k]), l.at([b])],
)
.unwrap();
// fan(A, C, S) :- C = count(), S = sum(B) by (A): matches(A, B, N), each N times.
p.aggregate(
fan.at([a, c, s]),
Aggregate::many([a], [Reduce::count(c), Reduce::sum(b, s)]).times([n]),
[matches.at([a, b, n])],
)
.unwrap();
let mut counted: BTreeMap<(u64, u64), u64> = BTreeMap::new();
for &x in &EDGES {
*counted.entry(x).or_default() += 1;
}
let mut inp = Inputs::new();
inp.facts(
&e,
Facts::from_rows(3, counted.iter().map(|(&(a, b), &k)| [a, b, k])).unwrap(),
)
.unwrap();
inp.facts(&l, Facts::from_rows(1, LABELLED.map(|x| [x])).unwrap())
.unwrap();
let mut want_fan: BTreeMap<u64, (u64, u64)> = BTreeMap::new();
for &(a, b) in &EDGES {
if LABELLED.contains(&a) && LABELLED.contains(&b) {
let w = want_fan.entry(a).or_default();
w.0 += 1;
w.1 += b;
}
}
let want_total: u64 = want_fan.values().map(|w| w.0).sum();
let want_fan: Vec<Vec<u64>> = want_fan
.into_iter()
.map(|(a, (c, s))| vec![a, c, s])
.collect();
for threads in [1, 8] {
let engine: Engine<CpuBackend> = Engine::new(CpuDevice::new(threads));
let out = engine.run(&p, &inp).await.unwrap();
let mut got: Vec<Vec<u64>> = out.get(&fan).unwrap().rows().collect();
got.sort();
assert_eq!(got, want_fan, "threads {threads}");
let got: Vec<Vec<u64>> = out.get(&total).unwrap().rows().collect();
assert_eq!(got, vec![vec![want_total]], "threads {threads}");
let bag: u64 = out.get(&matches).unwrap().rows().map(|r| r[2]).sum();
assert_eq!(bag, want_total, "threads {threads}");
}
}