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

import com.jaamsim.input.Keyword;
import com.sandwell.JavaSimulation.EntityListInput;
import com.sandwell.JavaSimulation.DoubleListInput;
import com.sandwell.JavaSimulation.InputErrorException;

/**
 * The WeightedSum object returns a weighted sum of its input values.
 * @author Harry King
 *
 */
public class WeightedSum extends DoubleCalculation {

	@Keyword(description = "The list of DoubleCalculations entities that are inputs to this calculation.",
	         example = "WeightedSum1 EntityList { Calc1  Calc2 }")
	private final EntityListInput<DoubleCalculation> entityList;

	@Keyword(description = "The list of multaplicative factors to be applied to the value provide by the inputs.",
	         example = "WeightedSum1 CoefficientList { 2.0  1.5 }")
	private final DoubleListInput coefficientList;

	{
		inputValue.setHidden(true);

		entityList = new EntityListInput<DoubleCalculation>( DoubleCalculation.class, "EntityList", "Key Inputs", null);
		this.addInput( entityList, true);

		coefficientList = new DoubleListInput( "CoefficientList", "Key Inputs", null);
		this.addInput( coefficientList, true);
	}

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

		// Confirm that the number of entries in the CoeffientList matches the EntityList
		if( coefficientList.getValue().size() != entityList.getValue().size() ) {
			throw new InputErrorException( "The number of entries for CoefficientList and EntityList must be equal" );
		}
	}

	@Override
	public void update(double simTime) {
		double val = 0.0;

		// Calculate the weighted sum
		for(int i=0; i<entityList.getValue().size(); i++ ) {
			val += coefficientList.getValue().get(i) * entityList.getValue().get(i).getValue();
		}

		// Set the present value
		this.setValue( val );
		return;
	}
}

@@ -659,72 +659,108 @@
 sim.
-input.Keyword;%0Aimport com.sandwell.JavaSimulation.EntityListInpu
+Samples.SampleListInput;%0Aimport com.jaamsim.input.Keyword;%0Aimport com.jaamsim.units.DimensionlessUni
 t;%0Ai
@@ -1065,71 +1065,166 @@
  of 
-DoubleCalculations
+inputs to the weighted sum.%5Cn%22 +%0A%09%09%09%22The inputs can be any
  entit
-ies
+y
  that 
-a
 re
- inputs to this calculation
+turns a number, such as a CalculationObject, ProbabilityDistribution, or a TimeSeries
 .%22,%0A
@@ -1259,16 +1259,21 @@
 dSum
+-
 1 
-Entity
+InputValue
 List
@@ -1283,15 +1283,17 @@
 Calc
+-
 1  Calc
+-
 2 %7D%22
@@ -1309,22 +1309,22 @@
 e final 
-Entity
+Sample
 ListInpu
@@ -1328,34 +1328,19 @@
 nput
-%3CDoubleCalculation%3E entity
+ inputValue
 List
@@ -1604,22 +1604,26 @@
 ue);%0A%0A%09%09
-entity
+inputValue
 List = n
@@ -1625,22 +1625,22 @@
 t = new 
-Entity
+Sample
 ListInpu
@@ -1644,86 +1644,101 @@
 nput
-%3CDoubleCalculation%3E( DoubleCalculation.class, %22EntityList%22, %22Key Inputs%22, null
+( %22InputValueList%22, %22Key Inputs%22, null);%0A%09%09inputValueList.setUnitType(DimensionlessUnit.class
 );%0A%09
@@ -1753,22 +1753,26 @@
 dInput( 
-entity
+inputValue
 List, tr
@@ -2088,22 +2088,26 @@
 ze() != 
-entity
+inputValue
 List.get
@@ -2362,22 +2362,26 @@
  i=0; i%3C
-entity
+inputValue
 List.get
@@ -2450,22 +2450,26 @@
 et(i) * 
-entity
+inputValue
 List.get
@@ -2486,22 +2486,34 @@
 t(i).get
-Value(
+NextSample(simTime
 );%0A%09%09%7D%0A%0A

Convert WeightedSum to accept SampleProviders