1   /*
2    *  TestAnnotation.java
3    *
4    *  Copyright (c) 1998-2004, 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.45 2004/07/21 17:10:02 akshay Exp $
14   */
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  /** Tests for the Annotation classes
27    */
28  public class TestAnnotation extends TestCase
29  {
30    /** Debug flag */
31    private static final boolean DEBUG = false;
32  
33    /** Construction */
34    public TestAnnotation(String name) { super(name); }
35  
36    /** A document */
37    protected Document doc1;
38  
39    /** An annotation set */
40    protected AnnotationSet basicAS;
41  
42    /** An empty feature map */
43    protected FeatureMap emptyFeatureMap;
44  
45    /** Fixture set up */
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");          // to trigger type indexing
62      basicAS.get(new Long(0));  // trigger offset index (though add will too)
63  
64      basicAS.add(new Long(10), new Long(20), "T1", fm);    // 0
65      basicAS.add(new Long(10), new Long(20), "T2", fm);    // 1
66      basicAS.add(new Long(10), new Long(20), "T3", fm);    // 2
67      basicAS.add(new Long(10), new Long(20), "T1", fm);    // 3
68  
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);    // 4
75      basicAS.add(new Long(15), new Long(40), "T1", fm);    // 5
76      basicAS.add(new Long(15), new Long(40), "T3", fm);    // 6
77      basicAS.add(new Long(15), new Long(40), "T1", fm);    // 7
78  
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);    // 8
86      basicAS.add(new Long(15), new Long(40), "T1", fm);    // 9
87      basicAS.add(new Long(15), new Long(40), "T1", fm);    // 10
88  
89      // Out.println(basicAS);
90    } // setUp
91  
92  
93    /** Test indexing by offset */
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   } // testOffsetIndex()
129 
130   /** Test exception throwing */
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     /*
187     // won't work until the doc size check is implemented
188     if(! threwUp) fail("Should have thrown InvalidOffsetException");
189     */
190     threwUp = false;
191 
192   } // testExceptions()
193 
194   /** Test type index */
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     // to trigger type indexing
210     as.get("T");
211     as.add(new Long(10), new Long(20), "T1", fm);    // 0
212     as.add(new Long(10), new Long(20), "T2", fm);    // 1
213     as.add(new Long(10), new Long(20), "T3", fm);    // 2
214     as.add(new Long(10), new Long(20), "T1", fm);    // 3
215     as.add(new Long(10), new Long(20), "T1", fm);    // 4
216     as.add(new Long(10), new Long(20), "T1", fm);    // 5
217     as.add(new Long(10), new Long(20), "T3", fm);    // 6
218     as.add(new Long(10), new Long(20), "T1", fm);    // 7
219     as.add(new Long(10), new Long(20), "T3", fm);    // 8
220     as.add(new Long(10), new Long(20), "T1", fm);    // 9
221     as.add(new Long(10), new Long(20), "T1", fm);    // 10
222 
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     // let's check that we've only got two nodes, what the ids are and so on;
234     // first construct a sorted set of annotations
235     SortedSet sortedAnnots = new TreeSet(as);
236 
237     // for checking the annotation id
238     int idCounter = 0;
239     Iterator iter = sortedAnnots.iterator();
240     while(iter.hasNext()) {
241       a = (Annotation) iter.next();
242 
243       // check annot ids
244       assertEquals(idCounter++, a.getId().intValue());
245 
246       startNode = a.getStartNode();
247       endNode = a.getEndNode();
248 
249       // start node id
250       assertEquals(0,  startNode.getId().intValue());
251 
252       // start offset
253       assertEquals(10, startNode.getOffset().longValue());
254 
255       // end id
256       assertEquals(1,  endNode.getId().intValue());
257 
258       // end offset
259       assertEquals(20, endNode.getOffset().longValue());
260     }
261 
262   } // testTypeIndex()
263 
264   /** Test the annotations set add method that uses existing nodes */
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     // to trigger type indexing
280     as.get("T");
281     newId = as.add(new Long(10), new Long(20), "T1", fm);    // 0
282     a = as.get(newId);
283     startNode = a.getStartNode();
284     endNode = a.getEndNode();
285 
286     as.add(startNode, endNode, "T2", fm);    // 1
287     as.add(startNode, endNode, "T3", fm);    // 2
288     as.add(startNode, endNode, "T1", fm);    // 3
289     as.add(startNode, endNode, "T1", fm);    // 4
290     as.add(startNode, endNode, "T1", fm);    // 5
291     as.add(startNode, endNode, "T3", fm);    // 6
292     as.add(startNode, endNode, "T1", fm);    // 7
293     as.add(startNode, endNode, "T3", fm);    // 8
294     as.add(startNode, endNode, "T1", fm);    // 9
295     as.add(startNode, endNode, "T1", fm);    // 10
296 
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     // let's check that we've only got two nodes, what the ids are and so on;
308     // first construct a sorted set of annotations
309     SortedSet sortedAnnots = new TreeSet(as);
310 
311     // for checking the annotation id
312     int idCounter = 0;
313     Iterator iter = sortedAnnots.iterator();
314     while(iter.hasNext()) {
315       a = (Annotation) iter.next();
316       // check annot ids
317       assertEquals(idCounter++, a.getId().intValue());
318 
319       startNode = a.getStartNode();
320       endNode = a.getEndNode();
321 
322       // start node id
323       assertEquals(0,  startNode.getId().intValue());
324 
325       // start offset
326       assertEquals(10, startNode.getOffset().longValue());
327 
328       // end id
329       assertEquals(1,  endNode.getId().intValue());
330 
331       // end offset
332       assertEquals(20, endNode.getOffset().longValue());
333     }
334 
335   } // testAddWithNodes()
336 
337   /** Test complex get (with type, offset and feature contraints) */
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     //Out.println(basicAS);
351     //Out.println(constraints);
352 
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     //Out.println(constraints);
368     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   } // testComplexGet()
389 
390   /** Test remove */
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   } // testRemove()
415 
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     //the second remove should do nothing...
421     basicAS.remove(ann);
422   }
423 
424   /** Test iterator remove */
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     // remove annotation with id 0; this is returned last by the
432     // iterator
433     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   } // testIteratorRemove()
448 
449   /** Test iterator */
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     } // while
463 
464     i = 0;
465     while(i < annots.length) {
466       basicAS.add(annots[i++]);
467       assertEquals(i, basicAS.size());
468     } // while
469 
470     AnnotationSet asBuf = basicAS.get("T1");
471     assertEquals(7, asBuf.size());
472     asBuf = basicAS.get(new Long(9));
473     assertEquals(5, asBuf.size());
474   } // testIterator
475 
476   /** Test Set methods */
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     //this will not work anymore as the semantics of addAll has changed (new
506     //annotations are created in order to avoid ID clashes
507 /*
508     basicAS.addAll(a1a2);
509     assertTrue(basicAS.contains(a2));
510     assertTrue(basicAS.containsAll(a1a2));
511 
512     assertTrue(basicAS.retainAll(a1a2));
513     assertTrue(basicAS.equals(a1a2));
514 */
515     basicAS.clear();
516     assertTrue(basicAS.isEmpty());
517 
518   } // testSetMethods()
519 
520   /** Test AnnotationSetImpl */
521   public void testAnnotationSet() throws Exception {
522     // constuct an empty AS
523     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     // add some annotations
533     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     // get by ID; remove; add(object)
553     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     // iterate over the annotations
560     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     // add some more
569     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     //get by feature names
580     HashSet hs = new HashSet();
581     hs.add("test");
582     AnnotationSet fnSet = as.get("Token", hs);
583     assertEquals(fnSet.size(), 2);
584     //now try without a concrete type, just features
585     //we'll get some Syntax ones now too
586     fnSet = as.get(null, hs);
587     assertEquals(fnSet.size(), 4);
588 
589 
590     // indexing by type
591     ((AnnotationSetImpl) as).indexByType();
592     AnnotationSet tokenAnnots = as.get("Token");
593     assertEquals(tokenAnnots.size(), 2);
594 
595     // indexing by position
596     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   } // testAnnotationSet
602   /** Test suite routine for the test runner */
603   public static Test suite() {
604     return new TestSuite(TestAnnotation.class);
605   } // suite
606 
607   /** Test get with offset and no annotation starting at given offset */
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     //do the input selection (ignore spaces)
618     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           //System.out.println("Adding " + ofOneType.getAllTypes());
633           if(annotations == null) annotations = ofOneType;
634           else annotations.addAll(ofOneType);
635         }
636       }
637     }
638     /* if(annotations == null) annotations = new AnnotationSetImpl(doc); */
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   /** Test Overlaps */
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     // Start=10, End = 20
683     Annotation annot1 = new AnnotationImpl(new Integer(1),
684                                            node1,
685                                            node2,
686                                            "pos",
687                                            null);
688     // Start=20, End = 30
689     Annotation annot2 = new AnnotationImpl (new Integer(2),
690                                             node2,
691                                             node6,
692                                             "pos",
693                                             null);
694     // Start=20, End = 30
695     Annotation annot3 = new AnnotationImpl (new Integer(3),
696                                             node5,
697                                             node6,
698                                             "pos",
699                                             null);
700     // Start=20, End = 20
701     Annotation annot4 = new AnnotationImpl (new Integer(4),
702                                             node2,
703                                             node5,
704                                             "pos",
705                                             null);
706     // Start=10, End = 30
707     Annotation annot5 = new AnnotationImpl (new Integer(5),
708                                             node1,
709                                             node6,
710                                             "pos",
711                                             null);
712     // Start=10, End = 15
713     Annotation annot6 = new AnnotationImpl (new Integer(6),
714                                             node1,
715                                             node4,
716                                             "pos",
717                                             null);
718     // Start=null, End = null
719     Annotation annot7 = new AnnotationImpl (new Integer(7),
720                                             null,
721                                             null,
722                                             "pos",
723                                             null);
724 
725     // MAP
726     // annot1 -> Start=10, End = 20
727     // annot2 -> Start=20, End = 30
728     // annot3 -> Start=20, End = 30
729     // annot4 -> Start=20, End = 20
730     // annot5 -> Start=10, End = 30
731     // annot6 -> Start=10, End = 15
732 
733     // Not overlaping situations
734    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    // Overlaping situations
745    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    // Not coextensive situations
753    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    //Coextensive situations
761    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   }//testOverlapsAndCoextensive
766 
767   /** Test Coextensive */
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     // Start=10, End = 20
799     Annotation annot1 = new AnnotationImpl(new Integer(1),
800                                            node1,
801                                            node2,
802                                            "pos",
803                                            fm1);
804     // Start=20, End = 30
805     Annotation annot2 = new AnnotationImpl (new Integer(2),
806                                             node2,
807                                             node6,
808                                             "pos",
809                                             fm2);
810     // Start=20, End = 30
811     Annotation annot3 = new AnnotationImpl (new Integer(3),
812                                             node5,
813                                             node6,
814                                             "pos",
815                                             fm3);
816     // Start=20, End = 20
817     Annotation annot4 = new AnnotationImpl (new Integer(4),
818                                             node2,
819                                             node5,
820                                             "pos",
821                                             fm4);
822     // Start=10, End = 30
823     Annotation annot5 = new AnnotationImpl (new Integer(5),
824                                             node1,
825                                             node6,
826                                             "pos",
827                                             fm3);
828     // Start=10, End = 15
829     Annotation annot6 = new AnnotationImpl (new Integer(6),
830                                             node1,
831                                             node4,
832                                             "pos",
833                                             fm1);
834     // Start=null, End = null
835     Annotation annot7 = new AnnotationImpl (new Integer(7),
836                                             null,
837                                             null,
838                                             "pos",
839                                             null);
840 
841 // MAP
842   /*
843    annot1 -> Start=10, End = 20,{color="red",Age="25",23="Cristian"}
844    annot2 -> Start=20, End = 30,{color="red",Age="25",23="Cristian"}
845    annot3 -> Start=20, End = 30,{color="red",Age="25",23="Cristian",best="true"}
846    annot4 -> Start=20, End = 20,{color="red",Age="26",23="Cristian"}
847    annot5 -> Start=10, End = 30,{color="red",Age="25",23="Cristian",best="true"}
848    annot6 -> Start=10, End = 15,{color="red",Age="25",23="Cristian"}
849   */
850   // Not compatible situations
851   assertTrue("Those annotations are not compatible!",!annot3.isCompatible(annot2));
852 
853   // Not partially compatible situations
854   // They don't overlap
855   assertTrue("Those annotations("+ annot1 +" & " +
856                                annot2+ ") are not partially compatible!",
857                                        !annot1.isPartiallyCompatible(annot2));
858 
859   // Again they don't overlap
860   assertTrue("Those annotations("+ annot1 +" & " +
861                                annot3+ ") are not partially compatible!",
862                                        !annot1.isPartiallyCompatible(annot3));
863   // Fails because of the age value
864   assertTrue("Those annotations("+ annot1 +" & " +
865                                annot4+ ") are not partially compatible!",
866                                        !annot1.isPartiallyCompatible(annot4));
867   // Fails because of the value of Age
868   assertTrue("Those annotations("+ annot4 +" & " +
869                                annot5+ ") are not partially compatible!",
870                                        !annot4.isPartiallyCompatible(annot5));
871   // Features from annot6 does not subsumes features annot3
872   assertTrue("Those annotations("+ annot3 +" & " +
873                                annot6+ ") are not partially compatible!",
874                                !annot3.isPartiallyCompatible(annot6,null));
875   // Features from annot2 does not subsumes features annot5
876   assertTrue("Those annotations("+ annot5 +" & " +
877                                annot2+ ") are not partially compatible!",
878                                !annot5.isPartiallyCompatible(annot2,null));
879   Set keySet = new HashSet();
880   // They don't overlap
881   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   // Fails because of best feture
888   assertTrue("Those annotations("+ annot5 +" & " +
889                                annot2+ ") are not partially compatible!",
890                                !annot5.isPartiallyCompatible(annot2,keySet));
891   // Fails because start=end in both cases and they don't overlap
892   assertTrue("Those annotations("+ annot4 +" & " +
893                                annot4+ ") are not partially compatible!",
894                                         !annot4.isPartiallyCompatible(annot4));
895 
896   /*
897    annot1 -> Start=10, End = 20,{color="red",Age="25",23="Cristian"}
898    annot2 -> Start=20, End = 30,{color="red",Age="25",23="Cristian"}
899    annot3 -> Start=20, End = 30,{color="red",Age="25",23="Cristian",best="true"}
900    annot4 -> Start=20, End = 20,{color="red",Age="26",23="Cristian"}
901    annot5 -> Start=10, End = 30,{color="red",Age="25",23="Cristian",best="true"}
902    annot6 -> Start=10, End = 15,{color="red",Age="25",23="Cristian"}
903   */
904 
905   // Compatible situations
906   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   // Partially compatible situations
926   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   }// testIsPartiallyCompatibleAndCompatible
944 
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   }// testFeatureSubsumeMethods();
997 
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} // class TestAnnotation
1014
1015