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

📄 propertyiterator.java

📁 Jena推理机
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    /**
     * Set the default value for this iteration, which will be a value that
     * is guaranteed to be returned as a member of the iteration.  To guarantee
     * that the default value is only returned if it has not already been
     * returned by the iterator, setting the default value should occur before
     * the first call to {@link #next}.
     *
     * @param defaultValue The default value for the iteration, or null for
     *                     there to be no default value.  The default default
     *                     value is null.
     */
    public void setDefaultValue( Object defaultValue ) {
        m_defaultValue = defaultValue;
    }


    /**
     * Answer true if this iteration has a default value.
     *
     * @return true if there is a default value
     */
    public boolean hasDefaultValue() {
        return m_defaultValue != null;
    }



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


    /**
     * Queue a node onto the evaluation queue, but only if it has not already been evaluated
     * (to avoid loops).
     *
     * @param node The node to add to the queue
     */
    private void enqueue( RDFNode node ) {
        if (!m_seen.containsKey( node )) {
            // mark this node as seen
            m_seen.put( node, Boolean.TRUE );

            // add it to the queue
            m_nodeQueue.addLast( node );

            // also queue up the equivalens to the node
            if (getUseEquivalence()  &&  node instanceof DAMLCommon) {
                for (Iterator i = ((DAMLCommon) node).getEquivalentValues();  i.hasNext(); ) {
                    enqueue( (RDFNode) i.next() );
                }
            }
        }
    }


    /**
     * Expand the queue with the arcs to/from the given resource
     *
     * @param r The resource we're expanding
     */
    protected void expandQueue( Resource r ) {
        // add all outgoing arcs if we're at the root or the predicate is transitive
        if (m_pred != null  &&  (m_transitive  ||  isRoot( r ))) {
            // we want all the related items from this node
            for (Iterator i = getStatementObjects( r );
                 i.hasNext();
                 enqueue( (RDFNode) i.next() ));
        }

        // if we know the inverse, we also add the incoming arcs to this node
        if (m_inverse != null  &&  (m_transitive  ||  isRoot( r ))) {
            // find statements whose predicate is m_inverse and object is current node
            for (Iterator i = getStatementSubjects( r );
                 i.hasNext();
                 enqueue( (RDFNode) i.next() ));
        }
    }


    /**
     * Answer true if the given resource was one of the starting points.
     *
     * @param r A resource
     * @return True if resource r was one of the roots of this iteration.
     */
    protected boolean isRoot( Resource r ) {
        if (m_roots != null) {
            // started with a set of roots
            return m_roots.contains( r );
        }
        else {
            // just the one root - compare this one to it
            return r == m_root;
        }
    }


    /**
     * Answer an iterator over objects of statements with res
     * as subject, and m_pred or one of its equivalent properties as property,
     * including all of the equivalent values to the object.
     *
     * @param res A resource
     * @return An iterator over the object of any statements whose subject is res.
     */
    protected Iterator getStatementObjects( Resource res )
    {
        Iterator i = null;

        if (getUseEquivalence()  &&  res instanceof DAMLCommon) {
            // consider equivalents for res and for m_pred
            for (Iterator j = m_predEquivs.iterator();   j.hasNext();  ) {
                // get an iterator for the values of the resource with this predicate
                Iterator pIter = new PropertyIterator( res, (Property) j.next(), null, m_transitive, false, false );

                // build a joint iterator
                i = (i == null) ? pIter  : new ConcatenatedIterator( pIter,  i );
            }
        }
        else {
            // don't use equivalents
            if (m_model != null) {
                // we have a model to query, so use it to get the object of the triple
                i = m_model.listObjectsOfProperty( res, m_pred );
            }
            else {
                // no model (can occur when using built-in constants from vocab)
                i = new LinkedList().iterator();
            }
        }

        return i;
    }


    /**
     * Answer an iterator over subjects of statements with res (or one of its equivalents)
     * as object, and m_inverse or one of its equivalent properties as property.
     *
     * @param res A resource
     * @return An iterator over the subject of any statements whose object is res.
     */
    protected Iterator getStatementSubjects( Resource res )
    {
        Iterator i = null;

        if (getUseEquivalence()  &&  res instanceof DAMLCommon) {
            // consider equivalents for res and for m_pred
            for (Iterator j = m_inverseEquivs.iterator();   j.hasNext();  ) {
                // get an iterator for the values of the resource with this predicate
                Iterator pIter = new PropertyIterator( res, null, (Property) j.next(), m_transitive, false, false );

                // build a joint iterator
                i = (i == null) ? pIter  : new ConcatenatedIterator( pIter,  i );
            }
        }
        else {
            // don't use equivalents
            if (m_model != null) {
                // we have a model to query, so use it to get the subject of the triple
                i = m_model.listSubjectsWithProperty( m_inverse, res );
            }
            else {
                // no model (can occur when using built-in constants from vocab)
                i = new LinkedList().iterator();
            }
        }

        return i;
    }


    /**
     * Cache the equivalent properties to the principal properties and their inverses we
     * are using
     */
    protected void cachePropertyEquivs() {
        if (getUseEquivalence()) {
            // store the equivalent properties to m_pred
            if (m_pred != null) {
                if (m_pred instanceof DAMLProperty) {
                    // daml property - we know about equivalence classes for these
                    for (Iterator i = ((DAMLProperty) m_pred).getEquivalentValues();  i.hasNext(); ) {
                        cacheProperty( m_predEquivs, (Property) i.next() );
                    }
                }
                else {
                    // normal rdf property, is its own equiv
                    cacheProperty( m_predEquivs, m_pred );
                }
            }

            // store the equivalent properties to m_inverse
            if (m_inverse != null) {
                if (m_inverse instanceof DAMLProperty) {
                    // daml property - we know about equivalence classes for these
                    for (Iterator i = ((DAMLProperty) m_inverse).getEquivalentValues();  i.hasNext(); ) {
                        cacheProperty( m_inverseEquivs, (Property) i.next() );
                    }
                }
                else {
                    // normal rdf property, is its own equiv
                    cacheProperty( m_inverseEquivs, m_inverse );
                }
            }
        }
    }


    /**
     * Add the given property, and it sub-properties, to the set of cached properties.
     *
     * @param s A set
     * @param p A property to add to the set s.
     */
    protected void cacheProperty( HashSet s, Property p ) {
        s.add( p );

        // also add the sub-properties of this property: if the sub-prop holds,
        // this property holds also
        // check for p being 'subPropertyOf' to avoid infinite regress!
        if (p instanceof DAMLProperty  &&  !p.getLocalName().equals( DAML_OIL.subPropertyOf.getLocalName() )) {
            for (Iterator i = ((DAMLProperty) p).getSubProperties();  i.hasNext(); ) {
                s.add( i.next() );
            }
        }
    }


    /**
     * Attempt to determine which model we are working on
     */
    protected void setModel() {
        // try the root object, if defined
        if (m_root != null  &&  m_root.getModel() != null) {
            m_model = m_root.getModel();
            return;
        }

        // try the predicate object
        if (m_pred != null  &&  m_pred.getModel() != null) {
            m_model = m_pred.getModel();
            return;
        }

        // try the predicate inverse
        if (m_inverse != null  &&  m_inverse.getModel() != null) {
            m_model = m_inverse.getModel();
            return;
        }

        // try the set of roots
        if (m_roots != null) {
            for (Iterator i = m_roots.iterator();  i.hasNext(); ) {
                RDFNode n = (RDFNode) i.next();
                if (n instanceof Resource  &&  ((Resource) n).getModel() != null) {
                    m_model = ((Resource) n).getModel();
                    return;
                }
            }
        }
    }


    /**
     * Answer true if we are computing with equivalence classes at the moment.
     *
     * @return True if equivlance is to be computed.
     */
    protected boolean getUseEquivalence() {
        /*
        // if we have a model, it must have equivalence switched on
        // if no model, default to relying on the m_useEquivalence setting
        boolean modelEquivFlag = m_model == null  ||
                                 !(m_model instanceof DAMLModel) ||
                                 ((DAMLModel) m_model).getUseEquivalence();

        return m_useEquivalence  &&  modelEquivFlag;
        */
        // equivalence processing is now handled by the inference engines
        return false;
    }



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

}

/*
    (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    are met:

    1. Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in the
       documentation and/or other materials provided with the distribution.

    3. The name of the author may not be used to endorse or promote products
       derived from this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

⌨️ 快捷键说明

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