import java.util.List;
import net.sf.openrocket.aerodynamics.WarningSet;
-import net.sf.openrocket.file.MotorLoader;
import net.sf.openrocket.file.simplesax.ElementHandler;
import net.sf.openrocket.file.simplesax.NullElementHandler;
import net.sf.openrocket.file.simplesax.PlainTextHandler;
import net.sf.openrocket.file.simplesax.SimpleSAX;
+import net.sf.openrocket.logging.LogHelper;
import net.sf.openrocket.motor.Manufacturer;
import net.sf.openrocket.motor.Motor;
import net.sf.openrocket.motor.MotorDigest;
-import net.sf.openrocket.motor.ThrustCurveMotor;
import net.sf.openrocket.motor.MotorDigest.DataType;
+import net.sf.openrocket.motor.ThrustCurveMotor;
+import net.sf.openrocket.startup.Application;
import net.sf.openrocket.util.Coordinate;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
-public class RockSimMotorLoader extends MotorLoader {
+public class RockSimMotorLoader extends AbstractMotorLoader {
+
+ private static final LogHelper log = Application.getLogger();
public static final String CHARSET_NAME = "UTF-8";
public static final Charset CHARSET = Charset.forName(CHARSET_NAME);
-
- /** Any delay longed than this will be interpreted as a plugged motor. */
+
+ /** Any delay longer than this will be interpreted as a plugged motor. */
private static final int DELAY_LIMIT = 90;
-
+
@Override
protected Charset getDefaultCharset() {
return CHARSET;
}
-
+
/**
}
-
+
/**
* Initial handler for the RockSim engine files.
*/
@Override
public ElementHandler openElement(String element,
HashMap<String, String> attributes, WarningSet warnings) throws SAXException {
-
+
if (element.equals("engine-database") ||
element.equals("engine-list")) {
// Ignore <engine-database> and <engine-list> elements
return null;
}
-
+
@Override
public void closeElement(String element, HashMap<String, String> attributes,
String content, WarningSet warnings) throws SAXException {
-
+
if (element.equals("engine")) {
Motor motor = motorHandler.getMotor();
motors.add(motor);
* Handler for a RockSim engine file <motor> element.
*/
private static class RSEMotorHandler extends ElementHandler {
-
+
private final String manufacturer;
private final String designation;
private final double[] delays;
private List<Double> cg;
private RSEMotorDataHandler dataHandler = null;
-
+
public RSEMotorHandler(HashMap<String, String> attributes) throws SAXException {
String str;
str = attributes.get("delays");
if (str != null) {
String[] split = str.split(",");
- for (String delay: split) {
+ for (String delay : split) {
try {
double d = Double.parseDouble(delay);
}
}
delays = new double[delayList.size()];
- for (int i=0; i<delayList.size(); i++) {
+ for (int i = 0; i < delayList.size(); i++) {
delays[i] = delayList.get(i);
}
@Override
public ElementHandler openElement(String element,
HashMap<String, String> attributes, WarningSet warnings) throws SAXException {
-
+
if (element.equals("comments")) {
return PlainTextHandler.INSTANCE;
}
dataHandler = new RSEMotorDataHandler();
return dataHandler;
}
-
+
warnings.add("Unknown element '" + element + "' encountered, ignoring.");
return null;
}
-
+
@Override
public void closeElement(String element, HashMap<String, String> attributes,
String content, WarningSet warnings) {
-
+
if (element.equals("comments")) {
if (description.length() > 0) {
description = description + "\n\n" + content.trim();
sortLists(time, force, mass, cg);
- for (double d: mass) {
+ for (double d : mass) {
if (Double.isNaN(d)) {
calculateMass = true;
break;
}
}
- for (double d: cg) {
+ for (double d : cg) {
if (Double.isNaN(d)) {
calculateCG = true;
break;
public Motor getMotor() throws SAXException {
if (time == null || time.size() == 0)
throw new SAXException("Illegal motor data");
-
+
finalizeThrustCurve(time, force, mass, cg);
final int n = time.size();
mass = calculateMass(time, force, initMass, propMass);
}
if (calculateCG) {
- for (int i=0; i < n; i++) {
- cg.set(i, length/2);
+ for (int i = 0; i < n; i++) {
+ cg.set(i, length / 2);
}
}
double[] timeArray = toArray(time);
double[] thrustArray = toArray(force);
Coordinate[] cgArray = new Coordinate[n];
- for (int i=0; i < n; i++) {
- cgArray[i] = new Coordinate(cg.get(i),0,0,mass.get(i));
+ for (int i = 0; i < n; i++) {
+ cgArray[i] = new Coordinate(cg.get(i), 0, 0, mass.get(i));
}
if (!calculateMass) {
motorDigest.update(DataType.MASS_PER_TIME, toArray(mass));
} else {
- motorDigest.update(DataType.MASS_SPECIFIC, initMass, initMass-propMass);
+ motorDigest.update(DataType.MASS_SPECIFIC, initMass, initMass - propMass);
}
if (!calculateCG) {
motorDigest.update(DataType.CG_PER_TIME, toArray(cg));
}
motorDigest.update(DataType.FORCE_PER_TIME, thrustArray);
- final String digest = motorDigest.getDigest();
-
+ // TODO: HIGH: Motor digest?
+ // final String digest = motorDigest.getDigest();
+
try {
- return new ThrustCurveMotor(Manufacturer.getManufacturer(manufacturer),
- designation, description, type,
- delays, diameter, length, timeArray, thrustArray, cgArray, digest);
+ Manufacturer m = Manufacturer.getManufacturer(manufacturer);
+ Motor.Type t = type;
+ if (t == Motor.Type.UNKNOWN) {
+ t = m.getMotorType();
+ } else {
+ if (m.getMotorType() != Motor.Type.UNKNOWN && m.getMotorType() != t) {
+ log.warn("Loaded motor type inconsistent with manufacturer," +
+ " loaded type=" + t + " manufacturer=" + m +
+ " manufacturer type=" + m.getMotorType() +
+ " designation=" + designation);
+ }
+ }
+
+ return new ThrustCurveMotor(m, designation, description, t,
+ delays, diameter, length, timeArray, thrustArray, cgArray);
} catch (IllegalArgumentException e) {
throw new SAXException("Illegal motor data", e);
}
public List<Double> getTime() {
return time;
}
+
public List<Double> getForce() {
return force;
}
+
public List<Double> getMass() {
return mass;
}
+
public List<Double> getCG() {
return cg;
}
@Override
public ElementHandler openElement(String element,
HashMap<String, String> attributes, WarningSet warnings) {
-
+
if (element.equals("eng-data")) {
return NullElementHandler.INSTANCE;
}
warnings.add("Unknown element '" + element + "' encountered, ignoring.");
return null;
}
-
+
@Override
public void closeElement(String element, HashMap<String, String> attributes,
String content, WarningSet warnings) throws SAXException {
-
+
double t = parseDouble(attributes.get("t"));
double f = parseDouble(attributes.get("f"));
double m = parseDouble(attributes.get("m")) / 1000.0;
}
-
+
private static boolean hasIllegalValue(List<Double> list) {
- for (Double d: list) {
+ for (Double d : list) {
if (d == null || d.isNaN() || d.isInfinite()) {
return true;
}
private static double[] toArray(List<Double> list) {
final int n = list.size();
double[] array = new double[n];
- for (int i=0; i < n; i++) {
+ for (int i = 0; i < n; i++) {
array[i] = list.get(i);
}
return array;