001    /**
002     * Copyright (C) 2007-2009, Jens Lehmann
003     *
004     * This file is part of DL-Learner.
005     * 
006     * DL-Learner is free software; you can redistribute it and/or modify
007     * it under the terms of the GNU General Public License as published by
008     * the Free Software Foundation; either version 3 of the License, or
009     * (at your option) any later version.
010     *
011     * DL-Learner is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014     * GNU General Public License for more details.
015     *
016     * You should have received a copy of the GNU General Public License
017     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018     *
019     */
020    package org.dllearner.tools.protege;
021    
022    import java.awt.Color;
023    import java.util.HashSet;
024    import java.util.List;
025    import java.util.Set;
026    import java.util.SortedSet;
027    
028    import org.dllearner.core.owl.Individual;
029    import org.dllearner.core.owl.NamedClass;
030    import org.dllearner.reasoning.FastInstanceChecker;
031    import org.protege.editor.owl.OWLEditorKit;
032    import org.semanticweb.owl.model.OWLClass;
033    import org.semanticweb.owl.model.OWLOntology;
034    
035    /**
036     * This class reads the ontologie in a separate thread.
037     * @author Christian Koetteritzsch
038     *
039     */
040    public class ReadingOntologyThread extends Thread {
041    
042            
043            private boolean hasIndividuals;
044            private FastInstanceChecker reasoner;
045            private NamedClass currentConcept;
046            private Set<Individual> individual;
047            private Set<String> ontologieURI;
048            private final OWLEditorKit editor;
049            private DLLearnerModel model;
050            private boolean isInconsistent;
051            private OWLClass current;
052            private DLLearnerView view;
053            
054            /**
055             * This is the constructor of the ReadingOntologyThread.
056             * @param editorKit OWLEditorKit
057             * @param frame OWLFrame
058             * @param v DL-Learner view
059             * @param m DL-Learner model
060             */
061            public ReadingOntologyThread(OWLEditorKit editorKit, DLLearnerView v, DLLearnerModel m) {
062                    this.editor = editorKit;
063                    this.view = v;
064                    this.model = m;
065            }
066            
067            public void setDLLearnerView(DLLearnerView v) {
068                    this.view = v;
069            }
070            
071            public void setDLLearnerModel(DLLearnerModel m) {
072                    this.model = m;
073            }
074            /**
075             * This method sets the individuals that belong to the concept which is
076             * chosen in protege.
077             */
078            private void setPositiveConcept() {
079                    current =  editor.getOWLWorkspace().getOWLSelectionModel().getLastSelectedClass();
080                    if(current != null) {
081                            SortedSet<Individual> individuals = null;
082                            hasIndividuals = false;
083                            // checks if selected concept is thing when yes then it selects all
084                            // individuals
085                            if (!(current.toString().equals("Thing"))) {
086                                    List<NamedClass> classList = reasoner.getAtomicConceptsList();
087                                    for(NamedClass concept : classList) {
088                                            // if individuals is null
089                                            if (individuals == null) {
090                                                    // checks if the concept is the selected concept in protege
091                                                    for(String onto : ontologieURI) {
092                                                            if (concept.toString().contains(onto)) {
093                                                                    if (concept.toString().equals(
094                                                                                    onto + current.toString())) {
095                                                                            // if individuals is not null it gets all
096                                                                            // individuals of
097                                                                            // the concept
098                                                                            currentConcept = concept;
099                                                                            if (reasoner.getIndividuals(concept) != null) {
100                                                                                    if (reasoner.getIndividuals(concept).size() > 0) {
101                                                                                            model.setInstancesCount(reasoner.getIndividuals(concept).size());
102                                                                                            hasIndividuals = true;
103                                                                                    }
104                                                                                    individual = reasoner.getIndividuals(concept);
105                                                                                    model.setIndividuals(individual);
106                                                                                    model.setHasIndividuals(hasIndividuals);
107                                                                                    System.out.println("current: " + currentConcept);
108                                                                                    model.setCurrentConcept(currentConcept);
109                                                                                    view.getRunButton().setEnabled(true);
110                                                                                    break;
111                                                                            }
112                                                                    }
113                                                            }
114                                                    }
115                                            }
116                                    }
117                            } else {
118                                    if (reasoner.getIndividuals().size() > 0) {
119                                            hasIndividuals = true;
120                                    
121                                    }
122                                    individual = reasoner.getIndividuals();
123                                    model.setIndividuals(individual);
124                                    model.setHasIndividuals(hasIndividuals);
125                            }
126                    }
127            }
128            
129            /**
130             * This Method checks if the selected class has any individuals.
131             * 
132             * @return boolean hasIndividuals
133             */
134            public boolean hasIndividuals() {
135                    return hasIndividuals;
136            }
137            
138            /**
139             * Checks the URI if a "#" is in it.
140             */
141            private void checkURI() {
142                    ontologieURI = new HashSet<String>();
143                    Set<OWLOntology> ont = editor.getModelManager().getActiveOntologies();
144                    Set<Individual> indi = reasoner.getIndividuals();
145                    for(OWLOntology onto : ont) {
146                            String ontURI = onto.getURI().toString();
147                            for(Individual ind : indi) {
148                                    if(ind.toString().contains(ontURI)) {
149                                            if(ind.toString().contains("#")) {
150                                                    ontologieURI.add(onto.getURI().toString()+"#");
151                                                    break;
152                                            } else {
153                                                    ontologieURI.add(onto.getURI().toString());
154                                                    break;
155                                            }
156                                    }
157                            }
158                    }
159                    model.setOntologyURIString(ontologieURI);
160            }
161            
162            @Override
163            public void run() {
164                    model.getSuggestModel().removeAllElements();
165                    model.initReasoner();
166                    reasoner = model.getReasoner();
167                    isInconsistent = false;
168                    if(!isInconsistent) {
169                            
170                            this.checkURI();
171                            this.setPositiveConcept();
172                            if (this.hasIndividuals()) {
173                                    view.getRunButton().setEnabled(true);
174                                    view.getHintPanel().setForeground(Color.BLACK);
175                                    view.setHintMessage("To get suggestions for class descriptions, please click the button above.");
176                                    
177                            } else {
178                                    view.getRunButton().setEnabled(false);
179                                    view.getHintPanel().setVisible(true);
180                                    String message ="There are no Instances for " + current + " available. Please insert some Instances.";
181                                    view.getHintPanel().setForeground(Color.RED);
182                                    view.setHintMessage(message);
183                            }
184                    } else {
185                            view.getHintPanel().setForeground(Color.RED);
186                            view.getRunButton().setEnabled(false);
187                            view.setHintMessage("The ontology is inconsistent and suggestions for class descriptions can only \nbe computed on consistent ontologies. Please repair the ontology first");
188                    }
189            }
190            
191            public NamedClass getCurrentConcept() {
192                    return currentConcept;
193            }
194    }