KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > reasoner > test > TestTransitiveGraphCache


1 /******************************************************************
2  * File: TestTransitiveGraphCacheNew.java
3  * Created by: Dave Reynolds
4  * Created on: 25-Nov-2004
5  *
6  * (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP
7  * [See end of file]
8  * $Id: TestTransitiveGraphCache.java,v 1.12 2005/02/21 12:18:17 andy_seaborne Exp $
9  *****************************************************************/

10
11 package com.hp.hpl.jena.reasoner.test;
12
13 import com.hp.hpl.jena.reasoner.transitiveReasoner.TransitiveGraphCache;
14 import com.hp.hpl.jena.reasoner.TriplePattern;
15 import com.hp.hpl.jena.graph.Node;
16 import com.hp.hpl.jena.graph.Triple;
17
18 import junit.framework.TestCase;
19 import junit.framework.TestSuite;
20
21 /**
22  * A purely temporary test suite just used during development and kept
23  * off the main unit test paths.
24  *
25  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
26  * @version $Revision: 1.12 $
27  */

28
29 public class TestTransitiveGraphCache extends TestCase {
30     
31     /** The cache under test */
32     TransitiveGraphCache cache;
33     
34     // Dummy predicates and nodes for the graph
35
String JavaDoc NS = "urn:x-hp-test:ex/";
36     Node directP = Node.createURI(NS+"directSubProperty");
37     Node closedP = Node.createURI(NS+"subProperty");
38     
39     Node a = Node.createURI(NS+"a");
40     Node b = Node.createURI(NS+"b");
41     Node c = Node.createURI(NS+"c");
42     Node d = Node.createURI(NS+"d");
43     Node e = Node.createURI(NS+"e");
44     Node f = Node.createURI(NS+"f");
45     Node g = Node.createURI(NS+"g");
46      
47     /**
48      * Boilerplate for junit
49      */

50     public TestTransitiveGraphCache( String JavaDoc name ) {
51         super( name );
52     }
53     
54     /**
55      * Boilerplate for junit.
56      * This is its own test suite
57      */

58     public static TestSuite suite() {
59         return new TestSuite( TestTransitiveGraphCache.class );
60 // TestSuite suite = new TestSuite();
61
// suite.addTest( new TestTransitiveGraphCacheNew("testBasicCache"));
62
// suite.addTest( new TestTransitiveGraphCacheNew("testRemove"));
63
// return suite;
64
}
65
66     /**
67      * Test the basic functioning a Transitive closure cache.
68      * Caches the graph but not the final closure.
69      */

70     public void testBasicCache() {
71         initCache();
72         cache.setCaching(false);
73         doBasicTest(cache);
74     }
75     
76     /**
77      * Test the basic functioning a Transitive closure cache.
78      * Caches the graph and any requested closures
79      */

80     public void testCachingCache() {
81         initCache();
82         cache.setCaching(true);
83         doBasicTest(cache);
84     }
85     
86     /**
87      * Test the clone operation
88      */

89     public void testCloning() {
90         initCache();
91         TransitiveGraphCache clone = cache.deepCopy();
92         // Mess with the original to check cloning
93
cache.addRelation(new Triple(a, closedP, d));
94         cache.addRelation(new Triple(g, closedP, a));
95         doBasicTest(clone);
96     }
97         
98     /**
99      * Initialize the cache with some test data
100      */

101     private void initCache() {
102         // Create a graph with reflexive references, cycles, redundant links
103
cache = new TransitiveGraphCache(directP, closedP);
104         cache.addRelation(new Triple(a, closedP, b));
105         cache.addRelation(new Triple(b, closedP, e));
106         cache.addRelation(new Triple(b, closedP, c));
107         cache.addRelation(new Triple(e, closedP, f));
108         cache.addRelation(new Triple(c, closedP, f));
109         cache.addRelation(new Triple(f, closedP, g));
110         cache.addRelation(new Triple(d, closedP, c));
111         cache.addRelation(new Triple(d, closedP, e));
112         cache.addRelation(new Triple(d, closedP, g)); // reduntant two ways
113
cache.addRelation(new Triple(a, closedP, e)); // redundant
114
cache.addRelation(new Triple(d, closedP, b)); // Makes both earlier d's redundant
115

116         cache.addRelation(new Triple(a, closedP, a));
117         cache.addRelation(new Triple(b, closedP, b));
118         cache.addRelation(new Triple(c, closedP, c));
119         cache.addRelation(new Triple(d, closedP, d));
120         cache.addRelation(new Triple(e, closedP, e));
121         cache.addRelation(new Triple(f, closedP, f));
122         cache.addRelation(new Triple(g, closedP, g));
123     }
124             
125     public void doBasicTest(TransitiveGraphCache cache) {
126          // Test forward property patterns
127
TestUtil.assertIteratorValues(this,
128             cache.find(new TriplePattern(a, directP, null)),
129             new Object JavaDoc[] {
130                 new Triple(a, closedP, a),
131                 new Triple(a, closedP, b)
132             });
133         TestUtil.assertIteratorValues(this,
134             cache.find(new TriplePattern(a, closedP, null)),
135             new Object JavaDoc[] {
136                 new Triple(a, closedP, a),
137                 new Triple(a, closedP, b),
138                 new Triple(a, closedP, c),
139                 new Triple(a, closedP, e),
140                 new Triple(a, closedP, f),
141                 new Triple(a, closedP, g)
142             });
143         TestUtil.assertIteratorValues(this,
144             cache.find(new TriplePattern(a, closedP, g)),
145             new Object JavaDoc[] {
146                 new Triple(a, closedP, g),
147             });
148             
149         // Test backward patterns
150
TestUtil.assertIteratorValues(this,
151             cache.find(new TriplePattern(null, directP, f)),
152             new Object JavaDoc[] {
153                 new Triple(e, closedP, f),
154                 new Triple(f, closedP, f),
155                 new Triple(c, closedP, f)
156             });
157         TestUtil.assertIteratorValues(this,
158             cache.find(new TriplePattern(null, closedP, f)),
159             new Object JavaDoc[] {
160                 new Triple(f, closedP, f),
161                 new Triple(e, closedP, f),
162                 new Triple(b, closedP, f),
163                 new Triple(c, closedP, f),
164                 new Triple(a, closedP, f),
165                 new Triple(d, closedP, f)
166             });
167         
168         // List all cases
169
TestUtil.assertIteratorValues(this,
170             cache.find(new TriplePattern(null, directP, null)),
171             new Object JavaDoc[] {
172                 new Triple(a, closedP, a),
173                 new Triple(a, closedP, b),
174                 new Triple(d, closedP, d),
175                 new Triple(d, closedP, b),
176                 new Triple(b, closedP, b),
177                 new Triple(b, closedP, e),
178                 new Triple(b, closedP, c),
179                 new Triple(e, closedP, e),
180                 new Triple(e, closedP, f),
181                 new Triple(c, closedP, c),
182                 new Triple(c, closedP, f),
183                 new Triple(f, closedP, f),
184                 new Triple(f, closedP, g),
185                 new Triple(g, closedP, g)
186             });
187         TestUtil.assertIteratorValues(this,
188             cache.find(new TriplePattern(null, closedP, null)),
189             new Object JavaDoc[] {
190                 new Triple(a, closedP, a),
191                 new Triple(a, closedP, b),
192                 new Triple(a, closedP, c),
193                 new Triple(a, closedP, e),
194                 new Triple(a, closedP, f),
195                 new Triple(a, closedP, g),
196                 new Triple(d, closedP, d),
197                 new Triple(d, closedP, b),
198                 new Triple(d, closedP, e),
199                 new Triple(d, closedP, c),
200                 new Triple(d, closedP, f),
201                 new Triple(d, closedP, g),
202                 new Triple(b, closedP, b),
203                 new Triple(b, closedP, e),
204                 new Triple(b, closedP, c),
205                 new Triple(b, closedP, f),
206                 new Triple(b, closedP, g),
207                 new Triple(e, closedP, e),
208                 new Triple(e, closedP, f),
209                 new Triple(e, closedP, g),
210                 new Triple(c, closedP, c),
211                 new Triple(c, closedP, f),
212                 new Triple(c, closedP, g),
213                 new Triple(f, closedP, f),
214                 new Triple(f, closedP, g),
215                 new Triple(g, closedP, g)
216              });
217         
218         // Add a look in the graph and check the loop from each starting position
219
cache.addRelation(new Triple(g, closedP, e));
220         
221         TestUtil.assertIteratorValues(this,
222                 cache.find(new TriplePattern(e, directP, null)),
223                 new Object JavaDoc[] {
224                     new Triple(e, closedP, e),
225                     new Triple(e, closedP, f),
226                     new Triple(e, closedP, g)
227                 });
228             TestUtil.assertIteratorValues(this,
229                 cache.find(new TriplePattern(f, directP, null)),
230                 new Object JavaDoc[] {
231                     new Triple(f, closedP, f),
232                     new Triple(f, closedP, g),
233                     new Triple(f, closedP, e)
234                 });
235             TestUtil.assertIteratorValues(this,
236                 cache.find(new TriplePattern(g, directP, null)),
237                 new Object JavaDoc[] {
238                     new Triple(g, closedP, g),
239                     new Triple(g, closedP, e),
240                     new Triple(g, closedP, f)
241                 });
242             TestUtil.assertIteratorValues(this,
243                     cache.find(new TriplePattern(null, directP, e)),
244                     new Object JavaDoc[] {
245                         new Triple(e, closedP, e),
246                         new Triple(f, closedP, e),
247                         new Triple(b, closedP, e),
248                         new Triple(c, closedP, e),
249                         new Triple(g, closedP, e)
250                     });
251                 TestUtil.assertIteratorValues(this,
252                     cache.find(new TriplePattern(null, directP, f)),
253                     new Object JavaDoc[] {
254                         new Triple(f, closedP, f),
255                         new Triple(g, closedP, f),
256                         new Triple(b, closedP, f),
257                         new Triple(c, closedP, f),
258                         new Triple(e, closedP, f)
259                     });
260                 TestUtil.assertIteratorValues(this,
261                     cache.find(new TriplePattern(null, directP, g)),
262                     new Object JavaDoc[] {
263                         new Triple(g, closedP, g),
264                         new Triple(e, closedP, g),
265                         new Triple(b, closedP, g),
266                         new Triple(c, closedP, g),
267                         new Triple(f, closedP, g)
268                     });
269         TestUtil.assertIteratorValues(this,
270             cache.find(new TriplePattern(g, closedP, null)),
271             new Object JavaDoc[] {
272                 new Triple(g, closedP, g),
273                 new Triple(g, closedP, e),
274                 new Triple(g, closedP, f)
275             });
276         TestUtil.assertIteratorValues(this,
277             cache.find(new TriplePattern(e, closedP, null)),
278             new Object JavaDoc[] {
279                 new Triple(e, closedP, g),
280                 new Triple(e, closedP, e),
281                 new Triple(e, closedP, f)
282             });
283         TestUtil.assertIteratorValues(this,
284             cache.find(new TriplePattern(f, closedP, null)),
285             new Object JavaDoc[] {
286                 new Triple(f, closedP, g),
287                 new Triple(f, closedP, e),
288                 new Triple(f, closedP, f)
289             });
290         /*
291         System.out.println("Add e-f-g-e loop");
292         cache.printAll();
293         listFind(cache, e, directP, null);
294         listFind(cache, e, closedP, null);
295         listFind(cache, f, directP, null);
296         listFind(cache, f, closedP, null);
297         listFind(cache, g, directP, null);
298         listFind(cache, g, closedP, null);
299         */

300     }
301     
302     /**
303      * Test a a case where an earlier version had a bug due to removing
304      * a link which was required rather than redundant.
305      */

306     public void testBug1() {
307         TransitiveGraphCache cache = new TransitiveGraphCache(directP, closedP);
308         cache.addRelation(new Triple(a, closedP, b));
309         cache.addRelation(new Triple(c, closedP, a));
310         cache.addRelation(new Triple(c, closedP, b));
311         cache.addRelation(new Triple(a, closedP, c));
312         TestUtil.assertIteratorValues(this,
313             cache.find(new TriplePattern(a, directP, null)),
314             new Object JavaDoc[] {
315                 new Triple(a, closedP, a),
316                 new Triple(a, closedP, b),
317                 new Triple(a, closedP, c),
318             });
319            
320     }
321         
322     /**
323      * Test the removeRelation functionality.
324      */

325     public void testRemove() {
326         TransitiveGraphCache cache = new TransitiveGraphCache(directP, closedP);
327         cache.addRelation(new Triple(a, closedP, b));
328         cache.addRelation(new Triple(a, closedP, c));
329         cache.addRelation(new Triple(b, closedP, d));
330         cache.addRelation(new Triple(c, closedP, d));
331         cache.addRelation(new Triple(d, closedP, e));
332         TestUtil.assertIteratorValues(this,
333             cache.find(new TriplePattern(a, closedP, null)),
334             new Object JavaDoc[] {
335                 new Triple(a, closedP, a),
336                 new Triple(a, closedP, b),
337                 new Triple(a, closedP, b),
338                 new Triple(a, closedP, c),
339                 new Triple(a, closedP, d),
340                 new Triple(a, closedP, e)
341             });
342         TestUtil.assertIteratorValues(this,
343             cache.find(new TriplePattern(b, closedP, null)),
344             new Object JavaDoc[] {
345                 new Triple(b, closedP, b),
346                 new Triple(b, closedP, d),
347                 new Triple(b, closedP, e)
348             });
349         cache.removeRelation(new Triple(b, closedP, d));
350         TestUtil.assertIteratorValues(this,
351             cache.find(new TriplePattern(a, closedP, null)),
352             new Object JavaDoc[] {
353                 new Triple(a, closedP, a),
354                 new Triple(a, closedP, b),
355                 new Triple(a, closedP, b),
356                 new Triple(a, closedP, c),
357                 new Triple(a, closedP, d),
358                 new Triple(a, closedP, e)
359             });
360         TestUtil.assertIteratorValues(this,
361             cache.find(new TriplePattern(b, closedP, null)),
362             new Object JavaDoc[] {
363                 new Triple(b, closedP, b),
364             });
365         cache.removeRelation(new Triple(a, closedP, c));
366         TestUtil.assertIteratorValues(this,
367             cache.find(new TriplePattern(a, closedP, null)),
368             new Object JavaDoc[] {
369                 new Triple(a, closedP, a),
370                 new Triple(a, closedP, b)
371             });
372         TestUtil.assertIteratorValues(this,
373             cache.find(new TriplePattern(b, closedP, null)),
374             new Object JavaDoc[] {
375                 new Triple(b, closedP, b),
376             });
377     }
378     
379     /**
380      * Test direct link case with adverse ordering.
381      */

382     public void testDirect() {
383         TransitiveGraphCache cache = new TransitiveGraphCache(directP, closedP);
384         cache.addRelation(new Triple(a, closedP, b));
385         cache.addRelation(new Triple(c, closedP, d));
386         cache.addRelation(new Triple(a, closedP, d));
387         cache.addRelation(new Triple(b, closedP, c));
388         TestUtil.assertIteratorValues(this,
389             cache.find(new TriplePattern(a, directP, null)),
390             new Object JavaDoc[] {
391                 new Triple(a, closedP, a),
392                 new Triple(a, closedP, b),
393             });
394     }
395     
396     /**
397      * Test cycle detection.
398      */

399     public void testCycle() {
400         TransitiveGraphCache cache = new TransitiveGraphCache(directP, closedP);
401         cache.addRelation(new Triple(a, closedP, b));
402         cache.addRelation(new Triple(b, closedP, c));
403         cache.addRelation(new Triple(a, closedP, c));
404         cache.addRelation(new Triple(c, closedP, b));
405         TestUtil.assertIteratorValues(this,
406             cache.find(new TriplePattern(a, directP, null)),
407             new Object JavaDoc[] {
408                 new Triple(a, closedP, a),
409                 new Triple(a, closedP, b),
410                 new Triple(a, closedP, c),
411             });
412     }
413     
414     /**
415      * A ring of three cycle
416      */

417     public void testCycle2() {
418         TransitiveGraphCache cache = new TransitiveGraphCache(directP, closedP);
419         cache.addRelation(new Triple(a, closedP, b));
420         cache.addRelation(new Triple(a, closedP, c));
421         cache.addRelation(new Triple(f, closedP, b));
422         cache.addRelation(new Triple(b, closedP, g));
423         cache.addRelation(new Triple(b, closedP, d));
424         cache.addRelation(new Triple(d, closedP, c));
425         cache.addRelation(new Triple(d, closedP, e));
426         cache.addRelation(new Triple(c, closedP, e));
427         cache.addRelation(new Triple(c, closedP, b));
428         TestUtil.assertIteratorValues(this,
429                 cache.find(new TriplePattern(c, directP, null)),
430                 new Object JavaDoc[] {
431                     new Triple(c, closedP, e),
432                     new Triple(c, closedP, g),
433                     new Triple(c, closedP, b),
434                     new Triple(c, closedP, d),
435                     new Triple(c, closedP, c),
436                 });
437         TestUtil.assertIteratorValues(this,
438                 cache.find(new TriplePattern(null, directP, c)),
439                 new Object JavaDoc[] {
440                     new Triple(a, closedP, c),
441                     new Triple(b, closedP, c),
442                     new Triple(d, closedP, c),
443                     new Triple(f, closedP, c),
444                     new Triple(c, closedP, c),
445                 });
446         TestUtil.assertIteratorValues(this,
447                 cache.find(new TriplePattern(f, closedP, null)),
448                 new Object JavaDoc[] {
449                     new Triple(f, closedP, f),
450                     new Triple(f, closedP, b),
451                     new Triple(f, closedP, c),
452                     new Triple(f, closedP, d),
453                     new Triple(f, closedP, g),
454                     new Triple(f, closedP, e),
455                 });
456     }
457     
458     /**
459      * Two ring-of-three cycles joined at two points
460      */

461     public void testCycle3() {
462         TransitiveGraphCache cache = new TransitiveGraphCache(directP, closedP);
463         cache.addRelation(new Triple(a, closedP, b));
464         cache.addRelation(new Triple(b, closedP, c));
465         cache.addRelation(new Triple(c, closedP, a));
466         cache.addRelation(new Triple(d, closedP, e));
467         cache.addRelation(new Triple(e, closedP, f));
468         cache.addRelation(new Triple(f, closedP, d));
469         cache.addRelation(new Triple(b, closedP, d));
470         cache.addRelation(new Triple(f, closedP, c));
471         TestUtil.assertIteratorValues(this,
472                 cache.find(new TriplePattern(a, directP, null)),
473                 new Object JavaDoc[] {
474                 new Triple(a, closedP, a),
475                 new Triple(a, closedP, b),
476                 new Triple(a, closedP, c),
477                 new Triple(a, closedP, d),
478                 new Triple(a, closedP, e),
479                 new Triple(a, closedP, f),
480                 });
481         TestUtil.assertIteratorValues(this,
482                 cache.find(new TriplePattern(null, directP, a)),
483                 new Object JavaDoc[] {
484                 new Triple(a, closedP, a),
485                 new Triple(b, closedP, a),
486                 new Triple(c, closedP, a),
487                 new Triple(d, closedP, a),
488                 new Triple(e, closedP, a),
489                 new Triple(f, closedP, a),
490                 });
491     }
492     
493     /**
494      * Test equivalences case
495      */

496     public void testEquivalences() {
497         TransitiveGraphCache cache = new TransitiveGraphCache(directP, closedP);
498         cache.addRelation(new Triple(a, closedP, b));
499         cache.addRelation(new Triple(b, closedP, a));
500         
501         cache.addRelation(new Triple(c, closedP, d));
502         cache.addRelation(new Triple(d, closedP, c));
503         
504         cache.addRelation(new Triple(b, closedP, d));
505         cache.addRelation(new Triple(d, closedP, b));
506
507         assertTrue("Test eq", cache.contains(new TriplePattern(a, closedP, d)));
508     }
509
510 }
511
512
513 /*
514     (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP
515     All rights reserved.
516
517     Redistribution and use in source and binary forms, with or without
518     modification, are permitted provided that the following conditions
519     are met:
520
521     1. Redistributions of source code must retain the above copyright
522        notice, this list of conditions and the following disclaimer.
523
524     2. Redistributions in binary form must reproduce the above copyright
525        notice, this list of conditions and the following disclaimer in the
526        documentation and/or other materials provided with the distribution.
527
528     3. The name of the author may not be used to endorse or promote products
529        derived from this software without specific prior written permission.
530
531     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
532     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
533     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
534     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
535     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
536     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
537     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
538     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
539     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
540     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
541 */

542
Popular Tags