|
8 | 8 | import com.hp.hpl.jena.util.iterator.Filter; |
9 | 9 | import com.hp.hpl.jena.util.iterator.UniqueExtendedIterator; |
10 | 10 | import org.apache.commons.lang3.time.StopWatch; |
| 11 | +import org.slf4j.Logger; |
| 12 | +import org.slf4j.LoggerFactory; |
11 | 13 |
|
12 | 14 | import javax.annotation.Nullable; |
13 | 15 | import java.util.*; |
|
18 | 20 |
|
19 | 21 | class JenaUtils { |
20 | 22 |
|
| 23 | + protected static Logger log = LoggerFactory.getLogger( JenaUtils.class ); |
| 24 | + |
21 | 25 | public static Collection<OntClass> getParents( OntModel model, Collection<OntClass> ontClasses, boolean direct, @Nullable Set<Restriction> additionalRestrictions ) { |
| 26 | + Collection<OntClass> parents = getParentsInternal( model, ontClasses, direct, additionalRestrictions ); |
| 27 | + if ( !direct && !parents.isEmpty() && additionalRestrictions != null && !supportsAdditionalPropertyInference( model ) ) { |
| 28 | + // if there are some missing direct parents, revisit the hierarchy |
| 29 | + Set<OntClass> parentsToRevisit = new HashSet<>( parents ); |
| 30 | + while ( !parentsToRevisit.isEmpty() ) { |
| 31 | + log.debug( "Revisiting the direct parents of {} terms...", parentsToRevisit.size() ); |
| 32 | + parentsToRevisit = new HashSet<>( getParentsInternal( model, parentsToRevisit, true, additionalRestrictions ) ); |
| 33 | + parentsToRevisit.removeAll( parents ); |
| 34 | + log.debug( "Found {} missed parents.", parentsToRevisit.size() ); |
| 35 | + parents.addAll( parentsToRevisit ); |
| 36 | + } |
| 37 | + } |
| 38 | + return ontClasses; |
| 39 | + } |
| 40 | + |
| 41 | + private static Collection<OntClass> getParentsInternal( OntModel model, Collection<OntClass> ontClasses, boolean direct, @Nullable Set<Restriction> additionalRestrictions ) { |
22 | 42 | ontClasses = ontClasses.stream() |
23 | 43 | .map( t -> t.inModel( model ) ) |
24 | 44 | .filter( t -> t.canAs( OntClass.class ) ) |
@@ -65,6 +85,23 @@ public static Collection<OntClass> getParents( OntModel model, Collection<OntCla |
65 | 85 | } |
66 | 86 |
|
67 | 87 | public static Collection<OntClass> getChildren( OntModel model, Collection<OntClass> terms, boolean direct, @Nullable Set<Restriction> additionalRestrictions ) { |
| 88 | + Collection<OntClass> children = getChildrenInternal( model, terms, direct, additionalRestrictions ); |
| 89 | + // collect any remaining children, this is useful if inference is incomplete (i.e. if not using a suitable OWL reasoner) |
| 90 | + if ( !direct && !children.isEmpty() && additionalRestrictions != null && !supportsAdditionalPropertyInference( model ) ) { |
| 91 | + // if there are some missing direct children, revisit the hierarchy |
| 92 | + Set<OntClass> childrenToRevisit = new HashSet<>( children ); |
| 93 | + while ( !childrenToRevisit.isEmpty() ) { |
| 94 | + log.debug( "Revisiting the direct parents of {} terms...", childrenToRevisit.size() ); |
| 95 | + childrenToRevisit = new HashSet<>( JenaUtils.getChildrenInternal( model, childrenToRevisit, true, additionalRestrictions ) ); |
| 96 | + childrenToRevisit.removeAll( children ); |
| 97 | + log.debug( "Found {} missed children.", childrenToRevisit.size() ); |
| 98 | + children.addAll( childrenToRevisit ); |
| 99 | + } |
| 100 | + } |
| 101 | + return children; |
| 102 | + } |
| 103 | + |
| 104 | + public static Collection<OntClass> getChildrenInternal( OntModel model, Collection<OntClass> terms, boolean direct, @Nullable Set<Restriction> additionalRestrictions ) { |
68 | 105 | terms = terms.stream() |
69 | 106 | .map( t -> t.inModel( model ) ) |
70 | 107 | .filter( t -> t.canAs( OntClass.class ) ) |
@@ -103,6 +140,11 @@ public static Collection<OntClass> getChildren( OntModel model, Collection<OntCl |
103 | 140 | return result; |
104 | 141 | } |
105 | 142 |
|
| 143 | + private static boolean supportsAdditionalPropertyInference( OntModel model ) { |
| 144 | + return model.getReasoner().supportsProperty( model.getProfile().SOME_VALUES_FROM() ) |
| 145 | + && model.getReasoner().supportsProperty( model.getProfile().ALL_VALUES_FROM() ); |
| 146 | + } |
| 147 | + |
106 | 148 | public static Resource getRestrictionValue( Restriction r ) { |
107 | 149 | if ( r.isSomeValuesFromRestriction() ) { |
108 | 150 | return r.asSomeValuesFromRestriction().getSomeValuesFrom(); |
|
0 commit comments