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

📄 ontresourceimpl.java

📁 Jena推理机
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
     *
     * @param property The property whose values are sought
     * @return An Iterator over the values of the property
     */
    public NodeIterator listPropertyValues( Property property ) {
        return new NodeIteratorImpl( listProperties( property ).mapWith( new ObjectAsOntResourceMapper() ), null );
    }

    /**
    * <p>Removes this resource from the ontology by deleting any statements that refer to it,
    * as either statement-subject or statement-object.
    * If this resource is a property, this method will <strong>not</strong> remove statements
    * whose predicate is this property.</p>
    * <p><strong>Caveat:</strong> Jena RDF models contain statements, not resources <em>per se</em>,
    * so this method simulates removal of an object by removing all of the statements that have
    * this resource as subject or object, with one exception. If the resource is referenced
    * in an RDF List, i.e. as the object of an <code>rdf:first</code> statement in a list cell,
    * this reference is <strong>not</strong> removed.  Removing an arbitrary <code>rdf:first</code>
    * statement from the midst of a list, without doing other work to repair the list, would
    * leave an ill-formed list in the model.  Therefore, if this resource is known to appear
    * in a list somewhere in the model, it should be separately deleted from that list before
    * calling this remove method.
    * </p>
    */
    public void remove() {
        Set stmts = new HashSet();
        List lists = new ArrayList();
        List skip = new ArrayList();
        Property first = getProfile().FIRST();

        // collect statements mentioning this object
        for (StmtIterator i = listProperties();  i.hasNext(); ) {
            stmts.add( i.next() );
        }
        for (StmtIterator i = getModel().listStatements( null, null, this ); i.hasNext(); ) {
            stmts.add( i.next() );
        }

        // check for lists
        for (Iterator i = stmts.iterator(); i.hasNext(); ) {
            Statement s = (Statement) i.next();
            if (s.getPredicate().equals( first ) &&
                s.getObject().equals( this )) {
                // _this_ is referenced from inside a list
                // we don't delete this reference, since it would make the list ill-formed
                log.debug( toString() + " is referened from an RDFList, so will not be fully removed");
                skip.add( s );
            }
            else if (s.getObject() instanceof Resource){
                // check for list-valued properties
                Resource obj = s.getResource();
                if (obj.canAs( RDFList.class )) {
                    // this value is a list, so we will want to remove all of the elements
                    lists.add( obj );
                }
            }
        }

        // add in the contents of the lists to the statements to be removed
        for (Iterator i = lists.iterator(); i.hasNext(); ) {
            Resource r = (Resource) i.next();
            stmts.addAll( ((RDFListImpl) r.as( RDFList.class )).collectStatements() );
        }

        // skip the contents of the skip list
        stmts.removeAll( skip );

        // and then remove the remainder
        for (Iterator i = stmts.iterator();  i.hasNext(); ) {
            ((Statement) i.next()).remove();
        }
    }


    /**
     * <p>Remove the specific RDF property-value pair from this DAML resource.</p>
     *
     * @param property The property to be removed
     * @param value The specific value of the property to be removed
     */
    public void removeProperty( Property property, RDFNode value ) {
        getModel().remove( this, property, value );
    }


    /**
     * <p>Answer a view of this resource as an annotation property</p>
     * @return This resource, but viewed as an AnnotationProperty
     * @exception ConversionException if the resource cannot be converted to an annotation property
     */
    public AnnotationProperty asAnnotationProperty() {
        return (AnnotationProperty) as( AnnotationProperty.class );
    }

    /**
     * <p>Answer a view of this resource as a property</p>
     * @return This resource, but viewed as an OntProperty
     * @exception ConversionException if the resource cannot be converted to a property
     */
    public OntProperty asProperty() {
        return (OntProperty) as( OntProperty.class );
    }

    /**
     * <p>Answer a view of this resource as an object property</p>
     * @return This resource, but viewed as an ObjectProperty
     * @exception ConversionException if the resource cannot be converted to an object property
     */
    public ObjectProperty asObjectProperty() {
        return (ObjectProperty) as( ObjectProperty.class );
    }

    /**
     * <p>Answer a view of this resource as a datatype property</p>
     * @return This resource, but viewed as a DatatypeProperty
     * @exception ConversionException if the resource cannot be converted to a datatype property
     */
    public DatatypeProperty asDatatypeProperty() {
        return (DatatypeProperty) as( DatatypeProperty.class );
    }

    /**
     * <p>Answer a view of this resource as an individual</p>
     * @return This resource, but viewed as an Individual
     * @exception ConversionException if the resource cannot be converted to an individual
     */
    public Individual asIndividual() {
        return (Individual) as( Individual.class );
    }

    /**
     * <p>Answer a view of this resource as a class</p>
     * @return This resource, but viewed as an OntClass
     * @exception ConversionException if the resource cannot be converted to a class
     */
    public OntClass asClass() {
        return (OntClass) as( OntClass.class );
    }

    /**
     * <p>Answer a view of this resource as an ontology description node</p>
     * @return This resource, but viewed as an Ontology
     * @exception ConversionException if the resource cannot be converted to an ontology description node
     */
    public Ontology asOntology() {
        return (Ontology) as( Ontology.class );
    }

    /**
     * <p>Answer a view of this resource as an 'all different' declaration</p>
     * @return This resource, but viewed as an AllDifferent node
     * @exception ConversionException if the resource cannot be converted to an all different declaration
     */
    public AllDifferent asAllDifferent() {
        return (AllDifferent) as( AllDifferent.class );
    }

    /**
     * <p>Answer a view of this resource as a data range</p>
     * @return This resource, but viewed as a DataRange
     * @exception ConversionException if the resource cannot be converted to a data range
     */
    public DataRange asDataRange() {
        return (DataRange) as( DataRange.class );
    }


    // Conversion test methods

    /**
     * <p>Answer true if this resource can be viewed as an annotation property</p>
     * @return True if this resource can be viewed as an AnnotationProperty
     */
    public boolean isAnnotationProperty() {
        return getProfile().ANNOTATION_PROPERTY() != null && canAs( AnnotationProperty.class );
    }

    /**
     * <p>Answer true if this resource can be viewed as a property</p>
     * @return True if this resource can be viewed as an OntProperty
     */
    public boolean isProperty() {
        return canAs( OntProperty.class );
    }

    /**
     * <p>Answer true if this resource can be viewed as an object property</p>
     * @return True if this resource can be viewed as an ObjectProperty
     */
    public boolean isObjectProperty() {
        return getProfile().OBJECT_PROPERTY() != null && canAs( ObjectProperty.class );
    }

    /**
     * <p>Answer true if this resource can be viewed as a datatype property</p>
     * @return True if this resource can be viewed as a DatatypeProperty
     */
    public boolean isDatatypeProperty() {
        return getProfile().DATATYPE_PROPERTY() != null && canAs( DatatypeProperty.class );
    }

    /**
     * <p>Answer true if this resource can be viewed as an individual</p>
     * @return True if this resource can be viewed as an Individual
     */
    public boolean isIndividual() {
        OntModel m = (getModel() instanceof OntModel) ? (OntModel) getModel() : null;

        // can we use the reasoner's native abilities to do the isntance test?
        boolean useInf = false;
        useInf = m.getProfile().THING() != null &&
                 m.getReasoner() != null &&
                 m.getReasoner().supportsProperty( ReasonerVocabulary.individualAsThingP );

        StmtIterator i = null, j = null;
        try {
            if (m != null) {
                if (!useInf) {
                    // either not using the OWL reasoner, or not using OWL
                    // look for an rdf:type of this resource that is a class
                    for (i = listProperties( RDF.type ); i.hasNext(); ) {
                        Resource rType = i.nextStatement().getResource();
                        if (rType.equals( m.getProfile().THING() )) {
                            // the resource has rdf:type owl:Thing (or equivalent)
                            return true;
                        }
                        for (j = rType.listProperties( RDF.type ); j.hasNext(); ) {
                            if (j.nextStatement().getResource().equals( getProfile().CLASS() )) {
                                // we have found an rdf:type of the subject that is an owl, rdfs or daml Class
                                // therefore this is an individual
                                return true;
                            }
                        }
                    }

                    // apparently not an instance
                    return false;
                }
                else {
                    // using the rule reasoner on an OWL graph, so we can determine
                    // individuals as those things that have rdf:type owl:Thing
                    return hasProperty( RDF.type, getProfile().THING() );
                }
            }
        }
        finally {
            if (i != null) { i.close(); }
            if (j != null) { j.close(); }
        }

        // default - try to convert and return false if fail
        return canAs( Individual.class );
    }

    /**
     * <p>Answer true if this resource can be viewed as a class</p>
     * @return True if this resource can be viewed as an OntClass
     */
    public boolean isClass() {
        return canAs( OntClass.class );
    }

    /**
     * <p>Answer true if this resource can be viewed as an ontology description node</p>
     * @return True if this resource can be viewed as an Ontology
     */
    public boolean isOntology() {
        return getProfile().ONTOLOGY() != null && canAs( Ontology.class );
    }

    /**
     * <p>Answer true if this resource can be viewed as a data range</p>
     * @return True if this resource can be viewed as a DataRange
     */
    public boolean isDataRange() {
        return getProfile().DATARANGE() != null && canAs( DataRange.class );
    }

    /**
     * <p>Answer true if this resource can be viewed as an 'all different' declaration</p>
     * @return True if this resource can be viewed as an AllDifferent node
     */
    public boolean isAllDifferent() {
        return getProfile().ALL_DIFFERENT() != null && canAs( AllDifferent.class );
    }



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


    /** Answer true if the node has the given type in the graph */
    protected static boolean hasType( Node n, EnhGraph g, Resource type ) {
        boolean hasType = false;
        ClosableIterator i = g.asGraph().find( n, RDF.type.asNode(), type.asNode() );
        hasType = i.hasNext();
        i.close();
        return hasType;
    }

    /**
     * Throw an exception if a term is not in the profile
     * @param term The term being checked
     * @param name The name of the term
     * @exception ProfileException if term is null (indicating it is not in the profile)
     **/
    protected void checkProfile( Object term, String name ) {
        if (term == null) {
            throw new ProfileException( name, getProfile() );
        }
    }


    /**
     * <p>Answer the literal with the language tag that best matches the required language</p>
     * @param stmts A StmtIterator over the candidates

⌨️ 快捷键说明

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