Merge commit '42b2e5ca519766e37ce6941ba4faecc9691cc403' into upstream
[debian/openrocket] / core / src / net / sf / openrocket / simulation / customexpression / IndexExpression.java
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 (file)
index 0000000..6fb084d
--- /dev/null
@@ -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<Double> data = status.getFlightData().get(type);
+               List<Double> 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");
+               }
+               
+       }
+}