@@ -30,7 +30,7 @@ use std::fs;
3030use std:: io:: { BufReader , Write } ;
3131use std:: path:: Path ;
3232use walkdir:: WalkDir ;
33- use std:: fmt;
33+ use std:: fmt:: { self , Display } ;
3434
3535// custom derive for ontologies field as vec of Ontology
3636fn ontologies_ser < S > (
5656 Ok ( map)
5757}
5858
59+ pub struct FailedImport {
60+ ontology : GraphIdentifier ,
61+ error : String ,
62+ }
63+
64+ impl FailedImport {
65+ pub fn new ( ontology : GraphIdentifier , error : String ) -> Self {
66+ Self { ontology, error }
67+ }
68+ }
69+
70+ impl Display for FailedImport {
71+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
72+ write ! ( f, "Failed to import ontology {}: {}" , self . ontology, self . error)
73+ }
74+ }
75+
5976pub struct EnvironmentStatus {
6077 // true if there is an environment that ontoenv can find
6178 exists : bool ,
@@ -831,25 +848,31 @@ impl OntoEnv {
831848 Ok ( closure)
832849 }
833850
834- /// Returns a graph containing the union of all graphs_ids
851+ /// Returns a graph containing the union of all graphs_ids, along with a list of
852+ /// graphs that could and could not be imported.
835853 pub fn get_union_graph (
836854 & self ,
837855 graph_ids : & [ GraphIdentifier ] ,
838856 rewrite_sh_prefixes : Option < bool > ,
839857 remove_owl_imports : Option < bool > ,
840- // TODO: remove_ontology_declarations
841- ) -> Result < Dataset > {
858+ ) -> Result < ( Dataset , Vec < GraphIdentifier > , Option < Vec < FailedImport > > ) > {
842859 // compute union of all graphs
843860 let mut union: Dataset = Dataset :: new ( ) ;
844861 let store = self . store ( ) ;
862+ let mut failed_imports: Vec < FailedImport > = vec ! [ ] ;
863+ let mut successful_imports: Vec < GraphIdentifier > = vec ! [ ] ;
845864 for id in graph_ids {
846865 let graphname: NamedOrBlankNode = match id. graphname ( ) ? {
847866 GraphName :: NamedNode ( n) => NamedOrBlankNode :: NamedNode ( n) ,
848867 _ => continue ,
849868 } ;
850869
851870 if !store. contains_named_graph ( graphname. as_ref ( ) ) ? {
852- return Err ( anyhow:: anyhow!( "Graph not found: {:?}" , id) ) ;
871+ failed_imports. push ( FailedImport {
872+ ontology : id. clone ( ) ,
873+ error : "Graph not found" . to_string ( ) ,
874+ } ) ;
875+ continue ;
853876 }
854877
855878 let mut count = 0 ;
@@ -881,12 +904,9 @@ impl OntoEnv {
881904 ONTOLOGY ,
882905 graphname. as_ref ( ) ,
883906 ) ;
884- if !union. remove ( to_remove) {
885- error ! ( "Failed to remove ontology declaration: {:?}" , to_remove) ;
886- }
907+ union. remove ( to_remove) ;
887908 }
888-
889-
909+ successful_imports. push ( id. clone ( ) ) ;
890910 info ! ( "Added {} triples from graph: {:?}" , count, id) ;
891911 }
892912 let first_id = graph_ids
@@ -896,15 +916,22 @@ impl OntoEnv {
896916
897917 // Rewrite sh:prefixes
898918 // defaults to true if not specified
899- if let Some ( true ) = rewrite_sh_prefixes. or ( Some ( true ) ) {
919+ if rewrite_sh_prefixes. unwrap_or ( true ) {
900920 transform:: rewrite_sh_prefixes ( & mut union, root_ontology) ;
901921 }
902922 // remove owl:imports
903- if let Some ( true ) = remove_owl_imports. or ( Some ( true ) ) {
904- transform:: remove_owl_imports ( & mut union)
923+ if remove_owl_imports. unwrap_or ( true ) {
924+ let to_remove: Vec < NamedNodeRef > = graph_ids. iter ( ) . map ( |id| id. into ( ) ) . collect ( ) ;
925+ println ! ( "Removing owl:imports: {:?}" , to_remove) ;
926+ transform:: remove_owl_imports ( & mut union, Some ( & to_remove) ) ;
905927 }
906928 transform:: remove_ontology_declarations ( & mut union, root_ontology) ;
907- Ok ( union)
929+ let failed_imports = if failed_imports. is_empty ( ) {
930+ None
931+ } else {
932+ Some ( failed_imports)
933+ } ;
934+ Ok ( ( union, successful_imports, failed_imports) )
908935 }
909936
910937 /// Returns a list of issues with the environment
0 commit comments