View Javadoc
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   * The ControllerThreadApplet controls the Interactions between Model and View (MVC-Pattern).
13   * <p>
14   * Simulated Evolution.
15   * Artificial Life Simulation of Bacteria Motion depending on DNA.
16   * <p>
17   * &copy; 2006 - 2013 Thomas Woehlke.
18   * http://thomas-woehlke.de/p/simulated-evolution/
19   *
20   * @author Thomas Woehlke
21   * Date: 05.02.2006
22   * Time: 00:36:20
23   */
24  @Log4j2
25  @Getter
26  @ToString(callSuper = true, exclude={"tabCtx"})
27  @EqualsAndHashCode(callSuper = true, exclude={"tabCtx"})
28  /**
29   * https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/util/concurrent/ThreadPoolExecutor.html
30   * https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/util/concurrent/Executor.html
31   * TODO: https://github.com/Computer-Kurzweil/computer_kurzweil/issues/18
32   * TODO: https://github.com/Computer-Kurzweil/computer_kurzweil/issues/19
33   * http://openbook.rheinwerk-verlag.de/javainsel9/javainsel_14_004.htm
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 }