/*
  Author: Algirdas Noreika. e-mail: Contact@Algirdas.COM
// Optimizer class for CLM Logistics Model.
*/

package lt.ktu.gmj.tasks;
import lt.ktu.gmj.*; 
import lt.ktu.gmj.analysis.*; 
import lt.ktu.gmj.propertySheet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;


class CLModelDomain extends Domain {
	static final String []dimensions={
		"SH1-Product-Amount-Margin (units)",
		"SH1-Product-Amount-Available (units)",
		"SH1-Product-Amount-Per-Order (units)",
		"SH1-Stock-Check-Period (hours)",
		"SH1-Lack (units)",
		"SH2-Product-Amount-Margin (units)",
		"SH2-Product-Amount-Available (units)",
		"SH2-Product-Amount-Per-Order (units)",
		"SH2-Stock-Check-Period (hours)",
		"SH2-Lack (units)",		
		"SH3-Product-Amount-Margin (units)",
		"SH3-Product-Amount-Available (units)",
		"SH3-Product-Amount-Per-Order (units)",
		"SH3-Stock-Check-Period (hours)",
		"SH3-Lack (units)",
		"ST1-Product-Amount-Margin (units)",
		"ST1-Product-Amount-Available (units)",
		"ST1-Product-Amount-Per-Order (units)",
		"ST1-Lack (units)"
	};

	public String[] dimensions () { return dimensions; }
	
	CLModelDomain () {
		//SH1-Product-Amount-Margin (units)
		min[0]= 5;
		max[0]= 40;
		defaultPoint.x[0]= 20;
		
		//SH1-Product-Amount-Available (units)
		min[1]= 20;
		max[1]= 70;
		defaultPoint.x[1]= 50;
		
		//SH1-Product-Amount-Per-Order (units)
		min[2]= 10;
		max[2]= 30;
		defaultPoint.x[2]= 20;
		
		//SH1-Stock-Check-Period (hours)
		min[3]= 1;
		max[3]= 5;
		defaultPoint.x[3]= 2;
		
		//SH1-Lack (units)
		min[4]= 0;
		max[4]= 10;
		defaultPoint.x[4]= 5;
		
		//SH2-Product-Amount-Margin (units)
		min[5]= 5;
		max[5]= 40;
		defaultPoint.x[5]= 20;
		
		//SH2-Product-Amount-Available (units)
		min[6]= 20;
		max[6]= 70;
		defaultPoint.x[6]= 40;
		
		//SH2-Product-Amount-Per-Order (units)
		min[7]= 10;
		max[7]= 30;
		defaultPoint.x[7]= 20;
		
		//SH2-Stock-Check-Period (hours)
		min[8]= 1;
		max[8]= 5;
		defaultPoint.x[8]= 2;
				
		//SH2-Lack (units)
		min[9]= 0;
		max[9]= 14;
		defaultPoint.x[9]= 7;
		
		//SH3-Product-Amount-Margin (units)
		min[10]= 5;
		max[10]= 50;
		defaultPoint.x[10]= 30;
		
		//SH3-Product-Amount-Available (units)
		min[11]= 20;
		max[11]= 80;
		defaultPoint.x[11]= 50;
		
		//SH3-Product-Amount-Per-Order (units)
		min[12]= 10;
		max[12]= 30;
		defaultPoint.x[12]= 20;
		
		//SH3-Stock-Check-Period (hours)
		min[13]= 1;
		max[13]= 5;
		defaultPoint.x[13]= 2;
				
		//SH3-Lack (units)
		min[14]= 0;
		max[14]= 14;
		defaultPoint.x[14]= 7;
		
		//ST1-Product-Amount-Margin (units)
		min[15]= 10;
		max[15]= 50;
		defaultPoint.x[15]= 30;
		
		//ST1-Product-Amount-Available (units)
		min[16]= 30;
		max[16]= 90;
		defaultPoint.x[16]= 50;
		
		//ST1-Product-Amount-Per-Order (units)
		min[17]= 20;
		max[17]= 50;
		defaultPoint.x[17]= 35;
		
		//ST1-Lack (units)
		min[18]= 0;
		max[18]= 20;
		defaultPoint.x[18]= 10;
	}
};

public class CLModel extends AbstractTask  implements TaskWithAnalyzers {
	private CLMOptimizer cLMOpt = new CLMOptimizer();;
	
	private CLModelDomain domain=new CLModelDomain();
	
	public Domain domain () { return domain; }
	public void customize (PropertyManager manager) {}

	public double f (lt.ktu.gmj.Point pt) {
		return cLMOpt.mIn(pt.x, domain.min, domain.max);
	}
	
	 public Class[] analyzers () throws ClassNotFoundException
	 {
	   Class CLMClass = Class.forName( "lt.ktu.gmj.analysis.CLMA" );
	   return new Class[]{ CLMClass };
	 } 	
};



////
/*
  Author: Algirdas Noreika.
// Optimizer class for CLM Logistics Model.
*/

class CLMOptimizer {
	CTimeLine cTimeLn;
	public CDataLogger cDataLog;
	int i;
	
	// METHODS
	public CLMOptimizer(){	// - Constructor.
		cDataLog = null;
		cTimeLn = new CTimeLine(this);
	}

	//public static void main(String sA[]) {
		/* Parameters:
		- Shops Count;
		- Stores Count;
		- iProductAmountMargin;
		- iProductAmountAvailable;
		- iProductLack;
		- iProductAmountPerOrder;
		- iStockCheckPeriod;
		- iDemandPerDay;
		- iTruckPassagePeriod;
		- iTruckPassagePeriod;
		- iOrderFulfillPeriod;
		- mSetStoreTruckCount;
		- iEndTime;
		
		
		
		*/
		/*
		CLMOptimizer co = new CLMOptimizer();
		int iIteration;
		for (iIteration=0; iIteration<100; iIteration++) {
			System.out.println("Iteration: " + iIteration);
			co.mStart();
			if (co.cDataLog != null) {
				CDataBlock cDB = co.cDataLog.mLoadData(1); // Loading Tick 10 data.
				System.out.println("Data of Tick 10: Shop0AmountInStock(" + cDB.cSHD[0].iProductsAmountInStock + ")  Shop0Demand(" + cDB.cSHD[0].iDemand + ")");
			}
		}
		*/
	//}
	
	// -------------------- OPTIMIZING ----------------------
	// SOF Procedure for optimization.
	// NOTE: If there's any ProductLack during simulation - Target function automatically results 1 - the worst value.
	//			There must be counted price to store products and price of products Lack (not sold products), cos in some
	//			cases there's a posibility, that a price to store a product is larger than not to sold some of them to the 
	//			customers.
	
	public double mIn(double pParams[], double dMin[], double dMax[]) { // Input parameters.
		double fX = 0; 
		int iIndex, iShopIdx, iStoreIdx;
		int iShopLackCount = 0, iStoreLackCount = 0;
		CDataBlock cDB;
		// AIS:Amount In Stock, PAM:Product Amount Margin. 
		double dfXShopAIS, dfXStoreAIS; // Storing in stock should be minimized.
		double dfXShopLack, dfXStoreLack; // Lack of products should be minimal.
		double dfXShopPAM, dfXStorePAM;  // Stock ProductAmountMargin should be as low as possible in case does not heap a store with orders.
		double dfXShopPAPO, dfXStorePAPO; // ProductsAmountPerOrder should be as minimal as possible to keep low products level in stock.
		double dfXShopSCP; // The lower we have stock check period, the better we can keep an eye on our stock and to make more and smaller
							// orders. In other hand the better is longer stock check period - to does not heap a store with orders.
		// NOTE: There must be set a criteria about parameters importance, which of them are the most important.
		// Init PARAMS.
		mInit(); // Initializing defaults.
				
		int[] iShopProductAISMAX =  new int[cDataLog.iShopCount];
		int[] iShopAISTotal =  new int[cDataLog.iShopCount];
		int[] iShopLack =  new int[cDataLog.iShopCount];		
		int[] iShopLackMAX =  new int[cDataLog.iShopCount];		
		int[] iStoreProductAISMAX =  new int[cDataLog.iStoreCount-1];
		int[] iStoreAISTotal =  new int[cDataLog.iStoreCount-1];
		int[] iStoreLackMAX =  new int[cDataLog.iStoreCount-1];
		int[] iStoreLack =  new int[cDataLog.iStoreCount-1];
		
		
		cTimeLn.cMMan.cShop[0].iProductAmountMargin = (int) Math.round(pParams[0]);
		cTimeLn.cMMan.cShop[0].iProductAmountAvailable = (int) Math.round(pParams[1]);
		cTimeLn.cMMan.cShop[0].iProductAmountPerOrder = (int) Math.round(pParams[2]);
		cTimeLn.cMMan.cShop[0].iStockCheckPeriod = (int) Math.round(pParams[3]);
		
		cTimeLn.cMMan.cShop[1].iProductAmountMargin = (int) Math.round(pParams[5]);
		cTimeLn.cMMan.cShop[1].iProductAmountAvailable = (int) Math.round(pParams[6]);
		cTimeLn.cMMan.cShop[1].iProductAmountPerOrder = (int) Math.round(pParams[7]);
		cTimeLn.cMMan.cShop[1].iStockCheckPeriod = (int) Math.round(pParams[8]);
		
		cTimeLn.cMMan.cShop[2].iProductAmountMargin = (int) Math.round(pParams[10]);
		cTimeLn.cMMan.cShop[2].iProductAmountAvailable = (int) Math.round(pParams[11]);
		cTimeLn.cMMan.cShop[2].iProductAmountPerOrder = (int) Math.round(pParams[12]);
		cTimeLn.cMMan.cShop[2].iStockCheckPeriod = (int) Math.round(pParams[13]);
		
		cTimeLn.cMMan.cStore[0].iProductAmountMargin = (int) Math.round(pParams[15]);
		cTimeLn.cMMan.cStore[0].iProductAmountAvailable = (int) Math.round(pParams[16]);
		cTimeLn.cMMan.cStore[0].iProductAmountPerOrder = (int) Math.round(pParams[17]);
		
		
		mStartSimulation();	 // Performing simulation.
		
		
		// Initialising.
	
		if (cDataLog != null) {
			cDB = cDataLog.mLoadData(0);
			// Shops.
			for (iShopIdx=0; iShopIdx<cDataLog.iShopCount; iShopIdx++) {
				// Product Amount in Stock.
				iShopAISTotal[iShopIdx] = cDB.cSHD[iShopIdx].iProductsAmountInStock;
				// Lack
				iShopLack[iShopIdx] = iShopLackMAX[iShopIdx] = cDB.cSHD[iShopIdx].iProductsLack;
			}
			// Stores.
			for (iStoreIdx=0; iStoreIdx<cDataLog.iStoreCount-1; iStoreIdx++) {
				// Product Amount in Stock.
				iStoreAISTotal[iStoreIdx] = cDB.cSD[iStoreIdx].iProductsAmountInStock;
				// Lack
				iStoreLack[iStoreIdx] = iStoreLackMAX[iStoreIdx] = cDB.cSD[iStoreIdx].iProductsLack;
			}
		}
		
		//Calculating for individual shops and stores.
		for (iIndex=1; iIndex<cTimeLn.iEndTime;iIndex++) {
			if (cDataLog != null) {
				cDB = cDataLog.mLoadData(iIndex);
				// Products amount in stock.
				//Shops.
				for (iShopIdx=0; iShopIdx<cDataLog.iShopCount; iShopIdx++) {
					iShopAISTotal[iShopIdx]+= cDB.cSHD[iShopIdx].iProductsAmountInStock;
					// Lack
					iShopLack[iShopIdx] += cDB.cSHD[iShopIdx].iProductsLack;
				}
				// Stores
				for (iStoreIdx=0; iStoreIdx<cDataLog.iStoreCount-1; iStoreIdx++) {
					iStoreAISTotal[iStoreIdx]+= cDB.cSD[iStoreIdx].iProductsAmountInStock;
					// Lack
					iStoreLack[iStoreIdx]+= cDB.cSD[iStoreIdx].iProductsLack;
				}
			}
		}
		
		dfXShopAIS = 0;
		dfXShopPAM = 0;
		dfXShopPAPO = 0;
		dfXShopLack = 0;
		
		// Normalizing.
		for (iShopIdx=0; iShopIdx<cDataLog.iShopCount; iShopIdx++) {
			// Shops
			dfXShopAIS += mNormalize(dMin[iShopIdx*5+1], dMax[iShopIdx*5+1], (double)(iShopAISTotal[iShopIdx]/cTimeLn.iEndTime));
			dfXShopPAM += mNormalize(dMin[iShopIdx*5], dMax[iShopIdx*5], (double)(cTimeLn.cMMan.cShop[iShopIdx].iProductAmountMargin));
			dfXShopPAPO += mNormalize(dMin[iShopIdx*5+2], dMax[iShopIdx*5+2],  (double)(cTimeLn.cMMan.cShop[iShopIdx].iProductAmountPerOrder));
			dfXShopLack += mNormalize(dMin[iShopIdx*5+4], dMax[iShopIdx*5+4], (double)(iShopLack[iShopIdx]/cTimeLn.iEndTime));
		}
		
		fX = (dfXShopAIS / cDataLog.iShopCount); // Calculating Shops AIS value.
//		fX += (dfXShopPAM / cDataLog.iShopCount); // Calculating Shops PAM value.
//		fX += (dfXShopPAPO / cDataLog.iShopCount); // Calculating Shops PAPO value.
		fX += (dfXShopLack / cDataLog.iShopCount); // Calculating Shops LACK value.
		

		dfXStoreAIS = 0;
		dfXStorePAM = 0;
		dfXStorePAPO = 0;
		dfXStoreLack = 0;
		

		// Normalizing.				
		for (iStoreIdx=0; iStoreIdx<cDataLog.iStoreCount-1; iStoreIdx++) {
			//Stores
			dfXStoreAIS += mNormalize(dMin[cDataLog.iShopCount * 5 + iStoreIdx*4+1],dMax[cDataLog.iShopCount * 5 + iStoreIdx*4+1], (double)(iStoreAISTotal[iStoreIdx]/cTimeLn.iEndTime));
			dfXStorePAM += mNormalize(dMin[cDataLog.iShopCount * 5 + iStoreIdx*4], dMax[cDataLog.iShopCount * 5 + iStoreIdx*4], (double)(cTimeLn.cMMan.cStore[iStoreIdx].iProductAmountMargin));
			dfXStorePAPO += mNormalize(dMin[cDataLog.iShopCount * 5 + iStoreIdx*4+2], dMax[cDataLog.iShopCount * 5 + iStoreIdx*4+2], (double)(cTimeLn.cMMan.cStore[iStoreIdx].iProductAmountPerOrder));
			dfXStoreLack += mNormalize(dMin[cDataLog.iShopCount * 5 + iStoreIdx*4+3], dMax[cDataLog.iShopCount * 5 + iStoreIdx*4+1], (double)(iStoreLack[iStoreIdx]/cTimeLn.iEndTime));
		}

		fX+= (dfXStoreAIS / (cDataLog.iStoreCount-1));
//		fX+= (dfXStorePAM / (cDataLog.iStoreCount-1));
//		fX+= (dfXStorePAPO / (cDataLog.iStoreCount-1));
		fX+= (dfXStoreLack / (cDataLog.iStoreCount-1));
		
		return (fX/4);
		
		
	}
	
	public double mNormalize(double dMin, double dMax, double dVal) {
		//dMax = dMax - dMin;
		dMin = 0;
		if (dVal>dMax) dVal = dMax;
		return ( (dVal-dMin) / (dMax-dMin) );
	}
	
	// EOF Procedures of optimization
	// -------------------- OPTIMIZING ----------------------

	
		
	public void mStartSimulation(){	// - Starts logistics model simulation.
		cTimeLn.mStart();
	}
	
	public void mStopSimulation(){	// - Stops logistics model simulation.
	}
	
	public void mPause(){			// - Pauses logistics model simulation.
	}
	
	public void mStart() {
		mInit(); // Initilising process.
		mStartSimulation();	
	}
	
	// - Vizualizes a completed Tick data.
	// Initialising time tick
	public void mTickInit() {
		// Starting loging Tick Data into Log.
		cDataLog.mNewTickDataBlock();
	}
	
	// Time tick accomplished.
	public void mTickComplete() {
		// Information on shops.		
		for (i=0; i<cTimeLn.cMMan.cShop.length; i++) {
			// Loging shop data.
			cDataLog.mSetShopData(i, cTimeLn.cMMan.cShop[i].iProductAmountAvailable, cTimeLn.cMMan.cShop[i].iProductLack, cTimeLn.cMMan.cShop[i].iProductsOrdered, cTimeLn.cMMan.cShop[i].iDemand);
		}
		
		// Information on stores.
		for (i=0; i<cTimeLn.cMMan.cStore.length; i++) {
			// Loging store data.
			cDataLog.mSetStoreData(i, cTimeLn.cMMan.cStore[i].iProductAmountAvailable, cTimeLn.cMMan.cStore[i].iProductLack, cTimeLn.cMMan.cStore[i].iProductsOrdered, cTimeLn.cMMan.cStore[i].iTruckCountAvailable);
		}
		cDataLog.mSaveTickDataBlock();
	}

	public void mEventOccured(int iAction, int iObjID, int iObjType, int iValue){
		// Loging event data.
		cDataLog.mSetEvent(iAction, iObjID, iObjType, iValue);
	}
	
	// INITPARAM12
	public void mInit() {
		// End time. 
		cTimeLn.mInitialize();
		cTimeLn.iEndTime = 24; // Simulation period - (72hours)3 days.
		
		// Setting Shops Count.
		cTimeLn.cMMan.mSetShopCount(3);
		
		// Setting Stores Count.
		cTimeLn.cMMan.mSetStoreCount(2);	
		
		// Assigning shops to Stores.
		cTimeLn.cMMan.mAssignShop(0, 0);
		cTimeLn.cMMan.mAssignShop(1, 0);
		cTimeLn.cMMan.mAssignShop(2, 0);

		// Product starting amount (S(t0))
		cTimeLn.cMMan.cShop[0].iProductAmountMargin = 16;
		cTimeLn.cMMan.cShop[1].iProductAmountMargin = 18;
		cTimeLn.cMMan.cShop[2].iProductAmountMargin = 21;
		
		// Product Amount must be equal or greater than Product Amount Margin. (Str)
		cTimeLn.cMMan.cShop[0].iProductAmountAvailable = 39;
		cTimeLn.cMMan.cShop[1].iProductAmountAvailable = 22;
		cTimeLn.cMMan.cShop[2].iProductAmountAvailable = 44;
		
		
		// Product lack is 0 (T(t0)).
		cTimeLn.cMMan.cShop[0].iProductLack = 0;
		cTimeLn.cMMan.cShop[1].iProductLack = 0;
		cTimeLn.cMMan.cShop[2].iProductLack = 0;
		
		// Product order amount is 0 P(t0).
		cTimeLn.cMMan.cShop[0].iProductAmountPerOrder = 20;
		cTimeLn.cMMan.cShop[1].iProductAmountPerOrder = 20;
		cTimeLn.cMMan.cShop[2].iProductAmountPerOrder = 20;
		
		// Stock check period in hours(epsilon).
		cTimeLn.cMMan.cShop[0].iStockCheckPeriod = 2;
		cTimeLn.cMMan.cShop[1].iStockCheckPeriod = 2;
		cTimeLn.cMMan.cShop[2].iStockCheckPeriod = 2;
		// Seeting into events to check stock after 2 hours.
		cTimeLn.cEvtStack.mPush((cTimeLn.iTime + cTimeLn.cMMan.cShop[0].iStockCheckPeriod), 0, cTimeLn.cMMan.ID_SHOP, cTimeLn.cEvtStack.E_SHOPCHECKSTOCK,0);
		cTimeLn.cEvtStack.mPush((cTimeLn.iTime + cTimeLn.cMMan.cShop[1].iStockCheckPeriod), 1, cTimeLn.cMMan.ID_SHOP, cTimeLn.cEvtStack.E_SHOPCHECKSTOCK,0);
		cTimeLn.cEvtStack.mPush((cTimeLn.iTime + cTimeLn.cMMan.cShop[2].iStockCheckPeriod), 2, cTimeLn.cMMan.ID_SHOP, cTimeLn.cEvtStack.E_SHOPCHECKSTOCK,0);
		
		// Shop demand per Day.
		cTimeLn.cMMan.cShop[0].iDemandPerDay = 50;
		cTimeLn.cMMan.cShop[1].iDemandPerDay = 50;
		cTimeLn.cMMan.cShop[2].iDemandPerDay = 50;

		cTimeLn.cMMan.cShop[0].iTruckPassagePeriod = 2; // 2 hours.
		cTimeLn.cMMan.cShop[1].iTruckPassagePeriod = 2; // 2 hours.
		cTimeLn.cMMan.cShop[2].iTruckPassagePeriod = 2; // 2 hours.
		
		cTimeLn.cMMan.cShop[0].iTruckReturnPeriod = 1; // 1 hours.
		cTimeLn.cMMan.cShop[1].iTruckReturnPeriod = 1; // 1 hours.
		cTimeLn.cMMan.cShop[2].iTruckReturnPeriod = 1; // 1 hours.
		
			
		// Stores.	
		// Setting stores truck count.
		cTimeLn.cMMan.mSetStoreTruckCount(0, 10);
		
		// Assigning shops to Stores.
		cTimeLn.cMMan.mAssignStore(0, 1);	// - Assigning Store to supplier.

		// (STr)		
		cTimeLn.cMMan.cStore[0].iProductAmountMargin = 14;

		// s(t0)
		cTimeLn.cMMan.cStore[0].iProductAmountAvailable = 37;
		
		// T(t0)
		cTimeLn.cMMan.cStore[0].iProductLack = 0;
		
		// P(t0)
		cTimeLn.cMMan.cStore[0].iProductAmountPerOrder = 26;
		
		cTimeLn.cMMan.cStore[0].iOrderFulfillPeriod = 1; // 1 hour.
		
		cTimeLn.cMMan.cStore[0].iTruckPassagePeriod = 2; // Rruck passage period from store or suplier to stock.
		
		cTimeLn.cMMan.cStore[0].iTruckReturnPeriod = 1; // Truck return period from stock to store or suplier.
		
		
		// Tiekejas.		
		cTimeLn.cMMan.cStore[1].iProductAmountMargin = 0;
		cTimeLn.cMMan.cStore[1].iProductAmountAvailable = 99999; // Unlimited product amount
		cTimeLn.cMMan.cStore[1].iProductLack = 0;
		cTimeLn.cMMan.cStore[1].iOrderFulfillPeriod = 0; // no time for order fulfilation.
		cTimeLn.cMMan.mSetStoreTruckCount(1, 10);	// Unlimited truck count.
		// No stock check is performed.
		
		cDataLog = null;
		cDataLog = new CDataLogger(cTimeLn.cMMan.cShop.length, cTimeLn.cMMan.cStore.length);		
		// - Setting initial data. (time-tick = 0)
		cDataLog.mNewTickDataBlock();
		
		// Logging Primary Information.
		// Displaying primary information:
		for (i=0; i<cTimeLn.cMMan.cShop.length; i++) {
			cDataLog.mSetShopData(i, cTimeLn.cMMan.cShop[i].iProductAmountAvailable, cTimeLn.cMMan.cShop[i].iProductLack, cTimeLn.cMMan.cShop[i].iProductsOrdered, cTimeLn.cMMan.cShop[i].iDemand);
		}
		
		for (i=0; i<cTimeLn.cMMan.cStore.length; i++) {
			cDataLog.mSetStoreData(i, cTimeLn.cMMan.cStore[i].iProductAmountAvailable, cTimeLn.cMMan.cStore[i].iProductLack, cTimeLn.cMMan.cStore[i].iProductsOrdered, cTimeLn.cMMan.cStore[i].iTruckCountAvailable);
		}
		cDataLog.mSaveTickDataBlock();
	}
};


///////

/***********************************************************
Title: CLM - Main class(Logistics Model).
Author:	Algirdas Noreika; norealgi@soften.ktu.lt
Date created: 2004-02-21
Description & Comments:
	- Performs all initializations. Starts simulations and so on. 
	- Applet version.
	- Ka veikia parduotuvese klientai kol nera prekiu ? Jie laukia ar ateina nauji ?
	- Nenumatyta ka daro parduotuve kai nera prekiu.
	- Numatytas masinos atvykimo ir isvykimo laikas.
	- Jeigu parduotuves sandelys tikrinamas antra karta, kai tuo tarpu vykdomas uzsakymas - ka daryti ? 
***********************************************************/

class CDataLogger {
	private CDataBlock cDB;
	public ArrayList iaDataLog;
	public int iShopCount, iStoreCount;
	
	// METHODS
	
	public int mGetLogSize() {
		return iaDataLog.size();
	}
	public CDataLogger(int iShopCount, int iStoreCount){	// - Constructor.
	  iaDataLog = new ArrayList();
	  this.iShopCount = iShopCount;
	  this.iStoreCount = iStoreCount;
	}
	
	public void mNewTickDataBlock() {
		cDB = new CDataBlock(iShopCount, iStoreCount);	
	}
	
	public void mSetShopData(int iID, int iPAIS, int iPL, int iPO, int iD) {
		CShopsData cSHD = new CShopsData();
		cSHD.iProductsAmountInStock = iPAIS;
		cSHD.iProductsLack = iPL;
		cSHD.iProductsOrdered = iPO;
		cSHD.iDemand = iD;
		cDB.cSHD[iID] = cSHD;
	}
	
	public void mSetStoreData(int iID, int iPAIS, int iPL, int iPO, int iTA) {
		CStoresData cSD = new CStoresData();
		cSD.iProductsAmountInStock = iPAIS;
		cSD.iProductsLack = iPL;
		cSD.iProductsOrdered = iPO;
		cSD.iTruckAvailable = iTA;
		cDB.cSD[iID] = cSD;
	}

	public void mSetEvent(int iEvent, int iObjectID, int iObjectType, int iVal) {
		CEventsData cED = new CEventsData();
		cED.iEvent = iEvent;
		cED.iObjID = iObjectID;
		cED.iObjType = iObjectType;
		cED.iValue = iVal;
		
		cDB.aEvents.add(cED);
	}
	
	public void mSaveTickDataBlock() {
		iaDataLog.add(cDB);
	}
		
	public CDataBlock mLoadData(int iTick) {
		return (CDataBlock) iaDataLog.get(iTick);
	}
}


// -- Data Block
class CDataBlock {
	public ArrayList aEvents;
	public CStoresData[] cSD;
	public CShopsData[] cSHD;
	
	CDataBlock(int iShopCount, int iStoreCount) {
		aEvents = new ArrayList();
		cSHD = new CShopsData[iShopCount];
	    cSD = new CStoresData[iStoreCount];
	}
}


class CStockData {
	public int iProductsAmountInStock;
	public int iProductsLack;
	public int iProductsOrdered;
}

class CStoresData  extends CStockData {
	public int iTruckAvailable;
}


class CShopsData extends CStockData {
	public int iDemand;
}

class CEventsData {
	public int iEvent;
	public int iObjID;
	public int iObjType;
	public int iValue;
};


////////


/*
Visually displays data result.


*/

class CAnalysis implements ActionListener{
		JFrame jF;
		CDataLogger cDL;
		int i;
		CAnalysisGraph cAG;
		JScrollPane scrollpane;
		JRadioButton jRB_1, jRB_2, jRB_3, jRB_4;
		JComboBox jLItems;
		
	CAnalysis (CDataLogger cDataLog){
		cDL = cDataLog;
	}
	
	public void mShowAnalysis() {
		jF = new JFrame("Analysis");
		cAG = new CAnalysisGraph();
		
		jF.setBounds(100,100,600,610);
		Container cntMain = jF.getContentPane();
		cntMain.setLayout(new BorderLayout());
		Container cntTopMenu = new Container();
		
		cntTopMenu.setLayout(new FlowLayout(FlowLayout.LEFT));
		
		// Adding data into Combo Box.		
		String[] sItemsList = new String[cDL.iShopCount + cDL.iStoreCount];
		for (i=0; i<sItemsList.length; i++) {
			if (i < cDL.iShopCount) {
				sItemsList[i] = new String("Shop["+i+"]");
			} else {
				sItemsList[i] = new String("Store["+ (i - cDL.iShopCount) + "]");
			}
		}
		
		ButtonGroup jRB_Group = new ButtonGroup();
		jRB_1 = new JRadioButton("ProductsAmountInStock");
		jRB_2 = new JRadioButton("ProductsLack");
		jRB_3 = new JRadioButton("ProductsOrdered");
		jRB_4 = new JRadioButton("");
		
		jLItems = new JComboBox(sItemsList);

				
		jLItems.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mSetAnalysisObject();
			}
		});
			
			
		cntTopMenu.add(jLItems);
		
		
		jRB_Group.add(jRB_1);
		jRB_Group.add(jRB_2);
		jRB_Group.add(jRB_3);
		jRB_Group.add(jRB_4);

		cntTopMenu.add(jRB_1);
		cntTopMenu.add(jRB_2);
		cntTopMenu.add(jRB_3);
		cntTopMenu.add(jRB_4);
		
		jRB_1.setSelected(true);		
		// Listening which button is selected.				
		jRB_1.setActionCommand("BT1");
		jRB_1.addActionListener(this);
		jRB_2.setActionCommand("BT2");
		jRB_2.addActionListener(this);
		jRB_3.setActionCommand("BT3");
		jRB_3.addActionListener(this);
		jRB_4.setActionCommand("BT4");
		jRB_4.addActionListener(this);

		// Creating Analysis graph.

		
		cAG.iXStep = 40;
//		cAG.iYValCount = 5;
		cAG.iYStep = 10;

		cAG.iDatasetsLength = cDL.mGetLogSize();
		cAG.iDataSetsCount = 4;
		cAG.iDataSetIndex = 0; // Data set which will be shown.
		cAG.mInit(0,0,500);
		
		mSetAnalysisObject();
		
		scrollpane = new JScrollPane(cAG);
//		scrollpane.getViewport().setScrollMode(JViewport.SIMPLE_SCROLL_MODE);
		scrollpane.getViewport().setBackground(new Color(255,255,255));
		
		//setBackground(new Color(255,255,255));
		
		scrollpane.setSize(100,100);
		
		cntMain.add(cntTopMenu, BorderLayout.NORTH);		
		cntMain.add(scrollpane, BorderLayout.CENTER);
		
		jF.setVisible(true);
		
		
	}
	
	public void mSetAnalysisObject() {
				CDataBlock cDB;
				CEventsData cED;
				int iSel, j;
				
				iSel = jLItems.getSelectedIndex();
				if (iSel < cDL.iShopCount) {
					jRB_4.setText(new String("Demand"));
					if (cAG.iDataSetIndex == 3) cAG.sLegendX = new String("Y - Demand (units),  X - Time (hours)");
					for (i=0; i<cAG.iDatasetsLength; i++) {
						cDB = cDL.mLoadData(i);
						cAG.mSetData(0, i, cDB.cSHD[iSel].iProductsAmountInStock); // Retrieving product amount in stock.
						cAG.mSetData(1, i, cDB.cSHD[iSel].iProductsLack);
						cAG.mSetData(2, i, cDB.cSHD[iSel].iProductsOrdered);
						cAG.mSetData(3, i, cDB.cSHD[iSel].iDemand);
						
						//Resetting 
						cAG.mSetEvent(i, 1, false);
						cAG.mSetEvent(i, 2, false);
						cAG.mSetEvent(i, 3, false);
						cAG.mSetEvent(i, 4, false);
						cAG.mSetEvent(i, 5, false);
						cAG.mSetEvent(i, 6, false);
						// Setting.
						for (j=0; j<cDB.aEvents.size(); j++) {
							cED = (CEventsData) cDB.aEvents.get(j);
							if (cED.iObjID == iSel && cED.iObjType == CModelManager.ID_SHOP) cAG.mSetEvent(i, cED.iEvent, true);
						}
						
					}
				} else {
					jRB_4.setText(new String("Trucks available"));
					if (cAG.iDataSetIndex == 3) cAG.sLegendX = new String("Y - Trucks available,  X - Time (hours)");
					for (i=0; i<cAG.iDatasetsLength; i++) {
						cDB = cDL.mLoadData(i);
						cAG.mSetData(0, i, cDB.cSD[iSel-cDL.iShopCount].iProductsAmountInStock); // Retrieving product amount in stock.
						cAG.mSetData(1, i, cDB.cSD[iSel-cDL.iShopCount].iProductsLack);
						cAG.mSetData(2, i, cDB.cSD[iSel-cDL.iShopCount].iProductsOrdered);
						cAG.mSetData(3, i, cDB.cSD[iSel-cDL.iShopCount].iTruckAvailable);
						
						//Resetting 
						cAG.mSetEvent(i, 1, false);
						cAG.mSetEvent(i, 2, false);
						cAG.mSetEvent(i, 3, false);
						cAG.mSetEvent(i, 4, false);
						cAG.mSetEvent(i, 5, false);
						cAG.mSetEvent(i, 6, false);
						// Setting.
						for (j=0; j<cDB.aEvents.size(); j++) {
							cED = (CEventsData) cDB.aEvents.get(j);
							if (cED.iObjID == iSel-cDL.iShopCount && cED.iObjType == CModelManager.ID_STORE) cAG.mSetEvent(i, cED.iEvent, true);
						}
					}
				}
				
				cAG.mNormalize(0); // Normalizing dataset.
				cAG.mNormalize(1); // Normalizing dataset.
				cAG.mNormalize(2); // Normalizing dataset.
				cAG.mNormalize(3); // Normalizing dataset.
				
				
				cAG.repaint();
	}
	
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand() == "BT1") {
			cAG.iDataSetIndex=0;
			cAG.sLegendX = new String("Y - ProductsAmountInStock (units),  X - Time (hours)");
		} else if (e.getActionCommand() == "BT2"){
			cAG.iDataSetIndex=1;
			cAG.sLegendX = new String("Y - ProductsLack (units),  X - Time (hours)");
		} else if (e.getActionCommand() == "BT3"){
			cAG.iDataSetIndex=2;
			cAG.sLegendX = new String("Y - ProductsOrdered units),  X - Time (hours)");
		} else if (e.getActionCommand() == "BT4"){
			cAG.iDataSetIndex=3;
			if (jLItems.getSelectedIndex() < cDL.iShopCount) {
				cAG.sLegendX = new String("Y - Demand (units),  X - Time (hours)");
			} else {
				cAG.sLegendX = new String("Y - Trucks available,  X - Time (hours)");
			}
		}
		cAG.repaint();
	}		
}





// Analysis graph to show data.
class CAnalysisGraph extends JComponent implements Scrollable {
	public int iX, iY, iWidth, iHeight;
	public int iXValCount, iYValCount;
	public int iXStep, iYStep;
	private int iMultiplier;
	public String sLegendX, sLegendY;
	public int iPaddingTop, iPaddingLeft, iPaddingRight, iPaddingBottom;
	public boolean bGridOn;
	public int iDatasetsLength;
	public int iDataSetsCount;
	public int iDataSetIndex;
	private double daDatasetX[][];
	private boolean baEventsSet[][];
	Color clrBackground;
	private int i, j;
	private int maxUnitIncrement = 1;
	Color cColor[];

// - Constructor.		
	CAnalysisGraph () {
		clrBackground = new Color(255,255,255);
		iPaddingTop = 10;
		iPaddingLeft = 30;
		iPaddingRight = 10;
		iPaddingBottom = 180;
		sLegendX = new String("X");
		sLegendY = new String("Y");
		bGridOn = false;
		iDataSetsCount = 1;
		iDataSetIndex = 0;
		cColor = new Color[6];
		cColor[0] = new Color(150,150,150);
		cColor[1] = new Color(150,150,0);
		cColor[2] = new Color(0,150,150);		
		cColor[3] = new Color(150,0,150);	
		cColor[4] = new Color(150,0,0);		
		cColor[5] = new Color(0,0,150);		
	}
	
	// Initiliasing analysis graph.
	public void mInit(int iX, int iY, int iHeight) {
		super.setBackground(clrBackground);	
		daDatasetX = new double[iDatasetsLength][iDataSetsCount*2];
		baEventsSet  = new boolean[iDatasetsLength][6]; 
		mSetBounds(iX, iY, iHeight);

	}
	
	// ------------------------------------------- Seriazible interface
	public Dimension getPreferredScrollableViewportSize() {
		return getPreferredSize();
	}
		
	public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
        //Get the current position.
        int currentPosition = 0;
       
        if (orientation == SwingConstants.HORIZONTAL) {
            currentPosition = visibleRect.x;
        } else {
            currentPosition = visibleRect.y;
        }

        //Return the number of pixels between currentPosition
        //and the nearest tick mark in the indicated direction.
        if (direction < 0) {
            int newPosition = currentPosition -
                             (currentPosition / maxUnitIncrement)
                              * maxUnitIncrement;
            return (newPosition == 0) ? maxUnitIncrement : newPosition;
        } else {
            return ((currentPosition / maxUnitIncrement) + 1)
                   * maxUnitIncrement
                   - currentPosition;
        }
        
	}

    public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
        if (orientation == SwingConstants.HORIZONTAL) {
            return visibleRect.width - maxUnitIncrement;
        } else {
            return visibleRect.height - maxUnitIncrement;
        }
    }

    public boolean getScrollableTracksViewportWidth() {
        return false;
    }

    public boolean getScrollableTracksViewportHeight() {
        return false;
    }

    public void setMaxUnitIncrement(int pixels) {
        maxUnitIncrement = pixels;
    }
// -------------------------------------------------   
   
	public void paint(Graphics g) {
		g.setColor(new Color(0,0,0));
		// Drawing Axis lines.
		// Drawing X.
		g.drawLine(iPaddingLeft+5, iHeight-iPaddingBottom-5, iPaddingLeft+5+iDatasetsLength*iXStep, iHeight-iPaddingBottom-5);
		for (i=0; i<iDatasetsLength; i++) {
			g.drawLine(iPaddingLeft + 5 + i * iXStep,
				iHeight-iPaddingBottom-5, 
				iPaddingLeft + 5 + i * iXStep, 
				iHeight-iPaddingBottom);	
		}
		
		// Drawing Y.
		g.drawLine(iPaddingLeft+5, iPaddingTop, iPaddingLeft+5, iHeight-iPaddingBottom-5);
		for (i=0; i<iYValCount; i++) {
			g.drawLine(iPaddingLeft,
				(iHeight-iPaddingBottom-5) - i * iYStep,
				iPaddingLeft+5,
				(iHeight-iPaddingBottom-5) - i * iYStep);	
		}
		
		FontMetrics fm = g.getFontMetrics();
		
		// Drawing Data. Set 0.
		for (i=0; i<iDatasetsLength; i++) {
			g.setColor(new Color(0,0,0));	
						
			g.drawString(Integer.toString((int)daDatasetX[i][iDataSetIndex]), 
			iPaddingLeft + 5 + i * iXStep - fm.stringWidth(Integer.toString((int)daDatasetX[i][iDataSetIndex]))/2,
				(int)(iHeight-iPaddingBottom-5 - daDatasetX[i][iDataSetIndex+iDataSetsCount] * iMultiplier - 2));

			g.drawString(Integer.toString((int) i), 
			iPaddingLeft + 5 + i * iXStep - fm.stringWidth(Integer.toString((int) i))/2,
				(int)(iHeight-iPaddingBottom+12));
								
			g.setColor(new Color(200,0,0));				
			g.fillRect(iPaddingLeft + 5 + i * iXStep-3,
				(int)(iHeight-iPaddingBottom-5 - daDatasetX[i][iDataSetIndex+iDataSetsCount] * iMultiplier), 
				5,
				(int)(daDatasetX[i][iDataSetIndex+iDataSetsCount] * iMultiplier));
		}
		
		// Drawing Legends
		g.setColor(new Color(0,0,0));	
		g.drawString(sLegendX, 
		(iWidth - (iPaddingLeft + iPaddingRight)) / 2 - fm.stringWidth(sLegendX)/2,  (int)(iHeight-iPaddingBottom + 30));
		
		mDrawEventsData(g);
		
	}
	
	
	
	// Drawing Events Data.
	public void mDrawEventsData(Graphics g) {
		int iLegendOffset = 85;
		int iEventYStep=6;
		int iLegendStepY = 0;
		int iEventLineStartY = 43;
		
		
		g.setColor(cColor[0]);	
		g.drawLine(iPaddingLeft+5, (iHeight-iPaddingBottom + iEventLineStartY) + iEventYStep * 0, iPaddingLeft+5+iDatasetsLength*iXStep, (iHeight-iPaddingBottom + iEventLineStartY) + iEventYStep * 0);		
		g.fillOval(iPaddingLeft + 5,(iHeight-iPaddingBottom + iLegendOffset + iLegendStepY), 4,4);
		g.drawString("- E_SHOPCHECKSTOCK", iPaddingLeft + 14, (iHeight-iPaddingBottom + iLegendOffset + iLegendStepY) + 6);
		iLegendStepY +=15;
		g.setColor(cColor[1]);	
		iEventLineStartY += 6;
		g.drawLine(iPaddingLeft+5, (iHeight-iPaddingBottom + iEventLineStartY) + iEventYStep * 0, iPaddingLeft+5+iDatasetsLength*iXStep, (iHeight-iPaddingBottom + iEventLineStartY) + iEventYStep * 0);		
		g.fillOval(iPaddingLeft + 5,(iHeight-iPaddingBottom + iLegendOffset+iLegendStepY), 4,4);
		g.drawString("- E_SHOPORDERARRIVED", iPaddingLeft + 14, (iHeight-iPaddingBottom + iLegendOffset+iLegendStepY) + 6);
		iLegendStepY +=15;
		g.setColor(cColor[2]);	
		iEventLineStartY += 6;
		g.drawLine(iPaddingLeft+5, (iHeight-iPaddingBottom + iEventLineStartY) + iEventYStep * 0, iPaddingLeft+5+iDatasetsLength*iXStep, (iHeight-iPaddingBottom + iEventLineStartY) + iEventYStep * 0);		
		g.fillOval(iPaddingLeft + 5,(iHeight-iPaddingBottom + iLegendOffset+iLegendStepY), 4,4);
		g.drawString("- E_ORDERFULFILCOMLETE", iPaddingLeft + 14, (iHeight-iPaddingBottom + iLegendOffset+iLegendStepY) + 6);
		iLegendStepY +=15;
		g.setColor(cColor[3]);	
		iEventLineStartY += 6;
		g.drawLine(iPaddingLeft+5, (iHeight-iPaddingBottom + iEventLineStartY) + iEventYStep * 0, iPaddingLeft+5+iDatasetsLength*iXStep, (iHeight-iPaddingBottom + iEventLineStartY) + iEventYStep * 0);		
		g.fillOval(iPaddingLeft + 5,(iHeight-iPaddingBottom + iLegendOffset+iLegendStepY), 4,4);
		g.drawString("- E_STORECHECKSTOCK", iPaddingLeft + 14, (iHeight-iPaddingBottom + iLegendOffset+iLegendStepY) + 6);
		iLegendStepY +=15;
		g.setColor(cColor[4]);	
		iEventLineStartY += 6;
		g.drawLine(iPaddingLeft+5, (iHeight-iPaddingBottom + iEventLineStartY) + iEventYStep * 0, iPaddingLeft+5+iDatasetsLength*iXStep, (iHeight-iPaddingBottom + iEventLineStartY) + iEventYStep * 0);		
		g.fillOval(iPaddingLeft + 5,(iHeight-iPaddingBottom + iLegendOffset+iLegendStepY), 4,4);
		g.drawString("- E_STOREORDERARRIVED", iPaddingLeft + 14, (iHeight-iPaddingBottom + iLegendOffset+iLegendStepY) + 6);
		iLegendStepY +=15;
		g.setColor(cColor[5]);	
		iEventLineStartY += 6;
		g.drawLine(iPaddingLeft+5, (iHeight-iPaddingBottom + iEventLineStartY) + iEventYStep * 0, iPaddingLeft+5+iDatasetsLength*iXStep, (iHeight-iPaddingBottom + iEventLineStartY) + iEventYStep * 0);		
		g.fillOval(iPaddingLeft + 5,(iHeight-iPaddingBottom + iLegendOffset+iLegendStepY), 4,4);
		g.drawString("- E_STORETRUCKARRIVED", iPaddingLeft + 14, (iHeight-iPaddingBottom + iLegendOffset+iLegendStepY) + 6);
		
		
		// Drawing Events.
		for (i=0; i<iDatasetsLength; i++) {
			g.setColor(new Color(0,0,0));
			g.drawLine(iPaddingLeft + 5 + i * iXStep, (iHeight-iPaddingBottom + 32) + iEventYStep, iPaddingLeft + 5 + i * iXStep, (iHeight-iPaddingBottom + 48) + iEventYStep * 5);							
			for (j=0; j<6; j++) {
				if (baEventsSet[i][j]) {
					g.setColor(cColor[j]);	
					g.fillOval(iPaddingLeft + 5 + i * iXStep-3,(iHeight-iPaddingBottom + 40) + iEventYStep * j, 7,7);
				}
			}
		}
		
	}
	
	public void mSetBounds(int iX, int iY, int iHeight) {
		this.iX = iX;
		this.iY = iY;
		this.iWidth = iPaddingLeft + 5 + iDatasetsLength * iXStep + iPaddingRight;
		this.iHeight = iHeight;
		setBounds(iX, iY, iWidth, iHeight);
		setPreferredSize(new Dimension(iWidth, iHeight));
		iYValCount = (int) ((iHeight - (iPaddingTop + iPaddingBottom + 5)) / iYStep);
		iMultiplier = iYValCount * iYStep;
	}
	
	public void mSetData(int iSet, int iIndex, int iValue) {
		daDatasetX[iIndex][iSet] = iValue;
	}
	
	public void mSetEvent(int iIndex, int iType, boolean bValue) {
		baEventsSet[iIndex][iType-1] = bValue;
	}
	
	public void mNormalize(int iSet) {
		double iMax = 0 ;
		for (i=0; i<iDatasetsLength; i++) {
			if (daDatasetX[i][iSet] > iMax) iMax = daDatasetX[i][iSet];
		}
		
		for (i=0; i<iDatasetsLength; i++) {
			daDatasetX[i][iSet+iDataSetsCount] = daDatasetX[i][iSet]/iMax;
		}
	}
};


///////////////

/***********************************************************
Title: CEventStack - Event stack object class.
Author: Algirdas Noreika; norealgi@soften.ktu.lt
Date created: 2004-02-21
Description & Comments:
	- Event stack for all events - shops or stores.
***********************************************************/

class CEventStack {
	// PROPERTIES
	public CEvent cEvent;	// - Event.
	public static final int E_SHOPCHECKSTOCK = 1;
	public static final int E_SHOPORDERARRIVED = 2;
	public static final int E_ORDERFULFILCOMLETE = 3;
	public static final int E_STORECHECKSTOCK = 4;
	public static final int E_STOREORDERARRIVED = 5;	
	public static final int E_STORETRUCKARRIVED = 6;	
	Hashtable iaTime = new Hashtable();
	int i;
	
	public void mReset() {
		this.iaTime = null;
		this.iaTime = new Hashtable();	
	}
	// METHODS
	public void mPop(int iTimeTick){		// - Retrieves event for Time tick.
		ArrayList alStack = null;
		alStack = (ArrayList) iaTime.remove(new Integer(iTimeTick));
		boolean bEventFound;
		cEvent = null;
		if (alStack != null) {
			if (alStack.size() > 0 ) {
				// E_STORECHECKSTOCK Priority 1
				bEventFound = false;
				for (i=0; i<alStack.size(); i++) {		
					cEvent = (CEvent) alStack.get(i);
					if (cEvent.iAction == E_STORECHECKSTOCK) {
						cEvent = (CEvent) alStack.remove(i);
						bEventFound = true;
						break;
					}
				}
				
				// E_STOREORDERARRIVED Priority 2
				if (bEventFound == false) {
					bEventFound = false;
					for (i=0; i<alStack.size(); i++) {		
						cEvent = (CEvent) alStack.get(i);
						if (cEvent.iAction == E_STOREORDERARRIVED) {
							cEvent = (CEvent) alStack.remove(i);
							bEventFound = true;
							break;
						}
					}
				}
				
				// E_STORETRUCKARRIVED Priority 3
				if (bEventFound == false) {
					bEventFound = false;
					for (i=0; i<alStack.size(); i++) {		
						cEvent = (CEvent) alStack.get(i);
						if (cEvent.iAction == E_STORETRUCKARRIVED) {
							cEvent = (CEvent) alStack.remove(i);
							bEventFound = true;
							break;
						}
					}
				}
				
				// E_ORDERFULFILCOMLETE Priority 4
				if (bEventFound == false) {
					bEventFound = false;
					for (i=0; i<alStack.size(); i++) {		
						cEvent = (CEvent) alStack.get(i);
						if (cEvent.iAction == E_ORDERFULFILCOMLETE) {
							cEvent = (CEvent) alStack.remove(i);
							bEventFound = true;
							break;
						}
					}
				}
				
				// E_SHOPORDERARRIVED Priority 5
				if (bEventFound == false) {
					bEventFound = false;
					for (i=0; i<alStack.size(); i++) {		
						cEvent = (CEvent) alStack.get(i);
						if (cEvent.iAction == E_SHOPORDERARRIVED) {
							cEvent = (CEvent) alStack.remove(i);
							bEventFound = true;
							break;
						}
					}
				}
				
				// E_SHOPCHECKSTOCK Priority 6
				if (bEventFound == false) {
					for (i=0; i<alStack.size(); i++) {		
						cEvent = (CEvent) alStack.get(i);
						if (cEvent.iAction == E_SHOPCHECKSTOCK) {
							cEvent = (CEvent) alStack.remove(i);
							break;
						}
					}
				}
				iaTime.put(new Integer(iTimeTick), alStack);
			} else {
				iaTime.remove(new Integer(iTimeTick));
				cEvent = null;
			}
		} else {
			cEvent = null;
		}
	}
	
	public void mPush(int iTimeTick, int iObjID, int iObjectType, int iAction, int iValue){	// - Places an event for Time tick.
		CEvent cE = new CEvent(iObjID, iObjectType, iAction, iValue);
		ArrayList alStack = null;
		alStack = (ArrayList) iaTime.remove(new Integer(iTimeTick));
		if (alStack == null) alStack = new ArrayList();
		alStack.add(cE);
		iaTime.put(new Integer(iTimeTick), alStack);
	}
};




class CEvent {
	public int iObjID;
	public int iObjType;
	public int iAction;
	public int iValue;
	
	CEvent (int iObjectID, int iObjectType, int iAct, int iVal) {
		this.iObjID = iObjectID;
		this.iObjType = iObjectType;
		this.iAction = iAct;
		this.iValue = iVal;
	}
		
		
		
};
//////////////////////


/***********************************************************
Title: CModelManager - Model management object class.
Author:	Algirdas Noreika; norealgi@soften.ktu.lt
Date created: 2004-02-21
Description & Comments:
	- Performs all model data management operations.
***********************************************************/


class CModelManager {
	public CShop[] cShop;
	public CStore[] cStore;
	public static final int ID_SHOP = 0; // - Shop identifier.
	public static final int ID_STORE = 1; // - Store identifier.	
	
	// METHODS
	public void mSave(){			// - Saves data into file.
	}
	
	public void mLoad(){			// - Loads simulation data from file. 
	}
	
	public void mReset() {		// - Resets all data structures..
	}
	
	public void mSetShopCount(int iCount) { // - Sets model shop count.
		cShop = new CShop[iCount];
		
		for(int i=0; i<iCount; i++) {
			cShop[i] = new CShop();
		}
	}

	public void mSetStoreCount(int iCount) { // - Sets model stores count.
		cStore = new CStore[iCount];
		
		for(int i=0; i<iCount; i++) {
			cStore[i] = new CStore();
		}
	}
	
	public void mSetStoreTruckCount(int iStoreID, int iCount) { // - Sets model stores count.
		cStore[iStoreID].mSetTruckCount(iCount);
	}
	
	public void mAssignShop(int iShopID, int iSupplierID) { // - Assigns a shop to store ID.
		cShop[iShopID].iSupplierID = iSupplierID;
	}
	
	public void mAssignStore(int iStoreID, int iSupplierID) { // - Assigns a Store to store ID.
		cStore[iStoreID].iSupplierID = iSupplierID;
	}
};


////////////////

/***********************************************************
Title: CShop - Shop object class.
Author:	Algirdas Noreika; norealgi@soften.ktu.lt
Date created: 2004-02-21
Description & Comments:
	- Performs shop simulation.
***********************************************************/


class CShop extends CStock {
	public int iDemandPerDay;	// - Product demand per day average. (Products amount within 24 hours.)
	public int iDemand; // - Current demand.
	
	
	// METHODS
	public void mSell(int iCount){		// - Performs sell operation. It means, decreases product count.
		iProductAmountAvailable = iProductAmountAvailable - iCount;
		if (iProductAmountAvailable < 0 ) { 
			iProductLack += (-iProductAmountAvailable); // Trukstamu prekiu kiekis padideja.
			iProductAmountAvailable = 0; // Likutis prilyginamas 0.
		}
	}
	
	public int mHourDemand() {	// - Calculates current hour demand.
		iDemand = (int) Math.round(Math.random() * (iDemandPerDay/24)*2)+1;
		return iDemand;
	}
};




//////////////////////


/***********************************************************
Title: CStock - Stock object class.
Author:	Algirdas Noreika; norealgi@soften.ktu.lt
Date created: 2004-02-21
Description & Comments:
	- A stock object for shops and stores.
***********************************************************/


class CStock {
	public int iProductAmountAvailable;	// - Product amount available in stock.
	public int iProductAmountMargin;		// - Product amount margin - when it's reached - do some actions.
	public int iStockCheckPeriod;			// - Stock check period in hours.
	public int iProductAmountPerOrder;		// - Uzsakomu prekiu kiekis.
	public int iProductLack;				// - Product lack in a stock. (T(to))
	public int iProductsOrdered;			// - Ordered product amount P(to).
	public int iSupplierID = -1;					// - SupplierID - where to get products. If negative is unlimited supplier by default.
	public int iTruckPassagePeriod;			// - How long it takes for truck to arrive to this stock.
	public int iTruckReturnPeriod;			// - How long it takes for truck to return to store.
	

	// METHODS
	public void mOrder(){	// - Performs Order Action.	
		iProductsOrdered += iProductAmountPerOrder;
	}
};




//////////////////////



/***********************************************************
Title: CStore - Store object class.
Author:	Algirdas Noreika; norealgi@soften.ktu.lt
Date created: 2004-02-21
Description & Comments:
	- Performs store simulation.
***********************************************************/

class CStore extends CStock {
	public ArrayList iaOrdersQueue;	// - Array for clients orders queue. //Could be a dynamic array like list. Works in FIFO method.
	public int iTruckCountTotal, iTruckCountAvailable;  // - Trucks available in Store for orders.
	public ArrayList iaWaitTruckQueue; // - Array of orders which waits for truck.
	public COrder cOrd;
	public COrder cCurrentOrd, cTransportOrd;
	public int iOrderFulfillPeriod; 
	public boolean bOrderFulfilProgressOn = false; // - Jeigu vykdomas uzsakymas.
	public boolean bInitOrderFulfil = false;	// Pradedamas vykdyti uzsakymas.
	public boolean bTruckReady = false;		// Masina pasiruosusi.
	public boolean bWaitingForProductsFromSupplier = false;
	
	
	CStore () { // Constructor. Initialising objects.
		iaOrdersQueue = new ArrayList();
		iaWaitTruckQueue = new ArrayList();
	}
	
	public void mSetTruckCount(int iCount) {	// - Setting truck count.
		iTruckCountAvailable = iTruckCountTotal = iCount;
	}
	
	// Placing order into orders Queue.
	public void mPushTruckWaitQueue(COrder cOrder) { // - These orders wait for truck.
		iaWaitTruckQueue.add(cOrder);	
	}
	
	public void mAcomplishOrder(COrder cOrder) { // - Acomplish order.
		// Order fulfil Complete, so placing it into truck.
		if (iTruckCountAvailable > 0) {	// Laisvu masinu yra. Galimas prekiu pervezimas.
			if (iTruckCountTotal >= 99999) {iTruckCountAvailable = 99999;} // Unlimited truck count.
			iTruckCountAvailable--;
			bTruckReady = true;
		} else {
			mPushTruckWaitQueue(cOrder);	// Jei laisvu masinu nera - apdorotas uzsakymas statomas i eile.
		}
		
		//bOrderFulfilProgressOn = false;
	}
	
	// - Now truck available. If there are some orders waiting for truck we can proceed it.
	public void mTransportNextOrder() {
		if (iaWaitTruckQueue.size()>0) {
			cTransportOrd = (COrder) iaWaitTruckQueue.remove(0);
		} else {
			cTransportOrd = null;
		}
	}
	
	
	public void	mProceedNextOrder() { // Proceeds next order in a queue.
		// Retrieving next.
		mGetFirstOrder();
		if (cOrd != null) {
			//System.out.println("Retrieve ORDER inside: Client["+cOrd.iClientID+"]");
			mFulfillOrder(cOrd.iClientID, cOrd.iClientType, cOrd.iAmount);
		} else {
			
		}
	}
	
	// - Fullfills Order or Places Order into an order queue.
	public void mFulfillOrder(int iClientID, int iClientType, int iAmount) { // - Uzsakymo gavimas ish uzsakovo (e'1...e'M+L).
		if ( !bOrderFulfilProgressOn ) {
			if (iProductAmountAvailable >= 99999) {iProductAmountAvailable = iProductAmountAvailable + iAmount;} // Unlimited Product Amount Avilable.
			iProductAmountAvailable = iProductAmountAvailable - iAmount;
			
			
			
			if (iProductAmountAvailable < 0 ) { 
				iProductLack += (-iProductAmountAvailable); // Trukstamu prekiu kiekis padideja.
				//iProductAmountAvailable = 0; // Likutis prilyginamas 0.
				
				// (Store 3) If no more available products - order goes into orders queue.
				mPushOrder(iClientID, iClientType, (-iProductAmountAvailable));
				iAmount+= iProductAmountAvailable;
				iProductAmountAvailable = 0;
			}
			
			if (iProductAmountAvailable + iAmount > 0) { // If no any other order fulfil and products available - fulfiling request.
				//System.out.println("FULFILING ORDER INSIDE: Client-" +iClientID + " iClientType-"+iClientType+ " iAmount-"+iAmount);
				bOrderFulfilProgressOn = true;
				bInitOrderFulfil = true;
				
				cCurrentOrd = new COrder(iClientID, iClientType, iAmount);
			}
			
			
		} else { // - (Store 1).
			mPushOrder(iClientID, iClientType, iAmount);
		}
	}
	
	// - Retrieves order from a order queue.
	public void mGetFirstOrder() {
		cOrd = null;
		if (iaOrdersQueue.size()>0) {
			//System.out.println("Retrieving order from Queue");
			cOrd = (COrder) iaOrdersQueue.remove(0);
			//System.out.println("OrderClient:"+cOrd.iClientID);
		} else {
			cOrd = null;
		}
	}
	
	// Placing order into orders Queue.
	public void mPushOrder(int iClientID, int iClientType, int iAmount) {
			//System.out.println("Pushing Order into Queue: ID["+iClientID +"] Type["+iClientType+"] iAmount["+iAmount+"]");
			cOrd = new COrder(iClientID, iClientType, iAmount);
			iaOrdersQueue.add(cOrd);
	}
}

// - Class describing a single order.
class COrder {
	public int iClientID;
	public int iClientType;
	public int iAmount;	
	
	public COrder(int iClntID, int iClntType, int iAmnt) {
		iClientID = iClntID;
		iClientType = iClntType;
		iAmount = iAmnt;
	}
};

//////////////////


/***********************************************************
Title: CTimeLine - Time operations object class.
Author: Algirdas Noreika; norealgi@soften.ktu.lt
Date created: 2004-02-21
Description & Comments:
	- Performs Time management.
***********************************************************/

class CTimeLine {
	public int iTime; 		// - Time in hours (=0) on inicialization and reset.
	public int iEndTime; 	// - End time in hours. 
	public CEventStack cEvtStack;
	public CModelManager cMMan;
	private int iTickerControl;		// - (0)run;(1)Stop;(2)Pause; 
	private CLMA cLM;
	private CLMOptimizer cLMOpt;
	//public FileOutputStream cFileOS;
	
	// METHODS
	CTimeLine(CLMA cLM){	// - Constructor.
		this.cLM = cLM;
		this.cLMOpt = null;
		cEvtStack = new CEventStack();
		cMMan = new CModelManager();
	}
	
	CTimeLine(CLMOptimizer cLMOpt){	// - Constructor.
		this.cLMOpt = cLMOpt;
		this.cLM = null;
		cEvtStack = new CEventStack();
		cMMan = new CModelManager();
	}
	
	public void mInitialize(){	// - Initilizes timeline.
		//try {
			iTime = 0;
			iTickerControl = 1;
			cEvtStack.mReset();
			//System.out.println("Creating log file...");
			//cFileOS = new FileOutputStream("clm.log", false);
			//cFileOS.write(new String("--- Logistics Model log file ---\n").getBytes());
			//cDataLog = new CDataLogger(cMMan.cShop.length, cMMan.cStore.length); // Logging initial Shops and stores count.
			//cFileOS.write(new String("Shops Total: " + cMMan.cShop.length+"\n").getBytes());
			//for (int iIndex=0; iIndex < cMMan.cShop.length; iIndex++) {
			//	cFileOS.write(new String("Shop["+iIndex+"] Amount limit: " + cMMan.cShop[iIndex].iProductAmountMargin+"\n").getBytes());
			//	cFileOS.write(new String("Shop["+iIndex+"] Order amount: " + cMMan.cShop[iIndex].iProductAmountPerOrder+"\n").getBytes());
			//}
			//cFileOS.write(new String("Stores Total: " + cMMan.cStore.length + ". Last Store - Supplier with unlimited product count.\n").getBytes());
			//cFileOS.write(new String("Simulation period: " + iEndTime + " hours ("+ iEndTime/24 +" day's)\n").getBytes());
			
		//} catch (FileNotFoundException e) {
		//} catch (IOException e) {}
		
		
		
	}
	
	public void mStart(){		// - Starts time counter.
		iTickerControl = 0;
		mTicker();
	}
	
	public void mStop(){			// - Stops time counter. 
		mInitialize();
	}
	
	public void mPause(){		// - Pauses a time counter.
		iTickerControl = 2;
	}
	
	public void mTicker(){		// - A ticker loop.
			//System.out.println("Starting Simulation.");
			while(iTickerControl == 0 && iTime < iEndTime) {
				iTime++;
				mTickAction();
			}
		
			//cFileOS.close();
			//System.out.println("Simulation Complete.");
	}
	
	// - One single time tick action method.
	public void mTickAction() {	// - Performs action available in event stack for current time tick.
		if (this.cLM != null) this.cLM.mTickInit();	
		if (this.cLMOpt != null) this.cLMOpt.mTickInit();	
		
		// - Performing sales;
		for (int iIndex=0; iIndex<cMMan.cShop.length; iIndex++) {
			int iBuy = cMMan.cShop[iIndex].mHourDemand();
			cMMan.cShop[iIndex].mSell(iBuy); // Selling a product.
			//cFileOS.write(new String("TICK["+iTime+"]: - Brought from shop ["+iIndex+"]:" + iBuy + " Available:" + cMMan.cShop[iIndex].iProductAmountAvailable+"\n").getBytes());
			//if (cMMan.cShop[iIndex].iProductAmountAvailable == 0) cFileOS.write(new String("     - !!! --- WARNING ! - Low products on shop "+iIndex+" --- !!!\n").getBytes());
		}
		
		// - Checking events. If there are some events available - proceed them.
		cEvtStack.mPop(iTime);
		while (cEvtStack.cEvent != null) {
			mProcessEvent();
			cEvtStack.mPop(iTime);
		}
		//System.out.println("\n");
		if (this.cLM != null) this.cLM.mTickComplete();	
		if (this.cLMOpt != null) this.cLMOpt.mTickComplete();	
	}
	
	
	// - Mehod that processes all time events from event stack.
	public void mProcessEvent() {	// - Processes event stack events.
	
// E_SHOPCHECKSTOCK.
		if (cEvtStack.cEvent.iAction == cEvtStack.E_SHOPCHECKSTOCK) {	// - Tikrinama ar prekiu kiekis ne didesnis uz nustatyta riba.
			if (this.cLM != null) this.cLM.mEventOccured(cEvtStack.cEvent.iAction, cEvtStack.cEvent.iObjID, cEvtStack.cEvent.iObjType, cEvtStack.cEvent.iValue);
			if (this.cLMOpt != null) this.cLMOpt.mEventOccured(cEvtStack.cEvent.iAction, cEvtStack.cEvent.iObjID, cEvtStack.cEvent.iObjType, cEvtStack.cEvent.iValue);
			
			if (cMMan.cShop[cEvtStack.cEvent.iObjID].iProductAmountAvailable < cMMan.cShop[cEvtStack.cEvent.iObjID].iProductAmountMargin) {
				// - Tokiu atveju Sukuriamas uzsakymas.
				cMMan.cShop[cEvtStack.cEvent.iObjID].mOrder();
				if (this.cLM != null) this.cLM.mEventAction("Shop["+cEvtStack.cEvent.iObjID+"]: Ordering products amount["+cMMan.cShop[cEvtStack.cEvent.iObjID].iProductAmountPerOrder+"] in store "+cMMan.cShop[cEvtStack.cEvent.iObjID].iSupplierID);
				if (this.cLM != null) this.cLM.mEventAction("Shop["+cEvtStack.cEvent.iObjID+"]: Total products ordered:["+cMMan.cShop[cEvtStack.cEvent.iObjID].iProductsOrdered+"]");
				if (this.cLM != null) this.cLM.mEventAction("Store["+cMMan.cShop[cEvtStack.cEvent.iObjID].iSupplierID+"]: Recieved an order from shop "+cEvtStack.cEvent.iObjID);
				//cFileOS.write(new String("     - Shop["+cEvtStack.cEvent.iObjID+"]: Ordering products amount["+cMMan.cShop[cEvtStack.cEvent.iObjID].iProductAmountPerOrder+"] in store "+cMMan.cShop[cEvtStack.cEvent.iObjID].iSupplierID+"\n").getBytes());
				//cFileOS.write(new String("     - Shop["+cEvtStack.cEvent.iObjID+"]: Total products ordered:["+cMMan.cShop[cEvtStack.cEvent.iObjID].iProductsOrdered+"]\n").getBytes());
				//cFileOS.write(new String("     - Store["+cMMan.cShop[cEvtStack.cEvent.iObjID].iSupplierID+"]: Recieved an order from shop "+cEvtStack.cEvent.iObjID+"\n").getBytes());
				
				// Sandelys gauna uzsakyma.
				cMMan.cStore[cMMan.cShop[cEvtStack.cEvent.iObjID].iSupplierID].mFulfillOrder(cEvtStack.cEvent.iObjID, cEvtStack.cEvent.iObjType, cMMan.cShop[cEvtStack.cEvent.iObjID].iProductAmountPerOrder);
				// Adding event to check if is here enougt products in store stock.
				if (!cMMan.cStore[cMMan.cShop[cEvtStack.cEvent.iObjID].iSupplierID].bWaitingForProductsFromSupplier)
					cEvtStack.mPush(iTime, cMMan.cShop[cEvtStack.cEvent.iObjID].iSupplierID, cMMan.ID_STORE, cEvtStack.E_STORECHECKSTOCK,0);					
				
				if ( cMMan.cStore[cMMan.cShop[cEvtStack.cEvent.iObjID].iSupplierID].bInitOrderFulfil ) {
					cMMan.cStore[cMMan.cShop[cEvtStack.cEvent.iObjID].iSupplierID].bInitOrderFulfil = false;
					cEvtStack.mPush(iTime + cMMan.cStore[cMMan.cShop[cEvtStack.cEvent.iObjID].iSupplierID].iOrderFulfillPeriod,
						cMMan.cShop[cEvtStack.cEvent.iObjID].iSupplierID,
						cMMan.ID_STORE,
						cEvtStack.E_ORDERFULFILCOMLETE, cEvtStack.cEvent.iObjID);
					if (this.cLM != null) this.cLM.mEventAction("Store["+cMMan.cShop[cEvtStack.cEvent.iObjID].iSupplierID+"]: Fulfiling order. Will be acomplished after "+cMMan.cStore[cMMan.cShop[cEvtStack.cEvent.iObjID].iSupplierID].iOrderFulfillPeriod+" hour's");						
					
					//System.out.println("Tick:["+iTime+"] FULFILING ORDER: Client-" +cEvtStack.cEvent.iObjID + " iClientType-"+cEvtStack.cEvent.iObjType+ " iAmount-"+cMMan.cShop[cEvtStack.cEvent.iObjID].iProductAmountPerOrder);
					//cFileOS.write(new String("     - Store["+cMMan.cShop[cEvtStack.cEvent.iObjID].iSupplierID+"]: Fulfiling order. Will be acomplished after "+cMMan.cStore[cMMan.cShop[cEvtStack.cEvent.iObjID].iSupplierID].iOrderFulfillPeriod+" hour's \n").getBytes());
				} else {
					if (this.cLM != null) this.cLM.mEventAction("Store["+cMMan.cShop[cEvtStack.cEvent.iObjID].iSupplierID+"]: Order pushed into queue for ID["+cEvtStack.cEvent.iObjID+"]");
					//cFileOS.write(new String("     - Store["+cMMan.cShop[cEvtStack.cEvent.iObjID].iSupplierID+"]: Order pushed into queue for ID["+cEvtStack.cEvent.iObjID+"]\n").getBytes());
				}
			} else {
				cEvtStack.mPush((iTime + cMMan.cShop[cEvtStack.cEvent.iObjID].iStockCheckPeriod), cEvtStack.cEvent.iObjID, cEvtStack.cEvent.iObjType, cEvtStack.E_SHOPCHECKSTOCK,0);
			}
			
// E_SHOPORDERARRIVED.	
		} else if (cEvtStack.cEvent.iAction == cEvtStack.E_SHOPORDERARRIVED ) { // - H(e'1) - prekes is sandelio pristatytos i parduotuve.
			if (this.cLM != null) this.cLM.mEventOccured(cEvtStack.cEvent.iAction, cEvtStack.cEvent.iObjID, cEvtStack.cEvent.iObjType, cEvtStack.cEvent.iValue);
			if (this.cLMOpt != null) this.cLMOpt.mEventOccured(cEvtStack.cEvent.iAction, cEvtStack.cEvent.iObjID, cEvtStack.cEvent.iObjType, cEvtStack.cEvent.iValue);			
			
			// - Padidinamas prekiu kiekis parduotuveje.						// - (e"i) - sandeliui, uzsakymo apdorojimo pabaiga. i-oji masina pristato prekes uzsakovui.
			cMMan.cShop[cEvtStack.cEvent.iObjID].iProductAmountAvailable += cEvtStack.cEvent.iValue;
			// - Sumazinamas uzsakytas prekiu kiekis.
			cMMan.cShop[cEvtStack.cEvent.iObjID].iProductsOrdered -= cEvtStack.cEvent.iValue;
			// - Sumazinamas prekiu trukumas. (Nera specifikacijoje.)
			// Pastaba 2004-04-14. Prekiu trukumas nekeiciamas.
			cMMan.cShop[cEvtStack.cEvent.iObjID].iProductLack = 0;			
			
			// -Returning truck to store.
			cEvtStack.mPush((iTime + cMMan.cShop[cEvtStack.cEvent.iObjID].iTruckReturnPeriod),
				cMMan.cShop[cEvtStack.cEvent.iObjID].iSupplierID,
				cMMan.ID_STORE,
				cEvtStack.E_STORETRUCKARRIVED, 0);
				
			cEvtStack.mPush((iTime + cMMan.cShop[cEvtStack.cEvent.iObjID].iStockCheckPeriod), cEvtStack.cEvent.iObjID, cEvtStack.cEvent.iObjType, cEvtStack.E_SHOPCHECKSTOCK,0);
				
			
// E_ORDERFULFILCOMLETE			
		} else if (cEvtStack.cEvent.iAction == cEvtStack.E_ORDERFULFILCOMLETE ) { // Order Fulfil complete
			if (this.cLM != null) this.cLM.mEventOccured(cEvtStack.cEvent.iAction, cEvtStack.cEvent.iObjID, cEvtStack.cEvent.iObjType, cEvtStack.cEvent.iValue);
			if (this.cLMOpt != null) this.cLMOpt.mEventOccured(cEvtStack.cEvent.iAction, cEvtStack.cEvent.iObjID, cEvtStack.cEvent.iObjType, cEvtStack.cEvent.iValue);			
			
			// Tas pats Order fulfill ir store ir SHOP !!!!
			cMMan.cStore[cEvtStack.cEvent.iObjID].bOrderFulfilProgressOn = false;
			//System.out.println("Tick:["+iTime+"] ORDER FULFIL COMPLETE: ObjID-" +cMMan.cStore[cEvtStack.cEvent.iObjID].cCurrentOrd.iClientID + " iClientType-"+cMMan.cStore[cEvtStack.cEvent.iObjID].cCurrentOrd.iClientType+ " iAmount-"+cMMan.cStore[cEvtStack.cEvent.iObjID].cCurrentOrd.iAmount);
			cMMan.cStore[cEvtStack.cEvent.iObjID].mAcomplishOrder(cMMan.cStore[cEvtStack.cEvent.iObjID].cCurrentOrd);
			if (cMMan.cStore[cEvtStack.cEvent.iObjID].bTruckReady) {
				if ( cMMan.cStore[cEvtStack.cEvent.iObjID].cCurrentOrd.iClientType == cMMan.ID_SHOP ) {
					if (this.cLM != null) this.cLM.mEventAction("Store["+cEvtStack.cEvent.iObjID+"]: Order Accomplished. Truck ready to carry products to shop ID["+cMMan.cStore[cEvtStack.cEvent.iObjID].cCurrentOrd.iClientID+"]");
					//cFileOS.write(new String("     - Store["+cEvtStack.cEvent.iObjID+"]: Order Accomplished. Truck ready to carry products to shop ID["+cMMan.cStore[cEvtStack.cEvent.iObjID].cCurrentOrd.iClientID+"]\n").getBytes());
					
					cEvtStack.mPush((iTime + cMMan.cShop[cMMan.cStore[cEvtStack.cEvent.iObjID].cCurrentOrd.iClientID].iTruckPassagePeriod),		
						cMMan.cStore[cEvtStack.cEvent.iObjID].cCurrentOrd.iClientID,
						cMMan.ID_SHOP,
						cEvtStack.E_SHOPORDERARRIVED, cMMan.cStore[cEvtStack.cEvent.iObjID].cCurrentOrd.iAmount);
				} else if ( cMMan.cStore[cEvtStack.cEvent.iObjID].cCurrentOrd.iClientType == cMMan.ID_STORE ){
					if (this.cLM != null) this.cLM.mEventAction("Store["+cEvtStack.cEvent.iObjID+"]: Order Accomplished. Truck ready to carry products to store ID["+cMMan.cStore[cEvtStack.cEvent.iObjID].cCurrentOrd.iClientID+"]");
					//cFileOS.write(new String("     - Store["+cEvtStack.cEvent.iObjID+"]: Order Accomplished. Truck ready to carry products to store ID["+cMMan.cStore[cEvtStack.cEvent.iObjID].cCurrentOrd.iClientID+"]\n").getBytes());
					
					cEvtStack.mPush((iTime + cMMan.cStore[cMMan.cStore[cEvtStack.cEvent.iObjID].cCurrentOrd.iClientID].iTruckPassagePeriod),		
						cMMan.cStore[cEvtStack.cEvent.iObjID].cCurrentOrd.iClientID,
						cMMan.ID_STORE,
						cEvtStack.E_STOREORDERARRIVED, cMMan.cStore[cEvtStack.cEvent.iObjID].cCurrentOrd.iAmount);
				}
			}
			// Check and proceed next order if available.
			//System.out.println("Tick:["+iTime+"] E_ORDERFULFILCOMLETE_ TRYING TO RETRIEVE NEXT ORDER");			
			cMMan.cStore[cEvtStack.cEvent.iObjID].mProceedNextOrder();
			
			if ( cMMan.cStore[cEvtStack.cEvent.iObjID].bInitOrderFulfil ) {
				cMMan.cStore[cEvtStack.cEvent.iObjID].bInitOrderFulfil = false;
				cEvtStack.mPush(iTime + cMMan.cStore[cEvtStack.cEvent.iObjID].iOrderFulfillPeriod,
					cEvtStack.cEvent.iObjID,
					cMMan.ID_STORE,
					cEvtStack.E_ORDERFULFILCOMLETE, cMMan.cStore[cEvtStack.cEvent.iObjID].cCurrentOrd.iAmount);
				if (this.cLM != null) this.cLM.mEventAction("Store["+cEvtStack.cEvent.iObjID+"]: Fulfiling order. Will be acomplished after "+cMMan.cStore[cEvtStack.cEvent.iObjID].iOrderFulfillPeriod+" hour's");						

				// Adding event to check if is here enougt products in store stock.
				if (!cMMan.cStore[cEvtStack.cEvent.iObjID].bWaitingForProductsFromSupplier)
					cEvtStack.mPush(iTime, cEvtStack.cEvent.iObjID, cMMan.ID_STORE, cEvtStack.E_STORECHECKSTOCK,0);					
								
				//System.out.println("Tick:["+iTime+"] E_ORDERFULFILCOMLETE_FULFILING ORDER: Client-" +cMMan.cStore[cEvtStack.cEvent.iObjID].cCurrentOrd.iClientID + " iClientType-"+cMMan.cStore[cEvtStack.cEvent.iObjID].cCurrentOrd.iClientType+ " iAmount-"+cMMan.cStore[cEvtStack.cEvent.iObjID].cCurrentOrd.iAmount);
			} else {
				if (this.cLM != null && cMMan.cStore[cEvtStack.cEvent.iObjID].cOrd != null) this.cLM.mEventAction("Store["+cEvtStack.cEvent.iObjID+"]: Order pushed into queue for ID["+cMMan.cStore[cEvtStack.cEvent.iObjID].cOrd.iClientID+"]");
				//cFileOS.write(new String("     - Store["+cMMan.cShop[cEvtStack.cEvent.iObjID].iSupplierID+"]: Order pushed into queue for ID["+cEvtStack.cEvent.iObjID+"]\n").getBytes());
			}
			
			
// E_STORECHECKSTOCK	
		} else if (cEvtStack.cEvent.iAction == cEvtStack.E_STORECHECKSTOCK ) { // Store checks if there is enought goods.
			if (this.cLM != null) this.cLM.mEventOccured(cEvtStack.cEvent.iAction, cEvtStack.cEvent.iObjID, cEvtStack.cEvent.iObjType, cEvtStack.cEvent.iValue);
			if (this.cLMOpt != null) this.cLMOpt.mEventOccured(cEvtStack.cEvent.iAction, cEvtStack.cEvent.iObjID, cEvtStack.cEvent.iObjType, cEvtStack.cEvent.iValue);
			
			if (cMMan.cStore[cEvtStack.cEvent.iObjID].iProductAmountAvailable < cMMan.cStore[cEvtStack.cEvent.iObjID].iProductAmountMargin) {
				// - Tokiu atveju Sukuriamas uzsakymas.
				cMMan.cStore[cEvtStack.cEvent.iObjID].mOrder();
				// - Siunciamas uzsakymas tiekejui.
				if (this.cLM != null) this.cLM.mEventAction("Store["+cEvtStack.cEvent.iObjID+"]: Ordering products amount["+cMMan.cStore[cEvtStack.cEvent.iObjID].iProductAmountPerOrder+"] in store "+cMMan.cStore[cEvtStack.cEvent.iObjID].iSupplierID);
				if (this.cLM != null) this.cLM.mEventAction("Store["+cEvtStack.cEvent.iObjID+"]: Total products ordered:["+cMMan.cStore[cEvtStack.cEvent.iObjID].iProductsOrdered+"]");
				if (this.cLM != null) this.cLM.mEventAction("Store or supplier["+cMMan.cStore[cEvtStack.cEvent.iObjID].iSupplierID+"]: Recieved an order from store "+cEvtStack.cEvent.iObjID);
				//cFileOS.write(new String("     - Store["+cEvtStack.cEvent.iObjID+"]: Ordering products amount["+cMMan.cStore[cEvtStack.cEvent.iObjID].iProductAmountPerOrder+"] in store "+cMMan.cStore[cEvtStack.cEvent.iObjID].iSupplierID+"\n").getBytes());
				//cFileOS.write(new String("     - Store["+cEvtStack.cEvent.iObjID+"]: Total products ordered:["+cMMan.cStore[cEvtStack.cEvent.iObjID].iProductsOrdered+"]\n").getBytes());
				//cFileOS.write(new String("     - Store or supplier["+cMMan.cStore[cEvtStack.cEvent.iObjID].iSupplierID+"]: Recieved an order from store "+cEvtStack.cEvent.iObjID+"\n").getBytes());
				
				// Sandelys gauna uzsakyma.
				cMMan.cStore[cMMan.cStore[cEvtStack.cEvent.iObjID].iSupplierID].mFulfillOrder(cEvtStack.cEvent.iObjID, cEvtStack.cEvent.iObjType, cMMan.cStore[cEvtStack.cEvent.iObjID].iProductAmountPerOrder);
				if ( cMMan.cStore[cMMan.cStore[cEvtStack.cEvent.iObjID].iSupplierID].bInitOrderFulfil ) {
					cMMan.cStore[cMMan.cStore[cEvtStack.cEvent.iObjID].iSupplierID].bInitOrderFulfil = false;
					cEvtStack.mPush(iTime + cMMan.cStore[cMMan.cStore[cEvtStack.cEvent.iObjID].iSupplierID].iOrderFulfillPeriod,
						cMMan.cStore[cEvtStack.cEvent.iObjID].iSupplierID,
						cMMan.ID_STORE,
						cEvtStack.E_ORDERFULFILCOMLETE, 0);
					if (this.cLM != null) this.cLM.mEventAction("Store["+cMMan.cStore[cEvtStack.cEvent.iObjID].iSupplierID+"]: Fulfiling order. Will be acomplished after "+cMMan.cStore[cMMan.cStore[cEvtStack.cEvent.iObjID].iSupplierID].iOrderFulfillPeriod+" hour's");
					cMMan.cStore[cEvtStack.cEvent.iObjID].bWaitingForProductsFromSupplier = true;
					//cFileOS.write(new String("Store["+cMMan.cStore[cEvtStack.cEvent.iObjID].iSupplierID+"]: Fulfiling order. Will be acomplished after "+cMMan.cStore[cMMan.cStore[cEvtStack.cEvent.iObjID].iSupplierID].iOrderFulfillPeriod+" hour's \n").getBytes());
				} else {
					if (this.cLM != null) this.cLM.mEventAction("Store["+cMMan.cStore[cEvtStack.cEvent.iObjID].iSupplierID+"]: Order pushed into queue for ID["+cEvtStack.cEvent.iObjID+"]");
					//cFileOS.write(new String("     - Store["+cMMan.cStore[cEvtStack.cEvent.iObjID].iSupplierID+"]: Order pushed into queue for ID["+cEvtStack.cEvent.iObjID+"]\n").getBytes());
				}
			//} else {
				//cEvtStack.mPush((iTime + cMMan.cStore[cEvtStack.cEvent.iObjID].iStockCheckPeriod), cEvtStack.cEvent.iObjID, cEvtStack.cEvent.iObjType, cEvtStack.E_STORECHECKSTOCK,0);
			}
			
// E_STOREORDERARRIVED	
		} else if (cEvtStack.cEvent.iAction == cEvtStack.E_STOREORDERARRIVED ) { // Store recieves an completed order for products from other store or suplier.
			if (this.cLM != null) this.cLM.mEventOccured(cEvtStack.cEvent.iAction, cEvtStack.cEvent.iObjID, cEvtStack.cEvent.iObjType, cEvtStack.cEvent.iValue);
			if (this.cLMOpt != null) this.cLMOpt.mEventOccured(cEvtStack.cEvent.iAction, cEvtStack.cEvent.iObjID, cEvtStack.cEvent.iObjType, cEvtStack.cEvent.iValue);
		
			// - Padidinamas prekiu kiekis sandelyje.						
			cMMan.cStore[cEvtStack.cEvent.iObjID].iProductAmountAvailable += cEvtStack.cEvent.iValue;
			// - Sumazinamas uzsakytas prekiu kiekis.
			cMMan.cStore[cEvtStack.cEvent.iObjID].iProductsOrdered -= cEvtStack.cEvent.iValue;
			// - Sumazinamas prekiu trukumas. (Nera specifikacijoje.)
			// Pastaba 2004-04-14. Prekiu trukumas nekeiciamas.
			cMMan.cStore[cEvtStack.cEvent.iObjID].iProductLack = 0;	
			
			// -Returning truck to store or supplier.
			cEvtStack.mPush((iTime + cMMan.cStore[cEvtStack.cEvent.iObjID].iTruckReturnPeriod),
				cMMan.cStore[cEvtStack.cEvent.iObjID].iSupplierID,
				cMMan.ID_STORE,
				cEvtStack.E_STORETRUCKARRIVED, 0);
				
			cMMan.cStore[cEvtStack.cEvent.iObjID].bWaitingForProductsFromSupplier = false;
			
			// If theres any orders available- proceed them.			
			// Check and proceed next order if available.
			//System.out.println("Tick:["+iTime+"] E_STOREORDERARRIVED_ TRYING TO RETRIEVE NEXT ORDER");			
			
			cMMan.cStore[cEvtStack.cEvent.iObjID].mProceedNextOrder();
			
			if ( cMMan.cStore[cEvtStack.cEvent.iObjID].bInitOrderFulfil ) {
				cMMan.cStore[cEvtStack.cEvent.iObjID].bInitOrderFulfil = false;
				cEvtStack.mPush(iTime + cMMan.cStore[cEvtStack.cEvent.iObjID].iOrderFulfillPeriod,
					cEvtStack.cEvent.iObjID,
					cMMan.ID_STORE,
					cEvtStack.E_ORDERFULFILCOMLETE, cMMan.cStore[cEvtStack.cEvent.iObjID].cCurrentOrd.iAmount);
				if (this.cLM != null) this.cLM.mEventAction("Store["+cEvtStack.cEvent.iObjID+"]: Fulfiling order. Will be acomplished after "+cMMan.cStore[cEvtStack.cEvent.iObjID].iOrderFulfillPeriod+" hour's");						
				
				// Adding event to check if is here enougt products in store stock.
				if (!cMMan.cStore[cEvtStack.cEvent.iObjID].bWaitingForProductsFromSupplier)
					cEvtStack.mPush(iTime, cEvtStack.cEvent.iObjID, cMMan.ID_STORE, cEvtStack.E_STORECHECKSTOCK,0);					
				
				//System.out.println("Tick:["+iTime+"] E_STOREORDERARRIVED_FULFILING ORDER: Client-" +cMMan.cStore[cEvtStack.cEvent.iObjID].cCurrentOrd.iClientID + " iClientType-"+cMMan.cStore[cEvtStack.cEvent.iObjID].cCurrentOrd.iClientType+ " iAmount-"+cMMan.cStore[cEvtStack.cEvent.iObjID].cCurrentOrd.iAmount);
			} else {
				if (this.cLM != null && cMMan.cStore[cEvtStack.cEvent.iObjID].cOrd != null) this.cLM.mEventAction("Store["+cEvtStack.cEvent.iObjID+"]: Order pushed into queue for ID["+cMMan.cStore[cEvtStack.cEvent.iObjID].cOrd.iClientID+"]");
				//cFileOS.write(new String("     - Store["+cMMan.cShop[cEvtStack.cEvent.iObjID].iSupplierID+"]: Order pushed into queue for ID["+cEvtStack.cEvent.iObjID+"]\n").getBytes());
			}
			
			//cEvtStack.mPush((iTime + cMMan.cStore[cEvtStack.cEvent.iObjID].iStockCheckPeriod), cEvtStack.cEvent.iObjID, cEvtStack.cEvent.iObjType, cEvtStack.E_STORECHECKSTOCK,0);
			
// E_STORETRUCKARRIVED			
		} else if (cEvtStack.cEvent.iAction == cEvtStack.E_STORETRUCKARRIVED ) { // Truck returned to store.
			if (this.cLM != null) this.cLM.mEventOccured(cEvtStack.cEvent.iAction, cEvtStack.cEvent.iObjID, cEvtStack.cEvent.iObjType, cEvtStack.cEvent.iValue);
			if (this.cLMOpt != null) this.cLMOpt.mEventOccured(cEvtStack.cEvent.iAction, cEvtStack.cEvent.iObjID, cEvtStack.cEvent.iObjType, cEvtStack.cEvent.iValue);
			
			cMMan.cStore[cEvtStack.cEvent.iObjID].iTruckCountAvailable++;
			cMMan.cStore[cEvtStack.cEvent.iObjID].mTransportNextOrder();
			if (cMMan.cStore[cEvtStack.cEvent.iObjID].cTransportOrd != null) { // If there is an order waiting for truck .
				cMMan.cStore[cEvtStack.cEvent.iObjID].mAcomplishOrder(cMMan.cStore[cEvtStack.cEvent.iObjID].cTransportOrd);
				if (cMMan.cStore[cEvtStack.cEvent.iObjID].bTruckReady) {
					if ( cMMan.cStore[cEvtStack.cEvent.iObjID].cTransportOrd.iClientType == cMMan.ID_SHOP ) {
						cEvtStack.mPush((iTime + cMMan.cShop[cMMan.cStore[cEvtStack.cEvent.iObjID].cTransportOrd.iClientID].iTruckPassagePeriod),		
							cMMan.cStore[cEvtStack.cEvent.iObjID].cTransportOrd.iClientID,
							cMMan.ID_SHOP,
							cEvtStack.E_SHOPORDERARRIVED, 0);
					} else if ( cMMan.cStore[cEvtStack.cEvent.iObjID].cTransportOrd.iClientType == cMMan.ID_STORE ){
						cEvtStack.mPush((iTime + cMMan.cStore[cMMan.cStore[cEvtStack.cEvent.iObjID].cTransportOrd.iClientID].iTruckPassagePeriod),		
							cMMan.cStore[cEvtStack.cEvent.iObjID].cTransportOrd.iClientID,
							cMMan.ID_STORE,
							cEvtStack.E_STOREORDERARRIVED, 0);
					}
				}
			}
		}
	}
};


///////////////////////


class CLMA extends ResultFrame{
	protected CTimeLine cTimeLn;
	protected JButton jBInitilize;
	protected JButton jBSimulate;
	protected JButton jBAnalyze;
	protected JButton jBCopy;
	protected JTextArea jTEventsLog;
	protected CDataLogger cDataLog;
	protected String sEvent = new String();
	protected String sObjType = new String();
	protected int i;
	
	public void newProgressResult (ResultRepository n) {}
	public void finalResult (ResultRepository n) {}
	public void newResult (ResultRepository n) {}


	// METHODS
	protected CLMA(){	// - Constructor.
		super ("LogisticModel");
		cTimeLn = new CTimeLine(this);
		init();
		start();

	}
	
	protected void mStartSimulation(){	// - Starts logistics model simulation.
		cTimeLn.mStart();
	}
	
	protected void mStopSimulation(){	// - Stops logistics model simulation.
	}
	
	protected void mPause(){			// - Pauses logistics model simulation.
	}
	
	protected void mReset(){			//- Resets model data and performs initialization process.	
		//System.out.println("Resetting Logistics Model");
	}
	
	// - Starting execution of an applet.
	protected void init() {
		jBInitilize = new JButton("Step 1. Initialize");
		jBSimulate = new JButton("Step 2. Simulate");
		jBAnalyze = new JButton("Step 3. Analyze");
		jBCopy = new JButton ("Copy Data");
		jTEventsLog = new JTextArea();
		
	}	
	
	// Comment this for Applet application.
	/*
	public static void main (String sA[]) {
		CLMA clm = new CLMA();
		clm.init();
		clm.start();
	}
	*/
	
	
	protected void start() {
		// Creating visual User Interface.
		JFrame jF = new JFrame("Logistics model");
		jF.setBounds(10,10,500,500);
		
		Container cntButtons = new Container();
		Container cntTextView = new Container();
		Container cntMain = jF.getContentPane();
		
		cntButtons.setLayout(new FlowLayout(FlowLayout.LEFT));
		cntButtons.add(jBInitilize);
		cntButtons.add(jBSimulate);
		cntButtons.add(jBAnalyze);
		cntButtons.add(jBCopy);
		
		jBInitilize.addActionListener( new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//CInitWindow cIW = new CInitWindow(cTimeLn);
				//cIW.setVisible(true);
				mInit(); // Initilising process.
				
				jBInitilize.setEnabled(false);
			}
		});

		jBSimulate.addActionListener( new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				jBSimulate.setEnabled(false);
				mStartSimulation();			
			}
		});
		
		jBAnalyze.addActionListener( new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CAnalysis aA = new CAnalysis(cDataLog);
				aA.mShowAnalysis();
			}
		});
		
		jBCopy.addActionListener( new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				jTEventsLog.selectAll();
				jTEventsLog.setSelectionColor(new Color(200,200,200));
				jTEventsLog.copy();
				jTEventsLog.requestFocus();
			}
		});

				
		jTEventsLog.setBounds(5,70, 490,300);
		JScrollPane jSP = new JScrollPane(jTEventsLog);
		jSP.setBounds(5,40,480,300);
		
		cntMain.setLayout(new BorderLayout());
		cntMain.add(cntButtons, BorderLayout.NORTH);
		cntMain.add(jSP, BorderLayout.CENTER);		
		jF.setVisible(true);
	}
	
	// - Vizualizes a completed Tick data.
	// Initialising time tick
	protected void mTickInit() {
		// Starting loging Tick Data into Log.
		cDataLog.mNewTickDataBlock();
		jTEventsLog.append("TICK["+cTimeLn.iTime+"] - STARTED.\n");
	}
	
	// Time tick accomplished.
	protected void mTickComplete() {
		
		jTEventsLog.append("TICK["+cTimeLn.iTime+"] - COMPLETE. \n----- RESULTS----- \n");
		// Information on shops.		
		for (i=0; i<cTimeLn.cMMan.cShop.length; i++) {
			jTEventsLog.append(new StringBuffer().append("Shop[").append(i).append("] - Amnt avail.: ").append(cTimeLn.cMMan.cShop[i].iProductAmountAvailable).append("; Demand[").append(cTimeLn.cMMan.cShop[i].iDemand).append("]").
				append("; Lack: ").append(cTimeLn.cMMan.cShop[i].iProductLack).
				append("; Products ordered: ").append(cTimeLn.cMMan.cShop[i].iProductsOrdered).append("\n").toString());
			// Loging shop data.
			cDataLog.mSetShopData(i, cTimeLn.cMMan.cShop[i].iProductAmountAvailable, cTimeLn.cMMan.cShop[i].iProductLack, cTimeLn.cMMan.cShop[i].iProductsOrdered, cTimeLn.cMMan.cShop[i].iDemand);
		}
		
		// Information on stores.
		jTEventsLog.append("--------------------\n");
		for (i=0; i<cTimeLn.cMMan.cStore.length; i++) {
			jTEventsLog.append(new StringBuffer().append("Store[").append(i).append("] - Amnt avail.: ").append(cTimeLn.cMMan.cStore[i].iProductAmountAvailable).append("; Truck Count[").append(cTimeLn.cMMan.cStore[i].iTruckCountAvailable).append("]").
				append("; Lack: ").append(cTimeLn.cMMan.cStore[i].iProductLack).
				append("; Products ordered: ").append(cTimeLn.cMMan.cStore[i].iProductsOrdered).append("\n").toString());
			// Loging store data.
			cDataLog.mSetStoreData(i, cTimeLn.cMMan.cStore[i].iProductAmountAvailable, cTimeLn.cMMan.cStore[i].iProductLack, cTimeLn.cMMan.cStore[i].iProductsOrdered, cTimeLn.cMMan.cStore[i].iTruckCountAvailable);
		}
		jTEventsLog.append("--------------------\n");
		
		
		cDataLog.mSaveTickDataBlock();
		
	}

	protected void mEventOccured(int iAction, int iObjID, int iObjType, int iValue){
		switch (iAction) {
		case CEventStack.E_SHOPCHECKSTOCK:
			sEvent = "E_SHOPCHECKSTOCK: ";
			break;
		case CEventStack.E_SHOPORDERARRIVED:
			sEvent = "E_SHOPORDERARRIVED: ";
			break;
		case CEventStack.E_ORDERFULFILCOMLETE:
			sEvent = "E_ORDERFULFILCOMLETE: ";
			break;
		case CEventStack.E_STORECHECKSTOCK:
			sEvent = "E_STORECHECKSTOCK: ";
			break;
		case CEventStack.E_STOREORDERARRIVED:
			sEvent = "E_STOREORDERARRIVED: ";
			break;
		case CEventStack.E_STORETRUCKARRIVED:
			sEvent = "E_STORETRUCKARRIVED: ";
			break;
		}
		
		switch (iObjType) {
		case CModelManager.ID_SHOP:
			sObjType = "Shop";
			break;
		case CModelManager.ID_STORE:
			sObjType = "Store";
			break;
		}
		
		jTEventsLog.append(new StringBuffer().append(sEvent)
			.append(sObjType)
			.append("     [")
			.append(iObjID)
			.append("]      Value=")
			.append(iValue)
			.append("\n")
			.toString());
			
		// Loging event data.
		cDataLog.mSetEvent(iAction, iObjID, iObjType, iValue);
	}
	
	
	protected void mEventAction(String sActionVal) {
		jTEventsLog.append(new StringBuffer().append("    - ").append(sActionVal).append("\n").toString());
	}
	
	protected void mInit() {
		// End time. 
		cTimeLn.mInitialize();
		cTimeLn.iEndTime = 24; // Simulation period - (72hours)3 days.
		
		
		// Setting Shops Count.
		cTimeLn.cMMan.mSetShopCount(1);
		
		// Setting Stores Count.
		cTimeLn.cMMan.mSetStoreCount(2);	
		
		// Assigning shops to Stores.
		cTimeLn.cMMan.mAssignShop(0, 0);
//		cTimeLn.cMMan.mAssignShop(1, 0);
//		cTimeLn.cMMan.mAssignShop(2, 0);

		// Product starting amount (S(t0))
		cTimeLn.cMMan.cShop[0].iProductAmountMargin = 9;
//		cTimeLn.cMMan.cShop[1].iProductAmountMargin = 20;
//		cTimeLn.cMMan.cShop[2].iProductAmountMargin = 20;
		
		// Product Amount must be equal or greater than Product Amount Margin. (Str)
		cTimeLn.cMMan.cShop[0].iProductAmountAvailable = 30;
//		cTimeLn.cMMan.cShop[1].iProductAmountAvailable = 60;
//		cTimeLn.cMMan.cShop[2].iProductAmountAvailable = 20;
		
		
		// Product lack is 0 (T(t0)).
		cTimeLn.cMMan.cShop[0].iProductLack = 0;
//		cTimeLn.cMMan.cShop[1].iProductLack = 0;
//		cTimeLn.cMMan.cShop[2].iProductLack = 0;
		
		// Product order amount is 0 P(t0).
		cTimeLn.cMMan.cShop[0].iProductAmountPerOrder = 20;
//		cTimeLn.cMMan.cShop[1].iProductAmountPerOrder = 20;
//		cTimeLn.cMMan.cShop[2].iProductAmountPerOrder = 20;
		
		// Stock check period in hours(epsilon).
		cTimeLn.cMMan.cShop[0].iStockCheckPeriod = 2;
//		cTimeLn.cMMan.cShop[1].iStockCheckPeriod = 2;
//		cTimeLn.cMMan.cShop[2].iStockCheckPeriod = 2;
		// Seeting into events to check stock after 2 hours.
		cTimeLn.cEvtStack.mPush((cTimeLn.iTime + cTimeLn.cMMan.cShop[0].iStockCheckPeriod), 0, cTimeLn.cMMan.ID_SHOP, cTimeLn.cEvtStack.E_SHOPCHECKSTOCK,0);
		//cTimeLn.cEvtStack.mPush((cTimeLn.iTime + cTimeLn.cMMan.cShop[1].iStockCheckPeriod), 1, cTimeLn.cMMan.ID_SHOP, cTimeLn.cEvtStack.E_SHOPCHECKSTOCK,0);
		//cTimeLn.cEvtStack.mPush((cTimeLn.iTime + cTimeLn.cMMan.cShop[2].iStockCheckPeriod), 2, cTimeLn.cMMan.ID_SHOP, cTimeLn.cEvtStack.E_SHOPCHECKSTOCK,0);
		
		// Shop demand per Day.
		cTimeLn.cMMan.cShop[0].iDemandPerDay = 50;
		//cTimeLn.cMMan.cShop[1].iDemandPerDay = 50;
		//cTimeLn.cMMan.cShop[2].iDemandPerDay = 60;

		cTimeLn.cMMan.cShop[0].iTruckPassagePeriod = 2; // 2 hours.
		//cTimeLn.cMMan.cShop[1].iTruckPassagePeriod = 2; // 2 hours.
		//cTimeLn.cMMan.cShop[2].iTruckPassagePeriod = 2; // 2 hours.
		
		cTimeLn.cMMan.cShop[0].iTruckReturnPeriod = 1; // 1 hours.
		//cTimeLn.cMMan.cShop[1].iTruckReturnPeriod = 1; // 1 hours.
		//cTimeLn.cMMan.cShop[2].iTruckReturnPeriod = 1; // 1 hours.
		
			
		// Stores.	
		// Setting stores truck count.
		cTimeLn.cMMan.mSetStoreTruckCount(0, 10);
		
		// Assigning shops to Stores.
		cTimeLn.cMMan.mAssignStore(0, 1);	// - Assigning Store to supplier.

		// (STr)		
		cTimeLn.cMMan.cStore[0].iProductAmountMargin = 20;

		// s(t0)
		cTimeLn.cMMan.cStore[0].iProductAmountAvailable = 50;
		
		// T(t0)
		cTimeLn.cMMan.cStore[0].iProductLack = 0;
		
		// P(t0)
		cTimeLn.cMMan.cStore[0].iProductAmountPerOrder = 40;
		
		cTimeLn.cMMan.cStore[0].iOrderFulfillPeriod = 1; // 1 hour.
		
		cTimeLn.cMMan.cStore[0].iTruckPassagePeriod = 2; // Rruck passage period from store or suplier to stock.
		
		cTimeLn.cMMan.cStore[0].iTruckReturnPeriod = 1; // Truck return period from stock to store or suplier.
		
		
		// Tiekejas.		
		cTimeLn.cMMan.cStore[1].iProductAmountMargin = 0;
		cTimeLn.cMMan.cStore[1].iProductAmountAvailable = 99999; // Unlimited product amount
		cTimeLn.cMMan.cStore[1].iProductLack = 0;
		cTimeLn.cMMan.cStore[1].iOrderFulfillPeriod = 0; // no time for order fulfilation.
		cTimeLn.cMMan.mSetStoreTruckCount(1, 10);	// Unlimited truck count.
		// No stock check is performed.
		
		
		cDataLog = new CDataLogger(cTimeLn.cMMan.cShop.length, cTimeLn.cMMan.cStore.length);		
		// - Setting initial data. (time-tick = 0)
		cDataLog.mNewTickDataBlock();
		// Displaying primary information:
		jTEventsLog.append("SHOPS TOTAL: "+cTimeLn.cMMan.cShop.length);
		for (i=0; i<cTimeLn.cMMan.cShop.length; i++) {
			jTEventsLog.append("\nShop["+i+"] Amount margin: "+cTimeLn.cMMan.cShop[i].iProductAmountMargin);
			jTEventsLog.append("; Amount available: "+cTimeLn.cMMan.cShop[i].iProductAmountAvailable);
			jTEventsLog.append("; Product lack: "+cTimeLn.cMMan.cShop[i].iProductLack);
			jTEventsLog.append("\n Amount per order: "+cTimeLn.cMMan.cShop[i].iProductAmountPerOrder);
			jTEventsLog.append("; Stock check period: "+cTimeLn.cMMan.cShop[i].iStockCheckPeriod + " hour's");
			cDataLog.mSetShopData(i, cTimeLn.cMMan.cShop[i].iProductAmountAvailable, cTimeLn.cMMan.cShop[i].iProductLack, cTimeLn.cMMan.cShop[i].iProductsOrdered, cTimeLn.cMMan.cShop[i].iDemand);
		}
		
		jTEventsLog.append("\nSTORES TOTAL: "+cTimeLn.cMMan.cStore.length);
		for (i=0; i<cTimeLn.cMMan.cStore.length; i++) {
			jTEventsLog.append("\nStore["+i+"] "+ (cTimeLn.cMMan.cStore[i].iProductAmountAvailable==99999?"- SUPPLIER.":"")+" Amount margin: "+cTimeLn.cMMan.cStore[i].iProductAmountMargin);
			jTEventsLog.append("; Amount available: "+cTimeLn.cMMan.cStore[i].iProductAmountAvailable);
			jTEventsLog.append("; Product lack: "+cTimeLn.cMMan.cStore[i].iProductLack);
			jTEventsLog.append("\n Amount per order: "+cTimeLn.cMMan.cStore[i].iProductAmountPerOrder);
			cDataLog.mSetStoreData(i, cTimeLn.cMMan.cStore[i].iProductAmountAvailable, cTimeLn.cMMan.cStore[i].iProductLack, cTimeLn.cMMan.cStore[i].iProductsOrdered, cTimeLn.cMMan.cStore[i].iTruckCountAvailable);
		}
		
		
		cDataLog.mSaveTickDataBlock();
		jTEventsLog.append("\nSimulation period: " + cTimeLn.iEndTime + " hours ("+ cTimeLn.iEndTime/24 +" day's)\n-------------\n");
	}
	
}



class CInitWindow extends JFrame {
	CTimeLine cTimeLn;
	JTextField jTFShops, jTFStores;
	Container cntPane;
	JButton jBOk;
	int i;
	int iSelIndex;
	boolean bShop;

	
	CInitWindow (CTimeLine cTimeL) {
		cTimeLn = cTimeL;
		setTitle("Modify parameters");
		setSize(445,250);
		
		mInitDataContainers();
		/*		
		Container cntTop = new Container(); // A container for: Stores and Shops count
		cntTop.setLayout(new GridLayout(1,5));
		
		
		jTFShops  = new JTextField("1");
		jTFStores = new JTextField("2");
		
		cntTop.add(new JLabel("Shops count: ", JLabel.RIGHT));
		cntTop.add(jTFShops);

		cntTop.add(new JLabel("  Stores count: ", JLabel.RIGHT));
		cntTop.add(jTFStores);
		jBOk = new JButton("Ok");
		cntTop.add(jBOk);

		
        cntPane.add(cntTop);
				
		jBOk.addActionListener( new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (jTFShops.getText().length() > 0 && jTFStores.getText().length() >0) {
				// Setting Shops Count
					cTimeLn.cMMan.mSetShopCount(Integer.parseInt(jTFShops.getText()));
					
					// Setting Stores Count.
					cTimeLn.cMMan.mSetStoreCount(Integer.parseInt(jTFStores.getText()));
					
					cntPane.removeAll();
					mInitDataContainers();
					setSize(445,400);
				}
			}
		});
		*/
		// -- End of Stores and Shops Count;
		// -- SOF Shop and Store parameters
		
		
	}
	
	private void mInitDataContainers() {
		
		cntPane = getContentPane();
		cntPane.setLayout(new BorderLayout());

		iSelIndex = 0;
		bShop = true;
		
		
		Container cntData = new Container(); // Stores Data
		
		// Creating selection.		
		String[] sSelectionList = new String[cTimeLn.cMMan.cShop.length + cTimeLn.cMMan.cStore.length-1];
		
		for (i=0; i<cTimeLn.cMMan.cShop.length; i++) {
			sSelectionList[i] = new String("Shop["+i+"]");
		}
		
		for (i=0; i<cTimeLn.cMMan.cStore.length-1; i++) {
			sSelectionList[cTimeLn.cMMan.cShop.length+i] = new String("Store["+i+"]");
		}
		
		final JComboBox jLSelList = new JComboBox(sSelectionList);
		
		cntPane.add(jLSelList, BorderLayout.NORTH);	
		
		cntData.setLayout(new GridLayout(10,2));
		cntPane.add(cntData, BorderLayout.CENTER);	

		
		//1
		cntData.add(new JLabel("Product amount margin: ",JLabel.RIGHT));//3
		final JTextField jTProdAmountMargin = new JTextField(Integer.toString(cTimeLn.cMMan.cShop[0].iProductAmountMargin));
		cntData.add(jTProdAmountMargin);//3.3- Amount Margin.
		
		jTProdAmountMargin.addKeyListener( new KeyListener() {
			public void keyTyped(KeyEvent e) {}
			public void keyPressed(KeyEvent e) {}
			public void keyReleased(KeyEvent e) {
				try {
					if (bShop) {
						cTimeLn.cMMan.cShop[iSelIndex].iProductAmountMargin = Integer.parseInt(jTProdAmountMargin.getText());
					} else {
						cTimeLn.cMMan.cStore[iSelIndex].iProductAmountMargin = Integer.parseInt(jTProdAmountMargin.getText());
					}
				} catch (NumberFormatException nfe) {
					jTProdAmountMargin.setText("");
					jTProdAmountMargin.requestFocus();
				}
			}
		});
		
		jTProdAmountMargin.addFocusListener( new FocusListener() {
			public void focusGained(FocusEvent e) {}
			public void focusLost(FocusEvent e) { if (jTProdAmountMargin.getText().length() == 0) jTProdAmountMargin.requestFocus();}
		});
		
		//2		
		cntData.add(new JLabel("Product amount available: ",JLabel.RIGHT));//2
		final JTextField jTProdAmountAvail = new JTextField(Integer.toString(cTimeLn.cMMan.cShop[0].iProductAmountAvailable));
		cntData.add(jTProdAmountAvail);//2.2- Amount Available.

		jTProdAmountAvail.addKeyListener( new KeyListener() {
			public void keyTyped(KeyEvent e) {}
			public void keyPressed(KeyEvent e) {}
			public void keyReleased(KeyEvent e) {
				try {
					if (bShop) {
						cTimeLn.cMMan.cShop[iSelIndex].iProductAmountAvailable = Integer.parseInt(jTProdAmountAvail.getText());
					} else {
						cTimeLn.cMMan.cStore[iSelIndex].iProductAmountAvailable = Integer.parseInt(jTProdAmountAvail.getText());
					}
				} catch (NumberFormatException nfe) {
					jTProdAmountAvail.setText("");
					jTProdAmountAvail.requestFocus();
				}
			}
		});
		
		jTProdAmountAvail.addFocusListener( new FocusListener() {
			public void focusGained(FocusEvent e) {}
			public void focusLost(FocusEvent e) { if (jTProdAmountAvail.getText().length() == 0) jTProdAmountAvail.requestFocus();}
		});
		
		//3
		cntData.add(new JLabel("Product amount per order: ",JLabel.RIGHT));//6
		final JTextField jTAmountPerOrder = new JTextField(Integer.toString(cTimeLn.cMMan.cShop[0].iProductAmountPerOrder));
		cntData.add(jTAmountPerOrder);//6.6- Amount per order.
		
		jTAmountPerOrder.addKeyListener( new KeyListener() {
			public void keyTyped(KeyEvent e) {}
			public void keyPressed(KeyEvent e) {}
			public void keyReleased(KeyEvent e) {
				try {
					if (bShop) {
						cTimeLn.cMMan.cShop[iSelIndex].iProductAmountPerOrder = Integer.parseInt(jTAmountPerOrder.getText());
					} else {
						cTimeLn.cMMan.cStore[iSelIndex].iProductAmountPerOrder = Integer.parseInt(jTAmountPerOrder.getText());
					}
				} catch (NumberFormatException nfe) {
					jTAmountPerOrder.setText("");
					jTAmountPerOrder.requestFocus();
				}
			}
		});
		
		jTAmountPerOrder.addFocusListener( new FocusListener() {
			public void focusGained(FocusEvent e) {}
			public void focusLost(FocusEvent e) { if (jTAmountPerOrder.getText().length() == 0) jTAmountPerOrder.requestFocus();}
		});
		
		//4
		cntData.add(new JLabel("Stock check period(in hours): ",JLabel.RIGHT));//4
		final JTextField jTStockCheckPeriod = new JTextField(Integer.toString(cTimeLn.cMMan.cShop[0].iStockCheckPeriod));
		cntData.add(jTStockCheckPeriod);//4.4.- Stock check period.
		
		jTStockCheckPeriod.addKeyListener( new KeyListener() {
			public void keyTyped(KeyEvent e) {}
			public void keyPressed(KeyEvent e) {}
			public void keyReleased(KeyEvent e) {
				try {
					if (bShop) {
						cTimeLn.cMMan.cShop[iSelIndex].iStockCheckPeriod = Integer.parseInt(jTStockCheckPeriod.getText());
					}
				} catch (NumberFormatException nfe) {
					jTStockCheckPeriod.setText("");
					jTStockCheckPeriod.requestFocus();
				}
			}
		});
		
		jTStockCheckPeriod.addFocusListener( new FocusListener() {
			public void focusGained(FocusEvent e) {}
			public void focusLost(FocusEvent e) { if (jTStockCheckPeriod.getText().length() == 0) jTStockCheckPeriod.requestFocus();}
		});
		
		//5
		cntData.add(new JLabel("Demand per Day: ",JLabel.RIGHT));	//1
		final JTextField jTAvgDemandPerDay = new JTextField(Integer.toString(cTimeLn.cMMan.cShop[0].iDemandPerDay));
		cntData.add(jTAvgDemandPerDay);//1.1- demand per day.
				
		jTAvgDemandPerDay.addKeyListener( new KeyListener() {
			public void keyTyped(KeyEvent e) {}
			public void keyPressed(KeyEvent e) {}
			public void keyReleased(KeyEvent e) {
				try {
					if (bShop) {
						cTimeLn.cMMan.cShop[iSelIndex].iDemandPerDay = Integer.parseInt(jTAvgDemandPerDay.getText());
					}
				} catch (NumberFormatException nfe) {
					jTAvgDemandPerDay.setText("");
					jTAvgDemandPerDay.requestFocus();
				}
			}
		});
		
		jTAvgDemandPerDay.addFocusListener( new FocusListener() {
			public void focusGained(FocusEvent e) {}
			public void focusLost(FocusEvent e) { if (jTAvgDemandPerDay.getText().length() == 0) jTAvgDemandPerDay.requestFocus();}
		});

				
		cntData.add(new JLabel("Initial product lack: ",JLabel.RIGHT));//5
		final JTextField jTProductLack = new JTextField(Integer.toString(cTimeLn.cMMan.cShop[0].iProductLack));
		cntData.add(jTProductLack);//5.5- Product Lack.

		jTProductLack.addKeyListener( new KeyListener() {
			public void keyTyped(KeyEvent e) {}
			public void keyPressed(KeyEvent e) {}
			public void keyReleased(KeyEvent e) {
				try {
					if (bShop) {
						cTimeLn.cMMan.cShop[iSelIndex].iProductLack = Integer.parseInt(jTProductLack.getText());
					} else {
						cTimeLn.cMMan.cStore[iSelIndex].iProductLack = Integer.parseInt(jTProductLack.getText());
					}
				} catch (NumberFormatException nfe) {
					jTProductLack.setText("");
					jTProductLack.requestFocus();
				}
			}
		});
		
		jTProductLack.addFocusListener( new FocusListener() {
			public void focusGained(FocusEvent e) {}
			public void focusLost(FocusEvent e) { if (jTProductLack.getText().length() == 0) jTProductLack.requestFocus();}
		});
				
		cntData.add(new JLabel("Truck passage from store period(h): ",JLabel.RIGHT));//7
		final JTextField jTTruckPassage = new JTextField(Integer.toString(cTimeLn.cMMan.cShop[0].iTruckPassagePeriod));
		cntData.add(jTTruckPassage);//7.7- Truck passage period.
		
		jTTruckPassage.addKeyListener( new KeyListener() {
			public void keyTyped(KeyEvent e) {}
			public void keyPressed(KeyEvent e) {}
			public void keyReleased(KeyEvent e) {
				try {
					if (bShop) {
						cTimeLn.cMMan.cShop[iSelIndex].iTruckPassagePeriod = Integer.parseInt(jTTruckPassage.getText());
					} else {
						cTimeLn.cMMan.cStore[iSelIndex].iTruckPassagePeriod = Integer.parseInt(jTTruckPassage.getText());
					}
				} catch (NumberFormatException nfe) {
					jTTruckPassage.setText("");
					jTTruckPassage.requestFocus();
				}
			}
		});
		
		jTTruckPassage.addFocusListener( new FocusListener() {
			public void focusGained(FocusEvent e) {}
			public void focusLost(FocusEvent e) { if (jTTruckPassage.getText().length() == 0) jTTruckPassage.requestFocus();}
		});
		
		cntData.add(new JLabel("Truck return to store period(h): ",JLabel.RIGHT));//8
		final JTextField jTTruckReturn = new JTextField(Integer.toString(cTimeLn.cMMan.cShop[0].iTruckReturnPeriod));
		cntData.add(jTTruckReturn);//8.8- Truck return period.

		jTTruckReturn.addKeyListener( new KeyListener() {
			public void keyTyped(KeyEvent e) {}
			public void keyPressed(KeyEvent e) {}
			public void keyReleased(KeyEvent e) {
				try {
					if (bShop) {
						cTimeLn.cMMan.cShop[iSelIndex].iTruckReturnPeriod = Integer.parseInt(jTTruckReturn.getText());
					} else {
						cTimeLn.cMMan.cStore[iSelIndex].iTruckReturnPeriod = Integer.parseInt(jTTruckReturn.getText());
					}
				} catch (NumberFormatException nfe) {
					jTTruckReturn.setText("");
					jTTruckReturn.requestFocus();
				}
			}
		});
		
		jTTruckReturn.addFocusListener( new FocusListener() {
			public void focusGained(FocusEvent e) {}
			public void focusLost(FocusEvent e) { if (jTTruckReturn.getText().length() == 0) jTTruckReturn.requestFocus();}
		});
				
		cntData.add(new JLabel("Order fulfil period(h): ",JLabel.RIGHT));//5
		final JTextField jTSOrderFulfil = new JTextField("");
		cntData.add(jTSOrderFulfil);//5.5- order fulfil period.

		jTSOrderFulfil.addKeyListener( new KeyListener() {
			public void keyTyped(KeyEvent e) {}
			public void keyPressed(KeyEvent e) {}
			public void keyReleased(KeyEvent e) {
				try {
					if (!bShop) {
						cTimeLn.cMMan.cStore[iSelIndex].iOrderFulfillPeriod = Integer.parseInt(jTSOrderFulfil.getText());
					}
				} catch (NumberFormatException nfe) {
					jTSOrderFulfil.setText("");
					jTSOrderFulfil.requestFocus();
				}
			}
		});
		
		jTSOrderFulfil.addFocusListener( new FocusListener() {
			public void focusGained(FocusEvent e) {}
			public void focusLost(FocusEvent e) { if (jTSOrderFulfil.getText().length() == 0) jTSOrderFulfil.requestFocus();}
		});
				
		cntData.add(new JLabel("Truck count: ",JLabel.RIGHT));//6
		final JTextField jTSTruckCount = new JTextField("");
		cntData.add(jTSTruckCount);//6.6- Truck count.
		
		jTSTruckCount.addKeyListener( new KeyListener() {
			public void keyTyped(KeyEvent e) {}
			public void keyPressed(KeyEvent e) {}
			public void keyReleased(KeyEvent e) {
				try {
					if (!bShop) {
						cTimeLn.cMMan.cStore[iSelIndex].iTruckCountTotal = Integer.parseInt(jTSTruckCount.getText());
					}
				} catch (NumberFormatException nfe) {
					jTSTruckCount.setText("");
					jTSTruckCount.requestFocus();
				}
			}
		});
		
		jTSTruckCount.addFocusListener( new FocusListener() {
			public void focusGained(FocusEvent e) {}
			public void focusLost(FocusEvent e) { if (jTSTruckCount.getText().length() == 0) jTSTruckCount.requestFocus();}
		});
		
		jTSOrderFulfil.setEnabled(false);
		jTSTruckCount.setEnabled(false);
		
		
		
		// Listening for changes.
		jLSelList.addActionListener( new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				iSelIndex = jLSelList.getSelectedIndex();
				
				if (iSelIndex < cTimeLn.cMMan.cShop.length) {
					bShop = true;
					jTSOrderFulfil.setEnabled(false);
					jTSTruckCount.setEnabled(false);
					jTAvgDemandPerDay.setEnabled(true);	
					jTStockCheckPeriod.setEnabled(true);				
				} else {
					bShop = false;
					jTSOrderFulfil.setEnabled(true);
					jTSTruckCount.setEnabled(true);
					jTAvgDemandPerDay.setEnabled(false);					
					jTStockCheckPeriod.setEnabled(false);
					
					iSelIndex = iSelIndex - cTimeLn.cMMan.cShop.length;
				}
				
				if (bShop) {
					jTProdAmountMargin.setText(Integer.toString(cTimeLn.cMMan.cShop[iSelIndex].iProductAmountMargin));
					jTProdAmountAvail.setText(Integer.toString(cTimeLn.cMMan.cShop[iSelIndex].iProductAmountAvailable));
					jTAmountPerOrder.setText(Integer.toString(cTimeLn.cMMan.cShop[iSelIndex].iProductAmountPerOrder));
					jTStockCheckPeriod.setText(Integer.toString(cTimeLn.cMMan.cShop[iSelIndex].iStockCheckPeriod));
					jTAvgDemandPerDay.setText(Integer.toString(cTimeLn.cMMan.cShop[iSelIndex].iDemandPerDay));
					jTProductLack.setText(Integer.toString(cTimeLn.cMMan.cShop[iSelIndex].iProductLack));
					jTTruckPassage.setText(Integer.toString(cTimeLn.cMMan.cShop[iSelIndex].iTruckPassagePeriod));
					jTTruckReturn.setText(Integer.toString(cTimeLn.cMMan.cShop[iSelIndex].iTruckReturnPeriod));
					jTSOrderFulfil.setText("");
					jTSTruckCount.setText("");
				} else {
					jTProdAmountMargin.setText(Integer.toString(cTimeLn.cMMan.cStore[iSelIndex].iProductAmountMargin));
					jTProdAmountAvail.setText(Integer.toString(cTimeLn.cMMan.cStore[iSelIndex].iProductAmountAvailable));
					jTAmountPerOrder.setText(Integer.toString(cTimeLn.cMMan.cStore[iSelIndex].iProductAmountPerOrder));
					jTStockCheckPeriod.setText("");
					jTAvgDemandPerDay.setText("");
					jTProductLack.setText(Integer.toString(cTimeLn.cMMan.cStore[iSelIndex].iProductLack));
					jTTruckPassage.setText(Integer.toString(cTimeLn.cMMan.cStore[iSelIndex].iTruckPassagePeriod));
					jTTruckReturn.setText(Integer.toString(cTimeLn.cMMan.cStore[iSelIndex].iTruckReturnPeriod));
					jTSOrderFulfil.setText(Integer.toString(cTimeLn.cMMan.cStore[iSelIndex].iOrderFulfillPeriod));
					jTSTruckCount.setText(Integer.toString(cTimeLn.cMMan.cStore[iSelIndex].iTruckCountTotal));
				}
				// Setting Text
			}
		});
		
        
        cntPane.validate();

	}
	
}
