diff --git a/core/src/net/sf/openrocket/file/openrocket/importt/OpenRocketLoader.java b/core/src/net/sf/openrocket/file/openrocket/importt/OpenRocketLoader.java index a56f0d78e..3bc872b95 100644 --- a/core/src/net/sf/openrocket/file/openrocket/importt/OpenRocketLoader.java +++ b/core/src/net/sf/openrocket/file/openrocket/importt/OpenRocketLoader.java @@ -1202,7 +1202,6 @@ class SimulationsHandler extends AbstractElementHandler { } class SingleSimulationHandler extends AbstractElementHandler { - private static final LogHelper log = Application.getLogger(); private final DocumentLoadingContext context; @@ -1225,6 +1224,10 @@ class SingleSimulationHandler extends AbstractElementHandler { public void setCustomExpressions(ArrayList expressions){ this.customExpressions = expressions; } + + public ArrayList getCustomExpressions(){ + return customExpressions; + } @Override public ElementHandler openElement(String element, HashMap attributes, @@ -1240,7 +1243,7 @@ class SingleSimulationHandler extends AbstractElementHandler { conditionHandler = new SimulationConditionsHandler(doc.getRocket(), context); return conditionHandler; } else if (element.equals("flightdata")) { - dataHandler = new FlightDataHandler(context); + dataHandler = new FlightDataHandler(this, context); return dataHandler; } else { warnings.add("Unknown element '" + element + "', ignoring."); @@ -1545,12 +1548,14 @@ class FlightDataHandler extends AbstractElementHandler { private FlightDataBranchHandler dataHandler; private WarningSet warningSet = new WarningSet(); private List branches = new ArrayList(); - + + private SingleSimulationHandler simHandler; private FlightData data; - public FlightDataHandler(DocumentLoadingContext context) { + public FlightDataHandler(SingleSimulationHandler simHandler, DocumentLoadingContext context) { this.context = context; + this.simHandler = simHandler; } public FlightData getFlightData() { @@ -1569,8 +1574,9 @@ class FlightDataHandler extends AbstractElementHandler { warnings.add("Illegal flight data definition, ignoring."); return null; } - dataHandler = new FlightDataBranchHandler(attributes.get("name"), - attributes.get("types"), context); + dataHandler = new FlightDataBranchHandler( attributes.get("name"), + attributes.get("types"), + simHandler, context); return dataHandler; } @@ -1666,19 +1672,52 @@ class FlightDataBranchHandler extends AbstractElementHandler { private final DocumentLoadingContext context; private final FlightDataType[] types; private final FlightDataBranch branch; - - public FlightDataBranchHandler(String name, String typeList, DocumentLoadingContext context) { + + private static final LogHelper log = Application.getLogger(); + private final SingleSimulationHandler simHandler; + + public FlightDataBranchHandler(String name, String typeList, SingleSimulationHandler simHandler, DocumentLoadingContext context) { + this.simHandler = simHandler; this.context = context; String[] split = typeList.split(","); types = new FlightDataType[split.length]; for (int i = 0; i < split.length; i++) { - types[i] = FlightDataType.getType(split[i], "None ("+split[i]+")", UnitGroup.UNITS_NONE); - // TODO: HIGH: Deal with symbols + String typeName = split[i]; + FlightDataType matching = findFlightDataType(typeName); + types[i] = matching; + //types[i] = FlightDataType.getType(typeName, matching.getSymbol(), matching.getUnitGroup()); } // TODO: LOW: May throw an IllegalArgumentException branch = new FlightDataBranch(name, types); } + + // Find the full flight data type given name only + // Note: this way of doing it requires that custom expressions always come before flight data in the file, + // not the nicest but this is always the case anyway. + private FlightDataType findFlightDataType(String name){ + + // Look in built in types + for (FlightDataType t : FlightDataType.ALL_TYPES){ + if (t.getName().equals(name) ){ + return t; + } + } + + // Look in custom expressions, meanwhile set priority based on order in file + int totalExpressions = simHandler.getCustomExpressions().size(); + for (int i=0; i AVAILABLE_OPERATORS = new TreeMap() {{ @@ -78,11 +78,11 @@ public class CustomExpression implements Cloneable{ * Use this to update the simulation this is associated with */ public void setSimulation(Simulation sim){ - CustomExpression.sim = sim; + this.sim = sim; } public Simulation getSimulation() { - return CustomExpression.sim; + return this.sim; } /* @@ -90,10 +90,11 @@ public class CustomExpression implements Cloneable{ * if no simulated data exists */ private FlightDataBranch getBranch() { - if ( sim == null || sim.getSimulatedData().getBranch(0) == null) { + if ( sim == null || sim.getSimulatedData().getBranchCount() == 0){//sim.getSimulatedData().getBranch(0) == null) { return new FlightDataBranch(); } else { + System.out.println("Using existing branch"); return sim.getSimulatedData().getBranch(0); } } @@ -145,7 +146,7 @@ public class CustomExpression implements Cloneable{ return false; // No bad characters - for (char c : "0123456789.()[]{}".toCharArray()) + for (char c : "0123456789.,()[]{}<> ".toCharArray()) if (symbol.indexOf(c) != -1 ) return false; @@ -170,6 +171,11 @@ public class CustomExpression implements Cloneable{ if (name.trim().isEmpty()) return false; + // No characters that could mess things up saving etc + for (char c : ",()[]{}<>".toCharArray()) + if (symbol.indexOf(c) != -1 ) + return false; + ArrayList names = getAllNames().clone(); if (names.contains(name.trim())){ int index = names.indexOf(name.trim()); @@ -217,6 +223,7 @@ public class CustomExpression implements Cloneable{ // Define the available variables as 0 for (FlightDataType type : getBranch().getTypes()){ + System.out.println( " " + type.getSymbol() ); builder.withVariable(type.getSymbol(), 0.0); } @@ -260,8 +267,14 @@ public class CustomExpression implements Cloneable{ * Returns the new flight data type corresponding to this calculated data */ public FlightDataType getType(){ + // Figure out priority from order in array so that customs expressions are always at the top + + int totalExpressions = sim.getCustomExpressions().size(); + int p = -1*(totalExpressions-sim.getCustomExpressions().indexOf(this)); UnitGroup ug = new FixedUnitGroup(unit); - return FlightDataType.getType(name, symbol, ug); + FlightDataType type = FlightDataType.getType(name, symbol, ug); + type.setPriority(p); + return type; } /* diff --git a/core/src/net/sf/openrocket/simulation/FlightDataBranch.java b/core/src/net/sf/openrocket/simulation/FlightDataBranch.java index 475a068ee..76bef7642 100644 --- a/core/src/net/sf/openrocket/simulation/FlightDataBranch.java +++ b/core/src/net/sf/openrocket/simulation/FlightDataBranch.java @@ -106,8 +106,8 @@ public class FlightDataBranch implements Monitorable { mutable.check(); ArrayList list = values.get(type); + if (list == null) { - list = new ArrayList(); int n = getLength(); for (int i = 0; i < n; i++) { @@ -115,10 +115,13 @@ public class FlightDataBranch implements Monitorable { } values.put(type, list); minValues.put(type, value); - maxValues.put(type, value); - + maxValues.put(type, value); } - list.set(list.size() - 1, value); + + if (list.size() > 0){ + list.set(list.size() - 1, value); + } + double min = minValues.get(type); double max = maxValues.get(type); diff --git a/core/src/net/sf/openrocket/simulation/FlightDataType.java b/core/src/net/sf/openrocket/simulation/FlightDataType.java index 9a7df7642..eb8a8d90d 100644 --- a/core/src/net/sf/openrocket/simulation/FlightDataType.java +++ b/core/src/net/sf/openrocket/simulation/FlightDataType.java @@ -37,7 +37,7 @@ public class FlightDataType implements Comparable { //// Vertical position and motion //// Altitude - public static final FlightDataType TYPE_ALTITUDE = newType(trans.get("FlightDataType.TYPE_ALTITUDE"), "a", UnitGroup.UNITS_DISTANCE, 10); + public static final FlightDataType TYPE_ALTITUDE = newType(trans.get("FlightDataType.TYPE_ALTITUDE"), "h", UnitGroup.UNITS_DISTANCE, 10); //// Vertical velocity public static final FlightDataType TYPE_VELOCITY_Z = newType(trans.get("FlightDataType.TYPE_VELOCITY_Z"), "Vz", UnitGroup.UNITS_VELOCITY, 11); //// Vertical acceleration @@ -180,6 +180,7 @@ public class FlightDataType implements Comparable { // An array of all the built in types public static final FlightDataType[] ALL_TYPES = { + TYPE_TIME, TYPE_ALTITUDE , TYPE_VELOCITY_Z , TYPE_ACCELERATION_Z, @@ -243,7 +244,15 @@ public class FlightDataType implements Comparable { * @return a data type. */ public static synchronized FlightDataType getType(String s, String symbol, UnitGroup u) { + // modified to include the unit FlightDataType type = EXISTING_TYPES.get(s.toLowerCase(Locale.ENGLISH)); + + // added this for backward compatibility. Will update type if symbol undefined + //if (type != null && type.getSymbol() != symbol){ + // EXISTING_TYPES.remove(type); + // type = null; + //} + if (type != null) { return type; } @@ -264,7 +273,7 @@ public class FlightDataType implements Comparable { private final String name; private final String symbol; private final UnitGroup units; - private final int priority; + private int priority; private final int hashCode; @@ -281,7 +290,9 @@ public class FlightDataType implements Comparable { } - + public void setPriority(int p){ + this.priority = p; + } public String getName() { return name; @@ -297,7 +308,7 @@ public class FlightDataType implements Comparable { @Override public String toString() { - return name; + return name; //+" ("+symbol+") "+units.getDefaultUnit().toString(); } @Override