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

📄 abstracttestquery.java

📁 jena2.5.4推理机系统的一种最基本实现 HP实验室出品
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        assertEquals( "testTwoPatterns: X = chris", d.get(0), node("chris") );
        assertEquals( "testTwoPatterns: Y = SF", d.get(1), node("SF") );        
        }
                
    public void testTwoGraphs()
        {
        Graph a = getGraphWith( "chris reads blish; chris reads norton; chris eats curry" );
        Graph b = getGraphWith( "blish inGenre SF; curry inGenre food" );
        Node reads = node("reads"), inGenre = node("inGenre");
        Q.addMatch( "a", X, reads, Y ).addMatch( "b", Y, inGenre, Z );
        NamedGraphMap args = Q.args().put( "a", a ).put( "b", b );
        List bindings = iteratorToList( Q.executeBindings( args, new Node [] {X, Z} ) ); // TODO
        assertEquals( "testTwoGraphs: one binding", 1, bindings.size() );
        Domain  d = (Domain) bindings.get( 0 );
        assertTrue( "testTwoGraphs: width 2", d.size() >= 2 );
        assertEquals( "testTwoGraphs: X = chris", d.get(0), node("chris") );
        assertEquals( "testTwoGraphs: Y = SF", d.get(1), node("SF") );     
        }
                
    public void testGraphConstraints( String title, Expression constraint, String wanted )
        { 
        Query Q = new Query()
            .addMatch( Query.ANY, Query.ANY, O )
            .addConstraint( constraint );
        Graph G = getGraphWith( "pigs fly south; dogs fly badly; plans fly flat" );
        Set results = iteratorToSet( eb( G, Q, new Node[] {O} ).mapWith( getFirst ) );
        assertEquals( "tgs", nodeSet( wanted ), results );
        }
        
     public void testGraphConstraints()
        {
        Node badly = node( "badly" ), flat = node( "flat" );
        testGraphConstraints( "tgs A", Expression.TRUE, "south flat badly" );
        testGraphConstraints( "tgs B", notEqual( O, badly ), "south flat" );
        testGraphConstraints( "tgs C", Dyadic.and( notEqual( O, badly ), notEqual( O, flat ) ), "south" );
        }
        
    private void helpConstraint( String title, Expression constraints, int n )
        {
        Query q = new Query();
        Graph g = getGraphWith( "blish wrote CIF; blish wrote VOR; hambly wrote Darwath; feynman mechanicked quanta" );
        q.addMatch( X, node("wrote"), Query.ANY );
        q.addConstraint( constraints );
        List bindings = ebList( g, q, justX ); 
        assertEquals( "testConstraint " + title + ": number of bindings", n, bindings.size() );
        }
        
    public void testConstraint()
        {
        helpConstraint( "none", Expression.TRUE, 3 );
        helpConstraint( "X /= blish", notEqual( X, node( "blish" ) ), 1 ); 
        helpConstraint( "X /= blish & X /= hambly", Dyadic.and( notEqual( X, node( "blish" ) ), notEqual( X, node( "hambly" ) ) ), 0 ); 
        }

    private void helpConstraintThree( String title, Expression c, int n )
        {           
        Query q = new Query();
        Graph g = getGraphWith( "brust wrote jhereg; hedgehog hacked code; angel age 230; brust wrote 230" );
        q.addConstraint( c );
        q.addMatch( X, Y, Z );
        List bindings = ebList( g, q, new Node [] {X, Z} );
        assertEquals( "testConstraint " + title + ": number of bindings", n, bindings.size() );         
        }
        
    public void testConstraintThree()
        {
        helpConstraintThree( "testConstraintThree 1:", areEqual( X, node( "brust" ) ), 2 );
        helpConstraintThree( "testConstraintThree 2:", areEqual( Y, node( "hacked" ) ), 1 );
        helpConstraintThree( "testConstraintThree 3:", areEqual( Z, node( "230" ) ), 2 );
       }
       
   public void testConstraintFour()
        {
        Query q = new Query();
        Graph g = getGraphWith( "bill pinged ben; ben pinged weed; weed pinged weed; bill ignored bill" );
        q.addMatch( X, node("pinged"), Y );
        q.addConstraint( notEqual( X, Y ) );
        Set bindings = iteratorToSet( eb( g, q, justX ).mapWith(getFirst) );
        assertEquals( arrayToSet( new Node[] {node("bill"), node("ben")} ), bindings );
        }
       
   /**
        Test that the MATCHES constraint works.
    */
   public void testMatchConstraint()
        {
        Set expected = CollectionFactory.createHashedSet();
        expected.add( node( "beta" ) );
        Query q = new Query()  
            .addMatch( X, node( "ppp" ), Y ).addConstraint( matches( Y, node( "'ell'" ) ) ) 
            ;
        Graph g = getGraphWith( "alpha ppp beta; beta ppp 'hello'; gamma ppp 'goodbye'" );
        Set bindings = iteratorToSet( eb( g, q, justX ).mapWith( getFirst ) ); 
        assertEquals( expected, bindings );
        }
        
    /**
        Test that a PatternStage extracts appropriate parts of a constraint set.
    */
    public void testExtractConstraint()
        {
            // Surely there should be something here?
        }
        
    public void testStringResults()
        {
        Graph g = getGraphWith( "ding dong dilly" );
        Query q = new Query() .addMatch( X, Y, Query.ANY );
        List bindings = ebList( g, q, new Node [] {X, Y} );
        assertEquals( "one result back by name", bindings.size(), 1 );
        assertEquals( "x = ding", ((Domain) bindings.get(0)).get(0), node("ding") );
        }
        
    /**
        this possible failure mode discovered by Andy when building a fast-path
        RDQL engine over the graph.query SPI.
    <br>
        test that we get a sensible result when unbound variables are used in the
        query result selector.
    */
    public void testMissingVariable()
        {
        Graph g = getGraphWith( "x y z" );
        List bindings = ebList( g, Q, new Node [] {X, Y} ); 
        List L = (List) bindings.get(0);
        assertEquals( "undefined variables get null", null, L.get( 0 ) );
        }
        
    /**
        More of an example than a test, for a query with "disconnected" triples.
    */
    public void testDisconnected()
        {
        Graph g = getGraphWith( "x pred1 foo; y pred2 bar" );
        Query q = new Query( getGraphWith( "?X ?? foo; ?Y ?? bar" ) );
        List bindings = ebList( g, q, nodeArray( "?X ?Y" ) );
        assertEquals( 1, bindings.size() );
        assertEquals( node( "x" ), ((Domain) bindings.get(0)).get(0) );
        assertEquals( node( "y" ), ((Domain) bindings.get(0)).get(1) );
        }
        
     /**
        Test that the default engine does not re-order triples.
     */
    public void testQueryTripleOrder()
        {
        Triple t1 = Triple.create( "A B C" ), t2 = Triple.create( "D E F" );
        List desired = Arrays.asList( new Triple[] {t1, t2} );
        List obtained = getTriplesFromQuery( desired );
        assertEquals( desired, obtained );
        }
        
    /**
        This horror to extract the order in which the triples are handed to
        patternStage illustrates that the Query code needs some refactoring
        to make it more testable.
        TODO make the Query code more testable. 
    */
    private List getTriplesFromQuery( List desired )
        {
        Query q = new Query();
        final Triple [][] tripleses = new Triple[1][];
        final Graph g = new GraphBase()
            {
            public ExtendedIterator graphBaseFind( TripleMatch tm )
                { return NullIterator.instance; }
            public QueryHandler queryHandler()
                {
                return new SimpleQueryHandler( this )
                    {
                    public Stage patternStage( Mapping map, ExpressionSet constraints, Triple [] t )
                        {
                        if (t.length > 1) tripleses[0] = t;
                        return super.patternStage( map, constraints, t );
                        }
                    }
                    ;
                }
            };
        for (int i = 0; i < desired.size(); i += 1) q.addMatch( (Triple) desired.get(i) );
        eb( g, q, none ); 
        return Arrays.asList( tripleses[0] );
        }
        
    /**
        test that we can correctly deduce the variable count for some queries.
     */
    public void testVariableCount()
        {
        assertCount( 0, "" );
        assertCount( 0, "x R y" );
        assertCount( 1, "?x R y" );
        assertCount( 1, "?x R y", "?x" );
        assertCount( 2, "?x R y", "?z" );
        assertCount( 1, "?x R ?x" );
        assertCount( 2, "?x R ?y" );
        assertCount( 3, "?x R ?y", "?z" );
        assertCount( 3, "?x ?R ?y" );
        assertCount( 6, "?x ?R ?y; ?a ?S ?c" );
        assertCount( 6, "?x ?R ?y; ?a ?S ?c", "?x" );
        assertCount( 6, "?x ?R ?y; ?a ?S ?c", "?x ?c" );
        assertCount( 6, "?x ?R ?y; ?a ?S ?c", "?x ?y ?c" );
        assertCount( 7, "?x ?R ?y; ?a ?S ?c", "?dog" );
        assertCount( 8, "?x ?R ?y; ?a ?S ?c", "?dog ?cat ?x" );
        assertCount( 18, "?a ?b ?c; ?d ?e ?f; ?g ?h ?i; ?j ?k ?l; ?m ?n ?o; ?p ?q ?r" );
        }
    
    public void assertCount( int expected, String query )
        { assertCount( expected, query, "" ); }
        
    public void assertCount( int expected, String query, String vars )
        {
        Graph g = getGraphWith( "" );
        Query q = new Query();
        Triple [] triples = tripleArray( query );
        for (int i = 0; i < triples.length; i += 1) q.addMatch( triples[i] );
        // eb( g, q, nodes( vars ) ); 
        q.executeBindings( g, nodeArray( vars ) );
        assertEquals( expected, q.getVariableCount() );
        }
        
    /**
        test that unbound constraint variables are handled "nicely".
    */  
    public void testQueryConstraintUnbound()
        {
        Query q = new Query()
            .addConstraint( notEqual( X, Z ) )
            .addMatch( X, Query.ANY, X )
            ;
        Graph g = getGraphWith( "x R x; x R y" );
        try
            {
            ExtendedIterator it = eb( g, q, justX );
            fail( "should spot unbound variable" );
            }
        catch (Query.UnboundVariableException b) { pass(); }
        } 
        
    public void testCloseQuery()
        {
        Graph g = getGraphWith( "x R y; a P b; i L j; d X f; h S g; no more heroes" );
        for (int n = 0; n < 1000; n += 1) graphAdd( g, "ping pong X" + n );
        Query q = new Query().addMatch( Query.S, Query.P, Query.O );
        List stages = new ArrayList();
        ExtendedIterator it = eb( g, q, nodeArray( "?P" ) ); 
        /* eat one answer to poke pipe */ it.next();
        for (int i = 0; i < stages.size(); i += 1) assertFalse( ((Stage) stages.get(i)).isClosed() );
        it.close();
        for (int i = 0; i < stages.size(); i += 1) assertTrue( ((Stage) stages.get(i)).isClosed() );
        }
        
    public void testRewriteStartswithExpression()
        {
        Query q = new Query();
        Expression L = constant( "x" );
        Expression R = createSimplePattern( "^begins" );
        Expression provided = dyadic( L, "Q_StringMatch", R );
        Expression desired = dyadic( L, "J_startsWith", constant( "begins" ) );
        q.addConstraint( provided );
        Expression e2 = (Expression) q.getConstraints().iterator().next();
        assertEquals( desired, e2 );
        }

    public void testRewriteStartswithInsensitiveExpression()
        {
        Query q = new Query();
        Expression L = constant( "x" );

⌨️ 快捷键说明

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