Skip to content

Commit 12c732b

Browse files
committed
Extract an interface from AbstractOntologyService to make it easier to reuse
1 parent d946ff4 commit 12c732b

File tree

3 files changed

+147
-75
lines changed

3 files changed

+147
-75
lines changed
Lines changed: 25 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) 2007-2019 Columbia University
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
@@ -22,48 +22,53 @@
2222

2323
/**
2424
* @author Paul
25-
*
2625
*/
2726
public interface OntologyTerm extends OntologyResource {
2827

29-
public Collection<String> getAlternativeIds();
28+
Collection<String> getAlternativeIds();
3029

31-
public Collection<AnnotationProperty> getAnnotations();
30+
Collection<AnnotationProperty> getAnnotations();
31+
32+
Collection<OntologyTerm> getChildren( boolean direct );
3233

3334
/**
3435
* @param direct return only the immediate children; if false, return all of them down to the leaves.
36+
* @param includePartOf include terms matched via
3537
* @return
3638
*/
37-
public Collection<OntologyTerm> getChildren( boolean direct );
39+
Collection<OntologyTerm> getChildren( boolean direct, boolean includePartOf );
3840

39-
public String getComment();
41+
String getComment();
4042

41-
public Collection<OntologyIndividual> getIndividuals();
43+
Collection<OntologyIndividual> getIndividuals();
4244

43-
public Collection<OntologyIndividual> getIndividuals( boolean direct );
45+
Collection<OntologyIndividual> getIndividuals( boolean direct );
4446

45-
public String getLocalName();
47+
String getLocalName();
4648

47-
public Object getModel();
49+
Object getModel();
4850

4951
/**
5052
* Note that any restriction superclasses are not returned, unless they are has_proper_part
51-
*
53+
*
5254
* @param direct
5355
* @return
5456
*/
55-
public Collection<OntologyTerm> getParents( boolean direct );
57+
Collection<OntologyTerm> getParents( boolean direct );
5658

57-
public Collection<OntologyRestriction> getRestrictions();
59+
Collection<OntologyTerm> getParents( boolean direct, boolean includePartOf );
5860

59-
public String getTerm();
61+
Collection<OntologyRestriction> getRestrictions();
6062

61-
@Override
62-
public String getUri();
63+
String getTerm();
6364

64-
public boolean isRoot();
65+
@Override
66+
String getUri();
6567

66-
/** check to see if the term is obsolete, if it is it should not be used */
67-
public boolean isTermObsolete();
68+
boolean isRoot();
6869

70+
/**
71+
* check to see if the term is obsolete, if it is it should not be used
72+
*/
73+
boolean isTermObsolete();
6974
}

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

Lines changed: 19 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
* @author kelsey
5151
*/
5252
@SuppressWarnings("unused")
53-
public abstract class AbstractOntologyService {
53+
public abstract class AbstractOntologyService implements OntologyService {
5454

5555
protected static Logger log = LoggerFactory.getLogger( AbstractOntologyService.class );
5656

@@ -67,9 +67,7 @@ public abstract class AbstractOntologyService {
6767

6868
private boolean isInitialized = false;
6969

70-
/**
71-
* Initialize this ontology service.
72-
*/
70+
@Override
7371
public void initialize( boolean forceLoad, boolean forceIndexing ) {
7472
if ( !forceLoad && isInitialized ) {
7573
log.warn( getOntologyName() + " is already loaded, and force=false, not restarting" );
@@ -155,9 +153,7 @@ public void closeIndex() {
155153
index.close();
156154
}
157155

158-
/**
159-
* Looks for any OntologyIndividuals that match the given search string.
160-
*/
156+
@Override
161157
public Collection<OntologyIndividual> findIndividuals( String search ) throws OntologySearchException {
162158
Lock lock = rwLock.readLock();
163159
try {
@@ -176,12 +172,7 @@ public Collection<OntologyIndividual> findIndividuals( String search ) throws On
176172
}
177173
}
178174

179-
/**
180-
* Looks for any OntologyIndividuals or ontologyTerms that match the given search string
181-
*
182-
* @return results, or an empty collection if the results are empty OR the ontology is not available to be
183-
* searched.
184-
*/
175+
@Override
185176
public Collection<OntologyResource> findResources( String searchString ) throws OntologySearchException {
186177
Lock lock = rwLock.readLock();
187178
try {
@@ -200,9 +191,7 @@ public Collection<OntologyResource> findResources( String searchString ) throws
200191
}
201192
}
202193

203-
/**
204-
* Looks for any ontologyTerms that match the given search string. Obsolete terms are filtered out.
205-
*/
194+
@Override
206195
public Collection<OntologyTerm> findTerm( String search ) throws OntologySearchException {
207196
if ( log.isDebugEnabled() ) log.debug( "Searching " + getOntologyName() + " for '" + search + "'" );
208197
Lock lock = rwLock.readLock();
@@ -222,6 +211,7 @@ public Collection<OntologyTerm> findTerm( String search ) throws OntologySearchE
222211
}
223212
}
224213

214+
@Override
225215
public OntologyTerm findUsingAlternativeId( String alternativeId ) {
226216
Lock lock = alternativeIDs != null ? rwLock.readLock() : rwLock.writeLock();
227217
try {
@@ -241,6 +231,7 @@ public OntologyTerm findUsingAlternativeId( String alternativeId ) {
241231
}
242232
}
243233

234+
@Override
244235
public Set<String> getAllURIs() {
245236
Lock lock = rwLock.readLock();
246237
try {
@@ -264,10 +255,7 @@ public Set<String> getAllURIs() {
264255
}
265256
}
266257

267-
/**
268-
* Looks through both Terms and Individuals for a OntologyResource that has a uri matching the uri given. If no
269-
* OntologyTerm is found only then will ontologyIndividuals be searched. returns null if nothing is found.
270-
*/
258+
@Override
271259
public OntologyResource getResource( String uri ) {
272260
if ( uri == null ) return null;
273261
Lock lock = rwLock.readLock();
@@ -297,9 +285,7 @@ public OntologyResource getResource( String uri ) {
297285
}
298286
}
299287

300-
/**
301-
* Looks for a OntologyTerm that has the match in URI given
302-
*/
288+
@Override
303289
public OntologyTerm getTerm( String uri ) {
304290
if ( uri == null ) throw new IllegalArgumentException( "URI cannot be null" );
305291
Lock lock = rwLock.readLock();
@@ -312,6 +298,7 @@ public OntologyTerm getTerm( String uri ) {
312298
}
313299
}
314300

301+
@Override
315302
public Collection<OntologyIndividual> getTermIndividuals( String uri ) {
316303
Lock lock = rwLock.readLock();
317304
try {
@@ -333,6 +320,7 @@ public Collection<OntologyIndividual> getTermIndividuals( String uri ) {
333320
}
334321
}
335322

323+
@Override
336324
public boolean isEnabled() {
337325
// quick path: just lookup the configuration
338326
String configParameter = "load." + getOntologyName();
@@ -349,28 +337,15 @@ public boolean isEnabled() {
349337
}
350338
}
351339

352-
/**
353-
* Used for determining if the Ontology has finished loading into memory. Although calls like getParents,
354-
* getChildren will still work (its much faster once the ontologies have been preloaded into memory.)
355-
*/
340+
@Override
356341
public boolean isOntologyLoaded() {
357342
// it's fine not to use the read lock here
358343
return isInitialized;
359344
}
360345

361346
private Thread initializationThread = null;
362347

363-
/**
364-
* Start the initialization thread.
365-
* <p>
366-
* If the initialization thread is already running, this method does nothing. If the initialization thread
367-
* previously completed, the ontology will be reinitialized.
368-
*
369-
* @param forceLoad Force loading of ontology, even if it is already loaded
370-
* @param forceIndexing If forceLoad is also true, indexing will be performed. If you know the index is
371-
* up to date, there's no need to do it again. Normally indexing is only done if there is no
372-
* index, or if the ontology has changed since last loaded.
373-
*/
348+
@Override
374349
public synchronized void startInitializationThread( boolean forceLoad, boolean forceIndexing ) {
375350
if ( initializationThread != null && initializationThread.isAlive() ) {
376351
log.warn( String.format( " Initialization thread for %s is currently running, not restarting.", getOntologyName() ) );
@@ -394,27 +369,28 @@ public synchronized void startInitializationThread( boolean forceLoad, boolean f
394369
initializationThread.start();
395370
}
396371

372+
@Override
397373
public boolean isInitializationThreadAlive() {
398374
return initializationThread != null && initializationThread.isAlive();
399375
}
400376

377+
@Override
401378
public boolean isInitializationThreadCancelled() {
402379
return initializationThread != null && initializationThread.isInterrupted();
403380
}
404381

405382
/**
406383
* Cancel the initialization thread.
407384
*/
385+
@Override
408386
public void cancelInitializationThread() {
409387
if ( initializationThread == null ) {
410388
throw new IllegalStateException( "The initialization thread has not started. Invoke startInitializationThread() first." );
411389
}
412390
initializationThread.interrupt();
413391
}
414392

415-
/**
416-
* Wait for the initialization thread to finish.
417-
*/
393+
@Override
418394
public void waitForInitializationThread() throws InterruptedException {
419395
if ( initializationThread == null ) {
420396
throw new IllegalStateException( "The initialization thread has not started. Invoke startInitializationThread() first." );
@@ -439,13 +415,7 @@ public void waitForInitializationThread() throws InterruptedException {
439415
*/
440416
protected abstract OntModel loadModel();
441417

442-
/**
443-
* Index the ontology for performing full-text searches.
444-
*
445-
* @see #findIndividuals(String)
446-
* @see #findTerm(String)
447-
* @see #findResources(String)
448-
*/
418+
@Override
449419
public void index( boolean force ) {
450420
Lock lock = rwLock.writeLock();
451421
try {
@@ -504,13 +474,7 @@ private OntologyTerm getTermInternal( String uri ) {
504474
} );
505475
}
506476

507-
/**
508-
* For testing! Overrides normal way of loading the ontology. This does not index the ontology unless 'force' is
509-
* true (if there is an existing index, it will be used)
510-
*
511-
* @param is input stream from which the ontology model is loaded
512-
* @param forceIndex initialize the index. Otherwise it will only be initialized if it doesn't exist.
513-
*/
477+
@Override
514478
public void loadTermsInNameSpace( InputStream is, boolean forceIndex ) {
515479
Lock lock = rwLock.writeLock();
516480
try {
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package ubic.basecode.ontology.providers;
2+
3+
import ubic.basecode.ontology.model.OntologyIndividual;
4+
import ubic.basecode.ontology.model.OntologyResource;
5+
import ubic.basecode.ontology.model.OntologyTerm;
6+
import ubic.basecode.ontology.search.OntologySearchException;
7+
8+
import java.io.InputStream;
9+
import java.util.Collection;
10+
import java.util.Set;
11+
12+
public interface OntologyService {
13+
14+
/**
15+
* Initialize this ontology service.
16+
*/
17+
void initialize( boolean forceLoad, boolean forceIndexing );
18+
19+
/**
20+
* Looks for any OntologyIndividuals that match the given search string.
21+
*/
22+
Collection<OntologyIndividual> findIndividuals( String search ) throws OntologySearchException;
23+
24+
/**
25+
* Looks for any OntologyIndividuals or ontologyTerms that match the given search string
26+
*
27+
* @return results, or an empty collection if the results are empty OR the ontology is not available to be
28+
* searched.
29+
*/
30+
Collection<OntologyResource> findResources( String searchString ) throws OntologySearchException;
31+
32+
/**
33+
* Looks for any ontologyTerms that match the given search string. Obsolete terms are filtered out.
34+
*/
35+
Collection<OntologyTerm> findTerm( String search ) throws OntologySearchException;
36+
37+
OntologyTerm findUsingAlternativeId( String alternativeId );
38+
39+
Set<String> getAllURIs();
40+
41+
/**
42+
* Looks through both Terms and Individuals for a OntologyResource that has a uri matching the uri given. If no
43+
* OntologyTerm is found only then will ontologyIndividuals be searched. returns null if nothing is found.
44+
*/
45+
OntologyResource getResource( String uri );
46+
47+
/**
48+
* Looks for a OntologyTerm that has the match in URI given
49+
*/
50+
OntologyTerm getTerm( String uri );
51+
52+
Collection<OntologyIndividual> getTermIndividuals( String uri );
53+
54+
boolean isEnabled();
55+
56+
/**
57+
* Used for determining if the Ontology has finished loading into memory. Although calls like getParents,
58+
* getChildren will still work (its much faster once the ontologies have been preloaded into memory.)
59+
*/
60+
boolean isOntologyLoaded();
61+
62+
/**
63+
* Start the initialization thread.
64+
* <p>
65+
* If the initialization thread is already running, this method does nothing. If the initialization thread
66+
* previously completed, the ontology will be reinitialized.
67+
*
68+
* @param forceLoad Force loading of ontology, even if it is already loaded
69+
* @param forceIndexing If forceLoad is also true, indexing will be performed. If you know the index is
70+
* up to date, there's no need to do it again. Normally indexing is only done if there is no
71+
* index, or if the ontology has changed since last loaded.
72+
*/
73+
void startInitializationThread( boolean forceLoad, boolean forceIndexing );
74+
75+
boolean isInitializationThreadAlive();
76+
77+
boolean isInitializationThreadCancelled();
78+
79+
void cancelInitializationThread();
80+
81+
/**
82+
* Wait for the initialization thread to finish.
83+
*/
84+
void waitForInitializationThread() throws InterruptedException;
85+
86+
/**
87+
* Index the ontology for performing full-text searches.
88+
*
89+
* @see #findIndividuals(String)
90+
* @see #findTerm(String)
91+
* @see #findResources(String)
92+
*/
93+
void index( boolean force );
94+
95+
/**
96+
* For testing! Overrides normal way of loading the ontology. This does not index the ontology unless 'force' is
97+
* true (if there is an existing index, it will be used)
98+
*
99+
* @param is input stream from which the ontology model is loaded
100+
* @param forceIndex initialize the index. Otherwise it will only be initialized if it doesn't exist.
101+
*/
102+
void loadTermsInNameSpace( InputStream is, boolean forceIndex );
103+
}

0 commit comments

Comments
 (0)