|
TestAnnotation |
|
1 /* 2 * TestAnnotation.java 3 * 4 * Copyright (c) 1998-2001, The University of Sheffield. 5 * 6 * This file is part of GATE (see http://gate.ac.uk/), and is free 7 * software, licenced under the GNU Library General Public License, 8 * Version 2, June 1991 (in the distribution as file licence.html, 9 * and also available at http://gate.ac.uk/gate/licence.html). 10 * 11 * Hamish Cunningham, 7/Feb/00 12 * 13 * $Id: TestAnnotation.java,v 1.41 2002/03/06 17:15:38 kalina Exp $ 14 */ 15 16 package gate.annotation; 17 18 import java.util.*; 19 import java.io.*; 20 import junit.framework.*; 21 import java.net.*; 22 23 import gate.*; 24 import gate.util.*; 25 import gate.corpora.*; 26 27 /** Tests for the Annotation classes 28 */ 29 public class TestAnnotation extends TestCase 30 { 31 /** Debug flag */ 32 private static final boolean DEBUG = false; 33 34 /** Construction */ 35 public TestAnnotation(String name) { super(name); } 36 37 /** A document */ 38 protected Document doc1; 39 40 /** An annotation set */ 41 protected AnnotationSet basicAS; 42 43 /** An empty feature map */ 44 protected FeatureMap emptyFeatureMap; 45 46 /** Fixture set up */ 47 public void setUp() throws Exception 48 { 49 String server = TestDocument.getTestServerName(); 50 assertNotNull(server); 51 FeatureMap params = Factory.newFeatureMap(); 52 params.put(Document.DOCUMENT_URL_PARAMETER_NAME, Gate.getUrl("tests/doc0.html")); 53 params.put(Document.DOCUMENT_MARKUP_AWARE_PARAMETER_NAME, "false"); 54 doc1 = (Document)Factory.createResource("gate.corpora.DocumentImpl", 55 params); 56 57 emptyFeatureMap = new SimpleFeatureMapImpl(); 58 59 basicAS = new AnnotationSetImpl(doc1); 60 FeatureMap fm = new SimpleFeatureMapImpl(); 61 62 basicAS.get("T"); // to trigger type indexing 63 basicAS.get(new Long(0)); // trigger offset index (though add will too) 64 65 basicAS.add(new Long(10), new Long(20), "T1", fm); // 0 66 basicAS.add(new Long(10), new Long(20), "T2", fm); // 1 67 basicAS.add(new Long(10), new Long(20), "T3", fm); // 2 68 basicAS.add(new Long(10), new Long(20), "T1", fm); // 3 69 70 fm = new SimpleFeatureMapImpl(); 71 fm.put("pos", "NN"); 72 fm.put("author", "hamish"); 73 fm.put("version", new Integer(1)); 74 75 basicAS.add(new Long(10), new Long(20), "T1", fm); // 4 76 basicAS.add(new Long(15), new Long(40), "T1", fm); // 5 77 basicAS.add(new Long(15), new Long(40), "T3", fm); // 6 78 basicAS.add(new Long(15), new Long(40), "T1", fm); // 7 79 80 fm = new SimpleFeatureMapImpl(); 81 fm.put("pos", "JJ"); 82 fm.put("author", "the devil himself"); 83 fm.put("version", new Long(44)); 84 fm.put("created", "monday"); 85 86 basicAS.add(new Long(15), new Long(40), "T3", fm); // 8 87 basicAS.add(new Long(15), new Long(40), "T1", fm); // 9 88 basicAS.add(new Long(15), new Long(40), "T1", fm); // 10 89 90 // Out.println(basicAS); 91 } // setUp 92 93 94 /** Test indexing by offset */ 95 public void testOffsetIndex() throws InvalidOffsetException { 96 AnnotationSet as = new AnnotationSetImpl(doc1); 97 AnnotationSet asBuf; 98 Integer newId; 99 FeatureMap fm = new SimpleFeatureMapImpl(); 100 Annotation a; 101 Node startNode; 102 Node endNode; 103 104 newId = as.add(new Long(10), new Long(20), "T", fm); 105 assertEquals(newId.intValue(), 11); 106 a = as.get(newId); 107 108 startNode = a.getStartNode(); 109 endNode = a.getEndNode(); 110 assertEquals(startNode.getId().intValue(), 4); 111 assertEquals(endNode.getId().intValue(), 5); 112 assertEquals(startNode.getOffset().longValue(), 10); 113 assertEquals(endNode.getOffset().longValue(), 20); 114 115 newId = as.add(new Long(10), new Long(30), "T", fm); 116 assertEquals(newId.intValue(), 12); 117 a = as.get(newId); 118 119 startNode = a.getStartNode(); 120 endNode = a.getEndNode(); 121 assertEquals(startNode.getId().intValue(), 4); 122 assertEquals(endNode.getId().intValue(), 6); 123 assertEquals(startNode.getOffset().longValue(), 10); 124 assertEquals(endNode.getOffset().longValue(), 30); 125 126 asBuf = as.get(new Long(10)); 127 assertEquals(asBuf.size(), 2); 128 129 } // testOffsetIndex() 130 131 /** Test exception throwing */ 132 public void testExceptions() { 133 AnnotationSet as = new AnnotationSetImpl(doc1); 134 boolean threwUp = false; 135 136 try { 137 as.add(new Long(-1), new Long(1), "T", emptyFeatureMap); 138 } catch (InvalidOffsetException e) { 139 threwUp = true; 140 } 141 142 if(! threwUp) fail("Should have thrown InvalidOffsetException"); 143 threwUp = false; 144 145 try { 146 as.add(new Long(1), new Long(-1), "T", emptyFeatureMap); 147 } catch (InvalidOffsetException e) { 148 threwUp = true; 149 } 150 151 if(! threwUp) fail("Should have thrown InvalidOffsetException"); 152 threwUp = false; 153 154 try { 155 as.add(new Long(1), new Long(0), "T", emptyFeatureMap); 156 } catch (InvalidOffsetException e) { 157 threwUp = true; 158 } 159 160 if(! threwUp) fail("Should have thrown InvalidOffsetException"); 161 threwUp = false; 162 163 try { 164 as.add(null, new Long(1), "T", emptyFeatureMap); 165 } catch (InvalidOffsetException e) { 166 threwUp = true; 167 } 168 169 if(! threwUp) fail("Should have thrown InvalidOffsetException"); 170 threwUp = false; 171 172 try { 173 as.add(new Long(1), null, "T", emptyFeatureMap); 174 } catch (InvalidOffsetException e) { 175 threwUp = true; 176 } 177 178 if(! threwUp) fail("Should have thrown InvalidOffsetException"); 179 threwUp = false; 180 181 try { 182 as.add(new Long(999999), new Long(100000000), "T", emptyFeatureMap); 183 } catch (InvalidOffsetException e) { 184 threwUp = true; 185 } 186 187 /* 188 // won't work until the doc size check is implemented 189 if(! threwUp) fail("Should have thrown InvalidOffsetException"); 190 */ 191 threwUp = false; 192 193 } // testExceptions() 194 195 /** Test type index */ 196 public void testTypeIndex() throws Exception { 197 FeatureMap params = Factory.newFeatureMap(); 198 params.put(Document.DOCUMENT_URL_PARAMETER_NAME, Gate.getUrl("tests/doc0.html")); 199 params.put(Document.DOCUMENT_MARKUP_AWARE_PARAMETER_NAME, "false"); 200 Document doc = (Document)Factory.createResource("gate.corpora.DocumentImpl", 201 params); 202 AnnotationSet as = new AnnotationSetImpl(doc); 203 AnnotationSet asBuf; 204 Integer newId; 205 FeatureMap fm = new SimpleFeatureMapImpl(); 206 Annotation a; 207 Node startNode; 208 Node endNode; 209 210 // to trigger type indexing 211 as.get("T"); 212 as.add(new Long(10), new Long(20), "T1", fm); // 0 213 as.add(new Long(10), new Long(20), "T2", fm); // 1 214 as.add(new Long(10), new Long(20), "T3", fm); // 2 215 as.add(new Long(10), new Long(20), "T1", fm); // 3 216 as.add(new Long(10), new Long(20), "T1", fm); // 4 217 as.add(new Long(10), new Long(20), "T1", fm); // 5 218 as.add(new Long(10), new Long(20), "T3", fm); // 6 219 as.add(new Long(10), new Long(20), "T1", fm); // 7 220 as.add(new Long(10), new Long(20), "T3", fm); // 8 221 as.add(new Long(10), new Long(20), "T1", fm); // 9 222 as.add(new Long(10), new Long(20), "T1", fm); // 10 223 224 asBuf = as.get("T"); 225 assertEquals(null, asBuf); 226 227 asBuf = as.get("T1"); 228 assertEquals(7, asBuf.size()); 229 asBuf = as.get("T2"); 230 assertEquals(1, asBuf.size()); 231 asBuf = as.get("T3"); 232 assertEquals(3, asBuf.size()); 233 234 // let's check that we've only got two nodes, what the ids are and so on; 235 // first construct a sorted set of annotations 236 SortedSet sortedAnnots = new TreeSet(as); 237 238 // for checking the annotation id 239 int idCounter = 0; 240 Iterator iter = sortedAnnots.iterator(); 241 while(iter.hasNext()) { 242 a = (Annotation) iter.next(); 243 244 // check annot ids 245 assertEquals(idCounter++, a.getId().intValue()); 246 247 startNode = a.getStartNode(); 248 endNode = a.getEndNode(); 249 250 // start node id 251 assertEquals(0, startNode.getId().intValue()); 252 253 // start offset 254 assertEquals(10, startNode.getOffset().longValue()); 255 256 // end id 257 assertEquals(1, endNode.getId().intValue()); 258 259 // end offset 260 assertEquals(20, endNode.getOffset().longValue()); 261 } 262 263 } // testTypeIndex() 264 265 /** Test the annotations set add method that uses existing nodes */ 266 public void testAddWithNodes() throws Exception { 267 FeatureMap params = Factory.newFeatureMap(); 268 params.put(Document.DOCUMENT_URL_PARAMETER_NAME, Gate.getUrl("tests/doc0.html")); 269 params.put(Document.DOCUMENT_MARKUP_AWARE_PARAMETER_NAME, "false"); 270 Document doc = (Document)Factory.createResource("gate.corpora.DocumentImpl", 271 params); 272 AnnotationSet as = new AnnotationSetImpl(doc); 273 AnnotationSet asBuf; 274 Integer newId; 275 FeatureMap fm = new SimpleFeatureMapImpl(); 276 Annotation a; 277 Node startNode; 278 Node endNode; 279 280 // to trigger type indexing 281 as.get("T"); 282 newId = as.add(new Long(10), new Long(20), "T1", fm); // 0 283 a = as.get(newId); 284 startNode = a.getStartNode(); 285 endNode = a.getEndNode(); 286 287 as.add(startNode, endNode, "T2", fm); // 1 288 as.add(startNode, endNode, "T3", fm); // 2 289 as.add(startNode, endNode, "T1", fm); // 3 290 as.add(startNode, endNode, "T1", fm); // 4 291 as.add(startNode, endNode, "T1", fm); // 5 292 as.add(startNode, endNode, "T3", fm); // 6 293 as.add(startNode, endNode, "T1", fm); // 7 294 as.add(startNode, endNode, "T3", fm); // 8 295 as.add(startNode, endNode, "T1", fm); // 9 296 as.add(startNode, endNode, "T1", fm); // 10 297 298 asBuf = as.get("T"); 299 assertEquals(null, asBuf); 300 301 asBuf = as.get("T1"); 302 assertEquals(7, asBuf.size()); 303 asBuf = as.get("T2"); 304 assertEquals(1, asBuf.size()); 305 asBuf = as.get("T3"); 306 assertEquals(3, asBuf.size()); 307 308 // let's check that we've only got two nodes, what the ids are and so on; 309 // first construct a sorted set of annotations 310 SortedSet sortedAnnots = new TreeSet(as); 311 312 // for checking the annotation id 313 int idCounter = 0; 314 Iterator iter = sortedAnnots.iterator(); 315 while(iter.hasNext()) { 316 a = (Annotation) iter.next(); 317 // check annot ids 318 assertEquals(idCounter++, a.getId().intValue()); 319 320 startNode = a.getStartNode(); 321 endNode = a.getEndNode(); 322 323 // start node id 324 assertEquals(0, startNode.getId().intValue()); 325 326 // start offset 327 assertEquals(10, startNode.getOffset().longValue()); 328 329 // end id 330 assertEquals(1, endNode.getId().intValue()); 331 332 // end offset 333 assertEquals(20, endNode.getOffset().longValue()); 334 } 335 336 } // testAddWithNodes() 337 338 /** Test complex get (with type, offset and feature contraints) */ 339 public void testComplexGet() throws InvalidOffsetException { 340 AnnotationSet as = basicAS; 341 AnnotationSet asBuf; 342 Integer newId; 343 FeatureMap fm = new SimpleFeatureMapImpl(); 344 Annotation a; 345 Node startNode; 346 Node endNode; 347 348 FeatureMap constraints = new SimpleFeatureMapImpl(); 349 constraints.put("pos", "NN"); 350 351 //Out.println(basicAS); 352 //Out.println(constraints); 353 354 asBuf = basicAS.get("T1", constraints); 355 assertEquals(3, asBuf.size()); 356 asBuf = basicAS.get("T3", constraints); 357 assertEquals(1, asBuf.size()); 358 asBuf = basicAS.get("T1", constraints, new Long(12)); 359 assertEquals(2, asBuf.size()); 360 asBuf = basicAS.get("T1", constraints, new Long(10)); 361 assertEquals(1, asBuf.size()); 362 asBuf = basicAS.get("T1", constraints, new Long(11)); 363 assertEquals(2, asBuf.size()); 364 asBuf = basicAS.get("T1", constraints, new Long(9)); 365 assertEquals(1, asBuf.size()); 366 367 constraints.put("pos", "JJ"); 368 //Out.println(constraints); 369 asBuf = basicAS.get("T1", constraints, new Long(0)); 370 assertEquals(null, asBuf); 371 asBuf = basicAS.get("T1", constraints, new Long(14)); 372 assertEquals(2, asBuf.size()); 373 374 constraints.put("author", "valentin"); 375 asBuf = basicAS.get("T1", constraints, new Long(14)); 376 assertEquals(null, asBuf); 377 378 constraints.put("author", "the devil himself"); 379 asBuf = basicAS.get("T1", constraints, new Long(14)); 380 assertEquals(2, asBuf.size()); 381 382 asBuf = basicAS.get("T1", constraints, new Long(5)); 383 assertEquals(null, asBuf); 384 385 constraints.put("this feature isn't", "there at all"); 386 asBuf = basicAS.get("T1", constraints, new Long(14)); 387 assertEquals(null, asBuf); 388 389 } // testComplexGet() 390 391 /** Test remove */ 392 public void testRemove() { 393 AnnotationSet asBuf = basicAS.get("T1"); 394 assertEquals(7, asBuf.size()); 395 asBuf = basicAS.get(new Long(9)); 396 assertEquals(5, asBuf.size()); 397 398 basicAS.remove(basicAS.get(new Integer(0))); 399 400 assertEquals(10, basicAS.size()); 401 assertEquals(10, ((AnnotationSetImpl) basicAS).annotsById.size()); 402 403 asBuf = basicAS.get("T1"); 404 assertEquals(6, asBuf.size()); 405 406 asBuf = basicAS.get(new Long(9)); 407 assertEquals(4, asBuf.size()); 408 assertEquals(null, basicAS.get(new Integer(0))); 409 basicAS.remove(basicAS.get(new Integer(8))); 410 assertEquals(9, basicAS.size()); 411 basicAS.removeAll(basicAS); 412 assertEquals(null, basicAS.get()); 413 assertEquals(null, basicAS.get("T1")); 414 assertEquals(null, basicAS.get(new Integer(0))); 415 } // testRemove() 416 417 public void testRemoveInexistant() throws Exception{ 418 basicAS.add(new Long(0), new Long(10), "Foo", emptyFeatureMap); 419 Annotation ann = (Annotation)basicAS.get("Foo").iterator().next(); 420 basicAS.remove(ann); 421 //the second remove should do nothing... 422 basicAS.remove(ann); 423 } 424 425 /** Test iterator remove */ 426 public void testIteratorRemove() { 427 AnnotationSet asBuf = basicAS.get("T1"); 428 assertEquals(7, asBuf.size()); 429 asBuf = basicAS.get(new Long(9)); 430 assertEquals(5, asBuf.size()); 431 432 // remove annotation with id 0; this is returned last by the 433 // iterator 434 Iterator iter = basicAS.iterator(); 435 while(iter.hasNext()) 436 iter.next(); 437 iter.remove(); 438 439 assertEquals(10, basicAS.size()); 440 assertEquals(10, ((AnnotationSetImpl) basicAS).annotsById.size()); 441 asBuf = basicAS.get("T1"); 442 assertEquals(6, asBuf.size()); 443 asBuf = basicAS.get(new Long(9)); 444 assertEquals(4, asBuf.size()); 445 assertEquals(null, basicAS.get(new Integer(0))); 446 basicAS.remove(basicAS.get(new Integer(8))); 447 448 } // testIteratorRemove() 449 450 /** Test iterator */ 451 public void testIterator() { 452 Iterator iter = basicAS.iterator(); 453 Annotation[] annots = new Annotation[basicAS.size()]; 454 int i = 0; 455 456 while(iter.hasNext()) { 457 Annotation a = (Annotation) iter.next(); 458 annots[i++] = a; 459 460 assertTrue(basicAS.contains(a)); 461 iter.remove(); 462 assertTrue(!basicAS.contains(a)); 463 } // while 464 465 i = 0; 466 while(i < annots.length) { 467 basicAS.add(annots[i++]); 468 assertEquals(i, basicAS.size()); 469 } // while 470 471 AnnotationSet asBuf = basicAS.get("T1"); 472 assertEquals(7, asBuf.size()); 473 asBuf = basicAS.get(new Long(9)); 474 assertEquals(5, asBuf.size()); 475 } // testIterator 476 477 /** Test Set methods */ 478 public void testSetMethods() { 479 Annotation a = basicAS.get(new Integer(6)); 480 assertTrue(basicAS.contains(a)); 481 482 Annotation[] annotArray = 483 (Annotation[]) basicAS.toArray(new Annotation[0]); 484 Object[] annotObjectArray = basicAS.toArray(); 485 assertEquals(11, annotArray.length); 486 assertEquals(11, annotObjectArray.length); 487 488 SortedSet sortedAnnots = new TreeSet(basicAS); 489 annotArray = (Annotation[]) sortedAnnots.toArray(new Annotation[0]); 490 for(int i = 0; i<11; i++) 491 assertTrue( annotArray[i].getId().equals(new Integer(i)) ); 492 493 Annotation a1 = basicAS.get(new Integer(3)); 494 Annotation a2 = basicAS.get(new Integer(4)); 495 Set a1a2 = new HashSet(); 496 a1a2.add(a1); 497 a1a2.add(a2); 498 assertTrue(basicAS.contains(a1)); 499 assertTrue(basicAS.containsAll(a1a2)); 500 basicAS.removeAll(a1a2); 501 502 assertEquals(9, basicAS.size()); 503 assertTrue(! basicAS.contains(a1)); 504 assertTrue(! basicAS.containsAll(a1a2)); 505 506 basicAS.addAll(a1a2); 507 assertTrue(basicAS.contains(a2)); 508 assertTrue(basicAS.containsAll(a1a2)); 509 510 assertTrue(basicAS.retainAll(a1a2)); 511 assertTrue(basicAS.equals(a1a2)); 512 513 basicAS.clear(); 514 assertTrue(basicAS.isEmpty()); 515 516 } // testSetMethods() 517 518 /** Test AnnotationSetImpl */ 519 public void testAnnotationSet() throws Exception { 520 // constuct an empty AS 521 FeatureMap params = Factory.newFeatureMap(); 522 params.put(Document.DOCUMENT_URL_PARAMETER_NAME, Gate.getUrl("tests/doc0.html")); 523 params.put(Document.DOCUMENT_MARKUP_AWARE_PARAMETER_NAME, "false"); 524 Document doc = (Document)Factory.createResource("gate.corpora.DocumentImpl", 525 params); 526 527 AnnotationSet as = new AnnotationSetImpl(doc); 528 assertEquals(as.size(), 0); 529 530 // add some annotations 531 FeatureMap fm1 = Factory.newFeatureMap(); 532 fm1.put("test", "my-value"); 533 FeatureMap fm2 = Factory.newFeatureMap(); 534 fm2.put("test", "my-value-different"); 535 FeatureMap fm3 = Factory.newFeatureMap(); 536 fm3.put("another test", "different my-value"); 537 538 Integer newId; 539 newId = 540 as.add(new Long(0), new Long(10), "Token", fm1); 541 assertEquals(newId.intValue(), 0); 542 newId = 543 as.add(new Long(11), new Long(12), "Token", fm2); 544 assertEquals(newId.intValue(), 1); 545 assertEquals(as.size(), 2); 546 assertTrue(! as.isEmpty()); 547 newId = 548 as.add(new Long(15), new Long(22), "Syntax", fm1); 549 550 // get by ID; remove; add(object) 551 Annotation a = as.get(new Integer(1)); 552 as.remove(a); 553 assertEquals(as.size(), 2); 554 as.add(a); 555 assertEquals(as.size(), 3); 556 557 // iterate over the annotations 558 Iterator iter = as.iterator(); 559 while(iter.hasNext()) { 560 a = (Annotation) iter.next(); 561 if(a.getId().intValue() != 2) 562 assertEquals(a.getType(), "Token"); 563 assertEquals(a.getFeatures().size(), 1); 564 } 565 566 // add some more 567 newId = 568 as.add(new Long(0), new Long(12), "Syntax", fm3); 569 assertEquals(newId.intValue(), 3); 570 newId = 571 as.add(new Long(14), new Long(22), "Syntax", fm1); 572 assertEquals(newId.intValue(), 4); 573 assertEquals(as.size(), 5); 574 newId = 575 as.add(new Long(15), new Long(22), "Syntax", new SimpleFeatureMapImpl()); 576 577 //get by feature names 578 HashSet hs = new HashSet(); 579 hs.add("test"); 580 AnnotationSet fnSet = as.get("Token", hs); 581 assertEquals(fnSet.size(), 2); 582 //now try without a concrete type, just features 583 //we'll get some Syntax ones now too 584 fnSet = as.get(null, hs); 585 assertEquals(fnSet.size(), 4); 586 587 588 // indexing by type 589 ((AnnotationSetImpl) as).indexByType(); 590 AnnotationSet tokenAnnots = as.get("Token"); 591 assertEquals(tokenAnnots.size(), 2); 592 593 // indexing by position 594 AnnotationSet annotsAfter10 = as.get(new Long(15)); 595 if(annotsAfter10 == null) 596 fail("no annots found after offset 10"); 597 assertEquals(annotsAfter10.size(), 2); 598 599 } // testAnnotationSet 600 /** Test suite routine for the test runner */ 601 public static Test suite() { 602 return new TestSuite(TestAnnotation.class); 603 } // suite 604 605 /** Test get with offset and no annotation starting at given offset */ 606 public void _testGap() throws InvalidOffsetException { 607 AnnotationSet as = basicAS; 608 as.clear(); 609 FeatureMap fm = Factory.newFeatureMap(); 610 fm.put("A", "B"); 611 as.add(new Long(0), new Long(10), "foo", fm); 612 as.add(new Long(11), new Long(20), "foo", fm); 613 as.add(new Long(10), new Long(11), "space", fm); 614 615 //do the input selection (ignore spaces) 616 Set input = new HashSet(); 617 input.add("foo"); 618 input.add("foofoo"); 619 AnnotationSet annotations = null; 620 621 if(input.isEmpty()) annotations = as; 622 else{ 623 Iterator typesIter = input.iterator(); 624 AnnotationSet ofOneType = null; 625 626 while(typesIter.hasNext()){ 627 ofOneType = as.get((String)typesIter.next()); 628 629 if(ofOneType != null){ 630 //System.out.println("Adding " + ofOneType.getAllTypes()); 631 if(annotations == null) annotations = ofOneType; 632 else annotations.addAll(ofOneType); 633 } 634 } 635 } 636 /* if(annotations == null) annotations = new AnnotationSetImpl(doc); */ 637 if (DEBUG) 638 Out.println( 639 "Actual input:" + annotations.getAllTypes() + "\n" + annotations 640 ); 641 642 AnnotationSet res = 643 annotations.get("foo", Factory.newFeatureMap(), new Long(10)); 644 645 if (DEBUG) 646 Out.println(res); 647 assertTrue(!res.isEmpty()); 648 } 649 650 /** Test Overlaps */ 651 public void testOverlapsAndCoextensive() throws InvalidOffsetException { 652 Node node1 = new NodeImpl(new Integer(1),new Long(10)); 653 Node node2 = new NodeImpl(new Integer(2),new Long(20)); 654 Node node3 = new NodeImpl(new Integer(3),new Long(15)); 655 Node node4 = new NodeImpl(new Integer(4),new Long(15)); 656 Node node5 = new NodeImpl(new Integer(5),new Long(20)); 657 Node node6 = new NodeImpl(new Integer(6),new Long(30)); 658 659 FeatureMap fm1 = new SimpleFeatureMapImpl(); 660 fm1.put("color","red"); 661 fm1.put("Age",new Long(25)); 662 fm1.put(new Long(23), "Cristian"); 663 664 FeatureMap fm2 = new SimpleFeatureMapImpl(); 665 fm2.put("color","red"); 666 fm2.put("Age",new Long(25)); 667 fm2.put(new Long(23), "Cristian"); 668 669 FeatureMap fm4 = new SimpleFeatureMapImpl(); 670 fm4.put("color","red"); 671 fm4.put("Age",new Long(26)); 672 fm4.put(new Long(23), "Cristian"); 673 674 FeatureMap fm3 = new SimpleFeatureMapImpl(); 675 fm3.put("color","red"); 676 fm3.put("Age",new Long(25)); 677 fm3.put(new Long(23), "Cristian"); 678 fm3.put("best",new Boolean(true)); 679 680 // Start=10, End = 20 681 Annotation annot1 = new AnnotationImpl(new Integer(1), 682 node1, 683 node2, 684 "pos", 685 null); 686 // Start=20, End = 30 687 Annotation annot2 = new AnnotationImpl (new Integer(2), 688 node2, 689 node6, 690 "pos", 691 null); 692 // Start=20, End = 30 693 Annotation annot3 = new AnnotationImpl (new Integer(3), 694 node5, 695 node6, 696 "pos", 697 null); 698 // Start=20, End = 20 699 Annotation annot4 = new AnnotationImpl (new Integer(4), 700 node2, 701 node5, 702 "pos", 703 null); 704 // Start=10, End = 30 705 Annotation annot5 = new AnnotationImpl (new Integer(5), 706 node1, 707 node6, 708 "pos", 709 null); 710 // Start=10, End = 15 711 Annotation annot6 = new AnnotationImpl (new Integer(6), 712 node1, 713 node4, 714 "pos", 715 null); 716 // Start=null, End = null 717 Annotation annot7 = new AnnotationImpl (new Integer(7), 718 null, 719 null, 720 "pos", 721 null); 722 723 // MAP 724 // annot1 -> Start=10, End = 20 725 // annot2 -> Start=20, End = 30 726 // annot3 -> Start=20, End = 30 727 // annot4 -> Start=20, End = 20 728 // annot5 -> Start=10, End = 30 729 // annot6 -> Start=10, End = 15 730 731 // Not overlaping situations 732 assertTrue("Those annotations does not overlap!",!annot1.overlaps(annot3)); 733 assertTrue("Those annotations does not overlap!",!annot1.overlaps(annot2)); 734 assertTrue("Those annotations does not overlap!",!annot2.overlaps(annot1)); 735 assertTrue("Those annotations does not overlap!",!annot3.overlaps(annot1)); 736 assertTrue("Those annotations does not overlap!",!annot4.overlaps(annot6)); 737 assertTrue("Those annotations does not overlap!",!annot6.overlaps(annot4)); 738 739 assertTrue("Those annotations does not overlap!",!annot6.overlaps(null)); 740 assertTrue("Those annotations does not overlap!",!annot1.overlaps(annot7)); 741 742 // Overlaping situations 743 assertTrue("Those annotations does overlap!",annot4.overlaps(annot5)); 744 assertTrue("Those annotations does overlap!",annot5.overlaps(annot4)); 745 assertTrue("Those annotations does overlap!",annot1.overlaps(annot6)); 746 assertTrue("Those annotations does overlap!",annot6.overlaps(annot1)); 747 assertTrue("Those annotations does overlap!",annot2.overlaps(annot5)); 748 assertTrue("Those annotations does overlap!",annot5.overlaps(annot2)); 749 750 // Not coextensive situations 751 assertTrue("Those annotations are not coextensive!",!annot1.coextensive(annot2)); 752 assertTrue("Those annotations are not coextensive!",!annot2.coextensive(annot1)); 753 assertTrue("Those annotations are not coextensive!",!annot4.coextensive(annot3)); 754 assertTrue("Those annotations are not coextensive!",!annot3.coextensive(annot4)); 755 assertTrue("Those annotations are not coextensive!",!annot4.coextensive(annot7)); 756 assertTrue("Those annotations are not coextensive!",!annot5.coextensive(annot6)); 757 assertTrue("Those annotations are not coextensive!",!annot6.coextensive(annot5)); 758 //Coextensive situations 759 assertTrue("Those annotations are coextensive!",annot2.coextensive(annot2)); 760 assertTrue("Those annotations are coextensive!",annot2.coextensive(annot3)); 761 assertTrue("Those annotations are coextensive!",annot3.coextensive(annot2)); 762 763 }//testOverlapsAndCoextensive 764 765 /** Test Coextensive */ 766 public void testIsPartiallyCompatibleAndCompatible() 767 throws InvalidOffsetException { 768 Node node1 = new NodeImpl(new Integer(1),new Long(10)); 769 Node node2 = new NodeImpl(new Integer(2),new Long(20)); 770 Node node3 = new NodeImpl(new Integer(3),new Long(15)); 771 Node node4 = new NodeImpl(new Integer(4),new Long(15)); 772 Node node5 = new NodeImpl(new Integer(5),new Long(20)); 773 Node node6 = new NodeImpl(new Integer(6),new Long(30)); 774 775 FeatureMap fm1 = new SimpleFeatureMapImpl(); 776 fm1.put("color","red"); 777 fm1.put("Age",new Long(25)); 778 fm1.put(new Long(23), "Cristian"); 779 780 FeatureMap fm2 = new SimpleFeatureMapImpl(); 781 fm2.put("color","red"); 782 fm2.put("Age",new Long(25)); 783 fm2.put(new Long(23), "Cristian"); 784 785 FeatureMap fm4 = new SimpleFeatureMapImpl(); 786 fm4.put("color","red"); 787 fm4.put("Age",new Long(26)); 788 fm4.put(new Long(23), "Cristian"); 789 790 FeatureMap fm3 = new SimpleFeatureMapImpl(); 791 fm3.put("color","red"); 792 fm3.put("Age",new Long(25)); 793 fm3.put(new Long(23), "Cristian"); 794 fm3.put("best",new Boolean(true)); 795 796 // Start=10, End = 20 797 Annotation annot1 = new AnnotationImpl(new Integer(1), 798 node1, 799 node2, 800 "pos", 801 fm1); 802 // Start=20, End = 30 803 Annotation annot2 = new AnnotationImpl (new Integer(2), 804 node2, 805 node6, 806 "pos", 807 fm2); 808 // Start=20, End = 30 809 Annotation annot3 = new AnnotationImpl (new Integer(3), 810 node5, 811 node6, 812 "pos", 813 fm3); 814 // Start=20, End = 20 815 Annotation annot4 = new AnnotationImpl (new Integer(4), 816 node2, 817 node5, 818 "pos", 819 fm4); 820 // Start=10, End = 30 821 Annotation annot5 = new AnnotationImpl (new Integer(5), 822 node1, 823 node6, 824 "pos", 825 fm3); 826 // Start=10, End = 15 827 Annotation annot6 = new AnnotationImpl (new Integer(6), 828 node1, 829 node4, 830 "pos", 831 fm1); 832 // Start=null, End = null 833 Annotation annot7 = new AnnotationImpl (new Integer(7), 834 null, 835 null, 836 "pos", 837 null); 838 839 // MAP 840 /* 841 annot1 -> Start=10, End = 20,{color="red",Age="25",23="Cristian"} 842 annot2 -> Start=20, End = 30,{color="red",Age="25",23="Cristian"} 843 annot3 -> Start=20, End = 30,{color="red",Age="25",23="Cristian",best="true"} 844 annot4 -> Start=20, End = 20,{color="red",Age="26",23="Cristian"} 845 annot5 -> Start=10, End = 30,{color="red",Age="25",23="Cristian",best="true"} 846 annot6 -> Start=10, End = 15,{color="red",Age="25",23="Cristian"} 847 */ 848 // Not compatible situations 849 assertTrue("Those annotations are not compatible!",!annot3.isCompatible(annot2)); 850 851 // Not partially compatible situations 852 // They don't overlap 853 assertTrue("Those annotations("+ annot1 +" & " + 854 annot2+ ") are not partially compatible!", 855 !annot1.isPartiallyCompatible(annot2)); 856 857 // Again they don't overlap 858 assertTrue("Those annotations("+ annot1 +" & " + 859 annot3+ ") are not partially compatible!", 860 !annot1.isPartiallyCompatible(annot3)); 861 // Fails because of the age value 862 assertTrue("Those annotations("+ annot1 +" & " + 863 annot4+ ") are not partially compatible!", 864 !annot1.isPartiallyCompatible(annot4)); 865 // Fails because of the value of Age 866 assertTrue("Those annotations("+ annot4 +" & " + 867 annot5+ ") are not partially compatible!", 868 !annot4.isPartiallyCompatible(annot5)); 869 // Features from annot6 does not subsumes features annot3 870 assertTrue("Those annotations("+ annot3 +" & " + 871 annot6+ ") are not partially compatible!", 872 !annot3.isPartiallyCompatible(annot6,null)); 873 // Features from annot2 does not subsumes features annot5 874 assertTrue("Those annotations("+ annot5 +" & " + 875 annot2+ ") are not partially compatible!", 876 !annot5.isPartiallyCompatible(annot2,null)); 877 Set keySet = new HashSet(); 878 // They don't overlap 879 assertTrue("Those annotations("+ annot2 +" & " + 880 annot4+ ") are not partially compatible!", 881 !annot2.isPartiallyCompatible(annot4,keySet)); 882 keySet.add("color"); 883 keySet.add("Age"); 884 keySet.add("best"); 885 // Fails because of best feture 886 assertTrue("Those annotations("+ annot5 +" & " + 887 annot2+ ") are not partially compatible!", 888 !annot5.isPartiallyCompatible(annot2,keySet)); 889 // Fails because start=end in both cases and they don't overlap 890 assertTrue("Those annotations("+ annot4 +" & " + 891 annot4+ ") are not partially compatible!", 892 !annot4.isPartiallyCompatible(annot4)); 893 894 /* 895 annot1 -> Start=10, End = 20,{color="red",Age="25",23="Cristian"} 896 annot2 -> Start=20, End = 30,{color="red",Age="25",23="Cristian"} 897 annot3 -> Start=20, End = 30,{color="red",Age="25",23="Cristian",best="true"} 898 annot4 -> Start=20, End = 20,{color="red",Age="26",23="Cristian"} 899 annot5 -> Start=10, End = 30,{color="red",Age="25",23="Cristian",best="true"} 900 annot6 -> Start=10, End = 15,{color="red",Age="25",23="Cristian"} 901 */ 902 903 // Compatible situations 904 assertTrue("Those annotations("+ annot2 +" & " + 905 annot3+ ") should be compatible!", 906 annot2.isCompatible(annot3)); 907 assertTrue("Those annotations("+ annot2 +" & " + 908 annot3+ ") should be compatible!", 909 annot2.isCompatible(annot3,null)); 910 assertTrue("Those annotations("+ annot2 +" & " + 911 annot3+ ") should be compatible!", 912 annot2.isCompatible(annot3,new HashSet())); 913 assertTrue("Those annotations("+ annot4 +" & " + 914 annot4+ ") should be compatible!", 915 annot4.isCompatible(annot4)); 916 keySet = new HashSet(); 917 keySet.add("color"); 918 keySet.add(new Long(23)); 919 assertTrue("Those annotations("+ annot3 +" & " + 920 annot2+ ") should be compatible!", 921 annot3.isCompatible(annot2,keySet)); 922 923 // Partially compatible situations 924 assertTrue("Those annotations("+ annot2 +" & " + 925 annot3+ ") should be partially compatible!", 926 annot2.isPartiallyCompatible(annot3)); 927 assertTrue("Those annotations("+ annot2 +" & " + 928 annot2+ ") should be partially compatible!", 929 annot2.isPartiallyCompatible(annot2)); 930 assertTrue("Those annotations are partially compatible!", 931 annot1.isPartiallyCompatible(annot5)); 932 assertTrue("Those annotations are partially compatible!", 933 annot1.isPartiallyCompatible(annot6)); 934 assertTrue("Those annotations are partially compatible!", 935 annot3.isPartiallyCompatible(annot5)); 936 assertTrue("Those annotations are partially compatible!", 937 annot5.isPartiallyCompatible(annot3)); 938 assertTrue("Those annotations are partially compatible!", 939 annot6.isPartiallyCompatible(annot5)); 940 941 }// testIsPartiallyCompatibleAndCompatible 942 943 944 public void testFeatureSubsumeMethods(){ 945 946 FeatureMap fm1 = Factory.newFeatureMap(); 947 fm1.put("k1","v1"); 948 fm1.put("k2","v2"); 949 950 FeatureMap fm2 = Factory.newFeatureMap(); 951 fm2.put("k1","v1"); 952 953 Set featKeysSet1 = new HashSet(); 954 featKeysSet1.add("k1"); 955 featKeysSet1.add("k2"); 956 featKeysSet1.add("k3"); 957 featKeysSet1.add("k4"); 958 959 assertTrue(fm1 + " should subsume " + fm2 + " using the key set" + 960 featKeysSet1,fm1.subsumes(fm2, featKeysSet1)); 961 assertTrue(fm1 + " should subsume " + fm2 + 962 " taking all feat into consideration", 963 fm1.subsumes(fm2, null)); 964 965 FeatureMap fm3 = Factory.newFeatureMap(); 966 fm3.put("k1","v1"); 967 fm3.put("k2","v2"); 968 fm3.put("k3",new Integer(3)); 969 970 Set featKeysSet2 = new HashSet(); 971 featKeysSet2.add("k1"); 972 973 assertTrue(fm1 + " should subsume " + fm3 + " using the key set" + 974 featKeysSet2,fm1.subsumes(fm3, featKeysSet2)); 975 assertTrue(fm1 + " should NOT subsume " + fm3 + 976 " taking all feats into consideration", 977 !fm1.subsumes(fm3,null)); 978 979 FeatureMap fm4 = Factory.newFeatureMap(); 980 fm4.put("k1",new Integer(2)); 981 fm4.put("k2","v2"); 982 fm4.put("k3","v3"); 983 984 Set featKeysSet3 = new HashSet(); 985 featKeysSet3.add("k2"); 986 987 assertTrue(fm3 + " should subsume " + fm4 + " using the key set" + 988 featKeysSet3, fm4.subsumes(fm3,featKeysSet3)); 989 assertTrue(fm4 + " should NOT subsume " + fm3 + 990 " taking all feats into consideration", 991 !fm4.subsumes(fm3,null)); 992 993 994 }// testFeatureSubsumeMethods(); 995 996 public static void main(String[] args){ 997 998 try{ 999 Gate.init(); 1000 TestAnnotation testAnnot = new TestAnnotation(""); 1001 testAnnot.setUp(); 1002 testAnnot.testIterator(); 1003 testAnnot._testGap(); 1004 testAnnot.tearDown(); 1005 testAnnot.testOverlapsAndCoextensive(); 1006 testAnnot.testIsPartiallyCompatibleAndCompatible(); 1007 }catch(Throwable t){ 1008 t.printStackTrace(); 1009 } 1010 } 1011} // class TestAnnotation 1012 1013
|
TestAnnotation |
|