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

📄 graphtestbase.java

📁 Jena推理机
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    
    /**
        Append to the string buffer <code>b</code> a space followed by the
        "nice" representation of the node <code>n</code>. If <code>n</code>
        is a bnode, re-use any existing string for it from <code>bnodes</code>
        or make a new one of the form <i>_bNNNN</i> with NNNN a new integer.
    */
    protected static void appendNode( StringBuffer b, Map bnodes, Node n )
        {
        b.append( ' ' );
        if (n.isBlank())
            {
            Object already = bnodes.get( n );
            if (already == null) bnodes.put( n, already = "_b" + bnc++ );
            b.append( already );
            }
        else
            b.append( nice( n ) );
        }
    
    /**
        Answer the "nice" representation of this node, the string returned by
        <code>n.toString(PrefixMapping.Extended,true)</code>.
    */
    protected static String nice( Node n )
        { return n.toString( PrefixMapping.Extended, true ); }
            
    /**
        Assert that the computed graph <code>got</code> is isomorphic with the
        desired graph <code>expected</code>; if not, fail with a default
        message (and pretty output of the graphs).
    */
    public static void assertIsomorphic( Graph expected, Graph got )
        { assertIsomorphic( "graphs must be isomorphic", expected, got ); }

    /**
        Assert that the graph <code>g</code> must contain the triple described
        by <code>s</code>; if not, fail with pretty output of both graphs
        and a message containing <code>name</code>.
    */
    public static void assertContains( String name, String s, Graph g )
        {
        assertTrue( name + " must contain " + s, g.contains( triple( s ) ) );
        }
    
    /**
        Assert that the graph <code>g</code> contains all the triples described
        by the string <code>s</code; if not, fail with a message containing
        <code>name</code>.
    */
    public static void assertContainsAll( String name, Graph g, String s )
        {
        StringTokenizer semis = new StringTokenizer( s, ";" );
        while (semis.hasMoreTokens()) assertContains( name, semis.nextToken(), g );       
        }
    
    /**
        Assert that the graph <code>g</code> does not contain the triple
        described by <code>s<code>; if it does, fail with a message containing
        <code>name</code>.
    */
    public static void assertOmits( String name, Graph g, String s )
        {
        assertFalse( name + " must not contain " + s, g.contains( triple( s ) ) );
        }
    
    /**
        Assert that the graph <code>g</code> contains none of the triples
        described by <code>s</code> in the usual way; otherwise, fail with
        a message containing <code>name</code>.
    */
    public static void assertOmitsAll( String name, Graph g, String s )
        {
        StringTokenizer semis = new StringTokenizer( s, ";" );
        while (semis.hasMoreTokens()) assertOmits( name, g, semis.nextToken() );     
        }
        
    /**
        Assert that <code>g</code> contains the triple described by 
        <code>fact</code> in the usual way.
    */
    public static boolean contains( Graph g, String fact )
        { return g.contains( triple( fact ) ); }
    
    /**
        Assert that <code>g</code> contains every triple in <code>triples</code>.
    */
    public void testContains( Graph g, Triple [] triples )
        { 
        for (int i = 0; i < triples.length; i += 1) 
            assertTrue( "contains " + triples[i], g.contains( triples[i] ) ); 
        }

    /**
        Assert that <code>g</code> contains every triple in <code>triples</code>.
    */
    public void testContains( Graph g, List triples )
        {
        for (int i = 0; i < triples.size(); i += 1)
             assertTrue( g.contains( (Triple) triples.get(i) ) );
        }

    /**
        Assert that <code>g</code> contains every triple in <code>it</code>.
    */
    public void testContains( Graph g, Iterator it )
        { while (it.hasNext()) assertTrue( g.contains( (Triple) it.next() ) ); }

    /**
        Assert that <code>g</code> contains every triple in <code>other</code>.
    */
    public void testContains( Graph g, Graph other )
        { testContains( g, GraphUtil.findAll( other ) ); }
    
    /**
        Assert that <code>g</code> contains none of the triples in 
        <code>triples</code>.
    */
    public void testOmits( Graph g, Triple [] triples )
        { for (int i = 0; i < triples.length; i += 1) assertFalse( "", g.contains( triples[i] ) ); }
    
    /**
        Assert that <code>g</code> contains none of the triples in 
        <code>triples</code>.
    */
    public void testOmits( Graph g, List triples )
        {
        for (int i = 0; i < triples.size(); i += 1)
             assertFalse( "", g.contains( (Triple) triples.get(i) ) );
        }
    
    /**
        Assert that <code>g</code> contains none of the triples in 
        <code>it</code>.
    */
    public void testOmits( Graph g, Iterator it )
        { while (it.hasNext()) assertFalse( "", g.contains( (Triple) it.next() ) ); }
    
    /**
        Assert that <code>g</code> contains none of the triples in 
        <code>other</code>.
    */
    public void testOmits( Graph g, Graph other )
        { testOmits( g, GraphUtil.findAll( other ) ); }

    /**
        Answer an instance of <code>graphClass</code>. If <code>graphClass</code> has
        a constructor that takes a <code>ReificationStyle</code> argument, then that
        constructor is run on <code>style</code> to get the instance. Otherwise, if it has a #
        constructor that takes an argument of <code>wrap</code>'s class before the
        <code>ReificationStyle</code>, that constructor is used; this allows non-static inner
        classes to be used for <code>graphClass</code>, with <code>wrap</code> being
        the outer class instance. If no suitable constructor exists, a JenaException is thrown.
        
        @param wrap the outer class instance if graphClass is an inner class
        @param graphClass a class implementing Graph
        @param style the reification style to use
        @return an instance of graphClass with the given style
        @throws RuntimeException or JenaException if construction fails
     */
    public static Graph getGraph( Object wrap, Class graphClass, ReificationStyle style ) 
        {
        try
            {
            Constructor cons = getConstructor( graphClass, new Class[] {ReificationStyle.class} );
            if (cons != null) return (Graph) cons.newInstance( new Object[] { style } );
            Constructor cons2 = getConstructor( graphClass, new Class [] {wrap.getClass(), ReificationStyle.class} );
            if (cons2 != null) return (Graph) cons2.newInstance( new Object[] { wrap, style } );
            throw new JenaException( "no suitable graph constructor found for " + graphClass );
            }
        catch (RuntimeException e)
            { throw e; }
        catch (Exception e)
            { throw new JenaException( e ); }
        }

    protected static Graph getReificationTriples( final Reifier r )
        {
        return new GraphBase()
            {
            public ExtendedIterator graphBaseFind( TripleMatch m ) { return r.find( m ); }
            };
        }

        
    }

/*
    (c) Copyright 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 + -