inference-engine 0.1.0-alpha.11

Native Rust inference foundation with checked RDF rule-profile compilation
Documentation
use super::{SchemaFreeRefutation, SchemaFreeRestriction};
use crate::reasoning::{LiteralRole, TermOccurrence, TermPosition};
use rdf_reasoning::Rdf12Datatype;

#[derive(Clone, Copy, Eq, PartialEq)]
pub(super) enum IriKind {
    Ordinary,
    Type,
    Property,
    Datatype(Rdf12Datatype),
    Reserved,
}
impl IriKind {
    pub(super) fn inspect(iri: &str) -> Self {
        if iri == "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" {
            Self::Type
        } else if iri == "http://www.w3.org/1999/02/22-rdf-syntax-ns#Property" {
            Self::Property
        } else if let Some(datatype) = Rdf12Datatype::recognize_iri(iri) {
            Self::Datatype(datatype)
        } else if is_reserved_iri(iri) {
            Self::Reserved
        } else {
            Self::Ordinary
        }
    }
}
pub(super) struct Inspection {
    pub restriction: Option<SchemaFreeRestriction>,
    pub iri: Option<IriKind>,
}
impl Inspection {
    pub(super) fn non_iri(restriction: Option<SchemaFreeRestriction>) -> Self {
        Self {
            restriction,
            iri: None,
        }
    }
}

pub(super) struct TargetEvidence<T> {
    // bounded: the current root subject and first refutation, two fixed-size records.
    subject: Option<TermOccurrence<T>>,
    type_predicate: bool,
    pub refutation: Option<SchemaFreeRefutation<T>>,
    pub has_property_candidate: bool,
}
impl<T: Clone> TargetEvidence<T> {
    pub(super) const fn new() -> Self {
        Self {
            subject: None,
            type_predicate: false,
            refutation: None,
            has_property_candidate: false,
        }
    }
    pub(super) fn observe(&mut self, occurrence: &TermOccurrence<T>, iri: Option<IriKind>) {
        if occurrence.role != LiteralRole::TargetContext {
            return;
        }
        let mut path = occurrence.path.positions();
        let root = *path.next().expect("occurrence paths are nonempty");
        if path.next().is_some() {
            return;
        } // Quoted contents are not asserted constraints.
        match root {
            TermPosition::Subject => {
                self.subject = (iri == Some(IriKind::Ordinary)).then(|| occurrence.clone());
                self.type_predicate = false;
            }
            TermPosition::Predicate => self.type_predicate = iri == Some(IriKind::Type),
            TermPosition::Object => {
                if self.type_predicate && self.subject.is_some() && iri == Some(IriKind::Property) {
                    self.has_property_candidate = true;
                }
                if self.type_predicate
                    && self.refutation.is_none()
                    && let Some(subject) = &self.subject
                    && let Some(IriKind::Datatype(datatype)) = iri
                {
                    self.refutation = Some(SchemaFreeRefutation::DefaultDatatypeMembership {
                        datatype,
                        occurrence: subject.clone(),
                    });
                }
                self.subject = None;
            }
        }
    }
}
pub(super) fn is_reserved_iri(iri: &str) -> bool {
    // bounded: exactly the three reserved namespace prefixes in the admission proof.
    [
        "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
        "http://www.w3.org/2000/01/rdf-schema#",
        "http://www.w3.org/2001/XMLSchema#",
    ]
    .iter()
    .any(|prefix| iri.starts_with(prefix))
}