Skip to content

Commit 5a31f6f

Browse files
committed
More cleanups
Eliminate AbstractOntologyMemoryBackedService and introduce two abstract class in the providers package: one for built-in baseCode ontologies and another for general use.
1 parent 975cd61 commit 5a31f6f

25 files changed

Lines changed: 291 additions & 408 deletions

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

Lines changed: 0 additions & 91 deletions
This file was deleted.

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

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
package ubic.basecode.ontology.jena;
2121

2222
import com.hp.hpl.jena.ontology.*;
23-
import com.hp.hpl.jena.rdf.model.NodeIterator;
24-
import com.hp.hpl.jena.rdf.model.Property;
25-
import com.hp.hpl.jena.rdf.model.Resource;
26-
import com.hp.hpl.jena.rdf.model.ResourceFactory;
23+
import com.hp.hpl.jena.rdf.model.*;
2724
import com.hp.hpl.jena.rdfxml.xmlinput.ARPErrorNumbers;
2825
import com.hp.hpl.jena.rdfxml.xmlinput.ParseException;
26+
import com.hp.hpl.jena.reasoner.ReasonerFactory;
27+
import com.hp.hpl.jena.reasoner.rulesys.OWLFBRuleReasonerFactory;
28+
import com.hp.hpl.jena.reasoner.rulesys.OWLMicroReasonerFactory;
29+
import com.hp.hpl.jena.reasoner.rulesys.OWLMiniReasonerFactory;
30+
import com.hp.hpl.jena.reasoner.transitiveReasoner.TransitiveReasonerFactory;
2931
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
3032
import com.hp.hpl.jena.vocabulary.DC_11;
3133
import org.apache.commons.lang3.RandomStringUtils;
@@ -40,7 +42,6 @@
4042
import ubic.basecode.ontology.providers.OntologyService;
4143
import ubic.basecode.ontology.search.OntologySearchException;
4244
import ubic.basecode.ontology.search.OntologySearchResult;
43-
import ubic.basecode.util.Configuration;
4445

4546
import javax.annotation.Nullable;
4647
import java.io.IOException;
@@ -472,13 +473,8 @@ public Set<OntologyTerm> getChildren( Collection<OntologyTerm> terms, boolean di
472473

473474
@Override
474475
public boolean isEnabled() {
475-
// quick path: just lookup the configuration
476-
String configParameter = "load." + getOntologyName();
477-
if ( Boolean.TRUE.equals( Configuration.getBoolean( configParameter ) ) ) {
478-
return true;
479-
}
480476
// could have forced, without setting config
481-
return getState().isPresent();
477+
return isOntologyEnabled() || isOntologyLoaded() || isInitializationThreadAlive();
482478
}
483479

484480
@Override
@@ -549,25 +545,69 @@ public void waitForInitializationThread() throws InterruptedException {
549545
protected abstract String getOntologyUrl();
550546

551547
/**
552-
* Delegates the call as to load the model into memory or leave it on disk. Simply delegates to either
553-
* OntologyLoader.loadMemoryModel( url ); OR OntologyLoader.loadPersistentModel( url, spec );
554-
*/
555-
protected abstract OntologyModel loadModel( boolean processImports, LanguageLevel languageLevel, InferenceMode inferenceMode ) throws IOException;
556-
557-
558-
/**
559-
* Load a model from a given input stream.
548+
* Indicate if this ontology is enabled.
560549
*/
561-
protected abstract OntologyModel loadModelFromStream( InputStream stream, boolean processImports, LanguageLevel languageLevel, InferenceMode inferenceMode ) throws IOException;
550+
protected abstract boolean isOntologyEnabled();
562551

563552
/**
564553
* A name for caching this ontology, or null to disable caching.
565554
* <p>
566555
* Note that if null is returned, the ontology will not have full-text search capabilities.
567556
*/
568557
@Nullable
569-
protected String getCacheName() {
570-
return getOntologyName();
558+
protected abstract String getCacheName();
559+
560+
/**
561+
* Delegates the call as to load the model into memory or leave it on disk. Simply delegates to either
562+
* OntologyLoader.loadMemoryModel( url ); OR OntologyLoader.loadPersistentModel( url, spec );
563+
*/
564+
protected OntologyModel loadModel( boolean processImports, LanguageLevel languageLevel, InferenceMode inferenceMode ) throws IOException {
565+
return new OntologyModelImpl( OntologyLoader.loadMemoryModel( this.getOntologyUrl(), this.getCacheName(), processImports, this.getSpec( languageLevel, inferenceMode ) ) );
566+
}
567+
568+
/**
569+
* Load a model from a given input stream.
570+
*/
571+
protected OntologyModel loadModelFromStream( InputStream is, boolean processImports, LanguageLevel languageLevel, InferenceMode inferenceMode ) throws IOException {
572+
return new OntologyModelImpl( OntologyLoader.loadMemoryModel( is, this.getOntologyUrl(), processImports, this.getSpec( languageLevel, inferenceMode ) ) );
573+
}
574+
575+
private OntModelSpec getSpec( LanguageLevel languageLevel, InferenceMode inferenceMode ) {
576+
String profile;
577+
switch ( languageLevel ) {
578+
case FULL:
579+
profile = ProfileRegistry.OWL_LANG;
580+
break;
581+
case DL:
582+
profile = ProfileRegistry.OWL_DL_LANG;
583+
break;
584+
case LITE:
585+
profile = ProfileRegistry.OWL_LITE_LANG;
586+
break;
587+
default:
588+
throw new UnsupportedOperationException( String.format( "Unsupported OWL language level %s.", languageLevel ) );
589+
}
590+
ReasonerFactory reasonerFactory;
591+
switch ( inferenceMode ) {
592+
case FULL:
593+
reasonerFactory = OWLFBRuleReasonerFactory.theInstance();
594+
break;
595+
case MINI:
596+
reasonerFactory = OWLMiniReasonerFactory.theInstance();
597+
break;
598+
case MICRO:
599+
reasonerFactory = OWLMicroReasonerFactory.theInstance();
600+
break;
601+
case TRANSITIVE:
602+
reasonerFactory = TransitiveReasonerFactory.theInstance();
603+
break;
604+
case NONE:
605+
reasonerFactory = null;
606+
break;
607+
default:
608+
throw new UnsupportedOperationException( String.format( "Unsupported inference level %s.", inferenceMode ) );
609+
}
610+
return new OntModelSpec( ModelFactory.createMemModelMaker(), null, reasonerFactory, profile );
571611
}
572612

573613
@Override

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,18 @@ class OntologyLoader {
5252
private static final String OLD_CACHE_SUFFIX = ".old";
5353
private static final String TMP_CACHE_SUFFIX = ".tmp";
5454

55-
public static OntModel loadMemoryModel( InputStream is, String url ) throws JenaException {
55+
public static OntModel loadMemoryModel( InputStream is, String url ) throws JenaException, IOException {
5656
return loadMemoryModel( is, url, true );
5757
}
5858

5959
/**
6060
* Load an ontology into memory. Use this type of model when fast access is critical and memory is available.
6161
*/
62-
public static OntModel loadMemoryModel( InputStream is, String url, boolean processImports ) throws JenaException {
62+
public static OntModel loadMemoryModel( InputStream is, String url, boolean processImports ) throws JenaException, IOException {
6363
return loadMemoryModel( is, url, processImports, OntModelSpec.OWL_MEM_TRANS_INF );
6464
}
6565

66-
public static OntModel loadMemoryModel( InputStream is, String url, boolean processImports, OntModelSpec spec ) throws JenaException {
66+
public static OntModel loadMemoryModel( InputStream is, String url, boolean processImports, OntModelSpec spec ) throws JenaException, IOException {
6767
OntModel model = getMemoryModel( url, processImports, spec );
6868
model.read( is, null );
6969
return model;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package ubic.basecode.ontology.providers;
2+
3+
import ubic.basecode.util.Configuration;
4+
5+
import javax.annotation.Nullable;
6+
7+
/**
8+
* Base class for all ontologies built-in to the baseCode project.
9+
* <p>
10+
* The ontologies that subclass this will honor settings in the {@code basecode.properties} file for loading and
11+
* locating the ontology.
12+
*
13+
* @author poirigui
14+
*/
15+
public abstract class AbstractBaseCodeOntologyService extends AbstractOntologyService {
16+
17+
private final String name;
18+
private final String cacheName;
19+
20+
/**
21+
* Intentionally package-private constructor.
22+
*/
23+
AbstractBaseCodeOntologyService( String name, String cacheName ) {
24+
this.name = name;
25+
this.cacheName = cacheName;
26+
}
27+
28+
@Override
29+
protected String getOntologyName() {
30+
return name;
31+
}
32+
33+
@Override
34+
protected String getOntologyUrl() {
35+
return Configuration.getString( "url." + cacheName );
36+
}
37+
38+
@Override
39+
protected boolean isOntologyEnabled() {
40+
return Boolean.TRUE.equals( Configuration.getBoolean( "load." + cacheName ) );
41+
}
42+
43+
@Nullable
44+
@Override
45+
public String getCacheName() {
46+
return cacheName;
47+
}
48+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package ubic.basecode.ontology.providers;
2+
3+
/**
4+
* Base class for all ontology services.
5+
* <p>
6+
* The actual implementation is provided by the {@code ubic.basecode.ontology.jena} package.
7+
*/
8+
public abstract class AbstractOntologyService extends ubic.basecode.ontology.jena.AbstractOntologyService {
9+
}
Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
22
* The baseCode project
3-
*
3+
*
44
* Copyright (c) 2010 University of British Columbia
5-
*
5+
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
88
* You may obtain a copy of the License at
@@ -19,26 +19,14 @@
1919

2020
package ubic.basecode.ontology.providers;
2121

22-
import ubic.basecode.ontology.jena.AbstractOntologyMemoryBackedService;
23-
import ubic.basecode.util.Configuration;
24-
2522
/**
26-
* See http://www.obofoundry.org/cgi-bin/detail.cgi?id=CLO
27-
*
23+
* <a href="https://obofoundry.org/ontology/clo.html">Cell Line Ontology</a>
24+
*
2825
* @author paul
29-
*
3026
*/
31-
public class CellLineOntologyService extends AbstractOntologyMemoryBackedService {
32-
33-
private static final String ONTOLOGY_URL = "url.cellLineOntology";
34-
35-
@Override
36-
protected String getOntologyName() {
37-
return "cellLineOntology";
38-
}
27+
public class CellLineOntologyService extends AbstractBaseCodeOntologyService {
3928

40-
@Override
41-
protected String getOntologyUrl() {
42-
return Configuration.getString( ONTOLOGY_URL );
29+
public CellLineOntologyService() {
30+
super( "Cell Line Ontology", "cellLineOntology" );
4331
}
4432
}
Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
22
* The baseCode project
3-
*
3+
*
44
* Copyright (c) 2010 University of British Columbia
5-
*
5+
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
88
* You may obtain a copy of the License at
@@ -19,26 +19,14 @@
1919

2020
package ubic.basecode.ontology.providers;
2121

22-
import ubic.basecode.ontology.jena.AbstractOntologyMemoryBackedService;
23-
import ubic.basecode.util.Configuration;
24-
2522
/**
26-
* See http://www.obofoundry.org/cgi-bin/detail.cgi?id=cell
27-
*
23+
* <a href="https://obofoundry.org/ontology/cl.html">Cell Ontology</a>
24+
*
2825
* @author paul
29-
*
3026
*/
31-
public class CellTypeOntologyService extends AbstractOntologyMemoryBackedService {
32-
33-
private static final String ONTOLOGY_URL = "url.cellTypeOntology";
34-
35-
@Override
36-
protected String getOntologyName() {
37-
return "cellTypeOntology";
38-
}
27+
public class CellTypeOntologyService extends AbstractBaseCodeOntologyService {
3928

40-
@Override
41-
protected String getOntologyUrl() {
42-
return Configuration.getString( ONTOLOGY_URL );
29+
public CellTypeOntologyService() {
30+
super( "Cell Ontology", "cellTypeOntology" );
4331
}
44-
}
32+
}

0 commit comments

Comments
 (0)