[#2204] Big rewrite of motor config substitutor to make it general + add cases
This commit is contained in:
parent
ba67e72e59
commit
ccffffa186
@ -12,11 +12,11 @@ import net.sf.openrocket.rocketcomponent.FlightConfigurationId;
|
|||||||
import net.sf.openrocket.rocketcomponent.MotorMount;
|
import net.sf.openrocket.rocketcomponent.MotorMount;
|
||||||
import net.sf.openrocket.rocketcomponent.Rocket;
|
import net.sf.openrocket.rocketcomponent.Rocket;
|
||||||
import net.sf.openrocket.rocketcomponent.RocketComponent;
|
import net.sf.openrocket.rocketcomponent.RocketComponent;
|
||||||
import net.sf.openrocket.startup.Application;
|
|
||||||
import net.sf.openrocket.util.ArrayList;
|
|
||||||
import net.sf.openrocket.util.Chars;
|
import net.sf.openrocket.util.Chars;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
@ -26,8 +26,8 @@ import java.util.regex.Pattern;
|
|||||||
* General substitutor for motor configurations. This currently includes substitutions for
|
* General substitutor for motor configurations. This currently includes substitutions for
|
||||||
* - {motors} - the motor designation (e.g. "M1350-0")
|
* - {motors} - the motor designation (e.g. "M1350-0")
|
||||||
* - {manufacturers} - the motor manufacturer (e.g. "AeroTech")
|
* - {manufacturers} - the motor manufacturer (e.g. "AeroTech")
|
||||||
* - a combination of motors and manufacturers, e.g. {motors | manufacturers} -> "M1350-0 | AeroTech"
|
* - a combination of motors and manufacturers, e.g. {motors manufacturers} -> "M1350-0 AeroTech"
|
||||||
* You can choose which comes first and what the separator is. E.g. {manufacturers, motors} -> "AeroTech, M1350-0".
|
* You can choose which comes first is. E.g. {manufacturers motors} -> "AeroTech M1350-0".
|
||||||
*
|
*
|
||||||
* <p>
|
* <p>
|
||||||
* This substitutor is added through injection. All substitutors with the "@Plugin" tag in the formatting package will
|
* This substitutor is added through injection. All substitutors with the "@Plugin" tag in the formatting package will
|
||||||
@ -35,126 +35,141 @@ import java.util.regex.Pattern;
|
|||||||
*/
|
*/
|
||||||
@Plugin
|
@Plugin
|
||||||
public class MotorConfigurationSubstitutor implements RocketSubstitutor {
|
public class MotorConfigurationSubstitutor implements RocketSubstitutor {
|
||||||
public static final String SUBSTITUTION_START = "{";
|
// Substitution start and end
|
||||||
public static final String SUBSTITUTION_END = "}";
|
public static final String SUB_START = "{";
|
||||||
public static final String SUBSTITUTION_MOTORS = "motors";
|
public static final String SUB_END = "}";
|
||||||
public static final String SUBSTITUTION_MANUFACTURERS = "manufacturers";
|
|
||||||
|
|
||||||
// Substitutions for combinations of motors and manufacturers
|
// Map containing substitution words and their corresponding replacement strings.
|
||||||
private static final String SUBSTITUTION_PATTERN = "\\" + SUBSTITUTION_START +
|
private static final Map<String, Substitutor> SUBSTITUTIONS = new HashMap<>();
|
||||||
"(" + SUBSTITUTION_MOTORS + "|" + SUBSTITUTION_MANUFACTURERS + ")" +
|
|
||||||
"(.*?)" +
|
static {
|
||||||
"(" + SUBSTITUTION_MOTORS + "|" + SUBSTITUTION_MANUFACTURERS + ")" +
|
SUBSTITUTIONS.put("motors", new MotorSubstitutor());
|
||||||
"\\" + SUBSTITUTION_END;
|
SUBSTITUTIONS.put("manufacturers", new ManufacturerSubstitutor());
|
||||||
|
SUBSTITUTIONS.put("cases", new CaseSubstitutor());
|
||||||
|
}
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private Translator trans;
|
private Translator trans;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean containsSubstitution(String input) {
|
public boolean containsSubstitution(String input) {
|
||||||
return getSubstitutionContent(input) != null;
|
Pattern pattern = Pattern.compile("\\" + SUB_START + "(.*?[^\\s])\\" + SUB_END); // ensures non-whitespace content
|
||||||
|
Matcher matcher = pattern.matcher(input);
|
||||||
|
|
||||||
|
while (matcher.find()) {
|
||||||
|
String tagContent = matcher.group(1).trim();
|
||||||
|
for (String key : SUBSTITUTIONS.keySet()) {
|
||||||
|
if (tagContent.contains(key)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String substitute(String input, Rocket rocket, FlightConfigurationId configId) {
|
public String substitute(String input, Rocket rocket, FlightConfigurationId configId) {
|
||||||
String description = getConfigurationSubstitution(input, rocket, configId);
|
Pattern pattern = Pattern.compile("\\" + SUB_START + "(.*?)\\" + SUB_END);
|
||||||
String substitutionString = getSubstiutionString(input);
|
Matcher matcher = pattern.matcher(input);
|
||||||
if (substitutionString != null) {
|
StringBuilder resultBuffer = new StringBuilder();
|
||||||
return input.replace(substitutionString, description);
|
|
||||||
|
while (matcher.find()) {
|
||||||
|
String tagContent = matcher.group(1).trim();
|
||||||
|
String[] keys = tagContent.split("\\s");
|
||||||
|
|
||||||
|
List<Map<AxialStage, List<String>>> stageSubstitutes = new ArrayList<>();
|
||||||
|
for (String key : keys) {
|
||||||
|
if (SUBSTITUTIONS.containsKey(key)) {
|
||||||
|
Map<AxialStage, List<String>> sub = SUBSTITUTIONS.get(key).substitute(rocket, configId);
|
||||||
|
stageSubstitutes.add(sub);
|
||||||
}
|
}
|
||||||
return input;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
FlightConfiguration config = rocket.getFlightConfiguration(configId);
|
||||||
public Map<String, String> getDescriptions() {
|
List<String> combinations = combineSubstitutesForStages(rocket, config, stageSubstitutes, " ");
|
||||||
return null;
|
|
||||||
|
String combined = String.join("; ", combinations);
|
||||||
|
matcher.appendReplacement(resultBuffer, Matcher.quoteReplacement(combined));
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getConfigurationSubstitution(String input, Rocket rocket, FlightConfigurationId fcid) {
|
matcher.appendTail(resultBuffer);
|
||||||
StringBuilder configurations = new StringBuilder();
|
|
||||||
int motorCount = 0;
|
|
||||||
|
|
||||||
// Iterate over each stage and store the manufacturer of each motor
|
return resultBuffer.toString();
|
||||||
List<List<String>> list = new ArrayList<>();
|
|
||||||
List<String> currentList = new ArrayList<>();
|
|
||||||
|
|
||||||
String[] content = getSubstitutionContent(input);
|
|
||||||
if (content == null) {
|
|
||||||
return "";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
FlightConfiguration config = rocket.getFlightConfiguration(fcid);
|
private List<String> combineSubstitutesForStages(Rocket rocket, FlightConfiguration config,
|
||||||
for (RocketComponent c : rocket) {
|
List<Map<AxialStage, List<String>>> stageSubstitutes, String separator) {
|
||||||
if (c instanceof AxialStage) {
|
List<String> combinations = new ArrayList<>();
|
||||||
currentList = new ArrayList<>();
|
|
||||||
list.add(currentList);
|
|
||||||
} else if (c instanceof MotorMount) {
|
|
||||||
MotorMount mount = (MotorMount) c;
|
|
||||||
MotorConfiguration inst = mount.getMotorConfig(fcid);
|
|
||||||
Motor motor = inst.getMotor();
|
|
||||||
|
|
||||||
if (mount.isMotorMount() && config.isComponentActive(mount) && (motor != null)) {
|
// Parse through all the stages to get the final configuration string
|
||||||
String motorDesignation = motor.getMotorName(inst.getEjectionDelay());
|
for (AxialStage stage : rocket.getStageList()) {
|
||||||
String manufacturer = "";
|
if (!config.isStageActive(stage.getStageNumber())) {
|
||||||
if (motor instanceof ThrustCurveMotor) {
|
combinations.add("");
|
||||||
manufacturer = ((ThrustCurveMotor) motor).getManufacturer().getDisplayName();
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < mount.getMotorCount(); i++) {
|
|
||||||
if (content.length == 2) {
|
|
||||||
if (SUBSTITUTION_MOTORS.equals(content[1])) {
|
|
||||||
currentList.add(motorDesignation);
|
|
||||||
} else if (SUBSTITUTION_MANUFACTURERS.equals(content[1])) {
|
|
||||||
currentList.add(manufacturer);
|
|
||||||
} else {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
} else if (content.length == 4) {
|
|
||||||
String configString;
|
StringBuilder sbStageSub = new StringBuilder();
|
||||||
if (content[1].equals(SUBSTITUTION_MOTORS)) {
|
// Parse through all the substitutes (motors, manufacturers, etc.) for each stage to build a combined stage substitution
|
||||||
configString = motorDesignation;
|
for (Map<AxialStage, List<String>> substituteMap : stageSubstitutes) {
|
||||||
} else if (content[1].equals(SUBSTITUTION_MANUFACTURERS)) {
|
List<String> substitutes = substituteMap.get(stage);
|
||||||
configString = manufacturer;
|
if (substitutes == null || substitutes.isEmpty()) {
|
||||||
} else {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
configString += content[2];
|
|
||||||
if (content[3].equals(SUBSTITUTION_MOTORS)) {
|
// If this is not the first substitute, add a separator between the different substitutes (motor, manufacturer, etc.)
|
||||||
configString += motorDesignation;
|
if (!sbStageSub.isEmpty()) {
|
||||||
} else if (content[3].equals(SUBSTITUTION_MANUFACTURERS)) {
|
sbStageSub.append(separator);
|
||||||
configString += manufacturer;
|
|
||||||
} else {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
currentList.add(configString);
|
|
||||||
} else {
|
// Create a final substitute for this sub tag from the list of substitutes
|
||||||
continue;
|
String finalSubstitute = getFinalSubstitute(substitutes, sbStageSub);
|
||||||
|
sbStageSub.append(finalSubstitute);
|
||||||
}
|
}
|
||||||
motorCount++;
|
|
||||||
|
if (sbStageSub.isEmpty()) {
|
||||||
|
sbStageSub.append(trans.get("Rocket.motorCount.noStageMotors"));
|
||||||
}
|
}
|
||||||
|
combinations.add(sbStageSub.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if all the stages are empty
|
||||||
|
boolean onlyEmpty = true;
|
||||||
|
for (String s : combinations) {
|
||||||
|
if (!s.isEmpty() && !s.equals(trans.get("Rocket.motorCount.noStageMotors"))) {
|
||||||
|
onlyEmpty = false;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (motorCount == 0) {
|
// If all the stages are empty, return a single "No motors" string
|
||||||
return trans.get("Rocket.motorCount.Nomotor");
|
if (combinations.isEmpty() || onlyEmpty) {
|
||||||
|
return Collections.singletonList(trans.get("Rocket.motorCount.Nomotor"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Change multiple occurrences of a motor to n x motor
|
return combinations;
|
||||||
List<String> stages = new ArrayList<>();
|
}
|
||||||
for (List<String> stage : list) {
|
|
||||||
|
private static String getFinalSubstitute(List<String> substitutes, StringBuilder sbStageSub) {
|
||||||
|
if (substitutes.size() == 1 || !sbStageSub.isEmpty()) {
|
||||||
|
return substitutes.get(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Change multiple occurrences of a configuration to 'n x configuration'
|
||||||
String stageName = "";
|
String stageName = "";
|
||||||
String previous = null;
|
String previous = null;
|
||||||
int count = 0;
|
int count = 0;
|
||||||
|
|
||||||
Collections.sort(stage);
|
Collections.sort(substitutes);
|
||||||
for (String current : stage) {
|
for (String current : substitutes) {
|
||||||
|
if (current.isEmpty()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if (current.equals(previous)) {
|
if (current.equals(previous)) {
|
||||||
count++;
|
count++;
|
||||||
} else {
|
} else {
|
||||||
if (previous != null) {
|
if (previous != null) {
|
||||||
String s = count > 1 ? count + Chars.TIMES + previous : previous;
|
String s = count > 1 ? count + Chars.TIMES + previous : previous;
|
||||||
stageName = stageName.equals("") ? s : stageName + "," + s;
|
stageName = stageName.isEmpty() ? s : stageName + "," + s;
|
||||||
}
|
}
|
||||||
|
|
||||||
previous = current;
|
previous = current;
|
||||||
@ -164,88 +179,106 @@ public class MotorConfigurationSubstitutor implements RocketSubstitutor {
|
|||||||
|
|
||||||
if (previous != null) {
|
if (previous != null) {
|
||||||
String s = count > 1 ? "" + count + Chars.TIMES + previous : previous;
|
String s = count > 1 ? "" + count + Chars.TIMES + previous : previous;
|
||||||
stageName = stageName.equals("") ? s : stageName + "," + s;
|
stageName = stageName.isEmpty() ? s : stageName + "," + s;
|
||||||
}
|
}
|
||||||
|
|
||||||
stages.add(stageName);
|
return stageName;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < stages.size(); i++) {
|
|
||||||
String s = stages.get(i);
|
|
||||||
if (s.equals("") && config.isStageActive(i)) {
|
|
||||||
s = trans.get("Rocket.motorCount.noStageMotors");
|
|
||||||
}
|
|
||||||
|
|
||||||
configurations.append(i == 0 ? s : "; " + s);
|
@Override
|
||||||
}
|
public Map<String, String> getDescriptions() {
|
||||||
|
|
||||||
return configurations.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns which string in input should be replaced, or null if no text needs to be replaced.
|
|
||||||
* @param input The input string
|
|
||||||
* @return The string to replace, or null if no text needs to be replaced.
|
|
||||||
*/
|
|
||||||
private static String getSubstiutionString(String input) {
|
|
||||||
String[] content = getSubstitutionContent(input);
|
|
||||||
if (content != null) {
|
|
||||||
return content[0];
|
|
||||||
}
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private interface Substitutor {
|
||||||
/**
|
/**
|
||||||
* Fills in the content of the substitution tag and the separator.
|
* Generates a string to substitute a certain substitutor word with.
|
||||||
* If there are both a motor and a manufacturer substitution tag, the array will contain the following:
|
* @param rocket The used rocket
|
||||||
* [0] = The full tag, including substitution start and end
|
* @param fcid The flight configuration id
|
||||||
* [1] = The motor/manufacturer substitution tag, depending on which one was found first.
|
* @return A list of strings to substitute the substitutor word with for each stage
|
||||||
* if there are two substitution tags, the array will also contain the following:
|
|
||||||
* ([2] = The separator)
|
|
||||||
* ([3] = The motor/manufacturer substitution tag, depending on which one was found first.)
|
|
||||||
* @param input The input string
|
|
||||||
* @return The content of the substitution tag and the separator, or null if no text needs to be replaced.
|
|
||||||
*/
|
*/
|
||||||
private static String[] getSubstitutionContent(String input) {
|
Map<AxialStage, List<String>> substitute(Rocket rocket, FlightConfigurationId fcid);
|
||||||
// First try with only the motors tag
|
|
||||||
String pattern = "\\" + SUBSTITUTION_START + "(" + SUBSTITUTION_MOTORS + ")" + "\\" + SUBSTITUTION_END;
|
|
||||||
Pattern regexPattern = Pattern.compile(pattern);
|
|
||||||
Matcher matcher = regexPattern.matcher(input);
|
|
||||||
if (matcher.find()) {
|
|
||||||
String[] content = new String[2];
|
|
||||||
content[0] = matcher.group(0);
|
|
||||||
content[1] = matcher.group(1);
|
|
||||||
return content;
|
|
||||||
}
|
|
||||||
// First try with only the manufacturers tag
|
|
||||||
pattern = "\\" + SUBSTITUTION_START + "(" + SUBSTITUTION_MANUFACTURERS + ")" + "\\" + SUBSTITUTION_END;
|
|
||||||
regexPattern = Pattern.compile(pattern);
|
|
||||||
matcher = regexPattern.matcher(input);
|
|
||||||
if (matcher.find()) {
|
|
||||||
String[] content = new String[2];
|
|
||||||
content[0] = matcher.group(0);
|
|
||||||
content[1] = matcher.group(1);
|
|
||||||
return content;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Then try combined patterns
|
public abstract static class BaseSubstitutor implements Substitutor {
|
||||||
pattern = SUBSTITUTION_PATTERN;
|
|
||||||
regexPattern = Pattern.compile(pattern);
|
protected abstract String getData(Motor motor, MotorConfiguration motorConfig);
|
||||||
matcher = regexPattern.matcher(input);
|
|
||||||
if (matcher.find()) {
|
@Override
|
||||||
String[] content = new String[4];
|
public Map<AxialStage, List<String>> substitute(Rocket rocket, FlightConfigurationId fcid) {
|
||||||
content[0] = matcher.group(0);
|
List<String> dataList; // Data for one stage. Is a list because multiple motors per stage are possible
|
||||||
content[1] = matcher.group(1);
|
Map<AxialStage, List<String>> stageMap = new HashMap<>(); // Data for all stages
|
||||||
if (matcher.groupCount() >= 3) {
|
|
||||||
content[2] = matcher.group(2);
|
FlightConfiguration config = rocket.getFlightConfiguration(fcid);
|
||||||
content[3] = matcher.group(3);
|
|
||||||
for (int i = 4; i < matcher.groupCount(); i++) {
|
for (AxialStage stage : rocket.getStageList()) {
|
||||||
content[3] += matcher.group(i);
|
if (config.isStageActive(stage.getStageNumber())) {
|
||||||
|
dataList = new ArrayList<>();
|
||||||
|
stageMap.put(stage, dataList);
|
||||||
|
} else {
|
||||||
|
stageMap.put(stage, null);
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (RocketComponent child : stage.getAllChildren()) {
|
||||||
|
// If the child is nested inside another stage (e.g. booster), skip it
|
||||||
|
// Plus other conditions :) But I'm not gonna bore you with those details. The goal
|
||||||
|
// of code documentation is to not waste a programmers time by making them read too
|
||||||
|
// much text when you can word something in a more concise way. I think I've done that
|
||||||
|
// here. I think I have succeeded. Yes. Anyway, have a good day reader!
|
||||||
|
if (child.getStage() != stage || !(child instanceof MotorMount mount) || !mount.isMotorMount()) {
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
return content;
|
|
||||||
|
MotorConfiguration inst = mount.getMotorConfig(fcid);
|
||||||
|
Motor motor = inst.getMotor();
|
||||||
|
|
||||||
|
// Mount has no motor
|
||||||
|
if (motor == null) {
|
||||||
|
dataList.add("");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the data for this substitutor word
|
||||||
|
String data = getData(motor, inst);
|
||||||
|
|
||||||
|
// Add the data for each motor instance
|
||||||
|
for (int i = 0; i < mount.getMotorCount(); i++) {
|
||||||
|
dataList.add(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return stageMap;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class MotorSubstitutor extends BaseSubstitutor {
|
||||||
|
@Override
|
||||||
|
protected String getData(Motor motor, MotorConfiguration motorConfig) {
|
||||||
|
return motor.getMotorName(motorConfig.getEjectionDelay());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class ManufacturerSubstitutor extends BaseSubstitutor {
|
||||||
|
@Override
|
||||||
|
protected String getData(Motor motor, MotorConfiguration motorConfig) {
|
||||||
|
if (motor instanceof ThrustCurveMotor) {
|
||||||
|
return ((ThrustCurveMotor) motor).getManufacturer().getDisplayName();
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class CaseSubstitutor extends BaseSubstitutor {
|
||||||
|
@Override
|
||||||
|
protected String getData(Motor motor, MotorConfiguration motorConfig) {
|
||||||
|
if (motor instanceof ThrustCurveMotor) {
|
||||||
|
return ((ThrustCurveMotor) motor).getCaseInfo();
|
||||||
|
}
|
||||||
|
return "";
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user