001 /**
002 * Copyright (C) 2007-2008, 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
021 package org.dllearner.tools.ore;
022
023 import java.awt.event.ActionEvent;
024 import java.awt.event.ActionListener;
025 import java.util.List;
026 import java.util.Timer;
027 import java.util.TimerTask;
028 import java.util.concurrent.ExecutionException;
029
030 import javax.swing.DefaultListModel;
031 import javax.swing.SwingUtilities;
032 import javax.swing.SwingWorker;
033 import javax.swing.event.ListSelectionEvent;
034 import javax.swing.event.ListSelectionListener;
035
036 import org.dllearner.core.EvaluatedDescription;
037 import org.dllearner.core.LearningAlgorithm;
038 import org.dllearner.learningproblems.EvaluatedDescriptionPosNeg;
039
040
041
042 /**
043 * Wizard panel descriptor where learned class description are shown.
044 * @author Lorenz Buehmann
045 *
046 */
047 public class LearningPanelDescriptor extends WizardPanelDescriptor implements ActionListener, ListSelectionListener{
048
049 public static final String IDENTIFIER = "LEARNING_PANEL";
050 public static final String INFORMATION = "In this panel you can start the learning algorithm. While it ist running, "
051 + "temporary results are shown in the list above. Select one of them and press Next";
052
053 private LearningPanel learnPanel;
054 private LearnSwingWorker worker;
055 private LearningAlgorithm la;
056 private Timer timer;
057
058
059 public LearningPanelDescriptor() {
060
061 learnPanel = new LearningPanel();
062 learnPanel.addStartButtonListener(this);
063 learnPanel.addStopButtonListener(this);
064 learnPanel.addSelectionListener(this);
065
066 setPanelDescriptorIdentifier(IDENTIFIER);
067 setPanelComponent(learnPanel);
068
069
070 }
071
072 @Override
073 public Object getNextPanelDescriptor() {
074 return RepairPanelDescriptor.IDENTIFIER;
075 }
076
077 @Override
078 public Object getBackPanelDescriptor() {
079 return ClassPanelOWLDescriptor.IDENTIFIER;
080 }
081
082
083
084 @Override
085 public void aboutToDisplayPanel() {
086 getWizard().getInformationField().setText(INFORMATION);
087 setNextButtonAccordingToConceptSelected();
088 }
089
090
091
092 public void valueChanged(ListSelectionEvent e) {
093 setNextButtonAccordingToConceptSelected();
094
095 // Description range = new ObjectAllRestriction(new ObjectProperty("http://example.com/father#hasChild"),
096 // new NamedClass("http://example.com/father#female"));
097 // ObjectAllRestriction role = new ObjectAllRestriction(new ObjectProperty("http://example.com/father#hasChild"),
098 // range);
099 // Description de = new NamedClass("http://example.com/father#male");
100
101 if (!e.getValueIsAdjusting()){
102 getWizardModel().getOre().setNewClassDescription(((EvaluatedDescriptionPosNeg) (learnPanel.getResultList().getSelectedValue())));
103 }
104
105 }
106
107 /**
108 * Actions for pressing start- or stop-button.
109 * @param event
110 */
111 public void actionPerformed(ActionEvent event) {
112 if(event.getActionCommand().equals("Start")){
113 learnPanel.getListModel().clear();
114 learnPanel.getStartButton().setEnabled(false);
115 learnPanel.getStopButton().setEnabled(true);
116 worker = new LearnSwingWorker();
117 worker.execute();
118 } else{
119
120 learnPanel.getStopButton().setEnabled(false);
121 la.stop();
122 timer.cancel();
123 learnPanel.getStartButton().setEnabled(true);
124 learnPanel.getStatusLabel().setText("Algorithm aborted");
125 learnPanel.getLoadingLabel().setBusy(false);
126
127 }
128
129 }
130
131 private void setNextButtonAccordingToConceptSelected() {
132
133 if (learnPanel.getResultList().getSelectedValue()!= null){
134 getWizard().setNextFinishButtonEnabled(true);
135 }else{
136 getWizard().setNextFinishButtonEnabled(false);
137 }
138
139 }
140
141 /**
142 * Returns the swing worker thread instance.
143 * @return swing worker
144 */
145 public LearnSwingWorker getWorkerThread(){
146 return worker;
147 }
148
149 /**
150 * Returns the timer instance.
151 * @return timer
152 */
153 public Timer getTimer(){
154 return timer;
155 }
156
157 /**
158 * Returns the learning algorithm instance.
159 * @return learning algorithm
160 */
161 public LearningAlgorithm getLa() {
162 return la;
163 }
164
165 /**
166 * Clear list and loading message.
167 */
168 public void setPanelDefaults(){
169 learnPanel.getListModel().clear();
170 learnPanel.getStatusLabel().setText("");
171 }
172
173
174 /**
175 * Inner class, containing the background thread for learning class descriptions.
176 * @author Lorenz Buehmann
177 *
178 */
179 class LearnSwingWorker extends SwingWorker<List<? extends EvaluatedDescription>, List<? extends EvaluatedDescription>> {
180
181 private Thread t;
182
183 @SuppressWarnings("unchecked")
184 @Override
185 public List<? extends EvaluatedDescription> doInBackground() {
186
187 learnPanel.getResultList().setCellRenderer(new ColumnListCellRenderer(getWizardModel().getOre()));
188 learnPanel.getLoadingLabel().setBusy(true);
189 learnPanel.getStatusLabel().setText("Learning");
190 getWizardModel().getOre().setNoise(learnPanel.getNoise());
191 la = getWizardModel().getOre().getLa();
192 timer = new Timer();
193 timer.schedule(new TimerTask(){
194
195 @Override
196 public void run() {
197 if(la != null){
198 publish(la.getCurrentlyBestEvaluatedDescriptions(30, 0.0, true));
199 }
200 }
201
202 }, 1000, 2000);
203
204
205 t = new Thread(new Runnable(){
206
207 @Override
208 public void run() {
209
210 getWizardModel().getOre().start();
211 }
212
213 });
214 // t.setPriority(Thread.MIN_PRIORITY);
215 t.start();
216
217
218 try {
219 t.join();
220 } catch (InterruptedException e) {
221 // TODO Auto-generated catch block
222 e.printStackTrace();
223 }
224 List<? extends EvaluatedDescription> result = la.getCurrentlyBestEvaluatedDescriptions(30, 0.0, true);
225
226 return result;
227 }
228
229 @Override
230 public void done() {
231
232 timer.cancel();
233 List<? extends EvaluatedDescription> result = null;
234 try {
235 result = get();
236 } catch (InterruptedException e) {
237 e.printStackTrace();
238 } catch (ExecutionException e) {
239 e.printStackTrace();
240 }
241
242 learnPanel.getStartButton().setEnabled(true);
243 learnPanel.getStopButton().setEnabled(false);
244 updateList(result);
245 learnPanel.getLoadingLabel().setBusy(false);
246 learnPanel.getStatusLabel().setText("Algorithm terminated successfully.");
247 }
248
249 @Override
250 protected void process(List<List<? extends EvaluatedDescription>> resultLists) {
251
252 // panel4.getModel().clear();
253
254 for (List<? extends EvaluatedDescription> list : resultLists) {
255 updateList(list);
256 }
257 }
258
259 private void updateList(final List<? extends EvaluatedDescription> result) {
260
261 Runnable doUpdateList = new Runnable() {
262
263
264 DefaultListModel dm = new DefaultListModel();
265 public void run() {
266 // learnPanel.getListModel().clear();
267 for (EvaluatedDescription d : result) {
268 dm.addElement(d);
269 // panel4.getModel().addElement(d);
270
271 }
272 learnPanel.getResultList().setModel(dm);
273
274 }
275 };
276 SwingUtilities.invokeLater(doUpdateList);
277
278 }
279
280
281 }
282 }