All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.bigdata.rdf.inf.BackchainOwlSameAsIterator Maven / Gradle / Ivy

package com.bigdata.rdf.inf;

import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;
import org.apache.log4j.Logger;
import com.bigdata.rdf.internal.IV;
import com.bigdata.rdf.spo.ISPO;
import com.bigdata.rdf.store.AbstractTripleStore;
import com.bigdata.rdf.store.TempTripleStore;
import com.bigdata.striterator.IChunkedOrderedIterator;

public abstract class BackchainOwlSameAsIterator implements IChunkedOrderedIterator {
    
    protected final static Logger log = Logger.getLogger(BackchainOwlSameAsIterator.class);
    
    /**
     * The database.
     */
    protected AbstractTripleStore db;

    /**
     * This flag is true since we do NOT want statement
     * identifiers to be generated for inferences produced by the backchainer.
     */
    protected final boolean copyOnly = true;
    
    protected IV sameAs;

    final int chunkSize = 100;//10000;

    protected IChunkedOrderedIterator src;
    
    public BackchainOwlSameAsIterator(IChunkedOrderedIterator src,
            AbstractTripleStore db, IV sameAs) {

        if (src == null)
            throw new IllegalArgumentException();
        
        if (db == null)
            throw new IllegalArgumentException();
        
        if (sameAs == null)
            throw new IllegalArgumentException();
        
        this.src = src;
        
        this.db = db;
        
        this.sameAs = sameAs;
    
    }
    
    protected Set getSelfAndSames(IV iv) {
        Set selfAndSames = new TreeSet();
        selfAndSames.add(iv);
        getSames(iv, selfAndSames);
        return selfAndSames;
    }

    protected Set getSames(IV iv) {
        Set sames = new TreeSet();
        sames.add(iv);
        getSames(iv, sames);
        sames.remove(iv);
        return sames;
    }

    protected void getSames(IV id, Set sames) {
        IChunkedOrderedIterator it = db.getAccessPath(id, sameAs, null).iterator();
        try {
            while (it.hasNext()) {
                IV same = it.next().o();
                if (!sames.contains(same)) {
                    sames.add(same);
                    getSames(same, sames);
                }
            }
        } finally {
            it.close();
        }
        it = db.getAccessPath(null, sameAs, id).iterator();
        try {
            while (it.hasNext()) {
                IV same = it.next().s();
                if (!sames.contains(same)) {
                    sames.add(same);
                    getSames(same, sames);
                }
            }
        } finally {
            it.close();
        }
    }
    
    protected TempTripleStore createTempTripleStore() {
        // log.info("creating temp triple store for owl:sameAs backchainer");
        // System.err.println("creating temp triple store for owl:sameAs backchainer");
        final Properties props = db.getProperties();
        // do not store terms
        props.setProperty(AbstractTripleStore.Options.LEXICON, "false");
        // only store the SPO index
        props.setProperty(AbstractTripleStore.Options.ONE_ACCESS_PATH, "true");
        // @todo MikeP : test w/ SPO bloom filter enabled and see if this improves performance.
        props.setProperty(AbstractTripleStore.Options.BLOOM_FILTER, "false");
        return new TempTripleStore(db.getIndexManager().getTempStore(), props, db);
    }

    protected void dumpSPO(ISPO spo) {
//        System.err.println(spo.toString(db));
    }
    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy