inference-engine 0.1.0-alpha.16

Native Rust RDF inference engine with a shared command-line interface
Documentation
//! Property certificates require scoped root and nested predicate occurrence checks.
#[allow(dead_code)]
mod datatype_refutation_support;
#[allow(dead_code)]
mod projection_support;
#[allow(dead_code)]
mod source_support;
use inference_engine::{
    entailment::{
        EntailmentError, EntailmentLimits, Rdfs12FragmentEntailment, Rdfs12FragmentError,
        Rdfs12FragmentLimits, Rdfs12FragmentOutcome, Rdfs12FragmentRefutation,
    },
    reasoning::{LiteralRole, OccurrenceLimits, ReasoningStore, TermPosition},
};
use source_support::{Fixture, MemoryStore};
use std::num::{NonZeroU32, NonZeroU64};
fn checker(steps: u64) -> Rdfs12FragmentEntailment {
    let u = |v| NonZeroU32::new(v).unwrap();
    let n = |v| NonZeroU64::new(v).unwrap();
    Rdfs12FragmentEntailment::new(Rdfs12FragmentLimits::new(
        OccurrenceLimits::new(u(16), n(10000), n(100000)),
        EntailmentLimits::new(u(16), u(16), u(64), n(steps)),
        u(4096),
    ))
}
#[tokio::test]
async fn every_candidate_is_considered_even_after_an_earlier_required_property() {
    for reverse in [false, true] {
        let mut f = Fixture::new();
        let (required, fresh) = if reverse {
            let fresh = f.iri("urn:fresh");
            (f.iri("urn:required"), fresh)
        } else {
            let required = f.iri("urn:required");
            (required, f.iri("urn:fresh"))
        };
        let a = f.iri("urn:a");
        let b = f.iri("urn:b");
        f.store.inner.asserted.insert([a, required, b]);
        let rdf_type = f.iri("http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
        let property = f.iri("http://www.w3.org/1999/02/22-rdf-syntax-ns#Property");
        let source = f.store.snapshot().await.unwrap();
        let target =
            MemoryStore::new([[required, rdf_type, property], [fresh, rdf_type, property]])
                .snapshot()
                .await
                .unwrap();
        let result = checker(10000)
            .check(&source, &target, &f.access)
            .await
            .unwrap();
        let Rdfs12FragmentOutcome::NotEntailed { report, refutation } = &result else {
            panic!("candidate order {reverse}: {result:?}")
        };
        assert!(report.search_steps > 0);
        assert_eq!(report.projected_target_triples, 0);
        let Rdfs12FragmentRefutation::NamedPropertyMembership { occurrence } = refutation else {
            panic!("wrong source model")
        };
        assert_eq!(occurrence.term, fresh);
        assert_eq!(occurrence.root, [fresh, rdf_type, property]);
        assert_eq!(occurrence.role, LiteralRole::TargetContext);
        assert_eq!(
            occurrence.path.positions().copied().collect::<Vec<_>>(),
            [TermPosition::Subject]
        );
        assert_eq!(
            serde_json::from_str::<Rdfs12FragmentOutcome<u64>>(
                &serde_json::to_string(&result).unwrap()
            )
            .unwrap(),
            result
        );
        assert_eq!(
            postcard::from_bytes::<Rdfs12FragmentOutcome<u64>>(
                &postcard::to_allocvec(&result).unwrap()
            )
            .unwrap(),
            result
        );
    }
}
#[tokio::test]
async fn root_and_nested_predicates_block_refutation_but_subjects_and_objects_do_not() {
    for position in 0..5 {
        let mut f = Fixture::new();
        let q = f.iri("urn:q");
        let p = f.iri("urn:p");
        let a = f.iri("urn:a");
        if position < 3 {
            let mut row = [a, p, a];
            row[position] = q;
            f.store.inner.asserted.insert(row);
        }
        if position == 4 {
            let quote = f.quoted([a, q, a]).await;
            f.store.inner.asserted.insert([a, p, quote]);
        }
        let rdf_type = f.iri("http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
        let property = f.iri("http://www.w3.org/1999/02/22-rdf-syntax-ns#Property");
        let source = f.store.snapshot().await.unwrap();
        let target = MemoryStore::new([[q, rdf_type, property]])
            .snapshot()
            .await
            .unwrap();
        let result = checker(10000)
            .check(&source, &target, &f.access)
            .await
            .unwrap();
        if position == 1 || position == 4 {
            assert!(matches!(result, Rdfs12FragmentOutcome::Unsupported { .. }));
        } else {
            assert!(
                matches!(result, Rdfs12FragmentOutcome::NotEntailed { .. }),
                "position {position}: {result:?}"
            );
        }
    }
}
#[tokio::test]
async fn the_search_budget_is_global_across_candidates() {
    let mut f = Fixture::new();
    let p = f.iri("urn:p");
    let q = f.iri("urn:q");
    let a = f.iri("urn:a");
    f.store.inner.asserted.extend([[a, p, a], [a, q, a]]);
    let rdf_type = f.iri("http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
    let property = f.iri("http://www.w3.org/1999/02/22-rdf-syntax-ns#Property");
    let source = f.store.snapshot().await.unwrap();
    let target = MemoryStore::new([[p, rdf_type, property], [q, rdf_type, property]])
        .snapshot()
        .await
        .unwrap();
    assert!(
        checker(30)
            .check(&source, &target, &f.access)
            .await
            .is_err()
    );
    assert!(matches!(
        checker(10000)
            .check(&source, &target, &f.access)
            .await
            .unwrap(),
        Rdfs12FragmentOutcome::Unsupported { .. }
    ));
}
#[tokio::test]
async fn late_probe_or_second_target_scan_errors_invalidate_pending_certificates() {
    for source_fault in [true, false] {
        let mut f = Fixture::new();
        let q = f.iri("urn:q");
        let p = f.iri("urn:p");
        let a = f.iri("urn:a");
        f.store.inner.asserted.insert([q, p, a]);
        let rdf_type = f.iri("http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
        let property = f.iri("http://www.w3.org/1999/02/22-rdf-syntax-ns#Property");
        let source = f.store.snapshot().await.unwrap();
        let target = MemoryStore::new([[q, rdf_type, property]])
            .snapshot()
            .await
            .unwrap();
        let result = if source_fault {
            checker(10000)
                .check(
                    &projection_support::SecondScanFailure::new(source),
                    &target,
                    &f.access,
                )
                .await
        } else {
            checker(10000)
                .check(
                    &source,
                    &projection_support::SecondScanFailure::new(target),
                    &f.access,
                )
                .await
        };
        assert!(result.is_err());
    }
}

#[tokio::test]
async fn matching_consumes_the_budget_remaining_after_property_probes() {
    let mut f = Fixture::new();
    let p = f.iri("urn:p");
    let a = f.iri("urn:a");
    f.store.inner.asserted.insert([a, p, a]);
    let rdf_type = f.iri("http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
    let property = f.iri("http://www.w3.org/1999/02/22-rdf-syntax-ns#Property");
    let source = f.store.snapshot().await.unwrap();
    let target = MemoryStore::new([[p, rdf_type, property], [a, p, a]])
        .snapshot()
        .await
        .unwrap();
    assert!(matches!(
        checker(25).check(&source, &target, &f.access).await,
        Err(Rdfs12FragmentError::Search(EntailmentError::StepLimit))
    ));
    assert!(matches!(
        checker(10000)
            .check(&source, &target, &f.access)
            .await
            .unwrap(),
        Rdfs12FragmentOutcome::Unsupported { .. }
    ));
}

#[tokio::test]
async fn existential_subjects_and_quoted_claims_are_not_property_candidates() {
    let mut f = Fixture::new();
    let q = f.iri("urn:q");
    let blank = f.blank(1, "property").unwrap();
    let rdf_type = f.iri("http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
    let property = f.iri("http://www.w3.org/1999/02/22-rdf-syntax-ns#Property");
    let quoted = f.quoted([q, rdf_type, property]).await;
    let source = f.store.snapshot().await.unwrap();
    for row in [[blank, rdf_type, property], [q, rdf_type, quoted]] {
        let target = MemoryStore::new([row]).snapshot().await.unwrap();
        assert!(matches!(
            checker(10000)
                .check(&source, &target, &f.access)
                .await
                .unwrap(),
            Rdfs12FragmentOutcome::Unsupported { .. }
        ));
    }
}
#[tokio::test]
async fn cancellation_at_source_probe_or_second_target_eof_discards_property_evidence() {
    use datatype_refutation_support::{CancelAtEof, Cancellable};
    use std::sync::{Arc, atomic::AtomicBool};
    for source_cancel in [true, false] {
        let mut f = Fixture::new();
        let q = f.iri("urn:q");
        let p = f.iri("urn:p");
        let a = f.iri("urn:a");
        f.store.inner.asserted.insert([q, p, a]);
        let rdf_type = f.iri("http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
        let property = f.iri("http://www.w3.org/1999/02/22-rdf-syntax-ns#Property");
        let source = f.store.snapshot().await.unwrap();
        let target = MemoryStore::new([[q, rdf_type, property]])
            .snapshot()
            .await
            .unwrap();
        let cancelled = Arc::new(AtomicBool::new(false));
        let access = Cancellable {
            inner: &f.access,
            cancelled: cancelled.clone(),
        };
        let result = if source_cancel {
            checker(10000)
                .check(&CancelAtEof::new(source, cancelled, 1), &target, &access)
                .await
        } else {
            checker(10000)
                .check(&source, &CancelAtEof::new(target, cancelled, 1), &access)
                .await
        };
        assert!(result.is_err());
    }
}
#[tokio::test]
async fn post_admission_candidate_metadata_failures_cannot_become_property_certificates() {
    use projection_support::{FaultAccess, MetadataFault, SignalSource};
    use std::sync::{Arc, atomic::AtomicBool};
    for fault in [
        MetadataFault::Storage,
        MetadataFault::Missing,
        MetadataFault::OverCap,
    ] {
        let mut f = Fixture::new();
        let q = f.iri("urn:q");
        let rdf_type = f.iri("http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
        let property = f.iri("http://www.w3.org/1999/02/22-rdf-syntax-ns#Property");
        let source = f.store.snapshot().await.unwrap();
        let active = Arc::new(AtomicBool::new(false));
        let access = FaultAccess {
            inner: &f.access,
            active: active.clone(),
            fault,
        };
        let target = SignalSource::new(
            MemoryStore::new([[q, rdf_type, property]])
                .snapshot()
                .await
                .unwrap(),
            active,
        );
        assert!(
            checker(10000)
                .check(&source, &target, &access)
                .await
                .is_err()
        );
    }
}