1
15
16 package gate.annotation;
17
18 import java.util.*;
19
20 import junit.framework.*;
21
22 import gate.*;
23 import gate.corpora.TestDocument;
24 import gate.util.*;
25
26
28 public class TestAnnotation extends TestCase
29 {
30
31 private static final boolean DEBUG = false;
32
33
34 public TestAnnotation(String name) { super(name); }
35
36
37 protected Document doc1;
38
39
40 protected AnnotationSet basicAS;
41
42
43 protected FeatureMap emptyFeatureMap;
44
45
46 public void setUp() throws Exception
47 {
48 String server = TestDocument.getTestServerName();
49 assertNotNull(server);
50 FeatureMap params = Factory.newFeatureMap();
51 params.put(Document.DOCUMENT_URL_PARAMETER_NAME, Gate.getUrl("tests/doc0.html"));
52 params.put(Document.DOCUMENT_MARKUP_AWARE_PARAMETER_NAME, "false");
53 doc1 = (Document)Factory.createResource("gate.corpora.DocumentImpl",
54 params);
55
56 emptyFeatureMap = new SimpleFeatureMapImpl();
57
58 basicAS = new AnnotationSetImpl(doc1);
59 FeatureMap fm = new SimpleFeatureMapImpl();
60
61 basicAS.get("T"); basicAS.get(new Long(0));
64 basicAS.add(new Long(10), new Long(20), "T1", fm); basicAS.add(new Long(10), new Long(20), "T2", fm); basicAS.add(new Long(10), new Long(20), "T3", fm); basicAS.add(new Long(10), new Long(20), "T1", fm);
69 fm = new SimpleFeatureMapImpl();
70 fm.put("pos", "NN");
71 fm.put("author", "hamish");
72 fm.put("version", new Integer(1));
73
74 basicAS.add(new Long(10), new Long(20), "T1", fm); basicAS.add(new Long(15), new Long(40), "T1", fm); basicAS.add(new Long(15), new Long(40), "T3", fm); basicAS.add(new Long(15), new Long(40), "T1", fm);
79 fm = new SimpleFeatureMapImpl();
80 fm.put("pos", "JJ");
81 fm.put("author", "the devil himself");
82 fm.put("version", new Long(44));
83 fm.put("created", "monday");
84
85 basicAS.add(new Long(15), new Long(40), "T3", fm); basicAS.add(new Long(15), new Long(40), "T1", fm); basicAS.add(new Long(15), new Long(40), "T1", fm);
89 }
92
93
94 public void testOffsetIndex() throws InvalidOffsetException {
95 AnnotationSet as = new AnnotationSetImpl(doc1);
96 AnnotationSet asBuf;
97 Integer newId;
98 FeatureMap fm = new SimpleFeatureMapImpl();
99 Annotation a;
100 Node startNode;
101 Node endNode;
102
103 newId = as.add(new Long(10), new Long(20), "T", fm);
104 assertEquals(newId.intValue(), 11);
105 a = as.get(newId);
106
107 startNode = a.getStartNode();
108 endNode = a.getEndNode();
109 assertEquals(startNode.getId().intValue(), 4);
110 assertEquals(endNode.getId().intValue(), 5);
111 assertEquals(startNode.getOffset().longValue(), 10);
112 assertEquals(endNode.getOffset().longValue(), 20);
113
114 newId = as.add(new Long(10), new Long(30), "T", fm);
115 assertEquals(newId.intValue(), 12);
116 a = as.get(newId);
117
118 startNode = a.getStartNode();
119 endNode = a.getEndNode();
120 assertEquals(startNode.getId().intValue(), 4);
121 assertEquals(endNode.getId().intValue(), 6);
122 assertEquals(startNode.getOffset().longValue(), 10);
123 assertEquals(endNode.getOffset().longValue(), 30);
124
125 asBuf = as.get(new Long(10));
126 assertEquals(asBuf.size(), 2);
127
128 }
130
131 public void testExceptions() {
132 AnnotationSet as = new AnnotationSetImpl(doc1);
133 boolean threwUp = false;
134
135 try {
136 as.add(new Long(-1), new Long(1), "T", emptyFeatureMap);
137 } catch (InvalidOffsetException e) {
138 threwUp = true;
139 }
140
141 if(! threwUp) fail("Should have thrown InvalidOffsetException");
142 threwUp = false;
143
144 try {
145 as.add(new Long(1), new Long(-1), "T", emptyFeatureMap);
146 } catch (InvalidOffsetException e) {
147 threwUp = true;
148 }
149
150 if(! threwUp) fail("Should have thrown InvalidOffsetException");
151 threwUp = false;
152
153 try {
154 as.add(new Long(1), new Long(0), "T", emptyFeatureMap);
155 } catch (InvalidOffsetException e) {
156 threwUp = true;
157 }
158
159 if(! threwUp) fail("Should have thrown InvalidOffsetException");
160 threwUp = false;
161
162 try {
163 as.add(null, new Long(1), "T", emptyFeatureMap);
164 } catch (InvalidOffsetException e) {
165 threwUp = true;
166 }
167
168 if(! threwUp) fail("Should have thrown InvalidOffsetException");
169 threwUp = false;
170
171 try {
172 as.add(new Long(1), null, "T", emptyFeatureMap);
173 } catch (InvalidOffsetException e) {
174 threwUp = true;
175 }
176
177 if(! threwUp) fail("Should have thrown InvalidOffsetException");
178 threwUp = false;
179
180 try {
181 as.add(new Long(999999), new Long(100000000), "T", emptyFeatureMap);
182 } catch (InvalidOffsetException e) {
183 threwUp = true;
184 }
185
186
190 threwUp = false;
191
192 }
194
195 public void testTypeIndex() throws Exception {
196 FeatureMap params = Factory.newFeatureMap();
197 params.put(Document.DOCUMENT_URL_PARAMETER_NAME, Gate.getUrl("tests/doc0.html"));
198 params.put(Document.DOCUMENT_MARKUP_AWARE_PARAMETER_NAME, "false");
199 Document doc = (Document)Factory.createResource("gate.corpora.DocumentImpl",
200 params);
201 AnnotationSet as = new AnnotationSetImpl(doc);
202 AnnotationSet asBuf;
203 Integer newId;
204 FeatureMap fm = new SimpleFeatureMapImpl();
205 Annotation a;
206 Node startNode;
207 Node endNode;
208
209 as.get("T");
211 as.add(new Long(10), new Long(20), "T1", fm); as.add(new Long(10), new Long(20), "T2", fm); as.add(new Long(10), new Long(20), "T3", fm); as.add(new Long(10), new Long(20), "T1", fm); as.add(new Long(10), new Long(20), "T1", fm); as.add(new Long(10), new Long(20), "T1", fm); as.add(new Long(10), new Long(20), "T3", fm); as.add(new Long(10), new Long(20), "T1", fm); as.add(new Long(10), new Long(20), "T3", fm); as.add(new Long(10), new Long(20), "T1", fm); as.add(new Long(10), new Long(20), "T1", fm);
223 asBuf = as.get("T");
224 assertEquals(null, asBuf);
225
226 asBuf = as.get("T1");
227 assertEquals(7, asBuf.size());
228 asBuf = as.get("T2");
229 assertEquals(1, asBuf.size());
230 asBuf = as.get("T3");
231 assertEquals(3, asBuf.size());
232
233 SortedSet sortedAnnots = new TreeSet(as);
236
237 int idCounter = 0;
239 Iterator iter = sortedAnnots.iterator();
240 while(iter.hasNext()) {
241 a = (Annotation) iter.next();
242
243 assertEquals(idCounter++, a.getId().intValue());
245
246 startNode = a.getStartNode();
247 endNode = a.getEndNode();
248
249 assertEquals(0, startNode.getId().intValue());
251
252 assertEquals(10, startNode.getOffset().longValue());
254
255 assertEquals(1, endNode.getId().intValue());
257
258 assertEquals(20, endNode.getOffset().longValue());
260 }
261
262 }
264
265 public void testAddWithNodes() throws Exception {
266 FeatureMap params = Factory.newFeatureMap();
267 params.put(Document.DOCUMENT_URL_PARAMETER_NAME, Gate.getUrl("tests/doc0.html"));
268 params.put(Document.DOCUMENT_MARKUP_AWARE_PARAMETER_NAME, "false");
269 Document doc = (Document)Factory.createResource("gate.corpora.DocumentImpl",
270 params);
271 AnnotationSet as = new AnnotationSetImpl(doc);
272 AnnotationSet asBuf;
273 Integer newId;
274 FeatureMap fm = new SimpleFeatureMapImpl();
275 Annotation a;
276 Node startNode;
277 Node endNode;
278
279 as.get("T");
281 newId = as.add(new Long(10), new Long(20), "T1", fm); a = as.get(newId);
283 startNode = a.getStartNode();
284 endNode = a.getEndNode();
285
286 as.add(startNode, endNode, "T2", fm); as.add(startNode, endNode, "T3", fm); as.add(startNode, endNode, "T1", fm); as.add(startNode, endNode, "T1", fm); as.add(startNode, endNode, "T1", fm); as.add(startNode, endNode, "T3", fm); as.add(startNode, endNode, "T1", fm); as.add(startNode, endNode, "T3", fm); as.add(startNode, endNode, "T1", fm); as.add(startNode, endNode, "T1", fm);
297 asBuf = as.get("T");
298 assertEquals(null, asBuf);
299
300 asBuf = as.get("T1");
301 assertEquals(7, asBuf.size());
302 asBuf = as.get("T2");
303 assertEquals(1, asBuf.size());
304 asBuf = as.get("T3");
305 assertEquals(3, asBuf.size());
306
307 SortedSet sortedAnnots = new TreeSet(as);
310
311 int idCounter = 0;
313 Iterator iter = sortedAnnots.iterator();
314 while(iter.hasNext()) {
315 a = (Annotation) iter.next();
316 assertEquals(idCounter++, a.getId().intValue());
318
319 startNode = a.getStartNode();
320 endNode = a.getEndNode();
321
322 assertEquals(0, startNode.getId().intValue());
324
325 assertEquals(10, startNode.getOffset().longValue());
327
328 assertEquals(1, endNode.getId().intValue());
330
331 assertEquals(20, endNode.getOffset().longValue());
333 }
334
335 }
337
338 public void testComplexGet() throws InvalidOffsetException {
339 AnnotationSet as = basicAS;
340 AnnotationSet asBuf;
341 Integer newId;
342 FeatureMap fm = new SimpleFeatureMapImpl();
343 Annotation a;
344 Node startNode;
345 Node endNode;
346
347 FeatureMap constraints = new SimpleFeatureMapImpl();
348 constraints.put("pos", "NN");
349
350
353 asBuf = basicAS.get("T1", constraints);
354 assertEquals(3, asBuf.size());
355 asBuf = basicAS.get("T3", constraints);
356 assertEquals(1, asBuf.size());
357 asBuf = basicAS.get("T1", constraints, new Long(12));
358 assertEquals(2, asBuf.size());
359 asBuf = basicAS.get("T1", constraints, new Long(10));
360 assertEquals(1, asBuf.size());
361 asBuf = basicAS.get("T1", constraints, new Long(11));
362 assertEquals(2, asBuf.size());
363 asBuf = basicAS.get("T1", constraints, new Long(9));
364 assertEquals(1, asBuf.size());
365
366 constraints.put("pos", "JJ");
367 asBuf = basicAS.get("T1", constraints, new Long(0));
369 assertEquals(null, asBuf);
370 asBuf = basicAS.get("T1", constraints, new Long(14));
371 assertEquals(2, asBuf.size());
372
373 constraints.put("author", "valentin");
374 asBuf = basicAS.get("T1", constraints, new Long(14));
375 assertEquals(null, asBuf);
376
377 constraints.put("author", "the devil himself");
378 asBuf = basicAS.get("T1", constraints, new Long(14));
379 assertEquals(2, asBuf.size());
380
381 asBuf = basicAS.get("T1", constraints, new Long(5));
382 assertEquals(null, asBuf);
383
384 constraints.put("this feature isn't", "there at all");
385 asBuf = basicAS.get("T1", constraints, new Long(14));
386 assertEquals(null, asBuf);
387
388 }
390
391 public void testRemove() {
392 AnnotationSet asBuf = basicAS.get("T1");
393 assertEquals(7, asBuf.size());
394 asBuf = basicAS.get(new Long(9));
395 assertEquals(5, asBuf.size());
396
397 basicAS.remove(basicAS.get(new Integer(0)));
398
399 assertEquals(10, basicAS.size());
400 assertEquals(10, ((AnnotationSetImpl) basicAS).annotsById.size());
401
402 asBuf = basicAS.get("T1");
403 assertEquals(6, asBuf.size());
404
405 asBuf = basicAS.get(new Long(9));
406 assertEquals(4, asBuf.size());
407 assertEquals(null, basicAS.get(new Integer(0)));
408 basicAS.remove(basicAS.get(new Integer(8)));
409 assertEquals(9, basicAS.size());
410 basicAS.removeAll(basicAS);
411 assertEquals(null, basicAS.get());
412 assertEquals(null, basicAS.get("T1"));
413 assertEquals(null, basicAS.get(new Integer(0)));
414 }
416 public void testRemoveInexistant() throws Exception{
417 basicAS.add(new Long(0), new Long(10), "Foo", emptyFeatureMap);
418 Annotation ann = (Annotation)basicAS.get("Foo").iterator().next();
419 basicAS.remove(ann);
420 basicAS.remove(ann);
422 }
423
424
425 public void testIteratorRemove() {
426 AnnotationSet asBuf = basicAS.get("T1");
427 assertEquals(7, asBuf.size());
428 asBuf = basicAS.get(new Long(9));
429 assertEquals(5, asBuf.size());
430
431 Iterator iter = basicAS.iterator();
434 while(iter.hasNext())
435 iter.next();
436 iter.remove();
437
438 assertEquals(10, basicAS.size());
439 assertEquals(10, ((AnnotationSetImpl) basicAS).annotsById.size());
440 asBuf = basicAS.get("T1");
441 assertEquals(6, asBuf.size());
442 asBuf = basicAS.get(new Long(9));
443 assertEquals(4, asBuf.size());
444 assertEquals(null, basicAS.get(new Integer(0)));
445 basicAS.remove(basicAS.get(new Integer(8)));
446
447 }
449
450 public void testIterator() {
451 Iterator iter = basicAS.iterator();
452 Annotation[] annots = new Annotation[basicAS.size()];
453 int i = 0;
454
455 while(iter.hasNext()) {
456 Annotation a = (Annotation) iter.next();
457 annots[i++] = a;
458
459 assertTrue(basicAS.contains(a));
460 iter.remove();
461 assertTrue(!basicAS.contains(a));
462 }
464 i = 0;
465 while(i < annots.length) {
466 basicAS.add(annots[i++]);
467 assertEquals(i, basicAS.size());
468 }
470 AnnotationSet asBuf = basicAS.get("T1");
471 assertEquals(7, asBuf.size());
472 asBuf = basicAS.get(new Long(9));
473 assertEquals(5, asBuf.size());
474 }
476
477 public void testSetMethods() {
478 Annotation a = basicAS.get(new Integer(6));
479 assertTrue(basicAS.contains(a));
480
481 Annotation[] annotArray =
482 (Annotation[]) basicAS.toArray(new Annotation[0]);
483 Object[] annotObjectArray = basicAS.toArray();
484 assertEquals(11, annotArray.length);
485 assertEquals(11, annotObjectArray.length);
486
487 SortedSet sortedAnnots = new TreeSet(basicAS);
488 annotArray = (Annotation[]) sortedAnnots.toArray(new Annotation[0]);
489 for(int i = 0; i<11; i++)
490 assertTrue( annotArray[i].getId().equals(new Integer(i)) );
491
492 Annotation a1 = basicAS.get(new Integer(3));
493 Annotation a2 = basicAS.get(new Integer(4));
494 Set a1a2 = new HashSet();
495 a1a2.add(a1);
496 a1a2.add(a2);
497 assertTrue(basicAS.contains(a1));
498 assertTrue(basicAS.containsAll(a1a2));
499 basicAS.removeAll(a1a2);
500
501 assertEquals(9, basicAS.size());
502 assertTrue(! basicAS.contains(a1));
503 assertTrue(! basicAS.containsAll(a1a2));
504
505
515 basicAS.clear();
516 assertTrue(basicAS.isEmpty());
517
518 }
520
521 public void testAnnotationSet() throws Exception {
522 FeatureMap params = Factory.newFeatureMap();
524 params.put(Document.DOCUMENT_URL_PARAMETER_NAME, Gate.getUrl("tests/doc0.html"));
525 params.put(Document.DOCUMENT_MARKUP_AWARE_PARAMETER_NAME, "false");
526 Document doc = (Document)Factory.createResource("gate.corpora.DocumentImpl",
527 params);
528
529 AnnotationSet as = new AnnotationSetImpl(doc);
530 assertEquals(as.size(), 0);
531
532 FeatureMap fm1 = Factory.newFeatureMap();
534 fm1.put("test", "my-value");
535 FeatureMap fm2 = Factory.newFeatureMap();
536 fm2.put("test", "my-value-different");
537 FeatureMap fm3 = Factory.newFeatureMap();
538 fm3.put("another test", "different my-value");
539
540 Integer newId;
541 newId =
542 as.add(new Long(0), new Long(10), "Token", fm1);
543 assertEquals(newId.intValue(), 0);
544 newId =
545 as.add(new Long(11), new Long(12), "Token", fm2);
546 assertEquals(newId.intValue(), 1);
547 assertEquals(as.size(), 2);
548 assertTrue(! as.isEmpty());
549 newId =
550 as.add(new Long(15), new Long(22), "Syntax", fm1);
551
552 Annotation a = as.get(new Integer(1));
554 as.remove(a);
555 assertEquals(as.size(), 2);
556 as.add(a);
557 assertEquals(as.size(), 3);
558
559 Iterator iter = as.iterator();
561 while(iter.hasNext()) {
562 a = (Annotation) iter.next();
563 if(a.getId().intValue() != 2)
564 assertEquals(a.getType(), "Token");
565 assertEquals(a.getFeatures().size(), 1);
566 }
567
568 newId =
570 as.add(new Long(0), new Long(12), "Syntax", fm3);
571 assertEquals(newId.intValue(), 3);
572 newId =
573 as.add(new Long(14), new Long(22), "Syntax", fm1);
574 assertEquals(newId.intValue(), 4);
575 assertEquals(as.size(), 5);
576 newId =
577 as.add(new Long(15), new Long(22), "Syntax", new SimpleFeatureMapImpl());
578
579 HashSet hs = new HashSet();
581 hs.add("test");
582 AnnotationSet fnSet = as.get("Token", hs);
583 assertEquals(fnSet.size(), 2);
584 fnSet = as.get(null, hs);
587 assertEquals(fnSet.size(), 4);
588
589
590 ((AnnotationSetImpl) as).indexByType();
592 AnnotationSet tokenAnnots = as.get("Token");
593 assertEquals(tokenAnnots.size(), 2);
594
595 AnnotationSet annotsAfter10 = as.get(new Long(15));
597 if(annotsAfter10 == null)
598 fail("no annots found after offset 10");
599 assertEquals(annotsAfter10.size(), 2);
600
601 }
603 public static Test suite() {
604 return new TestSuite(TestAnnotation.class);
605 }
607
608 public void _testGap() throws InvalidOffsetException {
609 AnnotationSet as = basicAS;
610 as.clear();
611 FeatureMap fm = Factory.newFeatureMap();
612 fm.put("A", "B");
613 as.add(new Long(0), new Long(10), "foo", fm);
614 as.add(new Long(11), new Long(20), "foo", fm);
615 as.add(new Long(10), new Long(11), "space", fm);
616
617 Set input = new HashSet();
619 input.add("foo");
620 input.add("foofoo");
621 AnnotationSet annotations = null;
622
623 if(input.isEmpty()) annotations = as;
624 else{
625 Iterator typesIter = input.iterator();
626 AnnotationSet ofOneType = null;
627
628 while(typesIter.hasNext()){
629 ofOneType = as.get((String)typesIter.next());
630
631 if(ofOneType != null){
632 if(annotations == null) annotations = ofOneType;
634 else annotations.addAll(ofOneType);
635 }
636 }
637 }
638
639 if (DEBUG)
640 Out.println(
641 "Actual input:" + annotations.getAllTypes() + "\n" + annotations
642 );
643
644 AnnotationSet res =
645 annotations.get("foo", Factory.newFeatureMap(), new Long(10));
646
647 if (DEBUG)
648 Out.println(res);
649 assertTrue(!res.isEmpty());
650 }
651
652
653 public void testOverlapsAndCoextensive() throws InvalidOffsetException {
654 Node node1 = new NodeImpl(new Integer(1),new Long(10));
655 Node node2 = new NodeImpl(new Integer(2),new Long(20));
656 Node node3 = new NodeImpl(new Integer(3),new Long(15));
657 Node node4 = new NodeImpl(new Integer(4),new Long(15));
658 Node node5 = new NodeImpl(new Integer(5),new Long(20));
659 Node node6 = new NodeImpl(new Integer(6),new Long(30));
660
661 FeatureMap fm1 = new SimpleFeatureMapImpl();
662 fm1.put("color","red");
663 fm1.put("Age",new Long(25));
664 fm1.put(new Long(23), "Cristian");
665
666 FeatureMap fm2 = new SimpleFeatureMapImpl();
667 fm2.put("color","red");
668 fm2.put("Age",new Long(25));
669 fm2.put(new Long(23), "Cristian");
670
671 FeatureMap fm4 = new SimpleFeatureMapImpl();
672 fm4.put("color","red");
673 fm4.put("Age",new Long(26));
674 fm4.put(new Long(23), "Cristian");
675
676 FeatureMap fm3 = new SimpleFeatureMapImpl();
677 fm3.put("color","red");
678 fm3.put("Age",new Long(25));
679 fm3.put(new Long(23), "Cristian");
680 fm3.put("best",new Boolean(true));
681
682 Annotation annot1 = new AnnotationImpl(new Integer(1),
684 node1,
685 node2,
686 "pos",
687 null);
688 Annotation annot2 = new AnnotationImpl (new Integer(2),
690 node2,
691 node6,
692 "pos",
693 null);
694 Annotation annot3 = new AnnotationImpl (new Integer(3),
696 node5,
697 node6,
698 "pos",
699 null);
700 Annotation annot4 = new AnnotationImpl (new Integer(4),
702 node2,
703 node5,
704 "pos",
705 null);
706 Annotation annot5 = new AnnotationImpl (new Integer(5),
708 node1,
709 node6,
710 "pos",
711 null);
712 Annotation annot6 = new AnnotationImpl (new Integer(6),
714 node1,
715 node4,
716 "pos",
717 null);
718 Annotation annot7 = new AnnotationImpl (new Integer(7),
720 null,
721 null,
722 "pos",
723 null);
724
725
733 assertTrue("Those annotations does not overlap!",!annot1.overlaps(annot3));
735 assertTrue("Those annotations does not overlap!",!annot1.overlaps(annot2));
736 assertTrue("Those annotations does not overlap!",!annot2.overlaps(annot1));
737 assertTrue("Those annotations does not overlap!",!annot3.overlaps(annot1));
738 assertTrue("Those annotations does not overlap!",!annot4.overlaps(annot6));
739 assertTrue("Those annotations does not overlap!",!annot6.overlaps(annot4));
740
741 assertTrue("Those annotations does not overlap!",!annot6.overlaps(null));
742 assertTrue("Those annotations does not overlap!",!annot1.overlaps(annot7));
743
744 assertTrue("Those annotations does overlap!",annot4.overlaps(annot5));
746 assertTrue("Those annotations does overlap!",annot5.overlaps(annot4));
747 assertTrue("Those annotations does overlap!",annot1.overlaps(annot6));
748 assertTrue("Those annotations does overlap!",annot6.overlaps(annot1));
749 assertTrue("Those annotations does overlap!",annot2.overlaps(annot5));
750 assertTrue("Those annotations does overlap!",annot5.overlaps(annot2));
751
752 assertTrue("Those annotations are not coextensive!",!annot1.coextensive(annot2));
754 assertTrue("Those annotations are not coextensive!",!annot2.coextensive(annot1));
755 assertTrue("Those annotations are not coextensive!",!annot4.coextensive(annot3));
756 assertTrue("Those annotations are not coextensive!",!annot3.coextensive(annot4));
757 assertTrue("Those annotations are not coextensive!",!annot4.coextensive(annot7));
758 assertTrue("Those annotations are not coextensive!",!annot5.coextensive(annot6));
759 assertTrue("Those annotations are not coextensive!",!annot6.coextensive(annot5));
760 assertTrue("Those annotations are coextensive!",annot2.coextensive(annot2));
762 assertTrue("Those annotations are coextensive!",annot2.coextensive(annot3));
763 assertTrue("Those annotations are coextensive!",annot3.coextensive(annot2));
764
765 }
767
768 public void testIsPartiallyCompatibleAndCompatible()
769 throws InvalidOffsetException {
770 Node node1 = new NodeImpl(new Integer(1),new Long(10));
771 Node node2 = new NodeImpl(new Integer(2),new Long(20));
772 Node node3 = new NodeImpl(new Integer(3),new Long(15));
773 Node node4 = new NodeImpl(new Integer(4),new Long(15));
774 Node node5 = new NodeImpl(new Integer(5),new Long(20));
775 Node node6 = new NodeImpl(new Integer(6),new Long(30));
776
777 FeatureMap fm1 = new SimpleFeatureMapImpl();
778 fm1.put("color","red");
779 fm1.put("Age",new Long(25));
780 fm1.put(new Long(23), "Cristian");
781
782 FeatureMap fm2 = new SimpleFeatureMapImpl();
783 fm2.put("color","red");
784 fm2.put("Age",new Long(25));
785 fm2.put(new Long(23), "Cristian");
786
787 FeatureMap fm4 = new SimpleFeatureMapImpl();
788 fm4.put("color","red");
789 fm4.put("Age",new Long(26));
790 fm4.put(new Long(23), "Cristian");
791
792 FeatureMap fm3 = new SimpleFeatureMapImpl();
793 fm3.put("color","red");
794 fm3.put("Age",new Long(25));
795 fm3.put(new Long(23), "Cristian");
796 fm3.put("best",new Boolean(true));
797
798 Annotation annot1 = new AnnotationImpl(new Integer(1),
800 node1,
801 node2,
802 "pos",
803 fm1);
804 Annotation annot2 = new AnnotationImpl (new Integer(2),
806 node2,
807 node6,
808 "pos",
809 fm2);
810 Annotation annot3 = new AnnotationImpl (new Integer(3),
812 node5,
813 node6,
814 "pos",
815 fm3);
816 Annotation annot4 = new AnnotationImpl (new Integer(4),
818 node2,
819 node5,
820 "pos",
821 fm4);
822 Annotation annot5 = new AnnotationImpl (new Integer(5),
824 node1,
825 node6,
826 "pos",
827 fm3);
828 Annotation annot6 = new AnnotationImpl (new Integer(6),
830 node1,
831 node4,
832 "pos",
833 fm1);
834 Annotation annot7 = new AnnotationImpl (new Integer(7),
836 null,
837 null,
838 "pos",
839 null);
840
841
850 assertTrue("Those annotations are not compatible!",!annot3.isCompatible(annot2));
852
853 assertTrue("Those annotations("+ annot1 +" & " +
856 annot2+ ") are not partially compatible!",
857 !annot1.isPartiallyCompatible(annot2));
858
859 assertTrue("Those annotations("+ annot1 +" & " +
861 annot3+ ") are not partially compatible!",
862 !annot1.isPartiallyCompatible(annot3));
863 assertTrue("Those annotations("+ annot1 +" & " +
865 annot4+ ") are not partially compatible!",
866 !annot1.isPartiallyCompatible(annot4));
867 assertTrue("Those annotations("+ annot4 +" & " +
869 annot5+ ") are not partially compatible!",
870 !annot4.isPartiallyCompatible(annot5));
871 assertTrue("Those annotations("+ annot3 +" & " +
873 annot6+ ") are not partially compatible!",
874 !annot3.isPartiallyCompatible(annot6,null));
875 assertTrue("Those annotations("+ annot5 +" & " +
877 annot2+ ") are not partially compatible!",
878 !annot5.isPartiallyCompatible(annot2,null));
879 Set keySet = new HashSet();
880 assertTrue("Those annotations("+ annot2 +" & " +
882 annot4+ ") are not partially compatible!",
883 !annot2.isPartiallyCompatible(annot4,keySet));
884 keySet.add("color");
885 keySet.add("Age");
886 keySet.add("best");
887 assertTrue("Those annotations("+ annot5 +" & " +
889 annot2+ ") are not partially compatible!",
890 !annot5.isPartiallyCompatible(annot2,keySet));
891 assertTrue("Those annotations("+ annot4 +" & " +
893 annot4+ ") are not partially compatible!",
894 !annot4.isPartiallyCompatible(annot4));
895
896
904
905 assertTrue("Those annotations("+ annot2 +" & " +
907 annot3+ ") should be compatible!",
908 annot2.isCompatible(annot3));
909 assertTrue("Those annotations("+ annot2 +" & " +
910 annot3+ ") should be compatible!",
911 annot2.isCompatible(annot3,null));
912 assertTrue("Those annotations("+ annot2 +" & " +
913 annot3+ ") should be compatible!",
914 annot2.isCompatible(annot3,new HashSet()));
915 assertTrue("Those annotations("+ annot4 +" & " +
916 annot4+ ") should be compatible!",
917 annot4.isCompatible(annot4));
918 keySet = new HashSet();
919 keySet.add("color");
920 keySet.add(new Long(23));
921 assertTrue("Those annotations("+ annot3 +" & " +
922 annot2+ ") should be compatible!",
923 annot3.isCompatible(annot2,keySet));
924
925 assertTrue("Those annotations("+ annot2 +" & " +
927 annot3+ ") should be partially compatible!",
928 annot2.isPartiallyCompatible(annot3));
929 assertTrue("Those annotations("+ annot2 +" & " +
930 annot2+ ") should be partially compatible!",
931 annot2.isPartiallyCompatible(annot2));
932 assertTrue("Those annotations are partially compatible!",
933 annot1.isPartiallyCompatible(annot5));
934 assertTrue("Those annotations are partially compatible!",
935 annot1.isPartiallyCompatible(annot6));
936 assertTrue("Those annotations are partially compatible!",
937 annot3.isPartiallyCompatible(annot5));
938 assertTrue("Those annotations are partially compatible!",
939 annot5.isPartiallyCompatible(annot3));
940 assertTrue("Those annotations are partially compatible!",
941 annot6.isPartiallyCompatible(annot5));
942
943 }
945
946 public void testFeatureSubsumeMethods(){
947
948 FeatureMap fm1 = Factory.newFeatureMap();
949 fm1.put("k1","v1");
950 fm1.put("k2","v2");
951
952 FeatureMap fm2 = Factory.newFeatureMap();
953 fm2.put("k1","v1");
954
955 Set featKeysSet1 = new HashSet();
956 featKeysSet1.add("k1");
957 featKeysSet1.add("k2");
958 featKeysSet1.add("k3");
959 featKeysSet1.add("k4");
960
961 assertTrue(fm1 + " should subsume " + fm2 + " using the key set" +
962 featKeysSet1,fm1.subsumes(fm2, featKeysSet1));
963 assertTrue(fm1 + " should subsume " + fm2 +
964 " taking all feat into consideration",
965 fm1.subsumes(fm2, null));
966
967 FeatureMap fm3 = Factory.newFeatureMap();
968 fm3.put("k1","v1");
969 fm3.put("k2","v2");
970 fm3.put("k3",new Integer(3));
971
972 Set featKeysSet2 = new HashSet();
973 featKeysSet2.add("k1");
974
975 assertTrue(fm1 + " should subsume " + fm3 + " using the key set" +
976 featKeysSet2,fm1.subsumes(fm3, featKeysSet2));
977 assertTrue(fm1 + " should NOT subsume " + fm3 +
978 " taking all feats into consideration",
979 !fm1.subsumes(fm3,null));
980
981 FeatureMap fm4 = Factory.newFeatureMap();
982 fm4.put("k1",new Integer(2));
983 fm4.put("k2","v2");
984 fm4.put("k3","v3");
985
986 Set featKeysSet3 = new HashSet();
987 featKeysSet3.add("k2");
988
989 assertTrue(fm3 + " should subsume " + fm4 + " using the key set" +
990 featKeysSet3, fm4.subsumes(fm3,featKeysSet3));
991 assertTrue(fm4 + " should NOT subsume " + fm3 +
992 " taking all feats into consideration",
993 !fm4.subsumes(fm3,null));
994
995
996 }
998 public static void main(String[] args){
999
1000 try{
1001 Gate.init();
1002 TestAnnotation testAnnot = new TestAnnotation("");
1003 testAnnot.setUp();
1004 testAnnot.testIterator();
1005 testAnnot._testGap();
1006 testAnnot.tearDown();
1007 testAnnot.testOverlapsAndCoextensive();
1008 testAnnot.testIsPartiallyCompatibleAndCompatible();
1009 }catch(Throwable t){
1010 t.printStackTrace();
1011 }
1012 }
1013}
1015