crates/kleene-datalog/tests/edge_rows.rs
//! Every edge type of a set also readable by its row, by name through the set's catalog
//! (kleene-gar's `GarGraph::catalog`): `E@row` (row, from, to) and `E@row.p` (row, value),
//! so two edges of one type between the same two nodes stay two edges and a rule joins each
//! edge's own values on its row. The answer checked against a plain loop over the same edges.
use std::collections::BTreeSet;
use std::path::PathBuf;
use graphar::mapped::{IntColumn, IntEdges, IntGraphWriter};
use kleene_datalog::{Datum, Engine, Inputs, Program, Type};
use kleene_gar::GarGraph;
use kleene_sparse::{CpuBackend, CpuDevice};
/// Calls (from, to, address, kind): three from 0 to 1, each with its own address and kind.
const CALLS: [(u32, u32, u64, &str); 5] = [
(0, 1, 100, "direct"),
(1, 2, 300, "direct"),
(0, 1, 200, "virtual"),
(2, 0, 500, "tail"),
(0, 1, 400, "direct"),
];
struct TempDir(PathBuf);
impl Drop for TempDir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
fn set() -> TempDir {
let dir = std::env::temp_dir().join(format!("kleene-datalog-edge-rows-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let mut w = IntGraphWriter::new(&dir, "X").unwrap();
w.vertices("lib", 2, vec![]).unwrap();
w.vertices("func", 3, vec![]).unwrap();
let src: Vec<u32> = CALLS.iter().map(|c| c.0).collect();
let dst: Vec<u32> = CALLS.iter().map(|c| c.1).collect();
let addr = IntColumn::U64(CALLS.iter().map(|c| c.2).collect());
let mut offsets = vec![0u32];
let mut bytes = Vec::new();
for c in CALLS {
bytes.extend_from_slice(c.3.as_bytes());
offsets.push(bytes.len() as u32);
}
let kind = IntColumn::Bytes { offsets, bytes };
let props = vec![("addr".to_string(), addr), ("kind".to_string(), kind)];
w.edges(IntEdges::from_pairs(("func", "call", "func"), (3, 3), &src, &dst, props).unwrap())
.unwrap();
w.finish().unwrap();
TempDir(dir)
}
fn as_row(d: &[Datum; 4]) -> (u64, u64, u64, String) {
match d {
[Datum::U64(f), Datum::U64(g), Datum::U64(a), Datum::Str(k)] => (*f, *g, *a, k.clone()),
other => panic!("not a call row: {other:?}"),
}
}
/// `site(F, G, A, K) :- call@row(R, F, G), call@row.addr(R, A), call@row.kind(R, K).`: one
/// row per call, each address with its own kind (the three calls from 0 to 1 too).
#[tokio::test]
async fn each_edge_is_a_row_with_its_own_values() {
let dir = set();
let g = GarGraph::open(&dir.0).unwrap();
let catalog = g.catalog().unwrap();
let base = g.id_space().unwrap().base("func").unwrap();
assert!(base > 0, "the ids of `func` start after `lib`'s");
let mut p = Program::new();
let calls = p.input_from::<3>(&catalog, "call@row").unwrap();
let addr = p.input_from::<2>(&catalog, "call@row.addr").unwrap();
let kind = p.input_from::<2>(&catalog, "call@row.kind").unwrap();
// by table name too, and the column types the catalog gives
let by_table = p.input_from::<3>(&catalog, "func_call_func@row").unwrap();
assert_eq!(p.types(calls.id()), [Type::U64, Type::U64, Type::U64]);
assert_eq!(p.types(addr.id()), [Type::U64, Type::U64]);
assert_eq!(p.types(kind.id()), [Type::U64, Type::Str]);
assert!(p.input_from::<2>(&catalog, "call@row.nope").is_err());
assert!(p.input_from::<2>(&catalog, "call@row").is_err());
let site = p.derived_typed::<4>("site", [Type::U64, Type::U64, Type::U64, Type::Str]);
let rows = p.derived::<1>("rows");
let [r, f, t, a, k] = ["R", "F", "G", "A", "K"].map(|n| p.var(n));
p.rule(
site.at([f, t, a, k]),
[calls.at([r, f, t]), addr.at([r, a]), kind.at([r, k])],
)
.unwrap();
p.rule(rows.at([r]), [by_table.at([r, f, t])]).unwrap();
let mut inputs = Inputs::new();
inputs.bind(&p, &catalog).unwrap();
let e: Engine<CpuBackend> = Engine::new(CpuDevice::new(2));
let out = e.run(&p, &inputs).await.unwrap();
let got: BTreeSet<_> = out.datums(&p, &site).unwrap().iter().map(as_row).collect();
let got_rows: BTreeSet<Vec<u64>> = out.get(&rows).unwrap().rows().collect();
// the plain loop: each call once, in the set's id space; rows 0..calls
let want: BTreeSet<_> = CALLS
.iter()
.map(|c| (base + c.0 as u64, base + c.1 as u64, c.2, c.3.to_string()))
.collect();
assert_eq!(want.len(), CALLS.len());
assert_eq!(got, want);
let want_rows: BTreeSet<Vec<u64>> = (0..CALLS.len() as u64).map(|r| vec![r]).collect();
assert_eq!(got_rows, want_rows);
}