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

📄 digadapter.java

📁 Jena推理机
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
     * <p>Translate a given class resource into a DIG concept description, as a child
     * of the given expression element</p>
     * @param expr The parent expression element
     * @param c The concept resource
     */
    protected void translateClassIdentifier( Element expr, Resource c ) {
        if (c.equals( getOntLanguage().THING())) {
            // this is TOP in DIG
            addElement( expr, DIGProfile.TOP );
            return;
        }
        else if (c.equals( getOntLanguage().NOTHING())) {
            // this is BOTTOM in DIG
            addElement( expr, DIGProfile.BOTTOM );
            return;
        }
        else {
            // a named class is represented as a catom element
            Element catom = addElement( expr, DIGProfile.CATOM );
            String digConceptName = getNodeID( c.asNode() );
            catom.setAttribute( DIGProfile.NAME, digConceptName );
        }
    }


    /**
     * <p>Translate a given restriction resource into a DIG concept description, as a child
     * of the given expression element</p>
     * @param expr The parent expression element
     * @param c The restriction concept resource
     */
    protected void translateRestrictionClass( Element expr, Resource c, Model source ) {
        Restriction r = (Restriction) c.as( Restriction.class );

        if (r.isAllValuesFromRestriction()) {
            // all values from restriction translates to a DIG <all>R E</all> axiom
            Element all = addElement( expr, DIGProfile.ALL );
            addNamedElement( all, DIGProfile.RATOM, r.getOnProperty().getURI() );
            addClassDescription( all, r.asAllValuesFromRestriction().getAllValuesFrom(), source );
        }
        else if (r.isSomeValuesFromRestriction()) {
            // some values from restriction translates to a DIG <some>R E</some> axiom
            Element some = addElement( expr, DIGProfile.SOME );
            addNamedElement( some, DIGProfile.RATOM, r.getOnProperty().getURI() );
            addClassDescription( some, r.asSomeValuesFromRestriction().getSomeValuesFrom(), source );
        }
        else if (r.isHasValueRestriction()) {
            // special case
            translateHasValueRestriction( expr, r.asHasValueRestriction() );
        }
        else if (r.isMinCardinalityRestriction()) {
            // unqualified, so we make the qualification class TOP
            translateCardinalityRestriction( expr, r.asMinCardinalityRestriction().getMinCardinality(), r,
                                             DIGProfile.ATLEAST, getOntLanguage().THING(), source );
        }
        else if (r.isMaxCardinalityRestriction()) {
            // unqualified, so we make the qualification class TOP
            translateCardinalityRestriction( expr, r.asMaxCardinalityRestriction().getMaxCardinality(), r,
                                             DIGProfile.ATMOST, getOntLanguage().THING(), source );
        }
        else if (r.isCardinalityRestriction()) {
            // we model a cardinality restriction as the intersection of min and max resrictions
            Element and = addElement( expr, DIGProfile.AND );

            // unqualified, so we make the qualification class TOP
            translateCardinalityRestriction( and, r.asCardinalityRestriction().getCardinality(), r,
                                             DIGProfile.ATMOST, getOntLanguage().THING(), source );
            translateCardinalityRestriction( and, r.asCardinalityRestriction().getCardinality(), r,
                                             DIGProfile.ATLEAST, getOntLanguage().THING(), source );
        }
        // TODO qualified cardinality restrictions
    }


    /** Translate an enumerated class to an iset element */
    protected void translateEnumeratedClass(Element expr, OntClass cls, Model source ) {
        // an anonymous enumeration of class expressions
        Element iset = addElement( expr, DIGProfile.ISET );
        for (Iterator i = cls.asEnumeratedClass().listOneOf(); i.hasNext(); ) {
            RDFNode n = (RDFNode) i.next();

            if (n instanceof Resource) {
                addNamedElement( iset, DIGProfile.INDIVIDUAL, ((Resource) n).getURI() );
            }
            else {
                LogFactory.getLog( getClass() ).warn( "DIG language cannot yet represent enumerations of concrete literals: " + ((Literal) n).getLexicalForm() );
                //translateLiteral( (Literal) n, iset );
            }
        }
    }


    /** Translate a complement class to a not element */
    protected void translateComplementClass(Element expr, OntClass cls, Model source ) {
        // an anonymous complement of another class expression
        Element not = addElement( expr, DIGProfile.NOT );
        addClassDescription( not, cls.asComplementClass().getOperand(), source );
    }


    /** Translate an intersection class to an and element */
    protected void translateIntersectionClass(Element expr, OntClass cls, Model source) {
        // an anonymous intersection of class expressions
        Element or = addElement( expr, DIGProfile.AND );
        translateClassList( or, cls.asIntersectionClass().getOperands(), source );
    }


    /** Translate an union class to an or element */
    protected void translateUnionClass(Element expr, OntClass cls, Model source) {
        // an anonymous intersection of class expressions
        Element or = addElement( expr, DIGProfile.OR );
        translateClassList( or, cls.asUnionClass().getOperands(), source );
    }


    /**
     * <p>Translate a cardinality restriction, with qualification</p>
     * @param parent The parent element
     * @param card The cardinality value
     * @param r The restriction we are translating
     * @param exprName The restriction type (e.g. mincardinality)
     * @param qualType The qualification class
     */
    private void translateCardinalityRestriction( Element parent, int card, Restriction r, String exprName, Resource qualType, Model source ) {
        Element restrict = addElement( parent, exprName );
        restrict.setAttribute( DIGProfile.NUM, Integer.toString( card ) );
        addNamedElement( restrict, DIGProfile.RATOM, r.getOnProperty().getURI() );
        addClassDescription( restrict, qualType, source );
    }


    /**
     * <p>Translate a has value restriction to DIG form.  This is slightly tricky, because there is no
     * direct translation in the DIG concept language.  We translate a has value restriction with an
     * individual value to a existential restriction of the singleton concept.  We translate a has
     * value restriction with a datatype value either to an exists restriction on an integer
     * equality or a string equality, depending on the value.</p>
     * @param expr The parent expression node
     * @param r The has value restriction to translate
     */
    protected void translateHasValueRestriction( Element expr, HasValueRestriction r ) {
        RDFNode value = r.getHasValue();
        Property p = r.getOnProperty();

        // we must chose whether to use the concrete domain construction or the individual domain
        if (value instanceof Literal) {
            // int or string domain?
            Literal lit = (Literal) value;
            boolean intDomain = isIntegerType( lit.getDatatype() );

            // encode as <intequals val="x"> or <stringequals val="x">
            Element eq =  addElement( expr, (intDomain ? DIGProfile.INTEQUALS : DIGProfile.STRINGEQUALS ) );
            eq.setAttribute( DIGProfile.VAL, lit.getLexicalForm() );

            addNamedElement( eq, DIGProfile.ATTRIBUTE, p.getURI() );
        }
        else {
            // we model hasValue as an existential restriction on a very small set of possible values!
            Element some = addElement( expr, DIGProfile.SOME );
            addNamedElement( some, DIGProfile.RATOM, p.getURI() );

            // we want the set of one individual
            Element iset = addElement( some, DIGProfile.ISET );
            addNamedElement( iset, DIGProfile.INDIVIDUAL, ((Resource) value).getURI() );
        }
    }

    /**
     * <p>Translate a list of class descriptions into DIG concept descriptions
     */
    protected void translateClassList( Element expr, RDFList operands, Model source ) {
        for (Iterator i = operands.iterator(); i.hasNext(); ) {
            addClassDescription( expr, (Resource) i.next(), source );
        }
    }


    /** Translate the individuals in the KB to DIG form */
    protected void translateIndividuals( Element expr ) {
        for (Iterator i = m_sourceData.listIndividuals(); i.hasNext(); ) {
            translateIndividual( expr, (Resource) i.next() );
        }
    }

    /** Translate the various axioms pertaining to an individual */
    protected void translateIndividual( Element expr, Resource r ) {
        Individual ind = (Individual) r.as( Individual.class );
        translateInstanceTypes( expr, ind );

        for (StmtIterator i = ind.listProperties(); i.hasNext(); ) {
            Statement s = i.nextStatement();
            OntProperty p = (OntProperty) s.getPredicate().as( OntProperty.class );

            if (p.equals( getOntLanguage().DIFFERENT_FROM())) {
                translateDifferentIndividuals( expr, ind, (Individual) s.getResource().as( Individual.class ) );
            }
            else if (p.equals( getOntLanguage().SAME_AS())) {
                translateSameIndividuals( expr, ind, (Individual) s.getResource().as( Individual.class ) );
            }
            else if (p.isObjectProperty() ||
                     p.isTransitiveProperty() ||
                     p.isSymmetricProperty() ||
                     p.isInverseFunctionalProperty()) {
                translateInstanceRole( expr, ind, p, (Individual) s.getResource().as( Individual.class ) );
            }
            else if (p.isDatatypeProperty()) {
                translateInstanceAttrib( expr, ind, p, s.getLiteral() );
            }
        }
    }


    /** The rdf:type of each individual becomes a DIG instanceof element */
    protected void translateInstanceTypes( Element expr, Individual ind ) {
        for (Iterator i = ind.listRDFTypes( true );  i.hasNext(); ) {
            Resource type = (Resource) i.next();
            Element inst = addElement( expr, DIGProfile.INSTANCEOF );
            addNamedElement( inst, DIGProfile.INDIVIDUAL, getResourceID( ind ) );
            addClassDescription( inst, (OntClass) type.as( OntClass.class ), m_sourceData );
        }
    }


    /** Translate an object property into a DIG related element */
    protected void translateInstanceRole( Element expr, Individual ind, OntProperty p, Individual obj) {
        Element related = addElement( expr, DIGProfile.RELATED );
        addNamedElement( related, DIGProfile.INDIVIDUAL, getResourceID( ind ) );
        addNamedElement( related, DIGProfile.RATOM, p.getURI() );
        addNamedElement( related, DIGProfile.INDIVIDUAL, getResourceID( obj ) );
    }


    /** Translate a datatype property into a DIG value element */
    protected void translateInstanceAttrib( Element expr, Individual ind, OntProperty p, Literal obj ) {
        Element related = addElement( expr, DIGProfile.VALUE );
        addNamedElement( related, DIGProfile.INDIVIDUAL, getResourceID( ind ) );
        addNamedElement( related, DIGProfile.ATTRIBUTE, p.getURI() );

        translateLiteral( obj, related);
    }

    /** Translate an RDF literal to an IVAL or SVAL element */
    protected void translateLiteral( Literal lit, Element parent ) {
        if (isIntegerType( lit.getDatatype() )) {
            Element ival = addElement( parent, DIGProfile.IVAL );
            ival.appendChild( parent.getOwnerDocument().createTextNode( lit.getLexicalForm() ) );
        }
        else {
            Element sval = addElement( parent, DIGProfile.SVAL );
            sval.appendChild( parent.getOwnerDocument().createTextNode( lit.getLexicalForm() ) );
        }
    }


    /** Translate differentFrom(i0, i1) we assert disjoint( iset(i0), iset(i1) ) */
    protected void translateDifferentIndividuals( Element expr, Individual ind, Individual other ) {
        Element disjoint = addElement( expr, DIGProfile.DISJOINT );
        Element iset0 = addElement( disjoint, DIGProfile.ISET );
        addNamedElement( iset0, DIGProfile.INDIVIDUAL, getResourceID( ind ) );
        Element iset1 = addElement( disjoint, DIGProfile.ISET );
        addNamedElement( iset1, DIGProfile.INDIVIDUAL, getResourceID( other ) );
    }


    /** Translate sameAs(i0, i1) we assert equalc( iset(i0), iset(i1) ) */
    protected void translateSameIndividuals( Element expr, Individual ind, Individual other ) {
        Element disjoint = addElement( expr, DIGProfile.EQUALC );
        Element iset0 = addElement( disjoint, DIGProfile.ISET );
        addNamedElement( iset0, DIGProfile.INDIVIDUAL, getResourceID( ind ) );
        Element iset1 = addElement( disjoint, DIGProfile.ISET );
        addNamedElement( iset1, DIGProfile.INDIVIDUAL, getResourceID( other ) );
    }


    /** Translate all of the roles (ObjectProperties) in the KB */
    protected void translateRoles( Element expr ) {
        Set roles = new HashSet();
        collectRoleProperties( roles );

        for (Iterator i = roles.iterator(); i.hasNext(); ) {
            translateRole( expr, (ObjectProperty) ((Property) i.next()).as( ObjectProperty.class ), m_sourceData );
        }
    }

    /** Translate the various axioms that can apply to roles */
    protected void translateRole( Element expr, ObjectProperty role, Model source ) {
        translateBinaryPropertyAxioms( expr, role.getURI(), DIGProfile.IMPLIESR, role.listSuperProperties(), DIGProfile.RATOM );
        translateBinaryPropertyAxioms( expr, role.getURI(), DIGProfile.EQUALR, role.listEquivalentProperties(), DIGProfile.RATOM );
        translateDomainRangeAxioms( expr, role.getURI(), DIGProfile.DOMAIN, role.listDomain(), DIGProfile.RATOM, source );
        translateDomainRangeAxioms( expr, role.getURI(), DIGProfile.RANGE, role.listRange(), DIGProfile.RATOM, source );
        translateInverseAxioms( expr, role, DIGProfile.RATOM );

⌨️ 快捷键说明

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