@@ -88,9 +88,15 @@ public abstract class AbstractOntologyService implements OntologyService {
8888
8989 private boolean isInitialized = false ;
9090
91-
92- @ Override
9391 public void initialize ( boolean forceLoad , boolean forceIndexing ) {
92+ initialize ( null , forceLoad , forceIndexing );
93+ }
94+
95+ public void initialize ( InputStream stream , boolean forceIndexing ) {
96+ initialize ( stream , true , forceIndexing );
97+ }
98+
99+ private void initialize ( InputStream stream , boolean forceLoad , boolean forceIndexing ) {
94100 if ( !forceLoad && isInitialized ) {
95101 log .warn ( "{} is already loaded, and force=false, not restarting" , this );
96102 return ;
@@ -118,20 +124,21 @@ public void initialize( boolean forceLoad, boolean forceIndexing ) {
118124 log .info ( "Loading ontology: {}..." , this );
119125 StopWatch loadTime = StopWatch .createStarted ();
120126
121- // use temporary variables so we can minimize the critical region for replacing the service's state
122- Map <String , OntologyTerm > terms = new HashMap <>();
123- Map <String , OntologyIndividual > individuals = new HashMap <>();
127+ // use temporary variables, so that we can minimize the critical region for replacing the service's state
124128 OntModel model ;
125129 SearchIndex index ;
126130
127- if ( Thread . currentThread (). isInterrupted () ) {
128- log . warn ( "The current thread is interrupted, initialization of {} will be stop." , this );
131+ // loading the model from disk or URL is lengthy
132+ if ( checkIfInterrupted () )
129133 return ;
130- }
131134
132- model = loadModel (); // can take a while.
135+ model = stream != null ? loadModelFromStream ( stream ) : loadModel (); // can take a while.
133136 assert model != null ;
134137
138+ // retrieving restrictions is lengthy
139+ if ( checkIfInterrupted () )
140+ return ;
141+
135142 // compute additional restrictions
136143 Set <Restriction > additionalRestrictions = model .listRestrictions ()
137144 .filterKeep ( new RestrictionWithOnPropertyFilter ( additionalProperties ) )
@@ -147,12 +154,15 @@ public void initialize( boolean forceLoad, boolean forceIndexing ) {
147154 */
148155 boolean force = forceReindexing || changed || !indexExists ;
149156
157+ // indexing is lengthy, don't bother if we're interrupted
158+ if ( checkIfInterrupted () )
159+ return ;
160+
150161 index = OntologyIndexer .indexOntology ( getCacheName (), model , force );
151162
152- if ( Thread . currentThread (). isInterrupted () ) {
153- log . warn ( "The current thread is interrupted, initialization of {} will be stop." , this );
163+ // if interrupted, we don't need to replace the model and clear the *old* cache
164+ if ( checkIfInterrupted () )
154165 return ;
155- }
156166
157167 Lock lock = rwLock .writeLock ();
158168 try {
@@ -161,18 +171,25 @@ public void initialize( boolean forceLoad, boolean forceIndexing ) {
161171 this .additionalRestrictions = additionalRestrictions ;
162172 this .index = index ;
163173 this .isInitialized = true ;
174+ // now that the terms have been replaced, we can clear old caches
175+ OntologyLoader .deleteOldCache ( getCacheName () );
164176 } finally {
165177 lock .unlock ();
166178 }
167179
168- // now that the terms have been replaced, we can clear old caches
169- OntologyLoader .deleteOldCache ( getCacheName () );
170-
171180 loadTime .stop ();
172181
173182 log .info ( "Finished loading {} in {}s" , this , String .format ( "%.2f" , loadTime .getTime () / 1000.0 ) );
174183 }
175184
185+ private boolean checkIfInterrupted () {
186+ if ( Thread .interrupted () ) {
187+ log .warn ( "The current thread is interrupted, initialization of {} will be stop." , this );
188+ return true ;
189+ }
190+ return false ;
191+ }
192+
176193 /**
177194 * Do not do this except before re-indexing.
178195 */
@@ -496,6 +513,14 @@ public void waitForInitializationThread() throws InterruptedException {
496513 */
497514 protected abstract OntModel loadModel ();
498515
516+
517+ /**
518+ * Load a model from a given input stream.
519+ */
520+ protected OntModel loadModelFromStream ( InputStream is ) {
521+ return OntologyLoader .loadMemoryModel ( is , this .getOntologyUrl () );
522+ }
523+
499524 protected String getCacheName () {
500525 return getOntologyName ();
501526 }
@@ -559,48 +584,29 @@ private void initSearchByAlternativeId() {
559584
560585 @ Override
561586 public void loadTermsInNameSpace ( InputStream is , boolean forceIndex ) {
562- Lock lock = rwLock .writeLock ();
563- try {
564- lock .lock ();
565- this .isInitialized = false ;
566-
567- if ( initializationThread != null && initializationThread .isAlive () ) {
568- log .warn ( "{} initialization is already running, trying to cancel ..." , this );
569- initializationThread .interrupt ();
570- // wait for the thread to die.
571- int maxWait = 10 ;
572- int wait = 0 ;
573- while ( initializationThread .isAlive () ) {
574- try {
575- initializationThread .join ( 5000 );
576- log .warn ( "Waiting for auto-initialization to stop so manual initialization can begin ..." );
577- } catch ( InterruptedException e ) {
578- Thread .currentThread ().interrupt ();
579- log .warn ( "Got interrupted while waiting for the initialization thread of {} to finish." , this );
580- return ;
581- }
582- ++wait ;
583- if ( wait >= maxWait && !initializationThread .isAlive () ) {
584- throw new RuntimeException ( String .format ( "Got tired of waiting for %s's initialization thread." , this ) );
585- }
587+ // wait for the initialization thread to finish
588+ if ( initializationThread != null && initializationThread .isAlive () ) {
589+ log .warn ( "{} initialization is already running, trying to cancel ..." , this );
590+ initializationThread .interrupt ();
591+ // wait for the thread to die.
592+ int maxWait = 10 ;
593+ int wait = 0 ;
594+ while ( initializationThread .isAlive () ) {
595+ try {
596+ initializationThread .join ( 5000 );
597+ log .warn ( "Waiting for auto-initialization to stop so manual initialization can begin ..." );
598+ } catch ( InterruptedException e ) {
599+ Thread .currentThread ().interrupt ();
600+ log .warn ( "Got interrupted while waiting for the initialization thread of {} to finish." , this );
601+ return ;
602+ }
603+ ++wait ;
604+ if ( wait >= maxWait && !initializationThread .isAlive () ) {
605+ throw new RuntimeException ( String .format ( "Got tired of waiting for %s's initialization thread." , this ) );
586606 }
587607 }
588-
589- this .model = OntologyLoader .loadMemoryModel ( is , this .getOntologyUrl () );
590- this .additionalRestrictions = model .listRestrictions ()
591- .filterKeep ( new RestrictionWithOnPropertyFilter ( additionalProperties ) )
592- .toSet ();
593- this .index = OntologyIndexer .getSubjectIndex ( getCacheName () );
594- if ( index == null || forceIndex ) {
595- this .index = OntologyIndexer .indexOntology ( getCacheName (), model , true /* force */ );
596- }
597-
598- isInitialized = true ;
599- } finally {
600- lock .unlock ();
601608 }
602-
603- log .info ( "Ontology {} is ready!" , this );
609+ initialize ( is , forceIndex );
604610 }
605611
606612 @ Override
0 commit comments