11#![ feature( once_cell_try) ]
22use :: ontoenv as ontoenvrs;
3- use :: ontoenv:: consts:: { ONTOLOGY , TYPE } ;
3+ use :: ontoenv:: consts:: { ONTOLOGY , TYPE , IMPORTS } ;
44use :: ontoenv:: ontology:: OntologyLocation ;
55use :: ontoenv:: transform;
66use anyhow:: Error ;
@@ -118,14 +118,15 @@ fn term_to_python<'a>(
118118}
119119
120120#[ pyclass]
121+ #[ derive( Clone ) ]
121122struct Config {
122123 cfg : ontoenvrs:: config:: Config ,
123124}
124125
125126#[ pymethods]
126127impl Config {
127128 #[ new]
128- #[ pyo3( signature = ( search_directories=None , require_ontology_names=false , strict=false , offline=false , resolution_policy="default" . to_owned( ) , root="." . to_owned( ) , includes=vec! [ ] , excludes=vec! [ ] ) ) ]
129+ #[ pyo3( signature = ( search_directories=None , require_ontology_names=false , strict=false , offline=false , resolution_policy="default" . to_owned( ) , root="." . to_owned( ) , includes=None , excludes=None ) ) ]
129130 fn new (
130131 search_directories : Option < Vec < String > > ,
131132 require_ontology_names : bool ,
@@ -173,10 +174,10 @@ struct OntoEnv {
173174#[ pymethods]
174175impl OntoEnv {
175176 #[ new]
176- #[ pyo3( signature = ( config=None , path=Path :: new( "." ) . to_owned( ) , recreate=false , read_only=false ) ) ]
177+ #[ pyo3( signature = ( config=None , path=Some ( Path :: new( "." ) . to_owned( ) ) , recreate=false , read_only=false ) ) ]
177178 fn new (
178179 _py : Python ,
179- config : Option < & Config > ,
180+ config : Option < Config > ,
180181 path : Option < PathBuf > ,
181182 recreate : bool ,
182183 read_only : bool ,
@@ -280,7 +281,8 @@ impl OntoEnv {
280281 transform:: rewrite_sh_prefixes_graph ( & mut graph, base_ontology) ;
281282 transform:: remove_ontology_declarations_graph ( & mut graph, base_ontology) ;
282283 }
283- transform:: remove_owl_imports_graph ( & mut graph) ;
284+ // remove the owl:import statement for the 'uri' ontology
285+ transform:: remove_owl_imports_graph ( & mut graph, Some ( & [ ( & iri) . into ( ) ] ) ) ;
284286
285287 Python :: with_gil ( |_py| {
286288 for triple in graph. into_iter ( ) {
@@ -304,6 +306,7 @@ impl OntoEnv {
304306 Ok ( ( ) )
305307 }
306308
309+ /// List the ontologies in the imports closure of the given ontology
307310 #[ pyo3( signature = ( uri) ) ]
308311 fn list_closure ( & self , py : Python , uri : & str ) -> PyResult < Vec < String > > {
309312 let iri = NamedNode :: new ( uri)
@@ -320,6 +323,9 @@ impl OntoEnv {
320323 Ok ( names)
321324 }
322325
326+ /// Merge all graphs in the imports closure of the given ontology into a single graph. If
327+ /// destination_graph is provided, add the merged graph to the destination_graph. If not,
328+ /// return the merged graph.
323329 #[ pyo3( signature = ( uri, destination_graph=None , rewrite_sh_prefixes=false , remove_owl_imports=false ) ) ]
324330 fn get_closure < ' a > (
325331 & self ,
@@ -345,7 +351,7 @@ impl OntoEnv {
345351 Some ( g) => g. clone ( ) ,
346352 None => rdflib. getattr ( "Graph" ) ?. call0 ( ) ?,
347353 } ;
348- let graph = env
354+ let ( graph, successful_imports , failed_imports ) = env
349355 . get_union_graph (
350356 & closure,
351357 Some ( rewrite_sh_prefixes) ,
@@ -357,7 +363,6 @@ impl OntoEnv {
357363 let s: Term = triple. subject . into ( ) ;
358364 let p: Term = triple. predicate . into ( ) ;
359365 let o: Term = triple. object . into ( ) ;
360-
361366 let t = PyTuple :: new (
362367 py,
363368 & [
@@ -368,10 +373,24 @@ impl OntoEnv {
368373 ) ?;
369374 destination_graph. getattr ( "add" ) ?. call1 ( ( t, ) ) ?;
370375 }
376+
377+ // Remove each successful_imports url in the closure from the destination_graph
378+ if remove_owl_imports {
379+ for graphid in successful_imports {
380+ let iri = term_to_python ( py, & rdflib, Term :: NamedNode ( graphid. into ( ) ) ) ?;
381+ let pred = term_to_python ( py, & rdflib, IMPORTS . into ( ) ) ?;
382+ // remove triples with (None, pred, iri)
383+ let remove_tuple = PyTuple :: new ( py, & [ py. None ( ) , pred. into ( ) , iri. into ( ) ] ) ?;
384+ destination_graph. getattr ( "remove" ) ?. call1 ( ( remove_tuple, ) ) ?;
385+ }
386+ }
387+
388+ // Remove each url in the closure from the destination_graph
371389 return Ok :: < Bound < ' _ , PyAny > , PyErr > ( destination_graph) ;
372390 } )
373391 }
374392
393+ /// Print the contents of the OntoEnv
375394 #[ pyo3( signature = ( includes=None ) ) ]
376395 fn dump ( & self , py : Python , includes : Option < String > ) -> PyResult < ( ) > {
377396 let inner = self . inner . clone ( ) ;
@@ -380,6 +399,8 @@ impl OntoEnv {
380399 Ok ( ( ) )
381400 }
382401
402+ /// Import the dependencies of the given graph into the graph. Removes the owl:imports
403+ /// of all imported ontologies.
383404 fn import_dependencies < ' a > (
384405 & self ,
385406 py : Python < ' a > ,
@@ -401,6 +422,7 @@ impl OntoEnv {
401422 self . get_closure ( py, & ontology, Some ( graph) , true , true )
402423 }
403424
425+ /// Add a new ontology to the OntoEnv
404426 fn add ( & self , location : & Bound < ' _ , PyAny > ) -> PyResult < ( ) > {
405427 let inner = self . inner . clone ( ) ;
406428 let mut env = inner. lock ( ) . unwrap ( ) ;
@@ -411,6 +433,8 @@ impl OntoEnv {
411433 Ok ( ( ) )
412434 }
413435
436+ /// Refresh the OntoEnv by re-loading all remote graphs and loading
437+ /// any local graphs which have changed since the last update
414438 fn refresh ( & self ) -> PyResult < ( ) > {
415439 let inner = self . inner . clone ( ) ;
416440 let mut env = inner. lock ( ) . unwrap ( ) ;
@@ -419,6 +443,7 @@ impl OntoEnv {
419443 Ok ( ( ) )
420444 }
421445
446+ /// Export the graph with the given URI to an rdflib.Graph
422447 fn get_graph ( & self , py : Python , uri : & Bound < ' _ , PyString > ) -> PyResult < Py < PyAny > > {
423448 let rdflib = py. import ( "rdflib" ) ?;
424449 let iri = NamedNode :: new ( uri. to_string ( ) )
@@ -451,6 +476,7 @@ impl OntoEnv {
451476 Ok ( res. into ( ) )
452477 }
453478
479+ /// Get the names of all ontologies in the OntoEnv
454480 fn get_ontology_names ( & self ) -> PyResult < Vec < String > > {
455481 let inner = self . inner . clone ( ) ;
456482 let env = inner. lock ( ) . unwrap ( ) ;
0 commit comments