/*
 * 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.OutputHandle;
import com.sandwell.JavaSimulation.Entity;
import com.sandwell.JavaSimulation.EntityInput;
import com.sandwell.JavaSimulation.InputErrorException;
import com.sandwell.JavaSimulation.Keyword;
import com.sandwell.JavaSimulation.StringInput;

/**
 * The Sensor connect the output from some other object to the DoubleCalculation components.
 * @author Harry King
 *
 */
public class Sensor extends DoubleCalculation {

	@Keyword(description = "The entity to read the output from.",
	         example = "Sensor1 Entity { StockPile2 }")
	private final EntityInput<Entity> entityInput;

	@Keyword(description = "The name of the output to read.",
	         example = "Sensor1 OutputName { 'Contents' }")
	private final StringInput outputNameInput;

	{
		inputValueInput.setHidden(true);

		entityInput = new EntityInput<Entity>( Entity.class, "Entity", "Key Inputs", null);
		this.addInput(entityInput, true);

		outputNameInput = new StringInput( "OutputName", "Key Inputs", null);
		this.addInput(outputNameInput, true);
	}

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

		// Check that the entity has been set
		if ( entityInput.getValue() == null ) {
			throw new InputErrorException( "The Entity keyword must be set." );
		}

		// Check that the output has been set
		if ( outputNameInput.getValue() == null ) {
			throw new InputErrorException( "The OutputName keyword must be set." );
		}

		// Check that the output is a double or a Double
		Entity ent = entityInput.getValue();
		String name = outputNameInput.getValue();
		OutputHandle out = ent.getOutputHandle(name);
		Class<?> retType = out.getReturnType();
		if ( retType != Double.class && retType != double.class) {
			throw new InputErrorException( "The OutputName keyword must specify a floating point output." );
		}
	}

	@Override
	public void update(double simTime) {
		double val = 0.0;
		Entity ent = entityInput.getValue();
		String name = outputNameInput.getValue();

		OutputHandle out = ent.getOutputHandle(name);
		Class<?> retType = out.getReturnType();
		if (retType == Double.class) {
			val = out.getValue(simTime, Double.class);
		} else if (retType == double.class){
			val = out.getValue(simTime, double.class);
		}

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

@@ -2264,108 +2264,34 @@
 ;%0A%09%09
-Class%3C?%3E retType = out.getReturnType();%0A%09%09if ( retType != Double.class && retType != double.class) %7B
+if (!out.isNumericValue())
 %0A%09%09%09
@@ -2387,20 +2387,16 @@
 ut.%22 );%0A
-%09%09%7D%0A
 %09%7D%0A%0A%09@Ov
@@ -2598,165 +2598,33 @@
 ;%0A%09%09
-Class%3C?%3E retType = out.getReturnType();%0A%09%09if (retType == Double.class) %7B%0A%09%09%09val = out.getValue(simTime, Double.class);%0A%09%09%7D else if (retType == double.class)%7B
+if (out.isNumericValue())
 %0A%09%09%09
@@ -2641,16 +2641,24 @@
 getValue
+AsDouble
 (simTime
@@ -2663,26 +2663,14 @@
 me, 
-double.class);%0A%09%09%7D
+0.0d);
 %0A%0A%09%09

use the isNumeric helper in Sensor when wiring up Outputs