From a7e73f6485e2aad2bcaa67bd56f717e5cec39b6c Mon Sep 17 00:00:00 2001 From: richard Date: Sun, 16 Dec 2012 22:58:02 -0800 Subject: [PATCH] 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. --- .../simulation/customexpression/CustomExpression.java | 6 ++++-- .../CustomExpressionSimulationListener.java | 4 +--- .../simulation/customexpression/IndexExpression.java | 11 +++++++++-- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/core/src/net/sf/openrocket/simulation/customexpression/CustomExpression.java b/core/src/net/sf/openrocket/simulation/customexpression/CustomExpression.java index c8973ed2d..a78916921 100644 --- a/core/src/net/sf/openrocket/simulation/customexpression/CustomExpression.java +++ b/core/src/net/sf/openrocket/simulation/customexpression/CustomExpression.java @@ -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"); } } diff --git a/core/src/net/sf/openrocket/simulation/customexpression/CustomExpressionSimulationListener.java b/core/src/net/sf/openrocket/simulation/customexpression/CustomExpressionSimulationListener.java index 243f7b155..ecba29943 100644 --- a/core/src/net/sf/openrocket/simulation/customexpression/CustomExpressionSimulationListener.java +++ b/core/src/net/sf/openrocket/simulation/customexpression/CustomExpressionSimulationListener.java @@ -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 diff --git a/core/src/net/sf/openrocket/simulation/customexpression/IndexExpression.java b/core/src/net/sf/openrocket/simulation/customexpression/IndexExpression.java index 6fb084de4..f712d7f93 100644 --- a/core/src/net/sf/openrocket/simulation/customexpression/IndexExpression.java +++ b/core/src/net/sf/openrocket/simulation/customexpression/IndexExpression.java @@ -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 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"); } - } }