Skip to content

Commit 8211eac

Browse files
committed
Support setting inference imports and search in OntologyService interface
Two inference mode are supported: NONE and TRANSITIVE. Take advantage of these settings to accelerate certain tests that do not use the aforementioned features.
1 parent 18aa4ad commit 8211eac

13 files changed

Lines changed: 170 additions & 48 deletions

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
package ubic.basecode.ontology.jena;
1616

1717
import com.hp.hpl.jena.ontology.OntModel;
18+
import com.hp.hpl.jena.ontology.OntModelSpec;
19+
import ubic.basecode.util.Configuration;
1820

1921
import java.io.IOException;
2022
import java.io.InputStream;
@@ -27,17 +29,29 @@
2729
*/
2830
public abstract class AbstractOntologyMemoryBackedService extends AbstractOntologyService {
2931

30-
protected boolean getProcessImport() {
31-
return true;
32+
@Override
33+
protected String getOntologyUrl() {
34+
return Configuration.getString( "url." + getOntologyName() );
3235
}
3336

3437
@Override
35-
protected OntModel loadModel() throws IOException {
36-
return OntologyLoader.loadMemoryModel( this.getOntologyUrl(), this.getCacheName(), this.getProcessImport() );
38+
protected OntModel loadModel( boolean processImports, InferenceMode inferenceMode ) throws IOException {
39+
return OntologyLoader.loadMemoryModel( this.getOntologyUrl(), this.getCacheName(), processImports, this.getSpec( inferenceMode ) );
3740
}
3841

3942
@Override
40-
protected OntModel loadModelFromStream( InputStream is ) {
41-
return OntologyLoader.loadMemoryModel( is, this.getOntologyUrl(), this.getProcessImport() );
43+
protected OntModel loadModelFromStream( InputStream is, boolean processImports, InferenceMode inferenceMode ) {
44+
return OntologyLoader.loadMemoryModel( is, this.getOntologyUrl(), processImports, this.getSpec( inferenceMode ) );
45+
}
46+
47+
private OntModelSpec getSpec( InferenceMode inferenceMode ) {
48+
switch ( inferenceMode ) {
49+
case TRANSITIVE:
50+
return OntModelSpec.OWL_MEM_TRANS_INF;
51+
case NONE:
52+
return OntModelSpec.OWL_MEM;
53+
default:
54+
throw new UnsupportedOperationException( String.format( "Unsupported inference level %s.", inferenceMode ) );
55+
}
4256
}
4357
}

src/ubic/basecode/ontology/jena/AbstractOntologyService.java

Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ public abstract class AbstractOntologyService implements OntologyService {
7878
additionalProperties.add( RO.properPartOf );
7979
}
8080

81+
/* settings (applicable for next initialization) */
82+
private InferenceMode nextInferenceMode = InferenceMode.TRANSITIVE;
83+
private boolean nextProcessImports = true;
84+
private boolean nextSearchEnabled = true;
85+
8186
/**
8287
* Lock used to prevent reads while the ontology is being initialized.
8388
*/
@@ -86,13 +91,64 @@ public abstract class AbstractOntologyService implements OntologyService {
8691
/* internal state protected by rwLock */
8792
private OntModel model;
8893
private Map<String, String> alternativeIDs;
89-
9094
@Nullable
9195
private SearchIndex index;
92-
9396
private Set<Restriction> additionalRestrictions;
94-
9597
private boolean isInitialized = false;
98+
@Nullable
99+
private InferenceMode inferenceMode = null;
100+
@Nullable
101+
private Boolean processImports = null;
102+
@Nullable
103+
private Boolean searchEnabled = null;
104+
105+
@Override
106+
public InferenceMode getInferenceMode() {
107+
Lock lock = rwLock.readLock();
108+
try {
109+
lock.lock();
110+
return this.inferenceMode != null ? this.inferenceMode : nextInferenceMode;
111+
} finally {
112+
lock.unlock();
113+
}
114+
}
115+
116+
@Override
117+
public void setInferenceMode( InferenceMode inferenceMode ) {
118+
this.nextInferenceMode = inferenceMode;
119+
}
120+
121+
@Override
122+
public boolean getProcessImports() {
123+
Lock lock = rwLock.readLock();
124+
try {
125+
lock.lock();
126+
return processImports != null ? processImports : nextProcessImports;
127+
} finally {
128+
lock.unlock();
129+
}
130+
}
131+
132+
@Override
133+
public void setProcessImports( boolean processImports ) {
134+
this.nextProcessImports = processImports;
135+
}
136+
137+
@Override
138+
public boolean isSearchEnabled() {
139+
Lock lock = rwLock.readLock();
140+
try {
141+
lock.lock();
142+
return searchEnabled != null ? searchEnabled : nextSearchEnabled;
143+
} finally {
144+
lock.unlock();
145+
}
146+
}
147+
148+
@Override
149+
public void setSearchEnabled( boolean searchEnabled ) {
150+
this.nextSearchEnabled = searchEnabled;
151+
}
96152

97153
public void initialize( boolean forceLoad, boolean forceIndexing ) {
98154
initialize( null, forceLoad, forceIndexing );
@@ -111,6 +167,9 @@ private void initialize( @Nullable InputStream stream, boolean forceLoad, boolea
111167
String ontologyUrl = getOntologyUrl();
112168
String ontologyName = getOntologyName();
113169
String cacheName = getCacheName();
170+
InferenceMode inferenceMode = nextInferenceMode;
171+
boolean processImports = nextProcessImports;
172+
boolean searchEnabled = nextSearchEnabled;
114173

115174
// Detect configuration problems.
116175
if ( StringUtils.isBlank( ontologyUrl ) ) {
@@ -142,7 +201,7 @@ private void initialize( @Nullable InputStream stream, boolean forceLoad, boolea
142201
return;
143202

144203
try {
145-
model = stream != null ? loadModelFromStream( stream ) : loadModel(); // can take a while.
204+
model = stream != null ? loadModelFromStream( stream, processImports, inferenceMode ) : loadModel( processImports, inferenceMode ); // can take a while.
146205
} catch ( Exception e ) {
147206
if ( isCausedByInterrupt( e ) ) {
148207
return;
@@ -164,7 +223,7 @@ private void initialize( @Nullable InputStream stream, boolean forceLoad, boolea
164223
if ( checkIfInterrupted() )
165224
return;
166225

167-
if ( cacheName != null ) {
226+
if ( searchEnabled && cacheName != null ) {
168227
//Checks if the current ontology has changed since it was last loaded.
169228
boolean changed = OntologyLoader.hasChanged( cacheName );
170229
boolean indexExists = OntologyIndexer.getSubjectIndex( cacheName ) != null;
@@ -195,6 +254,9 @@ private void initialize( @Nullable InputStream stream, boolean forceLoad, boolea
195254
this.additionalRestrictions = additionalRestrictions;
196255
this.index = index;
197256
this.isInitialized = true;
257+
this.inferenceMode = inferenceMode;
258+
this.processImports = processImports;
259+
this.searchEnabled = searchEnabled;
198260
if ( cacheName != null ) {
199261
// now that the terms have been replaced, we can clear old caches
200262
try {
@@ -553,13 +615,13 @@ public void waitForInitializationThread() throws InterruptedException {
553615
* Delegates the call as to load the model into memory or leave it on disk. Simply delegates to either
554616
* OntologyLoader.loadMemoryModel( url ); OR OntologyLoader.loadPersistentModel( url, spec );
555617
*/
556-
protected abstract OntModel loadModel() throws JenaException, IOException;
618+
protected abstract OntModel loadModel( boolean processImports, InferenceMode inferenceMode ) throws JenaException, IOException;
557619

558620

559621
/**
560622
* Load a model from a given input stream.
561623
*/
562-
protected abstract OntModel loadModelFromStream( InputStream stream ) throws JenaException, IOException;
624+
protected abstract OntModel loadModelFromStream( InputStream stream, boolean processImports, InferenceMode inferenceMode ) throws JenaException, IOException;
563625

564626
/**
565627
* A name for caching this ontology, or null to disable caching.
@@ -578,6 +640,10 @@ public void index( boolean force ) {
578640
log.warn( "This ontology does not support indexing; assign a cache name to be used." );
579641
return;
580642
}
643+
if ( !nextSearchEnabled ) {
644+
log.warn( "Search is not enabled for this ontology." );
645+
return;
646+
}
581647
SearchIndex index;
582648
Lock lock = rwLock.readLock();
583649
try {
@@ -598,6 +664,7 @@ public void index( boolean force ) {
598664
try {
599665
lock.lock();
600666
this.index = index;
667+
this.searchEnabled = true;
601668
} finally {
602669
lock.unlock();
603670
}

src/ubic/basecode/ontology/jena/OntologyLoader.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ public static OntModel loadMemoryModel( InputStream is, String url ) throws Jena
6060
* Load an ontology into memory. Use this type of model when fast access is critical and memory is available.
6161
*/
6262
public static OntModel loadMemoryModel( InputStream is, String url, boolean processImports ) throws JenaException {
63-
OntModel model = getMemoryModel( url, processImports );
63+
return loadMemoryModel( is, url, processImports, OntModelSpec.OWL_MEM_TRANS_INF );
64+
}
65+
66+
public static OntModel loadMemoryModel( InputStream is, String url, boolean processImports, OntModelSpec spec ) throws JenaException {
67+
OntModel model = getMemoryModel( url, processImports, spec );
6468
model.read( is, null );
6569
return model;
6670
}
@@ -78,19 +82,24 @@ public static OntModel loadMemoryModel( String url, @Nullable String cacheName )
7882
return loadMemoryModel( url, cacheName, true );
7983
}
8084

85+
public static OntModel loadMemoryModel( String url, @Nullable String cacheName, boolean processImports ) throws JenaException, IOException {
86+
return loadMemoryModel( url, cacheName, processImports, OntModelSpec.OWL_MEM_TRANS_INF );
87+
}
88+
8189
/**
8290
* Load an ontology into memory. Use this type of model when fast access is critical and memory is available.
8391
* If load from URL fails, attempt to load from disk cache under @cacheName.
8492
* <p>
8593
* Uses {@link OntModelSpec#OWL_MEM_TRANS_INF}.
8694
*
87-
* @param url a URL where the OWL file is stored
88-
* @param cacheName unique name of this ontology, will be used to load from disk in case of failed url connection
95+
* @param url a URL where the OWL file is stored
96+
* @param cacheName unique name of this ontology, will be used to load from disk in case of failed url connection
97+
* @param processImports process imports
98+
* @param spec spec to use as a basis
8999
*/
90-
public static OntModel loadMemoryModel( String url, @Nullable String cacheName, boolean processImports ) throws JenaException, IOException {
91-
StopWatch timer = new StopWatch();
92-
timer.start();
93-
OntModel model = getMemoryModel( url, processImports );
100+
public static OntModel loadMemoryModel( String url, @Nullable String cacheName, boolean processImports, OntModelSpec spec ) throws JenaException, IOException {
101+
StopWatch timer = StopWatch.createStarted();
102+
OntModel model = getMemoryModel( url, processImports, spec );
94103

95104
boolean attemptToLoadFromDisk = false;
96105
URLConnection urlc = null;
@@ -111,7 +120,6 @@ public static OntModel loadMemoryModel( String url, @Nullable String cacheName,
111120
try ( BufferedReader buf = new BufferedReader( reader ) ) {
112121
model.read( buf, url );
113122
}
114-
log.info( "Loading ontology model for " + url + " took " + timer.getTime() + "ms" );
115123
}
116124
} catch ( ClosedByInterruptException e ) {
117125
throw e;
@@ -163,6 +171,8 @@ public static OntModel loadMemoryModel( String url, @Nullable String cacheName,
163171
}
164172
}
165173

174+
log.info( "Loading ontology model for " + url + " took " + timer.getTime() + "ms" );
175+
166176
return model;
167177
}
168178

@@ -195,8 +205,8 @@ public static void deleteOldCache( String cacheName ) throws IOException {
195205
/**
196206
* Get model that is entirely in memory.
197207
*/
198-
private static OntModel getMemoryModel( String url, boolean processImports ) {
199-
OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_MEM_TRANS_INF );
208+
private static OntModel getMemoryModel( String url, boolean processImports, OntModelSpec spec ) {
209+
spec = new OntModelSpec( spec );
200210
ModelMaker maker = ModelFactory.createMemModelMaker();
201211
Model base = maker.createModel( url, false );
202212
spec.setImportModelMaker( maker );

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

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,7 @@
1414
*/
1515
package ubic.basecode.ontology.providers;
1616

17-
import com.hp.hpl.jena.ontology.OntModel;
1817
import ubic.basecode.ontology.jena.AbstractOntologyMemoryBackedService;
19-
import ubic.basecode.ontology.jena.OntologyLoader;
20-
21-
import ubic.basecode.ontology.jena.AbstractOntologyService;
22-
23-
import java.io.InputStream;
2418

2519
/**
2620
* A way to create ad hoc ontology services (in memory) for testing
@@ -32,7 +26,6 @@ public class GenericOntologyService extends AbstractOntologyMemoryBackedService
3226
private final String url;
3327
private final String name;
3428
private final boolean cache;
35-
private final boolean processImports;
3629

3730
public GenericOntologyService( String name, String url ) {
3831
this( name, url, false );
@@ -46,7 +39,7 @@ public GenericOntologyService( String name, String url, boolean cache, boolean p
4639
this.name = name;
4740
this.url = url;
4841
this.cache = cache;
49-
this.processImports = processImports;
42+
setProcessImports( processImports );
5043
}
5144

5245
@Override
@@ -63,9 +56,4 @@ protected String getOntologyUrl() {
6356
protected String getCacheName() {
6457
return this.cache ? this.name : null;
6558
}
66-
67-
@Override
68-
protected boolean getProcessImport() {
69-
return processImports;
70-
}
7159
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,19 @@ protected String getOntologyUrl() {
5353
}
5454

5555
@Override
56-
protected OntModel loadModel() {
56+
protected OntModel loadModel( boolean processImports, InferenceMode inferenceMode ) {
5757
try ( InputStream is = this.getClass().getResourceAsStream( MEDIC_ONTOLOGY_FILE ) ) {
5858
if ( is == null ) {
5959
throw new RuntimeException( String.format( "The MEDIC ontology was not found in classpath at %s.", MEDIC_ONTOLOGY_FILE ) );
6060
}
61-
return loadModelFromStream( new GZIPInputStream( is ) );
61+
return loadModelFromStream( new GZIPInputStream( is ), processImports, inferenceMode );
6262
} catch ( IOException e ) {
6363
throw new RuntimeException( e );
6464
}
6565
}
6666

6767
@Override
68-
protected OntModel loadModelFromStream( InputStream stream ) throws IOException {
68+
protected OntModel loadModelFromStream( InputStream stream, boolean processImports, InferenceMode inferenceMode ) throws IOException {
6969
return OntologyLoader.loadMemoryModel( stream, "classpath:" + MEDIC_ONTOLOGY_FILE );
7070
}
7171
}

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ public class NIFSTDOntologyService extends AbstractOntologyMemoryBackedService {
2828

2929
private static final String NIFSTD_ONTOLOGY_FILE = "/data/loader/ontology/nif-gemma.owl.gz";
3030

31+
public NIFSTDOntologyService() {
32+
setProcessImports( false );
33+
}
34+
3135
@Override
3236
protected String getOntologyName() {
3337
return "nifstdOntology";
@@ -39,17 +43,12 @@ protected String getOntologyUrl() {
3943
}
4044

4145
@Override
42-
protected boolean getProcessImport() {
43-
return false;
44-
}
45-
46-
@Override
47-
protected OntModel loadModel() {
46+
protected OntModel loadModel( boolean processImports, InferenceMode inferenceMode ) {
4847
try ( InputStream stream = getClass().getResourceAsStream( NIFSTD_ONTOLOGY_FILE ) ) {
4948
if ( stream == null ) {
5049
throw new RuntimeException( String.format( "The NIF ontology was not found in classpath at %s.", NIFSTD_ONTOLOGY_FILE ) );
5150
}
52-
return loadModelFromStream( new GZIPInputStream( stream ) );
51+
return loadModelFromStream( new GZIPInputStream( stream ), processImports, inferenceMode );
5352
} catch ( IOException e ) {
5453
throw new RuntimeException( e );
5554
}

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,31 @@
1212

1313
public interface OntologyService {
1414

15+
boolean getProcessImports();
16+
17+
void setProcessImports( boolean processImports );
18+
19+
enum InferenceMode {
20+
NONE,
21+
TRANSITIVE
22+
}
23+
24+
/**
25+
* Obtain the inference mode used for this ontology.
26+
*/
27+
InferenceMode getInferenceMode();
28+
29+
/**
30+
* Set the inference mode used for this ontology.
31+
* <p>
32+
* Changes are applicable only if the service is re-initialized.
33+
*/
34+
void setInferenceMode( InferenceMode inferenceMode );
35+
36+
boolean isSearchEnabled();
37+
38+
void setSearchEnabled( boolean searchEnabled );
39+
1540
/**
1641
* Initialize this ontology service.
1742
*

0 commit comments

Comments
 (0)