KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > db > test > AbstractTestQuery1


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: AbstractTestQuery1.java,v 1.6 2005/02/21 12:03:14 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.db.test;
8
9 import com.hp.hpl.jena.graph.*;
10 import com.hp.hpl.jena.graph.test.*;
11 import com.hp.hpl.jena.graph.query.*;
12 import com.hp.hpl.jena.shared.*;
13 import com.hp.hpl.jena.util.iterator.*;
14 import com.hp.hpl.jena.graph.impl.*;
15 import com.hp.hpl.jena.vocabulary.RDF;
16
17
18 import java.util.*;
19 import junit.framework.*;
20
21 /**
22     Abstract tests for graph query, parameterised on getGraph().
23     @author kers
24 */

25 public abstract class AbstractTestQuery1 extends GraphTestBase
26     {
27     public AbstractTestQuery1(String JavaDoc name)
28         { super(name); }
29     
30     public static TestSuite suite()
31         { return new TestSuite( AbstractTestQuery1.class ); }
32
33     public abstract Graph getGraph();
34     public abstract Graph getGraph( ReificationStyle style );
35
36     
37     // in the stmt strings below, L indicates a long (object or predicate)
38
// L, if specifed, MUST precede the URI. for the object, a prefix of
39
// U indicates a reference (URI). otherwise, the object is considered
40
// a literal string. if the string begins with R, it is treated as
41
// a reified statement with 4 tokens, where the first is the URI that
42
// reifies the subsequent statement.
43

44     // the database has the following pattern.
45

46     protected String JavaDoc[] statementList = {
47         
48         // metadata about properties
49

50         "Pname Pcard O1",
51         "Psex Pcard O1",
52         "Pdept Pcardmax O5",
53         "Pmgr Pcardmax O1",
54         "Ptitle Pcardmax O5",
55         
56         "Pname Prange Oliteral",
57         "Psex Prange Oliteral",
58         "Pdept Prange Oliteral",
59         "Pmgr Prange Oresource",
60         "Ptitle Prange Oliteral",
61
62         "S1 Pname Omm",
63         "S1 Psex Omale",
64         "S1 Pdept Ojena",
65         "S1 Pdept Oapp",
66         "S1 Pmgr US4",
67         "S1 Ptitle Oboss",
68         
69         "S2 Pname Obb",
70         "S2 Psex Omale",
71         "S2 Pdept Ogenesis",
72         "S2 Pdept Oapp",
73         "S2 Pmgr US4",
74         "S2 Ptitle Oboss",
75
76         "S3 Pname Ojb",
77         "S3 Psex Ofemale",
78         "S3 Pdept Oapp",
79         "S3 Pmgr US1",
80         "S3 Ptitle Oboss",
81         
82         "S4 Pname Odick",
83         "S4 Psex Omale",
84         "S4 Pmgr Oshane",
85         "S4 Ptitle Obigboss",
86     
87         "S5 Pname Okers",
88         "S5 Psex Omale",
89         "S5 Pdept Ojena",
90         "S5 Pdept Ordql",
91         "S5 Pmgr US1",
92         "S5 Ptitle Ogrunt",
93             
94         "S6 Pname Ojjc",
95         "S6 Psex Omale",
96         "S6 Pdept Ojena",
97         "S6 Pdept Ordf",
98         "S6 Pdept Oowl",
99         "S6 Pmgr US1",
100         "S6 Ptitle Ogrunt",
101         
102         "S7 Pname Oder",
103         "S7 Psex Omale",
104         "S7 Pdept Ojena",
105         "S7 Pdept Ordf",
106         "S7 Pdept Oowl",
107         "S7 Pmgr US1",
108         "S7 Ptitle Ogrunt",
109         "S7 Ptitle Oboss",
110         
111         "S8 Pname Obmk",
112         "S8 Psex Omale",
113         "S8 Pdept Ojena",
114         "S8 Pdept Ordf",
115         "S8 Pmgr US1",
116         "S8 Ptitle Ogrunt",
117         "S8 Ptitle Oboss",
118         
119         "S9 Pname Oas",
120         "S9 Psex Omale",
121         "S9 Pdept Ojena",
122         "S9 Pdept Ordf",
123         "S9 Pdept Ordql",
124         "S9 Pmgr US1",
125         "S9 Ptitle Ogrunt",
126         
127         "S10 Pname Oian",
128         "S10 Psex Omale",
129         "S10 Pdept Ojena",
130         "S10 Pdept Oowl",
131         "S10 Pmgr US1",
132         "S10 Ptitle Ogrunt",
133
134         "S11 Pname Osteveb",
135         "S11 Psex Omale",
136         "S11 Pdept Oapp",
137         "S11 Pmgr US3",
138         "S11 Ptitle Ogrunt",
139
140         "S12 Pname Ostevec",
141         "S12 Psex Omale",
142         "S12 Pdept Oapp",
143         "S12 Pmgr US3",
144         "S12 Ptitle Ogrunt",
145
146         "S13 Pname Ocs",
147         "S13 Psex Omale",
148         "S13 Pdept Ojena",
149         "S13 Pdept Ogenesis",
150         "S13 Pmgr US2",
151         "S13 Ptitle Ogrunt",
152         
153         "S14 Pname Ohk",
154         "S14 Psex Ofemale",
155         "S14 Pdept Oapp",
156         "S14 Pdept Ogenesis",
157         "S14 Pmgr US2",
158         "S14 Ptitle Ogrunt",
159
160         "S15 Pname Oks",
161         "S15 Psex Omale",
162         "S15 Pdept Oapp",
163         "S15 Pdept Ogenesis",
164         "S15 Pmgr US2",
165
166         "S16 Pname Owkw",
167         "S16 Psex Omale",
168         "S16 Pdept Ojena",
169         "S16 Pdept Oapp",
170         "S16 Pmgr US2",
171         "S16 Ptitle Ogrunt",
172         
173         // reify the first few sets of statements
174

175         "N1 S1 Pname Omm",
176         "N2 S1 Psex Omale",
177         "N3 S1 Pdept Ojena",
178         "N4 S1 Pdept Oapp",
179         "N5 S1 Pmgr US4",
180         "N6 S1 Ptitle Oboss",
181         
182         "N7 S2 Pname Obb",
183         "N8 S2 Psex Omale",
184         "N9 S2 Pdept Ogenesis",
185         "N10 S2 Pdept Oapp",
186         "N11 S2 Pmgr US4",
187         "N12 S2 Ptitle Oboss",
188
189         "N13 S3 Pname Ojb",
190         "N14 S3 Psex Ofemale",
191         "N15 S3 Pdept Oapp",
192         "N16 S3 Pmgr US1",
193         "N17 S3 Ptitle Oboss",
194         
195         "N18 S4 Pname Odick",
196         "N19 S4 Psex Omale",
197         "N20 S4 Pmgr Oshane",
198         "N21 S4 Ptitle Obigboss",
199     
200         "N22 S5 Pname Okers",
201         "N23 S5 Psex Omale",
202         "N24 S5 Pdept Ojena",
203         "N25 S5 Pdept Ordql",
204         "N26 S5 Pmgr US1",
205         "N27 S5 Ptitle Ogrunt",
206
207     };
208     
209     public void setUp() throws Exception JavaDoc
210         {
211         super.setUp();
212         }
213         
214     protected Graph standard()
215         { return fetchGraph( ReificationStyle.Standard ); }
216         
217      protected Graph convenient()
218         { return fetchGraph( ReificationStyle.Convenient ); }
219         
220     protected Graph fetchGraph( ReificationStyle style )
221         { Graph s = getGraph( style );
222         loadGraph( s );
223         return s; }
224  
225     protected void tearDown() throws Exception JavaDoc {
226         super.tearDown();
227         }
228
229     /**
230         The lots-of-dots prefix to use to make things long
231     */

232     protected final String JavaDoc longPrefix = makeLongPrefix();
233     
234     /**
235         Answer a string of 256 dots.
236     */

237     private String JavaDoc makeLongPrefix()
238         { StringBuffer JavaDoc sb = new StringBuffer JavaDoc( 256 );
239         for (int i = 0; i < 256; i += 1) sb.append( '.' );
240         return sb.toString(); }
241     
242     protected Node makeResource ( String JavaDoc u )
243         { return Node.createURI( expandLong( u ) ); }
244     
245     protected Node makeObject ( String JavaDoc u ) {
246         boolean isRef = u.charAt(0) == 'U';
247         return
248             isRef ? makeResource( u.substring(1) )
249             : Node.createLiteral( new LiteralLabel( expandLong( u ) ) );
250     }
251
252     protected String JavaDoc expandLong( String JavaDoc s )
253         { return s.charAt(0) == 'L' ? longPrefix + s.substring(1) : s; }
254     
255     static int stmtCnt = 0;
256     
257     protected void loadGraph ( Graph g ) {
258         Reifier r = g.getReifier();
259         for (int i = 0; i < statementList.length; i++) {
260             StringTokenizer st = new StringTokenizer( statementList[i] );
261             String JavaDoc k = st.nextToken();
262             if ( k.charAt(0) == 'N' )
263                 r.reifyAs( makeResource( k ), nextTriple( st.nextToken(), st ) );
264             else
265                 g.add( nextTriple( k, st ) );
266             stmtCnt++;
267         }
268     }
269     
270     protected Triple nextTriple( String JavaDoc k, StringTokenizer st )
271         { Node s = makeResource( k );
272         Node p = makeResource( st.nextToken() );
273         Node o = makeObject( st.nextToken() );
274         return Triple.create( s, p, o ); }
275
276     final Node V1 = node( "?v1" );
277     final Node V2 = node( "?v2" );
278     final Node V3 = node( "?v3" );
279     final Node V4 = node( "?v4" );
280     final Node V5 = node( "?v5" );
281
282     final Node Ptitle = makeResource("Ptitle");
283     final Node Psex = makeResource("Psex");
284     final Node Pname = makeResource("Pname");
285     final Node Pmgr = makeResource("Pmgr");
286     final Node Pcard = makeResource("Pcard");
287     final Node Pcardmax = makeResource("Pcardmax");
288     final Node Prange = makeResource("Prange");
289     final Node Pdept = makeResource("Pdept");
290     final Node S1 = makeResource("S1");
291     
292         // object constants
293
final Node Ogrunt = makeObject("Ogrunt");
294     final Node Ofemale = makeObject("Ofemale");
295     final Node Omale = makeObject("Omale");
296     final Node Obigboss = makeObject("Obigboss");
297     final Node Oboss = makeObject("Oboss");
298     final Node Oshane = makeObject("Oshane");
299     final Node Oliteral = makeObject("Oliteral");
300     final Node Oresource = makeObject("Oresource");
301     final Node Oapp = makeObject("Oapp");
302     final Node Ogenesis = makeObject("Ogenesis");
303
304     final Node O1 = makeObject("O1");
305              
306     public void test0()
307         {
308         Query query = new Query();
309         query.addMatch( V1, Ptitle, Ogrunt );
310         query.addMatch( V1, Psex, Ofemale );
311         query.addMatch( V1, Pname, V3 );
312         checkCount( 1, standard(), query, new Node[] {V1,V3} );
313         }
314         
315     /**
316         Q1: get names of managers of female grunts; this has a joining variable.
317     */

318     public void test1()
319         {
320         Query query = new Query();
321         query.addMatch( V1, Ptitle, Ogrunt );
322         query.addMatch( V1, Psex, Ofemale );
323         query.addMatch( V1, Pmgr, V2 );
324         query.addMatch( V2, Pname, V3 );
325         checkCount( 1, standard(), query, new Node[] {V1,V3} );
326         }
327         
328     /**
329         Q2: get names of female grunts with female managers
330     */

331     public void test2()
332         {
333         Query query = new Query();
334         query.addMatch( V1, Ptitle, Ogrunt );
335         query.addMatch( V1, Psex, Ofemale );
336         query.addMatch( V1, Pmgr, V2 );
337         query.addMatch( V2, Psex, Ofemale );
338         query.addMatch( V1, Pname, V3 );
339         checkCount( 0, standard(), query, new Node[] {V1,V3} );
340         }
341         
342     /**
343         Q3.0: get all properties of the bigboss
344     */

345     public void test3a()
346         {
347         Query query = new Query();
348         query.addMatch( V1, Ptitle, Obigboss );
349         query.addMatch( V1, Pmgr, Oshane );
350         query.addMatch( V1, V2, V3 );
351         checkCount( 4, standard(), query, new Node[] {V1,V2,V3} );
352         }
353         
354     /**
355         Q3: get all properties of female grunts with male managers
356         this has a predicate variable. for standard reification, it
357         requires a multi-stage query. for convenient, minimal, it can
358         be done as a single stage (since reification is not queried).
359     */

360     public void test3b()
361         {
362         Query query = new Query();
363         query.addMatch( V1, Ptitle, Ogrunt );
364         query.addMatch( V1, Psex, Ofemale );
365         query.addMatch( V1, Pmgr, V2 );
366         query.addMatch( V2, Psex, Omale );
367         query.addMatch( V1, V3, V4 );
368         checkCount( 6, standard(), query, new Node[] {V1,V3,V4} );
369         }
370          
371     /**
372         Q4: get all single-valued, required, literal properties of the bigboss
373         similar to Q3 in terms of stages.
374     */

375     public void test4()
376         {
377         Query query = new Query();
378         query.addMatch( V1, Ptitle, Obigboss );
379         query.addMatch( V1, Pmgr, Oshane );
380         query.addMatch( V2, Pcard, O1 );
381         query.addMatch( V2, Prange, Oliteral );
382         query.addMatch( V1, V2, V3 );
383         checkCount( 2, standard(), query, new Node[] {V2,V3} );
384         }
385         
386     /**
387         Q5: list the name and gender of martin's boss, where the pmgr property
388         is determined by a query).
389         similar to Q3 in terms of stages.
390     */

391     public void test5()
392         {
393         Query query = new Query();
394         query.addMatch( V1, Pcardmax, O1 ); // get the mgr property
395
query.addMatch( V1, Prange, Oresource );
396         query.addMatch( S1, V1, V2 ); // get mm's mgr
397
query.addMatch( V2, Pname, V3 );
398         query.addMatch( V2, Psex, V4 );
399         checkCount( 1, standard(), query, new Node[] {V2,V3,V4} );
400         }
401         
402     /**
403         Q6: list the reified subjects, predicates and objects.
404         should return nothing for minimal, convenient reification.
405     */

406     public void test6()
407         {
408         Query query = new Query();
409         query.addMatch( V1, RDF.Nodes.subject, V2 );
410         query.addMatch( V1, RDF.Nodes.predicate, V3 );
411         query.addMatch( V1, RDF.Nodes.object, V4 );
412     /* */
413         checkCount( 27, standard(), query, new Node[] {V2,V3,V4} );
414         checkCount( 0, convenient(), query, new Node[] {V2,V3,V4} );
415         }
416         
417     /**
418         Q7: list the reified predicates about the bigboss.
419         should return nothing for minimal, convenient reification.
420     */

421     public void test7()
422         {
423         Query query = new Query();
424         query.addMatch( V1, RDF.Nodes.subject, V2 );
425         query.addMatch( V1, RDF.Nodes.predicate, Ptitle );
426         query.addMatch( V1, RDF.Nodes.object, Obigboss );
427         query.addMatch( V3, RDF.Nodes.subject, V2 );
428         query.addMatch( V3, RDF.Nodes.predicate, V4 );
429     /* */
430         checkCount( 4, standard(), query, new Node[] {V2,V3} );
431         }
432         
433     /**
434         Q8: list the reification quads for the bigboss.
435         should return nothing for minimal, convenient reification.
436     */

437     public void test8()
438         {
439         Query query = new Query();
440         query.addMatch( V1, RDF.Nodes.subject, V2 );
441         query.addMatch( V1, RDF.Nodes.predicate, Ptitle );
442         query.addMatch( V1, RDF.Nodes.object, Obigboss );
443         query.addMatch( V3, RDF.Nodes.subject, V2 );
444         query.addMatch( V3, V4, V5 ); // V4 and V5 serve duty as ANY
445
checkCount( 16, standard(), query, new Node[] {V3} );
446         }
447         
448     /**
449         Check that the number of results obtained from the query over the graph is
450         that expected.
451         
452         @param expected the number of results expected from the query
453         @param g the graph to run the query over
454         @param q the query to apply to the graph
455         @param results the results-variable array
456      */

457     private void checkCount( int expected, Graph g, Query q, Node [] results )
458        {
459        BindingQueryPlan plan = g.queryHandler().prepareBindings( q, results );
460        ExtendedIterator it = plan.executeBindings();
461        assertEquals( "number of reified statements", expected, queryResultCount( it ) );
462        it.close();
463        }
464     
465     /**
466         Answer the number of elements in the iterator; each such element should be
467         a List (and we make sure size() works on it, don't know why).
468         
469         @param it the iterator to run down
470         @return the number of elements in that iterator
471      */

472     protected int queryResultCount( ExtendedIterator it ) {
473         int n = 0;
474         while (it.hasNext()) {
475             n++;
476             ((List) it.next()).size(); // hedgehog asks, do we need to check this works?
477
}
478         return n;
479     }
480
481     }
482
483
484 /*
485     (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
486     All rights reserved.
487
488     Redistribution and use in source and binary forms, with or without
489     modification, are permitted provided that the following conditions
490     are met:
491
492     1. Redistributions of source code must retain the above copyright
493        notice, this list of conditions and the following disclaimer.
494
495     2. Redistributions in binary form must reproduce the above copyright
496        notice, this list of conditions and the following disclaimer in the
497        documentation and/or other materials provided with the distribution.
498
499     3. The name of the author may not be used to endorse or promote products
500        derived from this software without specific prior written permission.
501
502     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
503     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
504     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
505     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
506     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
507     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
508     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
509     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
510     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
511     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
512 */
Popular Tags