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    /*
029     * AnnotatorUtil.java
030     *
031     * Created on July 26, 2005, 5:22 PM
032     */
033    
034    package edu.uchsc.ccp.knowtator.util;
035    
036    import java.awt.Component;
037    import java.util.Collection;
038    
039    import javax.swing.JOptionPane;
040    
041    import edu.stanford.smi.protege.model.Cls;
042    import edu.stanford.smi.protege.model.KnowledgeBase;
043    import edu.stanford.smi.protege.model.SimpleInstance;
044    import edu.stanford.smi.protege.ui.DisplayUtilities;
045    import edu.stanford.smi.protege.util.CollectionUtilities;
046    import edu.uchsc.ccp.knowtator.AnnotationUtil;
047    import edu.uchsc.ccp.knowtator.FilterUtil;
048    import edu.uchsc.ccp.knowtator.KnowtatorProjectUtil;
049    
050    /**
051     * 
052     * @author Philip V. Ogren
053     */
054    public class AnnotatorUtil {
055    
056            /**
057             * This method provides dialogs which collect the annotator and filter
058             * instances needed to forward on to the other batchAnnotatorChange method.
059             */
060    
061            public static void batchAnnotatorChange(Component parent, KnowtatorProjectUtil kpu, AnnotationUtil annotationUtil,
062                            FilterUtil filterUtil) {
063                    int option = JOptionPane.showConfirmDialog(parent,
064                                    "The following dialogs provide a way to set the annotator slot for a set of \n"
065                                                    + "annotations.  The next dialog will ask you to choose an annotator.  The value\n"
066                                                    + "you select will be used to set the value of the 'annotator' slot.  The \n"
067                                                    + "following dialog will ask you to choose an annotation filter.  The value\n "
068                                                    + "you select will be used to decide which annotations will be updated",
069                                    "Change annotator for annotations", JOptionPane.OK_CANCEL_OPTION);
070                    if (option == JOptionPane.OK_OPTION) {
071                            SimpleInstance annotator = (SimpleInstance) DisplayUtilities.pickInstance(parent, CollectionUtilities
072                                            .createCollection(kpu.getAnnotatorCls()), "Choose annotator of annotations");
073                            if (annotator == null)
074                                    return;
075                            SimpleInstance filter = (SimpleInstance) DisplayUtilities.pickInstance(parent, CollectionUtilities
076                                            .createCollection(kpu.getFilterCls()), "Choose an annotation filter");
077                            if (filter == null)
078                                    return;
079                            batchAnnotatorChange(annotator, kpu, filter, annotationUtil, filterUtil);
080                            JOptionPane.showMessageDialog(parent, "The annotator slot for each annotation satisfying the filter '"
081                                            + filter.getBrowserText() + "' has been set to '" + annotator.getBrowserText() + "'");
082    
083                    }
084            }
085    
086            /**
087             * This method sets the annotator slot value for a set of annotations. This
088             * method simply retrieves all annotations from the annotationUtil and
089             * filters them using the supplied filter. The annotator slot value for each
090             * annotation that passes the filter is updated with the first parameter.
091             * 
092             * @param annotator
093             *            the value of the knowtator_annotation_annotator slot for the
094             *            annotations to be changed
095             * @param kpu
096             *            provides the name of the slot that is being changed via
097             *            getAnnotationAnnotatorSlot()
098             * @param filter
099             *            an instance of 'knowtator filter' that will be used to select
100             *            the annotations that will be modified.
101             * @param annotationUtil
102             *            provides the annotations in the current knowledgebase via the
103             *            retrieveAllAnnotations method
104             * @param filterUtil
105             *            provides the filter method which selects only the annotations
106             *            that satisfy the filter.
107             */
108    
109            public static void batchAnnotatorChange(SimpleInstance annotator, KnowtatorProjectUtil kpu, SimpleInstance filter,
110                            AnnotationUtil annotationUtil, FilterUtil filterUtil) {
111                    java.util.List<SimpleInstance> annotations = annotationUtil.retrieveAllAnnotations();
112                    Collection<SimpleInstance> filteredAnnotations = filterUtil.filterAnnotations(annotations, filter);
113    
114                    for (SimpleInstance annotation : filteredAnnotations) {
115                            annotation.setDirectOwnSlotValue(kpu.getAnnotationAnnotatorSlot(), annotator);
116                    }
117            }
118    
119            public static void batchAnnotationSetChange(Component parent, KnowtatorProjectUtil kpu,
120                            AnnotationUtil annotationUtil, FilterUtil filterUtil) {
121                    int option = JOptionPane.showConfirmDialog(parent,
122                                    "The following dialogs provide a way to assign the annotation set for a large number of \n"
123                                                    + "annotations.  The next dialog will ask you to choose an annotation set.  The value\n"
124                                                    + "you select will be added to the 'annotation set' slot.  The \n"
125                                                    + "following dialog will ask you to choose an annotation filter.  The value\n "
126                                                    + "you select will be used to decide which annotations will be updated",
127                                    "Change annotator for annotations", JOptionPane.OK_CANCEL_OPTION);
128                    if (option == JOptionPane.OK_OPTION) {
129                            SimpleInstance annotationSet = (SimpleInstance) DisplayUtilities.pickInstance(parent, CollectionUtilities
130                                            .createCollection(kpu.getSetCls()), "Choose annotation set");
131                            if (annotationSet == null)
132                                    return;
133                            SimpleInstance filter = (SimpleInstance) DisplayUtilities.pickInstance(parent, CollectionUtilities
134                                            .createCollection(kpu.getFilterCls()), "Choose an annotation filter");
135                            if (filter == null)
136                                    return;
137                            batchAnnotationSetChange(annotationSet, kpu, filter, annotationUtil, filterUtil);
138                            JOptionPane.showMessageDialog(parent,
139                                            "The 'annotation set' slot for each annotation satisfying the filter '" + filter.getBrowserText()
140                                                            + "' has been set to '" + annotationSet.getBrowserText() + "'");
141    
142                    }
143            }
144    
145            /**
146             * This method sets the annotator slot value for a set of annotations. This
147             * method simply retrieves all annotations from the annotationUtil and
148             * filters them using the supplied filter. The annotator slot value for each
149             * annotation that passes the filter is updated with the first parameter.
150             * 
151             * @param annotationSet
152             *            the value of the knowtator_annotation_set slot for the
153             *            annotations to be changed
154             * @param kpu
155             *            provides the name of the slot that is being changed via
156             *            getAnnotationAnnotatorSlot()
157             * @param filter
158             *            an instance of 'knowtator filter' that will be used to select
159             *            the annotations that will be modified.
160             * @param annotationUtil
161             *            provides the annotations in the current knowledgebase via the
162             *            retrieveAllAnnotations method
163             * @param filterUtil
164             *            provides the filter method which selects only the annotations
165             *            that satisfy the filter.
166             */
167    
168            public static void batchAnnotationSetChange(SimpleInstance annotationSet, KnowtatorProjectUtil kpu,
169                            SimpleInstance filter, AnnotationUtil annotationUtil, FilterUtil filterUtil) {
170                    java.util.List<SimpleInstance> annotations = annotationUtil.retrieveAllAnnotations();
171                    Collection<SimpleInstance> filteredAnnotations = filterUtil.filterAnnotations(annotations, filter);
172    
173                    for (SimpleInstance annotation : filteredAnnotations) {
174                            annotation.setDirectOwnSlotValue(kpu.getSetSlot(), annotationSet);
175                    }
176            }
177    
178            public static boolean isTeamAnnotator(SimpleInstance annotator) {
179                    KnowledgeBase kb = annotator.getKnowledgeBase();
180                    Cls type = annotator.getDirectType();
181                    if (type.equals(kb.getCls(KnowtatorProjectUtil.ANNOTATOR_TEAM_CLS_NAME)))
182                            return true;
183                    return false;
184    
185            }
186    
187    }
188    
189    // /**
190    // * This method finds annotations created by oldAnnotator and sets the
191    // annotator
192    // * slot to newAnnotator.
193    // * @param kb the knowledge base the annotations reside in
194    // * @param oldAnnotator the annotator that is being replaced. If null then
195    // * method is equivalent to replaceAnnotator(KnowledgeBase kb, Instance
196    // annotator)
197    // * @param newAnnotator the annotations created by oldAnnotator will have the
198    // * annotator slot set to newAnnotator.
199    // */
200    //    
201    // public static void replaceAnnotator(KnowledgeBase kb,
202    // Instance oldAnnotator,
203    // Instance newAnnotator)
204    // {
205    // Cls annotationCls = kb.getCls(KnowtatorProjectUtil.ANNOTATION_CLS_NAME);
206    // Slot annotatorSlot =
207    // kb.getSlot(KnowtatorProjectUtil.ANNOTATION_ANNOTATOR_SLOT_NAME);
208    // Collection<Instance> annotations = kb.getInstances(annotationCls);
209    // for(Instance annotation : annotations)
210    // {
211    // if(oldAnnotator == null)
212    // {
213    // annotation.setOwnSlotValue(annotatorSlot, newAnnotator);
214    // }
215    // else
216    // {
217    // Instance currentAnnotator = (Instance)
218    // annotation.getOwnSlotValue(annotatorSlot);
219    // if(currentAnnotator != null && currentAnnotator.equals(oldAnnotator))
220    // {
221    // annotation.setOwnSlotValue(annotatorSlot, newAnnotator);
222    // }
223    // }
224    // }
225    // }
226    //    
227    // /**
228    // * This method finds annotations that have no value in the annotator slot and
229    // * sets the annotator value to annotator.
230    // * @param kb the knowledge base the annotations reside in
231    // * @param annotator all annotations that have no value in the annotator slot
232    // will
233    // * have their annotator slot value set to annotator.
234    // */
235    //    
236    // public static void setEmptyAnnotator(KnowledgeBase kb,
237    // Instance annotator)
238    // {
239    // Cls annotationCls = kb.getCls(KnowtatorProjectUtil.ANNOTATION_CLS_NAME);
240    // Slot annotatorSlot =
241    // kb.getSlot(KnowtatorProjectUtil.ANNOTATION_ANNOTATOR_SLOT_NAME);
242    // Collection<Instance> annotations = kb.getInstances(annotationCls);
243    // for(Instance annotation : annotations)
244    // {
245    // Instance currentAnnotator = (Instance)
246    // annotation.getOwnSlotValue(annotatorSlot);
247    // if(currentAnnotator == null)
248    // {
249    // annotation.setOwnSlotValue(annotatorSlot, annotator);
250    // }
251    // }
252    // }
253    // /**
254    // * This method sets the annotator for all annotations in the knowledge base.
255    // * @param kb the knowledge base the annotations reside in
256    // * @param annotator all annotations will have there annotator slot set to the
257    // * instance passed in this parameter.
258    // *
259    // */
260    // public static void replaceAnnotator(KnowledgeBase kb,
261    // Instance annotator)
262    // {
263    // replaceAnnotator(kb, null, annotator);
264    // }
265    //    
266