<commit_before>/*
 * The Gemma project
 * 
 * Copyright (c) 2007 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
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package ubic.basecode.ontology.providers;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.StopWatch;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.pool.PoolableObjectFactory;
import org.apache.commons.pool.impl.GenericObjectPool;

import ubic.basecode.ontology.Configuration;
import ubic.basecode.ontology.OntologyLoader;
import ubic.basecode.ontology.model.OntologyIndividual;
import ubic.basecode.ontology.model.OntologyResource;
import ubic.basecode.ontology.model.OntologyTerm;
import ubic.basecode.ontology.search.OntologyIndexer;
import ubic.basecode.ontology.search.OntologySearch;

import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.query.larq.IndexLARQ;

/**
 * @author kelsey
 * @version $Id$
 */
public abstract class AbstractOntologyService {

    protected static final Log log = LogFactory.getLog( AbstractOntologyService.class );

    protected AtomicBoolean cacheReady = new AtomicBoolean( false );
    protected IndexLARQ index;
    protected AtomicBoolean indexReady = new AtomicBoolean( false );

    protected Map<String, OntologyIndividual> individuals;

    protected AtomicBoolean modelReady = new AtomicBoolean( false );
    protected String ontology_URL;
    protected String ontologyName;

    private GenericObjectPool pool;

    protected AtomicBoolean ready = new AtomicBoolean( false );

    protected AtomicBoolean running = new AtomicBoolean( false );

    protected Map<String, OntologyTerm> terms;

    private boolean enabled = false;

    /**
     * 
     */
    public AbstractOntologyService() {
        super();
        ontology_URL = getOntologyUrl();
        ontologyName = getOntologyName();

        /*
         * We use a pool, because each of these holds on to a database connection forever.
         */
        this.pool = new GenericObjectPool( new PoolableObjectFactory() {

            @Override
            public void activateObject( Object obj ) throws Exception {
                // no op
            }

            @Override
            public void destroyObject( Object obj ) throws Exception {
//                ( ( OntModel ) obj ).close();
//                obj = null;
            }

            @Override
            public Object makeObject() throws Exception {
                log.warn( "Making model for: " + ontology_URL );
                OntModel model = loadModel( ontology_URL );
                modelReady.set( true );
                return model;
            }

            @Override
            public void passivateObject( Object obj ) throws Exception {
                // no op
            }

            @Override
            public boolean validateObject( Object obj ) {
                return true;
            }
        } );

        /*
         * These could be made into configuration settings (BASECODE)
         */
        pool.setMaxActive( 10 );
        pool.setMinIdle( 0 );
        pool.setMaxWait( 10000 );
        pool.setMinEvictableIdleTimeMillis( 1000L * 60L * 60L );
        pool.setTimeBetweenEvictionRunsMillis( 1000L * 60L );<commit_msg>for non-RDB-backed models, never idle the Models. This is only needed for the DB-backed models.<commit_after>/*
 * The Gemma project
 * 
 * Copyright (c) 2007 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
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package ubic.basecode.ontology.providers;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.StopWatch;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.pool.PoolableObjectFactory;
import org.apache.commons.pool.impl.GenericObjectPool;

import ubic.basecode.ontology.Configuration;
import ubic.basecode.ontology.OntologyLoader;
import ubic.basecode.ontology.model.OntologyIndividual;
import ubic.basecode.ontology.model.OntologyResource;
import ubic.basecode.ontology.model.OntologyTerm;
import ubic.basecode.ontology.search.OntologyIndexer;
import ubic.basecode.ontology.search.OntologySearch;

import com.hp.hpl.jena.db.ModelRDB;
import com.hp.hpl.jena.db.impl.GraphRDBMaker;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.query.larq.IndexLARQ;

/**
 * @author kelsey
 * @version $Id$
 */
public abstract class AbstractOntologyService {

    protected static final Log log = LogFactory.getLog( AbstractOntologyService.class );

    protected AtomicBoolean cacheReady = new AtomicBoolean( false );
    protected IndexLARQ index;
    protected AtomicBoolean indexReady = new AtomicBoolean( false );

    protected Map<String, OntologyIndividual> individuals;

    protected AtomicBoolean modelReady = new AtomicBoolean( false );
    protected String ontology_URL;
    protected String ontologyName;

    private final GenericObjectPool pool;

    protected AtomicBoolean ready = new AtomicBoolean( false );

    protected AtomicBoolean running = new AtomicBoolean( false );

    protected Map<String, OntologyTerm> terms;

    private boolean enabled = false;

    /**
     * 
     */
    public AbstractOntologyService() {
        super();
        ontology_URL = getOntologyUrl();
        ontologyName = getOntologyName();

        /*
         * We use a pool, because each of these holds on to a database connection forever.
         */
        this.pool = new GenericObjectPool( new PoolableObjectFactory() {

            @Override
            public void activateObject( Object obj ) throws Exception {
                // no op
            }

            @Override
            public void destroyObject( Object obj ) throws Exception {
                // ( ( OntModel ) obj ).close();
                // obj = null;
            }

            @Override
            public Object makeObject() throws Exception {
                log.warn( "Making model for: " + ontology_URL );
                OntModel model = loadModel( ontology_URL );
                if ( !( model.getImportModelMaker().getGraphMaker() instanceof GraphRDBMaker ) ) {
                    pool.setMinIdle( 1 );
                    pool.setMinEvictableIdleTimeMillis( -1 );
                }
                modelReady.set( true );
                return model;
            }

            @Override
            public void passivateObject( Object obj ) throws Exception {
                // no op
            }

            @Override
            public boolean validateObject( Object obj ) {
                return true;
            }
        } );

        /*
         * These could be made into configuration settings (BASECODE)
         */
        pool.setMaxActive( 10 );
        pool.setMinIdle( 0 );
        pool.setMaxWait( 10000 );
        pool.setMinEvictableIdleTimeMillis( 1000L * 60L * 60L );
        pool.setTimeBetweenEvictionRunsMillis( 1000L * 60L );
{'fraction_non_alphanumeric': 0.08460730540319757, 'fraction_numerical': 0.0072353833586182754, 'mean_word_length': 2.648361004682844, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '257854', 'n_tokens_mistral': 2401, 'n_tokens_neox': 2268, 'n_words': 659}