inference-engine 0.1.0-alpha.14

Native Rust RDF inference engine with a shared command-line interface
Documentation
//! A target triple term needs a property-valued predicate in every source model.
#[allow(dead_code)]
mod datatype_refutation_support;
#[allow(dead_code)]
mod projection_support;
#[allow(dead_code)]
mod source_support;
use inference_engine::{
    entailment::{
        EntailmentLimits, Rdfs12FragmentEntailment, Rdfs12FragmentLimits, Rdfs12FragmentOutcome,
        Rdfs12FragmentRefutation,
    },
    reasoning::{OccurrenceLimits, ReasoningStore},
};
use source_support::{Fixture, MemoryStore};
use std::num::{NonZeroU32, NonZeroU64};
fn checker() -> Rdfs12FragmentEntailment {
    checker_with_steps(100000)
}
fn checker_with_steps(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 an_unforced_named_predicate_cannot_give_a_target_triple_term_a_value() {
    for schema in 0..3 {
        let mut f = Fixture::new();
        let a = f.iri("urn:a");
        let p = f.iri("urn:p");
        let q = f.iri("urn:q");
        let resource = f.iri("http://www.w3.org/2000/01/rdf-schema#Resource");
        let class = f.iri("http://www.w3.org/2000/01/rdf-schema#Class");
        let proposition = f.iri("http://www.w3.org/2000/01/rdf-schema#Proposition");
        let property = f.iri("http://www.w3.org/1999/02/22-rdf-syntax-ns#Property");
        let subclass = f.iri("http://www.w3.org/2000/01/rdf-schema#subClassOf");
        if schema == 1 {
            f.store.inner.asserted.insert([resource, subclass, class]);
        }
        if schema == 2 {
            f.store
                .inner
                .asserted
                .insert([proposition, subclass, property]);
        }
        let quoted = f.quoted([a, q, a]).await;
        let source = f.store.snapshot().await.unwrap();
        let target = MemoryStore::new([[a, p, quoted]]).snapshot().await.unwrap();
        let result = checker().check(&source, &target, &f.access).await.unwrap();
        let Rdfs12FragmentOutcome::NotEntailed {
            report,
            refutation: Rdfs12FragmentRefutation::TripleTermPredicate { occurrence },
        } = result
        else {
            panic!("schema {schema}: {result:?}")
        };
        assert_eq!(report.projected_target_triples, 0);
        assert_eq!(occurrence.term, q);
        assert_eq!(occurrence.parent, [a, q, a]);
        assert_eq!(occurrence.root, [a, p, quoted]);
        assert_eq!(
            occurrence.path.positions().copied().collect::<Vec<_>>(),
            [
                inference_engine::reasoning::TermPosition::Object,
                inference_engine::reasoning::TermPosition::Predicate
            ]
        );
    }
}
#[tokio::test]
async fn an_asserted_property_declaration_disables_both_property_and_denotation_refutations() {
    let mut f = Fixture::new();
    let a = f.iri("urn:a");
    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 subclass = f.iri("http://www.w3.org/2000/01/rdf-schema#subClassOf");
    f.store.inner.asserted.insert([q, rdf_type, property]);
    let quoted = f.quoted([a, q, a]).await;
    let source = f.store.snapshot().await.unwrap();
    for root in [[q, rdf_type, property], [a, subclass, quoted]] {
        let target = MemoryStore::new([root]).snapshot().await.unwrap();
        let result = checker().check(&source, &target, &f.access).await.unwrap();
        assert!(
            matches!(
                result,
                Rdfs12FragmentOutcome::Unsupported { .. } | Rdfs12FragmentOutcome::Entailed(_)
            ),
            "{result:?}"
        );
    }
}

#[tokio::test]
async fn quoted_property_claims_do_not_declare_the_named_subject() {
    let mut f = Fixture::new();
    let a = f.iri("urn:a");
    let p = f.iri("urn:p");
    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 subclass = f.iri("http://www.w3.org/2000/01/rdf-schema#subClassOf");
    let quoted_claim = f.quoted([q, rdf_type, property]).await;
    f.store.inner.asserted.insert([a, p, quoted_claim]);
    let quoted = f.quoted([a, q, a]).await;
    let source = f.store.snapshot().await.unwrap();
    let target = MemoryStore::new([[a, subclass, quoted]])
        .snapshot()
        .await
        .unwrap();
    let result = checker().check(&source, &target, &f.access).await.unwrap();
    assert!(
        matches!(result, Rdfs12FragmentOutcome::NotEntailed { .. }),
        "{result:?}"
    );
}

#[tokio::test]
async fn all_nested_candidates_are_probed_and_all_source_property_requirements_count() {
    for source_kind in 0..3 {
        let mut f = Fixture::new();
        let a = f.iri("urn:a");
        let p = f.iri("urn:p");
        let q = f.iri("urn:q");
        let fresh = f.iri("urn:fresh");
        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 subclass = f.iri("http://www.w3.org/2000/01/rdf-schema#subClassOf");
        let row = match source_kind {
            0 => [a, q, a],
            1 => [a, p, f.quoted([a, q, a]).await],
            _ => [q, rdf_type, property],
        };
        f.store.inner.asserted.insert(row);
        let required = f.quoted([a, q, a]).await;
        let unforced = f.quoted([a, fresh, a]).await;
        let source = f.store.snapshot().await.unwrap();
        let only_required = MemoryStore::new([[a, subclass, required]])
            .snapshot()
            .await
            .unwrap();
        assert!(matches!(
            checker()
                .check(&source, &only_required, &f.access)
                .await
                .unwrap(),
            Rdfs12FragmentOutcome::Unsupported { .. }
        ));
        let both = MemoryStore::new([[a, subclass, required], [a, subclass, unforced]])
            .snapshot()
            .await
            .unwrap();
        let result = checker().check(&source, &both, &f.access).await.unwrap();
        assert!(
            matches!(result,Rdfs12FragmentOutcome::NotEntailed {refutation:Rdfs12FragmentRefutation::TripleTermPredicate {occurrence},..} if occurrence.term==fresh)
        );
    }
}
#[tokio::test]
async fn late_probe_and_target_failures_discard_nested_predicate_evidence() {
    for source_fault in [true, false] {
        let mut f = Fixture::new();
        let a = f.iri("urn:a");
        let q = f.iri("urn:q");
        let subclass = f.iri("http://www.w3.org/2000/01/rdf-schema#subClassOf");
        let quoted = f.quoted([a, q, a]).await;
        let source = f.store.snapshot().await.unwrap();
        let target = MemoryStore::new([[a, subclass, quoted]])
            .snapshot()
            .await
            .unwrap();
        let result = if source_fault {
            checker()
                .check(
                    &projection_support::SecondScanFailure::new(source),
                    &target,
                    &f.access,
                )
                .await
        } else {
            checker()
                .check(
                    &source,
                    &projection_support::SecondScanFailure::new(target),
                    &f.access,
                )
                .await
        };
        assert!(result.is_err());
    }
}
#[tokio::test]
async fn cancellation_and_aggregate_work_exhaustion_never_become_undefined_term_verdicts() {
    use datatype_refutation_support::{CancelAtEof, Cancellable};
    use std::sync::{Arc, atomic::AtomicBool};
    for source_cancel in [true, false] {
        let mut f = Fixture::new();
        let a = f.iri("urn:a");
        let q = f.iri("urn:q");
        let subclass = f.iri("http://www.w3.org/2000/01/rdf-schema#subClassOf");
        let quoted = f.quoted([a, q, a]).await;
        let source = f.store.snapshot().await.unwrap();
        let target = MemoryStore::new([[a, subclass, quoted]])
            .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()
                .check(&CancelAtEof::new(source, cancelled, 1), &target, &access)
                .await
        } else {
            checker()
                .check(&source, &CancelAtEof::new(target, cancelled, 1), &access)
                .await
        };
        assert!(result.is_err());
    }
    let mut f = Fixture::new();
    let a = f.iri("urn:a");
    let q = f.iri("urn:q");
    let subclass = f.iri("http://www.w3.org/2000/01/rdf-schema#subClassOf");
    let quoted = f.quoted([a, q, a]).await;
    let source = f.store.snapshot().await.unwrap();
    let target = MemoryStore::new([[a, subclass, quoted]])
        .snapshot()
        .await
        .unwrap();
    assert!(
        checker_with_steps(10)
            .check(&source, &target, &f.access)
            .await
            .is_err()
    );
}
#[tokio::test]
async fn late_nested_predicate_metadata_failure_is_an_error() {
    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 a = f.iri("urn:a");
        let q = f.iri("urn:q");
        let subclass = f.iri("http://www.w3.org/2000/01/rdf-schema#subClassOf");
        let quoted = f.quoted([a, q, a]).await;
        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([[a, subclass, quoted]])
                .snapshot()
                .await
                .unwrap(),
            active,
        );
        assert!(checker().check(&source, &target, &access).await.is_err());
    }
}