Merge pull request #297 from teyrana/fix-issue-294

Fix issue #294
This commit is contained in:
kruland2607 2016-10-23 12:18:51 -05:00 committed by GitHub
commit f890402756
5 changed files with 57 additions and 57 deletions

View File

@ -46,13 +46,15 @@ public class MotorConfiguration implements FlightConfigurableParameter<MotorConf
modID++; modID++;
} }
public MotorConfiguration( final MotorMount _mount, final FlightConfigurationId _fcid, final MotorConfiguration _template ) { public MotorConfiguration( final MotorMount _mount, final FlightConfigurationId _fcid, final MotorConfiguration _source ) {
this( _mount, _fcid); this( _mount, _fcid);
if( null != _template){ if( null != _source){
ejectionDelay = _template.getEjectionDelay(); motor = _source.motor;
ignitionEvent = _template.getIgnitionEvent(); ejectionDelay = _source.ejectionDelay;
ignitionDelay = _template.getIgnitionDelay(); ignitionOveride = _source.ignitionOveride;
ignitionEvent = _source.getIgnitionEvent();
ignitionDelay = _source.getIgnitionDelay();
} }
} }

View File

@ -43,7 +43,7 @@ public class MotorConfigurationSet extends FlightConfigurableParameterSet<MotorC
public String toDebug(){ public String toDebug(){
StringBuilder buffer = new StringBuilder(); StringBuilder buffer = new StringBuilder();
final MotorMount mnt = this.getDefault().getMount(); final MotorMount mnt = this.getDefault().getMount();
buffer.append(String.format(" ====== Dumping MotorConfigurationSet: %d motors in %s ====== ", buffer.append(String.format(" ====== Dumping MotorConfigurationSet: %d motors in %s ======\n",
this.size(), mnt.getDebugName() )); this.size(), mnt.getDebugName() ));
for( FlightConfigurationId loopFCID : this.map.keySet()){ for( FlightConfigurationId loopFCID : this.map.keySet()){

View File

@ -3,7 +3,6 @@ package net.sf.openrocket.rocketcomponent;
import java.util.ArrayDeque; import java.util.ArrayDeque;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Queue; import java.util.Queue;
@ -42,18 +41,17 @@ public class FlightConfiguration implements FlightConfigurableParameter<FlightCo
private class StageFlags implements Cloneable { private class StageFlags implements Cloneable {
public boolean active = true; public boolean active = true;
public int prev = -1; //public int prev = -1;
public AxialStage stage = null; public int stageNumber = -1;
public StageFlags(AxialStage _stage, int _prev, boolean _active) { public StageFlags( int _num, boolean _active) {
this.stage = _stage; this.stageNumber = _num;
this.prev = _prev;
this.active = _active; this.active = _active;
} }
@Override @Override
public StageFlags clone(){ public StageFlags clone(){
return new StageFlags( this.stage, this.prev, true); return new StageFlags( this.stageNumber, true);
} }
} }
@ -191,7 +189,7 @@ public class FlightConfiguration implements FlightConfigurableParameter<FlightCo
for (StageFlags flags : this.stages.values()) { for (StageFlags flags : this.stages.values()) {
if (flags.active) { if (flags.active) {
activeStages.add(flags.stage); activeStages.add( rocket.getStage( flags.stageNumber) );
} }
} }
@ -215,7 +213,7 @@ public class FlightConfiguration implements FlightConfigurableParameter<FlightCo
AxialStage bottomStage = null; AxialStage bottomStage = null;
for (StageFlags curFlags : this.stages.values()) { for (StageFlags curFlags : this.stages.values()) {
if (curFlags.active) { if (curFlags.active) {
bottomStage = curFlags.stage; bottomStage = rocket.getStage( curFlags.stageNumber);
} }
} }
return bottomStage; return bottomStage;
@ -264,18 +262,14 @@ public class FlightConfiguration implements FlightConfigurableParameter<FlightCo
} }
private void updateStages() { private void updateStages() {
if (this.rocket.getStageCount() == this.stages.size()) { if (this.rocket.getStageCount() == this.stages.size()) {
// no changes needed return;
return; }
}
this.stages.clear(); this.stages.clear();
for (AxialStage curStage : this.rocket.getStageList()) { for (AxialStage curStage : this.rocket.getStageList()) {
int prevStageNum = curStage.getStageNumber() - 1;
if (curStage.getParent() instanceof AxialStage) { StageFlags flagsToAdd = new StageFlags( curStage.getStageNumber(), true);
prevStageNum = curStage.getParent().getStageNumber();
}
StageFlags flagsToAdd = new StageFlags(curStage, prevStageNum, true);
this.stages.put(curStage.getStageNumber(), flagsToAdd); this.stages.put(curStage.getStageNumber(), flagsToAdd);
} }
} }
@ -361,9 +355,7 @@ public class FlightConfiguration implements FlightConfigurableParameter<FlightCo
private void updateMotors() { private void updateMotors() {
this.motors.clear(); this.motors.clear();
Iterator<RocketComponent> iter = rocket.iterator(false); for ( RocketComponent comp : getActiveComponents() ){
while( iter.hasNext() ){
RocketComponent comp = iter.next();
if (( comp instanceof MotorMount )&&( ((MotorMount)comp).isMotorMount())){ if (( comp instanceof MotorMount )&&( ((MotorMount)comp).isMotorMount())){
MotorMount mount = (MotorMount)comp; MotorMount mount = (MotorMount)comp;
MotorConfiguration motorConfig = mount.getMotorConfig( fcid); MotorConfiguration motorConfig = mount.getMotorConfig( fcid);
@ -443,15 +435,20 @@ public class FlightConfiguration implements FlightConfigurableParameter<FlightCo
} }
/** /**
* Perform a deep-clone. The object references are also cloned and no * Perform a shallow-clone; copies configuration fields only.
* listeners are listening on the cloned object. The rocket instance remains the same. *
* Preserved:
* - components
* - motors
* - configurables
*
*/ */
@Override @Override
public FlightConfiguration clone() { public FlightConfiguration clone() {
// Note the stages are updated in the constructor call. // Note the stages are updated in the constructor call.
FlightConfiguration clone = new FlightConfiguration( this.rocket, this.fcid ); FlightConfiguration clone = new FlightConfiguration( this.rocket, this.fcid );
clone.setName("clone[#"+clone.instanceNumber+"]"+clone.fcid.toShortKey()); clone.setName(configurationName);
clone.cachedBounds = this.cachedBounds.clone(); clone.cachedBounds = this.cachedBounds.clone();
clone.modID = this.modID; clone.modID = this.modID;
@ -532,8 +529,8 @@ public class FlightConfiguration implements FlightConfigurableParameter<FlightCo
final String fmt = " [%-2s][%4s]: %6s \n"; final String fmt = " [%-2s][%4s]: %6s \n";
buf.append(String.format(fmt, "#", "?actv", "Name")); buf.append(String.format(fmt, "#", "?actv", "Name"));
for (StageFlags flags : stages.values()) { for (StageFlags flags : stages.values()) {
AxialStage curStage = flags.stage; final int stageNumber = flags.stageNumber;
buf.append(String.format(fmt, curStage.getStageNumber(), (flags.active?" on": "off"), curStage.getName())); buf.append(String.format(fmt, stageNumber, (flags.active?" on": "off"), rocket.getStage( stageNumber).getName()));
} }
buf.append("\n"); buf.append("\n");
return buf.toString(); return buf.toString();

View File

@ -226,7 +226,12 @@ public class Rocket extends RocketComponent {
AxialStage value = stageMap.get(stageNumber); AxialStage value = stageMap.get(stageNumber);
if (newStage.equals(value)) { if (newStage.equals(value)) {
// stage is already added. skip. // stage is already added
if( newStage != value ){
// but the value is the wrong instance
stageMap.put(stageNumber, newStage);
}
return;
} else { } else {
stageNumber = getNewStageNumber(); stageNumber = getNewStageNumber();
newStage.setStageNumber(stageNumber); newStage.setStageNumber(stageNumber);
@ -310,24 +315,12 @@ public class Rocket extends RocketComponent {
// Rocket copy is cloned, so non-trivial members must be cloned as well: // Rocket copy is cloned, so non-trivial members must be cloned as well:
copy.stageMap = new HashMap<Integer, AxialStage>(); copy.stageMap = new HashMap<Integer, AxialStage>();
copy.configSet = new FlightConfigurableParameterSet<FlightConfiguration>( this.configSet ); copy.configSet = new FlightConfigurableParameterSet<FlightConfiguration>( this.configSet );
new HashMap<FlightConfigurationId, FlightConfiguration>(); copy.selectedConfiguration = copy.configSet.get( this.getSelectedConfiguration().getId());
if( 0 < this.configSet.size() ){
Rocket.cloneConfigs( this, copy);
}
copy.listenerList = new ArrayList<EventListener>(); copy.listenerList = new ArrayList<EventListener>();
return copy; return copy;
} }
private static void cloneConfigs( final Rocket source, Rocket dest ){
source.checkState();
dest.checkState();
dest.selectedConfiguration = source.selectedConfiguration.clone();
for( final FlightConfiguration config : source.configSet ){
dest.configSet.set( config.getId(), config.clone() );
}
}
public int getFlightConfigurationCount() { public int getFlightConfigurationCount() {
checkState(); checkState();
return this.configSet.size(); return this.configSet.size();
@ -363,7 +356,7 @@ public class Rocket extends RocketComponent {
this.functionalModID = r.functionalModID; this.functionalModID = r.functionalModID;
this.refType = r.refType; this.refType = r.refType;
this.customReferenceLength = r.customReferenceLength; this.customReferenceLength = r.customReferenceLength;
Rocket.cloneConfigs( r, this); this.configSet = new FlightConfigurableParameterSet<FlightConfiguration>( r.configSet );
this.perfectFinish = r.perfectFinish; this.perfectFinish = r.perfectFinish;
@ -467,9 +460,19 @@ public class Rocket extends RocketComponent {
@Override @Override
public void update(){ public void update(){
updateStageMap();
updateConfigurations(); updateConfigurations();
} }
private void updateStageMap(){
for( RocketComponent component : getChildren() ){
if (component instanceof AxialStage) {
AxialStage stage = (AxialStage) component;
trackStage(stage);
}
}
}
private void updateConfigurations(){ private void updateConfigurations(){
this.selectedConfiguration.update(); this.selectedConfiguration.update();
for( FlightConfiguration config : configSet ){ for( FlightConfiguration config : configSet ){

View File

@ -17,23 +17,21 @@ public class RocketTest extends BaseTestCase {
@Test @Test
public void testCopyIndependence() { public void testCopyIndependence() {
Rocket rkt1 = TestRockets.makeEstesAlphaIII(); Rocket rkt1 = TestRockets.makeEstesAlphaIII();
FlightConfiguration config1 = rkt1.getSelectedConfiguration(); FlightConfiguration config1 = new FlightConfiguration(rkt1, null);
FlightConfigurationId fcid1 = config1.getId(); rkt1.setFlightConfiguration( config1.getId(), config1);
rkt1.setSelectedConfiguration( config1.getId());
FlightConfiguration config2 = new FlightConfiguration(rkt1, null); FlightConfiguration config2 = new FlightConfiguration(rkt1, null);
rkt1.setFlightConfiguration( config2.getId(), config2); rkt1.setFlightConfiguration( config2.getId(), config2);
FlightConfiguration config3 = new FlightConfiguration(rkt1, null);
rkt1.setFlightConfiguration( config3.getId(), config3);
//System.err.println("src: "+ rkt1.toDebugConfigs());
// vvvv test target vvvv // vvvv test target vvvv
Rocket rkt2 = rkt1.copyWithOriginalID(); Rocket rkt2 = rkt1.copyWithOriginalID();
// ^^^^ test target ^^^^ // ^^^^ test target ^^^^
//System.err.println("cpy: "+ rkt1.toDebugConfigs());
FlightConfiguration config4 = rkt2.getSelectedConfiguration(); FlightConfiguration config4 = rkt2.getSelectedConfiguration();
FlightConfigurationId fcid4 = config4.getId(); FlightConfigurationId fcid4 = config4.getId();
assertThat("fcids should match: ", fcid1.key, equalTo(fcid4.key));
assertThat("Configurations should be different match: "+config1.toDebug()+"=?="+config4.toDebug(), config1.instanceNumber, not( config4.instanceNumber)); assertThat("fcids should match: ", config1.getId().key, equalTo(fcid4.key));
assertThat("Configurations should be different: "+config1.toDebug()+"=?="+config4.toDebug(), config1.instanceNumber, not( config4.instanceNumber));
FlightConfiguration config5 = rkt2.getFlightConfiguration(config2.getId()); FlightConfiguration config5 = rkt2.getFlightConfiguration(config2.getId());
FlightConfigurationId fcid5 = config5.getId(); FlightConfigurationId fcid5 = config5.getId();