Clarify Java code provider menu entry

This commit is contained in:
JoePfeiffer 2022-12-07 15:36:42 -07:00
parent 9027b21a12
commit dbcc0ffb5e
5 changed files with 161 additions and 66 deletions

View File

@ -0,0 +1,95 @@
package net.sf.openrocket.simulation.extension.impl;
import net.sf.openrocket.simulation.FlightEvent;
import net.sf.openrocket.simulation.SimulationConditions;
import net.sf.openrocket.simulation.SimulationStatus;
import net.sf.openrocket.simulation.exception.SimulationException;
import net.sf.openrocket.simulation.extension.AbstractSimulationExtension;
import net.sf.openrocket.simulation.listeners.AbstractSimulationListener;
/**
* A simulation listener that stops the simulation after a specified number of steps or
* after a specified amount of simulation time.
*
* @author Sampo Niskanen <sampo.niskanen@iki.fi>
*/
public class StopSimulation extends AbstractSimulationExtension {
@Override
public void initialize(SimulationConditions conditions) throws SimulationException {
conditions.getSimulationListenerList().add(new StopSimulationListener());
}
@Override
public String getName() {
return "Stop Simulation";
}
@Override
public String getDescription() {
return "Stop simulafter at a configurable simulation time or step count";
}
public int getReportRate() {
return config.getInt("reportRate", 500);
}
public void setReportRate(int reportRate) {
config.put("reportRate", reportRate);
}
public int getStopStep() {
return config.getInt("stopStep", 5000);
}
public void setStopStep(int stopStep) {
config.put("stopStep", stopStep);
}
public int getStopTime() {
return config.getInt("stopTime", 10);
}
public void setStopTime(int stopTime) {
config.put("stopTime", stopTime);
}
private class StopSimulationListener extends AbstractSimulationListener {
private int step = 0;
private long startTime = -1;
private long time = -1;
@Override
public boolean handleFlightEvent(SimulationStatus status, FlightEvent event) {
if (event.getType() == FlightEvent.Type.LAUNCH) {
System.out.println("Simulation starting.");
time = System.nanoTime();
startTime = System.nanoTime();
}
return true;
}
@Override
public void postStep(SimulationStatus status) throws SimulationException {
step++;
if ((step % getReportRate()) == 0) {
long t = System.nanoTime();
System.out.printf("Step %4d, time=%.3f, took %d us/step (avg. %d us/step)\n",
step, status.getSimulationTime(), (t - time) / 1000 / getReportRate(), (t - startTime) / 1000 / step);
time = t;
}
if (status.getSimulationTime() >= getStopTime() || step >= getStopStep()) {
System.out.printf("Stopping simulation, step=%d time=%.3f\n", step, status.getSimulationTime());
status.getEventQueue().add(new FlightEvent(FlightEvent.Type.SIMULATION_END,
status.getSimulationTime(), null));
}
}
}
}

View File

@ -0,0 +1,13 @@
package net.sf.openrocket.simulation.extension.impl;
import net.sf.openrocket.plugin.Plugin;
import net.sf.openrocket.simulation.extension.AbstractSimulationExtensionProvider;
@Plugin
public class StopSimulationProvider extends AbstractSimulationExtensionProvider {
public StopSimulationProvider() {
super(StopSimulation.class, "Simulation Conditions", "Stop Simulation");
}
}

View File

@ -7,7 +7,7 @@ import net.sf.openrocket.simulation.extension.AbstractSimulationExtensionProvide
public class JavaCodeProvider extends AbstractSimulationExtensionProvider {
public JavaCodeProvider() {
super(JavaCode.class, "User code", "Java code");
super(JavaCode.class, "Scripts", "Java listeners");
}
}

View File

@ -1,65 +0,0 @@
package net.sf.openrocket.simulation.listeners.example;
import net.sf.openrocket.simulation.FlightEvent;
import net.sf.openrocket.simulation.SimulationStatus;
import net.sf.openrocket.simulation.exception.SimulationException;
import net.sf.openrocket.simulation.listeners.AbstractSimulationListener;
/**
* A simulation listener that stops the simulation after a specified number of steps or
* after a specified amount of simulation time.
*
* @author Sampo Niskanen <sampo.niskanen@iki.fi>
*/
public class StopSimulationListener extends AbstractSimulationListener {
private final int REPORT = 500;
private final double stopTime;
private final int stopStep;
private int step = 0;
private long startTime = -1;
private long time = -1;
public StopSimulationListener() {
this(0, 0);
}
public StopSimulationListener(double t, int n) {
stopTime = t;
stopStep = n;
}
@Override
public boolean handleFlightEvent(SimulationStatus status, FlightEvent event) {
if (event.getType() == FlightEvent.Type.LAUNCH) {
System.out.println("Simulation starting.");
time = System.nanoTime();
startTime = System.nanoTime();
}
return true;
}
@Override
public void postStep(SimulationStatus status) throws SimulationException {
step++;
if ((step % REPORT) == 0) {
long t = System.nanoTime();
System.out.printf("Step %4d, time=%.3f, took %d us/step (avg. %d us/step)\n",
step, status.getSimulationTime(), (t - time) / 1000 / REPORT, (t - startTime) / 1000 / step);
time = t;
}
if (status.getSimulationTime() >= stopTime || step >= stopStep) {
System.out.printf("Stopping simulation, step=%d time=%.3f\n", step, status.getSimulationTime());
status.getEventQueue().add(new FlightEvent(FlightEvent.Type.SIMULATION_END,
status.getSimulationTime(), null));
}
}
}

View File

@ -0,0 +1,52 @@
package net.sf.openrocket.simulation.extension.impl;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import net.sf.openrocket.document.Simulation;
import net.sf.openrocket.gui.SpinnerEditor;
import net.sf.openrocket.gui.adaptors.IntegerModel;
import net.sf.openrocket.gui.components.BasicSlider;
import net.sf.openrocket.plugin.Plugin;
import net.sf.openrocket.simulation.extension.AbstractSwingSimulationExtensionConfigurator;
import net.sf.openrocket.unit.UnitGroup;
@Plugin
public class StopSimulationConfigurator extends AbstractSwingSimulationExtensionConfigurator<StopSimulation> {
private JPanel panel;
private StopSimulation extension;
public StopSimulationConfigurator() {
super(StopSimulation.class);
}
@Override
protected JComponent getConfigurationComponent(StopSimulation extension, Simulation simulation, JPanel panel) {
this.panel = panel;
this.extension = extension;
addRow("Report Rate", "ReportRate", 0, 1000);
addRow("Stop Step", "StopStep", 0, 50000);
addRow("Stop Time", "StopTime", 0, 1000);
return panel;
}
private void addRow(String prompt, String fieldName, int min, int max) {
panel.add(new JLabel(prompt + ":"));
IntegerModel m = new IntegerModel(extension, fieldName, min, max);
JSpinner spin = new JSpinner(m.getSpinnerModel());
spin.setEditor(new SpinnerEditor(spin));
panel.add(spin, "w 65lp!");
BasicSlider slider = new BasicSlider(m.getSliderModel());
panel.add(slider, "w 75lp, wrap");
}
}