KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > graph > query > test > TestExpressionConstraints


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP, all rights reserved.
3   [See end of file]
4   $Id: TestExpressionConstraints.java,v 1.21 2005/02/21 11:52:33 andy_seaborne Exp $
5 */

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 /**
19     TestExpressionConstraints - test the "new" (as of September 2003) Constraint system
20     for Query.
21
22     @author kers
23 */

24 public class TestExpressionConstraints extends QueryTestBase
25     {
26     public TestExpressionConstraints( String JavaDoc 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( Z, ANY, ANY )
66
.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 JavaDoc x, int i ) { values.put( x, new Integer JavaDoc( i ) ); return this; }
105         
106         public int indexOf( String JavaDoc name ) { return ((Integer JavaDoc) 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 JavaDoc x ) { values.put( new Integer JavaDoc( i ), Node.createLiteral( x ) ); return this; }
114         
115         public Object JavaDoc get( int i ) { return values.get( new Integer JavaDoc( 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         // Valuator ep = e.prepare( map );
151
}
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     /**
175         check that an expression which does not admit to being a constant,
176         variable, or application, fails the "containsAllVariableOf" test [and hence will
177         not be evaluated early by the evaluator].
178     */

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 /*
190     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
191     All rights reserved.
192
193     Redistribution and use in source and binary forms, with or without
194     modification, are permitted provided that the following conditions
195     are met:
196
197     1. Redistributions of source code must retain the above copyright
198        notice, this list of conditions and the following disclaimer.
199
200     2. Redistributions in binary form must reproduce the above copyright
201        notice, this list of conditions and the following disclaimer in the
202        documentation and/or other materials provided with the distribution.
203
204     3. The name of the author may not be used to endorse or promote products
205        derived from this software without specific prior written permission.
206
207     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
208     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
209     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
210     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
211     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
212     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
213     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
214     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
215     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
216     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
217 */
Popular Tags