1 package net.sf.openrocket.simulation.customexpression;
5 import de.congrace.exp4j.Calculable;
6 import de.congrace.exp4j.Variable;
7 import net.sf.openrocket.document.OpenRocketDocument;
8 import net.sf.openrocket.logging.LogHelper;
9 import net.sf.openrocket.simulation.customexpression.CustomExpression;
10 import net.sf.openrocket.simulation.FlightDataType;
11 import net.sf.openrocket.simulation.SimulationStatus;
12 import net.sf.openrocket.startup.Application;
13 import net.sf.openrocket.util.LinearInterpolator;
15 public class IndexExpression extends CustomExpression {
18 private static final LogHelper log = Application.getLogger();
20 public IndexExpression(OpenRocketDocument doc, String indexText, String typeText){
23 setExpression(indexText);
25 this.setSymbol(typeText);
29 public Variable evaluate(SimulationStatus status){
31 Calculable calc = buildExpression();
33 return new Variable("Unknown");
36 // From the given datatype, get the time and function values and make an interpolator
38 //Note: must get in a way that flight data system will figure out units. Otherwise there will be a type conflict when we get the new data.
39 FlightDataType type = FlightDataType.getType(null, getSymbol(), null);
41 List<Double> data = status.getFlightData().get(type);
42 List<Double> time = status.getFlightData().get(FlightDataType.TYPE_TIME);
43 LinearInterpolator interp = new LinearInterpolator(time, data);
45 // Evaluate this expression to get the t value
47 double tvalue = calc.calculate().getDoubleValue();
48 return new Variable(hash(), interp.getValue( tvalue ) );
50 catch (java.util.EmptyStackException e){
51 log.user("Unable to calculate time index for indexed expression "+getExpressionString()+" due to empty stack exception");
52 return new Variable("Unknown");