}
class SingleSimulationHandler extends AbstractElementHandler {
- private static final LogHelper log = Application.getLogger();
private final DocumentLoadingContext context;
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,
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.");
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() {
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;
}
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();
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>() {{
* 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;
}
/*
* 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);
}
}
return false;
// No bad characters
- for (char c : "0123456789.()[]{}".toCharArray())
+ for (char c : "0123456789.,()[]{}<> ".toCharArray())
if (symbol.indexOf(c) != -1 )
return false;
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());
// Define the available variables as 0
for (FlightDataType type : getBranch().getTypes()){
+ System.out.println( " " + type.getSymbol() );
builder.withVariable(type.getSymbol(), 0.0);
}
* 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;
}
/*
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++) {
}
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);
//// 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
// An array of all the built in types
public static final FlightDataType[] ALL_TYPES = {
+ TYPE_TIME,
TYPE_ALTITUDE ,
TYPE_VELOCITY_Z ,
TYPE_ACCELERATION_Z,
* @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;
}
private final String name;
private final String symbol;
private final UnitGroup units;
- private final int priority;
+ private int priority;
private final int hashCode;
}
-
+ public void setPriority(int p){
+ this.priority = p;
+ }
public String getName() {
return name;
@Override
public String toString() {
- return name;
+ return name; //+" ("+symbol+") "+units.getDefaultUnit().toString();
}
@Override