Skip to content

Commit 8205afa

Browse files
committed
Add an option to enable imports per-ontology
1 parent e6d8f78 commit 8205afa

5 files changed

Lines changed: 40 additions & 16 deletions

File tree

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,17 @@
2727
*/
2828
public abstract class AbstractOntologyMemoryBackedService extends AbstractOntologyService {
2929

30+
protected boolean getProcessImport() {
31+
return true;
32+
}
33+
3034
@Override
3135
protected OntModel loadModel() {
32-
return OntologyLoader.loadMemoryModel( this.getOntologyUrl(), this.getOntologyName() );
36+
return OntologyLoader.loadMemoryModel( this.getOntologyUrl(), this.getOntologyName(), this.getProcessImport() );
3337
}
3438

3539
@Override
3640
protected OntModel loadModelFromStream( InputStream is ) {
37-
return OntologyLoader.loadMemoryModel( is, this.getOntologyUrl() );
41+
return OntologyLoader.loadMemoryModel( is, this.getOntologyUrl(), this.getProcessImport() );
3842
}
3943
}

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.hp.hpl.jena.ontology.OntModel;
2222
import com.hp.hpl.jena.ontology.OntModelSpec;
2323
import com.hp.hpl.jena.rdf.model.*;
24+
import com.hp.hpl.jena.shared.CannotCreateException;
2425
import com.hp.hpl.jena.shared.JenaException;
2526
import org.apache.commons.io.FileUtils;
2627
import org.apache.commons.lang3.StringUtils;
@@ -50,22 +51,30 @@ public class OntologyLoader {
5051
private static final String OLD_CACHE_SUFFIX = ".old";
5152
private static final String TMP_CACHE_SUFFIX = ".tmp";
5253

54+
public static OntModel loadMemoryModel( InputStream is, String url ) {
55+
return loadMemoryModel( is, url, true );
56+
}
57+
5358
/**
5459
* Load an ontology into memory. Use this type of model when fast access is critical and memory is available.
5560
*/
56-
public static OntModel loadMemoryModel( InputStream is, String url ) {
57-
OntModel model = getMemoryModel( url );
61+
public static OntModel loadMemoryModel( InputStream is, String url, boolean processImports ) {
62+
OntModel model = getMemoryModel( url, processImports );
5863
model.read( is, null );
5964
return model;
6065
}
6166

6267
/**
6368
* Load an ontology into memory. Use this type of model when fast access is critical and memory is available.
6469
*
65-
* @see #loadMemoryModel(String, String)
70+
* @see #loadMemoryModel(String, String, boolean)
6671
*/
6772
public static OntModel loadMemoryModel( String url ) {
68-
return loadMemoryModel( url, null );
73+
return loadMemoryModel( url, null, true );
74+
}
75+
76+
public static OntModel loadMemoryModel( String url, @Nullable String cacheName ) {
77+
return loadMemoryModel( url, cacheName, true );
6978
}
7079

7180
/**
@@ -77,10 +86,10 @@ public static OntModel loadMemoryModel( String url ) {
7786
* @param url a URL where the OWL file is stored
7887
* @param cacheName unique name of this ontology, will be used to load from disk in case of failed url connection
7988
*/
80-
public static OntModel loadMemoryModel( String url, @Nullable String cacheName ) {
89+
public static OntModel loadMemoryModel( String url, @Nullable String cacheName, boolean processImports ) {
8190
StopWatch timer = new StopWatch();
8291
timer.start();
83-
OntModel model = getMemoryModel( url );
92+
OntModel model = getMemoryModel( url, processImports );
8493

8594
URLConnection urlc = openConnection( url );
8695

@@ -187,12 +196,12 @@ public static void deleteOldCache( String cacheName ) {
187196
/**
188197
* Get model that is entirely in memory.
189198
*/
190-
private static OntModel getMemoryModel( String url ) {
199+
private static OntModel getMemoryModel( String url, boolean processImports ) {
191200
OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_MEM_TRANS_INF );
192201
ModelMaker maker = ModelFactory.createMemModelMaker();
193202
Model base = maker.createModel( url, false );
194203
spec.setImportModelMaker( maker );
195-
spec.getDocumentManager().setProcessImports( true );
204+
spec.getDocumentManager().setProcessImports( processImports );
196205
spec.setImportModelGetter( new ModelGetter() {
197206
@Override
198207
public Model getModel( String URL ) {
@@ -207,7 +216,7 @@ public Model getModel( String URL, ModelReader loadIfAbsent ) {
207216
try ( InputStream in = urlc.getInputStream() ) {
208217
return model.read( in, URL );
209218
} catch ( JenaException | IOException e ) {
210-
log.error( String.format( "Failed to resolve import %s for %s: %s.", URL, url, e.getMessage() ) );
219+
throw new CannotCreateException( String.format( "Failed to resolve import %s for %s.", URL, url ), e );
211220
}
212221
}
213222
return loadIfAbsent.readModel( model, URL );

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,21 @@ public class GenericOntologyService extends AbstractOntologyMemoryBackedService
3232
private final String url;
3333
private final String name;
3434
private final boolean cache;
35+
private final boolean processImports;
3536

3637
public GenericOntologyService( String name, String url ) {
3738
this( name, url, false );
3839
}
3940

4041
public GenericOntologyService( String name, String url, boolean cache ) {
42+
this( name, url, cache, true );
43+
}
44+
45+
public GenericOntologyService( String name, String url, boolean cache, boolean processImports ) {
4146
this.name = name;
4247
this.url = url;
4348
this.cache = cache;
49+
this.processImports = processImports;
4450
}
4551

4652
@Override
@@ -57,4 +63,9 @@ protected String getOntologyUrl() {
5763
protected String getCacheName() {
5864
return this.cache ? this.name : null;
5965
}
66+
67+
@Override
68+
protected boolean getProcessImport() {
69+
return processImports;
70+
}
6071
}

test/ubic/basecode/ontology/providers/AbstractOntologyServiceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public void testInitializeInBackgroundThread() throws InterruptedException {
9595
}
9696

9797
private GenericOntologyService createService( String name, String resourceURL, boolean cache ) {
98-
GenericOntologyService s = new GenericOntologyService( name, resourceURL, cache );
98+
GenericOntologyService s = new GenericOntologyService( name, resourceURL, cache, false );
9999
s.initialize( true, false );
100100
return s;
101101
}

test/ubic/basecode/ontology/search/OntologySearchTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public static void setUpUberon() throws IOException {
5959
@Test
6060
public final void testIndexing() throws Exception {
6161
InputStream is = new GZIPInputStream( requireNonNull( this.getClass().getResourceAsStream( "/data/mged.owl.gz" ) ) );
62-
OntModel model = OntologyLoader.loadMemoryModel( is, "owl-test" );
62+
OntModel model = OntologyLoader.loadMemoryModel( is, "owl-test", false );
6363

6464
SearchIndex index = OntologyIndexer.indexOntology( "MGEDTEST", model, true );
6565

@@ -168,7 +168,7 @@ public final void testOmitDefinitions2() throws Exception {
168168
OntModel model;
169169
try ( InputStream is = this.getClass().getResourceAsStream( "/data/nif.organism.test.owl.xml" ) ) {
170170
assertNotNull( is );
171-
model = OntologyLoader.loadMemoryModel( is, "NIFORG_TEST" );
171+
model = OntologyLoader.loadMemoryModel( is, "NIFORG_TEST", false );
172172
}
173173

174174
SearchIndex index = OntologyIndexer.indexOntology( "NIFORG_TEST", model, true );
@@ -228,7 +228,7 @@ public final void testOmitDefinitions3() throws Exception {
228228
public final void testOmitDefinitions4() throws Exception {
229229
InputStream is = new GZIPInputStream( requireNonNull( this.getClass().getResourceAsStream( "/data/NIF-GrossAnatomy.owl.gz" ) ) );
230230

231-
OntModel model = OntologyLoader.loadMemoryModel( is, "NIFAN_TEST2" );
231+
OntModel model = OntologyLoader.loadMemoryModel( is, "NIFAN_TEST2", false );
232232
is.close();
233233

234234
SearchIndex index = OntologyIndexer.indexOntology( "NIFAN_TEST2", model, true );
@@ -268,7 +268,7 @@ public void testScore() throws OntologySearchException {
268268
@Test
269269
public final void testPersistence() throws Exception {
270270
InputStream is = new GZIPInputStream( requireNonNull( this.getClass().getResourceAsStream( "/data/mged.owl.gz" ) ) );
271-
OntModel model = OntologyLoader.loadMemoryModel( is, "owl-test" );
271+
OntModel model = OntologyLoader.loadMemoryModel( is, "owl-test", false );
272272

273273
SearchIndex index = OntologyIndexer.indexOntology( "MGEDTEST", model, false );
274274
index.close();

0 commit comments

Comments
 (0)