From 8ea3b14e516766e3f2cc5db4f4f7b08311e20e79 Mon Sep 17 00:00:00 2001 From: Jochen Keil Date: Mon, 5 Jul 2010 10:27:30 +0200 Subject: [PATCH] when the environment is done with its computation it notifys all observers with an argument of 1. TspAcoGui will react upon this and set the thread variable to null. The button text also gets updated. --- src/aco/TspAcoGui.java | 39 ++++++++++++++++++++++-------------- src/aco/environment/Environment.java | 2 ++ 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/aco/TspAcoGui.java b/src/aco/TspAcoGui.java index d53a227..8c4c9b8 100644 --- a/src/aco/TspAcoGui.java +++ b/src/aco/TspAcoGui.java @@ -13,6 +13,7 @@ import java.util.Observable; import aco.antview.*; import aco.mediator.*; +import aco.environment.*; public class TspAcoGui extends JFrame @@ -237,6 +238,7 @@ public class TspAcoGui antThread = new Thread(acom.getEnvironment()); acom.getEnvironment().addObserver(antView); + acom.getEnvironment().addObserver(this); antThread.start(); startButton.setText("Stop Computation"); @@ -357,24 +359,31 @@ public class TspAcoGui } public void update(Observable o, Object arg) { - antView.deleteObserver(this); - acom.getEnvironment().deleteObserver(antView); - antView = null; + if (o instanceof AntView) { + antView.deleteObserver(this); + acom.getEnvironment().deleteObserver(antView); + antView = null; - if (antThread != null) { - antThread.interrupt(); - try { - antThread.join(); - } catch (InterruptedException ie) { - System.err.println(ie); - return; + if (antThread != null) { + antThread.interrupt(); + try { + antThread.join(); + } catch (InterruptedException ie) { + System.err.println(ie); + return; + } } - } - - antThread = null; - startButton.setText("Run selected Algorithm"); - System.gc(); + antThread = null; + startButton.setText("Run selected Algorithm"); + System.gc(); + } else if (o instanceof Environment && arg instanceof Integer) { + if ((Integer)arg == 1) { + antThread = null; + startButton.setText("Run selected Algorithm"); + System.gc(); + } + } } public static void main(String[] args) { diff --git a/src/aco/environment/Environment.java b/src/aco/environment/Environment.java index 430e7b2..6c55014 100644 --- a/src/aco/environment/Environment.java +++ b/src/aco/environment/Environment.java @@ -51,6 +51,8 @@ public abstract class Environment updateStatistics(i); acom.pheromoneUpdate(ants); } + this.setChanged(); + this.notifyObservers(1); } protected boolean interruptCondition() { -- 2.11.4.GIT