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

📄 abstracttestquery.java

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

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

import com.hp.hpl.jena.graph.*;
import com.hp.hpl.jena.graph.query.*;
import com.hp.hpl.jena.shared.JenaException;
import com.hp.hpl.jena.shared.QueryStageException;
import com.hp.hpl.jena.util.CollectionFactory;
import com.hp.hpl.jena.util.iterator.*;
import com.hp.hpl.jena.graph.impl.*;

import java.util.*;
import junit.framework.*;

/**
    Abstract tests for graph query, parameterised on getGraph().
 	@author kers
*/
public abstract class AbstractTestQuery extends QueryTestBase
    {
    public AbstractTestQuery(String name)
        { super(name); }

    public abstract Graph getGraph();

    protected Query Q;
    protected Node O = node( "?O" );
    protected Graph empty;
    protected Graph single;
    
    protected final Node [] none = new Node[] {};
    
    protected final Node [] justX = new Node [] {Query.X};
    
    public static TestSuite suite()
        { return new TestSuite( QueryTest.class ); }
              
    public Graph getGraphWith( String facts )
        { return graphAdd( getGraph(), facts ); }
        
    public void setUp()
        {
		Q = new Query();
		empty = getGraphWith( "" );
		single = getGraphWith( "spindizzies drive cities" );
		}

    private void testTreeQuery( String title, String content, String pattern, String correct )
        {
        Graph gc = getGraphWith( content ), gp = getGraphWith( pattern );
        Graph answer = gc.queryHandler().prepareTree( gp ).executeTree();
        if (title.equals( "" )) title = "checking {" + content + "} against {" + pattern + "} should give {" + correct + "}" + " not " + answer;
        assertIsomorphic( title, getGraphWith( correct ), answer );
        }
        
    private void testTreeQuery( String content, String pattern, String answer )
        {
        testTreeQuery( "checking", content, pattern, answer );
        }
        
    private static final String [][] tests =
        {
            { "", "pigs might fly", "", "" },
            { "", "", "pigs might fly", "" },
            { "", "a pings b; b pings c", "a pings _x; _x pings c", "a pings b; b pings c" },
            { "", "a pings b; b pings c; a pings x; x pings c", "a pings _x; _x pings c", "a pings b; b pings c; a pings x; x pings c" }
        };
        
    public void testManyThings()
        {
        for (int i = 0; i < tests.length; i += 1) 
            testTreeQuery( tests[i][0], tests[i][1], tests[i][2], tests[i][3] );
        }
        
    public void testAtomicTreeQuery()
        {
        testTreeQuery( "pigs might fly; birds will joke; cats must watch", "birds will joke", "birds will joke" );
        }
        
    public void testCompositeTreeQuery()
        {
        testTreeQuery
            ( "pigs might fly; birds will joke; cats must watch", "birds will joke; pigs might fly", "pigs might fly; birds will joke" );
        }
        
    public void testChainedTreeQuery()
        {
        testTreeQuery( "a pings b; b pings c; c pings d", "a pings b; b pings c", "a pings b; b pings c" );
        }
        
    public void testEmptyIterator()
        {
        Graph empty = getGraph();
        Query q = new Query().addMatch( X, Y, Z );
        BindingQueryPlan bqp = empty.queryHandler().prepareBindings( q, justX );
        assertEquals( new HashSet(), iteratorToSet( bqp.executeBindings() ) );
        }
        
    public void testSingleBindings( )
        {
        Graph single = getGraphWith( "rice grows quickly" );
        Node V1 = node( "?v1" ), V3 = node( "?v3" );
        Query q = new Query().addMatch( V1, node( "grows" ), V3 );
        BindingQueryPlan qp = single.queryHandler().prepareBindings( q, new Node[] {V1, V3} );
        assertEquals( nodeListSet( "rice quickly" ), iteratorToSet( qp.executeBindings() ) );
        }

	public void testMultipleBindings() 
        { 
		Graph several = getGraphWith( "rice grows quickly; time isan illusion" );
		Node V1 = node( "?v1" ), V2 = node( "?v2" ), V3 = node( "?v3" );
		Query q = new Query().addMatch( V1, V2, V3 );
		BindingQueryPlan qp = several.queryHandler().prepareBindings
            ( q, new Node[] { V1, V2, V3 } );
        Set wanted = nodeListSet( "time isan illusion; rice grows quickly" );
        assertEquals( wanted, iteratorToSet( qp.executeBindings() ) );
        }

    protected static Set nodeListSet( String s )
        {
        Set result = new HashSet();
        StringTokenizer st = new StringTokenizer( s, ";" );
        while (st.hasMoreTokens()) result.add( nodeList( st.nextToken() ) );
        return result;
        }
    
    public void testMultiplePatterns()
        {
        Graph bookish = getGraphWith
            ( "ben wrote Clayface; Starfish ingenre SF; Clayface ingenre Geology; bill wrote Starfish" );
        Query q = new Query();
        Node A = node( "?A" ); 
        q.addMatch( X, node( "wrote" ), A ).addMatch(  A, node( "ingenre" ), node( "SF" ) );
        BindingQueryPlan qp = bookish.queryHandler().prepareBindings( q, justX );
        Set justBill = nodeListSet( "bill Starfish" );
        assertEquals( justBill, iteratorToSet( qp.executeBindings() ) );
        }
    
    /**
    	Utility. Run the query <code>q</code> over the graph <code>g</code> 
        requesting the output variables <code>nodes</code>.
     */
    protected ExtendedIterator eb( Graph g, Query q, Node [] nodes )
        { return g.queryHandler().prepareBindings( q, nodes ).executeBindings(); }
    
    protected List ebList( Graph g, Query q, Node [] nodes )
        { return iteratorToList( eb( g, q, nodes ) ); }
    
    protected Set ebSet( Graph g, Query q, Node [] nodes )
        { return iteratorToSet( eb( g, q, nodes ) ); }
        
    public void testNodeVariablesA()
        {
        Graph mine = getGraphWith( "storms hit England" );
        Node spoo = node( "?spoo" );
        Q.addMatch( spoo, node("hit"), node("England") );
        ClosableIterator it = eb( mine, Q, new Node[] {spoo} ); 
        assertTrue( "tnv: it has a solution", it.hasNext() );
        assertEquals( "", node("storms"), ((List) it.next()).get(0) );
        assertFalse( "tnv: just the one solution", it.hasNext() );
        }
   
    public void testNodeVariablesB()
        {
        Graph mine = getGraphWith( "storms hit England" );
        Node spoo = node( "?spoo" ), flarn = node( "?flarn" );
        Q.addMatch( spoo, node("hit"), flarn );
        ClosableIterator it = eb( mine, Q, new Node[] {flarn, spoo} );
        assertTrue( "tnv: it has a solution", it.hasNext() );
        List answer = (List) it.next();
        assertEquals( "tnvB", node("storms"), answer.get(1) );
        assertEquals( "tnvB", node("England"), answer.get(0) );
        assertFalse( "tnv: just the one solution", it.hasNext() );
        }

    public void testBindingQuery()
        {
        Graph empty = getGraphWith( "" );
        Graph base = getGraphWith( "pigs might fly; cats chase mice; dogs chase cars; cats might purr" );
    /* */
        Query any = new Query().addMatch( Query.ANY, Query.ANY, Query.ANY );
        assertFalse( "empty graph, no bindings", eb( empty, any, none ).hasNext() );
        assertTrue( "full graph, > 0 bindings", eb( base, new Query(), none ).hasNext() );
        }

    public void testEmpty()
        {
        List bindings = ebList( empty, Q, none );
        assertEquals( "testEmpty: select [] from {} => 1 empty binding [size]", bindings.size(), 1 );
        Domain d = (Domain) bindings.get( 0 );
        assertEquals( "testEmpty: select [] from {} => 1 empty binding [width]", d.size(), 0 );
        }
        
    public void testOneMatch()
        {
        Q.addMatch( X, Query.ANY, Query.ANY );
        List bindings = ebList( single, Q, justX ); 
        assertEquals( "select X from {spindizzies drive cities} => 1 binding [size]", bindings.size(), 1 );
        Domain d = (Domain) bindings.get( 0 );
        assertEquals( "select X from {spindizzies drive cities} => 1 binding [width]", d.size(), 1 );
        assertTrue( "select X from {spindizzies drive cities} => 1 binding [value]", d.get( 0 ).equals( node( "spindizzies" ) ) );
        }
        
    public void testMismatch()
        {
        Q.addMatch( X, X, X );
        List bindings = ebList( single, Q, justX );
        assertEquals( "bindings mismatch (X X X)", bindings.size(), 0 );
        }
        
    public void testXXXMatch1()
        {
        Q.addMatch( X, X, X );
        Graph xxx = getGraphWith( "ring ring ring" );
        List bindings = ebList( xxx, Q, justX ); 
        assertEquals( "bindings match (X X X)", bindings.size(), 1 );       
        }

    public void testXXXMatch3()
        {
        Q.addMatch( X, X, X );
        Graph xxx = getGraphWith( "ring ring ring; ding ding ding; ping ping ping" );
        List bindings = ebList( xxx, Q, justX ); 
        assertEquals( "bindings match (X X X)", bindings.size(), 3 );       
    /* */
        Set found = CollectionFactory.createHashedSet();
        for (int i = 0; i < bindings.size(); i += 1) 
            {
            Domain d = (Domain) bindings.get( i );
            assertEquals( "one bound variable", d.size(), 1 );
            found.add( d.get( 0 ) );
            }
        Set wanted = nodeSet( "ring ding ping" ); 
        assertEquals( "testMatch getting {ring ding ping}", found, wanted );
        }
        
    public void testTwoPatterns()
        {
        Node reads = node("reads"), inGenre = node("inGenre");
        Graph g = getGraphWith( "chris reads blish; blish inGenre SF" );
        // System.err.println( "| X = " + X + ", Y = " + Y + ", Z = " + Z );
        Q.addMatch( X, reads, Y );
        Q.addMatch( Y, inGenre, Z );
        List bindings = ebList( g, Q, new Node [] {X, Z} ); 
        assertEquals( "testTwoPatterns: one binding", 1, bindings.size() );
        Domain  d = (Domain) bindings.get( 0 );
        // System.out.println( "* width = " + d.width() );
        assertTrue( "testTwoPatterns: width 2", d.size() >= 2 );
        assertEquals( "testTwoPatterns: X = chris", d.get(0), node("chris") );
        assertEquals( "testTwoPatterns: Y = SF", d.get(1), node("SF") );
        }
        
    public void testGraphQuery()
        {
        Graph pattern = getGraphWith( "?X reads ?Y; ?Y inGenre ?Z" );
        Graph target = getGraphWith( "chris reads blish; blish inGenre SF" );
        // System.err.println( "| pattern: " + pattern );
        Query q = new Query( pattern );
        List bindings = ebList( target, q, new Node [] {node("?X"), node("?Z")} ); 
        assertEquals( "testTwoPatterns: one binding", 1, bindings.size() );
        Domain  d = (Domain) bindings.get( 0 );
        // System.out.println( "* width = " + d.width() );
        assertTrue( "testTwoPatterns: width 2", d.size() >= 2 );

⌨️ 快捷键说明

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