1 19 20 package com.hp.hpl.jena.util.iterator.test; 23 24 25 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 45 public class TestResourceUtils 46 extends TestCase 47 { 48 51 public static final String NS = "http://jena.hp.com/test#"; 52 53 56 59 62 public TestResourceUtils( String name ) { 63 super( name ); 64 } 65 66 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 [] {a,b,c,d} ); 83 List bcd = Arrays.asList( new Object [] {b,c,d} ); 84 List cd = Arrays.asList( new Object [] {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 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 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 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 ); m0.add( b, p, a ); m0.add( d, p, a ); 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 ); d.addProperty( RDFS.subClassOf, e ); 183 e.addProperty( RDFS.subClassOf, d ); 185 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 [] {a,b,c,d,e} ); 193 List ab = Arrays.asList( new Object [] {a,b} ); 194 List cde = Arrays.asList( new Object [] {c,d,e} ); 195 List abde = Arrays.asList( new Object [] {a,b,d,e} ); 196 List de = Arrays.asList( new Object [] {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 ); d.addProperty( RDFS.subClassOf, e ); 232 e.addProperty( RDFS.subClassOf, d ); 234 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 [] {a,b,c,d,e} ); 242 List ab = Arrays.asList( new Object [] {b,a} ); 243 List cc = Arrays.asList( new Object [] {c} ); 244 List de = Arrays.asList( new Object [] {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 257 261 } 262 263 264 293 | Popular Tags |