/*
 * JaamSim Discrete Event Simulation
 * Copyright (C) 2013 Ausenco Engineering Canada Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */
package com.jaamsim.CalculationObjects;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import com.jaamsim.input.Keyword;
import com.jaamsim.input.ValueInput;
import com.jaamsim.units.TimeUnit;
import com.sandwell.JavaSimulation.Entity;
import com.sandwell.JavaSimulation3D.DisplayEntity;

/**
 * The Controller object simulates the operation of a Programmable Logic Controller
 * @author Harry King
 *
 */
public class Controller extends DisplayEntity {

	@Keyword(description = "The sampling time for the Controller.",
	         example = "Controller1 SamplingTime { 100 ms }")
	private final ValueInput samplingTime;

	private final ArrayList<CalculationEntity> calculationEntityList;  // List of the CalculationEntities controller by this Controller.
	private int count;  // Number of times that the controller has initiated its calculations.

	{
		samplingTime = new ValueInput("SamplingTime", "Key Inputs", 1.0d);
		samplingTime.setUnitType(TimeUnit.class);
		samplingTime.setValidRange(0.0, Double.POSITIVE_INFINITY);
		this.addInput(samplingTime);
	}

	public Controller() {
		calculationEntityList = new ArrayList<CalculationEntity>();
	}

	@Override
	public void earlyInit() {
		super.earlyInit();
		count = 0;

		// Prepare a list of the calculation entities managed by this controller
		calculationEntityList.clear();
		for (CalculationEntity ent : Entity.getClonesOfIterator(CalculationEntity.class)) {
			if (ent.getController() == this)
				calculationEntityList.add(ent);
		}

		// Sort the calculation entities into the correct sequence
		Collections.sort(calculationEntityList, new SequenceCompare());
	}

	// Sorts by increasing sequence number
	private static class SequenceCompare implements Comparator<CalculationEntity> {
		@Override
		public int compare(CalculationEntity c1, CalculationEntity c2) {
			return Double.compare(c1.getSequenceNumber(), c2.getSequenceNumber());
		}
	}

	@Override
	public void startUp() {
		super.startUp();

		//Loop infinitely over the calculation entities
		while( true ) {

			// Wait for the samplingTime
			this.simWait( samplingTime.getValue() );

			// Update the last value for each entity
			double simTime = this.getSimTime();
			for( CalculationEntity ent : calculationEntityList ) {
				ent.update(simTime);
			}

			// Increment the number of cycles
			count++;
		}
	}

	public int getCount() {
		return count;
	}
}

@@ -724,16 +724,57 @@
 rator;%0A%0A
+import com.jaamsim.events.ProcessTarget;%0A
 import c
@@ -914,16 +914,65 @@
 Entity;%0A
+import com.sandwell.JavaSimulation.EntityTarget;%0A
 import c
@@ -1574,16 +1574,83 @@
 tions.%0A%0A
+%09private final ProcessTarget doUpdate = new DoUpdateTarget(this);%0A%0A
 %09%7B%0A%09%09sam
@@ -2763,153 +2763,334 @@
 %09%09//
-Loop infinitely over the calculation entities%0A%09%09while( true 
+ Schedule the first update%0A%09%09this.scheduleProcess(samplingTime.getValue(), 5, doUpdate);%0A%09%7D%0A%0A%09private static class DoUpdateTarget extends EntityTarget%3CController%3E %7B%0A%09%09DoUpdateTarget(Controller ent
 ) %7B%0A
-%0A
 %09%09%09
-// Wait for the samplingTime%0A%09%09%09this.simWait( samplingTime.getValu
+super(ent, %22doUpdate%22);%0A%09%09%7D%0A%0A%09%09@Override%0A%09%09public void process() %7B%0A%09%09%09ent.doUpdate();%0A%09%09%7D%0A%09%7D%0A%0A%09public void doUpdat
 e() 
-);
+%7B
 %0A%0A%09%09
-%09
 // U
@@ -3128,17 +3128,16 @@
 ntity%0A%09%09
-%09
 double s
@@ -3168,16 +3168,15 @@
 ();%0A
-%09
 %09%09for
-(
  
+(
 Calc
@@ -3220,14 +3220,12 @@
 List
- 
 ) %7B%0A
-%09
 %09%09%09e
@@ -3246,21 +3246,19 @@
 ime);%0A%09%09
-%09
 %7D%0A%0A
-%09
 %09%09// Inc
@@ -3285,17 +3285,16 @@
  cycles%0A
-%09
 %09%09count+
@@ -3296,19 +3296,108 @@
 ount++;%0A
+%0A
 %09%09
-%7D
+// Schedule the next update%0A%09%09this.scheduleProcess(samplingTime.getValue(), 5, doUpdate);
 %0A%09%7D%0A%0A%09pu

Convert Controller to use scheduleProcess instead of simWait