4545
4646import java .io .InputStream ;
4747import java .util .*;
48- import java .util .concurrent .ConcurrentHashMap ;
4948import java .util .concurrent .locks .Lock ;
5049import java .util .concurrent .locks .ReadWriteLock ;
5150import java .util .concurrent .locks .ReentrantReadWriteLock ;
@@ -82,13 +81,14 @@ public abstract class AbstractOntologyService implements OntologyService {
8281 /* internal state protected by rwLock */
8382 private OntModel model ;
8483 private Map <String , String > alternativeIDs ;
85- private final Map < String , OntologyTerm > termCache = new ConcurrentHashMap <>( 1024 );
84+
8685 private SearchIndex index ;
8786
8887 private Set <Restriction > additionalRestrictions ;
8988
9089 private boolean isInitialized = false ;
9190
91+
9292 @ Override
9393 public void initialize ( boolean forceLoad , boolean forceIndexing ) {
9494 if ( !forceLoad && isInitialized ) {
@@ -159,7 +159,6 @@ public void initialize( boolean forceLoad, boolean forceIndexing ) {
159159 lock .lock ();
160160 this .model = model ;
161161 this .additionalRestrictions = additionalRestrictions ;
162- this .termCache .clear ();
163162 this .index = index ;
164163 this .isInitialized = true ;
165164 } finally {
@@ -312,7 +311,7 @@ public OntologyResource getResource( String uri ) {
312311 }
313312 if ( resource instanceof OntClass ) {
314313 // use the cached term
315- res = getTermInternal ( uri );
314+ res = new OntologyTermImpl ( ( OntClass ) resource , additionalRestrictions );
316315 } else if ( resource instanceof Individual ) {
317316 res = new OntologyIndividualImpl ( ( Individual ) resource , additionalRestrictions );
318317 } else if ( resource instanceof OntProperty ) {
@@ -333,7 +332,12 @@ public OntologyTerm getTerm( String uri ) {
333332 try {
334333 lock .lock ();
335334 if ( !isInitialized ) return null ;
336- return getTermInternal ( uri );
335+ OntClass ontCls = model .getOntClass ( uri );
336+ // null or bnode
337+ if ( ontCls == null || ontCls .getURI () == null ) {
338+ return null ;
339+ }
340+ return new OntologyTermImpl ( ontCls , additionalRestrictions );
337341 } finally {
338342 lock .unlock ();
339343 }
@@ -347,7 +351,7 @@ public Collection<OntologyIndividual> getTermIndividuals( String uri ) {
347351 if ( !isInitialized ) {
348352 return Collections .emptySet ();
349353 }
350- OntologyTerm term = getTermInternal ( uri );
354+ OntologyTerm term = getTerm ( uri );
351355 if ( term == null ) {
352356 /*
353357 * Either the ontology hasn't been loaded, or the id was not valid.
@@ -553,17 +557,6 @@ private void initSearchByAlternativeId() {
553557 }
554558 }
555559
556- private OntologyTerm getTermInternal ( String uri ) {
557- return termCache .computeIfAbsent ( uri , u -> {
558- OntClass ontCls = model .getOntClass ( u );
559- // null or bnode
560- if ( ontCls == null || ontCls .getURI () == null ) {
561- return null ;
562- }
563- return new OntologyTermImpl ( ontCls , additionalRestrictions );
564- } );
565- }
566-
567560 @ Override
568561 public void loadTermsInNameSpace ( InputStream is , boolean forceIndex ) {
569562 Lock lock = rwLock .writeLock ();
@@ -597,7 +590,6 @@ public void loadTermsInNameSpace( InputStream is, boolean forceIndex ) {
597590 this .additionalRestrictions = model .listRestrictions ()
598591 .filterKeep ( new RestrictionWithOnPropertyFilter ( additionalProperties ) )
599592 .toSet ();
600- this .termCache .clear ();
601593 this .index = OntologyIndexer .getSubjectIndex ( getCacheName () );
602594 if ( index == null || forceIndex ) {
603595 this .index = OntologyIndexer .indexOntology ( getCacheName (), model , true /* force */ );
0 commit comments