KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > util > iterator > test > TestResourceUtils


1 /*****************************************************************************
2  * Source code information
3  * -----------------------
4  * Original author Ian Dickinson, HP Labs Bristol
5  * Author email Ian.Dickinson@hp.com
6  * Package Jena 2
7  * Web http://sourceforge.net/projects/jena/
8  * Created 06-Jun-2003
9  * Filename $RCSfile: TestResourceUtils.java,v $
10  * Revision $Revision: 1.8 $
11  * Release status $State: Exp $
12  *
13  * Last modified on $Date: 2005/02/21 12:19:20 $
14  * by $Author: andy_seaborne $
15  *
16  * (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
17  * (see footer for full conditions)
18  *****************************************************************************/

19
20 // Package
21
///////////////
22
package com.hp.hpl.jena.util.iterator.test;
23
24
25 // Imports
26
///////////////
27
import com.hp.hpl.jena.rdf.model.*;
28 import com.hp.hpl.jena.vocabulary.RDFS;
29 import com.hp.hpl.jena.util.*;
30
31 import junit.framework.*;
32
33 import java.util.*;
34
35
36 /**
37  * <p>
38  * Unit tests on resource utilities
39  * </p>
40  *
41  * @author Ian Dickinson, HP Labs
42  * (<a HREF="mailto:Ian.Dickinson@hp.com" >email</a>)
43  * @version CVS $Id: TestResourceUtils.java,v 1.8 2005/02/21 12:19:20 andy_seaborne Exp $
44  */

45 public class TestResourceUtils
46     extends TestCase
47 {
48     // Constants
49
//////////////////////////////////
50

51     public static final String JavaDoc NS = "http://jena.hp.com/test#";
52     
53     // Static variables
54
//////////////////////////////////
55

56     // Instance variables
57
//////////////////////////////////
58

59     // Constructors
60
//////////////////////////////////
61

62     public TestResourceUtils( String JavaDoc name ) {
63         super( name );
64     }
65     
66     // External signature methods
67
//////////////////////////////////
68

69     public void testMaximalLowerElements() {
70         Model m = ModelFactory.createDefaultModel();
71         
72         Resource a = m.createResource( NS + "a" );
73         Resource b = m.createResource( NS + "b" );
74         Resource c = m.createResource( NS + "c" );
75         Resource d = m.createResource( NS + "d" );
76         
77         b.addProperty( RDFS.subClassOf, a );
78         c.addProperty( RDFS.subClassOf, a );
79         d.addProperty( RDFS.subClassOf, c );
80         d.addProperty( RDFS.subClassOf, a );
81         
82         List abcd = Arrays.asList( new Object JavaDoc[] {a,b,c,d} );
83         List bcd = Arrays.asList( new Object JavaDoc[] {b,c,d} );
84         List cd = Arrays.asList( new Object JavaDoc[] {c,d} );
85
86         assertEquals( "Wrong number of remaining resources", 1, ResourceUtils.maximalLowerElements( abcd, RDFS.subClassOf, true ).size() );
87         assertEquals( "Result should be a", a, ResourceUtils.maximalLowerElements( abcd, RDFS.subClassOf, true ).iterator().next() );
88         assertEquals( "Wrong number of remaining resources", 2, ResourceUtils.maximalLowerElements( bcd, RDFS.subClassOf, true ).size() );
89         assertEquals( "Wrong number of remaining resources", 1, ResourceUtils.maximalLowerElements( cd, RDFS.subClassOf, true ).size() );
90         assertEquals( "Result should be a", c, ResourceUtils.maximalLowerElements( cd, RDFS.subClassOf, true ).iterator().next() );
91     }
92     
93     public void testRenameResource() {
94         Model m = ModelFactory.createDefaultModel();
95         
96         Resource a = m.createResource( NS + "a" );
97         Resource b = m.createResource( NS + "b" );
98         Resource c = m.createResource( NS + "c" );
99         Resource d = m.createResource( NS + "d" );
100         
101         Property p = m.createProperty( NS, "p" );
102         Property q = m.createProperty( NS, "q" );
103         
104         a.addProperty( p, b );
105         a.addProperty( q, c );
106         d.addProperty( p, a );
107         d.addProperty( p, b );
108         
109         // now rename a to e
110
Resource e = ResourceUtils.renameResource( a, NS + "e" );
111         
112         assertTrue( "should be no properties of a", !a.listProperties().hasNext() );
113         assertEquals( "uri of a", NS + "a", a.getURI() );
114         assertEquals( "uri of e", NS + "e", e.getURI() );
115
116         assertTrue( "d should not have p a", !d.hasProperty( p, a ));
117         assertTrue( "d should have p e", d.hasProperty( p, e ));
118         
119         assertTrue( "e should have p b", e.hasProperty( p, b ) );
120         assertTrue( "e should have q c", e.hasProperty( q, c ) );
121         
122         assertTrue( "d p b should be unchanged", d.hasProperty( p, b ) );
123         
124         // now rename e to anon
125
Resource anon = ResourceUtils.renameResource( e, null );
126         
127         assertTrue( "should be no properties of e", !e.listProperties().hasNext() );
128         assertEquals( "uri of e", NS + "e", e.getURI() );
129         assertTrue( "anon", anon.isAnon() );
130
131         assertTrue( "d should not have p e", !d.hasProperty( p, e ));
132         assertTrue( "d should have p anon", d.hasProperty( p, anon ));
133         
134         assertTrue( "anon should have p b", anon.hasProperty( p, b ) );
135         assertTrue( "anon should have q c", anon.hasProperty( q, c ) );
136         
137         assertTrue( "d p b should be unchanged", d.hasProperty( p, b ) );
138         
139         // reflexive case
140
Resource f = m.createResource( NS + "f" );
141         f.addProperty( p, f );
142         
143         Resource f1 = ResourceUtils.renameResource( f, NS +"f1" );
144         assertFalse( "Should be no f statements", m.listStatements( f, null, (RDFNode) null).hasNext() );
145         assertTrue( "f1 has p f1", f1.hasProperty( p, f1 ) );
146     }
147     
148     public void testReachableGraphClosure() {
149         Model m0 = ModelFactory.createDefaultModel();
150         Resource a = m0.createResource( "a" );
151         Resource b = m0.createResource( "b" );
152         Resource c = m0.createResource( "c" );
153         Resource d = m0.createResource( "d" );
154         Property p = m0.createProperty( "p" );
155         
156         m0.add( a, p, b );
157         m0.add( a, p, c );
158         m0.add( b, p, b ); // unit loop
159
m0.add( b, p, a ); // loop
160
m0.add( d, p, a ); // not reachable from a
161

162         Model m1 = ModelFactory.createDefaultModel();
163         m1.add( a, p, b );
164         m1.add( a, p, c );
165         m1.add( b, p, b );
166         m1.add( b, p, a );
167         
168         assertTrue( "m1 should be isomorphic with the reachable sub-graph from a", m1.isIsomorphicWith( ResourceUtils.reachableClosure(a)));
169     }
170     
171     public void testRemoveEquiv() {
172         Model m = ModelFactory.createDefaultModel();
173         
174         Resource a = m.createResource( NS + "a" );
175         Resource b = m.createResource( NS + "b" );
176         Resource c = m.createResource( NS + "c" );
177         Resource d = m.createResource( NS + "d" );
178         Resource e = m.createResource( NS + "e" );
179         
180         b.addProperty( RDFS.subClassOf, a );
181         a.addProperty( RDFS.subClassOf, b ); // a,b are equivalent
182
d.addProperty( RDFS.subClassOf, e );
183         e.addProperty( RDFS.subClassOf, d ); // d,e are equivalent
184

185         // reflexive relations - would be inferred by inf engine
186
a.addProperty( RDFS.subClassOf, a );
187         b.addProperty( RDFS.subClassOf, b );
188         c.addProperty( RDFS.subClassOf, c );
189         d.addProperty( RDFS.subClassOf, d );
190         e.addProperty( RDFS.subClassOf, e );
191         
192         List abcde = Arrays.asList( new Object JavaDoc[] {a,b,c,d,e} );
193         List ab = Arrays.asList( new Object JavaDoc[] {a,b} );
194         List cde = Arrays.asList( new Object JavaDoc[] {c,d,e} );
195         List abde = Arrays.asList( new Object JavaDoc[] {a,b,d,e} );
196         List de = Arrays.asList( new Object JavaDoc[] {d,e} );
197
198         List in = new ArrayList();
199         in.addAll( abcde );
200         List out = null;
201         assertTrue( in.equals( abcde ) );
202         assertFalse( in.equals( cde ));
203         assertNull( out );
204         
205         out = ResourceUtils.removeEquiv( in, RDFS.subClassOf, a );
206
207         assertFalse( in.equals( abcde ) );
208         assertTrue( in.equals( cde ));
209         assertNotNull( out );
210         assertEquals( out, ab );
211         
212         out = ResourceUtils.removeEquiv( in, RDFS.subClassOf, e );
213
214         assertFalse( in.equals( abcde ) );
215         assertTrue( in.equals( Collections.singletonList( c ) ));
216         assertNotNull( out );
217         assertEquals( out, de );
218     }
219     
220     public void testPartition() {
221         Model m = ModelFactory.createDefaultModel();
222         
223         Resource a = m.createResource( NS + "a" );
224         Resource b = m.createResource( NS + "b" );
225         Resource c = m.createResource( NS + "c" );
226         Resource d = m.createResource( NS + "d" );
227         Resource e = m.createResource( NS + "e" );
228         
229         b.addProperty( RDFS.subClassOf, a );
230         a.addProperty( RDFS.subClassOf, b ); // a,b are equivalent
231
d.addProperty( RDFS.subClassOf, e );
232         e.addProperty( RDFS.subClassOf, d ); // d,e are equivalent
233

234         // reflexive relations - would be inferred by inf engine
235
a.addProperty( RDFS.subClassOf, a );
236         b.addProperty( RDFS.subClassOf, b );
237         c.addProperty( RDFS.subClassOf, c );
238         d.addProperty( RDFS.subClassOf, d );
239         e.addProperty( RDFS.subClassOf, e );
240         
241         List abcde = Arrays.asList( new Object JavaDoc[] {a,b,c,d,e} );
242         List ab = Arrays.asList( new Object JavaDoc[] {b,a} );
243         List cc = Arrays.asList( new Object JavaDoc[] {c} );
244         List de = Arrays.asList( new Object JavaDoc[] {e,d} );
245
246         List partition = ResourceUtils.partition( abcde, RDFS.subClassOf );
247         assertEquals( "Should be 3 partitions", 3, partition.size() );
248         assertEquals( "First parition should be (a,b)", ab, partition.get(0) );
249         assertEquals( "First parition should be (c)", cc, partition.get(1) );
250         assertEquals( "First parition should be (d,e)", de, partition.get(2) );
251     }
252
253     
254     // Internal implementation methods
255
//////////////////////////////////
256

257     //==============================================================================
258
// Inner class definitions
259
//==============================================================================
260

261 }
262
263
264 /*
265     (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
266     All rights reserved.
267
268     Redistribution and use in source and binary forms, with or without
269     modification, are permitted provided that the following conditions
270     are met:
271
272     1. Redistributions of source code must retain the above copyright
273        notice, this list of conditions and the following disclaimer.
274
275     2. Redistributions in binary form must reproduce the above copyright
276        notice, this list of conditions and the following disclaimer in the
277        documentation and/or other materials provided with the distribution.
278
279     3. The name of the author may not be used to endorse or promote products
280        derived from this software without specific prior written permission.
281
282     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
283     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
284     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
285     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
286     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
287     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
288     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
289     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
290     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
291     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
292 */

293
Popular Tags