TestAnnotation.java
0001 /*
0002  *  TestAnnotation.java
0003  *
0004  *  Copyright (c) 1995-2010, The University of Sheffield. See the file
0005  *  COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
0006  *
0007  *  This file is part of GATE (see http://gate.ac.uk/), and is free
0008  *  software, licenced under the GNU Library General Public License,
0009  *  Version 2, June 1991 (in the distribution as file licence.html,
0010  *  and also available at http://gate.ac.uk/gate/licence.html).
0011  *
0012  *  Hamish Cunningham, 7/Feb/00
0013  *
0014  *  $Id: TestAnnotation.java 12006 2009-12-01 17:24:28Z thomas_heitz $
0015  */
0016 
0017 package gate.annotation;
0018 
0019 import java.util.*;
0020 
0021 import junit.framework.*;
0022 
0023 import gate.*;
0024 import gate.util.*;
0025 
0026 /** Tests for the Annotation classes
0027   */
0028 public class TestAnnotation extends TestCase
0029 {
0030   /** Debug flag */
0031   private static final boolean DEBUG = false;
0032 
0033   /** Construction */
0034   public TestAnnotation(String name) { super(name)}
0035 
0036   /** A document */
0037   protected Document doc1;
0038 
0039   /** An annotation set */
0040   protected AnnotationSet basicAS;
0041 
0042   /** An empty feature map */
0043   protected FeatureMap emptyFeatureMap;
0044 
0045   /** Fixture set up */
0046   public void setUp() throws Exception
0047   {
0048   Gate.setNetConnected(false);
0049     if (Gate.getGateHome() == null)
0050       Gate.init();
0051     FeatureMap params = Factory.newFeatureMap();
0052     params.put(Document.DOCUMENT_URL_PARAMETER_NAME, Gate.getUrl("tests/doc0.html"));
0053     params.put(Document.DOCUMENT_MARKUP_AWARE_PARAMETER_NAME, "false");
0054     doc1 = (Document)Factory.createResource("gate.corpora.DocumentImpl",
0055                                                     params);
0056 
0057     emptyFeatureMap = new SimpleFeatureMapImpl();
0058 
0059     basicAS = new AnnotationSetImpl(doc1);
0060     FeatureMap fm = new SimpleFeatureMapImpl();
0061 
0062     basicAS.get("T");          // to trigger type indexing
0063     basicAS.get(new Long(0));  // trigger offset index (though add will too)
0064 
0065     basicAS.add(new Long(10)new Long(20)"T1", fm);    // 0
0066     basicAS.add(new Long(10)new Long(20)"T2", fm);    // 1
0067     basicAS.add(new Long(10)new Long(20)"T3", fm);    // 2
0068     basicAS.add(new Long(10)new Long(20)"T1", fm);    // 3
0069 
0070     fm = new SimpleFeatureMapImpl();
0071     fm.put("pos""NN");
0072     fm.put("author""hamish");
0073     fm.put("version"new Integer(1));
0074 
0075     basicAS.add(new Long(10)new Long(20)"T1", fm);    // 4
0076     basicAS.add(new Long(15)new Long(40)"T1", fm);    // 5
0077     basicAS.add(new Long(15)new Long(40)"T3", fm);    // 6
0078     basicAS.add(new Long(15)new Long(40)"T1", fm);    // 7
0079 
0080     fm = new SimpleFeatureMapImpl();
0081     fm.put("pos""JJ");
0082     fm.put("author""the devil himself");
0083     fm.put("version"new Long(44));
0084     fm.put("created""monday");
0085 
0086     basicAS.add(new Long(15)new Long(40)"T3", fm);    // 8
0087     basicAS.add(new Long(15)new Long(40)"T1", fm);    // 9
0088     basicAS.add(new Long(15)new Long(40)"T1", fm);    // 10
0089 
0090     // Out.println(basicAS);
0091   // setUp
0092 
0093 
0094   /** Test indexing by offset */
0095   public void testOffsetIndex() throws InvalidOffsetException {
0096     AnnotationSet as = new AnnotationSetImpl(doc1);
0097     AnnotationSet asBuf;
0098     Integer newId;
0099     FeatureMap fm = new SimpleFeatureMapImpl();
0100     Annotation a;
0101     Node startNode;
0102     Node endNode;
0103 
0104     newId = as.add(new Long(10)new Long(20)"T", fm);
0105     assertEquals(newId.intValue()11);
0106     a = as.get(newId);
0107 
0108     startNode = a.getStartNode();
0109     endNode = a.getEndNode();
0110     assertEquals(startNode.getId().intValue()4);
0111     assertEquals(endNode.getId().intValue()5);
0112     assertEquals(startNode.getOffset().longValue()10);
0113     assertEquals(endNode.getOffset().longValue()20);
0114 
0115     newId = as.add(new Long(10)new Long(30)"T", fm);
0116     assertEquals(newId.intValue()12);
0117     a = as.get(newId);
0118 
0119     startNode = a.getStartNode();
0120     endNode = a.getEndNode();
0121     assertEquals(startNode.getId().intValue()4);
0122     assertEquals(endNode.getId().intValue()6);
0123     assertEquals(startNode.getOffset().longValue()10);
0124     assertEquals(endNode.getOffset().longValue()30);
0125 
0126     asBuf = as.get(new Long(10));
0127     assertEquals(asBuf.size()2);
0128 
0129   // testOffsetIndex()
0130 
0131   public void testImmutability() {
0132     Long l0= new Long(0);
0133     Long l1= new Long(20);
0134     FeatureMap fm = new SimpleFeatureMapImpl();
0135 
0136     // simple get
0137     AnnotationSet immutable = basicAS.get();
0138     assertTrue(_subtestImmutability(immutable));
0139 
0140     // get Long
0141     immutable = basicAS.get(l0);
0142     assertTrue(_subtestImmutability(immutable));
0143 
0144     // get two Longs
0145     immutable = basicAS.get(l0, l1);
0146     assertTrue(_subtestImmutability(immutable));
0147 
0148     // get Type
0149     immutable = basicAS.get("T1");
0150     assertTrue(_subtestImmutability(immutable));
0151 
0152     // get Types
0153     Set<String> types = new HashSet<String>();
0154     types.add ("T1");
0155     types.add ("T2");
0156     immutable = basicAS.get(types);
0157     assertTrue(_subtestImmutability(immutable));
0158 
0159     // get type + constraint
0160     immutable = basicAS.get("T1", fm);
0161     assertTrue(_subtestImmutability(immutable));
0162 
0163     // get type + constraint + Long
0164     immutable = basicAS.get("T1", fm, l0);
0165     assertTrue(_subtestImmutability(immutable));
0166 
0167     // type + Long + Long
0168     immutable = basicAS.get("T1", l0, l1);
0169     assertTrue(_subtestImmutability(immutable));
0170 
0171     // type + Set of feature names
0172     Set<String> annotset = new HashSet<String>();
0173     annotset.add("pos");
0174     immutable = basicAS.get("T3",annotset);
0175     assertTrue(_subtestImmutability(immutable));
0176   }
0177 
0178   // try all possible sorts of changes to an immutable AS
0179   private final boolean _subtestImmutability(AnnotationSet immutable){
0180     boolean threwException  = false;
0181     Node startNode = null;
0182 
0183     try {
0184       immutable.add(null, null, null, null, null);
0185     }
0186     catch(InvalidOffsetException e) {}
0187     catch(UnsupportedOperationException e) {threwException=true;}
0188 
0189     if (threwException==false)return false;
0190 
0191     try {
0192     immutable.add(startNode, null, null, null);
0193     }
0194     catch(UnsupportedOperationException e){threwException=true;}
0195 
0196     if (threwException==false)return false;
0197 
0198     try {
0199       immutable.add(new Long(0), null, null, null);
0200     }
0201     catch(InvalidOffsetException e) {}
0202     catch(UnsupportedOperationException e){threwException=true;}
0203 
0204     if (threwException==false)return false;
0205 
0206     try {
0207     immutable.add(null);}
0208     catch(UnsupportedOperationException e){threwException=true;}
0209 
0210     if (threwException==false)return false;
0211 
0212     try {
0213     immutable.removeAll(null);}
0214     catch(UnsupportedOperationException e){threwException=true;}
0215 
0216     if (threwException==false)return false;
0217 
0218     try {
0219     immutable.addAll(null);}
0220     catch(UnsupportedOperationException e){threwException=true;}
0221 
0222     if (threwException==false)return false;
0223 
0224     try {
0225     immutable.remove(null);}
0226     catch(UnsupportedOperationException e){threwException=true;}
0227 
0228     if (threwException==false)return false;
0229 
0230     try {
0231     immutable.retainAll(null);}
0232     catch(UnsupportedOperationException e){threwException=true;}
0233 
0234     if (threwException==false)return false;
0235 
0236     try {
0237     immutable.clear();}
0238     catch(UnsupportedOperationException e){threwException=true;}
0239 
0240     return threwException;
0241   }
0242 
0243 
0244   /** Test exception throwing */
0245   public void testExceptions() {
0246     AnnotationSet as = new AnnotationSetImpl(doc1);
0247     boolean threwUp = false;
0248 
0249     try {
0250       as.add(new Long(-1)new Long(1)"T", emptyFeatureMap);
0251     catch (InvalidOffsetException e) {
0252       threwUp = true;
0253     }
0254 
0255     if(! threwUpfail("Should have thrown InvalidOffsetException");
0256     threwUp = false;
0257 
0258     try {
0259       as.add(new Long(1)new Long(-1)"T", emptyFeatureMap);
0260     catch (InvalidOffsetException e) {
0261       threwUp = true;
0262     }
0263 
0264     if(! threwUpfail("Should have thrown InvalidOffsetException");
0265     threwUp = false;
0266 
0267     try {
0268       as.add(new Long(1)new Long(0)"T", emptyFeatureMap);
0269     catch (InvalidOffsetException e) {
0270       threwUp = true;
0271     }
0272 
0273     if(! threwUpfail("Should have thrown InvalidOffsetException");
0274     threwUp = false;
0275 
0276     try {
0277       as.add(null, new Long(1)"T", emptyFeatureMap);
0278     catch (InvalidOffsetException e) {
0279       threwUp = true;
0280     }
0281 
0282     if(! threwUpfail("Should have thrown InvalidOffsetException");
0283     threwUp = false;
0284 
0285     try {
0286       as.add(new Long(1), null, "T", emptyFeatureMap);
0287     catch (InvalidOffsetException e) {
0288       threwUp = true;
0289     }
0290 
0291     if(! threwUpfail("Should have thrown InvalidOffsetException");
0292     threwUp = false;
0293 
0294     try {
0295       as.add(new Long(999999)new Long(100000000)"T", emptyFeatureMap);
0296     catch (InvalidOffsetException e) {
0297       threwUp = true;
0298     }
0299 
0300     /*
0301     // won't work until the doc size check is implemented
0302     if(! threwUp) fail("Should have thrown InvalidOffsetException");
0303     */
0304     threwUp = false;
0305 
0306   // testExceptions()
0307 
0308   /** Test type index */
0309   public void testTypeIndex() throws Exception {
0310     FeatureMap params = Factory.newFeatureMap();
0311     params.put(Document.DOCUMENT_URL_PARAMETER_NAME, Gate.getUrl("tests/doc0.html"));
0312     params.put(Document.DOCUMENT_MARKUP_AWARE_PARAMETER_NAME, "false");
0313     Document doc = (Document)Factory.createResource("gate.corpora.DocumentImpl",
0314                                                     params);
0315     AnnotationSet as = new AnnotationSetImpl(doc);
0316     AnnotationSet asBuf;
0317     FeatureMap fm = new SimpleFeatureMapImpl();
0318     Annotation a;
0319     Node startNode;
0320     Node endNode;
0321 
0322     // to trigger type indexing
0323     as.get("T");
0324     as.add(new Long(10)new Long(20)"T1", fm);    // 0
0325     as.add(new Long(10)new Long(20)"T2", fm);    // 1
0326     as.add(new Long(10)new Long(20)"T3", fm);    // 2
0327     as.add(new Long(10)new Long(20)"T1", fm);    // 3
0328     as.add(new Long(10)new Long(20)"T1", fm);    // 4
0329     as.add(new Long(10)new Long(20)"T1", fm);    // 5
0330     as.add(new Long(10)new Long(20)"T3", fm);    // 6
0331     as.add(new Long(10)new Long(20)"T1", fm);    // 7
0332     as.add(new Long(10)new Long(20)"T3", fm);    // 8
0333     as.add(new Long(10)new Long(20)"T1", fm);    // 9
0334     as.add(new Long(10)new Long(20)"T1", fm);    // 10
0335 
0336     asBuf = as.get("T");
0337     assertEquals(0, asBuf.size());
0338 
0339     asBuf = as.get("T1");
0340     assertEquals(7, asBuf.size());
0341     asBuf = as.get("T2");
0342     assertEquals(1, asBuf.size());
0343     asBuf = as.get("T3");
0344     assertEquals(3, asBuf.size());
0345 
0346     // let's check that we've only got two nodes, what the ids are and so on;
0347     // first construct a sorted set of annotations
0348     SortedSet<Annotation> sortedAnnots = new TreeSet<Annotation>(as);
0349 
0350     // for checking the annotation id
0351     int idCounter = 0;
0352     Iterator<Annotation> iter = sortedAnnots.iterator();
0353     while(iter.hasNext()) {
0354       a = iter.next();
0355 
0356       // check annot ids
0357       assertEquals(idCounter++, a.getId().intValue());
0358 
0359       startNode = a.getStartNode();
0360       endNode = a.getEndNode();
0361 
0362       // start node id
0363       assertEquals(0,  startNode.getId().intValue());
0364 
0365       // start offset
0366       assertEquals(10, startNode.getOffset().longValue());
0367 
0368       // end id
0369       assertEquals(1,  endNode.getId().intValue());
0370 
0371       // end offset
0372       assertEquals(20, endNode.getOffset().longValue());
0373     }
0374 
0375   // testTypeIndex()
0376 
0377   /** Test the annotations set add method that uses existing nodes */
0378   public void testAddWithNodes() throws Exception {
0379     FeatureMap params = Factory.newFeatureMap();
0380     params.put(Document.DOCUMENT_URL_PARAMETER_NAME, Gate.getUrl("tests/doc0.html"));
0381     params.put(Document.DOCUMENT_MARKUP_AWARE_PARAMETER_NAME, "false");
0382     Document doc = (Document)Factory.createResource("gate.corpora.DocumentImpl",
0383                                                     params);
0384     AnnotationSet as = new AnnotationSetImpl(doc);
0385     AnnotationSet asBuf;
0386     Integer newId;
0387     FeatureMap fm = new SimpleFeatureMapImpl();
0388     Annotation a;
0389     Node startNode;
0390     Node endNode;
0391 
0392     // to trigger type indexing
0393     as.get("T");
0394     newId = as.add(new Long(10)new Long(20)"T1", fm);    // 0
0395     a = as.get(newId);
0396     startNode = a.getStartNode();
0397     endNode = a.getEndNode();
0398 
0399     as.add(startNode, endNode, "T2", fm);    // 1
0400     as.add(startNode, endNode, "T3", fm);    // 2
0401     as.add(startNode, endNode, "T1", fm);    // 3
0402     as.add(startNode, endNode, "T1", fm);    // 4
0403     as.add(startNode, endNode, "T1", fm);    // 5
0404     as.add(startNode, endNode, "T3", fm);    // 6
0405     as.add(startNode, endNode, "T1", fm);    // 7
0406     as.add(startNode, endNode, "T3", fm);    // 8
0407     as.add(startNode, endNode, "T1", fm);    // 9
0408     as.add(startNode, endNode, "T1", fm);    // 10
0409 
0410     asBuf = as.get("T");
0411     assertEquals(0, asBuf.size());
0412 
0413     asBuf = as.get("T1");
0414     assertEquals(7, asBuf.size());
0415     asBuf = as.get("T2");
0416     assertEquals(1, asBuf.size());
0417     asBuf = as.get("T3");
0418     assertEquals(3, asBuf.size());
0419 
0420     // let's check that we've only got two nodes, what the ids are and so on;
0421     // first construct a sorted set of annotations
0422     Set<Annotation> sortedAnnots = new TreeSet<Annotation>(as);
0423 
0424     // for checking the annotation id
0425     int idCounter = 0;
0426     Iterator<Annotation> iter = sortedAnnots.iterator();
0427     while(iter.hasNext()) {
0428       a = iter.next();
0429       // check annot ids
0430       assertEquals(idCounter++, a.getId().intValue());
0431 
0432       startNode = a.getStartNode();
0433       endNode = a.getEndNode();
0434 
0435       // start node id
0436       assertEquals(0,  startNode.getId().intValue());
0437 
0438       // start offset
0439       assertEquals(10, startNode.getOffset().longValue());
0440 
0441       // end id
0442       assertEquals(1,  endNode.getId().intValue());
0443 
0444       // end offset
0445       assertEquals(20, endNode.getOffset().longValue());
0446     }
0447 
0448   // testAddWithNodes()
0449 
0450   /** Test complex get (with type, offset and feature contraints) */
0451   public void testGetFeatureMapOffset() throws InvalidOffsetException {
0452     AnnotationSet asBuf;
0453 
0454     FeatureMap constraints = new SimpleFeatureMapImpl();
0455     constraints.put("pos""NN");
0456 
0457     //Out.println(basicAS);
0458     //Out.println(constraints);
0459 
0460     asBuf = basicAS.get("T1", constraints);
0461     assertEquals(3, asBuf.size());
0462     asBuf = basicAS.get("T3", constraints);
0463     assertEquals(1, asBuf.size());
0464     asBuf = basicAS.get("T1", constraints, new Long(12));
0465     assertEquals(2, asBuf.size());
0466     asBuf = basicAS.get("T1", constraints, new Long(10));
0467     assertEquals(1, asBuf.size());
0468     asBuf = basicAS.get("T1", constraints, new Long(11));
0469     assertEquals(2, asBuf.size());
0470     asBuf = basicAS.get("T1", constraints, new Long(9));
0471     assertEquals(1, asBuf.size());
0472 
0473     constraints.put("pos""JJ");
0474     //Out.println(constraints);
0475     asBuf = basicAS.get("T1", constraints, new Long(0));
0476     assertEquals(0, asBuf.size());
0477     asBuf = basicAS.get("T1", constraints, new Long(14));
0478     assertEquals(2, asBuf.size());
0479 
0480     constraints.put("author""valentin");
0481     asBuf = basicAS.get("T1", constraints, new Long(14));
0482     assertEquals(0, asBuf.size());
0483 
0484     constraints.put("author""the devil himself");
0485     asBuf = basicAS.get("T1", constraints, new Long(14));
0486     assertEquals(2, asBuf.size());
0487 
0488     asBuf = basicAS.get("T1", constraints, new Long(5));
0489     assertEquals(0, asBuf.size());
0490 
0491     constraints.put("this feature isn't""there at all");
0492     asBuf = basicAS.get("T1", constraints, new Long(14));
0493     assertEquals(0, asBuf.size());
0494 
0495   // testGetFeatureMapOffset()
0496 
0497   public void testGetStringLongLong() throws InvalidOffsetException {
0498     FeatureMap fm = new SimpleFeatureMapImpl();
0499     basicAS.add(21l25l"T1", fm);
0500     basicAS.add(22l25l"T1", fm);
0501     basicAS.add(45l50l"T3", fm);
0502 
0503     assertEquals(0, basicAS.get(0l5l).size());
0504     assertEquals(0, basicAS.get("T1"0l5l).size());
0505 
0506     assertEquals(5, basicAS.get(10l12l).size());
0507     assertEquals(3, basicAS.get("T1"10l12l).size());
0508 
0509     assertEquals(11, basicAS.get(10l16l).size());
0510     assertEquals(7, basicAS.get("T1"10l16l).size());
0511 
0512     assertEquals(11, basicAS.get(10l20l).size());
0513     assertEquals(7, basicAS.get("T1"10l20l).size());
0514 
0515     assertEquals(6, basicAS.get(20l21l).size());
0516     assertEquals(4, basicAS.get("T1"20l21l).size());
0517 
0518     assertEquals(0, basicAS.get(41l42l).size());
0519     assertEquals(0, basicAS.get("T1"41l42l).size());
0520   // testGetStringLongLong()
0521 
0522   public void testGetCovering() throws InvalidOffsetException {
0523     assertEquals(0, basicAS.getCovering(null, 0l5l).size());
0524     assertEquals(0, basicAS.getCovering("T1"0l5l).size());
0525 
0526     //null and blank strings should be treated the same.  Just test
0527     //with both a couple of times.  Mostly can just test with null.
0528     assertEquals(0, basicAS.getCovering(null, 9l12l).size());
0529     assertEquals(0, basicAS.getCovering("  "9l12l).size());
0530     assertEquals(0, basicAS.getCovering("T1"9l12l).size());
0531 
0532     assertEquals(5, basicAS.getCovering(null, 10l20l).size());
0533     assertEquals(5, basicAS.getCovering("  "10l20l).size());
0534     assertEquals(3, basicAS.getCovering("T1"10l20l).size());
0535 
0536     assertEquals(11, basicAS.getCovering(null, 16l20l).size());
0537     assertEquals(7, basicAS.getCovering("T1"16l20l).size());
0538 
0539     assertEquals(6, basicAS.getCovering(null, 16l21l).size());
0540     assertEquals(4, basicAS.getCovering("T1"16l21l).size());
0541 
0542   // testGetCovering()
0543 
0544   /** Test remove */
0545   public void testRemove() {
0546     AnnotationSet asBuf = basicAS.get("T1");
0547     assertEquals(7, asBuf.size());
0548     asBuf = basicAS.get(new Long(9));
0549     assertEquals(5, asBuf.size());
0550 
0551     basicAS.remove(basicAS.get(new Integer(0)));
0552 
0553     assertEquals(10, basicAS.size());
0554     assertEquals(10((AnnotationSetImplbasicAS).annotsById.size());
0555 
0556     asBuf = basicAS.get("T1");
0557     assertEquals(6, asBuf.size());
0558 
0559     asBuf = basicAS.get(new Long(9));
0560     assertEquals(4, asBuf.size());
0561 
0562     assertEquals(null, basicAS.get(new Integer(0)));
0563     basicAS.remove(basicAS.get(new Integer(8)));
0564     assertEquals(9, basicAS.size());
0565     basicAS.removeAll(basicAS);
0566     assertEquals(0, basicAS.get().size());
0567     assertEquals(0, basicAS.get("T1").size());
0568     assertEquals(null, basicAS.get(new Integer(0)));
0569   // testRemove()
0570 
0571   public void testRemoveInexistant() throws Exception{
0572     basicAS.add(new Long(0)new Long(10)"Foo", emptyFeatureMap);
0573     Annotation ann = basicAS.get("Foo").iterator().next();
0574     basicAS.remove(ann);
0575     //the second remove should do nothing...
0576     basicAS.remove(ann);
0577   }
0578 
0579   /** Test iterator remove */
0580 /* This seems to be a bad idea - just testing order of hashset iterator, which
0581  * isn't stable....
0582  *
0583   public void testIteratorRemove() {
0584     AnnotationSet asBuf = basicAS.get("T1");
0585     assertEquals(7, asBuf.size());
0586     asBuf = basicAS.get(new Long(9));
0587     assertEquals(5, asBuf.size());
0588 
0589     // remove annotation with id 0; this is returned last by the
0590     // iterator
0591     Iterator<Annotation> iter = basicAS.iterator();
0592     Annotation ann = null;
0593     while(iter.hasNext()){
0594       ann = iter.next();
0595     }
0596     iter.remove();
0597     assertEquals("Last annotation from iterator not ID 0!", 0,
0598              ann.getId().intValue());
0599 
0600     assertEquals(10, basicAS.size());
0601     assertEquals(10, ((AnnotationSetImpl) basicAS).annotsById.size());
0602     asBuf = basicAS.get("T1");
0603     assertEquals(6, asBuf.size());
0604     asBuf = basicAS.get(new Long(9));
0605     assertEquals(4, asBuf.size());
0606     assertEquals(null, basicAS.get(new Integer(0)));
0607     basicAS.remove(basicAS.get(new Integer(8)));
0608 
0609   } // testIteratorRemove()
0610   */
0611 
0612   /** Test iterator */
0613   public void testIterator() {
0614     Iterator<Annotation> iter = basicAS.iterator();
0615     Annotation[] annots = new Annotation[basicAS.size()];
0616     int i = 0;
0617 
0618     while(iter.hasNext()) {
0619       Annotation a = iter.next();
0620       annots[i++= a;
0621 
0622       assertTrue(basicAS.contains(a));
0623       iter.remove();
0624       assertTrue(!basicAS.contains(a));
0625     // while
0626 
0627     i = 0;
0628     while(i < annots.length) {
0629       basicAS.add(annots[i++]);
0630       assertEquals(i, basicAS.size());
0631     // while
0632 
0633     AnnotationSet asBuf = basicAS.get("T1");
0634     assertEquals(7, asBuf.size());
0635     asBuf = basicAS.get(new Long(9));
0636     assertEquals(5, asBuf.size());
0637   // testIterator
0638 
0639   /** Test Set methods */
0640   public void testSetMethods() {
0641     Annotation a = basicAS.get(new Integer(6));
0642     assertTrue(basicAS.contains(a));
0643 
0644     Annotation[] annotArray = basicAS.toArray(new Annotation[0]);
0645     Object[] annotObjectArray = basicAS.toArray();
0646     assertEquals(11, annotArray.length);
0647     assertEquals(11, annotObjectArray.length);
0648 
0649     SortedSet<Annotation> sortedAnnots = new TreeSet<Annotation>(basicAS);
0650     annotArray = sortedAnnots.toArray(new Annotation[0]);
0651     for(int i = 0; i<11; i++)
0652       assertTrueannotArray[i].getId().equals(new Integer(i)) );
0653 
0654     Annotation a1 = basicAS.get(new Integer(3));
0655     Annotation a2 = basicAS.get(new Integer(4));
0656     Set<Annotation> a1a2 = new HashSet<Annotation>();
0657     a1a2.add(a1);
0658     a1a2.add(a2);
0659     assertTrue(basicAS.contains(a1));
0660     assertTrue(basicAS.containsAll(a1a2));
0661     basicAS.removeAll(a1a2);
0662 
0663     assertEquals(9, basicAS.size());
0664     assertTrue(! basicAS.contains(a1));
0665     assertTrue(! basicAS.containsAll(a1a2));
0666 
0667     //this will not work anymore as the semantics of addAll has changed (new
0668     //annotations are created in order to avoid ID clashes
0669 /*
0670     basicAS.addAll(a1a2);
0671     assertTrue(basicAS.contains(a2));
0672     assertTrue(basicAS.containsAll(a1a2));
0673 
0674     assertTrue(basicAS.retainAll(a1a2));
0675     assertTrue(basicAS.equals(a1a2));
0676 */
0677     basicAS.clear();
0678     assertTrue(basicAS.isEmpty());
0679 
0680   // testSetMethods()
0681 
0682   /** Test AnnotationSetImpl */
0683   public void testAnnotationSet() throws Exception {
0684     // constuct an empty AS
0685     FeatureMap params = Factory.newFeatureMap();
0686     params.put(Document.DOCUMENT_URL_PARAMETER_NAME, Gate.getUrl("tests/doc0.html"));
0687     params.put(Document.DOCUMENT_MARKUP_AWARE_PARAMETER_NAME, "false");
0688     Document doc = (Document)Factory.createResource("gate.corpora.DocumentImpl",
0689                                                     params);
0690 
0691     AnnotationSet as = new AnnotationSetImpl(doc);
0692     assertEquals(as.size()0);
0693 
0694     // add some annotations
0695     FeatureMap fm1 = Factory.newFeatureMap();
0696     fm1.put("test""my-value");
0697     FeatureMap fm2 = Factory.newFeatureMap();
0698     fm2.put("test""my-value-different");
0699     FeatureMap fm3 = Factory.newFeatureMap();
0700     fm3.put("another test""different my-value");
0701 
0702     Integer newId;
0703     newId =
0704       as.add(new Long(0)new Long(10)"Token", fm1);
0705     assertEquals(newId.intValue()0);
0706     newId =
0707       as.add(new Long(11)new Long(12)"Token", fm2);
0708     assertEquals(newId.intValue()1);
0709     assertEquals(as.size()2);
0710     assertTrue(! as.isEmpty());
0711     newId =
0712       as.add(new Long(15)new Long(22)"Syntax", fm1);
0713 
0714     // get by ID; remove; add(object)
0715     Annotation a = as.get(new Integer(1));
0716     as.remove(a);
0717     assertEquals(as.size()2);
0718     as.add(a);
0719     assertEquals(as.size()3);
0720 
0721     // iterate over the annotations
0722     Iterator<Annotation> iter = as.iterator();
0723     while(iter.hasNext()) {
0724       a = iter.next();
0725       if(a.getId().intValue() != 2)
0726         assertEquals(a.getType()"Token");
0727       assertEquals(a.getFeatures().size()1);
0728     }
0729 
0730     // add some more
0731     newId =
0732       as.add(new Long(0)new Long(12)"Syntax", fm3);
0733     assertEquals(newId.intValue()3);
0734     newId =
0735       as.add(new Long(14)new Long(22)"Syntax", fm1);
0736     assertEquals(newId.intValue()4);
0737     assertEquals(as.size()5);
0738     newId =
0739       as.add(new Long(15)new Long(22)"Syntax"new SimpleFeatureMapImpl());
0740 
0741     //get by feature names
0742     Set<String> hs = new HashSet<String>();
0743     hs.add("test");
0744     AnnotationSet fnSet = as.get("Token", hs);
0745     assertEquals(fnSet.size()2);
0746     //now try without a concrete type, just features
0747     //we'll get some Syntax ones now too
0748     fnSet = as.get(null, hs);
0749     assertEquals(fnSet.size()4);
0750 
0751 
0752     // indexing by type
0753     ((AnnotationSetImplas).indexByType();
0754     AnnotationSet tokenAnnots = as.get("Token");
0755     assertEquals(tokenAnnots.size()2);
0756 
0757     // indexing by position
0758     AnnotationSet annotsAfter10 = as.get(new Long(15));
0759     if(annotsAfter10 == null)
0760       fail("no annots found after offset 10");
0761     assertEquals(annotsAfter10.size()2);
0762 
0763   // testAnnotationSet
0764   /** Test suite routine for the test runner */
0765   public static Test suite() {
0766     return new TestSuite(TestAnnotation.class);
0767   // suite
0768 
0769   /** Test get with offset and no annotation starting at given offset */
0770   public void _testGap() throws InvalidOffsetException {
0771     AnnotationSet as = basicAS;
0772     as.clear();
0773     FeatureMap fm = Factory.newFeatureMap();
0774     fm.put("A""B");
0775     as.add(new Long(0)new Long(10)"foo", fm);
0776     as.add(new Long(11)new Long(20)"foo", fm);
0777     as.add(new Long(10)new Long(11)"space", fm);
0778 
0779     //do the input selection (ignore spaces)
0780     Set<String> input = new HashSet<String>();
0781     input.add("foo");
0782     input.add("foofoo");
0783     AnnotationSet annotations = null;
0784 
0785     if(input.isEmpty()) annotations = as;
0786     else{
0787       Iterator<String> typesIter = input.iterator();
0788       AnnotationSet ofOneType = null;
0789 
0790       while(typesIter.hasNext()){
0791         ofOneType = as.get(typesIter.next());
0792 
0793         if(ofOneType != null){
0794           //System.out.println("Adding " + ofOneType.getAllTypes());
0795           if(annotations == nullannotations = ofOneType;
0796           else annotations.addAll(ofOneType);
0797         }
0798       }
0799     }
0800     /* if(annotations == null) annotations = new AnnotationSetImpl(doc); */
0801     if (DEBUG)
0802       Out.println(
0803         "Actual input:" + annotations.getAllTypes() "\n" + annotations
0804       );
0805 
0806     AnnotationSet res =
0807       annotations.get("foo", Factory.newFeatureMap()new Long(10));
0808 
0809     if (DEBUG)
0810       Out.println(res);
0811     assertTrue(!res.isEmpty());
0812   }
0813 
0814   /** Test Overlaps */
0815   public void testOverlapsAndCoextensive() throws InvalidOffsetException {
0816     Node node1 = new NodeImpl(new Integer(1),new Long(10));
0817     Node node2 = new NodeImpl(new Integer(2),new Long(20));
0818     Node node4 = new NodeImpl(new Integer(4),new Long(15));
0819     Node node5 = new NodeImpl(new Integer(5),new Long(20));
0820     Node node6 = new NodeImpl(new Integer(6),new Long(30));
0821 
0822     FeatureMap fm1 = new SimpleFeatureMapImpl();
0823     fm1.put("color","red");
0824     fm1.put("Age",new Long(25));
0825     fm1.put(new Long(23)"Cristian");
0826 
0827     FeatureMap fm2 = new SimpleFeatureMapImpl();
0828     fm2.put("color","red");
0829     fm2.put("Age",new Long(25));
0830     fm2.put(new Long(23)"Cristian");
0831 
0832     FeatureMap fm4 = new SimpleFeatureMapImpl();
0833     fm4.put("color","red");
0834     fm4.put("Age",new Long(26));
0835     fm4.put(new Long(23)"Cristian");
0836 
0837     FeatureMap fm3 = new SimpleFeatureMapImpl();
0838     fm3.put("color","red");
0839     fm3.put("Age",new Long(25));
0840     fm3.put(new Long(23)"Cristian");
0841     fm3.put("best",new Boolean(true));
0842 
0843     // Start=10, End = 20
0844     Annotation annot1 = createAnnotation(new Integer(1),
0845                                            node1,
0846                                            node2,
0847                                            "pos",
0848                                            null);
0849     // Start=20, End = 30
0850     Annotation annot2 = createAnnotation (new Integer(2),
0851                                             node2,
0852                                             node6,
0853                                             "pos",
0854                                             null);
0855     // Start=20, End = 30
0856     Annotation annot3 = createAnnotation (new Integer(3),
0857                                             node5,
0858                                             node6,
0859                                             "pos",
0860                                             null);
0861     // Start=20, End = 20
0862     Annotation annot4 = createAnnotation (new Integer(4),
0863                                             node2,
0864                                             node5,
0865                                             "pos",
0866                                             null);
0867     // Start=10, End = 30
0868     Annotation annot5 = createAnnotation (new Integer(5),
0869                                             node1,
0870                                             node6,
0871                                             "pos",
0872                                             null);
0873     // Start=10, End = 15
0874     Annotation annot6 = createAnnotation (new Integer(6),
0875                                             node1,
0876                                             node4,
0877                                             "pos",
0878                                             null);
0879     // Start=null, End = null
0880     Annotation annot7 = createAnnotation (new Integer(7),
0881                                             null,
0882                                             null,
0883                                             "pos",
0884                                             null);
0885 
0886     // MAP
0887     // annot1 -> Start=10, End = 20
0888     // annot2 -> Start=20, End = 30
0889     // annot3 -> Start=20, End = 30
0890     // annot4 -> Start=20, End = 20
0891     // annot5 -> Start=10, End = 30
0892     // annot6 -> Start=10, End = 15
0893 
0894     // Not overlaping situations
0895    assertTrue("Those annotations does not overlap!",!annot1.overlaps(annot3));
0896    assertTrue("Those annotations does not overlap!",!annot1.overlaps(annot2));
0897    assertTrue("Those annotations does not overlap!",!annot2.overlaps(annot1));
0898    assertTrue("Those annotations does not overlap!",!annot3.overlaps(annot1));
0899    assertTrue("Those annotations does not overlap!",!annot4.overlaps(annot6));
0900    assertTrue("Those annotations does not overlap!",!annot6.overlaps(annot4));
0901 
0902    assertTrue("Those annotations does not overlap!",!annot6.overlaps(null));
0903    assertTrue("Those annotations does not overlap!",!annot1.overlaps(annot7));
0904 
0905    // Overlaping situations
0906    assertTrue("Those annotations does overlap!",annot4.overlaps(annot5));
0907    assertTrue("Those annotations does overlap!",annot5.overlaps(annot4));
0908    assertTrue("Those annotations does overlap!",annot1.overlaps(annot6));
0909    assertTrue("Those annotations does overlap!",annot6.overlaps(annot1));
0910    assertTrue("Those annotations does overlap!",annot2.overlaps(annot5));
0911    assertTrue("Those annotations does overlap!",annot5.overlaps(annot2));
0912 
0913    // Not coextensive situations
0914    assertTrue("Those annotations are not coextensive!",!annot1.coextensive(annot2));
0915    assertTrue("Those annotations are not coextensive!",!annot2.coextensive(annot1));
0916    assertTrue("Those annotations are not coextensive!",!annot4.coextensive(annot3));
0917    assertTrue("Those annotations are not coextensive!",!annot3.coextensive(annot4));
0918    assertTrue("Those annotations are not coextensive!",!annot4.coextensive(annot7));
0919    assertTrue("Those annotations are not coextensive!",!annot5.coextensive(annot6));
0920    assertTrue("Those annotations are not coextensive!",!annot6.coextensive(annot5));
0921    //Coextensive situations
0922    assertTrue("Those annotations are coextensive!",annot2.coextensive(annot2));
0923    assertTrue("Those annotations are coextensive!",annot2.coextensive(annot3));
0924    assertTrue("Those annotations are coextensive!",annot3.coextensive(annot2));
0925 
0926   }//testOverlapsAndCoextensive
0927 
0928   /** Test Coextensive */
0929   public void testIsPartiallyCompatibleAndCompatible()
0930                                                 throws InvalidOffsetException {
0931     Node node1 = new NodeImpl(new Integer(1),new Long(10));
0932     Node node2 = new NodeImpl(new Integer(2),new Long(20));
0933     Node node4 = new NodeImpl(new Integer(4),new Long(15));
0934     Node node5 = new NodeImpl(new Integer(5),new Long(20));
0935     Node node6 = new NodeImpl(new Integer(6),new Long(30));
0936 
0937     FeatureMap fm1 = new SimpleFeatureMapImpl();
0938     fm1.put("color","red");
0939     fm1.put("Age",new Long(25));
0940     fm1.put(new Long(23)"Cristian");
0941 
0942     FeatureMap fm2 = new SimpleFeatureMapImpl();
0943     fm2.put("color","red");
0944     fm2.put("Age",new Long(25));
0945     fm2.put(new Long(23)"Cristian");
0946 
0947     FeatureMap fm4 = new SimpleFeatureMapImpl();
0948     fm4.put("color","red");
0949     fm4.put("Age",new Long(26));
0950     fm4.put(new Long(23)"Cristian");
0951 
0952     FeatureMap fm3 = new SimpleFeatureMapImpl();
0953     fm3.put("color","red");
0954     fm3.put("Age",new Long(25));
0955     fm3.put(new Long(23)"Cristian");
0956     fm3.put("best",new Boolean(true));
0957 
0958     // Start=10, End = 20
0959     Annotation annot1 = createAnnotation(new Integer(1),
0960                                            node1,
0961                                            node2,
0962                                            "pos",
0963                                            fm1);
0964     // Start=20, End = 30
0965     Annotation annot2 = createAnnotation (new Integer(2),
0966                                             node2,
0967                                             node6,
0968                                             "pos",
0969                                             fm2);
0970     // Start=20, End = 30
0971     Annotation annot3 = createAnnotation (new Integer(3),
0972                                             node5,
0973                                             node6,
0974                                             "pos",
0975                                             fm3);
0976     // Start=20, End = 20
0977     Annotation annot4 = createAnnotation (new Integer(4),
0978                                             node2,
0979                                             node5,
0980                                             "pos",
0981                                             fm4);
0982     // Start=10, End = 30
0983     Annotation annot5 = createAnnotation (new Integer(5),
0984                                             node1,
0985                                             node6,
0986                                             "pos",
0987                                             fm3);
0988     // Start=10, End = 15
0989     Annotation annot6 = createAnnotation (new Integer(6),
0990                                             node1,
0991                                             node4,
0992                                             "pos",
0993                                             fm1);
0994 
0995 // MAP
0996   /*
0997    annot1 -> Start=10, End = 20,{color="red",Age="25",23="Cristian"}
0998    annot2 -> Start=20, End = 30,{color="red",Age="25",23="Cristian"}
0999    annot3 -> Start=20, End = 30,{color="red",Age="25",23="Cristian",best="true"}
1000    annot4 -> Start=20, End = 20,{color="red",Age="26",23="Cristian"}
1001    annot5 -> Start=10, End = 30,{color="red",Age="25",23="Cristian",best="true"}
1002    annot6 -> Start=10, End = 15,{color="red",Age="25",23="Cristian"}
1003   */
1004   // Not compatible situations
1005   assertTrue("Those annotations are not compatible!",!annot3.isCompatible(annot2));
1006 
1007   // Not partially compatible situations
1008   // They don't overlap
1009   assertTrue("Those annotations("+ annot1 +" & " +
1010                                annot2+ ") are not partially compatible!",
1011                                        !annot1.isPartiallyCompatible(annot2));
1012 
1013   // Again they don't overlap
1014   assertTrue("Those annotations("+ annot1 +" & " +
1015                                annot3+ ") are not partially compatible!",
1016                                        !annot1.isPartiallyCompatible(annot3));
1017   // Fails because of the age value
1018   assertTrue("Those annotations("+ annot1 +" & " +
1019                                annot4+ ") are not partially compatible!",
1020                                        !annot1.isPartiallyCompatible(annot4));
1021   // Fails because of the value of Age
1022   assertTrue("Those annotations("+ annot4 +" & " +
1023                                annot5+ ") are not partially compatible!",
1024                                        !annot4.isPartiallyCompatible(annot5));
1025   // Features from annot6 does not subsumes features annot3
1026   assertTrue("Those annotations("+ annot3 +" & " +
1027                                annot6+ ") are not partially compatible!",
1028                                !annot3.isPartiallyCompatible(annot6,null));
1029   // Features from annot2 does not subsumes features annot5
1030   assertTrue("Those annotations("+ annot5 +" & " +
1031                                annot2+ ") are not partially compatible!",
1032                                !annot5.isPartiallyCompatible(annot2,null));
1033   Set<Object> keySet = new HashSet<Object>();
1034   // They don't overlap
1035   assertTrue("Those annotations("+ annot2 +" & " +
1036                                annot4+ ") are not partially compatible!",
1037                                !annot2.isPartiallyCompatible(annot4,keySet));
1038   keySet.add("color");
1039   keySet.add("Age");
1040   keySet.add("best");
1041   // Fails because of best feture
1042   assertTrue("Those annotations("+ annot5 +" & " +
1043                                annot2+ ") are not partially compatible!",
1044                                !annot5.isPartiallyCompatible(annot2,keySet));
1045   // Fails because start=end in both cases and they don't overlap
1046   assertTrue("Those annotations("+ annot4 +" & " +
1047                                annot4+ ") are not partially compatible!",
1048                                         !annot4.isPartiallyCompatible(annot4));
1049 
1050   /*
1051    annot1 -> Start=10, End = 20,{color="red",Age="25",23="Cristian"}
1052    annot2 -> Start=20, End = 30,{color="red",Age="25",23="Cristian"}
1053    annot3 -> Start=20, End = 30,{color="red",Age="25",23="Cristian",best="true"}
1054    annot4 -> Start=20, End = 20,{color="red",Age="26",23="Cristian"}
1055    annot5 -> Start=10, End = 30,{color="red",Age="25",23="Cristian",best="true"}
1056    annot6 -> Start=10, End = 15,{color="red",Age="25",23="Cristian"}
1057   */
1058 
1059   // Compatible situations
1060   assertTrue("Those annotations("+ annot2 +" & " +
1061                                annot3+ ") should be compatible!",
1062                                       annot2.isCompatible(annot3));
1063   assertTrue("Those annotations("+ annot2 +" & " +
1064                                annot3+ ") should be compatible!",
1065                                       annot2.isCompatible(annot3,null));
1066   assertTrue("Those annotations("+ annot2 +" & " +
1067                                annot3+ ") should be compatible!",
1068                                      annot2.isCompatible(annot3,new HashSet<String>()));
1069   assertTrue("Those annotations("+ annot4 +" & " +
1070                                annot4+ ") should be compatible!",
1071                                         annot4.isCompatible(annot4));
1072   keySet = new HashSet<Object>();
1073   keySet.add("color");
1074   keySet.add(new Long(23));
1075   assertTrue("Those annotations("+ annot3 +" & " +
1076                                annot2+ ") should be compatible!",
1077                                       annot3.isCompatible(annot2,keySet));
1078 
1079   // Partially compatible situations
1080   assertTrue("Those annotations("+ annot2 +" & " +
1081                                annot3+ ") should be partially compatible!",
1082                                         annot2.isPartiallyCompatible(annot3));
1083   assertTrue("Those annotations("+ annot2 +" & " +
1084                                annot2+ ") should be partially compatible!",
1085                                         annot2.isPartiallyCompatible(annot2));
1086   assertTrue("Those annotations are partially compatible!",
1087                                         annot1.isPartiallyCompatible(annot5));
1088   assertTrue("Those annotations are partially compatible!",
1089                                         annot1.isPartiallyCompatible(annot6));
1090   assertTrue("Those annotations are partially compatible!",
1091                                         annot3.isPartiallyCompatible(annot5));
1092   assertTrue("Those annotations are partially compatible!",
1093                                         annot5.isPartiallyCompatible(annot3));
1094   assertTrue("Those annotations are partially compatible!",
1095                                         annot6.isPartiallyCompatible(annot5));
1096 
1097   }// testIsPartiallyCompatibleAndCompatible
1098 
1099 
1100   public void testFeatureSubsumeMethods(){
1101 
1102     FeatureMap fm1 = Factory.newFeatureMap();
1103     fm1.put("k1","v1");
1104     fm1.put("k2","v2");
1105 
1106     FeatureMap fm2 = Factory.newFeatureMap();
1107     fm2.put("k1","v1");
1108 
1109     Set<String> featKeysSet1 = new HashSet<String>();
1110     featKeysSet1.add("k1");
1111     featKeysSet1.add("k2");
1112     featKeysSet1.add("k3");
1113     featKeysSet1.add("k4");
1114 
1115     assertTrue(fm1 + " should subsume " + fm2 + " using the key set" +
1116                                featKeysSet1,fm1.subsumes(fm2, featKeysSet1));
1117     assertTrue(fm1 + " should subsume " + fm2 +
1118                             " taking all feat into consideration",
1119                             fm1.subsumes(fm2, null));
1120 
1121     FeatureMap fm3 = Factory.newFeatureMap();
1122     fm3.put("k1","v1");
1123     fm3.put("k2","v2");
1124     fm3.put("k3",new Integer(3));
1125 
1126     Set<String> featKeysSet2 = new HashSet<String>();
1127     featKeysSet2.add("k1");
1128 
1129     assertTrue(fm1 + " should subsume " + fm3 + " using the key set" +
1130                           featKeysSet2,fm1.subsumes(fm3, featKeysSet2));
1131     assertTrue(fm1 + " should NOT subsume " + fm3 +
1132                                 " taking all feats into consideration",
1133                                 !fm1.subsumes(fm3,null));
1134 
1135     FeatureMap fm4 = Factory.newFeatureMap();
1136     fm4.put("k1",new Integer(2));
1137     fm4.put("k2","v2");
1138     fm4.put("k3","v3");
1139 
1140     Set<String> featKeysSet3 = new HashSet<String>();
1141     featKeysSet3.add("k2");
1142 
1143     assertTrue(fm3 + " should subsume " + fm4 + " using the key set" +
1144                               featKeysSet3, fm4.subsumes(fm3,featKeysSet3));
1145     assertTrue(fm4 + " should NOT subsume " + fm3 +
1146                                 " taking all feats into consideration",
1147                                 !fm4.subsumes(fm3,null));
1148 
1149 
1150   }// testFeatureSubsumeMethods();
1151 
1152   protected Annotation createAnnotation
1153     (Integer id, Node start, Node end, String type, FeatureMap features) {
1154       return new AnnotationImpl(id, start, end, type, features);
1155   }
1156 
1157   public static void main(String[] args){
1158 
1159     try{
1160       TestAnnotation testAnnot = new TestAnnotation("");
1161       testAnnot.setUp();
1162       testAnnot.testIterator();
1163       testAnnot._testGap();
1164       testAnnot.tearDown();
1165       testAnnot.testOverlapsAndCoextensive();
1166       testAnnot.testIsPartiallyCompatibleAndCompatible();
1167     }catch(Throwable t){
1168       t.printStackTrace();
1169     }
1170   }
1171 // class TestAnnotation