1818 */
1919package ubic .basecode .ontology .jena ;
2020
21+ import com .hp .hpl .jena .ontology .ConversionException ;
2122import com .hp .hpl .jena .ontology .Individual ;
2223import com .hp .hpl .jena .ontology .OntClass ;
2324import com .hp .hpl .jena .ontology .OntModel ;
@@ -60,7 +61,8 @@ class OntologySearch {
6061 public static ExtendedIterator <SearchResult <OntClass >> matchClasses ( OntModel model , SearchIndex index , String queryString ) throws OntologySearchException {
6162 return runSearch ( model , index , queryString )
6263 .filterKeep ( where ( r -> r .result .isURIResource () && r .result .canAs ( OntClass .class ) ) )
63- .mapWith ( r -> r .as ( OntClass .class ) );
64+ .mapWith ( r -> r .as ( OntClass .class ) )
65+ .filterKeep ( where ( Objects ::nonNull ) );
6466 }
6567
6668 /**
@@ -73,7 +75,8 @@ public static ExtendedIterator<SearchResult<OntClass>> matchClasses( OntModel mo
7375 public static ExtendedIterator <SearchResult <Individual >> matchIndividuals ( OntModel model , SearchIndex index , String queryString ) throws OntologySearchException {
7476 return runSearchWithWildcard ( model , index , queryString )
7577 .filterKeep ( where ( r -> r .result .isURIResource () && r .result .canAs ( Individual .class ) ) )
76- .mapWith ( r -> r .as ( Individual .class ) );
78+ .mapWith ( r -> r .as ( Individual .class ) )
79+ .filterKeep ( where ( Objects ::nonNull ) );
7780 }
7881
7982 /**
@@ -87,7 +90,8 @@ public static ExtendedIterator<SearchResult<Individual>> matchIndividuals( OntMo
8790 public static ExtendedIterator <SearchResult <Resource >> matchResources ( OntModel model , SearchIndex index , String queryString ) throws OntologySearchException {
8891 return runSearchWithWildcard ( model , index , queryString )
8992 .filterKeep ( where ( o -> o .result .isURIResource () && o .result .isResource () ) )
90- .mapWith ( r -> r .as ( Resource .class ) );
93+ .mapWith ( r -> r .as ( Resource .class ) )
94+ .filterKeep ( where ( Objects ::nonNull ) );
9195 }
9296
9397 private static ExtendedIterator <SearchResult <RDFNode >> runSearchWithWildcard ( Model model , SearchIndex index , String queryString ) throws OntologySearchException {
@@ -180,7 +184,12 @@ public String toString() {
180184 }
181185
182186 private <U extends Resource > SearchResult <U > as ( Class <U > clazz ) {
183- return new SearchResult <>( docId , result .as ( clazz ), score );
187+ try {
188+ return new SearchResult <>( docId , result .as ( clazz ), score );
189+ } catch ( ConversionException e ) {
190+ log .error ( "Conversion failed for " + result , e );
191+ return null ;
192+ }
184193 }
185194 }
186195}
0 commit comments