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

📄 digadapter.java

📁 jena2.5.4推理机系统的一种最基本实现 HP实验室出品
💻 JAVA
📖 第 1 页 / 共 5 页
字号:

        if (role.isTransitiveProperty()) {
            translateUnaryPropertyAxiom( expr, role.getURI(), DIGProfile.TRANSITIVE, DIGProfile.RATOM );
        }
        if (role.isFunctionalProperty()) {
            translateUnaryPropertyAxiom( expr, role.getURI(), DIGProfile.FUNCTIONAL, DIGProfile.RATOM );
        }
        if (role.isInverseFunctionalProperty()) {
            translateInverseFunctionalAxiom( expr, role, DIGProfile.RATOM );
        }
        if (role.isSymmetricProperty()) {
            translateInverseAxiom( expr, role, DIGProfile.RATOM, role );
        }
    }

    /** Translate all of the attribute (datatype properties) in the KB */
    protected void translateAttributes( Element expr ) {
        for (Iterator i = m_sourceData.listDatatypeProperties(); i.hasNext(); ) {
            translateAttribute( expr, (DatatypeProperty) ((Property) i.next()).as( DatatypeProperty.class ), m_sourceData );
        }
    }

    /** Attributes (datatype properties) have fewer axiom choices than roles */
    protected void translateAttribute( Element expr, DatatypeProperty attrib, Model source ) {
        translateBinaryPropertyAxioms( expr, attrib.getURI(), DIGProfile.IMPLIESR, attrib.listSuperProperties(), DIGProfile.ATTRIBUTE);
        translateBinaryPropertyAxioms( expr, attrib.getURI(), DIGProfile.EQUALR, attrib.listEquivalentProperties(), DIGProfile.ATTRIBUTE );
        translateDomainRangeAxioms( expr, attrib.getURI(), DIGProfile.DOMAIN, attrib.listDomain(), DIGProfile.ATTRIBUTE, source );
        translateAttribRangeAxioms( expr, attrib.getURI(), attrib.listRange(), DIGProfile.ATTRIBUTE );

        if (attrib.isFunctionalProperty()) {
            translateUnaryPropertyAxiom( expr, attrib.getURI(), DIGProfile.FUNCTIONAL, DIGProfile.ATTRIBUTE );
        }
    }

    /** Helper method for binary axioms each argument of which is an ratom element */
    protected void translateBinaryPropertyAxioms( Element expr, String propURI, String axiomType, Iterator i, String propType ) {
        while (i.hasNext()) {
            Property prop = (Property) i.next();
            Element binaryAxiom = addElement( expr, axiomType );
            addNamedElement( binaryAxiom, propType, propURI );
            addNamedElement( binaryAxiom, propType, prop.getURI() );
        }
    }

    /** Helper method for unary axioms, the argument of which is an ratom element */
    protected void translateUnaryPropertyAxiom( Element expr, String propURI, String axiomType, String propType ) {
        Element unaryAxiom = addElement( expr, axiomType );
        addNamedElement( unaryAxiom, propType, propURI );
    }

    /** Domain and range are translated as dig domain and range elements */
    protected void translateDomainRangeAxioms( Element expr, String propURI, String axiomType, Iterator i, String propType, Model source ) {
        while (i.hasNext()) {
            Element drAxiom = addElement( expr, axiomType );
            addNamedElement( drAxiom, propType, propURI );
            addClassDescription( drAxiom, (Resource) i.next(), source );
        }
    }

    /** Concrete ranges have special treatment*/
    protected void translateAttribRangeAxioms( Element expr, String propURI, Iterator i, String propType ) {
        while (i.hasNext()) {
            Resource type = (Resource) i.next();
            RDFDatatype dt = TypeMapper.getInstance().getTypeByName( type.getURI() );

            Element drAxiom = addElement( expr, isIntegerType( dt ) ? DIGProfile.RANGEINT : DIGProfile.RANGESTRING );
            addNamedElement( drAxiom, propType, propURI );
        }
    }

    /** Axioms for all of the inverses of a property */
    protected void translateInverseAxioms( Element expr, ObjectProperty p, String propType ) {
        for (Iterator i = p.listInverse(); i.hasNext(); ) {
            translateInverseAxiom(expr, p, propType, (Property) i.next() );
        }
    }

    /** Translate inverseOf as equality between the role and the inverse of the named inverse role */
    protected void translateInverseAxiom( Element expr, Property p, String propType, Property inv ) {
        Element equalr = addElement( expr, DIGProfile.EQUALR );
        addNamedElement( equalr, propType, p.getURI() );
        Element inverse = addElement( equalr, DIGProfile.INVERSE );
        addNamedElement( inverse, propType, inv.getURI() );
    }


    /** To translate an inverse functional property, we must introduce a new skolem constant for the inverse role */
    protected void translateInverseFunctionalAxiom( Element expr, ObjectProperty role, String propType ) {
        // we need a skolem name for the inverse property
        String skolemName = getSkolemName( role.getLocalName() );

        // first we make the skolem role functional
        addNamedElement( expr, DIGProfile.DEFROLE, skolemName );
        Element functional = addElement( expr, DIGProfile.FUNCTIONAL );
        addNamedElement( functional, propType, skolemName );

        // then we make its inverse equal to role
        Element equalr = addElement( expr, DIGProfile.EQUALR );
        addNamedElement( equalr, propType, role.getURI() );
        Element inverse = addElement( equalr, DIGProfile.INVERSE );
        addNamedElement( inverse, propType, skolemName );
    }


    /** Translate all of the AllDifferent axioms in the KB */
    protected void translateAllDifferentAxioms( Element expr ) {
        if (m_sourceData.getProfile().ALL_DIFFERENT() != null) {
            for (Iterator i = m_sourceData.listAllDifferent(); i.hasNext(); ) {
                AllDifferent ad = (AllDifferent) ((Resource) i.next()).as( AllDifferent.class );
                translateAllDifferent( expr, ad.getDistinctMembers() );
            }
        }
    }


    /** Translate a single AllDifferent declaration as a set of pair-wise disjoints */
    protected void translateAllDifferent( Element expr, RDFList diffMembers ) {
        List dm = diffMembers.asJavaList();

        for (int i = 0;  i < dm.size(); i++) {
            Individual ind0 = (Individual) ((Resource) dm.get(i)).as( Individual.class );

            for (int j = i+1; j < dm.size(); j++) {
                Individual ind1 = (Individual) ((Resource) dm.get(j)).as( Individual.class );
                translateDifferentIndividuals( expr, ind0, ind1 );
            }
        }
    }


    /**
     * <p>Answer true if the given RDF datatype represents an integer value</p>
     */
    private boolean isIntegerType( RDFDatatype type ) {
        String typeURI = (type != null) ? type.getURI() : null;
        return  typeURI != null && XSD_INT_TYPES.contains( typeURI );
    }


    /** Answer a skolem constant, using the given name as a root */
    private String getSkolemName( String root ) {
        return "skolem(" + root + "," + m_skolemCounter++ + ")";

    }



    /**
     * <p>Answer an iterator of the individual names known to the DIG reasoner, from the cache if possible.</p>
     * @return An iterator of the known individual names
     */
    protected Set getKnownIndividuals() {
        if (!m_indNamesAsked) {
            m_indNames.addAll( collectNamedTerms( DIGProfile.ALL_INDIVIDUALS,
                                                  new String[] {DIGProfile.INDIVIDUAL_SET, DIGProfile.INDIVIDUAL} ) );
            m_indNamesAsked = true;
        }

        return m_indNames;
    }


    /**
     * <p>Answer an iterator of the concept names known to the DIG reasoner, from the cache if possible.</p>
     * @return An iterator of the known concept names
     */
    protected Set getKnownConcepts() {
        if (!m_conceptNamesAsked) {
            m_conceptNames.addAll( collectNamedTerms( DIGProfile.ALL_CONCEPT_NAMES,
                                                new String[] {DIGProfile.CONCEPT_SET, DIGProfile.SYNONYMS, DIGProfile.CATOM} ) );
            m_conceptNamesAsked = true;
        }

        return m_conceptNames;
    }


    /**
     * <p>Answer an iterator of the role names known to the DIG reasoner, from the cache if possible.</p>
     * @return An iterator of the known role names
     */
    protected Set getKnownRoles() {
        if (!m_roleNamesAsked) {
            m_roleNames.addAll( collectNamedTerms( DIGProfile.ALL_ROLE_NAMES,
                                             new String[] {DIGProfile.ROLE_SET, DIGProfile.SYNONYMS, DIGProfile.RATOM} ) );
            m_roleNamesAsked = true;
        }

        return m_roleNames;
    }


    /**
     * <p>Answer an iterator of named terms known to the DIG reasoner, from the cache if possible.</p>
     * @param queryType The query verb for the ask
     * @param path A list of element names to extract the term names from the returned document
     * @return An iterator of the known names of a particular type
     */
    protected Set collectNamedTerms( String queryType, String[] path ) {
        Set names = new HashSet();

        // query the DIG ks for the currently known individuals
        Document query = getConnection().createDigVerb( DIGProfile.ASKS, getProfile() );
        createQueryElement( query, queryType );
        Document response = getConnection().sendDigVerb( query, getProfile() );

        // build the path to extract the names
        SimpleXMLPath p = new SimpleXMLPath( true );
        for (int j = 0;  j < path.length;  j++) {
            p.appendElementPath( path[j] );
        }
        p.appendAttrPath( DIGProfile.NAME );

        // collect them into a cached set
        addAll( p.getAll( response ), names );

        return names;
    }


    /** Check whether the given node represents a class in the premises */
    private boolean isPremisesClass( com.hp.hpl.jena.graph.Node node, Model premises ) {
        RDFNode rdfNode = premises.getRDFNode( node );
        Profile oProf = getOntLanguage();

        if (rdfNode instanceof Resource) {
            Resource r = (Resource) rdfNode;
            Resource any = null;

            return
                ((oProf.CLASS() != null)            && premises.contains( r, RDF.type, oProf.CLASS())       ) ||
                ((oProf.RESTRICTION() != null)      && premises.contains( r, RDF.type, oProf.RESTRICTION()) ) ||
                ((oProf.SUB_CLASS_OF() != null)     && premises.contains( r, oProf.SUB_CLASS_OF(), any )    ) ||
                ((oProf.SUB_CLASS_OF() != null)     && premises.contains( any, oProf.SUB_CLASS_OF(), r )    ) ||
                ((oProf.UNION_OF() != null)         && premises.contains( r, oProf.SUB_CLASS_OF(), any )    ) ||
                ((oProf.INTERSECTION_OF() != null)  && premises.contains( r, oProf.SUB_CLASS_OF(), any )    ) ||
                ((oProf.COMPLEMENT_OF() != null)    && premises.contains( r, oProf.SUB_CLASS_OF(), any )    ) ||
                ((oProf.DISJOINT_WITH() != null)    && premises.contains( r, oProf.DISJOINT_WITH(), any )   ) ||
                ((oProf.EQUIVALENT_CLASS() != null) && premises.contains( r, oProf.EQUIVALENT_CLASS(), any ));
        }

        // by default it is not a class
        return false;
    }

    /** Check whether the given node represents a class in the premises */
    private boolean isPremisesRole( com.hp.hpl.jena.graph.Node node, Model premises ) {
        RDFNode rdfNode = premises.getRDFNode( node );
        Profile oProf = getOntLanguage();

        if (rdfNode instanceof Resource) {
            Resource r = (Resource) rdfNode;
            Resource any = null;

            return
                ((oProf.PROPERTY() != null)                    && premises.contains( r, RDF.type, oProf.PROPERTY())                   ) ||
                ((oProf.OBJECT_PROPERTY() != null)             && premises.contains( r, RDF.type, oProf.OBJECT_PROPERTY())            ) ||
                ((oProf.DATATYPE_PROPERTY() != null)           && premises.contains( r, RDF.type, oProf.DATATYPE_PROPERTY())          ) ||
                ((oProf.TRANSITIVE_PROPERTY() != null)         && premises.contains( r, RDF.type, oProf.TRANSITIVE_PROPERTY())        ) ||
                ((oProf.FUNCTIONAL_PROPERTY() != null)         && premises.contains( r, RDF.type, oProf.FUNCTIONAL_PROPERTY())        ) ||
                ((oProf.INVERSE_FUNCTIONAL_PROPERTY() != null) && premises.contains( r, RDF.type, oProf.INVERSE_FUNCTIONAL_PROPERTY())) ||
                ((oProf.SYMMETRIC_PROPERTY() != null)          && premises.contains( r, RDF.type, oProf.SYMMETRIC_PROPERTY())         ) ||
                ((oProf.SUB_PROPERTY_OF() != null)             && premises.contains( r, oProf.SUB_PROPERTY_OF(), any )                ) ||
                ((oProf.SUB_PROPERTY_OF() != null)             && premises.contains( any, oProf.SUB_PROPERTY_OF(), r )                ) ||
                ((oProf.INVERSE_OF() != null)                  && premises.contains( r, oProf.INVERSE_OF (), any )                    ) ||
                ((oProf.INVERSE_OF() != null)                  && premises.contains( any, oProf.INVERSE_OF (), r )                    );
        }

        // by default it is not a class
        return false;
    }

    /** Normalise any variables to Node.ANY */
    private com.hp.hpl.jena.graph.Node normaliseNode( com.hp.hpl.jena.graph.Node n ) {
        return n.isConcrete() ? n : com.hp.hpl.jena.graph.Node.ANY;
    }


    //==============================================================================
    // Inner class definitions
    //==============================================================================



    /** Encapsulates the identification information from a DIG reasoner */
    private class DigIdentifierImpl
        implements DIGIdentifier
    {
        private Document m_id;

        private DigIdentifierImpl( 

⌨️ 快捷键说明

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