ontoenv
is an environment manager for ontologies. It can be used as a Rust library to manage local and remote RDF ontologies and their dependencies.
It recursively discovers and resolves owl:imports
statements, and provides an API for querying the dependency graph and retrieving a unified "imports closure" of an ontology.
The environment is backed by an Oxigraph
store.
Usage
Here is a basic example of how to use the ontoenv
Rust library. This example will:
- Create a temporary directory.
- Write two simple ontologies to files in that directory, where one imports the other.
- Configure and initialize
ontoenv
to use this directory.
- Compute the dependency closure of one ontology to demonstrate that
ontoenv
correctly resolves and includes the imported ontology.
use ontoenv::config::Config;
use ontoenv::ToUriString;
use ontoenv::api::{OntoEnv, ResolveTarget};
use oxigraph::model::NamedNode;
use std::path::PathBuf;
use std::fs;
use std::io::Write;
use std::collections::HashSet;
# fn main() -> anyhow::Result<()> {
let test_dir = PathBuf::from("target/doc_test_temp_readme");
if test_dir.exists() {
fs::remove_dir_all(&test_dir)?;
}
fs::create_dir_all(&test_dir)?;
let root = test_dir.canonicalize()?;
let ontology_a_path = root.join("ontology_a.ttl");
let mut file_a = fs::File::create(&ontology_a_path)?;
writeln!(file_a, r#"
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix : <http://example.com/ontology_a#> .
<http://example.com/ontology_a> a owl:Ontology .
"#)?;
let ontology_b_path = root.join("ontology_b.ttl");
let mut file_b = fs::File::create(&ontology_b_path)?;
writeln!(file_b, r#"
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix : <http://example.com/ontology_b#> .
<http://example.com/ontology_b> a owl:Ontology ;
owl:imports <http://example.com/ontology_a> .
"#)?;
let config = Config::builder()
.root(root.clone())
.locations(vec![root.clone()])
.temporary(true) .build()?;
let mut env = OntoEnv::init(config, false)?;
env.update()?;
let ontologies = env.ontologies();
assert_eq!(ontologies.len(), 2);
let ont_b_name = NamedNode::new("http://example.com/ontology_b")?;
let ont_b_id = env.resolve(ResolveTarget::Graph(ont_b_name)).unwrap();
let closure_ids = env.get_closure(&ont_b_id, -1)?;
assert_eq!(closure_ids.len(), 2);
let closure_names: HashSet<String> = closure_ids.iter().map(|id| id.to_uri_string()).collect();
assert!(closure_names.contains("http://example.com/ontology_a"));
assert!(closure_names.contains("http://example.com/ontology_b"));
let union_graph_result = env.get_union_graph(&closure_ids, Some(false), Some(false))?;
assert_eq!(union_graph_result.dataset.len(), 2);
fs::remove_dir_all(&test_dir)?;
# Ok(())
# }