/*
 * JaamSim Discrete Event Simulation
 * Copyright (C) 2014 Ausenco Engineering Canada Inc.
 * Copyright (C) 2016 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.BasicObjects;

import com.jaamsim.Graphics.DisplayEntity;
import com.jaamsim.Samples.SampleConstant;
import com.jaamsim.Samples.SampleInput;
import com.jaamsim.input.Keyword;
import com.jaamsim.units.TimeUnit;

public class EntityGate extends LinkedService {

	@Keyword(description = "The time delay before each queued entity is released.\n" +
			"Entities arriving at an open gate are not delayed.",
	         exampleList = {"3.0 h", "ExponentialDistribution1", "'1[s] + 0.5*[TimeSeries1].PresentValue'"})
	private final SampleInput releaseDelay;

	private DisplayEntity servedEntity; // the entity about to be released from the queue

	{
		releaseDelay = new SampleInput("ReleaseDelay", "Key Inputs", new SampleConstant(0.0));
		releaseDelay.setUnitType(TimeUnit.class);
		releaseDelay.setValidRange(0.0, Double.POSITIVE_INFINITY);
		releaseDelay.setEntity(this);
		this.addInput(releaseDelay);
	}

	public EntityGate() {}

	@Override
	public void earlyInit() {
		super.earlyInit();
		servedEntity = null;
	}

	@Override
	public void addEntity(DisplayEntity ent) {

		// If the gate is closed, in maintenance or breakdown, or other entities are already
		// queued, then add the entity to the queue
		Queue queue = waitQueue.getValue();
		if (!queue.isEmpty() || !this.isIdle()) {
			queue.addEntity(ent);
			return;
		}

		// If the gate is open and there are no other entities still in the queue, then send the entity to the next component
		this.registerEntity(ent);
		this.sendToNextComponent(ent);
	}

	@Override
	public void startAction() {

		// Stop if there is a forced downtime activity about to begin
		if (forcedDowntimePending) {
			forcedDowntimePending = false;
			this.setBusy(false);
			this.setPresentState();
			return;
		}

		// Determine the match value
		Integer m = this.getNextMatchValue(getSimTime());

		// Stop if the gate has closed or the queue has become empty
		if (!this.isOpen() || waitQueue.getValue().getMatchCount(m) == 0) {
			this.setBusy(false);
			this.setPresentState();
			return;
		}

		// Select the next entity to release
		servedEntity = this.getNextEntityForMatch(m);
		this.moveToProcessPosition(servedEntity);

		// Schedule the release of the next entity
		startTime = this.getSimTime();
		duration = releaseDelay.getValue().getNextSample(startTime);
		this.scheduleProcess(duration, 5, endActionTarget, endActionHandle);
	}

	/**
	 * Loop recursively through the queued entities, releasing them one by one.
	 */
	@Override
	public void endAction() {

		// Release the first element in the queue and send to the next component
		this.sendToNextComponent(servedEntity);
		servedEntity = null;

		// Try to release another entity
		this.startAction();
	}

}

@@ -2239,31 +2239,55 @@
 e%0A%09p
-ublic void startAction(
+rotected boolean startProcessing(double simTime
 ) %7B%0A
@@ -2423,65 +2423,20 @@
 %0A%09%09%09
-this.setBusy(false);%0A%09%09%09this.setPresentState();%0A%09%09%09
 return
+ false
 ;%0A%09%09
@@ -2662,65 +2662,20 @@
 %0A%09%09%09
-this.setBusy(false);%0A%09%09%09this.setPresentState();%0A%09%09%09
 return
+ false
 ;%0A%09%09
@@ -2815,216 +2815,19 @@
 %0A%0A%09%09
-// Schedule the release of the next entity%0A%09%09startTime = this.getSimTime();%0A%09%09duration = releaseDelay.getValue().getNextSample(startTime);%0A%09%09this.scheduleProcess(duration, 5, endActionTarget, endActionHandle)
+return true
 ;%0A%09%7D
@@ -2924,29 +2924,32 @@
 @Override%0A%09p
-ublic
+rotected
  void endAct
@@ -2945,23 +2945,41 @@
 void end
-Action(
+Processing(double simTime
 ) %7B%0A%0A%09%09/
@@ -3119,63 +3119,131 @@
 ll;%0A
-%0A%09%09// Try to release another entity%0A%09%09this.startAction(
+%09%7D%0A%0A%09@Override%0A%09protected double getProcessingTime(double simTime) %7B%0A%09%09return releaseDelay.getValue().getNextSample(simTime
 );%0A%09

use the new LinkedService framework for EntityGate