Skip to content

Commit

Permalink
Revisit parents/children of terms to make sure none were missed
Browse files Browse the repository at this point in the history
When using a limited reasoner (i.e. none or transitive), some of the
ancestors or descendants of a term may be missed. To compensate for
that, do a few additional direct checks.
  • Loading branch information
arteymix committed Sep 18, 2023
1 parent 6f62477 commit f31c6c7
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/ubic/basecode/ontology/jena/JenaUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.hp.hpl.jena.util.iterator.Filter;
import com.hp.hpl.jena.util.iterator.UniqueExtendedIterator;
import org.apache.commons.lang3.time.StopWatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nullable;
import java.util.*;
Expand All @@ -18,7 +20,25 @@

class JenaUtils {

protected static Logger log = LoggerFactory.getLogger( JenaUtils.class );

public static Collection<OntClass> getParents( OntModel model, Collection<OntClass> ontClasses, boolean direct, @Nullable Set<Restriction> additionalRestrictions ) {
Collection<OntClass> parents = getParentsInternal( model, ontClasses, direct, additionalRestrictions );
if ( !direct && !parents.isEmpty() && additionalRestrictions != null && !supportsAdditionalPropertyInference( model ) ) {
// if there are some missing direct parents, revisit the hierarchy
Set<OntClass> parentsToRevisit = new HashSet<>( parents );
while ( !parentsToRevisit.isEmpty() ) {
log.debug( "Revisiting the direct parents of {} terms...", parentsToRevisit.size() );
parentsToRevisit = new HashSet<>( getParentsInternal( model, parentsToRevisit, true, additionalRestrictions ) );
parentsToRevisit.removeAll( parents );
log.debug( "Found {} missed parents.", parentsToRevisit.size() );
parents.addAll( parentsToRevisit );
}
}
return ontClasses;
}

private static Collection<OntClass> getParentsInternal( OntModel model, Collection<OntClass> ontClasses, boolean direct, @Nullable Set<Restriction> additionalRestrictions ) {
ontClasses = ontClasses.stream()
.map( t -> t.inModel( model ) )
.filter( t -> t.canAs( OntClass.class ) )
Expand Down Expand Up @@ -65,6 +85,23 @@ public static Collection<OntClass> getParents( OntModel model, Collection<OntCla
}

public static Collection<OntClass> getChildren( OntModel model, Collection<OntClass> terms, boolean direct, @Nullable Set<Restriction> additionalRestrictions ) {
Collection<OntClass> children = getChildrenInternal( model, terms, direct, additionalRestrictions );
// collect any remaining children, this is useful if inference is incomplete (i.e. if not using a suitable OWL reasoner)
if ( !direct && !children.isEmpty() && additionalRestrictions != null && !supportsAdditionalPropertyInference( model ) ) {
// if there are some missing direct children, revisit the hierarchy
Set<OntClass> childrenToRevisit = new HashSet<>( children );
while ( !childrenToRevisit.isEmpty() ) {
log.debug( "Revisiting the direct parents of {} terms...", childrenToRevisit.size() );
childrenToRevisit = new HashSet<>( JenaUtils.getChildrenInternal( model, childrenToRevisit, true, additionalRestrictions ) );
childrenToRevisit.removeAll( children );
log.debug( "Found {} missed children.", childrenToRevisit.size() );
children.addAll( childrenToRevisit );
}
}
return children;
}

public static Collection<OntClass> getChildrenInternal( OntModel model, Collection<OntClass> terms, boolean direct, @Nullable Set<Restriction> additionalRestrictions ) {
terms = terms.stream()
.map( t -> t.inModel( model ) )
.filter( t -> t.canAs( OntClass.class ) )
Expand Down Expand Up @@ -103,6 +140,11 @@ public static Collection<OntClass> getChildren( OntModel model, Collection<OntCl
return result;
}

private static boolean supportsAdditionalPropertyInference( OntModel model ) {
return model.getReasoner().supportsProperty( model.getProfile().SOME_VALUES_FROM() )
&& model.getReasoner().supportsProperty( model.getProfile().ALL_VALUES_FROM() );
}

public static Resource getRestrictionValue( Restriction r ) {
if ( r.isSomeValuesFromRestriction() ) {
return r.asSomeValuesFromRestriction().getSomeValuesFrom();
Expand Down

0 comments on commit f31c6c7

Please sign in to comment.