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

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

public class Unpack extends LinkedService {

	@Keyword(description = "The service time required to unpack each entity.",
	         exampleList = { "3.0 h", "NormalDistribution1", "'1[s] + 0.5*[TimeSeries1].PresentValue'" })
	private final SampleInput serviceTime;

	private int numberToRemove;   // Number of entities to remove from the present EntityContainer

	{
		serviceTime = new SampleInput("ServiceTime", KEY_INPUTS, new SampleConstant(TimeUnit.class, 0.0));
		serviceTime.setUnitType(TimeUnit.class);
		serviceTime.setEntity(this);
		serviceTime.setValidRange(0, Double.POSITIVE_INFINITY);
		this.addInput(serviceTime);
	}

	private EntityContainer container;	// the received EntityContainer
	private int numberRemoved;   // Number of entities removed from the received EntityContainer

	public Unpack() {}

	@Override
	public void earlyInit() {
		super.earlyInit();
		container = null;
		numberRemoved = 0;
	}

	@Override
	protected boolean startProcessing(double simTime) {

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

		// Is there a container waiting to be unpacked?
		if (container == null && waitQueue.getValue().getMatchCount(m) == 0) {
			return false;
		}

		if (container == null) {

			// Remove the container from the queue
			container = (EntityContainer)this.getNextEntityForMatch(m);
			numberToRemove = this.getNumberToRemove();
			numberRemoved = 0;
		}

		return true;
	}

	protected void disposeContainer(EntityContainer c) {
		c.kill();
	}

	protected int getNumberToRemove() {
		return Integer.MAX_VALUE;
	}

	@Override
	protected boolean processStep(double simTime) {

		// Remove the next entity from the container
		if (numberRemoved < numberToRemove && container.getCount() > 0) {
			this.sendToNextComponent(container.removeEntity());
			numberRemoved++;
		}

		// Stop when the desired number of entities have been removed
		if (container.getCount() == 0 || numberRemoved == numberToRemove) {
			this.disposeContainer(container);
			container = null;
		}

		return true;
	}

	@Override
	public void updateGraphics(double simTime) {
		if (container == null)
			return;
		moveToProcessPosition(container);
	}

	@Override
	protected double getStepDuration(double simTime) {
		double dur = 0.0;
		if (numberRemoved < numberToRemove && container.getCount() > 0)
			dur = serviceTime.getValue().getNextSample(simTime);
		return dur;
	}

}

@@ -838,102 +838,1019 @@
 sim.
-input.Keyword;%0Aimport com.jaamsim.units.TimeUnit;%0A%0Apublic class Unpack extends LinkedService %7B
+StringProviders.StringProvInput;%0Aimport com.jaamsim.input.Keyword;%0Aimport com.jaamsim.units.DimensionlessUnit;%0Aimport com.jaamsim.units.TimeUnit;%0A%0Apublic class Unpack extends LinkedService %7B%0A%0A%09@Keyword(description = %22An expression returning a string value that determines which of the %22%0A%09                     + %22entities in the container are eligible to be removed. %22%0A%09                     + %22If used, the only entities eligible for selection are the ones whose %22%0A%09                     + %22inputs for the container's Match keyword are equal to value returned by %22%0A%09                     + %22the expression entered for this Match keyword. %22%0A%09                     + %22Expressions that return a dimensionless integer or an object are also %22%0A%09                     + %22valid. The returned number or object is converted to a string %22%0A%09                     + %22automatically. A floating point number is truncated to an integer.%22,%0A%09         exampleList = %7B%22this.obj.Attrib1%22%7D)%0A%09private final StringProvInput matchForEntities;
 %0A%0A%09@
@@ -2167,16 +2167,225 @@
 ner%0A%0A%09%7B%0A
+%09%09matchForEntities = new StringProvInput(%22MatchForEntities%22, KEY_INPUTS, null);%0A%09%09matchForEntities.setUnitType(DimensionlessUnit.class);%0A%09%09matchForEntities.setEntity(this);%0A%09%09this.addInput(matchForEntities);%0A%0A
 %09%09servic

add keyword 'MatchForEntities' to Unpack