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

import com.jaamsim.Samples.SampleConstant;
import com.jaamsim.events.EventManager;
import com.jaamsim.input.Keyword;
import com.jaamsim.input.TimeSeriesInput;
import com.jaamsim.rng.MRG1999a;
import com.jaamsim.units.DimensionlessUnit;
import com.jaamsim.units.TimeUnit;

/**
 * Non-Stationary Exponential Distribution.
 * Adapted from A.M. Law, "Simulation Modelling and Analysis, 5th Edition", page 479.
 */
public class NonStatExponentialDist extends Distribution {

	@Keyword(description = "A time series containing the expected cumulative number of arrivals as a function of time.",
			exampleList = {"TimeSeries1"})
	private final TimeSeriesInput expectedArrivals;

	private final MRG1999a rng = new MRG1999a();

	{
		minValueInput.setDefaultValue(new SampleConstant(0.0d));

		unitType.setHidden(true);
		unitType.setDefaultValue(TimeUnit.class);
		this.setUnitType(TimeUnit.class);

		expectedArrivals = new TimeSeriesInput("ExpectedArrivals", KEY_INPUTS, null);
		expectedArrivals.setUnitType(DimensionlessUnit.class);
		expectedArrivals.setRequired(true);
		this.addInput(expectedArrivals);
	}

	public NonStatExponentialDist() {}

	@Override
	public void earlyInit() {
		super.earlyInit();
		rng.setSeedStream(getStreamNumber(), getSubstreamNumber());
	}

	@Override
	protected double getSample(double simTime) {

		long ticksNow = getSimTicks();  // ignore the simTime passed as an argument
		double valueNow = expectedArrivals.getValue().getInterpolatedCumulativeValueForTicks(ticksNow);
		double valueNext = valueNow - Math.log(rng.nextUniform());
		long ticksNext = expectedArrivals.getValue().getInterpolatedTicksForValue(valueNext);

		if (ticksNext == Long.MAX_VALUE)
			return Double.POSITIVE_INFINITY;

		if (ticksNext < ticksNow)
			error("Negative time advance");

		return EventManager.ticksToSecs(ticksNext - ticksNow);
	}

	@Override
	protected double getMean(double simTime) {
		if (expectedArrivals.getValue() == null)
			return Double.NaN;
		double arrivals = expectedArrivals.getValue().getMaxValue();
		double dt = EventManager.ticksToSecs( expectedArrivals.getValue().getMaxTicksValue() );
		return dt/arrivals;
	}

	@Override
	protected double getStandardDev(double simTime) {
		return 0.0d;
	}

}

@@ -108,16 +108,21 @@
 (C) 2016
+-2018
  JaamSim
@@ -136,16 +136,16 @@
 re Inc.%0A
-
  *%0A * Li
@@ -788,16 +788,55 @@
 nstant;%0A
+import com.jaamsim.Samples.TimeSeries;%0A
 import c
@@ -867,16 +867,62 @@
 anager;%0A
+import com.jaamsim.input.InputErrorException;%0A
 import c
@@ -1393,16 +1393,43 @@
 rrivals 
+%22%0A%09                     + %22
 as a fun
@@ -1962,16 +1962,16 @@
 s);%0A%09%7D%0A%0A
-
 %09public 
@@ -1999,16 +1999,647 @@
 t() %7B%7D%0A%0A
+%09@Override%0A%09public void validate() %7B%0A%09%09super.validate();%0A%0A%09%09if (!(expectedArrivals.getValue() instanceof TimeSeries))%0A%09%09%09throw new InputErrorException(%22The ExpectedArrivals input must be a TimeSeries, %22%0A%09%09%09%09%09+ %22not a constant.%22);%0A%0A%09%09TimeSeries ts = (TimeSeries) (expectedArrivals.getValue());%0A%0A%09%09if (!ts.isMonotonic(1))%0A%09%09%09throw new InputErrorException(%22The ExpectedArrivals input must be a TimeSeries %22%0A%09%09%09%09%09+ %22that increases monotonically.%22);%0A%0A%09%09if (ts.getMinValue() != 0.0d)%0A%09%09%09throw new InputErrorException(%22The ExpectedArrivals input must be a TimeSeries %22%0A%09%09%09%09%09+ %22that starts with zero expected arrivals at time zero.%22);%0A%09%7D%0A%0A
 %09@Overri

validate the ExpectedArrivals input to NonStatExponentialDist