Skip to content

Commit d50296b

Browse files
committed
Handle all possible ConversionException
1 parent 8241aa1 commit d50296b

5 files changed

Lines changed: 57 additions & 18 deletions

File tree

src/ubic/basecode/ontology/jena/AbstractOntologyService.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -390,14 +390,20 @@ public Collection<OntologyResource> findResources( String searchString, boolean
390390
return OntologySearch.matchResources( model, index, searchString )
391391
.filterKeep( where( r -> r.result.canAs( OntClass.class ) || r.result.canAs( Individual.class ) ) )
392392
.mapWith( r -> {
393-
OntologyResource res;
394-
if ( r.result.canAs( OntClass.class ) ) {
395-
res = new OntologyTermImpl( r.result.as( OntClass.class ), additionalRestrictions, r.score );
396-
} else {
397-
res = new OntologyIndividualImpl( r.result.as( Individual.class ), additionalRestrictions, r.score );
393+
try {
394+
if ( r.result.canAs( OntClass.class ) ) {
395+
return new OntologyTermImpl( r.result.as( OntClass.class ), additionalRestrictions, r.score );
396+
} else if ( r.result.canAs( Individual.class ) ) {
397+
return new OntologyIndividualImpl( r.result.as( Individual.class ), additionalRestrictions, r.score );
398+
} else {
399+
return ( OntologyResource ) null;
400+
}
401+
} catch ( ConversionException e ) {
402+
log.error( "Conversion failed for " + r, e );
403+
return null;
398404
}
399-
return res;
400405
} )
406+
.filterKeep( where( Objects::nonNull ) )
401407
.filterKeep( where( ontologyTerm -> keepObsoletes || !ontologyTerm.isObsolete() ) )
402408
.toSet();
403409
} finally {

src/ubic/basecode/ontology/jena/IndexerSelector.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@
1818
*/
1919
package ubic.basecode.ontology.jena;
2020

21+
import com.hp.hpl.jena.ontology.ConversionException;
2122
import com.hp.hpl.jena.rdf.model.*;
2223
import com.hp.hpl.jena.vocabulary.OWL2;
2324
import com.hp.hpl.jena.vocabulary.RDFS;
25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
2427

2528
import java.util.Collection;
2629
import java.util.HashSet;
@@ -33,6 +36,8 @@
3336
*/
3437
class IndexerSelector implements Selector {
3538

39+
private static final Logger log = LoggerFactory.getLogger( IndexerSelector.class );
40+
3641
private static final Collection<Property> wantedForIndexing;
3742

3843
static {
@@ -104,8 +109,12 @@ public boolean test( Statement s ) {
104109
boolean retain = wantedForIndexing.contains( s.getPredicate() );
105110

106111
// bit of a special case ...
107-
if ( s.getPredicate().equals( OWL2.annotatedProperty ) ) {
108-
retain = wantedForIndexing.contains( s.getObject().as( Property.class ) );
112+
if ( s.getPredicate().equals( OWL2.annotatedProperty ) && s.getObject().canAs( Property.class ) ) {
113+
try {
114+
retain = wantedForIndexing.contains( s.getObject().as( Property.class ) );
115+
} catch ( ConversionException e ) {
116+
log.error( "Conversion failed for " + s.getObject(), e );
117+
}
109118
}
110119

111120
return retain;

src/ubic/basecode/ontology/jena/JenaUtils.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,15 @@ public static Collection<OntClass> getChildrenInternal( OntModel model, Collecti
149149
for ( Restriction r : restrictions ) {
150150
result.addAll( model.listResourcesWithProperty( subClassOf, r )
151151
.filterDrop( new BnodeFilter<>() )
152-
.mapWith( r2 -> r2.as( OntClass.class ) )
152+
.mapWith( r2 -> {
153+
try {
154+
return r2.as( OntClass.class );
155+
} catch ( ConversionException e ) {
156+
log.error( "Conversion failed for " + r2, e );
157+
return null;
158+
}
159+
} )
160+
.filterKeep( where( Objects::nonNull ) )
153161
.toSet() );
154162
}
155163
}

src/ubic/basecode/ontology/jena/OntologySearch.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package ubic.basecode.ontology.jena;
2020

21+
import com.hp.hpl.jena.ontology.ConversionException;
2122
import com.hp.hpl.jena.ontology.Individual;
2223
import com.hp.hpl.jena.ontology.OntClass;
2324
import 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
}

src/ubic/basecode/ontology/jena/OntologyTermImpl.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515
package ubic.basecode.ontology.jena;
1616

17+
import com.hp.hpl.jena.ontology.ConversionException;
1718
import com.hp.hpl.jena.ontology.OntClass;
1819
import com.hp.hpl.jena.ontology.OntResource;
1920
import com.hp.hpl.jena.ontology.Restriction;
@@ -86,11 +87,17 @@ public Collection<AnnotationProperty> getAnnotations() {
8687
// this is a little slow because we have to go through all statements for the term.
8788
while ( iterator.hasNext() ) {
8889
Statement state = iterator.next();
89-
OntResource res = state.getPredicate().as( OntResource.class );
90-
if ( res.isAnnotationProperty() ) {
91-
com.hp.hpl.jena.ontology.AnnotationProperty p = res.asAnnotationProperty();
92-
RDFNode n = state.getObject();
93-
annots.add( new AnnotationPropertyImpl( p, n ) );
90+
if ( state.getPredicate().canAs( OntResource.class ) ) {
91+
try {
92+
OntResource res = state.getPredicate().as( OntResource.class );
93+
if ( res.isAnnotationProperty() ) {
94+
com.hp.hpl.jena.ontology.AnnotationProperty p = res.asAnnotationProperty();
95+
RDFNode n = state.getObject();
96+
annots.add( new AnnotationPropertyImpl( p, n ) );
97+
}
98+
} catch ( ConversionException e ) {
99+
log.error( "Conversion failed for " + state.getPredicate(), e );
100+
}
94101
}
95102
}
96103
return annots;

0 commit comments

Comments
 (0)