package memap.main;

import akka.timeManagement.CurrentTimeStepSubscriber;

/**
 * Tracks the simulation progress, considering both global and
 * per building optimization
 */
public class SimulationProgress implements CurrentTimeStepSubscriber {
	private static final SimulationProgress sp = new SimulationProgress();

	/** Simulation status*/
	private Status status;
	/** Error to be printed in the gui */
	private String error;
	
	private double progress = 0;
	private final int max = (TopologyConfig.getInstance().getNrDays()
			* TopologyConfig.getInstance().getTimeStepsPerDay());

	private SimulationProgress() {
		setStatus(Status.OK, "");
	}
	
	public void restart() {
		progress = 0;
		setStatus(Status.OK, "");
	}
	
	public static SimulationProgress getInstance() {
		return sp;
	}

	public double getProgress() {
		return progress * 100 / max;
	}

	@Override
	public void update(int currentTimeStep) {
		if (currentTimeStep > progress) {
			progress = currentTimeStep;
		}
	}

	/**
	 * @return the status
	 */
	public Status getStatus() {
		return status;
	}

	/**
	 * @param status the status to set
	 */
	public void setStatus(Status status, String error) {
		this.status = status;
		setError(error);
	}

	/**
	 * @return the error
	 */
	public String getError() {
		return error;
	}

	/**
	 * @param error the error to set
	 */
	private void setError(String error) {
		this.error = error;
	}

}

@@ -135,19 +135,16 @@
 obal and
-%0A *
  per bui
@@ -148,16 +148,19 @@
 building
+%0A *
  optimiz
@@ -335,16 +335,17 @@
 n status
+ 
 */%0A%09priv
@@ -425,17 +425,16 @@
  error;%0A
-%09
 %0A%09privat
@@ -456,16 +456,84 @@
 ss = 0;%0A
+%09private double fastProgress = 0;%0A%09private double slowProgress = 0;%0A
 %09private
@@ -704,33 +704,32 @@
 tus.OK, %22%22);%0A%09%7D%0A
-%09
 %0A%09public void re
@@ -785,17 +785,16 @@
 %22%22);%0A%09%7D%0A
-%09
 %0A%09public
@@ -914,136 +914,601 @@
 0 / 
+(
 max
+)
 ;%0A%09%7D%0A%0A%09
-@Override%0A%09public void update(int currentTimeStep) %7B%0A%09%09if (currentTimeStep %3E progress) %7B%0A%09%09%09progress = currentTimeStep
+/**%0A%09 * Update the simulation progress considering the individual progress on each%0A%09 * simulation thread%0A%09 */%0A%09@Override%0A%09public void update(int currentTimeStep) %7B%0A%09%09// The progress of the fastest thread is stored, but it does not trigger a%0A%09%09// progress update%0A%09%09if (currentTimeStep %3E fastProgress) %7B%0A%09%09%09fastProgress = currentTimeStep;%0A%09%09%7D else if (currentTimeStep %3C fastProgress) %7B // Do nothing if ==%0A%09%09%09// The progress off the slowest thread is stored, and the overall progress is%0A%09%09%09// updated%0A%09%09%09slowProgress = currentTimeStep;%0A%09%09%09progress = (fastProgress + slowProgress) / 2
 ;%0A%09%09

Fix simulation progress algorithm