Skip to main content

ontologos_swrl/
lib.rs

1//! DLSafe SWRL rule engine integrated with DL classification.
2
3#![warn(missing_docs)]
4
5mod engine;
6mod engine_adapter;
7mod rules;
8
9use ontologos_core::{Ontology, OwlConstruct};
10use ontologos_profile::scanner::scan_constructs;
11use thiserror::Error;
12
13pub use engine::materialize_swrl_rules;
14pub use engine_adapter::SwrlEngine;
15pub use rules::{SwrlReport, apply_swrl_rules};
16
17/// Result type for SWRL operations.
18pub type Result<T> = std::result::Result<T, Error>;
19
20/// SWRL engine errors.
21#[derive(Debug, Error)]
22pub enum Error {
23    /// DL classification error.
24    #[error(transparent)]
25    Dl(#[from] ontologos_dl::Error),
26    /// Core error.
27    #[error(transparent)]
28    Core(#[from] ontologos_core::Error),
29    /// SWRL profile not yet implemented.
30    #[error(
31        "SWRL rule execution is not implemented (preview); ontology has no executable SWRL rules"
32    )]
33    NotImplemented,
34    /// Preview-only limitation.
35    #[error("SWRL preview: {0}")]
36    PreviewLimit(String),
37}
38
39/// Classify with SWRL rules materialized post-DL.
40pub fn classify_with_swrl(ontology: &Ontology) -> Result<(ontologos_core::Taxonomy, SwrlReport)> {
41    if !scan_constructs(ontology).contains(&OwlConstruct::SwrlRule)
42        && ontology.swrl_rules().is_empty()
43    {
44        return Err(Error::NotImplemented);
45    }
46    let mut working = ontology.clone();
47    let report = apply_swrl_rules(&mut working)?;
48    if report.rules_found == 0 {
49        return Err(Error::PreviewLimit(
50            "SWRL rules detected in profile scan but not mapped for execution".into(),
51        ));
52    }
53    let taxonomy = ontologos_dl::classify(&working)?;
54    Ok((taxonomy, report))
55}
56
57/// Apply SWRL rules and check DL consistency on the materialized ontology.
58pub fn is_consistent_with_swrl(ontology: &Ontology) -> Result<bool> {
59    let mut working = ontology.clone();
60    let report = apply_swrl_rules(&mut working)?;
61    if report.rules_found == 0 && ontology.swrl_rules().is_empty() {
62        return Err(Error::NotImplemented);
63    }
64    Ok(ontologos_dl::is_consistent(&working)?)
65}