KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: QueryTestBase.java,v 1.5 2005/02/21 11:52:32 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.graph.query.test;
8
9 import java.util.List JavaDoc;
10
11 import com.hp.hpl.jena.graph.Node;
12 import com.hp.hpl.jena.graph.query.*;
13 import com.hp.hpl.jena.graph.test.GraphTestBase;
14 import com.hp.hpl.jena.util.iterator.Map1;
15
16 /**
17     Various things that are common to some of the query/expression
18     tests, so pulled up here into a shared superclass.
19     
20     @author hedgehog
21 */

22 public abstract class QueryTestBase extends GraphTestBase
23     {
24     public QueryTestBase( String JavaDoc name )
25         { super( name ); }
26     
27     /**
28         An expression that is true if x and y differ. Variable nodes
29         are treated as variables, non-variable nodes as constants.
30     */

31     protected Expression notEqual( Node x, Node y )
32         {
33         return new Dyadic( asExpression( x ), "http://jena.hpl.hp.com/constraints/NE", asExpression( y ) )
34             {
35             public boolean evalBool( Object JavaDoc x, Object JavaDoc y )
36                 { return !x.equals( y ); }
37             };
38         }
39         
40     /**
41         An expression that is true if x and y are equal. Variable nodes
42         are treated as variables, non-variable nodes as constants.
43     */

44     protected Expression areEqual( Node x, Node y )
45         {
46         return new Dyadic( asExpression( x ), "http://jena.hpl.hp.com/constraints/EQ", asExpression( y ) )
47             {
48             public boolean evalBool( Object JavaDoc x, Object JavaDoc y )
49                 { return x.equals( y ); }
50             };
51         }
52         
53     /**
54         An expression that is true if x and y "match", in the sense
55         that x contains y. Variable nodes are treated as variables,
56         non-variable nodes as constants.
57     */

58     protected Expression matches( Node x, Node y )
59         {
60         return new Dyadic( asExpression( x ), "http://jena.hpl.hp.com/constraints/MATCHES", asExpression( y ) )
61             {
62             public boolean evalBool( Object JavaDoc L, Object JavaDoc R )
63                 {
64                 Node l = (Node) L, r = (Node) R;
65                 return l.toString( false ).indexOf( r.toString( false ) ) > -1;
66                 }
67             };
68         }
69
70     /**
71         Answer an expression that evaluates the node <code>x</code>,
72         treating variable nodes as variables (<i>quelle surprise</i>) and other
73         nodes as constants.
74     */

75     public static Expression asExpression( final Node x )
76         {
77         if( x.isVariable()) return new Expression.Variable()
78             {
79             public String JavaDoc getName()
80                 { return x.getName(); }
81         
82             public Valuator prepare( VariableIndexes vi )
83                 { return new SlotValuator( vi.indexOf( x.getName() ) ); }
84             };
85         return new Expression.Fixed( x );
86         }
87
88     /**
89         A Map1 (suitable for a .mapWith iterator conversion) which
90         assumes the elements are lists and extracts their first elements.
91     */

92     protected static Map1 getFirst = new Map1()
93         { public Object JavaDoc map1( Object JavaDoc x ) { return ((List JavaDoc) x).get(0); } };
94     
95     /**
96        An IndexValues with no elements - ever slot maps to null
97     */

98     protected static final IndexValues noIVs = new IndexValues()
99         { public Object JavaDoc get( int i ) { return null; } };
100
101     /**
102          A mapping with no elements pre-defined
103     */

104     protected static final Mapping emptyMapping = new Mapping( new Node[0] );
105
106     /**
107         A mapping from variables to their indexes where no variables
108         are defined (all names map to the non-existant slot -1).
109      */

110     protected VariableIndexes noVariables = new VariableIndexes()
111         { public int indexOf( String JavaDoc name ) { return -1; } };
112         
113     /**
114         A Node with spelling "X".
115     */

116     protected static final Node X = Query.X;
117
118     /**
119         A Node with spelling "Y".
120     */

121     protected static final Node Y = Query.Y;
122     
123     /**
124         A Node with spelling "Z".
125     */

126     protected static final Node Z = Query.Z;
127
128     /**
129         A convenient way to refer to Node.ANY
130     */

131     protected static final Node ANY = Node.ANY;
132     
133     /**
134         An array containing just the node X.
135     */

136     protected final Node [] justX = new Node [] {X};
137     }
138
139
140 /*
141     (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP
142     All rights reserved.
143     
144     Redistribution and use in source and binary forms, with or without
145     modification, are permitted provided that the following conditions
146     are met:
147     
148     1. Redistributions of source code must retain the above copyright
149        notice, this list of conditions and the following disclaimer.
150     
151     2. Redistributions in binary form must reproduce the above copyright
152        notice, this list of conditions and the following disclaimer in the
153        documentation and/or other materials provided with the distribution.
154     
155     3. The name of the author may not be used to endorse or promote products
156        derived from this software without specific prior written permission.
157     
158     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
159     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
160     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
161     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
162     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
163     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
164     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
165     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
166     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
167     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
168 */
Popular Tags