use tensorlogic_adapters::{DomainInfo, PredicateInfo, SymbolTable};
use tensorlogic_compiler::{compile_to_einsum_with_context, CompilerContext};
use tensorlogic_ir::{TLExpr, Term};
#[test]
fn test_schema_driven_compilation_basic() {
let mut table = SymbolTable::new();
table.add_domain(DomainInfo::new("Person", 100)).unwrap();
table.add_domain(DomainInfo::new("City", 50)).unwrap();
table
.add_predicate(PredicateInfo::new(
"lives_in",
vec!["Person".to_string(), "City".to_string()],
))
.unwrap();
let mut ctx = CompilerContext::from_symbol_table(&table);
let expr = TLExpr::pred("lives_in", vec![Term::var("x"), Term::var("y")]);
let graph = compile_to_einsum_with_context(&expr, &mut ctx).unwrap();
assert!(!graph.tensors.is_empty());
assert_eq!(ctx.domains.len(), 2);
assert!(ctx.domains.contains_key("Person"));
assert!(ctx.domains.contains_key("City"));
let person = ctx.domains.get("Person").unwrap();
assert_eq!(person.cardinality, 100);
}
#[test]
fn test_schema_with_quantifiers() {
let mut table = SymbolTable::new();
table.add_domain(DomainInfo::new("Person", 100)).unwrap();
table
.add_predicate(PredicateInfo::new(
"knows",
vec!["Person".to_string(), "Person".to_string()],
))
.unwrap();
let mut ctx = CompilerContext::from_symbol_table(&table);
let expr = TLExpr::exists(
"y",
"Person",
TLExpr::pred("knows", vec![Term::var("x"), Term::var("y")]),
);
let graph = compile_to_einsum_with_context(&expr, &mut ctx).unwrap();
assert!(!graph.tensors.is_empty());
assert!(!graph.nodes.is_empty());
assert!(ctx.domains.contains_key("Person"));
}
#[test]
fn test_schema_with_metadata() {
let mut table = SymbolTable::new();
let domain =
DomainInfo::new("Person", 100).with_description("All persons in the social network");
table.add_domain(domain).unwrap();
let predicate = PredicateInfo::new("knows", vec!["Person".to_string(), "Person".to_string()])
.with_description("Binary relationship indicating familiarity");
table.add_predicate(predicate).unwrap();
let ctx = CompilerContext::from_symbol_table(&table);
let person = ctx.domains.get("Person").unwrap();
assert!(person.description.is_some());
assert_eq!(
person.description.as_ref().unwrap(),
"All persons in the social network"
);
}
#[test]
fn test_schema_with_variable_bindings() {
let mut table = SymbolTable::new();
table.add_domain(DomainInfo::new("Person", 100)).unwrap();
table
.add_predicate(PredicateInfo::new("p", vec!["Person".to_string()]))
.unwrap();
table.bind_variable("x", "Person").unwrap();
let ctx = CompilerContext::from_symbol_table(&table);
assert_eq!(ctx.var_to_domain.len(), 1);
assert_eq!(ctx.var_to_domain.get("x"), Some(&"Person".to_string()));
}
#[test]
fn test_schema_driven_with_config() {
use tensorlogic_compiler::CompilationConfig;
let mut table = SymbolTable::new();
table.add_domain(DomainInfo::new("Person", 100)).unwrap();
let ctx = CompilerContext::from_symbol_table_with_config(
&table,
CompilationConfig::fuzzy_lukasiewicz(),
);
assert_eq!(ctx.domains.len(), 1);
assert!(matches!(
ctx.config.and_strategy,
tensorlogic_compiler::AndStrategy::Lukasiewicz
));
}
#[test]
fn test_manual_domain_addition_after_import() {
let mut table = SymbolTable::new();
table.add_domain(DomainInfo::new("Person", 100)).unwrap();
let mut ctx = CompilerContext::from_symbol_table(&table);
ctx.add_domain("City", 50);
assert_eq!(ctx.domains.len(), 2);
assert!(ctx.domains.contains_key("Person"));
assert!(ctx.domains.contains_key("City"));
assert_eq!(ctx.domains.get("Person").unwrap().cardinality, 100);
assert_eq!(ctx.domains.get("City").unwrap().cardinality, 50);
}
#[test]
fn test_add_domain_info_with_full_metadata() {
let mut ctx = CompilerContext::new();
let domain = DomainInfo::with_elements(
"Person",
vec![
"alice".to_string(),
"bob".to_string(),
"charlie".to_string(),
],
)
.with_description("All persons in the system");
ctx.add_domain_info(domain);
let person = ctx.domains.get("Person").unwrap();
assert!(person.description.is_some());
assert!(person.elements.is_some());
assert_eq!(person.elements.as_ref().unwrap().len(), 3);
assert_eq!(person.cardinality, 3); }