1 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 28 29 public class TestTransitiveGraphCache extends TestCase { 30 31 32 TransitiveGraphCache cache; 33 34 String 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 50 public TestTransitiveGraphCache( String name ) { 51 super( name ); 52 } 53 54 58 public static TestSuite suite() { 59 return new TestSuite( TestTransitiveGraphCache.class ); 60 } 65 66 70 public void testBasicCache() { 71 initCache(); 72 cache.setCaching(false); 73 doBasicTest(cache); 74 } 75 76 80 public void testCachingCache() { 81 initCache(); 82 cache.setCaching(true); 83 doBasicTest(cache); 84 } 85 86 89 public void testCloning() { 90 initCache(); 91 TransitiveGraphCache clone = cache.deepCopy(); 92 cache.addRelation(new Triple(a, closedP, d)); 94 cache.addRelation(new Triple(g, closedP, a)); 95 doBasicTest(clone); 96 } 97 98 101 private void initCache() { 102 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)); cache.addRelation(new Triple(a, closedP, e)); cache.addRelation(new Triple(d, closedP, b)); 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 TestUtil.assertIteratorValues(this, 128 cache.find(new TriplePattern(a, directP, null)), 129 new Object [] { 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 [] { 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 [] { 146 new Triple(a, closedP, g), 147 }); 148 149 TestUtil.assertIteratorValues(this, 151 cache.find(new TriplePattern(null, directP, f)), 152 new Object [] { 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 [] { 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 TestUtil.assertIteratorValues(this, 170 cache.find(new TriplePattern(null, directP, null)), 171 new Object [] { 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 [] { 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 cache.addRelation(new Triple(g, closedP, e)); 220 221 TestUtil.assertIteratorValues(this, 222 cache.find(new TriplePattern(e, directP, null)), 223 new Object [] { 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 [] { 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 [] { 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 [] { 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 [] { 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 [] { 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 [] { 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 [] { 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 [] { 286 new Triple(f, closedP, g), 287 new Triple(f, closedP, e), 288 new Triple(f, closedP, f) 289 }); 290 300 } 301 302 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 [] { 315 new Triple(a, closedP, a), 316 new Triple(a, closedP, b), 317 new Triple(a, closedP, c), 318 }); 319 320 } 321 322 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 [] { 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 [] { 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 [] { 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 [] { 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 [] { 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 [] { 375 new Triple(b, closedP, b), 376 }); 377 } 378 379 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 [] { 391 new Triple(a, closedP, a), 392 new Triple(a, closedP, b), 393 }); 394 } 395 396 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 [] { 408 new Triple(a, closedP, a), 409 new Triple(a, closedP, b), 410 new Triple(a, closedP, c), 411 }); 412 } 413 414 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 [] { 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 [] { 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 [] { 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 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 [] { 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 [] { 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 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 542 | Popular Tags |