4343import ubic .basecode .ontology .search .OntologySearchException ;
4444import ubic .basecode .util .Configuration ;
4545
46+ import javax .annotation .Nullable ;
4647import java .io .InputStream ;
4748import java .util .*;
4849import java .util .concurrent .locks .Lock ;
@@ -82,6 +83,7 @@ public abstract class AbstractOntologyService implements OntologyService {
8283 private OntModel model ;
8384 private Map <String , String > alternativeIDs ;
8485
86+ @ Nullable
8587 private SearchIndex index ;
8688
8789 private Set <Restriction > additionalRestrictions ;
@@ -96,7 +98,7 @@ public void initialize( InputStream stream, boolean forceIndexing ) {
9698 initialize ( stream , true , forceIndexing );
9799 }
98100
99- private void initialize ( InputStream stream , boolean forceLoad , boolean forceIndexing ) {
101+ private void initialize ( @ Nullable InputStream stream , boolean forceLoad , boolean forceIndexing ) {
100102 if ( !forceLoad && isInitialized ) {
101103 log .warn ( "{} is already loaded, and force=false, not restarting" , this );
102104 return ;
@@ -149,8 +151,8 @@ private void initialize( InputStream stream, boolean forceLoad, boolean forceInd
149151 .toSet ();
150152
151153 //Checks if the current ontology has changed since it was last loaded.
152- boolean changed = OntologyLoader .hasChanged ( cacheName );
153- boolean indexExists = OntologyIndexer .getSubjectIndex ( cacheName ) != null ;
154+ boolean changed = cacheName == null || OntologyLoader .hasChanged ( cacheName );
155+ boolean indexExists = cacheName != null && OntologyIndexer .getSubjectIndex ( cacheName ) != null ;
154156 boolean forceReindexing = forceLoad && forceIndexing ;
155157
156158 /*
@@ -162,7 +164,11 @@ private void initialize( InputStream stream, boolean forceLoad, boolean forceInd
162164 if ( checkIfInterrupted () )
163165 return ;
164166
165- index = OntologyIndexer .indexOntology ( cacheName , model , force );
167+ if ( cacheName != null ) {
168+ index = OntologyIndexer .indexOntology ( cacheName , model , force );
169+ } else {
170+ index = null ;
171+ }
166172
167173 // if interrupted, we don't need to replace the model and clear the *old* cache
168174 if ( checkIfInterrupted () )
@@ -175,8 +181,10 @@ private void initialize( InputStream stream, boolean forceLoad, boolean forceInd
175181 this .additionalRestrictions = additionalRestrictions ;
176182 this .index = index ;
177183 this .isInitialized = true ;
178- // now that the terms have been replaced, we can clear old caches
179- OntologyLoader .deleteOldCache ( cacheName );
184+ if ( cacheName != null ) {
185+ // now that the terms have been replaced, we can clear old caches
186+ OntologyLoader .deleteOldCache ( cacheName );
187+ }
180188 } finally {
181189 lock .unlock ();
182190 }
@@ -318,7 +326,6 @@ public Set<String> getAllURIs() {
318326
319327 @ Override
320328 public OntologyResource getResource ( String uri ) {
321- if ( uri == null ) return null ;
322329 Lock lock = rwLock .readLock ();
323330 try {
324331 lock .lock ();
@@ -348,7 +355,6 @@ public OntologyResource getResource( String uri ) {
348355
349356 @ Override
350357 public OntologyTerm getTerm ( String uri ) {
351- if ( uri == null ) throw new IllegalArgumentException ( "URI cannot be null" );
352358 Lock lock = rwLock .readLock ();
353359 try {
354360 lock .lock ();
@@ -521,16 +527,25 @@ public void waitForInitializationThread() throws InterruptedException {
521527 /**
522528 * Load a model from a given input stream.
523529 */
524- protected OntModel loadModelFromStream ( InputStream is ) {
525- return OntologyLoader .loadMemoryModel ( is , this .getOntologyUrl () );
526- }
530+ protected abstract OntModel loadModelFromStream ( InputStream stream );
527531
532+ /**
533+ * A name for caching this ontology, or null to disable caching.
534+ * <p>
535+ * Note that if null is returned, the ontology will not have full-text search capabilities.
536+ */
537+ @ Nullable
528538 protected String getCacheName () {
529539 return getOntologyName ();
530540 }
531541
532542 @ Override
533543 public void index ( boolean force ) {
544+ String cacheName = getCacheName ();
545+ if ( cacheName == null ) {
546+ log .warn ( "This ontology does not support indexing; assign a cache name to be used." );
547+ return ;
548+ }
534549 SearchIndex index ;
535550 Lock lock = rwLock .readLock ();
536551 try {
0 commit comments