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

📄 digadapter.java

📁 jena2.5.4推理机系统的一种最基本实现 HP实验室出品
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/*****************************************************************************
 * Source code information
 * -----------------------
 * Original author    Ian Dickinson, HP Labs Bristol
 * Author email       ian.dickinson@hp.com
 * Package            Jena 2
 * Web                http://sourceforge.net/projects/jena/
 * Created            11-Sep-2003
 * Filename           $RCSfile: DIGAdapter.java,v $
 * Revision           $Revision: 1.25 $
 * Release status     $State: Exp $
 *
 * Last modified on   $Date: 2007/01/02 11:49:27 $
 *               by   $Author: andy_seaborne $
 *
 * (c) Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP
 * [See end of file]
 *****************************************************************************/

// Package
///////////////
package com.hp.hpl.jena.reasoner.dig;



// Imports
///////////////
import java.util.*;

import com.hp.hpl.jena.datatypes.*;
import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
import com.hp.hpl.jena.graph.Graph;
import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.reasoner.TriplePattern;
import com.hp.hpl.jena.util.iterator.*;
import com.hp.hpl.jena.util.xml.SimpleXMLPath;
import com.hp.hpl.jena.vocabulary.*;

import org.apache.commons.logging.LogFactory;
import org.w3c.dom.*;


/**
 * <p>
 * An adapter class that mediates between a Jena InfGraph and a DIG reasoner process.
 * </p>
 *
 * @author Ian Dickinson, HP Labs
 *         (<a  href="mailto:Ian.Dickinson@hp.com" >email</a>)
 * @version CVS $Id: DIGAdapter.java,v 1.25 2007/01/02 11:49:27 andy_seaborne Exp $
 */
public class DIGAdapter
{
    // Constants
    //////////////////////////////////

    /** DIG profile for 1.7 */
    public static final DIGProfile RACER_17_PROFILE = new DIGProfile() {
        public String getDIGNamespace()          {return "http://dl.kr.org/dig/lang"; }
        public String getSchemaLocation()        {return "http://potato.cs.man.ac.uk/dig/level0/dig.xsd"; }
        public String getContentType()           {return "application/x-www-form-urlencoded";}
        public String getInconsistentKBMessage() {return null;}
    };

    /** DIG profile for Pellet */
    public static final DIGProfile PELLET_PROFILE = new DIGProfile() {
        public String getDIGNamespace()          {return "http://dl.kr.org/dig/lang"; }
        public String getSchemaLocation()        {return "http://potato.cs.man.ac.uk/dig/level0/dig.xsd"; }
        public String getContentType()           {return "application/x-www-form-urlencoded";}
        public String getInconsistentKBMessage() {return "Inconsistent KB";}
    };

    // switch codes for expression types
    private static final int UNION = 1;
    private static final int INTERSECTION = 2;
    private static final int COMPLEMENT = 3;
    private static final int ENUMERATED = 4;
    private static final int RESTRICTION = 5;

    /** Mark a bNode identifier */
    public static final String ANON_MARKER = "anon:";

    /** Well known concept URI's */
    public static final List KNOWN_CONCEPTS = Arrays.asList( new Object[] {OWL.Thing.getURI(), OWL.Nothing.getURI(),
                                                                           DAML_OIL.Thing.getURI(), DAML_OIL.Thing.getURI() } );

    /** Well known integer type URI's, these we will translate into DIG integer attributes */
    public static final List XSD_INT_TYPES = Arrays.asList( new Object[] {
            XSDDatatype.XSDint.getURI(),
            XSDDatatype.XSDinteger.getURI(),
            XSDDatatype.XSDnonNegativeInteger.getURI(),
            XSDDatatype.XSDbyte.getURI(),
            XSDDatatype.XSDshort.getURI(),
            XSDDatatype.XSDlong.getURI(),
            XSDDatatype.XSDunsignedByte.getURI(),
            XSDDatatype.XSDunsignedLong.getURI(),
            XSDDatatype.XSDunsignedInt.getURI(),
            XSDDatatype.XSDunsignedShort.getURI(),
    } );


    // Static variables
    //////////////////////////////////

    /** Query ID counter */
    private static int s_queryID = 0;

    /** The table that represents the query translations we know about */
    protected static DIGQueryTranslator[] s_queryTable = {
        // subsumes when testing for subsumption between two known class expressions
        new DIGQuerySubsumesTranslator( RDFS.subClassOf.getURI() ),
        new DIGQuerySubsumesTranslator( DAML_OIL.subClassOf.getURI() ),

        // testing for disjoint between two known class expressions
        new DIGQueryDisjointTranslator( OWL.disjointWith.getURI() ),
        new DIGQueryDisjointTranslator( DAML_OIL.disjointWith.getURI() ),

        // ancestors and parents when testing for a named and variable node
        new DIGQueryAncestorsTranslator( RDFS.subClassOf.getURI(), true ),
        new DIGQueryAncestorsTranslator( RDFS.subClassOf.getURI(), false ),
        new DIGQueryAncestorsTranslator( DAML_OIL.subClassOf.getURI(), true ),
        new DIGQueryAncestorsTranslator( DAML_OIL.subClassOf.getURI(), false ),

        // all parents and all children
        new DIGQueryParentsTranslator( ReasonerVocabulary.directSubClassOf.getURI(), true ),
        new DIGQueryParentsTranslator( ReasonerVocabulary.directSubClassOf.getURI(), false ),

        // specific named parent or child
        new DIGQueryParentsTranslator( null, ReasonerVocabulary.directSubClassOf.getURI(), null, true ),
        new DIGQueryParentsTranslator( null, ReasonerVocabulary.directSubClassOf.getURI(), null, false ),

        // the entire class hierarchy
        new DIGQueryClassHierarchyTranslator( RDFS.subClassOf.getURI() ),
        new DIGQueryClassHierarchyTranslator( DAML_OIL.subClassOf.getURI() ),

        // equivalent classes
        new DIGQueryEquivalentsTranslator( OWL.equivalentClass.getURI(), true ),
        new DIGQueryEquivalentsTranslator( OWL.equivalentClass.getURI(), false ),
        new DIGQueryEquivalentsTranslator( DAML_OIL.sameClassAs.getURI(), true ),
        new DIGQueryEquivalentsTranslator( DAML_OIL.sameClassAs.getURI(), false ),

        new DIGQueryIsEquivalentTranslator( OWL.equivalentClass.getURI() ),
        new DIGQueryIsEquivalentTranslator( DAML_OIL.sameClassAs.getURI() ),

        // rancestors and rparents when testing for a named and variable node
        new DIGQueryRoleAncestorsTranslator( RDFS.subPropertyOf.getURI(), true ),
        new DIGQueryRoleAncestorsTranslator( RDFS.subPropertyOf.getURI(), false ),
        new DIGQueryRoleAncestorsTranslator( DAML_OIL.subPropertyOf.getURI(), true ),
        new DIGQueryRoleAncestorsTranslator( DAML_OIL.subPropertyOf.getURI(), false ),

        new DIGQueryRoleParentsTranslator( ReasonerVocabulary.directSubPropertyOf.getURI(), true ),
        new DIGQueryRoleParentsTranslator( ReasonerVocabulary.directSubPropertyOf.getURI(), false ),

        // the entire role hierarchy
        new DIGQueryRoleHierarchyTranslator( RDFS.subPropertyOf.getURI() ),
        new DIGQueryRoleHierarchyTranslator( DAML_OIL.subPropertyOf.getURI() ),

        // all concepts query for [* rdf:type :Class]
        new DIGQueryAllConceptsTranslator( RDF.type.getURI(), RDFS.Class.getURI() ),
        new DIGQueryAllConceptsTranslator( RDF.type.getURI(), OWL.Class.getURI() ),
        new DIGQueryAllConceptsTranslator( RDF.type.getURI(), DAML_OIL.Class.getURI() ),

        // instances
        new DIGQueryInstancesTranslator( RDF.type.getURI() ),
        new DIGQueryInstancesTranslator( DAML_OIL.type.getURI() ),
        new DIGQueryTypesTranslator( RDF.type.getURI() ),
        new DIGQueryTypesTranslator( DAML_OIL.type.getURI() ),
        new DIGQueryInstanceTranslator( RDF.type.getURI() ),
        new DIGQueryInstanceTranslator( DAML_OIL.type.getURI() ),
        new DIGQueryDifferentFromTranslator( OWL.differentFrom.getURI() ),
        new DIGQueryDifferentFromTranslator( DAML_OIL.differentIndividualFrom.getURI() ),
        new DIGQueryRoleFillersTranslator(),
        new DIGQueryRoleFillerTranslator(),
        new DIGQueryRelatedIndividualsTranslator(),

        // specific type tests
        new DIGQueryIsConceptTranslator(),
        new DIGQueryIsRoleTranslator(),
        new DIGQueryIsIndividualTranslator(),
    };


    // Instance variables
    //////////////////////////////////

    /** The profile for the DIG interface this reasoner is interacting with. Pellet */
    protected DIGProfile m_profile = PELLET_PROFILE;

    /** The graph that contains the data we are uploading to the external DIG reasoner */
    protected OntModel m_sourceData;

    /** Counter for generating skolem names */
    private int m_skolemCounter = 0;

    /** The connection to the DIG reasoner */
    private DIGConnection m_connection;

    /** The set of known individual names from the DIG reasoner */
    protected Set m_indNames = new HashSet();

    /** Flag that is set to true once we have asked the remote reasoner for its list of individual names */
    protected boolean m_indNamesAsked = false;

    /** The set of known concept names from the DIG reasoner */
    protected Set m_conceptNames = new HashSet();

    /** Flag that is set to true once we have asked the remote reasoner for its list of concept names */
    protected boolean m_conceptNamesAsked = false;

    /** The set of known role names from the DIG reasoner */
    protected Set m_roleNames = new HashSet();

    /** Flag that is set to true once we have asked the remote reasoner for its list of role names */
    protected boolean m_roleNamesAsked = false;

    /** Model containing axiom statements */
    protected Model m_axioms = null;


    // Constructors
    //////////////////////////////////

    /**
     * <p>Construct a DIG adapter for the given source data graph, which is encoding an
     * ontology in a language represented by the given model spec. Allocates a new
     * DIG connection using the default connection URL (<code>http://localhost:8081</code>).</p>
     * @param spec An ont model spec encoding the ontology language of the source graph
     * @param source The graph that contains the source data on which the DIG reasoner
     * will operate
     */
    public DIGAdapter( OntModelSpec spec, Graph source ) {
        this( spec, source, DIGConnectionPool.getInstance().allocate(), null );
    }


    /**
     * <p>Construct a DIG adapter for the given source data graph, which is encoding an
     * ontology in a language represented by the given model spec.</p>
     * @param spec An ont model spec encoding the ontology language of the source graph
     * @param source The graph that contains the source data on which the DIG reasoner
     * will operate
     * @param connection A pre-configured DIG connection to use to communicate with the
     * external reasoner
     * @param axioms A model containing axioms appropriate to the ontology language
     * this adapter is processing. May be null.
     */
    public DIGAdapter( OntModelSpec spec, Graph source, DIGConnection connection, Model axioms ) {
        m_connection = connection;
        m_axioms = axioms;

        // we wrap the given graph in a suitable ontology model
        m_sourceData = ModelFactory.createOntologyModel( spec, ModelFactory.createModelForGraph( source ) );

        // don't do the .as() checking, since we know we're not using the reasoner
        m_sourceData.setStrictMode( false );
    }


    // External signature methods
    //////////////////////////////////

    /**
     * <p>Answer the DIG profile for the DIG interface this reasoner is attached to.</p>
     * @return A profile detailing the parameters of the DIG variant this reasoner is interacting with.
     */
    public DIGProfile getProfile() {
        return m_profile;
    }


    /**
     * <p>Set the profile specifying the variable parts of the DIG profile that are being
     * used in this instance.</p>
     * @param profile The new DIG profile
     */
    public void setProfile( DIGProfile profile ) {
        m_profile = profile;
    }


    /**
     * <p>Answer the ontology language profile we're assuming in this reasoner.</p>
     * @return The ontology language via the language profile
     */
    public Profile getOntLanguage() {
        return m_sourceData.getProfile();
    }


    /**
     * <p>Answer the DIG identification structure we obtain by querying the attached reasoner.</p>

⌨️ 快捷键说明

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