X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=core%2Fsrc%2Fnet%2Fsf%2Fopenrocket%2Fsimulation%2Fcustomexpression%2FIndexExpression.java;fp=core%2Fsrc%2Fnet%2Fsf%2Fopenrocket%2Fsimulation%2Fcustomexpression%2FIndexExpression.java;h=6fb084de4154f18ab2b66436acf8334c497bf137;hb=9349577cdfdff682b2aabd6daa24fdc3a7449b58;hp=0000000000000000000000000000000000000000;hpb=30ba0a882f0c061176ba14dbf86d3d6fad096c02;p=debian%2Fopenrocket diff --git a/core/src/net/sf/openrocket/simulation/customexpression/IndexExpression.java b/core/src/net/sf/openrocket/simulation/customexpression/IndexExpression.java new file mode 100644 index 00000000..6fb084de --- /dev/null +++ b/core/src/net/sf/openrocket/simulation/customexpression/IndexExpression.java @@ -0,0 +1,56 @@ +package net.sf.openrocket.simulation.customexpression; + +import java.util.List; + +import de.congrace.exp4j.Calculable; +import de.congrace.exp4j.Variable; +import net.sf.openrocket.document.OpenRocketDocument; +import net.sf.openrocket.logging.LogHelper; +import net.sf.openrocket.simulation.customexpression.CustomExpression; +import net.sf.openrocket.simulation.FlightDataType; +import net.sf.openrocket.simulation.SimulationStatus; +import net.sf.openrocket.startup.Application; +import net.sf.openrocket.util.LinearInterpolator; + +public class IndexExpression extends CustomExpression { + + FlightDataType type; + private static final LogHelper log = Application.getLogger(); + + public IndexExpression(OpenRocketDocument doc, String indexText, String typeText){ + super(doc); + + setExpression(indexText); + this.setName(""); + this.setSymbol(typeText); + } + + @Override + public Variable evaluate(SimulationStatus status){ + + Calculable calc = buildExpression(); + if (calc == null){ + return new Variable("Unknown"); + } + + // From the given datatype, get the time and function values and make an interpolator + + //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. + FlightDataType type = FlightDataType.getType(null, getSymbol(), null); + + List data = status.getFlightData().get(type); + List time = status.getFlightData().get(FlightDataType.TYPE_TIME); + LinearInterpolator interp = new LinearInterpolator(time, data); + + // Evaluate this expression to get the t value + try{ + double tvalue = calc.calculate().getDoubleValue(); + return new Variable(hash(), interp.getValue( tvalue ) ); + } + catch (java.util.EmptyStackException e){ + log.user("Unable to calculate time index for indexed expression "+getExpressionString()+" due to empty stack exception"); + return new Variable("Unknown"); + } + + } +}