View Javadoc
1   package ubic.basecode.ontology.jena;
2   
3   import ubic.basecode.ontology.model.OntologyModel;
4   
5   import org.jspecify.annotations.Nullable;
6   import java.io.IOException;
7   import java.io.InputStream;
8   import java.util.zip.GZIPInputStream;
9   
10  /**
11   * An ontology service that loads an ontology from a classpath resource.
12   * @author poirigui
13   */
14  public class ClasspathOntologyService extends AbstractOntologyService {
15  
16      private final String classpathResource;
17  
18      public ClasspathOntologyService( String name, String classpathResource, boolean ontologyEnabled, @Nullable String cacheName ) {
19          super( name, "classpath:" + classpathResource, ontologyEnabled, cacheName );
20          this.classpathResource = classpathResource;
21      }
22  
23      @Override
24      protected OntologyModel loadModel( boolean processImports, LanguageLevel languageLevel, InferenceMode inferenceMode ) throws IOException {
25          try ( InputStream stream = getClass().getResourceAsStream( classpathResource ) ) {
26              if ( stream == null ) {
27                  throw new RuntimeException( String.format( "The NIF ontology was not found in classpath at %s.", classpathResource ) );
28              }
29              return loadModelFromStream( classpathResource.endsWith( ".gz" ) ? new GZIPInputStream( stream ) : stream, processImports, languageLevel, inferenceMode );
30          }
31      }
32  
33      @Override
34      protected OntologyModel loadModelFromStream( InputStream is, boolean processImports, LanguageLevel languageLevel, InferenceMode inferenceMode ) throws IOException {
35          return new OntologyModelImpl( OntologyLoader.createMemoryModel( is, this.getOntologyUrl(), processImports, this.getSpec( languageLevel, inferenceMode ) ) );
36      }
37  }