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

import com.jaamsim.input.Keyword;
import com.jaamsim.input.ValueInput;
import com.jaamsim.units.TimeUnit;
import com.sandwell.JavaSimulation.EntityInput;
import com.sandwell.JavaSimulation.InputErrorException;
import com.sandwell.JavaSimulation3D.DisplayEntity;
import com.sandwell.JavaSimulation3D.Queue;

public class EntityGate extends LinkedService {

	@Keyword(description = "The queue in which the waiting DisplayEntities will be placed.",
	         example = "EntityGate1 WaitQueue { Queue1 }")
	private final EntityInput<Queue> waitQueue;

	@Keyword(description = "The time delay before each queued entity is released.\n" +
			"Entities arriving at an open gate are not delayed.",
	         example = "EntityGate1 ReleaseDelay { 5.0 s }")
	private final ValueInput releaseDelay;

	{
		waitQueue = new EntityInput<Queue>( Queue.class, "WaitQueue", "Key Inputs", null);
		this.addInput( waitQueue);

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

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

		// Confirm that the target queue has been specified
		if( waitQueue.getValue() == null ) {
			throw new InputErrorException( "The keyword WaitQueue must be set." );
		}
	}

	@Override
	public void earlyInit() {
		super.earlyInit();
		this.setPresentState();
	}

	@Override
	public void addDisplayEntity( DisplayEntity ent ) {
		super.addDisplayEntity(ent);

		// If the gate is closed or other entities are already queued, then add the entity to the queue
		Queue queue = waitQueue.getValue();
		if( queue.getCount() > 0 || this.isClosed() ) {
			queue.addLast( 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.sendToNextComponent( ent );
	}

	@Override
	public void startAction() {

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

		// Schedule the release of the next entity
		this.scheduleProcess(releaseDelay.getValue(), 5, endActionTarget);
	}

	/**
	 * 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(waitQueue.getValue().removeFirst());

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

}

@@ -1458,17 +1458,16 @@
 %3CQueue%3E(
- 
 Queue.cl
@@ -1521,17 +1521,16 @@
 ddInput(
- 
 waitQueu
@@ -1566,17 +1566,16 @@
 ueInput(
- 
 %22Release
@@ -1723,17 +1723,16 @@
 ddInput(
- 
 releaseD
@@ -1738,24 +1738,49 @@
 Delay);%0A%09%7D%0A%0A
+%09public EntityGate() %7B%7D%0A%0A
 %09@Override%0A%09
@@ -1883,18 +1883,18 @@
 ied%0A%09%09if
-(
  
+(
 waitQueu
@@ -1913,17 +1913,16 @@
  == null
- 
 ) %7B%0A%09%09%09t
@@ -1950,17 +1950,16 @@
 ception(
- 
 %22The key
@@ -1986,17 +1986,16 @@
 be set.%22
- 
 );%0A%09%09%7D%0A%09
@@ -2127,17 +2127,16 @@
 yEntity(
- 
 DisplayE
@@ -2144,17 +2144,16 @@
 tity ent
- 
 ) %7B%0A%09%09su
@@ -2320,18 +2320,18 @@
 ();%0A%09%09if
-(
  
+(
 queue.ge
@@ -2361,17 +2361,16 @@
 Closed()
- 
 ) %7B%0A%09%09%09q
@@ -2382,21 +2382,19 @@
 addLast(
- 
 ent
- 
 );%0A%09%09%09re
@@ -2555,13 +2555,11 @@
 ent(
- 
 ent
- 
 );%0A%09

Use standard code style for EntityGate