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

📄 baseinfgraph.java

📁 Jena推理机
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/******************************************************************
 * File:        BaseInfGraph.java
 * Created by:  Dave Reynolds
 * Created on:  18-Jan-03
 *
 * (c) Copyright 2003, 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP
 * [See end of file]
 * $Id: BaseInfGraph.java,v 1.46 2007/01/12 10:42:34 chris-dollin Exp $
 *****************************************************************/
package com.hp.hpl.jena.reasoner;

import com.hp.hpl.jena.graph.*;
import com.hp.hpl.jena.graph.compose.Union;
import com.hp.hpl.jena.graph.impl.*;
import com.hp.hpl.jena.shared.*;
import com.hp.hpl.jena.util.iterator.*;
import java.util.Iterator;

/**
 * A base level implementation of the InfGraph interface.
 *
 * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
 * @version $Revision: 1.46 $ on $Date: 2007/01/12 10:42:34 $
 */
public abstract class BaseInfGraph extends GraphBase implements InfGraph {

    /** The Reasoner instance which performs all inferences and Tbox lookups */
    protected Reasoner reasoner;

    /** The graph of raw data which is being reasoned over */
    protected FGraph fdata;

    /** Flag, if set to true then derivations are recorded */
    protected boolean recordDerivations;

    /** Flag to record if the preparation call has been made and so the graph is ready for queries */
    protected boolean isPrepared = false;

    /** version count */
    protected volatile int version = 0;
    
    /**
         Inference graphs share the prefix-mapping of their underlying raw graph.
     	@see com.hp.hpl.jena.graph.Graph#getPrefixMapping()
    */
    public PrefixMapping getPrefixMapping()
        { return getRawGraph().getPrefixMapping(); }

    /**
        Inference graphs share the reifiers of their underlying raw graphs. This may
        be too simplistic - they won't see quads flying past.
        TODO write a test case that reveals this.
     	@see com.hp.hpl.jena.graph.Graph#getReifier()
    */    
    public Reifier constructReifier()
        {  return getRawGraph().getReifier(); }

    /**
     * Constructor
     * @param data the raw data file to be augmented with entailments
     * @param reasoner the engine, with associated tbox data, whose find interface
     * can be used to extract all entailments from the data.
     */
    public BaseInfGraph(Graph data, Reasoner reasoner) {
       this( data, reasoner, ReificationStyle.Minimal ); // should pick style from data? TODO
    }

    public BaseInfGraph( Graph data, Reasoner reasoner, ReificationStyle style )
        {
        super( style );
        this.fdata = new FGraph( data );
        this.reasoner = reasoner;
        }

    /**
        Answer the InfCapabilities of this InfGraph.
     */
    public Capabilities getCapabilities() {
        if (capabilities == null) {
            return getReasoner().getGraphCapabilities();
        } else {
            return capabilities;
        }
    }

    /**
        An InfCapabilities notes that size may not be accurate, and some
        triples may be irremovable.

        TODO accomodate the properties of the base graph, too.

        @author hedgehog
    */
    public static class InfCapabilities extends AllCapabilities
        {
        public boolean sizeAccurate() { return false; }
        public boolean deleteAllowed( boolean every ) { return !every; }
        public boolean iteratorRemoveAllowed() { return false; }
        public boolean findContractSafe() { return false; }
        }

    /**
        An InfCapabilities notes that size may not be accurate, and some
        triples may be irremovable.

        TODO accomodate the properties of the base graph, too.

        @author hedgehog
    */
    public static class InfFindSafeCapabilities extends InfCapabilities
        {
        public boolean findContractSafe() { return true; }
        }

    public BulkUpdateHandler getBulkUpdateHandler()
        {
        if (bulkHandler == null) bulkHandler = new InfBulkUpdateHandler( this );
        return bulkHandler;
        }

    /**
        InfBulkUpdateHandler - a bulk update handler specialised for inference
        graphs by code for <code>removeAll()</code>.

        @author kers
    */
    static class InfBulkUpdateHandler extends SimpleBulkUpdateHandler
    	{
        public InfBulkUpdateHandler( BaseInfGraph  graph )
            { super(graph); }

        public void remove( Node s, Node p, Node o )
            {
            BaseInfGraph g = (BaseInfGraph) graph;
            g.getRawGraph().getBulkUpdateHandler().remove( s, p, o );
            g.discardState();
            g.rebind();
            manager.notifyEvent( graph, GraphEvents.remove( s, p, o ) );
            }

        public void removeAll()
            {
            BaseInfGraph g = (BaseInfGraph) graph;
            g.getRawGraph().getBulkUpdateHandler().removeAll();
            g.discardState();
            g.rebind();
            g.getEventManager().notifyEvent( g, GraphEvents.removeAll );
            }
    	}

    public TransactionHandler getTransactionHandler()
        { return new InfTransactionHandler( this ); }

    public static class InfTransactionHandler
        extends TransactionHandlerBase implements TransactionHandler
        {
        protected final BaseInfGraph base;

        public InfTransactionHandler( BaseInfGraph base )
            { this.base = base; }

        public boolean transactionsSupported()
            { return getBaseHandler().transactionsSupported(); }

        protected TransactionHandler getBaseHandler()
            { return base.getRawGraph().getTransactionHandler(); }

        public void begin()
            { getBaseHandler().begin(); }

        public void abort()
            { getBaseHandler().abort();
            base.rebind(); }

        public void commit()
            { getBaseHandler().commit(); }
        }

    /**
     	discard any state that depends on the content of fdata, because
     	it's just been majorly trashed, solid gone.
    */
    protected void discardState()
        {}

    /**
     * Return the raw RDF data Graph being processed (i.e. the argument
     * to the Reasonder.bind call that created this InfGraph).
     */
    public Graph getRawGraph() {
        return fdata.getGraph();
    }

    /**
     * Return the Reasoner which is being used to answer queries to this graph.
     */
    public Reasoner getReasoner() {
        return reasoner;
    }

    /**
     * Replace the underlying data graph for this inference graph and start any
     * inferences over again. This is primarily using in setting up ontology imports
     * processing to allow an imports multiunion graph to be inserted between the
     * inference graph and the raw data, before processing.
     * @param data the new raw data graph
     */
    public void rebind(Graph data) {
        fdata = new FGraph(data);
        isPrepared = false;
    }

    /**
     * Cause the inference graph to reconsult the underlying graph to take
     * into account changes. Normally changes are made through the InfGraph's add and
     * remove calls are will be handled appropriately. However, in some cases changes
     * are made "behind the InfGraph's back" and this forces a full reconsult of
     * the changed data.
     */
    public void rebind() {
        version++;
        isPrepared = false;
    }

    /**
     * Reset any internal caches. Some systems, such as the tabled backchainer,
     * retain information after each query. A reset will wipe this information preventing
     * unbounded memory use at the expense of more expensive future queries. A reset
     * does not cause the raw data to be reconsulted and so is less expensive than a rebind.
     */
    public void reset() {
        version++;
    }

    /**
     * Perform any initial processing and caching. This call is optional. Most
     * engines either have negligable set up work or will perform an implicit
     * "prepare" if necessary. The call is provided for those occasions where
     * substantial preparation work is possible (e.g. running a forward chaining
     * rule system) and where an application might wish greater control over when
     * this prepration is done.
     */
    public void prepare() {
        // Default is to do no preparation
        isPrepared = true;
    }

    /**
     * Returns a derivations graph. The rule reasoners typically create a
     * graph containing those triples added to the base graph due to rule firings.

⌨️ 快捷键说明

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