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

📄 graphtestbase.java

📁 Jena推理机
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
  (c) Copyright 2003, 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP
  [See end of file]ispo
  $Id: GraphTestBase.java,v 1.33 2007/01/02 11:50:07 andy_seaborne Exp $
*/

package com.hp.hpl.jena.graph.test;

/**
    An extension of JenaTestBase (which see) with Graph-specific methods.
	@author kers
*/

import com.hp.hpl.jena.util.*;
import com.hp.hpl.jena.util.iterator.*;
import com.hp.hpl.jena.graph.*;
import com.hp.hpl.jena.graph.impl.GraphBase;
import com.hp.hpl.jena.shared.*;
import com.hp.hpl.jena.test.*;

import java.lang.reflect.Constructor;
import java.util.*;

public class GraphTestBase extends JenaTestBase
    {
    public GraphTestBase( String name )
    	{ super( name ); }
    	
    /**
        Answer a Node as described by <code>x</code>; a shorthand for
        <code>Node.create(x)</code>, which see.
    */
    public static Node node( String x )
        { return Node.create( x ); }
        
    /**
        Answer a set containing the elements from the iterator <code>it</code>;
        a shorthand for <code>IteratorCollection.iteratorToSet(it)</code>,
        which see.
    */
    public static Set iteratorToSet( Iterator it )
        { return IteratorCollection.iteratorToSet( it ); }
    
    /**
        Answer a list containing the elements from the iterator <code>it</code>,
        in order; a shorthand for <code>IteratorCollection.iteratorToList(it)</code>,
        which see.
    */
    public static List iteratorToList( Iterator it )
        { return IteratorCollection.iteratorToList( it ); }
                
    /**
        Answer a set of the nodes described (as per <code>node()</code>) by
        the space-separated substrings of <code>nodes</code>.
    */
    public Set nodeSet( String nodes )
        {
        Set result = CollectionFactory.createHashedSet();
        StringTokenizer st = new StringTokenizer( nodes );
        while (st.hasMoreTokens()) result.add( node( st.nextToken() ) );
        return result;
        }
    
    /**
        Answer a set of the elements of <code>A</code>.
    */
    public Set arrayToSet( Object [] A )
        { return CollectionFactory.createHashedSet( Arrays.asList( A ) ); }
    
    /**
        Answer a triple described by the three space-separated node descriptions
        in <code>fact</code>; a shorthand for <code>Triple.create(fact)</code>,
        which see.
    */
    public static Triple triple( String fact )
        { return Triple.create( fact ); }
    
    /**
        Answer a triple described by the three space-separated node descriptions
        in <code>fact</code>, using prefix-mappings from <code>pm</code>; a 
        shorthand for <code>Triple.create(pm, fact)</code>, which see.
    */
    public static Triple triple( PrefixMapping pm, String fact )
        { return Triple.create( pm, fact ); }
        
    /**
        Answer an array of triples; each triple is described by one of the
        semi-separated substrings of <code>facts</code>, as per 
        <code>triple</code> with prefix-mapping <code>Extended</code>.
    */
    public static Triple [] tripleArray( String facts )
        {
        ArrayList al = new ArrayList();
        StringTokenizer semis = new StringTokenizer( facts, ";" );
        while (semis.hasMoreTokens()) al.add( triple( PrefixMapping.Extended, semis.nextToken() ) );   
        return (Triple []) al.toArray( new Triple [al.size()] );
        }
    
    /**
        Answer a set of triples where the elements are described by the
        semi-separated substrings of <code>facts</code>, as per 
        <code>triple</code>.
    */
    public static Set tripleSet( String facts )
        {
        Set result = new HashSet();
        StringTokenizer semis = new StringTokenizer( facts, ";" );
        while (semis.hasMoreTokens()) result.add( triple( semis.nextToken() ) );   
        return result;
        }
       
    /**
        Answer a list of nodes, where the nodes are described by the 
        space-separated substrings of <code>items</code> as per
        <code>node()</code>.
    */
    public static List nodeList( String items )
        {
        ArrayList nl = new ArrayList();
        StringTokenizer nodes = new StringTokenizer( items );
        while (nodes.hasMoreTokens()) nl.add( node( nodes.nextToken() ) );   
        return nl;
        }   
    
    /**
        Answer an array of nodes, where the nodes are described by the 
        space-separated substrings of <code>items</code> as per
    */
    public static Node [] nodeArray( String items )
        {
        List nl = nodeList( items );  
        return (Node []) nl.toArray( new Node [nl.size()] );
        } 
    
    /**
        Answer the graph <code>g</code> after adding to it every triple
        encoded in <code>s</code> in the fashion of <code>tripleArray</code>,
        a semi-separated sequence of space-separated node descriptions.
    */
    public static Graph graphAdd( Graph g, String s )
        {
        StringTokenizer semis = new StringTokenizer( s, ";" );
        while (semis.hasMoreTokens()) g.add( triple( PrefixMapping.Extended, semis.nextToken() ) );     
        return g;
        }
    
    /**
        Answer a new memory-based graph with Extended prefixes.
    */
    public static Graph newGraph()
        {
        Graph result = Factory.createGraphMem();
        result.getPrefixMapping().setNsPrefixes( PrefixMapping.Extended );
        return result;
        }
    
    /**
        Answer a new memory-based graph with initial contents as described
        by <code>s</code> in the fashion of <code>graphAdd()</code>.
        Not over-ridable; do not use for abstraction.
    */
    public static Graph graphWith( String s )
        { return graphAdd( newGraph(), s ); }
    
    /**
        Assert that the graph <code>g</code> is isomorphic to the graph 
        described by <code>template</code> in the fashion of 
        <code>graphWith</code>.
    */
    public static void assertEqualsTemplate( String title, Graph g, String template )
        { assertIsomorphic( title, graphWith( template ), g ); }
                
    /**
        Assert that the supplied graph <code>got</code> is isomorphic with the
        the desired graph <code>expected</code>; if not, display a readable
        description of both graphs.
    */
    public static void assertIsomorphic( String title, Graph expected, Graph got )
        {
        if (!expected.isIsomorphicWith( got ))
            {
            Map map = CollectionFactory.createHashedMap();
            fail( title + ": wanted " + nice( expected, map ) + "\nbut got " + nice( got, map ) );
            }
        }
    
    /**
        Answer a string which is a newline-separated list of triples (as
        produced by niceTriple) in the graph <code>g</code>. The map 
        <code>bnodes</code> maps already-seen bnodes to their "nice" strings. 
    */
    public static String nice( Graph g, Map bnodes )
        {
        StringBuffer b = new StringBuffer( g.size() * 100 );
        ExtendedIterator it = GraphUtil.findAll( g );
        while (it.hasNext()) niceTriple( b, bnodes, (Triple) it.next() );
        return b.toString();
        }
    
    /**
        Append to the string buffer <code>b</code> a "nice" representation 
        of the triple <code>t</code> on a new line, using (and updating)
        <code>bnodes</code> to supply "nice" strings for any blank nodes.
    */
    protected static void niceTriple( StringBuffer b, Map bnodes, Triple t )
        {
        b.append( "\n    " );
        appendNode( b, bnodes, t.getSubject() );
        appendNode( b, bnodes, t.getPredicate() );
        appendNode( b, bnodes, t.getObject() );
        }

    /**
        A counter for new bnode strings; it starts at 1000 so as to make
        the bnode strings more uniform (at least for the first 9000 bnodes).
    */
    protected static int bnc = 1000;

⌨️ 快捷键说明

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