Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/kleene

crates/kleene-datalog/tests/flags.rs

//! Relations of no column (flags) from the Datalog client: `datalog!` against the builder,
//! an input flag given as a bool, `Output::holds`, the proof of a flag, the refusals.

use kleene_datalog::{Aggregate, Engine, Facts, How, Inputs, Program, datalog};
use kleene_sparse::{CpuBackend, CpuDevice};

fn engine() -> Engine<CpuBackend> {
    Engine::new(CpuDevice::new(2)).explain(true)
}

fn ones(xs: &[u64]) -> Facts {
    Facts::from_rows(1, xs.iter().map(|&x| [x])).unwrap()
}

#[tokio::test]
async fn a_flag_written_with_the_macro_and_the_builder_holds_alike() {
    let (m, r) = datalog! {
        input t/1, s/1, open/0.
        derived has/0, none/0, shut/1.
        has() :- t(X).
        none() :- s(X), !has().
        shut(X) :- s(X), !open().
    }
    .unwrap();
    let mut b = Program::new();
    let t = b.input::<1>("t");
    let s = b.input::<1>("s");
    let open = b.input::<0>("open");
    let has = b.derived::<0>("has");
    let none = b.derived::<0>("none");
    let shut = b.derived::<1>("shut");
    let x = b.var("X");
    b.rule(has.atom([]), [t.at([x])]).unwrap();
    b.rule(none.atom([]), [s.at([x]).into(), !has.atom([])])
        .unwrap();
    b.rule(shut.at([x]), [s.at([x]).into(), !open.atom([])])
        .unwrap();
    assert_eq!(m.rules(), b.rules());
    for (tv, open_v) in [
        (vec![], false),
        (vec![5], false),
        (vec![5], true),
        (vec![], true),
    ] {
        let mut i = Inputs::new();
        i.facts(&r.t, ones(&tv))
            .unwrap()
            .facts(&r.s, ones(&[1, 2]))
            .unwrap()
            .flag(&r.open, open_v);
        for p in [&m, &b] {
            let o = engine().run(p, &i).await.unwrap();
            assert_eq!(o.holds(&r.has), !tv.is_empty());
            assert_eq!(o.holds(&r.none), tv.is_empty());
            let shut_rows: Vec<Vec<u64>> = o.get(&r.shut).unwrap().sorted_rows();
            let want: Vec<Vec<u64>> = if open_v {
                vec![]
            } else {
                vec![vec![1], vec![2]]
            };
            assert_eq!(shut_rows, want);
            if tv.is_empty() {
                let why = o.why(p, &r.none, [0u64; 0]).expect("none() holds");
                let How::Rule { absent, .. } = &why.how else {
                    panic!("by a rule: {why:?}");
                };
                assert_eq!(absent, &vec![(r.has.id(), vec![])]);
                assert!(o.render(p, &why).starts_with("none()"));
            } else {
                assert!(o.why(p, &r.none, [0u64; 0]).is_none());
                let why = o.why(p, &r.has, [0u64; 0]).expect("has() holds");
                assert!(why.tuple.is_empty());
            }
        }
    }
}

#[test]
fn a_flag_has_no_aggregate_and_a_flag_atom_has_no_term() {
    let mut p = Program::new();
    let t = p.input::<1>("t");
    let has = p.derived::<0>("has");
    let (x, n) = (p.var("X"), p.var("N"));
    assert!(
        p.aggregate(has.atom([]), Aggregate::count([], [x], n), [t.at([x])])
            .is_err()
    );
    // through the plan, an atom of a flag with a term is refused
    let bad = kleene_datalog::Atom {
        rel: has.id(),
        terms: vec![x.into()],
    };
    assert!(p.rule(bad, [t.at([x])]).is_err());
}