/*
 * JaamSim Discrete Event Simulation
 * Copyright (C) 2014 Ausenco Engineering Canada Inc.
 * Copyright (C) 2019 JaamSim Software Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jaamsim.basicsim;

import com.jaamsim.events.EventManager;
import com.jaamsim.events.ProcessTarget;

public class InitModelTarget extends ProcessTarget {

	final Simulation simulation;

	public InitModelTarget(Simulation sim) {
		simulation = sim;
	}

	@Override
	public String getDescription() {
		return "SimulationInit";
	}

	@Override
	public void process() {

		// Initialise each entity
		for (Entity each : Entity.getClonesOfIterator(Entity.class)) {
			each.earlyInit();
		}

		// Initialise each entity a second time
		for (Entity each : Entity.getClonesOfIterator(Entity.class)) {
			each.lateInit();
		}

		// Start each entity
		double startTime = Simulation.getStartTime();
		for (Entity each : Entity.getClonesOfIterator(Entity.class)) {
			if (!each.isActive())
				continue;
			EventManager.scheduleSeconds(startTime, 0, true, new StartUpTarget(each), null);
		}

		// Schedule the initialisation period
		if (Simulation.getInitializationTime() > 0.0) {
			double clearTime = startTime + Simulation.getInitializationTime();
			EventManager.scheduleSeconds(clearTime, 5, false, new ClearStatisticsTarget(), null);
		}

		// Schedule the end of the simulation run
		double endTime = Simulation.getEndTime();
		EventManager.scheduleSeconds(endTime, 5, false, new EndModelTarget(), null);

		// Start checking the pause condition
		simulation.doPauseCondition();
	}
}

@@ -1073,242 +1073,127 @@
 ) %7B%0A
-%0A
 %09%09
-// Initialise each entity%0A%09%09for (Entity each : Entity.getClonesOfIterator(Entity.class)) %7B%0A%09%09%09each.earlyInit();%0A%09%09%7D%0A%0A%09%09// Initialise each entity a second time%0A%09%09for (Entity each : Entity.getClonesOfIterator(Entity.class)) %7B%0A%09%09%09each
+JaamSimModel simModel = simulation.getJaamSimModel();%0A%0A%09%09// Initialise each entity%0A%09%09simModel.earlyInit();%0A%09%09simModel
 .lat
@@ -1200,20 +1200,16 @@
 eInit();
-%0A%09%09%7D
 %0A%0A%09%09// S
@@ -1298,47 +1298,29 @@
 h : 
-Entity.getClonesOfIterator(Entity.class
+simModel.getEntities(
 )) %7B

use JaamSimModel methods in InitModelTarget