Merge pull request #1766 from SiboVG/issue-1687

[#1687] Save stage activeness to design file
This commit is contained in:
Sibo Van Gool 2022-10-28 21:08:44 +02:00 committed by GitHub
commit 4deb6da68d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 167 additions and 21 deletions

View File

@ -15,6 +15,7 @@ public class OpenRocketDocumentFactory {
//// Sustainer
stage.setName(trans.get("BasicFrame.StageName.Sustainer"));
rocket.addChild(stage);
rocket.getSelectedConfiguration().setAllStages();
OpenRocketDocument doc = new OpenRocketDocument(rocket);
doc.setSaved(true);
return doc;

View File

@ -20,6 +20,7 @@ class MotorConfigurationHandler extends AbstractElementHandler {
private final Rocket rocket;
private String name = null;
private boolean inNameElement = false;
private HashMap<Integer, Boolean> stageActiveness = new HashMap<>();
public MotorConfigurationHandler(Rocket rocket, DocumentLoadingContext context) {
this.rocket = rocket;
@ -30,11 +31,13 @@ class MotorConfigurationHandler extends AbstractElementHandler {
public ElementHandler openElement(String element, HashMap<String, String> attributes,
WarningSet warnings) {
if (inNameElement || !element.equals("name")) {
if ((inNameElement && element.equals("name")) || !(element.equals("name") || element.equals("stage"))) {
warnings.add(Warning.FILE_INVALID_PARAMETER);
return null;
}
inNameElement = true;
if (element.equals("name")) {
inNameElement = true;
}
return PlainTextHandler.INSTANCE;
}
@ -42,7 +45,13 @@ class MotorConfigurationHandler extends AbstractElementHandler {
@Override
public void closeElement(String element, HashMap<String, String> attributes,
String content, WarningSet warnings) {
name = content;
if (element.equals("name")) {
name = content;
} else if (element.equals("stage")) {
int stageNr = Integer.parseInt(attributes.get("number"));
boolean isActive = Boolean.parseBoolean(attributes.get("active"));
stageActiveness.put(stageNr, isActive);
}
}
@Override
@ -60,7 +69,11 @@ class MotorConfigurationHandler extends AbstractElementHandler {
if (name != null && name.trim().length() > 0) {
rocket.getFlightConfiguration(fcid).setName(name);
}
for (int stageNr : stageActiveness.keySet()) {
rocket.getFlightConfiguration(fcid).preloadStageActiveness(stageNr, stageActiveness.get(stageNr));
}
if ("true".equals(attributes.remove("default"))) {
rocket.setSelectedConfiguration( fcid);
}

View File

@ -4,6 +4,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import net.sf.openrocket.rocketcomponent.FlightConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.InputSource;
@ -53,7 +54,10 @@ public class OpenRocketLoader extends AbstractRocketLoader {
throw new RocketLoadException("Malformed XML in input.", e);
}
doc.getSelectedConfiguration().setAllStages();
// load the stage activeness
for (FlightConfiguration config : doc.getRocket().getFlightConfigurations()) {
config.applyPreloadedStageActiveness();
}
// Deduce suitable time skip
double timeSkip = StorageOptions.SIMULATION_DATA_NONE;

View File

@ -4,6 +4,8 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import net.sf.openrocket.file.openrocket.OpenRocketSaver;
import net.sf.openrocket.rocketcomponent.AxialStage;
import net.sf.openrocket.rocketcomponent.FlightConfiguration;
import net.sf.openrocket.rocketcomponent.FlightConfigurationId;
import net.sf.openrocket.rocketcomponent.ReferenceType;
@ -53,15 +55,23 @@ public class RocketSaver extends RocketComponentSaver {
if ( rocket.getSelectedConfiguration().equals( flightConfig )){
str += " default=\"true\"";
}
// close motorconfiguration opening tag
str += ">";
elements.add(str);
// flight configuration name
if (flightConfig.isNameOverridden()){
str += "><name>" + net.sf.openrocket.util.TextUtil.escapeXML(flightConfig.getNameRaw())
+ "</name></motorconfiguration>";
} else {
str += "/>";
elements.add(OpenRocketSaver.INDENT + "<name>" + net.sf.openrocket.util.TextUtil.escapeXML(flightConfig.getNameRaw())
+ "</name>");
}
// stage activeness
for (AxialStage stage : rocket.getStageList()) {
elements.add(OpenRocketSaver.INDENT + "<stage number=\"" + stage.getStageNumber() + "\"" +
" active=\"" + flightConfig.isStageActive(stage.getStageNumber()) + "\"/>");
}
elements.add(str);
elements.add("</motorconfiguration>");
}
// Reference diameter

View File

@ -64,6 +64,7 @@ public class FlightConfiguration implements FlightConfigurableParameter<FlightCo
/* Cached data */
final protected Map<Integer, StageFlags> stages = new HashMap<>(); // Map of stage number to StageFlags of the corresponding stage
final protected Map<MotorConfigurationId, MotorConfiguration> motors = new HashMap<MotorConfigurationId, MotorConfiguration>();
private Map<Integer, Boolean> preloadStageActiveness = null;
final private Collection<MotorConfiguration> activeMotors = new ConcurrentLinkedQueue<MotorConfiguration>();
final private InstanceMap activeInstances = new InstanceMap();
@ -276,6 +277,34 @@ public class FlightConfiguration implements FlightConfigurableParameter<FlightCo
stages.get(stageNumber) != null && stages.get(stageNumber).active;
}
/**
* Preload the stage activeness of a certain stage.
* This method is to be used during the import and readout of an OpenRocket design file.
* @param stageNumber stage number to preload the stage activeness for
* @param isActive whether the stage should be active or not
*/
public void preloadStageActiveness(int stageNumber, boolean isActive) {
if (this.preloadStageActiveness == null) {
preloadStageActiveness = new HashMap<>();
}
this.preloadStageActiveness.put(stageNumber, isActive);
}
/**
* Applies preloaded stage activeness.
* This method should be called after the rocket has been loaded from a file.
*/
public void applyPreloadedStageActiveness() {
if (preloadStageActiveness == null) {
return;
}
for (int stageNumber : preloadStageActiveness.keySet()) {
_setStageActive(stageNumber, preloadStageActiveness.get(stageNumber), false);
}
preloadStageActiveness.clear();
preloadStageActiveness = null;
}
public Collection<RocketComponent> getAllComponents() {
List<RocketComponent> traversalOrder = new ArrayList<RocketComponent>();
recurseAllComponentsDepthFirst(this.rocket,traversalOrder);
@ -795,6 +824,7 @@ public class FlightConfiguration implements FlightConfigurableParameter<FlightCo
// Note the stages are updated in the constructor call.
FlightConfiguration clone = new FlightConfiguration( this.rocket, this.fcid );
clone.setName(configurationName);
clone.preloadStageActiveness = this.preloadStageActiveness == null ? null : new HashMap<>(this.preloadStageActiveness);
clone.cachedBoundsAerodynamic = this.cachedBoundsAerodynamic.clone();
clone.cachedBounds = this.cachedBounds.clone();
@ -824,6 +854,7 @@ public class FlightConfiguration implements FlightConfigurableParameter<FlightCo
cloneMotor.getMount().setMotorConfig(cloneMotor, copyId);
}
copy.preloadStageActiveness = this.preloadStageActiveness == null ? null : new HashMap<>(this.preloadStageActiveness);
copy.cachedBoundsAerodynamic = this.cachedBoundsAerodynamic.clone();
copy.cachedBounds = this.cachedBounds.clone();
copy.modID = this.modID;

View File

@ -752,23 +752,23 @@ public class Rocket extends ComponentAssembly {
* Return a flight configuration. If the supplied id does not have a specific instance, the default is returned.
*
* @param fcid the flight configuration id
* @return FlightConfiguration instance
* @return FlightConfiguration instance
*/
public FlightConfigurationId createFlightConfiguration( final FlightConfigurationId fcid) {
public FlightConfiguration createFlightConfiguration( final FlightConfigurationId fcid) {
checkState();
if( null == fcid ){
// fall-through to the default case:
// ...creating a FlightConfiguration( null ) just allocates a fresh new FCID
}else if( fcid.hasError() ){
return configSet.getDefault().getFlightConfigurationID();
return configSet.getDefault();
}else if( configSet.containsId(fcid)){
return fcid;
return configSet.get(fcid);
}
FlightConfiguration nextConfig = new FlightConfiguration(this, fcid);
this.configSet.set(nextConfig.getId(), nextConfig);
fireComponentChangeEvent(ComponentChangeEvent.TREE_CHANGE);
return nextConfig.getFlightConfigurationID();
return nextConfig;
}
/**

View File

@ -941,7 +941,7 @@ public class TestRockets {
Rocket rocket = new Rocket();
rocket.setName("Falcon9H Scale Rocket");
FlightConfigurationId selFCID = rocket.createFlightConfiguration( new FlightConfigurationId( FALCON_9H_FCID_1 ));
FlightConfigurationId selFCID = rocket.createFlightConfiguration( new FlightConfigurationId( FALCON_9H_FCID_1 )).getFlightConfigurationID();
rocket.setSelectedConfiguration(selFCID);
// ====== Payload Stage ======
@ -1625,6 +1625,14 @@ public class TestRockets {
return document;
}
public static OpenRocketDocument makeTestRocket_v108_withDisabledStage() {
Rocket rocket = makeFalcon9Heavy();
rocket.getSelectedConfiguration()._setStageActive(0, false, false);
OpenRocketDocument document = OpenRocketDocumentFactory.createDocumentFromRocket(rocket);
return document;
}
/*
* Create a new test rocket for testing OpenRocketSaver.estimateFileSize()

View File

@ -2,6 +2,8 @@ package net.sf.openrocket.file.openrocket;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.fail;
import java.io.File;
@ -19,6 +21,7 @@ import net.sf.openrocket.database.ComponentPresetDatabase;
import net.sf.openrocket.database.motor.MotorDatabase;
import net.sf.openrocket.database.motor.ThrustCurveMotorSetDatabase;
import net.sf.openrocket.document.OpenRocketDocument;
import net.sf.openrocket.document.OpenRocketDocumentFactory;
import net.sf.openrocket.document.StorageOptions;
import net.sf.openrocket.file.GeneralRocketLoader;
import net.sf.openrocket.file.RocketLoadException;
@ -29,6 +32,10 @@ import net.sf.openrocket.motor.Manufacturer;
import net.sf.openrocket.motor.Motor;
import net.sf.openrocket.motor.ThrustCurveMotor;
import net.sf.openrocket.plugin.PluginModule;
import net.sf.openrocket.rocketcomponent.BodyTube;
import net.sf.openrocket.rocketcomponent.FlightConfiguration;
import net.sf.openrocket.rocketcomponent.FlightConfigurationId;
import net.sf.openrocket.rocketcomponent.Rocket;
import net.sf.openrocket.simulation.extension.impl.ScriptingExtension;
import net.sf.openrocket.simulation.extension.impl.ScriptingUtil;
import net.sf.openrocket.startup.Application;
@ -126,6 +133,7 @@ public class OpenRocketSaverTest {
rocketDocs.add(TestRockets.makeTestRocket_v106_withStageSeparationConfig());
rocketDocs.add(TestRockets.makeTestRocket_v107_withSimulationExtension(SIMULATION_EXTENSION_SCRIPT));
rocketDocs.add(TestRockets.makeTestRocket_v108_withBoosters());
rocketDocs.add(TestRockets.makeTestRocket_v108_withDisabledStage());
rocketDocs.add(TestRockets.makeTestRocket_for_estimateFileSize());
StorageOptions options = new StorageOptions();
@ -138,6 +146,77 @@ public class OpenRocketSaverTest {
assertNotNull(rocketDocLoaded);
}
}
@Test
public void testSaveStageActiveness() {
OpenRocketDocument rocketDoc = TestRockets.makeTestRocket_v108_withDisabledStage();
StorageOptions options = new StorageOptions();
options.setSimulationTimeSkip(0.05);
// Save rockets, load, validate
File file = saveRocket(rocketDoc, options);
OpenRocketDocument rocketDocLoaded = loadRocket(file.getPath());
// Check that the stages activeness is saved
FlightConfiguration config = rocketDocLoaded.getRocket().getSelectedConfiguration();
assertFalse(" selected config, stage 0 should have been disabled after saving", config.isStageActive(0));
assertTrue(" selected config, stage 1 should have been enabled after saving", config.isStageActive(1));
assertTrue(" selected config, stage 2 should have been enabled after saving", config.isStageActive(2));
// Disable second stage
config._setStageActive(1, false, false);
file = saveRocket(rocketDocLoaded, options);
rocketDocLoaded = loadRocket(file.getPath());
config = rocketDocLoaded.getRocket().getSelectedConfiguration();
assertFalse(" selected config, stage 0 should have been disabled after saving", config.isStageActive(0));
assertFalse(" selected config, stage 1 should have been disabled after saving", config.isStageActive(1));
assertTrue(" selected config, stage 2 should have been enabled after saving", config.isStageActive(2));
// Re-enable first stage
config._setStageActive(0, true, false);
file = saveRocket(rocketDocLoaded, options);
rocketDocLoaded = loadRocket(file.getPath());
config = rocketDocLoaded.getRocket().getSelectedConfiguration();
assertTrue(" selected config, stage 0 should have been enabled after saving", config.isStageActive(0));
assertFalse(" selected config, stage 1 should have been disabled after saving", config.isStageActive(1));
assertTrue(" selected config, stage 2 should have been enabled after saving", config.isStageActive(2));
// Check that other configurations are not affected
FlightConfiguration extraConfig = rocketDocLoaded.getRocket().createFlightConfiguration(TestRockets.TEST_FCID_0);
extraConfig.setAllStages();
file = saveRocket(rocketDocLoaded, options);
rocketDocLoaded = loadRocket(file.getPath());
config = rocketDocLoaded.getRocket().getSelectedConfiguration();
extraConfig = rocketDocLoaded.getRocket().getFlightConfiguration(TestRockets.TEST_FCID_0);
assertTrue(" selected config, stage 0 should have been enabled after saving", config.isStageActive(0));
assertFalse(" selected config, stage 1 should have been disabled after saving", config.isStageActive(1));
assertTrue(" selected config, stage 2 should have been enabled after saving", config.isStageActive(2));
assertTrue(" extra config, stage 0 should have been enabled after saving", extraConfig.isStageActive(0));
assertTrue(" extra config, stage 1 should have been enabled after saving", extraConfig.isStageActive(1));
assertTrue(" extra config, stage 2 should have been enabled after saving", extraConfig.isStageActive(2));
// Disable a stage in the extra config, and an extra one in the selected config
extraConfig._setStageActive(0, false, false);
config._setStageActive(2, false, false);
file = saveRocket(rocketDocLoaded, options);
rocketDocLoaded = loadRocket(file.getPath());
config = rocketDocLoaded.getRocket().getSelectedConfiguration();
extraConfig = rocketDocLoaded.getRocket().getFlightConfiguration(TestRockets.TEST_FCID_0);
assertTrue(" selected config, stage 0 should have been enabled after saving", config.isStageActive(0));
assertFalse(" selected config, stage 1 should have been disabled after saving", config.isStageActive(1));
assertFalse(" selected config, stage 2 should have been disabled after saving", config.isStageActive(2));
assertFalse(" extra config, stage 0 should have been disabled after saving", extraConfig.isStageActive(0));
assertTrue(" extra config, stage 1 should have been enabled after saving", extraConfig.isStageActive(1));
assertTrue(" extra config, stage 2 should have been enabled after saving", extraConfig.isStageActive(2));
// Test an empty rocket with no configurations
OpenRocketDocument document = OpenRocketDocumentFactory.createNewRocket();
file = saveRocket(document, options);
rocketDocLoaded = loadRocket(file.getPath());
rocketDocLoaded.getRocket().getStage(0).addChild(new BodyTube()); // Add a child, otherwise the stage is always marked inactive
config = rocketDocLoaded.getRocket().getSelectedConfiguration();
assertTrue(" empty rocket, selected config, stage 0 should have been enabled after saving", config.isStageActive(0));
}
@Test
public void testUntrustedScriptDisabledOnLoad() {

View File

@ -16,7 +16,7 @@ public class AxialStageTest extends BaseTestCase {
public void testDisableStage() {
final Rocket rocket = TestRockets.makeFalcon9Heavy();
final FlightConfiguration config = rocket.getSelectedConfiguration();
final FlightConfigurationId fcid = rocket.createFlightConfiguration(new FlightConfigurationId());
final FlightConfigurationId fcid = rocket.createFlightConfiguration(new FlightConfigurationId()).getFlightConfigurationID();
final FlightConfiguration config2 = rocket.getFlightConfiguration(fcid);
// Disable the payload stage
@ -106,7 +106,7 @@ public class AxialStageTest extends BaseTestCase {
public void testDisableStageAndMove() {
final Rocket rocket = TestRockets.makeFalcon9Heavy();
final FlightConfiguration config = rocket.getSelectedConfiguration();
final FlightConfigurationId fcid = rocket.createFlightConfiguration(new FlightConfigurationId());
final FlightConfigurationId fcid = rocket.createFlightConfiguration(new FlightConfigurationId()).getFlightConfigurationID();
final FlightConfiguration config2 = rocket.getFlightConfiguration(fcid);
// Disable the payload stage
@ -173,7 +173,7 @@ public class AxialStageTest extends BaseTestCase {
public void testDisableStageAndCopy() {
final Rocket rocket = TestRockets.makeFalcon9Heavy();
final FlightConfiguration config = rocket.getSelectedConfiguration();
final FlightConfigurationId fcid = rocket.createFlightConfiguration(new FlightConfigurationId());
final FlightConfigurationId fcid = rocket.createFlightConfiguration(new FlightConfigurationId()).getFlightConfigurationID();
final FlightConfiguration config2 = rocket.getFlightConfiguration(fcid);
// Disable the core stage

View File

@ -57,6 +57,7 @@ The following file format versions exist:
Transitions, Inner Tubes, Launch Lugs, and Fins.
Added PhotoStudio settings saving (<photostudio>)
Added override CD parameter (<overridecd>)
Added stage activeness remembrance (<stage> under <motorconfiguration>)
Separated <overridesubcomponents> into individual parameters for mass, CG, and CD.
Rename <fincount> to <instancecount> (<fincount> remains for backward compatibility)
Rename <position> to <axialoffset> (<position> remains for backward compatibility)

View File

@ -154,7 +154,6 @@ public class BasicFrame extends JFrame {
this.document = document;
this.rocket = document.getRocket();
this.rocket.getSelectedConfiguration().setAllStages();
BasicFrame.lastFrameInstance = this;
// Create the component tree selection model that will be used