KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > rdf > model > test > AbstractTestReifiedStatements


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

6
7 package com.hp.hpl.jena.rdf.model.test;
8
9 import com.hp.hpl.jena.rdf.model.*;
10 import com.hp.hpl.jena.util.CollectionFactory;
11 import com.hp.hpl.jena.vocabulary.RDF;
12 import com.hp.hpl.jena.graph.test.*;
13
14 import java.util.*;
15
16 /**
17     @author kers
18 */

19 public abstract class AbstractTestReifiedStatements extends ModelTestBase
20     {
21     public AbstractTestReifiedStatements( String JavaDoc name )
22         {
23         super( name );
24         }
25         
26     public abstract Model getModel();
27        
28     private Model model;
29     private Resource S;
30     private Property P;
31     private RDFNode O;
32     private Statement SPO;
33     private Statement SPO2;
34     
35     private static final String JavaDoc aURI = "jena:test/reifying#someURI";
36     private static final String JavaDoc anotherURI = "jena:test/reifying#anotherURI";
37     private static final String JavaDoc anchor = "jena:test/Reifying#";
38     
39     public void setUp()
40         {
41         model = getModel();
42         Resource S2 = model.createResource( anchor + "subject2" );
43         S = model.createResource( anchor + "subject" );
44         P = model.createProperty( anchor + "predicate" );
45         O = model.createLiteral( anchor + "object" );
46         SPO = model.createStatement( S, P, O );
47         SPO2 = model.createStatement( S2, P, O );
48         }
49         
50     /**
51         the simplest case: if we assert all the components of a reification quad,
52         we can get a ReifiedStatement that represents the reified statement.
53     */

54     public void testBasicReification()
55         {
56         if (model.getReificationStyle() != ModelFactory.Minimal)
57             { Resource R = model.createResource( aURI );
58             model.add( R, RDF.type, RDF.Statement );
59             model.add( R, RDF.subject, S );
60             model.add( R, RDF.predicate, P );
61             model.add( R, RDF.object, O );
62             RDFNode rs = R.as( ReifiedStatement.class );
63             assertEquals( "can recover statement", SPO, ((ReifiedStatement) rs).getStatement() ); }
64         }
65         
66     /**
67         check that, from a model with any combination of the statements given,
68         we can convert R into a ReifiedStatement iff the four components of the
69         quad are in the model.
70     */

71     public void testReificationCombinations()
72         {
73         Resource RR = model.createResource( aURI ), SS = model.createResource( anotherURI );
74         Property PP = (Property) RR.as( Property.class );
75         Object JavaDoc [][] statements =
76             {
77                 { model.createStatement( RR, RDF.type, RDF.Statement ), new Integer JavaDoc(1) },
78                 { model.createStatement( RR, RDF.subject, SS ), new Integer JavaDoc(2) },
79                 { model.createStatement( RR, RDF.predicate, PP ), new Integer JavaDoc(4) },
80                 { model.createStatement( RR, RDF.object, O ), new Integer JavaDoc(8) },
81                 { model.createStatement( SS, PP, O ), new Integer JavaDoc(16) },
82                 { model.createStatement( RR, PP, O ), new Integer JavaDoc(32) },
83                 { model.createStatement( SS, RDF.subject, SS ), new Integer JavaDoc(64) },
84                 { model.createStatement( SS, RDF.predicate, PP ), new Integer JavaDoc(128) },
85                 { model.createStatement( SS, RDF.object, O ), new Integer JavaDoc(256) },
86                 { model.createStatement( SS, RDF.type, RDF.Statement ), new Integer JavaDoc(512) }
87             };
88         if (model.getReificationStyle() != ModelFactory.Minimal)
89             testCombinations( model, RR, 0, statements, statements.length );
90         }
91
92     /**
93         walk down the set of statements (represented as an array), recursing with and
94         without each statement being present. The mask bits record those statements
95         that are in the model. At the bottom of the recursion (n == 0), check that R
96         can be reified exactly when all four quad components are present; the other
97         statements don't matter.
98     */

99     private void testCombinations( Model m, Resource R, int mask, Object JavaDoc [][] statements, int n )
100         {
101         if (n == 0)
102             {
103             try
104                 {
105                 // System.err.println( "| hello. mask = " + mask );
106
ReifiedStatement rs = (ReifiedStatement) R.as( ReifiedStatement.class );
107                 // System.err.println( "+ we constructed " + rs );
108
assertTrue( "should not reify: not all components present [" + mask + "]: " + rs, (mask & 15) == 15 );
109                 // System.err.println( "+ and we passed the assertion." );
110
}
111             catch (DoesNotReifyException e)
112                 { // System.err.println( "+ we exploded" );
113
assertFalse( "should reify: all components present", mask == 15 ); }
114             }
115         else
116             {
117             int i = n - 1;
118             Statement s = (Statement) statements[i][0];
119             int bits = ((Integer JavaDoc) statements[i][1]).intValue();
120             testCombinations( m, R, mask, statements, i );
121             m.add( s );
122             testCombinations( m, R, mask + bits, statements, i );
123             m.remove( s );
124             }
125         }
126         
127     public void testThisWillBreak()
128         {
129         Resource R = model.createResource( aURI );
130         SPO.createReifiedStatement( aURI );
131         model.add( R, RDF.subject, R );
132         }
133
134     /**
135         "dirty" reifications - those with conflicting quadlets - should fail.
136     */

137     public void testDirtyReification()
138         {
139         Resource R = model.createResource( aURI );
140         model.add( R, RDF.type, RDF.Statement );
141         model.add( R, RDF.subject, S );
142         model.add( R, RDF.subject, P );
143         testDoesNotReify( "boo", R );
144         }
145    
146     public void testDoesNotReify( String JavaDoc title, Resource r )
147         {
148         try { r.as( ReifiedStatement.class ); fail( title + " (" + r + ")" ); }
149         catch (DoesNotReifyException e) { /* that's what we expect */ }
150         }
151     
152     public void testConversion()
153         {
154         final String JavaDoc uri = "spoo:handle";
155         model.createReifiedStatement( uri, SPO );
156         ReifiedStatement rs2 = (ReifiedStatement) model.createResource( uri ).as( ReifiedStatement.class );
157         assertEquals( "recover statement", SPO, rs2.getStatement() );
158         }
159     
160     public void testDoesNotReifyUnknown()
161         {
162         testDoesNotReify( "model should not reify rubbish", model.createResource( "spoo:rubbish" ) );
163         }
164
165     public void testConstructionByURI()
166         {
167         ReifiedStatement rs = model.createReifiedStatement( "spoo:handle", SPO );
168         ReifiedStatement rs2 = SPO.createReifiedStatement( "spoo:gripper");
169         assertEquals( "recover statement (URI)", SPO, rs.getStatement() );
170         assertEquals( "recover URI", "spoo:handle", rs.getURI() );
171         assertEquals( "recover URI", "spoo:gripper", rs2.getURI() );
172         }
173
174     public void testStatementAndModel( String JavaDoc title, ReifiedStatement rs, Model m, Statement st )
175         {
176         assertEquals( title + ": recover statement", st, rs.getStatement() );
177         assertEquals( title + ": recover model", m, rs.getModel() );
178         }
179         
180     public void testConstructionFromStatements()
181         {
182         testStatementAndModel( "fromStatement", SPO.createReifiedStatement(), model, SPO );
183         }
184
185     public void testConstructionFromModels()
186         {
187         testStatementAndModel( "fromModel", model.createReifiedStatement( SPO ) , model, SPO );
188         }
189     
190     /**
191         utility method: get a set of all the elements delivered by
192         _m.listReifiedStatements_.
193     */

194     public Set getSetRS( Model m )
195         { return GraphTestBase.iteratorToSet( m.listReifiedStatements() ); }
196    
197     protected static Set empty = CollectionFactory.createHashedSet();
198     
199     /**
200         test that listReifiedStatements produces an iterator that contains
201         the right reified statements. We *don't* test that they're not
202         duplicated, because they might be; disallowing duplicates
203         could be expensive.
204     */

205     public void testListReifiedStatements()
206         {
207         assertEquals( "initially: no reified statements", empty, getSetRS( model ) );
208         ReifiedStatement rs = model.createReifiedStatement( aURI, SPO );
209         // assertEquals( "still: no reified statements", empty, getSetRS( m ) );
210
/* */
211         model.add( rs, P, O );
212         Set justRS = arrayToSet( new Object JavaDoc [] {rs} );
213         assertEquals( "post-add: one reified statement", justRS, getSetRS( model ) );
214         model.add( S, P, rs );
215         assertEquals( "post-add: still one reified statement", justRS, getSetRS( model ) );
216     /* */
217         ReifiedStatement rs2 = model.createReifiedStatement( anotherURI, SPO2 );
218         Set bothRS = arrayToSet( new Object JavaDoc[] {rs, rs2} );
219         model.add( rs2, P, O );
220         assertEquals( "post-add: still one reified statement", bothRS, getSetRS( model ) );
221         }
222
223     /**
224         this test appeared when TestStatementResources crashed using reified
225         statements as a step-0 implementation for asSubject()/asObject(). Looks
226         like there was a problem in modelReifier().getRS(), which we're fixing ...
227     */

228     public void testListDoesntCrash()
229         {
230         model.createReifiedStatement( SPO );
231         model.createReifiedStatement( SPO2 );
232         assertTrue( "should be non-empty", model.listReifiedStatements().hasNext() );
233         }
234         
235     public Set getSetRS( Model m, Statement st )
236         { return GraphTestBase.iteratorToSet( m.listReifiedStatements( st ) ); }
237         
238     public void testListReifiedSpecificStatements()
239         {
240         assertEquals( "no statements should match st", empty, getSetRS( model, SPO ) );
241     /* */
242         ReifiedStatement rs = model.createReifiedStatement( aURI, SPO );
243         ReifiedStatement rs2 = model.createReifiedStatement( anotherURI, SPO2 );
244         model.add( rs, P, O );
245         // assertEquals( "still no matching statement", empty, getSetRS( m, stOther ) );
246
/* */
247         Set justRS2 = arrayToSet( new Object JavaDoc [] {rs2} );
248         model.add( rs2, P, O );
249         assertEquals( "now one matching statement", justRS2, getSetRS( model, SPO2 ) );
250         }
251     
252     public void testStatementListReifiedStatements()
253         {
254         Statement st = SPO;
255         Model m = model;
256         assertEquals( "it's not there yet", empty, GraphTestBase.iteratorToSet( st.listReifiedStatements() ) );
257         ReifiedStatement rs = m.createReifiedStatement( aURI, st );
258         Set justRS = arrayToSet( new Object JavaDoc [] {rs} );
259         m.add( rs, P, O );
260         assertEquals( "it's here now", justRS, GraphTestBase.iteratorToSet( st.listReifiedStatements() ) );
261         }
262     
263     public void testIsReified()
264         {
265         ReifiedStatement rs = model.createReifiedStatement( aURI, SPO );
266         Resource BS = model.createResource( anchor + "BS" );
267         Property BP = model.createProperty( anchor + "BP" );
268         RDFNode BO = model.createProperty( anchor + "BO" );
269         model.add( rs, P, O );
270         assertTrue( "st should be reified now", SPO.isReified() );
271         assertTrue( "m should have st reified now", model.isReified( SPO ) );
272         assertFalse( "this new statement should not be reified", model.createStatement( BS, BP, BO ).isReified() );
273         }
274     
275     public void testGetAny()
276         {
277         Resource r = model.getAnyReifiedStatement( SPO );
278         assertTrue( "should get reified statement back", r instanceof ReifiedStatement );
279         assertEquals( "should get me the statement", SPO, ((ReifiedStatement) r).getStatement() );
280         }
281     
282     public void testRemoveReificationWorks()
283         {
284         Statement st = SPO;
285         Model m = model;
286         m.createReifiedStatement( aURI, st );
287         assertTrue( "st is now reified", st.isReified() );
288         m.removeAllReifications( st );
289         assertFalse( "st is no longer reified", st.isReified() );
290         }
291     
292     /**
293         Leo Bard spotted a problem whereby removing a reified statement from a model
294         with style Standard didn't leave the model empty. Here's a test for it.
295     */

296     public void testLeosBug()
297         {
298         Model A = getModel();
299         Statement st = statement( A, "pigs fly south" );
300         ReifiedStatement rst = st.createReifiedStatement( "eh:pointer" );
301         A.removeReification( rst );
302         assertIsoModels( ModelFactory.createDefaultModel(), A );
303         }
304     
305     public void testRR()
306         {
307         Statement st = SPO;
308         Model m = model;
309         ReifiedStatement rs1 = m.createReifiedStatement( aURI, st );
310         ReifiedStatement rs2 = m.createReifiedStatement( anotherURI, st );
311         m.removeReification( rs1 );
312         testNotReifying( m, aURI );
313         assertTrue( "st is still reified", st.isReified() );
314         m.removeReification( rs2 );
315         assertFalse( "st should no longer be reified", st.isReified() );
316         }
317     
318     private void testNotReifying( Model m, String JavaDoc uri )
319         {
320         try
321             {
322             m.createResource( uri ).as( ReifiedStatement.class );
323             fail( "there should be no reifiedStatement for " + uri );
324             }
325         catch (DoesNotReifyException e)
326             { /* that's what we require */ }
327         }
328
329     public void testDoesNotReifyElsewhere()
330         {
331         final String JavaDoc uri = "spoo:rubbish";
332         Model m2 = getModel();
333         model.createReifiedStatement( uri, SPO );
334         testDoesNotReify( "blue model should not reify rubbish", m2.createResource( uri ) );
335         }
336 // public void testXXX()
337
// {
338
// String root = "http://root/root#";
339
// Model m = ModelFactory.createDefaultModel();
340
// Model r = ModelFactory.createRDFSModel( m );
341
// Resource S = r.createResource( root + "S" );
342
// Property P = r.createProperty( root + "P" );
343
// RDFNode O = r.createResource( root + "O" );
344
// Statement st = r.createStatement( S, P, O );
345
// ReifiedStatement rs = st.createReifiedStatement( root + "RS" );
346
// }
347
}
348
349
350 /*
351     (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
352     All rights reserved.
353
354     Redistribution and use in source and binary forms, with or without
355     modification, are permitted provided that the following conditions
356     are met:
357
358     1. Redistributions of source code must retain the above copyright
359        notice, this list of conditions and the following disclaimer.
360
361     2. Redistributions in binary form must reproduce the above copyright
362        notice, this list of conditions and the following disclaimer in the
363        documentation and/or other materials provided with the distribution.
364
365     3. The name of the author may not be used to endorse or promote products
366        derived from this software without specific prior written permission.
367
368     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
369     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
370     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
371     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
372     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
373     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
374     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
375     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
376     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
377     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
378 */
Popular Tags