Fixed issue where symbols / units for FlightDataTypes would not be defined after...
authorrichardgraham <richardgraham@180e2498-e6e9-4542-8430-84ac67f01cd8>
Mon, 4 Jun 2012 03:08:29 +0000 (03:08 +0000)
committerrichardgraham <richardgraham@180e2498-e6e9-4542-8430-84ac67f01cd8>
Mon, 4 Jun 2012 03:08:29 +0000 (03:08 +0000)
Also changed the priority of the data types so that custom expressions show up first.

git-svn-id: https://openrocket.svn.sourceforge.net/svnroot/openrocket/trunk@741 180e2498-e6e9-4542-8430-84ac67f01cd8

core/src/net/sf/openrocket/file/openrocket/importt/OpenRocketLoader.java
core/src/net/sf/openrocket/simulation/CustomExpression.java
core/src/net/sf/openrocket/simulation/FlightDataBranch.java
core/src/net/sf/openrocket/simulation/FlightDataType.java

index a56f0d78e08b4f649c3a86a2e3c522426c5ff837..3bc872b9541eefb90e48d41be7d0e4bce4a92ae3 100644 (file)
@@ -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<CustomExpression> expressions){
                this.customExpressions = expressions;
        }
+       
+       public ArrayList<CustomExpression> getCustomExpressions(){
+               return customExpressions;
+       }
 
        @Override
        public ElementHandler openElement(String element, HashMap<String, String> 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<FlightDataBranch> branches = new ArrayList<FlightDataBranch>();
-
+       
+       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<totalExpressions; i++){
+                       CustomExpression exp = simHandler.getCustomExpressions().get(i);                        
+                       if (exp.getName().equals(name) ){
+                               FlightDataType t = exp.getType();
+                               t.setPriority(-1*(totalExpressions-i));
+                               return exp.getType();
+                       }
+               }
+               
+               log.warn("Could not find the flight data type '"+name+"' used in the XML file. Substituted type with unknown symbol and units.");
+               return FlightDataType.getType(name, "Unknown", UnitGroup.UNITS_NONE);
+       }
 
        public FlightDataBranch getBranch() {
                branch.immute();
index 1903079a2fb66be9b3437be639931e230532846d..1ca9bd08f19dce103472ca774b4067d7710ff8fb 100644 (file)
@@ -24,7 +24,7 @@ public class CustomExpression implements Cloneable{
        
        private String name, symbol, unit, expression;
        private ExpressionBuilder builder;
-       private static Simulation sim = null;
+       private Simulation sim = null;
        
        // A map of available operator strings (keys) and description of function (value)
        public static final SortedMap<String, String> AVAILABLE_OPERATORS = new TreeMap<String, String>() {{
@@ -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<String> 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;
        }
        
        /*
index 475a068eecf96faf20dda54dfffb7ef6da733596..76bef7642841cc5150969b912d1efd19032b8796 100644 (file)
@@ -106,8 +106,8 @@ public class FlightDataBranch implements Monitorable {
                mutable.check();
                
                ArrayList<Double> list = values.get(type);
+               
                if (list == null) {
-                       
                        list = new ArrayList<Double>();
                        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);
                
index 9a7df76420029953393c65401126f9c56b9a2d55..eb8a8d90da6e1c82b8ac28b9f441e29d717cc8aa 100644 (file)
@@ -37,7 +37,7 @@ public class FlightDataType implements Comparable<FlightDataType> {
        
        //// 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<FlightDataType> {
        
        // 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<FlightDataType> {
         * @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<FlightDataType> {
        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<FlightDataType> {
        }
        
        
-       
+       public void setPriority(int p){
+               this.priority = p;
+       }
        
        public String getName() {
                return name;
@@ -297,7 +308,7 @@ public class FlightDataType implements Comparable<FlightDataType> {
        
        @Override
        public String toString() {
-               return name;
+               return name; //+" ("+symbol+") "+units.getDefaultUnit().toString();
        }
        
        @Override