/*
 * 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.BasicObjects;

import com.jaamsim.DisplayModels.DisplayModel;
import com.jaamsim.Samples.SampleInput;
import com.jaamsim.units.TimeUnit;
import com.sandwell.JavaSimulation.Entity;
import com.sandwell.JavaSimulation.EntityListInput;
import com.sandwell.JavaSimulation.EntityTarget;
import com.sandwell.JavaSimulation.InputErrorException;
import com.sandwell.JavaSimulation.Keyword;
import com.sandwell.JavaSimulation.Process;
import com.sandwell.JavaSimulation3D.DisplayEntity;

/**
 * EntityGenerator creates sequence of DisplayEntities at random intervals, which are placed in a target Queue.
 */
public class EntityGenerator extends LinkedComponent {

	@Keyword(description = "The probability distribution object used to select the inter-arrival time between generated DisplayEntities.",
	         example = "EntityGenerator1 IATdistribution { Dist1 }")
	private final SampleInput iatDistributionInput;

	@Keyword(description = "The list of DisplayModels to be assigned to the generated DisplayEntities.",
	         example = "EntityGenerator1 GeneratedDisplayModelList { Sphere }")
	private final EntityListInput<DisplayModel> generatedDisplayModelListInput;

	{
		iatDistributionInput = new SampleInput( "IATdistribution", "Key Inputs", null);
		iatDistributionInput.setUnitType( TimeUnit.class );
		this.addInput( iatDistributionInput, true);

		generatedDisplayModelListInput = new EntityListInput<DisplayModel>( DisplayModel.class, "DisplayModelList", "Key Inputs", null);
		this.addInput( generatedDisplayModelListInput, true);
	}

	public EntityGenerator() {
	}

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

		// Confirm that probability distribution has been specified
		if( iatDistributionInput.getValue() == null ) {
			throw new InputErrorException( "The keyword IATdistribution must be set." );
		}

		iatDistributionInput.verifyUnit();
	}

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

		// Start a new process for the Generator's main creation loop
		Process.start(new CreateEntitiesTarget(this));
	}

	private static class CreateEntitiesTarget extends EntityTarget<EntityGenerator> {
		public CreateEntitiesTarget(EntityGenerator ent) {
			super(ent, "createEntities");
		}

		@Override
		public void process() {
			ent.createEntities();
		}
	}

	/**
	* Loop endlessly creating DisplayEntities
	*/
	public void createEntities() {

		while( true ) {

			// Determine the interarrival time for the next creation event
			double dt = iatDistributionInput.getValue().getNextSample(getSimTime());

			// Schedule the creation event at this time
			this.simWait( dt );

			// Create the new DisplayEntity
			DisplayEntity newDisplayEntity = new DisplayEntity();
			newDisplayEntity.setFlag(Entity.FLAG_GENERATED);

			//  Send the entity to the next element in the chain
			this.sendToNextComponent( newDisplayEntity );
		}
	}

}

@@ -653,34 +653,27 @@
 sim.
-DisplayModels.DisplayModel
+Samples.SampleInput
 ;%0Aim
@@ -689,34 +689,31 @@
 jaamsim.
-Samples.SampleInpu
+input.InputAgen
 t;%0Aimpor
@@ -822,28 +822,24 @@
 ation.Entity
-List
 Input;%0Aimpor
@@ -1540,77 +1540,109 @@
 The 
-list of DisplayModels to be assigned to the generated DisplayEntities
+prototype for entities to be generated.%5Cn%22 +%0A%09%09%09%22The generated entities will be copies of this entity
 .%22,%0A
@@ -1681,44 +1681,33 @@
 ator
+-
 1 
-GeneratedDisplayModelList %7B Sphere
+PrototypeEntity %7B Ship
  %7D%22)
@@ -1724,28 +1724,24 @@
 final Entity
-List
 Input%3CDispla
@@ -1745,40 +1745,31 @@
 play
-Model%3E generatedDisplayModelList
+Entity%3E prototypeEntity
 Inpu
@@ -1964,33 +1964,23 @@
 %0A%0A%09%09
-generatedDisplayModelList
+prototypeEntity
 Inpu
@@ -1993,20 +1993,16 @@
 w Entity
-List
 Input%3CDi
@@ -2006,21 +2006,22 @@
 %3CDisplay
-Model
+Entity
 %3E( Displ
@@ -2022,21 +2022,22 @@
  Display
-Model
+Entity
 .class, 
@@ -2037,32 +2037,31 @@
 class, %22
-DisplayModelList
+PrototypeEntity
 %22, %22Key 
@@ -2097,33 +2097,23 @@
 ut( 
-generatedDisplayModelList
+prototypeEntity
 Inpu
@@ -2413,16 +2413,205 @@
 );%0A%09%09%7D%0A%0A
+%09%09// Confirm that prototype entity has been specified%0A%09%09if( prototypeEntityInput.getValue() == null ) %7B%0A%09%09%09throw new InputErrorException( %22The keyword PrototypeEntity must be set.%22 );%0A%09%09%7D%0A%0A
 %09%09iatDis
@@ -3146,16 +3146,44 @@
 es() %7B%0A%0A
+%09%09int numberGenerated = 0;%0A%0A
 %09%09while(
@@ -3449,81 +3449,297 @@
 %0A%09%09%09
-DisplayEntity newDisplayEntity = new DisplayEntity();%0A%09%09%09newDisplayEntity
+numberGenerated++;%0A%09%09%09DisplayEntity proto = prototypeEntityInput.getValue();%0A%09%09%09String name = String.format(%22Copy_of_%25s-%25s%22, proto.getInputName(), numberGenerated);%0A%09%09%09DisplayEntity ent = InputAgent.defineEntityWithUniqueName(proto.getClass(), name, true);%0A%09%09%09ent.copyInputs(proto);%0A%09%09%09ent
 .set
@@ -3857,24 +3857,11 @@
 nt( 
-newDisplayEntity
+ent
  );%0A

Use prototype entity for EntityGenerator