⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 transitiveengine.java

📁 Jena推理机
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/******************************************************************
 * File:        TransitiveEngine.java
 * Created by:  Dave Reynolds
 * Created on:  23-Jun-2003
 * 
 * (c) Copyright 2003, 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP
 * [See end of file]
 * $Id: TransitiveEngine.java,v 1.11 2007/01/02 11:49:43 andy_seaborne Exp $
 *****************************************************************/
package com.hp.hpl.jena.reasoner.transitiveReasoner;

import com.hp.hpl.jena.graph.*;
import com.hp.hpl.jena.reasoner.*;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import com.hp.hpl.jena.vocabulary.RDFS;

import java.util.*;

/**
 * Uses two transitive graph caches to store a subclass and a subproperty
 * lattice and use them within a larger inference graph.
 * 
 * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
 * @version $Revision: 1.11 $ on $Date: 2007/01/02 11:49:43 $
 */
public class TransitiveEngine {
    
    /** The precomputed cache of the subClass graph */
    protected TransitiveGraphCache subClassCache;
    
    /** The precomputed cache of the subProperty graph */
    protected TransitiveGraphCache subPropertyCache;
    
    /** The base data set from which the caches can be rebuilt */
    protected Finder data;
    
    /** True if the internal data structures have been initialized */
    protected boolean isPrepared = false;
    
    /** The set of predicates which are aliases for subClassOf */
    protected static HashSet subClassAliases;
    
    /** The set of predicates which are aliases for subPropertyOf */
    protected static HashSet subPropertyAliases;
    
    /** Classification flag: not relevant to this engine */
    private static final int NOT_RELEVANT = 1;
    
    /** Classification flag: simple or indirect subClass */
    private static final int SUBCLASS = 2;
    
    /** Classification flag: simple subProperty */
    private static final int SUBPROPERTY = 4;
    
    /** Mask for the lattice update cases */
//    private static final int UPDATE_MASK = SUBCLASS | SUBPROPERTY;
    private static final int UPDATE_MASK = SUBCLASS | SUBPROPERTY | NOT_RELEVANT;
    
    /** Classification flag: subProperty of subClass */
    private static final int REBUILD_SUBCLASS = 8;
    
    /** Classification flag: subProperty of subProperty */
    private static final int REBUILD_SUBPROPERTY = 16;
    
    /** The direct (minimal) version of the subPropertyOf property */
    public static Node directSubPropertyOf;
    
    /** The direct (minimal) version of the subClassOf property */
    public static Node directSubClassOf;
    
    /** The normal subPropertyOf property */
    public static Node subPropertyOf;
    
    /** The normal subClassOf property */
    public static Node subClassOf;
    
    // Static initializer
    static {
        directSubPropertyOf = TransitiveReasoner.directSubPropertyOf;
        directSubClassOf    = TransitiveReasoner.directSubClassOf;
        subPropertyOf = RDFS.subPropertyOf.asNode();
        subClassOf = RDFS.subClassOf.asNode();
    }
   
    /**
     * Constructor.
     * @param subClassCache pre-initialized subclass TGC
     * @param subPropertyCache pre-initialized subproperty TGC
     */
    public TransitiveEngine(TransitiveGraphCache subClassCache,
                             TransitiveGraphCache subPropertyCache) {
         this.subClassCache = subClassCache;
         this.subPropertyCache = subPropertyCache;
    }
   
    /**
     * Constructor.
     * @param tengine an instance of TransitiveEngine to be cloned
     */
    public TransitiveEngine(TransitiveEngine tengine) {
         this.subClassCache = tengine.getSubClassCache().deepCopy();
         this.subPropertyCache = tengine.getSubPropertyCache().deepCopy();
    }
    
    /**
     * Prepare the engine by inserting any new data not already included
     * in the existing caches.
     * @param baseData the base dataset on which the initial caches were based, could be null
     * @param newData a dataset to be added to the engine, not known to be already
     *                 included in the caches from construction time
     * @return a concatenation of the inserted data and the original data
     */
    public Finder insert(Finder baseData, FGraph newData) {
        Graph newDataG = newData.getGraph();
        if (baseData != null) {
            data = FinderUtil.cascade(baseData, newData);
        } else {
            data = newData;
        }
        if ((TransitiveEngine.checkOccuranceUtility(subPropertyOf, newDataG, subPropertyCache) ||
               TransitiveEngine.checkOccuranceUtility(subClassOf, newDataG, subPropertyCache))) {
             subClassCache = new TransitiveGraphCache(directSubClassOf, subClassOf);
             subPropertyCache = new TransitiveGraphCache(directSubPropertyOf, subPropertyOf);
             TransitiveEngine.cacheSubPropUtility(data, subPropertyCache);
             TransitiveEngine.cacheSubClassUtility(data, subPropertyCache, subClassCache);
         }        
         return data;    
    }

    /**
     * Return the cache of the subclass lattice.
     */
    public TransitiveGraphCache getSubClassCache() {
        return subClassCache;
    }

    /**
     * Return the cache of the subclass lattice.
     */
    public TransitiveGraphCache getSubPropertyCache() {
        return subPropertyCache;
    }
    
    /**
     * Set the closure caching flags.
     * @param cacheSP true if caching of subPropertyOf closure is wanted
     * @param cacheSC true if caching of subClassOf closure is wanted
     */
    public void setCaching(boolean cacheSP, boolean cacheSC) {
        subPropertyCache.setCaching(cacheSP);
        subClassCache.setCaching(cacheSC);
    }
    
    /**
     * Build alias tables for subclass and subproperty.
     */
    private void prepare() {
        if (isPrepared) return;
        subClassAliases = new HashSet();
        subClassAliases.add(subClassOf);
        subClassAliases.add(directSubClassOf);
        
        subPropertyAliases = new HashSet();
        subPropertyAliases.add(subPropertyOf);
        subPropertyAliases.add(directSubPropertyOf);
        
        Iterator subProps = subPropertyCache.find(new TriplePattern(null, subPropertyOf, subPropertyOf));
        while (subProps.hasNext()) {
            Triple spT = (Triple) subProps.next();
            Node spAlias = spT.getSubject();
            subPropertyAliases.add(spAlias);
            Iterator subClasses = subPropertyCache.find(new TriplePattern(null, spAlias, subClassOf));
            while (subClasses.hasNext()) {
                subClassAliases.add(((Triple)subClasses.next()).getObject());
            }
        }
        isPrepared = true;
    }
    
    /**
     * Classify an incoming triple to detect whether it is relevant to this engine.
     * @param t the triple being added
     * @return a classification flag, as specified in the above private properties
     */
    private int triage(Triple t) {
        if (!isPrepared) prepare();
        Node predicate = t.getPredicate();
        if (subClassAliases.contains(predicate)) {
            return SUBCLASS;
        } else if (subPropertyAliases.contains(predicate)) {
            Node target = t.getObject();
            if (subClassAliases.contains(target)) {
                return REBUILD_SUBCLASS | SUBPROPERTY;
            } else if (subPropertyAliases.contains(target)) {
                return REBUILD_SUBCLASS | REBUILD_SUBPROPERTY;
            } else {
                return SUBPROPERTY;
            }
        } else {
            return NOT_RELEVANT;
        }
        
    }
    
    /**
     * Return a Finder instance appropriate for the given query.
     */
    public Finder getFinder(TriplePattern pattern, Finder continuation) {
        if (!isPrepared) prepare();
        Node predicate = pattern.getPredicate();
        if (predicate.isVariable()) {
            // Want everything in the cache, the tbox and the continuation
            return FinderUtil.cascade(subPropertyCache, subClassCache, continuation);
        } else if (subPropertyAliases.contains(predicate)) {
            return subPropertyCache;
        } else if (subClassAliases.contains(predicate)) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -