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

📄 digadapter.java

📁 jena2.5.4推理机系统的一种最基本实现 HP实验室出品
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
     */
    public boolean isRole( com.hp.hpl.jena.graph.Node node, Model premises ) {
        return node.isConcrete() &&
               (getKnownRoles().contains( getNodeID( node ) ) ||
                ((premises != null) &&
                isPremisesRole( node, premises )) );
    }


    /**
     * <p>Answer true if the given node corresponds to one of the concepts known to
     * the DIG reasoner.</p>
     * @param node A node to test
     * @param premises A model defining premises that may encode more information about
     * node, or may be null
     * @return True if <code>node</code> is a known concept
     */
    public boolean isConcept( com.hp.hpl.jena.graph.Node node, Model premises ) {
        return node.isConcrete() && !node.isLiteral() &&
               (getKnownConcepts().contains( getNodeID( node ) ) ||
                ((premises != null) && isPremisesClass( node, premises )) ||
                KNOWN_CONCEPTS.contains( getNodeID( node ) ));
    }


    /**
     * <p>Answer the ontology language specification for the source model underlying
     * this DIG adapter.</p>
     * @return The ontology model spec
     */
    public OntModelSpec getSourceSpecification() {
        return m_sourceData.getSpecification();
    }


    /**
     * <p>Create a new element to represent a query, adding to it a unique query
     * ID.</p>
     * @param query The query document
     * @param elemName The string name of the query element
     * @return The new query element
     */
    public Element createQueryElement( Document query, String elemName ) {
        Element qElem = addElement( query.getDocumentElement(), elemName );
        qElem.setAttribute( DIGProfile.ID, "q" + s_queryID++ );
        return qElem;
    }


    // Internal implementation methods
    //////////////////////////////////


    /**
     * <p>In Dig, defXXX elements are required to introduce all named entities,
     * such as concepts and roles.  This method collects such definitions and
     * adds the defXXX elements as children of the tell element.</p>
     * @param tell The XML element, typically &lt;tells&gt;, to which to attach the
     * declarations
     */
    protected void addNamedEntities( Element tell ) {
        // first we collect the named entities
        HashSet roles = new HashSet();
        HashSet attributes = new HashSet();
        HashSet concepts = new HashSet();
        HashSet individuals = new HashSet();

        addAll( m_sourceData.listClasses(), concepts );
        addAll( m_sourceData.listDatatypeProperties(), attributes );
        addAll( m_sourceData.listIndividuals(), individuals );

        collectRoleProperties( roles );

        // collect the DIG definitions at the beginning of the document
        addNamedDefs( tell, concepts.iterator(), DIGProfile.DEFCONCEPT, m_conceptNames );
        addNamedDefs( tell, roles.iterator(), DIGProfile.DEFROLE, m_roleNames );
        addNamedDefs( tell, attributes.iterator(), DIGProfile.DEFATTRIBUTE, null);
        addNamedDefs( tell, individuals.iterator(), DIGProfile.DEFINDIVIDUAL, m_indNames );
    }


    /** Add all object properties (roles) to the given collection */
    protected void collectRoleProperties( Collection roles ) {
        addAll( m_sourceData.listObjectProperties(), roles );
        addAll( m_sourceData.listInverseFunctionalProperties(), roles );
        addAll( m_sourceData.listTransitiveProperties(), roles );

        // not present in DAML
        if (m_sourceData.getProfile().SYMMETRIC_PROPERTY() != null) {
            addAll( m_sourceData.listSymmetricProperties(), roles );
        }
    }


    /**
     * <p>Add the named definitions from the given iterator to the tell document we are building.</p>
     * @param tell The document being built
     * @param i An iterator over resources
     * @param defType The type of DIG element we want to build
     * @param nameCollection Optional set of names of this type of entity to collect
     */
    protected void addNamedDefs( Element tell, Iterator i, String defType, Set nameCollection ) {
        while (i.hasNext()) {
            RDFNode n = (Resource) i.next();
            if (n instanceof Resource) {
                String id = getNodeID( n.asNode() );
                addNamedElement( tell, defType, getNodeID( n.asNode() ) );

                // a named concept, role, etc is being defined
                if (nameCollection != null) {
                    nameCollection.add( id );
                }
            }
        }
    }


    /**
     * <p>Answer a element with the given element name,
     * and with a attribute 'name' with the given uri as name.<p>
     * @param parent The parent node to add to
     * @param elemName The element name, eg defconcept
     * @param uri The URI of the definition
     * @return A named element
     */
    protected Element addNamedElement( Element parent, String elemName, String uri ) {
        Element elem = addElement( parent, elemName );
        elem.setAttribute( DIGProfile.NAME, uri );

        return elem;
    }


    /** Add to the given element a child element with the given name */
    protected Element addElement( Element parent, String childName ) {
        Element child = parent.getOwnerDocument().createElement( childName );
        return (Element) parent.appendChild( child );
    }


    /** Add iterator contents to collection */
    private void addAll( Iterator i, Collection c ) {
        for (; i.hasNext(); c.add( i.next() ) );
    }


    /**
     * <p>Translate all of the classes in the current KB into descriptions
     * using the DIG concept language, and attach the axioms generated
     * to the given element.</p>
     * @param tell The XML element, typically &lt;tells&gt;, to which
     * to attach the generated translations.
     */
    protected void translateClasses( Element tell ) {
        translateSubClassAxioms( tell );
        translateClassEquivalences( tell );
        translateClassDisjointAxioms( tell );

        translateRestrictions( tell );

        // now the implicit equivalences
        translateClassExpressions( tell, getOntLanguage().INTERSECTION_OF(), INTERSECTION );
        translateClassExpressions( tell, getOntLanguage().UNION_OF(), UNION );
        translateClassExpressions( tell, getOntLanguage().COMPLEMENT_OF(), COMPLEMENT );
        translateClassExpressions( tell, getOntLanguage().ONE_OF(), ENUMERATED );
    }

    /**
     * <p>Translate the sub-class axioms in the source model into DIG
     * impliesc axioms</p>
     * @param tell The node representing the DIG tell verb
     */
    protected void translateSubClassAxioms( Element tell ) {
        StmtIterator i = m_sourceData.listStatements( null, getOntLanguage().SUB_CLASS_OF(), (RDFNode) null );
        while (i.hasNext()) {
            Statement sc = i.nextStatement();
            Element impliesc = addElement( tell, DIGProfile.IMPLIESC );
            addClassDescription( impliesc, sc.getSubject(), m_sourceData );
            addClassDescription( impliesc, sc.getResource(), m_sourceData );
        }
    }


    /**
     * <p>Translate the class equivalence axioms in the source model into DIG
     * equalsc axioms.</p>
     * @param tell The node representing the DIG tell verb
     */
    protected void translateClassEquivalences( Element tell ) {
        // first we do stated equivalences
        StmtIterator i = m_sourceData.listStatements( null, getOntLanguage().EQUIVALENT_CLASS(), (RDFNode) null );
        while (i.hasNext()) {
            Statement sc = i.nextStatement();
            Element equalc = addElement( tell, DIGProfile.EQUALC );
            addClassDescription( equalc, sc.getSubject(), m_sourceData );
            addClassDescription( equalc, sc.getResource(), m_sourceData );
        }
    }


    /**
     * <p>Translate class expressions, such as union classes, intersection classes, etc, into the DIG.</p>
     * concept language. The translations are attached to the given tell node.</p>
     * @param tell The node representing the DIG tell verb
     * @param p A property that will require an implicit equivalence to be made explicit
     * in a correct translation to DIG
     * @param classExprType Denotes the type of class expression we are translating
     */
    protected void translateClassExpressions( Element tell, Property p, int classExprType ) {
        translateClassExpressions( tell, m_sourceData.listStatements( null, p, (RDFNode) null ), classExprType, m_sourceData );
    }


    /**
     * <p>Translate the restrictions in the source model into the DIG concept language.</p>
     * @param tell The node representing the DIG tell verb
     */
    protected void translateRestrictions( Element tell ) {
        translateClassExpressions( tell,
                                   m_sourceData.listStatements( null, RDF.type, getOntLanguage().RESTRICTION() ),
                                   RESTRICTION, m_sourceData );
    }


    /**
     * <p>A named owl:class with a class-construction axiom directly attached is implicitly
     * an equivalence axiom with the anonymous class that has the given class construction.</p>
     * @param tell The node representing the DIG tell verb
     * @param i A statement iterator whose subjects denote the class expressions to be translated
     * @param classExprType Denotes the type of class expression we are translating
     */
    protected void translateClassExpressions( Element tell, StmtIterator i, int classExprType, Model source ) {
        while (i.hasNext()) {
            OntClass cls = (OntClass) i.nextStatement().getSubject().as( OntClass.class );

            Element equalc = addElement( tell, DIGProfile.EQUALC );
            addClassDescription( equalc, cls, source );

            switch (classExprType) {
                case UNION:          translateUnionClass( equalc, cls, source );        break;
                case INTERSECTION:   translateIntersectionClass( equalc, cls, source ); break;
                case COMPLEMENT:     translateComplementClass( equalc, cls, source );   break;
                case ENUMERATED:     translateEnumeratedClass( equalc, cls, source );   break;
                case RESTRICTION:    translateRestrictionClass( equalc, cls, source );   break;
            }
        }
    }


    /**
     * <p>Translate a node representing a class expression (presumed anonymous, though
     * this is not tested) into the appropriate DIG class axiom.</p>
     * @param parent The XML node that will be the parent of the class description axiom
     * @param classDescr An OntClass representing the class expression to be translated
     */
    protected void translateClassDescription( Element parent, OntClass classDescr, Model source ) {
        if (classDescr.isUnionClass()) {
            translateUnionClass( parent, classDescr, source );
        }
        else if (classDescr.isIntersectionClass()) {
            translateIntersectionClass( parent, classDescr, source );
        }
        else if (classDescr.isComplementClass()) {
            translateComplementClass( parent, classDescr, source );
        }
        else if (classDescr.isEnumeratedClass()) {
            translateEnumeratedClass( parent, classDescr, source );
        }
        else if (classDescr.isRestriction()) {
            translateRestrictionClass( parent, classDescr, source );
        }
    }


    /**
     * <p>Translate any statements from the KB that indicates disjointness between
     * two classes.</p>
     * @param tell The XML element representing the tell verb we will attach the
     * translations to.
     */
    protected void translateClassDisjointAxioms( Element tell ) {
        StmtIterator i = m_sourceData.listStatements( null, getOntLanguage().DISJOINT_WITH(), (RDFNode) null );
        while (i.hasNext()) {
            Statement sc = i.nextStatement();
            Element impliesc = addElement( tell, DIGProfile.DISJOINT );
            addClassDescription( impliesc, sc.getSubject(), m_sourceData );
            addClassDescription( impliesc, sc.getResource(), m_sourceData );
        }
    }


    /**

⌨️ 快捷键说明

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