Fix bug with custom expressions where old sub expressions remained in an

expression after it was changed.

Fix bug to allow flight variables to be used inside index expressions.
This commit is contained in:
richard 2012-12-16 22:58:02 -08:00
parent 8c49d33731
commit a7e73f6485
3 changed files with 14 additions and 7 deletions

View File

@ -86,6 +86,7 @@ public class CustomExpression implements Cloneable{
this.expression = expression;
// Replace any indexed variables
subExpressions.clear();
expression = subTimeIndexes(expression);
expression = subTimeRanges(expression);
@ -450,13 +451,13 @@ public class CustomExpression implements Cloneable{
if ( !expressions.isEmpty() ) {
// check if expression already exists
if ( expressions.contains(this) ){
log.user("Expression already in document. This unit : "+this.getUnit()+", existing unit : "+expressions.get(0).getUnit());
log.debug("Expression already in document");
return;
}
}
if (this.checkAll()){
log.user("Custom expression added to rocket document");
log.user("New custom expression added to rocket document");
doc.addCustomExpression( this );
}
}
@ -470,6 +471,7 @@ public class CustomExpression implements Cloneable{
else {
int index = doc.getCustomExpressions().indexOf(this);
doc.getCustomExpressions().set(index, newExpression);
log.debug("Overwriting custom expression already in document");
}
}

View File

@ -29,11 +29,9 @@ public class CustomExpressionSimulationListener extends AbstractSimulationListen
FlightDataBranch data = status.getFlightData();
for (CustomExpression expression : expressions ) {
double value = expression.evaluateDouble(status);
//log.debug("Setting value of custom expression "+expression.toString()+" = "+value);
log.debug("Setting value of custom expression "+expression.toString()+" = "+value);
data.setValue(expression.getType(), value);
}
}
@Override

View File

@ -3,6 +3,7 @@ package net.sf.openrocket.simulation.customexpression;
import java.util.List;
import de.congrace.exp4j.Calculable;
import de.congrace.exp4j.ExpressionBuilder;
import de.congrace.exp4j.Variable;
import net.sf.openrocket.document.OpenRocketDocument;
import net.sf.openrocket.logging.LogHelper;
@ -27,7 +28,6 @@ public class IndexExpression extends CustomExpression {
@Override
public Variable evaluate(SimulationStatus status){
Calculable calc = buildExpression();
if (calc == null){
return new Variable("Unknown");
@ -42,15 +42,22 @@ public class IndexExpression extends CustomExpression {
List<Double> time = status.getFlightData().get(FlightDataType.TYPE_TIME);
LinearInterpolator interp = new LinearInterpolator(time, data);
// Set the variables in the expression to evaluate
for (FlightDataType etype : status.getFlightData().getTypes()){
double value = status.getFlightData().getLast(etype);
calc.setVariable( new Variable(etype.getSymbol(), value ) );
}
// Evaluate this expression to get the t value
//System.out.println("Evaluating expression to get t value "+this.getExpressionString());
try{
double tvalue = calc.calculate().getDoubleValue();
//System.out.println("t = "+tvalue);
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");
}
}
}