001    /*
002     * The contents of this file are subject to the Mozilla Public
003     * License Version 1.1 (the "License"); you may not use this file
004     * except in compliance with the License. You may obtain a copy of
005     * the License at http://www.mozilla.org/MPL/
006     *
007     * Software distributed under the License is distributed on an "AS
008     * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
009     * implied. See the License for the specific language governing
010     * rights and limitations under the License.
011     *
012     * The Original Code is Knowtator.
013     *
014     * The Initial Developer of the Original Code is University of Colorado.  
015     * Copyright (C) 2005 - 2008.  All Rights Reserved.
016     *
017     * Knowtator was developed by the Center for Computational Pharmacology
018     * (http://compbio.uchcs.edu) at the University of Colorado Health 
019     *  Sciences Center School of Medicine with support from the National 
020     *  Library of Medicine.  
021     *
022     * Current information about Knowtator can be obtained at 
023     * http://knowtator.sourceforge.net/
024     *
025     * Contributor(s):
026     *   Philip V. Ogren <philip@ogren.info> (Original Author)
027     */
028    package edu.uchsc.ccp.iaa.matcher;
029    
030    import java.util.Collection;
031    import java.util.HashSet;
032    import java.util.Set;
033    
034    import edu.uchsc.ccp.iaa.Annotation;
035    import edu.uchsc.ccp.iaa.IAA;
036    
037    public class SpanMatcher implements Matcher {
038            /**
039             * This method will return an annotation with the same class and spans. If
040             * one does not exist, then it will return an annotation with the same spans
041             * (but different class). Otherwise, null is returned.
042             * 
043             * @param annotation
044             * @param compareSetName
045             * @param excludeAnnotations
046             * @param iaa
047             * @param matchResult
048             *            will be set to NONTRIVIAL_MATCH or NONTRIVIAL_NONMATCH.
049             *            Trivial matches and non-matches are not defined for this
050             *            matcher.
051             * @see edu.uchsc.ccp.iaa.matcher.Matcher#match(Annotation, String, Set,
052             *      IAA, MatchResult)
053             * @see edu.uchsc.ccp.iaa.matcher.MatchResult#NONTRIVIAL_MATCH
054             * @see edu.uchsc.ccp.iaa.matcher.MatchResult#NONTRIVIAL_NONMATCH
055             * @see edu.uchsc.ccp.iaa.Annotation#getShortestAnnotation(Collection)
056             */
057    
058            public Annotation match(Annotation annotation, String compareSetName, Set<Annotation> excludeAnnotations, IAA iaa,
059                            MatchResult matchResult) {
060                    Annotation spanAndClassMatch = ClassAndSpanMatcher.match(annotation, compareSetName, iaa, excludeAnnotations);
061                    if (spanAndClassMatch != null) {
062                            matchResult.setResult(MatchResult.NONTRIVIAL_MATCH);
063                            return spanAndClassMatch;
064                    }
065    
066                    Set<Annotation> candidateAnnotations = new HashSet<Annotation>(iaa.getExactlyOverlappingAnnotations(annotation,
067                                    compareSetName));
068                    candidateAnnotations.remove(excludeAnnotations);
069    
070                    for (Annotation candidateAnnotation : candidateAnnotations) {
071                            if (!excludeAnnotations.contains(candidateAnnotation)) {
072                                    matchResult.setResult(MatchResult.NONTRIVIAL_MATCH);
073                                    return candidateAnnotation;
074                            }
075                    }
076                    matchResult.setResult(MatchResult.NONTRIVIAL_NONMATCH);
077                    return null;
078            }
079    
080            public String getName() {
081                    return "Span matcher";
082            }
083    
084            public String getDescription() {
085                    return "Annotations match if they have the same spans.";
086            }
087    
088            public boolean returnsTrivials() {
089                    return false;
090            }
091    
092    }