|
13 | 13 | import toe.datamodel.MeasureType; |
14 | 14 | import toe.helper.SimOptiCallback; |
15 | 15 | import toe.support; |
| 16 | +import toe.typedef; |
16 | 17 | import toe.typedef.typeOfLogLevel; |
17 | 18 |
|
18 | 19 | /** |
|
21 | 22 | * |
22 | 23 | * @author Christoph Bodenstein |
23 | 24 | */ |
24 | | -public class SimulatorCached extends Thread implements Simulator { |
| 25 | +public class SimulatorCached extends Thread implements Simulator, SimOptiCallback { |
25 | 26 |
|
26 | 27 | //Temporary simulation cache, to store current simulation results. Results find in this cache are taged as "isCache", other results are tagged as fresh simulations |
27 | 28 | SimulationCache myTmpSimulationCache = support.getTmpSimulationCache(); |
28 | 29 | ArrayList<SimulationType> myListOfSimulations = null; |
29 | 30 | final String logFileName; |
30 | 31 | ArrayList<ArrayList<parameter>> listOfParameterSetsTMP = null; |
31 | 32 | boolean log = false; |
| 33 | + private SimOptiCallback listener = null; |
| 34 | + private SimulationType calculatedOptimum = null; |
32 | 35 |
|
33 | 36 | /** |
34 | 37 | * Constructor |
@@ -232,26 +235,105 @@ public String getLogfileName() { |
232 | 235 | return this.logFileName; |
233 | 236 | } |
234 | 237 |
|
235 | | - @Override |
| 238 | + @Override |
236 | 239 | public boolean isOptimumCalculated() { |
237 | | - return true; |
| 240 | + return (this.calculatedOptimum != null); |
238 | 241 | } |
239 | 242 |
|
240 | 243 | @Override |
241 | 244 | public void startCalculatingOptimum(SimOptiCallback listener) { |
242 | | - //TODO: implement later |
243 | | - throw new UnsupportedOperationException("Not supported yet."); |
| 245 | + this.listener = listener; |
| 246 | + //Check if all parametersets are generated (ListOfParameterSetsToBeWritten) |
| 247 | + if (support.getMainFrame().getListOfParameterSetsToBeWritten().size() <= support.DEFAULT_MINIMUM_DESIGNSPACE_FOR_OPTIMIZATION) { |
| 248 | + support.setStatusText("Not enaugh Parametersets to simulate for target check."); |
| 249 | + support.log("No Parametersets to simulate for target check.", typeOfLogLevel.INFO); |
| 250 | + listener.operationFeedback("Generate DS first!", typedef.typeOfProcessFeedback.TargetCheckFailed); |
| 251 | + return; |
| 252 | + } else { |
| 253 | + //If not, ask to generate it (not necessary, button is disabled in this case) |
| 254 | + } |
| 255 | + support.setParameterBase(support.getMainFrame().getParameterBase()); |
| 256 | + support.setOriginalParameterBase(support.getMainFrame().getParameterBase()); |
| 257 | + this.initSimulator(support.getMainFrame().getListOfParameterSetsToBeWritten(), false); |
| 258 | + support.waitForSimulatorAsynchronous(this, this); |
244 | 259 | } |
245 | 260 |
|
246 | 261 | @Override |
247 | 262 | public void stopCalculatingOptimum(SimOptiCallback listener) { |
248 | | - //TODO: implement later |
249 | | - throw new UnsupportedOperationException("Not supported yet."); |
| 263 | + listener.operationFeedback("Targetcheck canceled.", typedef.typeOfProcessFeedback.TargetDiscarded); |
| 264 | + this.calculatedOptimum = null; |
250 | 265 | } |
251 | 266 |
|
252 | 267 | @Override |
253 | 268 | public void discardCalculatedOptimum() { |
254 | | - //TODO: implement later |
255 | | - throw new UnsupportedOperationException("Not supported yet."); |
| 269 | + support.log("CalculatedOptimum will be discarded.", typeOfLogLevel.INFO); |
| 270 | + this.calculatedOptimum = null; |
| 271 | + } |
| 272 | + |
| 273 | + @Override |
| 274 | + public void operationFeedback(String message, typedef.typeOfProcessFeedback feedback) { |
| 275 | + try { |
| 276 | + switch (feedback) { |
| 277 | + case SimulationSuccessful: |
| 278 | + ArrayList<SimulationType> simulationResults = this.getListOfCompletedSimulations(); |
| 279 | + double oldDistance = simulationResults.get(0).getDistanceToTargetValue(); |
| 280 | + ArrayList<SimulationType> foundOptima = new ArrayList<>(); |
| 281 | + foundOptima.add(simulationResults.get(0)); |
| 282 | + |
| 283 | + for (int i = 1; i < simulationResults.size(); i++) { |
| 284 | + support.spinInLabel(); |
| 285 | + support.log(String.valueOf(simulationResults.get(i).getDistanceToTargetValue()), typeOfLogLevel.VERBOSE); |
| 286 | + if (oldDistance > simulationResults.get(i).getDistanceToTargetValue()) { |
| 287 | + foundOptima.clear(); |
| 288 | + foundOptima.add(simulationResults.get(i)); |
| 289 | + oldDistance = simulationResults.get(i).getDistanceToTargetValue(); |
| 290 | + } else if (Math.abs(oldDistance - simulationResults.get(i).getDistanceToTargetValue()) < support.DEFAULT_TARGET_STEPPING) { |
| 291 | + foundOptima.add(simulationResults.get(i)); |
| 292 | + } |
| 293 | + } |
| 294 | + |
| 295 | + if (foundOptima.size() < 1) { |
| 296 | + //Error checking the target |
| 297 | + listener.operationFeedback("Opticheck failed.", typedef.typeOfProcessFeedback.TargetCheckFailed); |
| 298 | + } else if (foundOptima.size() > 1) { |
| 299 | + //Not unique |
| 300 | + listener.operationFeedback("Selected Target is not unique There are " + foundOptima.size() + " same targets.", typedef.typeOfProcessFeedback.TargetValueNotUnique); |
| 301 | + support.log("The distance to target is: " + oldDistance, typeOfLogLevel.INFO); |
| 302 | + } else if (foundOptima.size() == 1) { |
| 303 | + //Exactly one optimum with selected target value was found |
| 304 | + if (oldDistance > 0.0) { |
| 305 | + //distance not zero --> will adapt selected optimum! |
| 306 | + listener.operationFeedback("Target is unique, will change target value to match distance of 0.0.", typedef.typeOfProcessFeedback.TargetCheckSuccessful); |
| 307 | + support.log("Old distance to target is: " + oldDistance, typeOfLogLevel.INFO); |
| 308 | + |
| 309 | + } else { |
| 310 | + listener.operationFeedback("Target is unique and distance is 0.0!", typedef.typeOfProcessFeedback.TargetCheckSuccessful); |
| 311 | + } |
| 312 | + } |
| 313 | + support.log("Target value(s) found at: ", typeOfLogLevel.RESULT); |
| 314 | + for (int i = 0; i < foundOptima.size(); i++) { |
| 315 | + support.log("Parameterset: " + i, typeOfLogLevel.RESULT); |
| 316 | + for (int c = 0; c < foundOptima.get(i).getListOfParameters().size(); c++) { |
| 317 | + if (foundOptima.get(i).getListOfParameters().get(c).isIteratable()) { |
| 318 | + foundOptima.get(i).getListOfParameters().get(c).printInfo(typeOfLogLevel.INFO); |
| 319 | + } |
| 320 | + } |
| 321 | + } |
| 322 | + support.log("Targetvalue #0 will be used for optimization and statistics.", typeOfLogLevel.RESULT); |
| 323 | + calculatedOptimum = foundOptima.get(0); |
| 324 | + break; |
| 325 | + case SimulationCanceled: |
| 326 | + support.log("Simulation aborted during targetcheck.", typeOfLogLevel.INFO); |
| 327 | + listener.operationFeedback("Targetecheck aborted.", typedef.typeOfProcessFeedback.TargetCheckFailed); |
| 328 | + break; |
| 329 | + default: |
| 330 | + support.log("Unexpected feedback from simulator during targetcheck.", typeOfLogLevel.ERROR); |
| 331 | + listener.operationFeedback("Unexpected feedback.", typedef.typeOfProcessFeedback.TargetCheckFailed); |
| 332 | + } |
| 333 | + } catch (Exception e) { |
| 334 | + support.log("General error during targetcheck. Maybe Reference to simulator missing?", typeOfLogLevel.ERROR); |
| 335 | + listener.operationFeedback("Error during targetcheck.", typedef.typeOfProcessFeedback.TargetCheckFailed); |
| 336 | + support.log(e.getMessage(), typeOfLogLevel.ERROR); |
| 337 | + } |
256 | 338 | } |
257 | 339 | } |
0 commit comments