/*
 * JaamSim Discrete Event Simulation
 * Copyright (C) 2013 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.input.Keyword;
import com.jaamsim.input.ValueInput;
import com.jaamsim.rng.MRG1999a;
import com.jaamsim.units.Unit;
import com.jaamsim.units.UserSpecifiedUnit;

/**
 * Exponential Distribution.
 * Adapted from A.M. Law, "Simulation Modelling and Analysis, 4th Edition", page 448.
 */
public class ExponentialDistribution extends Distribution {

	@Keyword(description = "The mean of the exponential distribution.",
	         exampleList = {"5.0"})
	private final ValueInput meanInput;

	private final MRG1999a rng = new MRG1999a();

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

		meanInput = new ValueInput("Mean", "Key Inputs", 1.0d);
		meanInput.setUnitType(UserSpecifiedUnit.class);
		meanInput.setValidRange(0.0d, Double.POSITIVE_INFINITY);
		this.addInput(meanInput);
	}

	public ExponentialDistribution() {}

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

	@Override
	protected void setUnitType(Class<? extends Unit> specified) {
		super.setUnitType(specified);
		meanInput.setUnitType(specified);
	}

	@Override
	protected double getSample(double simTime) {

		// Inverse transform method
		return (-meanInput.getValue() * Math.log(rng.nextUniform()));
	}

	@Override
	protected double getMean(double simTime) {
		return meanInput.getValue();
	}

	@Override
	protected double getStandardDev(double simTime) {
		return meanInput.getValue();
	}
}

@@ -806,21 +806,27 @@
 sim.
-input.Keyword
+Samples.SampleInput
 ;%0Aim
@@ -848,26 +848,23 @@
 m.input.
-ValueInput
+Keyword
 ;%0Aimport
@@ -1249,16 +1249,60 @@
 = %7B%225.0%22
+, %22InputValue1%22, %22'2 * %5BInputValue1%5D.Value'%22
 %7D)%0A%09priv
@@ -1311,20 +1311,21 @@
 e final 
-Valu
+Sampl
 eInput m
@@ -1463,20 +1463,21 @@
 t = new 
-Valu
+Sampl
 eInput(%22
@@ -1501,13 +1501,33 @@
 s%22, 
+new SampleConstant(
 1.0d)
+)
 ;%0A%09%09
@@ -1633,16 +1633,45 @@
 INITY);%0A
+%09%09meanInput.setEntity(this);%0A
 %09%09this.a
@@ -2097,17 +2097,22 @@
 d%0A%09%09
-return (-
+double mean = 
 mean
@@ -2127,16 +2127,56 @@
 tValue()
+.getNextSample(simTime);%0A%09%09return (-mean
  * Math.
@@ -2284,24 +2284,47 @@
 t.getValue()
+.getNextSample(simTime)
 ;%0A%09%7D%0A%0A%09@Over
@@ -2371,32 +2371,32 @@
 uble simTime) %7B%0A
-
 %09%09return meanInp
@@ -2408,15 +2408,38 @@
 tValue()
+.getNextSample(simTime)
 ;%0A%09%7D%0A%7D%0A

convert ExponentialDistribution keywords to SampleInput