1 6 7 package com.hp.hpl.jena.graph.query.test; 8 9 import com.hp.hpl.jena.graph.*; 10 import com.hp.hpl.jena.graph.query.*; 11 import com.hp.hpl.jena.graph.query.Expression.Util; 12 13 import com.hp.hpl.jena.util.CollectionFactory; 14 15 import junit.framework.*; 16 import java.util.*; 17 18 24 public class TestExpressionConstraints extends QueryTestBase 25 { 26 public TestExpressionConstraints( String name ) 27 { super( name ); } 28 29 public static TestSuite suite() 30 { return new TestSuite( TestExpressionConstraints.class ); } 31 32 protected static final Expression eTRUE = Expression.TRUE; 33 protected static final Expression eFALSE = Expression.FALSE; 34 35 public void testConstraintFALSE() 36 { 37 Graph g = graphWith( "x R y; a P b" ); 38 Query q = new Query().addMatch( X, ANY, ANY ).addConstraint( eFALSE ); 39 assertFalse( q.executeBindings( g, justX ).hasNext() ); 40 } 41 42 public void testConstraintTRUE() 43 { 44 Graph g = graphWith( "x R y; a P b" ); 45 Query q = new Query().addMatch( X, ANY, ANY ).addConstraint( eTRUE ); 46 assertTrue( q.executeBindings( g, justX ).hasNext() ); 47 } 48 49 public void testConstraintNE1() 50 { 51 Graph g = graphWith( "x R y; a P a" ); 52 Query q = new Query() 53 .addMatch( X, ANY, Y ) 54 .addConstraint( notEqual( X, Y ) ) 55 ; 56 Set expected = CollectionFactory.createHashedSet(); 57 expected.add( node( "x" ) ); 58 assertEquals( expected, iteratorToSet( q.executeBindings( g, justX ).mapWith( getFirst ) ) ); 59 } 60 61 public void testConstraintNE2() 62 { 63 Graph g = graphWith( "x R y; a P a" ); 64 Query q = new Query() 65 .addMatch( X, ANY, Y ) 67 .addConstraint( notEqual( X, Y ) ) 68 ; 69 Set expected = CollectionFactory.createHashedSet(); 70 expected.add( node( "x" ) ); 71 assertEquals( expected, iteratorToSet( q.executeBindings( g, justX ).mapWith( getFirst ) ) ); 72 } 73 74 public void testConstraintNE3() 75 { 76 Graph g = graphWith( "x R a; y P b; z Q c" ); 77 Query q = new Query() 78 .addMatch( X, ANY, ANY ) 79 .addConstraint( notEqual( X, node( "y" ) ) ) 80 ; 81 Set expected = CollectionFactory.createHashedSet(); 82 expected.add( node( "x" ) ); 83 expected.add( node( "z" ) ); 84 assertEquals( expected, iteratorToSet( q.executeBindings( g, justX ).mapWith( getFirst ) ) ); 85 } 86 87 public void testConstraintNE4() 88 { 89 Graph g = graphWith( "x R a; y P b; z Q c" ); 90 Query q = new Query() 91 .addMatch( X, ANY, ANY ) 92 .addConstraint( notEqual( X, node( "y" ) ) ) 93 .addConstraint( notEqual( X, node( "x" ) ) ) 94 ; 95 Set expected = CollectionFactory.createHashedSet(); 96 expected.add( node( "z" ) ); 97 assertEquals( expected, iteratorToSet( q.executeBindings( g, justX ).mapWith( getFirst ) ) ); 98 } 99 100 public static class VI implements VariableIndexes 101 { 102 private Map values = CollectionFactory.createHashedMap(); 103 104 public VI set( String x, int i ) { values.put( x, new Integer ( i ) ); return this; } 105 106 public int indexOf( String name ) { return ((Integer ) values.get( name )).intValue(); } 107 } 108 109 public static class IV implements IndexValues 110 { 111 private Map values = CollectionFactory.createHashedMap(); 112 113 public IV set( int i, String x ) { values.put( new Integer ( i ), Node.createLiteral( x ) ); return this; } 114 115 public Object get( int i ) { return values.get( new Integer ( i ) ); } 116 } 117 118 public void testVI() 119 { 120 VI varValues = new VI() .set( "X", 1 ).set( "Y", 2 ).set( "Z", 3 ); 121 assertEquals( 1, varValues.indexOf( "X" ) ); 122 assertEquals( 2, varValues.indexOf( "Y" ) ); 123 assertEquals( 3, varValues.indexOf( "Z" ) ); 124 } 125 126 public void testNE() 127 { 128 Expression e = areEqual( X, Y ); 129 VariableIndexes vi = new VI().set( "X", 1 ).set( "Y", 2 ); 130 IndexValues iv = new IV().set( 1, "something" ).set( 2, "else" ); 131 assertEquals( false, e.prepare( vi ).evalBool( iv ) ); 132 } 133 134 public void testVVTrue() 135 { assertEquals( true, Expression.TRUE.prepare( noVariables ).evalBool( noIVs ) ); } 136 137 public void testVVFalse() 138 { assertEquals( false, Expression.FALSE.prepare( noVariables ).evalBool( noIVs ) ); } 139 140 public void testVVMatches() 141 { VariableIndexes vi = new VI().set( "X", 0 ).set( "Y", 1 ); 142 IndexValues iv = new IV().set( 0, "hello" ).set( 1, "ell" ); 143 assertEquals( true, matches( X, Y ).prepare( vi ).evalBool( iv ) ); } 144 145 146 public void testPrepareNE() 147 { 148 Expression e = notEqual( X, Y ); 149 VariableIndexes map = new Mapping( new Node[0] ); 150 } 152 153 public void testURIs() 154 { 155 assertEquals( "http://jena.hpl.hp.com/constraints/NE", notEqual( X, Y ).getFun() ); 156 assertEquals( "http://jena.hpl.hp.com/constraints/EQ", areEqual( X, Y ).getFun() ); 157 assertEquals( "http://jena.hpl.hp.com/constraints/MATCHES", matches( X, Y ).getFun() ); 158 } 159 160 public void testNotConstant() 161 { 162 assertFalse( notEqual( X, Y ).isConstant() ); 163 } 164 165 public void testDetectAnd() 166 { 167 Expression e1 = notEqual( X, Y ), e2 = notEqual( X, Z ); 168 Query q = new Query().addConstraint( Dyadic.and( e1, e2 ) ); 169 Set eBoth = CollectionFactory.createHashedSet(); eBoth.add( e1 ); eBoth.add( e2 ); 170 Set s = iteratorToSet( q.getConstraints().iterator() ); 171 assertEquals( eBoth, s ); 172 } 173 174 179 public void testUnknownExpression() 180 { 181 Expression eOpaque = new Expression.Base() 182 { public Valuator prepare(VariableIndexes vi) 183 { return null; } 184 }; 185 assertFalse( Util.containsAllVariablesOf( CollectionFactory.createHashedSet(), eOpaque ) ); 186 } 187 } 188 189 | Popular Tags |