Skip to content

Commit bfefd50

Browse files
committed
make sure to load store when loading environment from file. Add get_dependents method
1 parent 8595ff0 commit bfefd50

File tree

7 files changed

+56
-10
lines changed

7 files changed

+56
-10
lines changed

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ members = [
77
resolver = "2"
88

99
[workspace.package]
10-
version = "0.1.10-a8"
10+
version = "0.1.10-a9"
1111
authors = ["Gabe Fierro <[email protected]>"]
1212
license = "BSD-3-Clause"
1313
edition = "2021"
@@ -35,7 +35,7 @@ clap = { version = "4.4.18", features = ["derive"] }
3535
derive_builder = "0.20"
3636
oxigraph = "0.4.4"
3737

38-
ontoenv = { version = "0.1.10-a8", path = "lib" }
38+
ontoenv = { version = "0.1.10-a9", path = "lib" }
3939

4040
[profile.profiling]
4141
inherits = "release"

cli/src/main.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ enum Commands {
108108
#[clap(long, short)]
109109
output: Option<String>,
110110
},
111+
/// Lists all ontologies which depend on the given ontology
112+
Dependents {
113+
/// The name (URI) of the ontology to find dependents for
114+
ontologies: Vec<String>,
115+
},
111116
/// Run the doctor to check the environment for issues
112117
Doctor,
113118
/// Reset the ontology environment by removing the .ontoenv directory
@@ -294,6 +299,19 @@ fn main() -> Result<()> {
294299
));
295300
}
296301
}
302+
Commands::Dependents { ontologies } => {
303+
// load env from .ontoenv/ontoenv.json
304+
let path = current_dir()?.join(".ontoenv/ontoenv.json");
305+
let env = OntoEnv::from_file(&path, true)?;
306+
for ont in ontologies {
307+
let iri = NamedNode::new(ont).map_err(|e| anyhow::anyhow!(e.to_string()))?;
308+
let dependents = env.get_dependents(&iri)?;
309+
println!("Dependents of {}: ", iri);
310+
for dep in dependents {
311+
println!("{}", dep);
312+
}
313+
}
314+
}
297315
Commands::Doctor => {
298316
// load env from .ontoenv/ontoenv.json
299317
let path = current_dir()?.join(".ontoenv/ontoenv.json");

lib/src/lib.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ impl OntoEnv {
201201
how_created,
202202
inner_store: None,
203203
};
204-
env.inner_store = Some(env.get_store()?);
204+
env.inner_store = Some(env.get_store(env.read_only)?);
205205
Ok(env)
206206
}
207207

@@ -215,10 +215,10 @@ impl OntoEnv {
215215
}
216216

217217
// TODO: add a read-only version? make this thread-safe?
218-
fn get_store(&self) -> Result<Store> {
218+
fn get_store(&self, read_only: bool) -> Result<Store> {
219219
let ontoenv_dir = self.config.root.join(".ontoenv");
220220
std::fs::create_dir_all(&ontoenv_dir)?;
221-
if self.read_only {
221+
if read_only {
222222
return Store::open_read_only(ontoenv_dir.join("store.db"))
223223
.map_err(|e| anyhow::anyhow!("Could not open store: {}", e));
224224
}
@@ -330,7 +330,8 @@ impl OntoEnv {
330330

331331
let file = std::fs::File::open(path)?;
332332
let reader = BufReader::new(file);
333-
let env: OntoEnv = serde_json::from_reader(reader)?;
333+
let mut env: OntoEnv = serde_json::from_reader(reader)?;
334+
env.inner_store = Some(env.get_store(read_only)?);
334335
Ok(Self { read_only, ..env })
335336
}
336337

@@ -961,6 +962,17 @@ impl OntoEnv {
961962
}
962963
}
963964

965+
/// Returns a list of all ontologies that depend on the given ontology
966+
pub fn get_dependents(&self, id: &NamedNode) -> Result<Vec<GraphIdentifier>> {
967+
let mut dependents = vec![];
968+
for ontology in self.ontologies.values() {
969+
if ontology.imports.contains(&id) {
970+
dependents.push(ontology.id().clone());
971+
}
972+
}
973+
Ok(dependents)
974+
}
975+
964976
/// Outputs a human-readable dump of the environment, including all ontologies
965977
/// and their metadata and imports
966978
pub fn dump(&self, contains: Option<&str>) {

python/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ features = ["pyo3/extension-module"]
1818

1919
[tool.poetry]
2020
name = "ontoenv"
21-
version = "0.1.10a8"
21+
version = "0.1.10a9"
2222
description = "Python bindings for the OntoEnv Rust library. Manages ontology-based environments for building knowledge graphs."
2323
license = "bsd-3-clause"
2424
authors = ["Gabe Fierro <[email protected]>"]

python/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ impl OntoEnv {
401401

402402
/// Import the dependencies of the given graph into the graph. Removes the owl:imports
403403
/// of all imported ontologies.
404+
#[pyo3(signature = (graph))]
404405
fn import_dependencies<'a>(
405406
&self,
406407
py: Python<'a>,
@@ -443,6 +444,19 @@ impl OntoEnv {
443444
Ok(())
444445
}
445446

447+
/// Get the names of all ontologies that depend on the given ontology
448+
fn get_dependents(&self, uri: &str) -> PyResult<Vec<String>> {
449+
let iri = NamedNode::new(uri)
450+
.map_err(|e| PyErr::new::<pyo3::exceptions::PyValueError, _>(e.to_string()))?;
451+
let inner = self.inner.clone();
452+
let env = inner.lock().unwrap();
453+
let dependents = env
454+
.get_dependents(&iri)
455+
.map_err(anyhow_to_pyerr)?;
456+
let names: Vec<String> = dependents.iter().map(|ont| ont.name().to_string()).collect();
457+
Ok(names)
458+
}
459+
446460
/// Export the graph with the given URI to an rdflib.Graph
447461
fn get_graph(&self, py: Python, uri: &Bound<'_, PyString>) -> PyResult<Py<PyAny>> {
448462
let rdflib = py.import("rdflib")?;

python/test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
env2.import_graph(brick, "https://w3id.org/rec")
3434
brick.serialize("test.ttl", format="turtle")
3535

36+
print(env2.get_dependents('https://brickschema.org/schema/1.4-rc1/Brick'))
37+
3638
# get an rdflib.Dataset (https://rdflib.readthedocs.io/en/stable/apidocs/rdflib.html#rdflib.Dataset)
3739
ds = env2.to_rdflib_dataset()
3840
for graphname in ds.graphs():

0 commit comments

Comments
 (0)