Blame view

src/mior/model/AbstractMiorModel.java 3.38 KB
1b1e928cc   glaville   initial import of...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
  package mior.model;
  
  import java.util.ArrayList;
  
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  
  
  public abstract class AbstractMiorModel implements IMiorModel {
  	
  	private int step;
  	private boolean batchModeEnabled;
  	private boolean randomEnabled;
  	private boolean topologyDone;
  	private boolean finished;
  	
  	private ArrayList<IMiorUpdateListener> updateListeners;
  	private ArrayList<IMiorFinishListener> finishListeners;
  	
  	private final static Logger logger = LoggerFactory.getLogger(AbstractMiorModel.class);
  	
  	public AbstractMiorModel() {
  		this.step = 0;
  		this.batchModeEnabled = false;
  		this.randomEnabled = true;
  		this.finished = false;
  		this.updateListeners = new ArrayList<IMiorUpdateListener>();
  		this.finishListeners = new ArrayList<IMiorFinishListener>();
  	}
  	
  	@Override
  	public final boolean isBatchModeEnabled() {
  		return batchModeEnabled;
  	}
  	
  	@Override
  	public final void setBatchModeEnabled(boolean b) {
  		this.batchModeEnabled = b;	
  	}
  	
  	@Override
  	public final boolean isRandomEnabled() {
  		return randomEnabled;
  	}
  	
  	@Override
  	public final void setRandomEnabled(boolean b) {
  		this.randomEnabled = b;	
  	}
  	
  	@Override
  	public int getBlockSize() {
  		throw new UnsupportedOperationException("Explicit block size is not supported on this implementation.");
  	}
  	
  	@Override
  	public void setBlockSize(int blockSize) {
  		throw new UnsupportedOperationException("Setting an explicit block size is not supported on this implementation.");
  	}
  	
  	@Override
  	public boolean isFinished() {
  		return finished;
  	}
  	
  	@Override
  	public final void reset() {
  		this.step = 0;
  		this.finished = false;
  		resetImpl();
  		fireMiorUpdated();
  	}
  	
  	@Override
  	public final int getStep() {
  		return step;
  	}
  	
  	@Override
  	public final void addUpdateListener(IMiorUpdateListener listener) {
  		updateListeners.add(listener);
  	}
  	
  	@Override
  	public final void removeUpdateListener(IMiorUpdateListener listener) {
  		updateListeners.remove(listener);
  	}
  	
  	protected final void fireMiorUpdated() {
  		for (IMiorUpdateListener listener : updateListeners) {
  			listener.onMiorModelUpdated(this);
  		}
  	}
  	
  	protected final void fireMiorFinished() {
  		for (IMiorFinishListener listener : finishListeners) {
  			listener.onMiorModelFinished(this);
  		}
  	}
  	
  	@Override
  	public final void doTopology() {
  		logger.trace("doTopology");
  		
  		step++;
  		doTopologyImpl();
  		this.topologyDone = true;
  		fireMiorUpdated();
  		
  		logger.trace("doTopologyEnd");
  	}
  	
  	@Override
  	public final void doLive() {
  		logger.trace("doLive");
  		
  		if (! topologyDone) {
  			doTopology();
  		}
  		
  		step ++;
  		doLiveImpl();
  		fireMiorUpdated();
  		
  		logger.trace("doLiveEnd");
  	}
  	
  	@Override
  	public void doAutoLive() {
  		int notChangedSince = 0;
  		int oldWorldCO2 = getWorld().CO2;
  		
  		if (step != 0) {
  			doLive();
  		}
  		
  		if (oldWorldCO2 == getWorld().CO2) {
  			reset();
  			doTopology();
  			doLive();
  		}
  		
  		while (notChangedSince < 2) {
  			if (getWorld().CO2 == oldWorldCO2) {
  				notChangedSince++;
  			} else {
  				oldWorldCO2 = getWorld().CO2;
  				notChangedSince = 0;
  			}
  			
  			doLive();
  		}
  		
  		this.finished = true;
  		onSimulationFinished();
  	}
  	
  	@Override
  	public final void release() {
  		releaseImpl();
  	}
  	
  	protected abstract void resetImpl();
  	protected abstract void doTopologyImpl();
  	protected abstract void doLiveImpl();
  	protected abstract void releaseImpl();
  	
  	protected void onSimulationFinished() {
  		fireMiorUpdated();
  		fireMiorFinished();
  	}
  }