use domainforge_core::{
policy::{BinaryOp, Expression, Policy, Quantifier},
primitives::{Entity, Flow, Resource},
units::unit_from_string,
Graph,
};
use rust_decimal::Decimal;
use std::str::FromStr;
fn build_graph_with_optional_flow_attribute() -> Graph {
let mut graph = Graph::new();
let warehouse = Entity::new_with_namespace("Warehouse", "default");
let factory = Entity::new_with_namespace("Factory", "default");
let product = Resource::new_with_namespace("Widget", unit_from_string("units"), "default");
let warehouse_id = warehouse.id().clone();
let factory_id = factory.id().clone();
let product_id = product.id().clone();
graph.add_entity(warehouse).unwrap();
graph.add_entity(factory).unwrap();
graph.add_resource(product).unwrap();
let mut flow1 = Flow::new(
product_id.clone(),
warehouse_id.clone(),
factory_id.clone(),
Decimal::from_str("100").unwrap(),
);
flow1.set_attribute("tag", serde_json::Value::Null);
graph.add_flow(flow1).unwrap();
let mut flow2 = Flow::new(
product_id.clone(),
warehouse_id.clone(),
factory_id.clone(),
Decimal::from_str("200").unwrap(),
);
flow2.set_attribute("tag", serde_json::json!("X"));
graph.add_flow(flow2).unwrap();
graph
}
#[test]
fn test_forall_with_nulls_results_in_unknown() {
let graph = build_graph_with_optional_flow_attribute();
let expr = Expression::quantifier(
Quantifier::ForAll,
"flow",
Expression::variable("flows"),
Expression::comparison("flow.tag", "==", "X").unwrap(),
);
let policy = Policy::new("ForAll TagX", expr);
let result = policy.evaluate(&graph).unwrap();
assert!(!result.is_satisfied);
assert_eq!(result.is_satisfied_tristate, None);
assert_eq!(result.violations.len(), 1);
assert_eq!(
result.violations[0].severity,
domainforge_core::policy::Severity::Error
);
}
#[test]
fn test_exists_with_true_and_null_returns_true() {
let graph = build_graph_with_optional_flow_attribute();
let expr = Expression::quantifier(
Quantifier::Exists,
"flow",
Expression::variable("flows"),
Expression::comparison("flow.tag", "==", "X").unwrap(),
);
let policy = Policy::new("Exists TagX", expr);
let result = policy.evaluate(&graph).unwrap();
assert!(result.is_satisfied);
assert_eq!(result.is_satisfied_tristate, Some(true));
assert_eq!(result.violations.len(), 0);
}
#[test]
fn test_exists_unique_with_duplicate_true_returns_false() {
let graph = build_graph_with_optional_flow_attribute();
let mut graph2 = graph.clone();
let mut flow3 = Flow::new(
graph2.all_resources()[0].id().clone(),
graph2.all_entities()[0].id().clone(),
graph2.all_entities()[1].id().clone(),
Decimal::from_str("300").unwrap(),
);
flow3.set_attribute("tag", serde_json::json!("X"));
graph2.add_flow(flow3).unwrap();
let expr = Expression::quantifier(
Quantifier::ExistsUnique,
"flow",
Expression::variable("flows"),
Expression::comparison("flow.tag", "==", "X").unwrap(),
);
let policy = Policy::new("ExistsUnique TagX", expr);
let result = policy.evaluate(&graph2).unwrap();
assert!(!result.is_satisfied);
assert_eq!(result.is_satisfied_tristate, Some(false));
}
#[test]
fn test_nested_null_propagation_and_forall_unknown() {
let mut graph = build_graph_with_optional_flow_attribute();
let flow_id2 = graph.all_flows()[1].id().clone();
if let Some(mut f2) = graph.get_flow(&flow_id2).cloned() {
f2.set_attribute("tag", serde_json::Value::Null);
let _ = graph.remove_flow(&flow_id2);
graph.add_flow(f2).unwrap();
}
let expr = Expression::quantifier(
Quantifier::ForAll,
"flow",
Expression::variable("flows"),
Expression::binary(
BinaryOp::And,
Expression::comparison("flow.tag", "==", "X").unwrap(),
Expression::comparison("flow.quantity", ">", 0).unwrap(),
),
);
let policy = Policy::new("Nested Null ForAll", expr);
let result = policy.evaluate(&graph).unwrap();
assert_eq!(result.is_satisfied_tristate, None);
assert!(
!result.violations.is_empty(),
"Expected at least one violation when evaluation is NULL"
);
assert_eq!(
result.violations[0].severity,
domainforge_core::policy::Severity::Error
);
}