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

import com.jaamsim.Graphics.DisplayEntity;
import com.jaamsim.basicsim.ErrorException;
import com.jaamsim.input.ExpError;
import com.jaamsim.input.ExpEvaluator;
import com.jaamsim.input.ExpressionInput;
import com.jaamsim.input.Keyword;
import com.jaamsim.input.Output;
import com.jaamsim.units.DimensionlessUnit;

public class ExpressionEntity extends DisplayEntity {
	@Keyword(description = "The variable for which statistics will be collected.",
	         example = "Statistics1  SampleValue { 'this.obj.Attrib1' }")
	private final ExpressionInput sampleValue;

	{
		sampleValue = new ExpressionInput("Expression", "Key Inputs", null);
		sampleValue.setEntity(this);
		this.addInput(sampleValue);
	}

	public ExpressionEntity() {}

	@Output(name = "Value",
	 description = "The evaluated value of the expression.",
	    unitType = DimensionlessUnit.class,
	  reportable = true)
	public double evaluateExpression(double simTime) {
		if (sampleValue.getValue() == null)
			return 0.0d;

		try {
			// Evaluate the expression
			double ret = ExpEvaluator.evaluateExpression(sampleValue.getValue(), simTime, this).value;
			return ret;
		} catch(ExpError e) {
			throw new ErrorException(e);
		}
	}
}

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

import com.jaamsim.Graphics.DisplayEntity;
import com.jaamsim.basicsim.ErrorException;
import com.jaamsim.input.ExpError;
import com.jaamsim.input.ExpEvaluator;
import com.jaamsim.input.ExpressionInput;
import com.jaamsim.input.Keyword;
import com.jaamsim.input.Output;
import com.jaamsim.units.DimensionlessUnit;

public class ExpressionEntity extends DisplayEntity {

	@Keyword(description = "The expression to be evaluated.",
	         exampleList = {"'[Queue1].QueueLength + [Queue2].QueueLength'"})
	private final ExpressionInput sampleValue;

	{
		sampleValue = new ExpressionInput("Expression", "Key Inputs", null);
		sampleValue.setEntity(this);
		sampleValue.setRequired(true);
		this.addInput(sampleValue);
	}

	public ExpressionEntity() {}

	@Output(name = "Value",
	 description = "The evaluated value of the expression.",
	    unitType = DimensionlessUnit.class,
	  reportable = true)
	public double evaluateExpression(double simTime) {
		if (sampleValue.getValue() == null)
			return 0.0d;

		try {
			// Evaluate the expression
			double ret = ExpEvaluator.evaluateExpression(sampleValue.getValue(), simTime, this).value;
			return ret;
		} catch(ExpError e) {
			throw new ErrorException(e);
		}
	}
}

correct keyword description for ExpressionEntity and make it a required input