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.awt.event.ActionEvent;
024 import java.awt.event.ActionListener;
025 import java.awt.event.ItemEvent;
026 import java.awt.event.ItemListener;
027 import java.awt.event.MouseEvent;
028 import java.awt.event.MouseListener;
029 import java.util.List;
030 import java.util.Set;
031 import java.util.Timer;
032 import java.util.TimerTask;
033 import java.util.concurrent.ExecutionException;
034
035 import javax.swing.DefaultListModel;
036 import javax.swing.SwingUtilities;
037 import javax.swing.SwingWorker;
038 import javax.swing.event.ListDataEvent;
039 import javax.swing.event.ListDataListener;
040 import javax.swing.event.ListSelectionEvent;
041 import javax.swing.event.ListSelectionListener;
042
043 import org.dllearner.core.EvaluatedDescription;
044 import org.dllearner.core.LearningAlgorithm;
045 import org.dllearner.core.owl.Description;
046 import org.dllearner.learningproblems.EvaluatedDescriptionClass;
047
048 /**
049 * This class processes input from the user.
050 *
051 * @author Christian Koetteritzsch
052 *
053 */
054 public class ActionHandler implements ActionListener, ItemListener,
055 MouseListener, ListSelectionListener, ListDataListener {
056
057 // This is the DLLearnerModel.
058
059 private final DLLearnerModel model;
060
061 // This is the id that checks if the equivalent class or subclass button is
062 // pressed in protege
063 private final String id;
064 // this is a boolean that checked if the advanced button was pressed or not.
065 private boolean toggled;
066 // This is the Tread of the DL-Learner
067 private EvaluatedDescription evaluatedDescription;
068 // This is the view of the DL-Learner tab.
069 private Timer timer;
070 private LearningAlgorithm la;
071 private SuggestionRetriever retriever;
072 private final Color colorRed = new Color(139, 0, 0);
073 private final Color colorGreen = new Color(0, 139, 0);
074 private final DLLearnerView view;
075
076 /**
077 * This is the constructor for the action handler.
078 *
079 * @param a
080 * ActionHandler
081 * @param m
082 * DLLearnerModel
083 * @param view
084 * DLlearner tab
085 * @param i
086 * id if it is a subclass or an equivalent class
087 *
088 */
089 public ActionHandler(DLLearnerModel m, DLLearnerView view, String i) {
090 this.view = view;
091 this.id = i;
092 this.model = m;
093 toggled = false;
094
095 }
096
097 /**
098 * When a Button is pressed this method select the right.
099 *
100 * @param z
101 * ActionEvent
102 */
103 public void actionPerformed(ActionEvent z) {
104
105 if (z.getActionCommand().equals("suggest class expression")) {
106 model.setKnowledgeSource();
107 model.setReasoner();
108 model.setLearningProblem();
109 model.setLearningAlgorithm();
110 view.getRunButton().setEnabled(false);
111 view.renderErrorMessage("learning\nstarted");
112 retriever = new SuggestionRetriever();
113 retriever.execute();
114 // model.setCurrentConcept(null);
115
116 }
117
118 if (z.getActionCommand().equals("ADD")) {
119 if (evaluatedDescription != null) {
120 model
121 .changeDLLearnerDescriptionsToOWLDescriptions(evaluatedDescription
122 .getDescription());
123 } else {
124 model
125 .changeDLLearnerDescriptionsToOWLDescriptions((Description) view
126 .getSuggestClassPanel().getSuggestList()
127 .getSelectedValue());
128 }
129 String message = "class expression\nadded";
130 view.renderErrorMessage(message);
131 }
132 if (z.getActionCommand().equals("")) {
133 if (!toggled) {
134 toggled = true;
135 view.setIconToggled(toggled);
136 view.setExamplePanelVisible(toggled);
137 } else {
138 toggled = false;
139 view.setIconToggled(toggled);
140 view.setExamplePanelVisible(toggled);
141 }
142 }
143 }
144
145 /**
146 *
147 * @return id StringID if it is a Subclass or an equivalent class.
148 */
149 public String getID() {
150 return id;
151 }
152
153 /**
154 * select/deselect the Check boxes.
155 *
156 * @param i
157 * ItemEvent
158 */
159 public void itemStateChanged(ItemEvent i) {
160
161 }
162
163 /**
164 * Nothing happens here.
165 *
166 * @param e
167 * ListSelectionEvent
168 */
169 public void valueChanged(ListSelectionEvent e) {
170
171 }
172
173 /**
174 * Nothing happens here.
175 *
176 * @param m
177 * MouseEvent
178 */
179 public void mouseReleased(MouseEvent m) {
180
181 }
182
183 /**
184 * Nothing happens here.
185 *
186 * @param m
187 * MouseEvent
188 */
189 public void mouseEntered(MouseEvent m) {
190
191 }
192
193 /**
194 * Choses the right EvaluatedDescription object after a concept is chosen in
195 * the list.
196 *
197 * @param m
198 * MouseEvent
199 */
200 public void mouseClicked(MouseEvent m) {
201 if (view.getSuggestClassPanel().getSuggestList().getSelectedValue() != null) {
202 SuggestListItem item = (SuggestListItem) view
203 .getSuggestClassPanel().getSuggestList().getSelectedValue();
204 String desc = item.getValue();
205 if (model.getEvaluatedDescriptionList() != null) {
206 List<? extends EvaluatedDescription> evalList = model
207 .getEvaluatedDescriptionList();
208 Set<String> onto = model.getOntologyURIString();
209 for (EvaluatedDescription eDescription : evalList) {
210 for (String ont : onto) {
211 if (desc.equals(eDescription.getDescription()
212 .toManchesterSyntaxString(ont, null))) {
213 evaluatedDescription = eDescription;
214 break;
215 }
216 }
217 }
218 }
219 view.getMoreDetailForSuggestedConceptsPanel()
220 .renderDetailPanel(evaluatedDescription);
221 view.setGraphicalPanel();
222 view.getMoreDetailForSuggestedConceptsPanel().repaint();
223 }
224 }
225
226 /**
227 * Nothing happens here.
228 *
229 * @param m
230 * MouseEvent
231 */
232 public void mouseExited(MouseEvent m) {
233
234 }
235
236 /**
237 * Sets the ADD button enable after a concept is chosen.
238 *
239 * @param m
240 * MouseEvent
241 */
242 public void mousePressed(MouseEvent m) {
243 if (view.getSuggestClassPanel().getSuggestList().getSelectedValue() != null) {
244 if (!view.getAddButton().isEnabled()) {
245 view.getAddButton().setEnabled(true);
246 }
247 }
248 }
249
250 /**
251 * Destroys the Thread after the Pluigin is closed.
252 */
253 public void destroyDLLearnerThread() {
254 // dlLearner = null;
255 }
256
257 /**
258 * Resets the toggled Button after the plugin is closed.
259 */
260 public void resetToggled() {
261 toggled = false;
262 }
263
264 @Override
265 public void contentsChanged(ListDataEvent listEvent) {
266 System.out.println(listEvent);
267
268 }
269
270 @Override
271 public void intervalAdded(ListDataEvent listEvent) {
272 // TODO Auto-generated method stub
273
274 }
275
276 @Override
277 public void intervalRemoved(ListDataEvent listEvent) {
278 // TODO Auto-generated method stub
279
280 }
281
282 /**
283 * Inner Class that retrieves the concepts given by the DL-Learner.
284 *
285 * @author Christian Koetteritzsch
286 *
287 */
288 class SuggestionRetriever
289 extends
290 SwingWorker<List<? extends EvaluatedDescription>, List<? extends EvaluatedDescription>> {
291
292 private Thread dlLearner;
293 private final DefaultListModel dm = new DefaultListModel();
294
295 @SuppressWarnings("unchecked")
296 @Override
297 protected List<? extends EvaluatedDescription> doInBackground()
298 throws Exception {
299 la = model.getLearningAlgorithm();
300 timer = new Timer();
301 timer.schedule(new TimerTask() {
302
303 @Override
304 public void run() {
305 if (la != null) {
306 publish(la.getCurrentlyBestEvaluatedDescriptions(view
307 .getPosAndNegSelectPanel().getOptionPanel()
308 .getNrOfConcepts()));
309 }
310 }
311
312 }, 0, 500);
313
314 dlLearner = new Thread(new Runnable() {
315
316 @Override
317 public void run() {
318 try {
319 model.run();
320 } catch (Exception e) {
321 e.printStackTrace();
322 }
323 }
324
325 });
326 dlLearner.start();
327
328 try {
329 dlLearner.join();
330 } catch (InterruptedException e) {
331 // TODO Auto-generated catch block
332 e.printStackTrace();
333 }
334 List<? extends EvaluatedDescription> result = la
335 .getCurrentlyBestEvaluatedDescriptions(view
336 .getPosAndNegSelectPanel().getOptionPanel()
337 .getNrOfConcepts());
338
339 return result;
340 }
341
342 @Override
343 public void done() {
344
345 timer.cancel();
346 List<? extends EvaluatedDescription> result = null;
347 try {
348 result = get();
349 } catch (InterruptedException e) {
350 e.printStackTrace();
351 } catch (ExecutionException e) {
352 e.printStackTrace();
353 }
354
355 view.algorithmTerminated();
356 updateList(result);
357 }
358
359 @Override
360 protected void process(
361 List<List<? extends EvaluatedDescription>> resultLists) {
362
363 for (List<? extends EvaluatedDescription> list : resultLists) {
364 updateList(list);
365 }
366 }
367
368 private void updateList(
369 final List<? extends EvaluatedDescription> result) {
370
371 Runnable doUpdateList = new Runnable() {
372
373 public void run() {
374 model.setSuggestList(result);
375 dm.clear();
376 int i = 0;
377 for (EvaluatedDescription eval : result) {
378 Set<String> ont = model.getOntologyURIString();
379 for (String ontology : ont) {
380 if (eval.getDescription().toString().contains(
381 ontology)) {
382 // dm.add(i, new SuggestListItem(colorGreen,
383 // eval
384 // .getDescription().toManchesterSyntaxString
385 // (ontology, null),
386 // ((EvaluatedDescriptionClass)
387 // eval).getAccuracy()*100));
388 if (model.isConsistent(eval)) {
389 dm.add(i, new SuggestListItem(colorGreen,
390 eval.getDescription()
391 .toManchesterSyntaxString(
392 ontology, null),
393 ((EvaluatedDescriptionClass) eval)
394 .getAccuracy() * 100));
395 break;
396 } else {
397 dm.add(i, new SuggestListItem(colorRed,
398 eval.getDescription()
399 .toManchesterSyntaxString(
400 ontology, null),
401 ((EvaluatedDescriptionClass) eval)
402 .getAccuracy() * 100));
403 view.setIsInconsistent(true);
404 break;
405 }
406 }
407 }
408 }
409 view.getSuggestClassPanel().setSuggestList(dm);
410 }
411 };
412 SwingUtilities.invokeLater(doUpdateList);
413
414 }
415
416 }
417
418 }