1 package org.woehlke.computer.kurzweil.tabs.simulatedevolution;
2
3 import lombok.EqualsAndHashCode;
4 import lombok.Getter;
5 import lombok.ToString;
6 import lombok.extern.log4j.Log4j2;
7 import org.woehlke.computer.kurzweil.commons.tabs.TabController;
8 import org.woehlke.computer.kurzweil.tabs.simulatedevolution.model.population.SimulatedEvolutionPopulation;
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 @Log4j2
25 @Getter
26 @ToString(callSuper = true, exclude={"tabCtx"})
27 @EqualsAndHashCode(callSuper = true, exclude={"tabCtx"})
28
29
30
31
32
33
34
35 public class SimulatedEvolutionController extends Thread implements TabController, SimulatedEvolution {
36
37 private final SimulatedEvolutionContext tabCtx;
38 private Boolean goOn;
39 private final int threadSleepTimeConf;
40 private final int initialPopulation;
41
42 private final SimulatedEvolutionTab view;
43 private final SimulatedEvolutionModel model;
44
45 @Override
46 public UncaughtExceptionHandler getUncaughtExceptionHandler() {
47 return super.getUncaughtExceptionHandler();
48 }
49
50 public SimulatedEvolutionController(
51 SimulatedEvolutionContext tabCtx
52 ) {
53 super(TAB_TYPE.name()+"-Controller");
54 this.tabCtx = tabCtx;
55 this.goOn = Boolean.TRUE;
56 this.threadSleepTimeConf = this.tabCtx.getCtx().getProperties().getSimulatedevolution().getControl().getThreadSleepTime();
57 this.initialPopulation = this.tabCtx.getCtx().getProperties().getSimulatedevolution().getPopulation().getInitialPopulation();
58 this.view = this.tabCtx.getTab();
59 this.model = this.tabCtx.getTabModel();
60 }
61
62 public void run() {
63 boolean doMyJob;
64 int threadSleepTime = this.threadSleepTimeConf;
65 SimulatedEvolutionPopulation population;
66 do {
67 synchronized (goOn) {
68 doMyJob = goOn.booleanValue();
69 }
70 if( this.tabCtx != null){
71 synchronized (this.tabCtx) {
72 this.model.exec();
73 population = this.model.getPopulation();
74 if(population.getPopulation() > this.initialPopulation) {
75 threadSleepTime = this.threadSleepTimeConf + this.initialPopulation * (population.getPopulation() / this.initialPopulation);
76 } else {
77 threadSleepTime = this.threadSleepTimeConf;
78 }
79 }
80 synchronized (this.tabCtx) {
81 this.view.update(population);
82 }
83 this.view.repaint();
84 }
85 try {
86 sleep( threadSleepTime );
87 } catch (InterruptedException e) {
88 log.info(e.getMessage());
89 }
90 }
91 while (doMyJob);
92 }
93
94 @Override
95 public void exit() {
96 try {
97 synchronized (goOn) {
98 goOn = Boolean.FALSE;
99 }
100 join();
101 } catch (InterruptedException e){
102 log.info(e.getMessage());
103 }
104 }
105
106 }