KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > graph > test > AbstractTestReifier


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

6
7 package com.hp.hpl.jena.graph.test;
8
9 import com.hp.hpl.jena.db.impl.DBReifier;
10 import com.hp.hpl.jena.graph.*;
11 import com.hp.hpl.jena.graph.impl.GraphBase;
12 import com.hp.hpl.jena.shared.*;
13 import com.hp.hpl.jena.util.CollectionFactory;
14 import com.hp.hpl.jena.util.iterator.ExtendedIterator;
15 import com.hp.hpl.jena.vocabulary.RDF;
16
17 /**
18     Abstract base class for reification tests.
19     @author kers
20 */

21 public abstract class AbstractTestReifier extends GraphTestBase
22     {
23     protected static final ReificationStyle Minimal = ReificationStyle.Minimal;
24     protected static final ReificationStyle Standard = ReificationStyle.Standard;
25     protected static final ReificationStyle Convenient = ReificationStyle.Convenient;
26
27     protected static final Triple ALL = Triple.create( "?? ?? ??" );
28     
29     public AbstractTestReifier(String JavaDoc name)
30         { super(name); }
31         
32     public Graph getGraph()
33         { return getGraph( Minimal ); }
34     
35     public abstract Graph getGraph( ReificationStyle style );
36
37     protected final Graph getGraphWith( String JavaDoc facts )
38         {
39         Graph result = getGraph();
40         graphAdd( result, facts );
41         return result;
42         }
43         
44     /**
45         Answer the empty graph if cond is false, otherwise the graph with the given facts.
46     */

47     protected final Graph graphWithUnless( boolean cond, String JavaDoc facts )
48         { return graphWith( cond ? "" : facts ); }
49           
50     protected final Graph graphWithIf( boolean cond, String JavaDoc facts )
51         { return graphWithUnless( !cond, facts ); }
52             
53     public void testStyle()
54         {
55         assertSame( Minimal, getGraph( Minimal ).getReifier().getStyle() );
56         assertSame( Standard, getGraph( Standard ).getReifier().getStyle() );
57         assertSame( Convenient, getGraph( Convenient ).getReifier().getStyle() );
58         }
59         
60     public void testEmptyReifiers()
61         {
62         assertFalse( getGraphWith( "x R y" ).getReifier().findExposed( ALL ).hasNext() );
63         assertFalse( getGraphWith( "x R y; p S q" ).getReifier().findExposed( ALL ).hasNext() );
64         }
65         
66     public void testSameReifier()
67         {
68         Graph G = getGraph();
69         Reifier R1 = G.getReifier();
70         G.add( triple( "x R y" ) );
71         assertTrue( "same reifier", R1 == G.getReifier() );
72         }
73     
74     public void testReifierClosed()
75         {
76         Graph g = getGraph();
77         Reifier r = g.getReifier();
78         g.close();
79         }
80         
81     public void testParent()
82         {
83         Graph G = getGraph(), H = getGraph();
84         assertTrue( "correct reifier (G)", G == G.getReifier().getParentGraph() );
85         assertTrue( "correct reifier (H)", H == H.getReifier().getParentGraph() );
86         }
87         
88     public void testIntercept()
89         {
90         Graph g = getGraph( Convenient );
91         Reifier r = g.getReifier();
92         Node S = node( "sub" ), O = node( "obj" );
93         Node RS = node( "http://example.org/type" );
94     /* */
95         assertFalse( "reifier must not intercept quadlet", r.handledAdd( Triple.create( S, RDF.Nodes.type, RS ) ) );
96         assertFalse( "reifier must not intercept quadlet", r.handledAdd( Triple.create( S, S, RDF.Nodes.subject ) ) );
97         assertFalse( "reifier must not intercept quadlet", r.handledAdd( Triple.create( S, S, RDF.Nodes.type ) ) );
98     /* */
99         assertTrue( "reifier must intercept quadlet", r.handledAdd( Triple.create( S, RDF.Nodes.predicate, O ) ) );
100         assertTrue( "reifier must intercept quadlet", r.handledAdd( Triple.create( S, RDF.Nodes.type, RDF.Nodes.Statement ) ) );
101         }
102
103     /**
104         Check that the standard reifier will note, but not hide, reification quads.
105     */

106     public void testStandard()
107         {
108         Graph g = getGraph( Standard );
109         assertFalse( g.getReifier().hasTriple( triple( "s p o" ) ) );
110         g.add( Triple.create( "x rdf:subject s" ) );
111         assertEquals( 1, g.size() );
112         g.add( Triple.create( "x rdf:predicate p" ) );
113         assertEquals( 2, g.size() );
114         g.add( Triple.create( "x rdf:object o" ) );
115         assertEquals( 3, g.size() );
116         g.add( Triple.create( "x rdf:type rdf:Statement" ) );
117         assertEquals( 4, g.size() );
118         assertTrue( g.getReifier().hasTriple( triple( "s p o" ) ) );
119         }
120         
121     /**
122         Test that the Standard reifier will expose implicit quads arising from reifyAs().
123     */

124     public void testStandardExplode()
125         {
126         Graph g = getGraph( Standard );
127         g.getReifier().reifyAs( node( "a" ), triple( "p Q r" ) );
128         Graph r = Factory.createDefaultGraph( Minimal );
129         graphAdd( r, "a rdf:type rdf:Statement; a rdf:subject p; a rdf:predicate Q; a rdf:object r" );
130         assertEquals( 4, g.size() );
131         assertIsomorphic( r, g );
132         }
133         
134     public void testMinimalExplode()
135         {
136         Graph g = getGraph( Minimal );
137         g.getReifier().reifyAs( node( "a" ), triple( "p Q r" ) );
138         assertEquals( 0, g.size() );
139         }
140         
141     public void testReificationTriplesConvenient()
142         { testReificationTriples( Convenient ); }
143         
144     public void testReificationTriplesStandard()
145         { testReificationTriples( Standard ); }
146     
147     public void testReificationQuadletsMinimal()
148         { testReificationTriples( Minimal ); }
149         
150     /**
151         test that a reifier with the given style sees [or not, if it's minimal] the reification quads
152         that are inserted through its graph.
153     */

154     protected void testReificationTriples( ReificationStyle style )
155         {
156         Graph g = getGraph( style );
157         Graph quadlets = getReificationTriples( g.getReifier() );
158         String JavaDoc S1 = "SSS rdf:predicate PPP", S2 = "SSS rdf:subject SSS";
159         g.add( triple( S1 ) );
160         assertIsomorphic( graphWithUnless( style == Minimal, S1 ), quadlets );
161         g.add( triple( S2 ) );
162         assertIsomorphic( graphWithUnless( style == Minimal, S1 + "; " + S2 ), quadlets );
163         assertEquals( "convenient hides quadlets", style == Convenient, g.size() == 0 );
164         }
165         
166     /**
167          Ensure that over-specifying a reification means that we don't get a triple
168          back. Goodness knows why this test wasn't in right from the beginning.
169     */

170     public void testOverspecificationSuppressesReification()
171         {
172         Graph g = getGraph( Standard );
173         Reifier r = g.getReifier();
174         graphAdd( g, "x rdf:subject A; x rdf:predicate P; x rdf:object O; x rdf:type rdf:Statement" );
175         assertEquals( triple( "A P O" ), r.getTriple( node( "x" ) ) );
176         try
177             { graphAdd( g, "x rdf:subject BOOM" );
178             assertEquals( null, r.getTriple( node( "x" ) ) ); }
179         catch (AlreadyReifiedException e)
180             {
181             if (r instanceof DBReifier) { System.err.println( "! Db reifier must fix over-specification problem" ); }
182             else throw e;
183             }
184         }
185     
186     public void testReificationSubjectClash()
187         {
188         testReificationClash( "x rdf:subject SS" );
189         }
190     
191     public void testReificationPredicateClash()
192         {
193         testReificationClash( "x rdf:predicate PP" );
194         }
195     
196     public void testReificationObjectClash()
197         {
198         testReificationClash( "x rdf:object OO" );
199         }
200         
201     /**
202      * @param clashingStatement
203      */

204     protected void testReificationClash( String JavaDoc clashingStatement )
205         {
206         Graph g = getGraph( Standard );
207         Triple SPO = Triple.create( "S P O" );
208         g.getReifier().reifyAs( node( "x" ), SPO );
209         assertTrue( g.getReifier().hasTriple( SPO ) );
210         try
211             {
212             graphAdd( g, clashingStatement );
213             assertEquals( null, g.getReifier().getTriple( node( "x" ) ) );
214             assertFalse( g.getReifier().hasTriple( SPO ) );
215             }
216         catch (AlreadyReifiedException e)
217             {
218             if (g.getReifier() instanceof DBReifier) { System.err.println( "! Db reifier must fix over-specification problem" ); }
219             else throw e;
220             }
221         }
222
223     public void testManifestQuadsStandard()
224         { testManifestQuads( Standard ); }
225         
226     public void testManifestQuadsConvenient()
227         { testManifestQuads( Convenient ); }
228         
229     public void testManifestQuadsMinimal()
230         { testManifestQuads( Minimal ); }
231         
232     /**
233         Test that reifying a triple explicitly has some effect on the graph only for Standard
234         reifiers.
235      */

236     public void testManifestQuads( ReificationStyle style )
237         {
238         Graph g = getGraph( style );
239         Reifier r = g.getReifier();
240         r.reifyAs( node( "A" ), triple( "S P O" ) );
241         String JavaDoc reified = "A rdf:type rdf:Statement; A rdf:subject S; A rdf:predicate P; A rdf:object O";
242         assertIsomorphic( graphWithIf( style == Standard, reified ), g );
243         }
244         
245     public void testHiddenVsReificationMinimal()
246         { testHiddenVsReification( Minimal ); }
247         
248     public void testHiddenVsStandard()
249         { testHiddenVsReification( Standard ); }
250         
251     public void testHiddenVsReificationConvenient()
252         { testHiddenVsReification( Convenient ); }
253         
254     public void testHiddenVsReification( ReificationStyle style )
255         {
256         Graph g = getGraph( style );
257         Reifier r = g.getReifier();
258         r.reifyAs( node( "A" ), triple( "S P O" ) );
259         assertEquals( style == Standard, r.findEither( ALL, false ).hasNext() );
260         }
261       
262     public void testRetrieveTriplesByNode()
263         {
264         Graph G = getGraph();
265         Reifier R = G.getReifier();
266         Node N = Node.createAnon(), M = Node.createAnon();
267         R.reifyAs( N, triple( "x R y" ) );
268         assertEquals( "gets correct triple", triple( "x R y" ), R.getTriple( N ) );
269         R.reifyAs( M, triple( "p S q" ) );
270         assertDiffer( "the anon nodes must be distinct", N, M );
271         assertEquals( "gets correct triple", triple( "p S q" ), R.getTriple( M ) );
272     /* */
273         assertTrue( "node is known bound", R.hasTriple( M ) );
274         assertTrue( "node is known bound", R.hasTriple( N ) );
275         assertFalse( "node is known unbound", R.hasTriple( Node.createURI( "any:thing" ) ) );
276     /* */
277 // Graph GR = R.getReifiedTriples();
278
// assertTrue( "reified triples", getGraphWith( "x R y; p S q" ).isIsomorphicWith(GR) );
279
// assertTrue( "untouched graph", getGraph().isIsomorphicWith(G) );
280
}
281         
282     public void testRetrieveTriplesByTriple()
283         {
284         Graph G = getGraph();
285         Reifier R = G.getReifier();
286         Triple T = triple( "x R y" ), T2 = triple( "y R x" );
287         Node N = node( "someNode" );
288         R.reifyAs( N, T );
289         assertTrue( "R must have T", R.hasTriple( T ) );
290         assertFalse( "R must not have T2", R.hasTriple( T2 ) );
291         }
292              
293     public void testReifyAs()
294         {
295         Graph G = getGraph();
296         Reifier R = G.getReifier();
297         Node X = Node.createURI( "some:uri" );
298         assertEquals( "node used", X, R.reifyAs( X, triple( "x R y" ) ) );
299         assertEquals( "retrieves correctly", triple( "x R y" ), R.getTriple( X ) );
300         }
301         
302     public void testAllNodes()
303         {
304         Reifier R = getGraph().getReifier();
305         R.reifyAs( node("x"), triple( "cows eat grass" ) );
306         R.reifyAs( node("y"), triple( "pigs can fly" ) );
307         R.reifyAs( node("z"), triple( "dogs may bark" ) );
308         assertEquals( "", nodeSet( "z y x" ), iteratorToSet( R.allNodes() ) );
309         }
310         
311     public void testRemoveByNode()
312         {
313         Graph G = getGraph();
314         Reifier R = G.getReifier();
315         Node X = node( "x" ), Y = node( "y" );
316         R.reifyAs( X, triple( "x R a" ) );
317         R.reifyAs( Y, triple( "y R a" ) );
318         R.remove( X, triple( "x R a" ) );
319         assertFalse( "triple X has gone", R.hasTriple( X ) );
320         assertEquals( "triple Y still there", triple( "y R a" ), R.getTriple( Y ) );
321         }
322         
323     public void testRemoveFromNothing()
324         {
325         Graph G = getGraph();
326         Reifier R = G.getReifier();
327         G.delete( triple( "quint rdf:subject S" ) );
328         }
329     
330 // public void testRemoveByTriple()
331
// {
332
// Graph G = getGraph();
333
// Reifier R = G.getReifier();
334
// Node X = node( "x" ), Y = node( "y" );
335
// R.reifyAs( X, triple( "x R a" ) );
336
// R.reifyAs( Y, triple( "y R a" ) );
337
// R.remove( triple( "x R a" ) );
338
// assertFalse( "triple X has gone", R.hasTriple( X ) );
339
// assertEquals( "triple Y still there", triple( "y R a" ), R.getTriple( Y ) );
340
// }
341

342     public void testException()
343         {
344         Graph G = getGraph();
345         Reifier R = G.getReifier();
346         Node X = node( "x" );
347         R.reifyAs( X, triple( "x R y" ) );
348         R.reifyAs( X, triple( "x R y" ) );
349         try { R.reifyAs( X, triple( "x R z" ) ); fail( "did not detect already reified node" ); }
350         catch (AlreadyReifiedException e) { }
351         }
352         
353     public void testKevinCaseA()
354         {
355         Graph G = getGraph( Standard );
356         Node X = node( "x" ), a = node( "a" ), b = node( "b" ), c = node( "c" );
357         G.add( Triple.create( X, RDF.Nodes.type, RDF.Nodes.Statement ) );
358         G.getReifier().reifyAs( X, Triple.create( a, b, c ) );
359         }
360         
361     public void testKevinCaseB()
362         {
363         Graph G = getGraph( Standard );
364         Node X = node( "x" ), Y = node( "y" );
365         Node a = node( "a" ), b = node( "b" ), c = node( "c" );
366         G.add( Triple.create( X, RDF.Nodes.subject, Y ) );
367         try
368             {
369             G.getReifier().reifyAs( X, Triple.create( a, b, c ) );
370             fail( "X already has subject Y: cannot make it a" );
371             }
372         catch (CannotReifyException e)
373             { pass(); }
374         }
375
376     /**
377         Test that the hidden triples graph is dynamic, not static.
378     */

379     public void testDynamicHiddenTriples()
380         {
381         Graph g = getGraph();
382         Reifier r = g.getReifier();
383         Graph h = getHiddenTriples( r );
384         Graph wanted = graphWith
385             (
386             "x rdf:type rdf:Statement"
387             + "; x rdf:subject a"
388             + "; x rdf:predicate B"
389             + "; x rdf:object c"
390             );
391         assertTrue( h.isEmpty() );
392         r.reifyAs( node( "x" ), triple( "a B c" ) );
393         assertIsomorphic( wanted, h );
394         }
395     
396     protected Graph getHiddenTriples( final Reifier r )
397         {
398         return new GraphBase()
399             {
400             public ExtendedIterator graphBaseFind( TripleMatch m ) { return r.findEither( m, true ); }
401             };
402         }
403             
404     public void testQuadRemove()
405         {
406         Graph g = getGraph( Standard );
407         assertEquals( 0, g.size() );
408     
409         Triple s = Triple.create( "x rdf:subject s" );
410         Triple p = Triple.create( "x rdf:predicate p" );
411         Triple o = Triple.create( "x rdf:object o" );
412         Triple t = Triple.create( "x rdf:type rdf:Statement");
413         g.add(s); g.add(p); g.add(o); g.add(t);
414         assertEquals( 4, g.size() );
415
416         g.delete(s); g.delete(p); g.delete(o); g.delete(t);
417         assertEquals( 0, g.size() );
418         }
419
420     public void testReifierSize()
421         {
422         Graph g = getGraph();
423         Reifier r = g.getReifier();
424         assertEquals( 0, r.size() );
425         }
426     
427     public void testEmpty()
428         {
429         Graph g = getGraph( Standard );
430         assertTrue( g.isEmpty() );
431         graphAdd( g, "x rdf:type rdf:Statement" ); assertFalse( g.isEmpty() );
432         graphAdd( g, "x rdf:subject Deconstruction" ); assertFalse( g.isEmpty() );
433         graphAdd( g, "x rdf:predicate rdfs:subTypeOf" ); assertFalse( g.isEmpty() );
434         graphAdd( g, "x rdf:object LiteraryCriticism" ); assertFalse( g.isEmpty() );
435         }
436     
437     public void testReifierEmptyFind()
438         {
439         Graph g = getGraph( Standard );
440         Reifier r = g.getReifier();
441         assertEquals( CollectionFactory.createHashedSet(), iteratorToSet( r.findExposed( triple( "?? ?? ??" ) ) ) );
442         }
443
444     public void testReifierFindSubject()
445         { testReifierFind( "x rdf:subject S" ); }
446     
447     public void testReifierFindObject()
448         { testReifierFind( "x rdf:object O" ); }
449     
450     public void testReifierFindPredicate()
451         { testReifierFind( "x rdf:predicate P" ); }
452     
453     public void testReifierFindComplete()
454         { testReifierFind( "x rdf:predicate P; x rdf:subject S; x rdf:object O; x rdf:type rdf:Statement" ); }
455     
456     public void testReifierFindFilter()
457         {
458         Graph g = getGraph( Standard );
459         Reifier r = g.getReifier();
460         graphAdd( g, "s rdf:subject S" );
461         assertEquals( tripleSet( "" ), iteratorToSet( r.findExposed( triple( "s otherPredicate S" ) ) ) );
462         }
463
464     protected void testReifierFind( String JavaDoc triples )
465         { testReifierFind( triples, "?? ?? ??" ); }
466     
467     protected void testReifierFind( String JavaDoc triples, String JavaDoc pattern )
468         {
469         Graph g = getGraph( Standard );
470         Reifier r = g.getReifier();
471         graphAdd( g, triples );
472         assertEquals( tripleSet( triples ), iteratorToSet( r.findExposed( triple( pattern ) ) ) );
473         }
474
475 // public void testKevinCaseC()
476
// {
477
// Graph G = GraphBase.withReification( getGraph() );
478
// Node X = node( "x" ), Y = node( "y" );
479
// Node a = node( "a" ), b = node( "b" ), c = node( "c" );
480
// G.getReifier().reifyAs( X, Triple.create( a, b, c ) );
481
// try
482
// {
483
// G.add( Triple.create( X, Reifier.subject, Y ) );
484
// fail( "X already reifies (a, b, c): cannot give it subject Y" );
485
// }
486
// catch (Reifier.CannotReifyException e)
487
// { /* as requried */ }
488
// }
489

490 // public void testQuads()
491
// {
492
// Node A = node( "a" ), B = node( "b" );
493
// Graph G = getGraph();
494
// Graph quads = getGraphWith
495
// (
496
// "a " + RDF.type + " " + RDF.Statement
497
// + "; a " + RDF.subject + " x"
498
// + "; a " + RDF.predicate + " R"
499
// + "; a " + RDF.object + " y"
500
// + "; b " + RDF.type + " " + RDF.Statement
501
// + "; b " + RDF.subject + " p"
502
// + "; b " + RDF.predicate + " S"
503
// + "; b " + RDF.object + " q"
504
// );
505
// Reifier R = G.getReifier();
506
// R.reifyAs( A, triple( "x R y") );
507
// R.reifyAs( B, triple( "p S q" ) );
508
// assertEquals( "same", quads, R.getReificationQuads() );
509
// }
510

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