- Implemented a DampingMoment simulation listener example
[debian/openrocket] / core / src / net / sf / openrocket / simulation / customexpression / IndexExpression.java
1 package net.sf.openrocket.simulation.customexpression;
2
3 import java.util.List;
4
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;
14
15 public class IndexExpression extends CustomExpression {
16
17         FlightDataType type;
18         private static final LogHelper log = Application.getLogger();
19         
20         public IndexExpression(OpenRocketDocument doc, String indexText, String typeText){
21                 super(doc);
22                 
23                 setExpression(indexText);
24                 this.setName("");
25                 this.setSymbol(typeText);
26         }
27         
28         @Override
29         public Variable evaluate(SimulationStatus status){
30                 
31                 Calculable calc = buildExpression();
32                 if (calc == null){
33                         return new Variable("Unknown");
34                 }
35                 
36                 // From the given datatype, get the time and function values and make an interpolator
37
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);  
40                                 
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); 
44                 
45                 // Evaluate this expression to get the t value
46                 try{
47                         double tvalue = calc.calculate().getDoubleValue();
48                         return new Variable(hash(), interp.getValue( tvalue ) );
49                 }
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");
53                 }
54                 
55         }
56 }