/*
 *  2004 - 2011 Frantisek Zboril jr.
 *  Faculty of Information Technology
 *  zborilf@fit.vutbr.cz
 *  http://www.fit.vutbr.cz/~zborilf
 *
 * (C) Brno University of Technology
 * Freeware licence VUT v Brně
 * https://wis.fit.vutbr.cz/FIT/db/dir.php/dr/VZ2007/externiInformace/softwareNaWebu/Priloha2_rozh23.pdf

 */

package jTMass;

import jTMass.core.jTMassSimulator.TSimulator;
import jTMass.core.jTMassSimulator.jTCalendar.TCalendar;

/**
 *
 * @author zborilf
 */
public class TSimulationThread extends Thread{

        static boolean SRunning=false;
        static Long PEventsExecuted=new Long(0);

        @Override
        public void run(){
            TCalendar calendar=TMass.PSimulator.getCalendar();

            boolean nextStep=false;

            if((!calendar.isEmpty())&&
                ((calendar.getFirstEventTime()<TMass.PDeadline)||(TMass.PTermination!=TMass._STT_Time))&&
                ((TMass.PStepsToGo>=1)||(TMass.PTermination!=TMass._STT_Steps))&&
                                (calendar.getFirstEventTime()<TMass.PSystemDeadline))
            nextStep=true;

        while(nextStep){

            calendar.getFirstEvent().fireEvent(TMass.PSimulator);
            PEventsExecuted++;
            calendar.deleteEvent(calendar.getFirstEvent());
            TMass.PSimulator.setModelTime(calendar.getFirstEventTime());

            TMass.getPMainFrame().setTitle("TMass-X "+"("+TMass.getModel().getModelName()
                        +" ["+TMass.PSimulator.getModelTime()+" : "+PEventsExecuted+"])");

            if(TMass.PTermination==TMass._STT_Steps)
                TMass.PStepsToGo--;

            nextStep=((!calendar.isEmpty())&&
                ((calendar.getFirstEventTime()<TMass.PDeadline)||(TMass.PTermination!=TMass._STT_Time))&&
                ((TMass.PStepsToGo>=1)||(TMass.PTermination!=TMass._STT_Steps))&&
                (calendar.getFirstEventTime()<TMass.PSystemDeadline));

        }
  
        TMass.PMainFrame.simulationPaused();
        
    }
}
