mod support;
use datalog_structural_model::{
AggregateBody, Aggregator, BindExpr, InventionKind, Literal, Program, Rule, VariableSymbol,
};
use support::build::{atom, constant, var};
fn bind(name: &str, expr: BindExpr) -> Literal {
Literal::Bind {
var: VariableSymbol::new(name),
expr,
}
}
fn recursive(expression: BindExpr, clock_first: bool, alias: bool) -> Program {
let mut body = vec![Literal::Positive(atom("tick", vec![var("previous")]))];
let source = bind("instant", expression);
if clock_first {
body.insert(0, source);
} else {
body.push(source);
}
if alias {
body.push(bind("result", BindExpr::Term(var("instant"))));
}
Program {
rules: vec![Rule::new(
atom("tick", vec![var(if alias { "result" } else { "instant" })]),
body,
)],
}
}
#[test]
fn recursive_clock_is_not_a_termination_certificate() {
let program = recursive(BindExpr::Now, false, false);
program.validate().unwrap();
let violation = program.check_weak_acyclicity().unwrap_err();
assert_eq!(violation.edge().kind, InventionKind::Bind);
}
#[test]
fn a_nested_clock_cannot_hide_behind_a_pure_operator() {
let program = recursive(BindExpr::Str(Box::new(BindExpr::Now)), false, false);
program.validate().unwrap();
assert!(program.check_weak_acyclicity().is_err());
}
#[test]
fn a_clock_before_its_recursive_guard_is_still_recursive() {
let program = recursive(BindExpr::Now, true, false);
program.validate().unwrap();
assert!(program.check_weak_acyclicity().is_err());
}
#[test]
fn clock_dependence_survives_a_binding_alias() {
let program = recursive(BindExpr::Now, true, true);
program.validate().unwrap();
assert!(program.check_weak_acyclicity().is_err());
}
#[test]
fn recursive_constant_assignment_has_finite_output() {
let program = recursive(BindExpr::Term(constant("fixed")), true, true);
program.validate().unwrap();
assert_eq!(program.check_weak_acyclicity().unwrap().special_edges(), 0);
}
#[test]
fn an_acyclic_clock_assignment_can_be_certified() {
let program = Program {
rules: vec![Rule::new(
atom("observed", vec![var("instant")]),
vec![
Literal::Positive(atom("seed", vec![var("x")])),
bind("instant", BindExpr::Now),
],
)],
};
program.validate().unwrap();
let certificate = program.check_weak_acyclicity().unwrap();
assert_eq!(certificate.special_edges(), 1);
assert_eq!(certificate.skolem_depth(), 1);
}
#[test]
fn clock_recursion_through_a_second_rule_is_not_certified() {
let program = Program {
rules: vec![
Rule::new(
atom("observed", vec![var("instant")]),
vec![
Literal::Positive(atom("seed", vec![var("x")])),
bind("instant", BindExpr::Now),
],
),
Rule::new(
atom("seed", vec![var("x")]),
vec![Literal::Positive(atom("observed", vec![var("x")]))],
),
],
};
program.validate().unwrap();
assert!(program.check_weak_acyclicity().is_err());
}
#[test]
fn a_discarded_clock_does_not_invent_a_head_value() {
let program = Program {
rules: vec![Rule::new(
atom("seed", vec![var("x")]),
vec![
Literal::Positive(atom("seed", vec![var("x")])),
bind("unused", BindExpr::Now),
],
)],
};
program.validate().unwrap();
assert_eq!(program.check_weak_acyclicity().unwrap().special_edges(), 0);
}
#[test]
fn clock_group_keys_leave_the_aggregate_scope() {
let program = aggregate_clock(true);
program.validate().unwrap();
let certificate = program.check_weak_acyclicity().unwrap();
assert_eq!(certificate.special_edges(), 1);
assert_eq!(certificate.skolem_depth(), 1);
}
#[test]
fn clock_inputs_flow_into_aggregate_results() {
let program = aggregate_clock(false);
program.validate().unwrap();
let certificate = program.check_weak_acyclicity().unwrap();
assert_eq!(certificate.special_edges(), 1);
assert_eq!(certificate.skolem_depth(), 1);
}
fn aggregate_clock(group_key: bool) -> Program {
let aggregate = AggregateBody {
aggregator: Aggregator::Count,
input_var: VariableSymbol::new(if group_key { "x" } else { "instant" }),
group_by: if group_key {
vec![VariableSymbol::new("instant")]
} else {
vec![]
},
result_var: VariableSymbol::new("count"),
inner_body: vec![
Literal::Positive(atom("seed", vec![var("x")])),
bind("instant", BindExpr::Now),
],
};
Program {
rules: vec![Rule::new(
atom(
"observed",
vec![var(if group_key { "instant" } else { "count" })],
),
vec![Literal::Aggregate(aggregate)],
)],
}
}