📄 abstracttestquery.java
字号:
Expression R = createModifiedPattern( "^begins", "i" );
Expression provided = dyadic( L, "Q_StringMatch", R );
Expression desired = dyadic( L, "J_startsWithInsensitive", constant( "begins" ) );
q.addConstraint( provided );
Expression e2 = (Expression) q.getConstraints().iterator().next();
assertEquals( desired, e2 );
}
public void testRewriteEndswithExpression()
{
Query q = new Query();
Expression L = constant( "x" );
Expression R = createSimplePattern( "ends$" );
Expression provided = dyadic( L, "Q_StringMatch", R );
Expression desired = dyadic( L, "J_endsWith", constant( "ends" ) );
q.addConstraint( provided );
Expression e2 = (Expression) q.getConstraints().iterator().next();
assertEquals( desired, e2 );
}
public void testRewriteEndswithInsensitiveExpression()
{
Query q = new Query();
Expression L = constant( "x" );
Expression R = createModifiedPattern( "ends$", "i" );
Expression provided = dyadic( L, "Q_StringMatch", R );
Expression desired = dyadic( L, "J_endsWithInsensitive", constant( "ends" ) );
q.addConstraint( provided );
Expression e2 = (Expression) q.getConstraints().iterator().next();
assertEquals( desired, e2 );
}
public void testRewriteContainsExpression()
{
Query q = new Query();
Expression L = constant( "x" );
Expression R = createSimplePattern( "contains" );
Expression provided = dyadic( L, "Q_StringMatch", R );
Expression desired = dyadic( L, "J_contains", constant( "contains" ) );
q.addConstraint( provided );
Expression e2 = (Expression) q.getConstraints().iterator().next();
assertEquals( desired, e2 );
}
public void testRewritePreservesCharacterCases()
{
Query q = new Query();
Expression L = constant( "x" );
Expression R = createModifiedPattern( "coNtaIns", "i" );
Expression provided = dyadic( L, "Q_StringMatch", R );
Expression desired = dyadic( L, "J_containsInsensitive", constant( "coNtaIns" ) );
q.addConstraint( provided );
Expression e2 = (Expression) q.getConstraints().iterator().next();
assertEquals( desired, e2 );
}
protected static class BangException extends JenaException
{
public BangException() { super( "bang!" ); }
}
public void testQueryExceptionCleanlyExits()
{
Query q = new Query().addMatch( Triple.ANY );
Graph g = new GraphBase()
{
protected ExtendedIterator graphBaseFind( TripleMatch m )
{ throw new BangException(); }
};
ExtendedIterator it = eb( g, q, new Node[] {} );
try { it.next(); fail( "should fail because graph explodes" ); }
catch (QueryStageException e) { assertTrue( e.getCause() instanceof BangException ); }
catch (Exception e) { fail( "should throw QueryStageException" ); }
}
protected static class PL extends Expression.Fixed implements PatternLiteral
{
protected String modifiers = "";
public PL( String content ) { super( content ); }
public PL( String content, String modifiers ) { super( content ); this.modifiers = modifiers; }
public String getPatternString() { return (String) value; }
public String getPatternModifiers() { return modifiers; }
public String getPatternLanguage() { return rdql; }
}
public Expression createSimplePattern( final String p )
{ return new PL( p ); }
public Expression createModifiedPattern( String content, String modifiers )
{ return new PL( content, modifiers ); }
private Expression constant( final Object it )
{ return new Expression.Fixed( it ); }
private Expression dyadic( Expression l, String op, Expression r )
{
final String f = ExpressionFunctionURIs.prefix + op;
return new Dyadic( l, f, r )
{
public boolean evalBool( Object l, Object r )
{ return false; }
};
}
/**
Test that a variety of triple-sorters make no difference to the results of a query
over a moderately interesting graph.
*/
public void testTripleSorting()
{
Graph g = dataGraph();
Map answer = getAnswer( g, TripleSorter.dontSort );
assertEquals( 1, answer.size() );
assertEquals( new Integer(1), answer.get( Arrays.asList( nodeArray( "a d" ) ) ) );
/* */
assertEquals( answer, getAnswer( g, TripleSorter.dontSort ) );
assertEquals( answer, getAnswer( g, fiddle( 0, 2, 1 ) ) );
assertEquals( answer, getAnswer( g, fiddle( 1, 0, 2 ) ) );
assertEquals( answer, getAnswer( g, fiddle( 1, 2, 0 ) ) );
assertEquals( answer, getAnswer( g, fiddle( 2, 1, 0 ) ) );
assertEquals( answer, getAnswer( g, fiddle( 2, 0, 1 ) ) );
}
protected TripleSorter fiddle( final int a, final int b, final int c )
{
return new TripleSorter()
{
public Triple [] sort( Triple [] triples )
{ return new Triple[] {triples[a], triples[b], triples[c]}; }
};
}
protected Graph dataGraph()
{
Graph result = getGraph();
graphAdd( result, "a SPOO d; a X b; b Y c" );
return result;
}
protected Map getAnswer( Graph g, TripleSorter sorter )
{
Map result = CollectionFactory.createHashedMap();
Query q = new Query();
q.addMatch( triple( "?a ?? ?d " ) ).addMatch( triple( "?a X ?b" ) ).addMatch( triple( "?b Y ?c" ) );
q.addConstraint( notEqual( node( "?d" ), node( "?b" ) ) );
Node [] answers = nodeArray( "?a ?d" );
q.setTripleSorter( sorter );
ExtendedIterator it = eb( g, q, answers );
while (it.hasNext()) addAnswer( result, (List) it.next(), answers.length );
return result;
}
protected void addAnswer( Map result, List bindings, int limit )
{
List key = bindings.subList( 0, limit );
Integer already = (Integer) result.get( key );
if (already == null) already = new Integer( 0 );
result.put( key, new Integer( already.intValue() + 1 ) );
}
public void testQueryOptimisation()
{
int dontCount = queryCount( TripleSorter.dontSort );
int optimCount = queryCount( new SimpleTripleSorter() );
// System.err.println( ">> dontCount=" + dontCount + " optimCount=" + optimCount );
if (optimCount > dontCount)
fail( "optimisation " + optimCount + " yet plain " + dontCount );
}
public void testFixedTypedLiterals()
{
Graph g = getGraphWith( "a P 'value'xsd:string; b P 'value'xsd:nosuch" );
if (g.getCapabilities().handlesLiteralTyping())
{
Query q = new Query()
.addMatch( Query.S, Query.P, node( "'value'" ) );
ExtendedIterator it = q.executeBindings( g, new Node[] {Query.S, Query.P} );
assertEquals( nodeSet( "a" ), iteratorToSet( it.mapWith( select(0) ) ) );
}
}
public void testBoundTypedLiterals()
{
Graph g = getGraphWith( "a P 'value'xsd:string; b V 'value'" );
if (g.getCapabilities().handlesLiteralTyping())
{
Query q = new Query()
.addMatch( node( "b" ), node( "V" ), Query.X )
.addMatch( Query.S, node( "P" ), Query.X );
ExtendedIterator it = q.executeBindings( g, new Node[] {Query.S, Query.P} );
assertEquals( nodeSet( "a" ), iteratorToSet( it.mapWith( select(0) ) ) );
}
}
int queryCount( TripleSorter sort )
{
CountingGraph g = bigCountingGraph();
for (int a = 0; a < 10; a += 1)
for (int b = 0; b < 10; b += 1)
for (int X = 0; X < 3; X += 1)
graphAdd( g, "a" + a + " X" + (X == 0 ? "" : X + "") + " b" + b );
graphAdd( g, "a SPOO d; a X b; b Y c" );
getAnswer( g, sort );
return g.getCount();
}
static class CountingGraph extends WrappedGraph
{
int counter;
private QueryHandler qh;
public QueryHandler queryHandler( ) { return qh; }
CountingGraph( Graph base )
{ super( base ); qh = new SimpleQueryHandler( this ); }
public ExtendedIterator find( Node s, Node p, Node o )
{ return find( Triple.createMatch( s, p, o ) ); }
public ExtendedIterator find( TripleMatch tm )
{ return count( base.find( tm ) ); }
ExtendedIterator count( ExtendedIterator it )
{
return new WrappedIterator( it )
{
public Object next() { try { return super.next(); } finally { counter += 1; } }
};
}
int getCount()
{ return counter; }
public String toString()
{ return base.toString(); }
}
CountingGraph bigCountingGraph()
{
Graph bigGraph = getGraph();
return new CountingGraph( bigGraph );
}
}
/*
(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 + -