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++;
}
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);
if( null != _template){
ejectionDelay = _template.getEjectionDelay();
ignitionEvent = _template.getIgnitionEvent();
ignitionDelay = _template.getIgnitionDelay();
if( null != _source){
motor = _source.motor;
ejectionDelay = _source.ejectionDelay;
ignitionOveride = _source.ignitionOveride;
ignitionEvent = _source.getIgnitionEvent();
ignitionDelay = _source.getIgnitionDelay();
}
}

View File

@ -43,7 +43,7 @@ public class MotorConfigurationSet extends FlightConfigurableParameterSet<MotorC
public String toDebug(){
StringBuilder buffer = new StringBuilder();
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() ));
for( FlightConfigurationId loopFCID : this.map.keySet()){

View File

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

View File

@ -226,7 +226,12 @@ public class Rocket extends RocketComponent {
AxialStage value = stageMap.get(stageNumber);
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 {
stageNumber = getNewStageNumber();
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:
copy.stageMap = new HashMap<Integer, AxialStage>();
copy.configSet = new FlightConfigurableParameterSet<FlightConfiguration>( this.configSet );
new HashMap<FlightConfigurationId, FlightConfiguration>();
if( 0 < this.configSet.size() ){
Rocket.cloneConfigs( this, copy);
}
copy.selectedConfiguration = copy.configSet.get( this.getSelectedConfiguration().getId());
copy.listenerList = new ArrayList<EventListener>();
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() {
checkState();
return this.configSet.size();
@ -363,7 +356,7 @@ public class Rocket extends RocketComponent {
this.functionalModID = r.functionalModID;
this.refType = r.refType;
this.customReferenceLength = r.customReferenceLength;
Rocket.cloneConfigs( r, this);
this.configSet = new FlightConfigurableParameterSet<FlightConfiguration>( r.configSet );
this.perfectFinish = r.perfectFinish;
@ -467,9 +460,19 @@ public class Rocket extends RocketComponent {
@Override
public void update(){
updateStageMap();
updateConfigurations();
}
private void updateStageMap(){
for( RocketComponent component : getChildren() ){
if (component instanceof AxialStage) {
AxialStage stage = (AxialStage) component;
trackStage(stage);
}
}
}
private void updateConfigurations(){
this.selectedConfiguration.update();
for( FlightConfiguration config : configSet ){

View File

@ -17,23 +17,21 @@ public class RocketTest extends BaseTestCase {
@Test
public void testCopyIndependence() {
Rocket rkt1 = TestRockets.makeEstesAlphaIII();
FlightConfiguration config1 = rkt1.getSelectedConfiguration();
FlightConfigurationId fcid1 = config1.getId();
FlightConfiguration config1 = new FlightConfiguration(rkt1, null);
rkt1.setFlightConfiguration( config1.getId(), config1);
rkt1.setSelectedConfiguration( config1.getId());
FlightConfiguration config2 = new FlightConfiguration(rkt1, null);
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
Rocket rkt2 = rkt1.copyWithOriginalID();
// ^^^^ test target ^^^^
//System.err.println("cpy: "+ rkt1.toDebugConfigs());
FlightConfiguration config4 = rkt2.getSelectedConfiguration();
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());
FlightConfigurationId fcid5 = config5.getId();