Skip to content

Commit

Permalink
Merge branch 'hotfix-1.1.11'
Browse files Browse the repository at this point in the history
  • Loading branch information
arteymix committed Mar 27, 2023
2 parents 48ac3c8 + 84c10f7 commit 3bd83fe
Show file tree
Hide file tree
Showing 13 changed files with 838 additions and 983 deletions.
13 changes: 6 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<name>baseCode</name>
<groupId>baseCode</groupId>
<artifactId>baseCode</artifactId>
<version>1.1.10</version>
<version>1.1.11</version>
<inceptionYear>2003</inceptionYear>
<description>
<![CDATA[Data structures, math and statistics tools, and utilities that are often needed across projects.]]>
Expand Down Expand Up @@ -132,11 +132,6 @@
<artifactId>mtj</artifactId>
<version>1.0.4</version>
</dependency>
<dependency>
<groupId>net.sourceforge.f2j</groupId>
<artifactId>arpack_combined_all</artifactId>
<version>0.1</version>
</dependency>

<!-- Logging -->
<dependency>
Expand Down Expand Up @@ -178,6 +173,10 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<artifactId>icu4j</artifactId>
<groupId>com.ibm.icu</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
Expand Down Expand Up @@ -378,7 +377,7 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<argLine>-enableassertions -Xmx512m -Djava.awt.headless=true</argLine>
<argLine>-enableassertions -Xmx1024m -Djava.awt.headless=true</argLine>
<reportsDirectory>target/surefire-reports</reportsDirectory>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
<includes>
Expand Down
201 changes: 37 additions & 164 deletions src/ubic/basecode/ontology/OntologyLoader.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
* The baseCode project
*
*
* Copyright (c) 2010 University of British Columbia
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand All @@ -18,49 +18,29 @@
*/
package ubic.basecode.ontology;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Collection;
import java.util.HashSet;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.StopWatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.ModelMaker;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;

import ubic.basecode.ontology.model.OntologyIndividual;
import ubic.basecode.ontology.model.OntologyIndividualImpl;
import ubic.basecode.ontology.model.OntologyProperty;
import ubic.basecode.ontology.model.OntologyResource;
import ubic.basecode.ontology.model.OntologyTerm;
import ubic.basecode.ontology.model.OntologyTermImpl;
import ubic.basecode.ontology.model.PropertyFactory;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.StopWatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ubic.basecode.util.Configuration;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

/**
* Reads ontologies from OWL resources
*
* @author paul
*
* @author paul
*/
public class OntologyLoader {

Expand All @@ -69,133 +49,37 @@ public class OntologyLoader {
private static final String OLD_CACHE_SUFFIX = ".old";
private static final String TMP_CACHE_SUFFIX = ".tmp";

/**
* @param url
* @param model
* @return
*/
public static Collection<OntologyResource> initialize( String url, OntModel model ) {

Collection<OntologyResource> result = new HashSet<>();

ExtendedIterator<OntClass> classIt = model.listClasses();
int count = 0;
log.debug( "Reading classes for ontology: " + url );
while ( classIt.hasNext() ) {
OntClass element = classIt.next();
if ( element.isAnon() ) continue;
OntologyTerm ontologyTerm = new OntologyTermImpl( element );
result.add( ontologyTerm );
if ( ++count % 1000 == 0 ) {
log.debug( "Loaded " + count + " terms, last was " + ontologyTerm );
}
}

log.debug( "Loaded " + count + " terms" );

ExtendedIterator<com.hp.hpl.jena.ontology.ObjectProperty> propIt = model.listObjectProperties();
count = 0;
log.debug( "Reading object properties..." );
while ( propIt.hasNext() ) {
com.hp.hpl.jena.ontology.ObjectProperty element = propIt.next();
OntologyProperty ontologyTerm = PropertyFactory.asProperty( element );
if ( ontologyTerm == null ) continue; // couldn't be converted for some reason.
result.add( ontologyTerm );
if ( ++count % 1000 == 0 ) {
log.debug( "Loaded " + count + " object properties, last was " + ontologyTerm );
}
}

ExtendedIterator<com.hp.hpl.jena.ontology.DatatypeProperty> dtPropIt = model.listDatatypeProperties();
log.debug( "Reading datatype properties..." );
while ( dtPropIt.hasNext() ) {
com.hp.hpl.jena.ontology.DatatypeProperty element = dtPropIt.next();
OntologyProperty ontologyTerm = PropertyFactory.asProperty( element );
if ( ontologyTerm == null ) continue; // couldn't be converted for some reason.
result.add( ontologyTerm );
if ( ++count % 1000 == 0 ) {
log.debug( "Loaded " + count + " datatype properties, last was " + ontologyTerm );
}
}

log.debug( "Loaded " + count + " properties" );

ExtendedIterator<Individual> indiIt = model.listIndividuals();
count = 0;
log.debug( "Reading individuals..." );
while ( indiIt.hasNext() ) {
Individual element = indiIt.next();
if ( element.isAnon() ) continue;
OntologyIndividual ontologyTerm = new OntologyIndividualImpl( element );
result.add( ontologyTerm );
if ( ++count % 1000 == 0 ) {
log.debug( "Loaded " + count + " individuals, last was " + ontologyTerm );
}
}
log.debug( "Loaded " + count + " individuals" );
return result;
}

/**
* Load an ontology into memory. Use this type of model when fast access is critical and memory is available.
*
* @param is
* @param url, used as a key
* @param spec
* @return
*/
public static OntModel loadMemoryModel( InputStream is, String url, OntModelSpec spec ) {
OntModel model = getMemoryModel( url, spec );
public static OntModel loadMemoryModel( InputStream is, String url ) {
OntModel model = getMemoryModel( url );
model.read( is, null );
return model;
}

/**
* Load an ontology into memory. Use this type of model when fast access is critical and memory is available. Uses
* OWL_MEM_TRANS_INF
*
* @param url
* @return
*/
public static OntModel loadMemoryModel( String url ) {
return loadMemoryModel( url, OntModelSpec.OWL_MEM_TRANS_INF );
}

/**
* Load an ontology into memory. Use this type of model when fast access is critical and memory is available. Uses
* OWL_MEM_TRANS_INF
* If load from URL fails, attempt to load from disk cache under @cacheName.
*
* @param url
* @return
*/
public static OntModel loadMemoryModel( String url, String cacheName ) {
return loadMemoryModel( url, OntModelSpec.OWL_MEM_TRANS_INF, cacheName );
}

/**
* Load an ontology into memory. Use this type of model when fast access is critical and memory is available.
*
* @param url
* @return
*
* @see #loadMemoryModel(String, String)
*/
public static OntModel loadMemoryModel( String url, OntModelSpec spec ) {
return loadMemoryModel( url, spec, null );
public static OntModel loadMemoryModel( String url ) {
return loadMemoryModel( url, null );
}

/**
* Load an ontology into memory. Use this type of model when fast access is critical and memory is available.
* If load from URL fails, attempt to load from disk cache under @cacheName.
*
* @param url
* @param spec e.g. OWL_MEM_TRANS_INF
* @param cacheName unique name of this ontology, will be used to load from disk in case of failed url connection
* @return
* <p>
* Uses {@link OntModelSpec#OWL_MEM_TRANS_INF}.
*
* @param url a URL where the OWL file is stored
* @param cacheName unique name of this ontology, will be used to load from disk in case of failed url connection
*/
public static OntModel loadMemoryModel( String url, OntModelSpec spec, String cacheName ) {
public static OntModel loadMemoryModel( String url, String cacheName ) {
StopWatch timer = new StopWatch();
timer.start();
OntModel model = getMemoryModel( url, spec );
OntModel model = getMemoryModel( url );

URLConnection urlc = null;
int tries = 0;
Expand Down Expand Up @@ -242,7 +126,7 @@ public static OntModel loadMemoryModel( String url, OntModelSpec spec, String ca
}

if ( urlc != null ) {
try (InputStream in = urlc.getInputStream();) {
try ( InputStream in = urlc.getInputStream(); ) {
Reader reader;
if ( cacheName != null ) {
// write tmp to disk
Expand All @@ -261,7 +145,7 @@ public static OntModel loadMemoryModel( String url, OntModelSpec spec, String ca
}

assert reader != null;
try (BufferedReader buf = new BufferedReader( reader );) {
try ( BufferedReader buf = new BufferedReader( reader ); ) {
model.read( buf, url );
}

Expand All @@ -286,7 +170,7 @@ public static OntModel loadMemoryModel( String url, OntModelSpec spec, String ca
}

if ( f.exists() && !f.isDirectory() ) {
try (BufferedReader buf = new BufferedReader( new FileReader( f ) );) {
try ( BufferedReader buf = new BufferedReader( new FileReader( f ) ); ) {
model.read( buf, url );
// We successfully loaded the cached ontology. Copy the loaded ontology to oldFile
// so that we don't recreate indices during initialization based on a false change in
Expand Down Expand Up @@ -359,25 +243,14 @@ public static boolean deleteOldCache( String cacheName ) {
return false;
}

/**
* Get model that is entirely in memory with default OntModelSpec.OWL_MEM_RDFS_INF.
*
* @param url
* @return
*/
static OntModel getMemoryModel( String url ) {
return getMemoryModel( url, OntModelSpec.OWL_MEM_RDFS_INF );
}

/**
* Get model that is entirely in memory.
*
* @param url
* @param specification
*
* @param url
* @return
*/
static OntModel getMemoryModel( String url, OntModelSpec specification ) {
OntModelSpec spec = new OntModelSpec( specification );
private static OntModel getMemoryModel( String url ) {
OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_MEM_TRANS_INF );
ModelMaker maker = ModelFactory.createMemModelMaker();
Model base = maker.createModel( url, false );
spec.setImportModelMaker( maker );
Expand All @@ -389,7 +262,7 @@ static OntModel getMemoryModel( String url, OntModelSpec specification ) {
}

/**
* @param name
* @param name
* @return
*/
public static File getDiskCachePath( String name ) {
Expand Down
Loading

0 comments on commit 3bd83fe

Please sign in to comment.