#[derive(Debug, Clone, Copy)]
pub struct OntologyMetadata {
pub name: &'static str,
pub namespace: &'static str,
pub size: usize,
pub content: &'static [u8],
}
const RDF_CONTENT: &[u8] = br#"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
rdf:type a rdf:Property .
rdf:value a rdf:Property .
rdf:subject a rdf:Property .
rdf:predicate a rdf:Property .
rdf:object a rdf:Property .
rdf:Bag a rdfs:Class .
rdf:Seq a rdfs:Class .
rdf:Alt a rdfs:Class .
rdf:_1 a rdf:Property .
rdf:_2 a rdf:Property .
"#;
const RDFS_CONTENT: &[u8] = br#"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
rdfs:Class a rdfs:Class .
rdfs:Resource a rdfs:Class .
rdfs:Literal a rdfs:Class .
rdfs:Datatype a rdfs:Class .
rdfs:subClassOf a rdf:Property .
rdfs:subPropertyOf a rdf:Property .
rdfs:label a rdf:Property .
rdfs:comment a rdf:Property .
rdfs:domain a rdf:Property .
rdfs:range a rdf:Property .
"#;
const OWL_CONTENT: &[u8] = br#"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
owl:Class a rdfs:Class .
owl:ObjectProperty a rdfs:Class .
owl:DatatypeProperty a rdfs:Class .
owl:Ontology a rdfs:Class .
owl:Thing a owl:Class .
owl:Nothing a owl:Class .
owl:equivalentClass a rdf:Property .
owl:equivalentProperty a rdf:Property .
owl:inverseOf a rdf:Property .
owl:sameAs a rdf:Property .
"#;
static CORE_ONTOLOGIES: &[OntologyMetadata] = &[
OntologyMetadata {
name: "rdf",
namespace: "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
size: RDF_CONTENT.len(),
content: RDF_CONTENT,
},
OntologyMetadata {
name: "rdfs",
namespace: "http://www.w3.org/2000/01/rdf-schema#",
size: RDFS_CONTENT.len(),
content: RDFS_CONTENT,
},
OntologyMetadata {
name: "owl",
namespace: "http://www.w3.org/2002/07/owl#",
size: OWL_CONTENT.len(),
content: OWL_CONTENT,
},
];
#[derive(Debug, Clone, Copy)]
pub struct CoreOntologyBundle;
impl CoreOntologyBundle {
pub fn all() -> &'static [OntologyMetadata] {
CORE_ONTOLOGIES
}
pub fn by_namespace(uri: &str) -> Option<&'static OntologyMetadata> {
for (i, ont) in CORE_ONTOLOGIES.iter().enumerate() {
if ont.namespace == uri {
return Some(&CORE_ONTOLOGIES[i]);
}
}
None
}
pub fn by_name(name: &str) -> Option<&'static OntologyMetadata> {
for (i, ont) in CORE_ONTOLOGIES.iter().enumerate() {
if ont.name == name {
return Some(&CORE_ONTOLOGIES[i]);
}
}
None
}
pub fn available() -> Vec<(&'static str, &'static str)> {
CORE_ONTOLOGIES
.iter()
.map(|ont| (ont.name, ont.namespace))
.collect()
}
pub fn stats() -> OntologyStats {
let count = CORE_ONTOLOGIES.len();
let total_size_bytes = CORE_ONTOLOGIES.iter().map(|o| o.size).sum();
OntologyStats {
count,
total_size_bytes,
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct OntologyStats {
pub count: usize,
pub total_size_bytes: usize,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_core_ontologies_available() {
let ontologies = CoreOntologyBundle::all();
assert!(!ontologies.is_empty(), "Core ontologies should be embedded");
assert!(ontologies.len() >= 3, "Should have at least 3 core ontologies");
}
#[test]
fn test_lookup_by_namespace() {
let rdf = CoreOntologyBundle::by_namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#");
assert!(rdf.is_some(), "RDF ontology should be available");
if let Some(rdf_meta) = rdf {
assert_eq!(rdf_meta.name, "rdf");
assert!(!rdf_meta.content.is_empty(), "Ontology content should not be empty");
}
}
#[test]
fn test_lookup_by_name() {
let owl = CoreOntologyBundle::by_name("owl");
assert!(owl.is_some(), "OWL ontology should be available");
if let Some(owl_meta) = owl {
assert!(owl_meta.namespace.contains("owl"));
}
}
#[test]
fn test_available_list() {
let available = CoreOntologyBundle::available();
assert!(!available.is_empty(), "Should have at least one core ontology");
for (name, ns) in available {
assert!(!name.is_empty(), "Name should not be empty");
assert!(!ns.is_empty(), "Namespace should not be empty");
assert!(ns.starts_with("http"), "Namespace should be a valid URI");
}
}
#[test]
fn test_stats() {
let stats = CoreOntologyBundle::stats();
assert!(stats.count > 0, "Should have at least one core ontology");
assert!(stats.total_size_bytes > 0, "Total size should be greater than zero");
}
#[test]
fn test_all_ontologies_static_references() {
let ontologies = CoreOntologyBundle::all();
for ont in ontologies {
let _ = ont.name;
let _ = ont.namespace;
let _ = ont.size;
let _ = ont.content;
assert!(!ont.content.is_empty(), "Ontology {} should have content", ont.name);
}
}
#[test]
fn test_namespace_case_sensitive() {
assert!(CoreOntologyBundle::by_namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#")
.is_some());
assert!(CoreOntologyBundle::by_namespace("HTTP://www.w3.org/1999/02/22-rdf-syntax-ns#")
.is_none());
assert!(CoreOntologyBundle::by_namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns")
.is_none());
}
#[test]
fn test_name_case_sensitive() {
assert!(CoreOntologyBundle::by_name("rdf").is_some());
assert!(CoreOntologyBundle::by_name("RDF").is_none());
assert!(CoreOntologyBundle::by_name("Rdf").is_none());
}
#[test]
fn test_nonexistent_ontology_returns_none() {
assert!(CoreOntologyBundle::by_namespace("http://example.com/nonexistent#").is_none());
assert!(CoreOntologyBundle::by_name("nonexistent").is_none());
}
#[test]
fn test_available_contains_all_ontologies() {
let all = CoreOntologyBundle::all();
let available = CoreOntologyBundle::available();
assert_eq!(
all.len(),
available.len(),
"available() should return all ontologies"
);
for (i, ont) in all.iter().enumerate() {
let (name, ns) = available[i];
assert_eq!(name, ont.name, "Name mismatch at index {i}");
assert_eq!(ns, ont.namespace, "Namespace mismatch at index {i}");
}
}
#[test]
fn test_stats_accuracy() {
let stats = CoreOntologyBundle::stats();
let all = CoreOntologyBundle::all();
assert_eq!(stats.count, all.len(), "Stats count should match all() length");
let expected_size: usize = all.iter().map(|o| o.size).sum();
assert_eq!(
stats.total_size_bytes, expected_size,
"Stats total size should equal sum of all ontology sizes"
);
for ont in all {
assert_eq!(
ont.size,
ont.content.len(),
"Ontology {} size field must match content length",
ont.name
);
}
}
#[test]
fn test_content_not_empty() {
for ont in CoreOntologyBundle::all() {
assert!(
!ont.content.is_empty(),
"Ontology {} has empty content",
ont.name
);
assert!(
ont.size > 0,
"Ontology {} has zero size",
ont.name
);
assert_eq!(
ont.size, ont.content.len(),
"Ontology {} size doesn't match content",
ont.name
);
}
}
#[test]
fn test_hash_stability() {
let rdf1 = CoreOntologyBundle::by_namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#");
let rdf2 = CoreOntologyBundle::by_namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#");
assert!(rdf1.is_some() && rdf2.is_some());
assert_eq!(
rdf1.unwrap().content.as_ptr(),
rdf2.unwrap().content.as_ptr(),
"Same ontology should return identical content pointers"
);
assert_eq!(
rdf1.unwrap().content,
rdf2.unwrap().content,
"Same ontology should return identical content"
);
}
#[test]
fn test_metadata_clone() {
let original = CoreOntologyBundle::by_name("owl").expect("OWL should exist");
let cloned = original.clone();
assert_eq!(original.name, cloned.name);
assert_eq!(original.namespace, cloned.namespace);
assert_eq!(original.size, cloned.size);
assert_eq!(original.content, cloned.content);
}
#[test]
fn test_metadata_debug() {
let meta = CoreOntologyBundle::by_name("rdf").expect("RDF should exist");
let debug_str = format!("{:?}", meta);
assert!(debug_str.contains("rdf"));
assert!(debug_str.contains("namespace"));
assert!(debug_str.contains("size"));
}
#[test]
fn test_namespaces_valid_uris() {
for ont in CoreOntologyBundle::all() {
assert!(
ont.namespace.starts_with("http://") || ont.namespace.starts_with("https://"),
"Namespace '{}' should be a valid URI",
ont.namespace
);
assert!(
ont.namespace.ends_with("#"),
"RDF namespace '{}' should end with #",
ont.namespace
);
}
}
#[test]
fn test_namespace_format_variations() {
let rdf_full = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
let rdf_no_hash = "http://www.w3.org/1999/02/22-rdf-syntax-ns";
let rdf_with_trailing_slash = "http://www.w3.org/1999/02/22-rdf-syntax-ns/#";
assert!(CoreOntologyBundle::by_namespace(rdf_full).is_some());
assert!(CoreOntologyBundle::by_namespace(rdf_no_hash).is_none());
assert!(CoreOntologyBundle::by_namespace(rdf_with_trailing_slash).is_none());
}
#[test]
fn test_multiple_lookups_safe() {
for _ in 0..100 {
let _ = CoreOntologyBundle::by_namespace("http://www.w3.org/2002/07/owl#");
let _ = CoreOntologyBundle::by_name("rdfs");
let _ = CoreOntologyBundle::available();
let _ = CoreOntologyBundle::all();
let _ = CoreOntologyBundle::stats();
}
}
#[test]
fn test_core_bundle_copy_trait() {
let bundle1 = CoreOntologyBundle;
let bundle2 = bundle1;
assert_eq!(bundle1.all().len(), bundle2.all().len());
assert_eq!(
bundle1.by_name("owl").is_some(),
bundle2.by_name("owl").is_some()
);
}
}