Skip to content

Commit 37bd9da

Browse files
committed
Improve OntologyService API
Add a public initialize() method to initialize an ontology service and use that in the initialization thread. Use a read-write lock for initializing and refreshing the ontology, allowing more than one reader at a time. Expose the initialization thread's properties via public method in AbstractOntologyService. Catch all possible exceptions in ontology initialization thread
1 parent 608538d commit 37bd9da

4 files changed

Lines changed: 392 additions & 440 deletions

File tree

src/ubic/basecode/ontology/providers/AbstractOntologyMemoryBackedService.java

Lines changed: 6 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,30 @@
11
/*
22
* The baseCode project
3-
*
3+
*
44
* Copyright (c) 2013 University of British Columbia
5-
*
5+
*
66
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
77
* the License. You may obtain a copy of the License at
8-
*
8+
*
99
* http://www.apache.org/licenses/LICENSE-2.0
10-
*
10+
*
1111
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
1212
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
1313
* specific language governing permissions and limitations under the License.
1414
*/
1515
package ubic.basecode.ontology.providers;
1616

17-
import java.io.IOException;
18-
import java.io.InputStream;
19-
20-
import ubic.basecode.ontology.OntologyLoader;
21-
import ubic.basecode.ontology.search.OntologyIndexer;
22-
2317
import com.hp.hpl.jena.ontology.OntModel;
24-
import com.hp.hpl.jena.ontology.OntModelSpec;
18+
import ubic.basecode.ontology.OntologyLoader;
2519

2620
/**
2721
* This class has some stuff that's specific to in-memory ontologies. Unlike database backed ontologies we don't use a
2822
* pool keeping only one instance of model in memory.
29-
*
23+
*
3024
* @author paul
3125
*/
3226
public abstract class AbstractOntologyMemoryBackedService extends AbstractOntologyService {
3327

34-
/**
35-
* For testing! Overrides normal way of loading the ontology. This does not index the ontology unless 'force' is
36-
* true (if there is an existing index, it will be used)
37-
*
38-
* @param is
39-
* @param forceIndex initialize the index. Otherwise it will only be initialized if it doesn't exist.
40-
* @throws IOException
41-
*/
42-
public synchronized void loadTermsInNameSpace( InputStream is, boolean forceIndex ) {
43-
synchronized ( isInitialized ) {
44-
this.indexReady.set( false );
45-
this.modelReady.set( false );
46-
this.isInitialized.set( false );
47-
this.cacheReady.set( false );
48-
}
49-
50-
if ( initializationThread.isAlive() ) {
51-
log.warn( this.getOntologyName() + " initialization is already running, trying to cancel ..." );
52-
initializationThread.cancel();
53-
54-
// wait for the thread to die.
55-
int maxWait = 10;
56-
int wait = 0;
57-
while ( initializationThread.isAlive() ) {
58-
try {
59-
Thread.sleep( 5000 );
60-
log.warn( "Waiting for auto-initialization to stop so manual initialization can begin ..." );
61-
} catch ( InterruptedException e ) {
62-
// no-op.
63-
}
64-
if ( ++wait >= maxWait ) {
65-
log.error( "Got tired of waiting" );
66-
break;
67-
}
68-
if ( initializationThread.isInterrupted() ) {
69-
log.warn( "Got interrupt." );
70-
break;
71-
}
72-
}
73-
}
74-
75-
if ( this.terms != null ) this.terms.clear();
76-
if ( this.individuals != null ) this.individuals.clear();
77-
78-
this.model = OntologyLoader.loadMemoryModel( is, this.getOntologyUrl(), OntModelSpec.OWL_MEM );
79-
this.index = OntologyIndexer.getSubjectIndex( getOntologyName() );
80-
if ( index == null || forceIndex ) {
81-
this.index = OntologyIndexer.indexOntology( getOntologyName(), model, true /* force */ );
82-
}
83-
84-
addTerms( OntologyLoader.initialize( this.getOntologyUrl(), model ) );
85-
86-
synchronized ( isInitialized ) {
87-
indexReady.set( this.index != null );
88-
cacheReady.set( true );
89-
modelReady.set( true );
90-
isInitialized.set( true );
91-
}
92-
log.info( this.getClass().getSimpleName() + " ready" );
93-
}
94-
9528
@Override
9629
protected synchronized OntModel loadModel() {
9730
return OntologyLoader.loadMemoryModel( this.getOntologyUrl(), this.getOntologyName() );

0 commit comments

Comments
 (0)