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.knowtator.util;
029    
030    import java.awt.Component;
031    import java.awt.Cursor;
032    import java.io.File;
033    import java.util.ArrayList;
034    import java.util.Collection;
035    import java.util.HashMap;
036    import java.util.HashSet;
037    import java.util.Map;
038    import java.util.Set;
039    
040    import javax.swing.JFileChooser;
041    import javax.swing.JOptionPane;
042    import javax.swing.ProgressMonitor;
043    
044    import edu.stanford.smi.protege.model.Cls;
045    import edu.stanford.smi.protege.model.Instance;
046    import edu.stanford.smi.protege.model.KnowledgeBase;
047    import edu.stanford.smi.protege.model.Project;
048    import edu.stanford.smi.protege.model.SimpleInstance;
049    import edu.stanford.smi.protege.model.Slot;
050    import edu.stanford.smi.protege.ui.DisplayUtilities;
051    import edu.stanford.smi.protege.util.CollectionUtilities;
052    import edu.stanford.smi.protege.util.ExtensionFilter;
053    import edu.uchsc.ccp.knowtator.AnnotationUtil;
054    import edu.uchsc.ccp.knowtator.FilterUtil;
055    import edu.uchsc.ccp.knowtator.KnowtatorManager;
056    import edu.uchsc.ccp.knowtator.KnowtatorProjectUtil;
057    import edu.uchsc.ccp.knowtator.MentionUtil;
058    import edu.uchsc.ccp.knowtator.Span;
059    import edu.uchsc.ccp.knowtator.TextSourceUtil;
060    import edu.uchsc.ccp.knowtator.textsource.TextSource;
061    import edu.uchsc.ccp.knowtator.textsource.TextSourceAccessException;
062    
063    public class MergeAnnotations {
064    
065            Project addedProject;
066    
067            Project masterProject;
068    
069            TextSourceUtil masterTextSourceUtil;
070    
071            Component parentComponent;
072    
073            KnowledgeBase masterKB;
074    
075            KnowtatorManager masterManager;
076    
077            KnowtatorProjectUtil masterKPU;
078    
079            AnnotationUtil masterAnnotationUtil;
080    
081            MentionUtil masterMentionUtil;
082    
083            KnowledgeBase addedKB;
084    
085            KnowtatorManager addedManager;
086    
087            KnowtatorProjectUtil addedKPU;
088    
089            AnnotationUtil addedAnnotationUtil;
090    
091            MentionUtil addedMentionUtil;
092    
093            FilterUtil addedFilterUtil;
094    
095            Map<SimpleInstance, SimpleInstance> annotatorMap;
096    
097            Map<SimpleInstance, SimpleInstance> setsMap;
098    
099            Set<SimpleInstance> addedAnnotations;
100    
101            Set<SimpleInstance> addedMentions;
102    
103            Map<SimpleInstance, SimpleInstance> annotationsMap;
104    
105            Map<SimpleInstance, SimpleInstance> mentionsMap;
106    
107            /**
108             * This class facilitates the merging of an annotation project with a
109             * 'master' annotation project.
110             * 
111             * @param addedProject
112             *            - the project that will be added to the master project. This
113             *            project will not be modified. The .pont for this project
114             *            should be identical to that of the master project.
115             * @param masterProject
116             *            - the project that the added project will be added to. This
117             *            project will be modified - all annotations from the added
118             *            project will be added. However, the project will not be saved
119             *            - so changes can be discarded if desired.
120             */
121    
122            public MergeAnnotations(Project addedProject, Project masterProject, TextSourceUtil masterTextSourceUtil,
123                            Component parentComponent) throws TextSourceAccessException {
124                    this.addedProject = addedProject;
125                    this.masterProject = masterProject;
126                    this.masterTextSourceUtil = masterTextSourceUtil;
127                    this.parentComponent = parentComponent;
128    
129                    masterKB = masterProject.getKnowledgeBase();
130                    masterKPU = new KnowtatorProjectUtil(masterKB);
131                    masterManager = new KnowtatorManager(masterKPU);
132                    masterAnnotationUtil = masterManager.getAnnotationUtil();
133                    masterAnnotationUtil.setTextSourceUtil(masterTextSourceUtil);
134                    masterMentionUtil = masterManager.getMentionUtil();
135    
136                    addedKB = addedProject.getKnowledgeBase();
137                    addedKPU = new KnowtatorProjectUtil(addedKB);
138                    addedManager = new KnowtatorManager(addedKPU);
139                    addedAnnotationUtil = addedManager.getAnnotationUtil();
140                    addedMentionUtil = addedManager.getMentionUtil();
141                    addedFilterUtil = addedManager.getFilterUtil();
142    
143                    annotatorMap = new HashMap<SimpleInstance, SimpleInstance>();
144                    setsMap = new HashMap<SimpleInstance, SimpleInstance>();
145                    addedAnnotations = new HashSet<SimpleInstance>();
146                    addedMentions = new HashSet<SimpleInstance>();
147                    annotationsMap = new HashMap<SimpleInstance, SimpleInstance>();
148                    mentionsMap = new HashMap<SimpleInstance, SimpleInstance>();
149    
150            }
151    
152            /*
153             * To do: - I do not worry about subclasses of annotation. If we allow
154             * annotation to be subclassed and have slot values that point to other slot
155             * annotations or any other slot values, then we will need to expand the
156             * code here.
157             */
158    
159            public void mergeAnnotations(SimpleInstance addedFilter, Collection<SimpleInstance> addedTextSources)
160                            throws Exception {
161    
162                    annotatorMap.clear();
163                    setsMap.clear();
164                    addedAnnotations.clear();
165                    addedMentions.clear();
166                    annotationsMap.clear();
167                    mentionsMap.clear();
168    
169                    // collect the annotations and mentions from the added project that are
170                    // going to be added to the master project
171                    for (SimpleInstance addedTextSource : addedTextSources) {
172                            Collection<SimpleInstance> tsAnnotations = addedAnnotationUtil.getAnnotations(addedTextSource);
173                            tsAnnotations = addedFilterUtil.filterAnnotations(tsAnnotations, addedFilter);
174                            for (SimpleInstance tsAnnotation : tsAnnotations) {
175                                    addedAnnotations.add(tsAnnotation);
176    
177                                    SimpleInstance mention = addedAnnotationUtil.getMention(tsAnnotation);
178                                    addedMentions.add(mention);
179                                    Set<SimpleInstance> connectedMentions = addedMentionUtil.getAllConnectedMentions(mention);
180                                    addedMentions.addAll(connectedMentions);
181                                    for (SimpleInstance connectedMention : connectedMentions) {
182                                            SimpleInstance connectedAnnotation = addedMentionUtil.getMentionAnnotation(connectedMention);
183                                            if (connectedAnnotation != null)
184                                                    addedAnnotations.add(connectedAnnotation);
185                                    }
186                            }
187                    }
188    
189                    System.out.println("adding " + addedAnnotations.size() + " annotations:");
190    
191                    int npes = 0;
192    
193                    ProgressMonitor progressMonitor = new ProgressMonitor(parentComponent, "Merging annotations", null, 0,
194                                    addedAnnotations.size() + addedMentions.size());
195    
196                    try {
197    
198                            int progress = 0;
199    
200                            for (SimpleInstance addedAnnotation : addedAnnotations) {
201                                    progressMonitor.setProgress(progress++);
202    
203                                    if (progressMonitor.isCanceled())
204                                            throw new Exception("Merge cancelled by user.");
205    
206                                    // get the spans of the new annotation
207                                    java.util.List<Span> spans = addedAnnotationUtil.getSpans(addedAnnotation);
208                                    // get the spanned text of the new annotation
209                                    String spannedText = addedAnnotationUtil.getText(addedAnnotation);
210                                    // we want the text source instance in the master kb for the new
211                                    // annotation - not the one from the added kb
212                                    Slot addedTSSlot = addedKPU.getAnnotationTextSourceSlot();
213                                    Instance addedTS = (Instance) addedAnnotation.getOwnSlotValue(addedTSSlot);
214                                    String addedTextSourceName = addedTS.getName();
215    
216                                    SimpleInstance masterTextSourceInstance = masterKB.getSimpleInstance(addedTextSourceName);
217                                    if (masterTextSourceInstance == null) {
218                                            TextSource masterTextSource = masterTextSourceUtil.getCurrentTextSourceCollection().get(
219                                                            addedTextSourceName);
220                                            masterTextSourceInstance = masterTextSourceUtil.getTextSourceInstance(masterTextSource, true);
221                                    }
222    
223                                    SimpleInstance addedAnnotator = (SimpleInstance) (addedAnnotation.getOwnSlotValue(addedKPU
224                                                    .getAnnotationAnnotatorSlot()));
225                                    SimpleInstance masterAnnotator = getMasterAnnotator(addedAnnotator);
226    
227                                    Collection<SimpleInstance> masterSets = getMasterSets(addedAnnotation);
228    
229                                    SimpleInstance masterAnnotation = masterAnnotationUtil.createAnnotation(null, masterAnnotator, spans,
230                                                    spannedText, masterTextSourceInstance, masterSets);
231    
232                                    String comment = addedAnnotationUtil.getComment(addedAnnotation);
233                                    if (comment != null) {
234                                            masterAnnotationUtil.setComment(masterAnnotation, comment);
235                                    }
236                                    annotationsMap.put(addedAnnotation, masterAnnotation);
237                            }
238                            System.out.println("Number of NullPointerExceptions for new annotations = " + npes);
239    
240                            System.out.println("adding " + addedMentions.size() + " mentions:");
241    
242                            for (SimpleInstance addedMention : addedMentions) {
243                                    System.out.print(".");
244                                    System.out.flush();
245                                    if (progress % 30 == 0)
246                                            System.out.println("");
247                                    progressMonitor.setProgress(progress++);
248                                    addMention(addedMention, addedKPU, masterKPU, addedMentionUtil, annotationsMap, mentionsMap,
249                                                    masterMentionUtil, masterKB);
250                            }
251    
252                            progressMonitor.close();
253    
254                    } catch (Exception exception) {
255                            progressMonitor.close();
256                            throw exception;
257                    }
258            }
259    
260            /**
261             * This method adds a mention from added project to the master project.
262             * 
263             * @param addedMention
264             *            an instance of a mention from the added project
265             * @param addedKPU
266             *            a kpu initialized with the knowledge base from the added
267             *            project
268             * @param masterKPU
269             *            a kpu initialized with the knowledge base from the master
270             *            project
271             * @param addedMentionUtil
272             *            a MentionUtil initialized with addedKPU
273             * @param annotationsMap
274             *            keys are added annotations and values are master annotations.
275             *            Each annotation in the added project is copied and added to
276             *            the master project. An added annotation is an instance in the
277             *            added project. A master annotation is an instance in the
278             *            master project.
279             * @param mentionsMap
280             *            keys are added mentions and values are master mentions. Each
281             *            mention in the added project is copied and added to the master
282             *            project. An added mention is an instance in the added project.
283             *            A master mention is an instance in the master project.
284             * @param masterMentionUtil
285             *            a MentionUtil initialized with masterKPU
286             * @param masterKB
287             *            the knowledge base of the master project
288             */
289    
290            private static SimpleInstance addMention(SimpleInstance addedMention, KnowtatorProjectUtil addedKPU,
291                            KnowtatorProjectUtil masterKPU, MentionUtil addedMentionUtil,
292                            Map<SimpleInstance, SimpleInstance> annotationsMap, Map<SimpleInstance, SimpleInstance> mentionsMap,
293                            MentionUtil masterMentionUtil, KnowledgeBase masterKB) {
294                    if (!mentionsMap.containsKey(addedMention)) {
295                            SimpleInstance masterMention = null;
296    
297                            if (addedMentionUtil.isClassMention(addedMention)) {
298                                    Cls addedCls = addedMentionUtil.getMentionCls(addedMention);
299                                    String addedClsName = addedCls.getName();
300                                    Cls masterCls = masterKB.getCls(addedClsName);
301                                    masterMention = masterMentionUtil.createClassMention(masterCls);
302                                    mentionsMap.put(addedMention, masterMention);
303                                    Collection<SimpleInstance> addedSlotMentions = ProtegeUtil.castSimpleInstances(addedMention
304                                                    .getOwnSlotValues(addedKPU.getSlotMentionSlot()));
305                                    for (SimpleInstance addedSlotMention : addedSlotMentions) {
306                                            SimpleInstance masterSlotMention = addMention(addedSlotMention, addedKPU, masterKPU,
307                                                            addedMentionUtil, annotationsMap, mentionsMap, masterMentionUtil, masterKB);
308                                            if (masterSlotMention != null)
309                                                    masterMention.addOwnSlotValue(masterKPU.getSlotMentionSlot(), masterSlotMention);
310                                    }
311                            } else if (addedMentionUtil.isInstanceMention(addedMention)) {
312                                    String addedInstanceName = addedMentionUtil.getMentionInstance(addedMention).getName();
313                                    SimpleInstance masterInstance = (SimpleInstance) masterKB.getInstance(addedInstanceName);
314                                    masterMention = masterMentionUtil.createInstanceMention(masterInstance);
315                                    mentionsMap.put(addedMention, masterMention);
316                                    Collection<SimpleInstance> addedSlotMentions = ProtegeUtil.castSimpleInstances(addedMention
317                                                    .getOwnSlotValues(addedKPU.getSlotMentionSlot()));
318                                    for (SimpleInstance addedSlotMention : addedSlotMentions) {
319                                            SimpleInstance masterSlotMention = addMention(addedSlotMention, addedKPU, masterKPU,
320                                                            addedMentionUtil, annotationsMap, mentionsMap, masterMentionUtil, masterKB);
321                                            masterMention.addOwnSlotValue(masterKPU.getSlotMentionSlot(), masterSlotMention);
322                                    }
323                            } else if (addedMentionUtil.isSlotMention(addedMention)) {
324                                    Slot addedSlot = addedMentionUtil.getSlotMentionSlot(addedMention);
325                                    if (addedSlot == null) {
326                                            System.out.println("WARNING: slot mention has no slot specified.  It will not be added.");
327                                            return null;
328                                    }
329                                    String addedSlotName = addedMentionUtil.getSlotMentionSlot(addedMention).getName();
330                                    Slot masterSlot = masterKB.getSlot(addedSlotName);
331                                    masterMention = masterMentionUtil.createSlotMention(masterSlot);
332                                    mentionsMap.put(addedMention, masterMention);
333                                    Collection addedValues = addedMention.getOwnSlotValues(addedKPU.getMentionSlotValueSlot());
334    
335                                    if (addedValues != null && addedValues.size() > 0) {
336                                            Object value = CollectionUtilities.getFirstItem(addedValues);
337                                            if (value instanceof SimpleInstance) {
338                                                    for (Object addedValue : addedValues) {
339                                                            SimpleInstance addedInstance = (SimpleInstance) addedValue;
340                                                            SimpleInstance masterInstance = mentionsMap.get(addedInstance);
341                                                            if (masterInstance == null) {
342                                                                    masterInstance = addMention(addedInstance, addedKPU, masterKPU, addedMentionUtil,
343                                                                                    annotationsMap, mentionsMap, masterMentionUtil, masterKB);
344                                                            }
345                                                            masterMention.addOwnSlotValue(masterKPU.getMentionSlotValueSlot(), masterInstance);
346                                                    }
347                                            } else {
348                                                    masterMention.setOwnSlotValues(masterKPU.getMentionSlotValueSlot(), addedValues);
349                                            }
350                                    }
351                            }
352                            if (masterMention != null) {
353                                    Collection<SimpleInstance> mentionAnnotations = ProtegeUtil.castSimpleInstances(addedMention
354                                                    .getOwnSlotValues(addedKPU.getMentionAnnotationSlot()));
355                                    if (mentionAnnotations != null) {
356                                            for (SimpleInstance mentionAnnotation : mentionAnnotations) {
357                                                    SimpleInstance masterAnnotation = annotationsMap.get(mentionAnnotation);
358                                                    masterMention.addOwnSlotValue(masterKPU.getMentionAnnotationSlot(), masterAnnotation);
359                                            }
360                                    }
361                                    return masterMention;
362                            }
363                    } else {
364                            return mentionsMap.get(addedMention);
365                    }
366                    return null;
367            }
368    
369            /**
370             * Does not handle 'knowtator consensus set' slot values
371             * 
372             * @param addedAnnotation
373             * @return
374             */
375            private Collection<SimpleInstance> getMasterSets(SimpleInstance addedAnnotation) {
376                    Set<SimpleInstance> addedSets = addedAnnotationUtil.getSets(addedAnnotation);
377                    Collection<SimpleInstance> masterSets = new ArrayList<SimpleInstance>();
378                    SETS_LOOP: for (SimpleInstance addedSet : addedSets) {
379                            // if there is already a mapping from the added set to the master
380                            // set then use it
381                            if (setsMap.containsKey(addedSet)) {
382                                    masterSets.add(setsMap.get(addedSet));
383                                    continue;
384                            }
385    
386                            // //if the same instance exists in both projects (i.e. they have
387                            // the same system id) then use it
388                            // String setName = addedSet.getName();
389                            // SimpleInstance masterSet = masterKB.getSimpleInstance(setName);
390                            // if(masterSet != null)
391                            // {
392                            // masterSets.add(masterSet);
393                            // setsMap.put(addedSet, masterSet);
394                            // continue;
395                            // }
396    
397                            // try to find a set in the master project that has the same slot
398                            // value for knowtator_set_name
399                            String name = (String) addedSet.getOwnSlotValue(addedKPU.getSetNameSlot());
400                            if (name != null) {
401                                    Collection<SimpleInstance> allMasterSets = ProtegeUtil.castSimpleInstances(masterKB
402                                                    .getInstances(masterKPU.getSetCls()));
403                                    for (SimpleInstance mstrSet : allMasterSets) {
404                                            String masterName = (String) mstrSet.getOwnSlotValue(masterKPU.getSetNameSlot());
405                                            if (masterName != null && masterName.equals(name)) {
406                                                    masterSets.add(mstrSet);
407                                                    setsMap.put(addedSet, mstrSet);
408                                                    continue SETS_LOOP;
409                                            }
410                                    }
411                            }
412    
413                            // create the set in the master project if it does not exist
414                            String description = (String) addedSet.getOwnSlotValue(addedKPU.getSetDescriptionSlot());
415                            SimpleInstance masterSet = (SimpleInstance) masterKB.createInstance(null, masterKPU.getSetCls());
416                            if (name != null)
417                                    masterSet.setOwnSlotValue(masterKPU.getSetNameSlot(), name);
418                            if (description != null)
419                                    masterSet.setOwnSlotValue(masterKPU.getSetDescriptionSlot(), description);
420                            masterSets.add(masterSet);
421                            setsMap.put(addedSet, masterSet);
422                    }
423                    return masterSets;
424            }
425    
426            private SimpleInstance getMasterAnnotator(SimpleInstance addedAnnotator) {
427                    if (annotatorMap.containsKey(addedAnnotator))
428                            return annotatorMap.get(addedAnnotator);
429    
430                    if (addedAnnotator != null) {
431                            String addedAnnotatorName = addedAnnotator.getName();
432                            System.out.println("addedAnnotatorName=" + addedAnnotatorName);
433                            System.out.println("frame id = " + addedAnnotator.getFrameID().toString());
434    
435                            SimpleInstance masterAnnotator = (SimpleInstance) masterKB.getInstance(addedAnnotatorName);
436    
437                            if (masterAnnotator != null && masterAnnotator.hasType(masterKPU.getAnnotatorCls())) {
438                                    annotatorMap.put(addedAnnotator, masterAnnotator);
439                                    return masterAnnotator;
440                            } else {
441                                    if (addedAnnotator.hasType(addedKB.getCls("knowtator human annotator"))) {
442                                            String affiliation = (String) addedAnnotator.getOwnSlotValue(addedKB
443                                                            .getSlot("knowtator_annotation_annotator_affiliation"));
444                                            String firstName = (String) addedAnnotator.getOwnSlotValue(addedKB
445                                                            .getSlot("knowtator_annotation_annotator_firstname"));
446                                            String lastName = (String) addedAnnotator.getOwnSlotValue(addedKB
447                                                            .getSlot("knowtator_annotation_annotator_lastname"));
448    
449                                            Cls masterAnnotatorCls = masterKB.getCls("knowtator human annotator");
450                                            masterAnnotator = (SimpleInstance) masterKB.createInstance(null, masterAnnotatorCls);
451                                            masterAnnotator.setOwnSlotValue(masterKB.getSlot("knowtator_annotation_annotator_affiliation"),
452                                                            affiliation);
453                                            masterAnnotator.setOwnSlotValue(masterKB.getSlot("knowtator_annotation_annotator_firstname"),
454                                                            firstName);
455                                            masterAnnotator.setOwnSlotValue(masterKB.getSlot("knowtator_annotation_annotator_lastname"),
456                                                            lastName);
457    
458                                            annotatorMap.put(addedAnnotator, masterAnnotator);
459                                            return masterAnnotator;
460                                    } else if (addedAnnotator.hasType(addedKB.getCls("knowtator annotator team"))) {
461                                            String teamName = (String) addedAnnotator.getOwnSlotValue(addedKB
462                                                            .getSlot("knowtator_annotator_team_name"));
463                                            Collection addedTeamMembers = addedAnnotator.getOwnSlotValues(addedKB
464                                                            .getSlot("knowtator_annotator_team_members"));
465                                            Collection<SimpleInstance> masterTeamMembers = new ArrayList<SimpleInstance>();
466                                            for (Object teamMember : addedTeamMembers) {
467                                                    masterTeamMembers.add(getMasterAnnotator((SimpleInstance) teamMember));
468                                            }
469    
470                                            Cls masterAnnotatorCls = masterKB.getCls("knowtator annotator team");
471                                            masterAnnotator = (SimpleInstance) masterKB.createInstance(null, masterAnnotatorCls);
472                                            masterAnnotator.setOwnSlotValue(masterKB.getSlot("knowtator_annotator_team_name"), teamName);
473                                            masterAnnotator.setOwnSlotValues(masterKB.getSlot("knowtator_annotator_team_members"),
474                                                            masterTeamMembers);
475                                            annotatorMap.put(addedAnnotator, masterAnnotator);
476                                            return masterAnnotator;
477                                    }
478                            }
479                    }
480                    return null;
481            }
482    
483            public static void mergeAnnotations(Component parent, Project masterProject, TextSourceUtil masterTextSourceUtil) {
484                    int option = JOptionPane
485                                    .showConfirmDialog(
486                                                    parent,
487                                                    "Select 'OK' to proceed with merging annotation projects.\n"
488                                                                    + "Please make an archive of your Protege project and consult available documentation before using this feature.\n"
489                                                                    + "Loading and merging the annotations from the Protege project may take some time.\n"
490                                                                    + "Please be patient!", "Merge annotation projects", JOptionPane.OK_CANCEL_OPTION);
491                    if (option == JOptionPane.OK_OPTION) {
492                            JFileChooser chooser = new JFileChooser();
493                            chooser.setFileFilter(new ExtensionFilter(".pprj", "Protege projects"));
494                            chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
495                            int returnVal = chooser.showOpenDialog(parent);
496                            if (returnVal == JFileChooser.APPROVE_OPTION) {
497                                    File file = chooser.getSelectedFile();
498                                    ArrayList errors = new ArrayList();
499                                    System.out.println("file.getPath()= " + file.getPath());
500                                    parent.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
501                                    Project addedProject = new Project(file.getPath(), errors);
502                                    parent.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
503                                    option = JOptionPane.showConfirmDialog(parent, "Project loaded into memory.  Proceed with merging?",
504                                                    "Proceed?", JOptionPane.OK_CANCEL_OPTION);
505    
506                                    if (option == JOptionPane.OK_OPTION) {
507                                            try {
508                                                    MergeAnnotations merge = new MergeAnnotations(addedProject, masterProject,
509                                                                    masterTextSourceUtil, parent);
510    
511                                                    SimpleInstance addedFilter = (SimpleInstance) DisplayUtilities.pickInstance(parent,
512                                                                    CollectionUtilities.createCollection(addedProject.getKnowledgeBase().getCls(
513                                                                                    KnowtatorProjectUtil.FILTER_CLS_NAME)),
514                                                                    "Select filter for annotations to add from selected project.");
515                                                    if (addedFilter == null)
516                                                            return;
517    
518                                                    Collection<SimpleInstance> textSources = ProtegeUtil.castSimpleInstances(DisplayUtilities
519                                                                    .pickInstances(parent, addedProject.getKnowledgeBase(), CollectionUtilities
520                                                                                    .createCollection(addedProject.getKnowledgeBase().getCls(
521                                                                                                    KnowtatorProjectUtil.TEXT_SOURCE_CLS_NAME)),
522                                                                                    "Select text sources that contain annotations you want to add."));
523                                                    if (textSources == null || textSources.size() == 0)
524                                                            return;
525    
526                                                    merge.mergeAnnotations(addedFilter, textSources);
527    
528                                                    JOptionPane
529                                                                    .showMessageDialog(
530                                                                                    parent,
531                                                                                    "Merge completed without error.  To save merged annotations, save the Protege project.",
532                                                                                    "Merge Completion", JOptionPane.INFORMATION_MESSAGE);
533                                            } catch (Exception tsae) {
534                                                    tsae.printStackTrace();
535                                                    JOptionPane.showMessageDialog(parent,
536                                                                    "Merge caused an exception to be thrown.  Please discard changes caused by aborted merge\n"
537                                                                                    + "by closing the project without saving changes.\n" + tsae.getMessage());
538                                            }
539                                    }
540                            }
541                    }
542            }
543    }
544    
545    // public static void main(String[] args)
546    // {
547    // public static void mergeAnnotations(Project addedProject,
548    // Project masterProject,
549    // TextSourceUtil textSourceUtil)
550    // throws Exception
551    //    
552    // String addedProjectFileName = args[0];
553    // String masterProjectFileName = args[0];
554    // String grifLinesTSCFileName = args[1];
555    // String filterInstanceName = args[2];
556    // String log4jConfigurationFileName = args[3];
557    //            
558    // PropertyConfigurator.configure(log4jConfigurationFileName);
559    //            
560    //
561    // // GRIFFileLinesTextSourceCollection tsc = new
562    // GRIFFileLinesTextSourceCollection(grifLinesTSCFileName);
563    // GRIFTextSourceCollection tsc = new
564    // GRIFTextSourceCollection(grifLinesTSCFileName);
565    //
566    // ArrayList errors = new ArrayList();
567    // Project project = new Project(protegeProjectFileName, errors);
568    // if(errors.isEmpty())
569    // {
570    // System.out.println("Protege project loaded without error.");
571    // }
572    // else
573    // {
574    // System.out.println("Protege project loaded with errors.");
575    // for(int i=0; i<errors.size();i++)
576    // {
577    // System.out.println("errors.get(i)="+errors.get(i));
578    // }
579    // }
580    //
581    // }
582    
583    // Instance masterAnnotationSet = null;
584    // Instance addedAnnotationSet =
585    // (Instance)(addedAnnotation.getOwnSlotValue(addedKPU.getSetSlot()));
586    // if(addedAnnotationSet != null)
587    // {
588    // String addedAnnotationSetName = addedAnnotationSet.getName();
589    // masterAnnotationSet = masterKB.getInstance(addedAnnotationSetName);
590    // }
591    //        
592    // SimpleInstance masterAnnotation = masterAnnotationUtil.createAnnotation(
593    // null,
594    // masterAnnotator,
595    // spans,
596    // masterTextSourceInstance,
597    // masterAnnotationSet);
598