only:Improve toolset for default tune canned tune generation #4871

This commit is contained in:
rusefillc 2023-06-18 19:35:46 -04:00
parent 3e5998979e
commit 63872f3e15
5 changed files with 136 additions and 91 deletions

View File

@ -66,7 +66,7 @@ public class Msq {
return ci;
}
public static Msq readTune(String fileName) throws Exception {
public static Msq readTune(String fileName) throws JAXBException {
return XmlUtil.readModel(Msq.class, fileName);
}

View File

@ -31,7 +31,7 @@ public class XmlUtil {
writer.close();
}
public static <T> T readModel(Class<?> modelClass, String fileName) throws Exception {
public static <T> T readModel(Class<?> modelClass, String fileName) throws JAXBException {
File xmlFile = new File(fileName);
JAXBContext jaxbContext;
jaxbContext = JAXBContext.newInstance(modelClass);

View File

@ -0,0 +1,130 @@
package com.rusefi.tools.tune;
import com.opensr5.ini.DialogModel;
import com.opensr5.ini.IniFileModel;
import com.rusefi.*;
import com.rusefi.core.preferences.storage.Node;
import com.rusefi.output.ConfigStructure;
import com.rusefi.tune.xml.Constant;
import com.rusefi.tune.xml.Msq;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import static com.rusefi.ConfigFieldImpl.unquote;
import static com.rusefi.tools.tune.WriteSimulatorConfiguration.INI_FILE_FOR_SIMULATOR;
public class TuneCanTool {
public static final String SRC_TEST_RESOURCES = "src/test/resources/";
private static final String FOLDER = "generated";
public static final String DEFAULT_TUNE = FOLDER + File.separator + "simulator_tune.xml";
public static void main(String[] args) throws Exception {
IniFileModel ini = new IniFileModel().readIniFile(INI_FILE_FOR_SIMULATOR);
if (ini == null)
throw new IllegalStateException("Not found " + INI_FILE_FOR_SIMULATOR);
Msq simulatorDefaultTune = Msq.readTune(TuneCanTool.DEFAULT_TUNE);
RootHolder.ROOT = "../firmware/";
String fileName = "C:\\Users\\Dell2019\\Downloads\\1258.msq";
Msq custom = Msq.readTune(fileName);
StringBuilder sb = TuneCanTool.getTunePatch2(simulatorDefaultTune, custom, ini);
System.out.println(sb);
}
private static boolean isHardwareEnum(String type) {
switch (type) {
case "output_pin_e":
case "brain_input_pin_e":
case "adc_channel_e":
case "Gpio":
return true;
}
return false;
}
private static Object simplerSpaces(String value) {
if (value == null)
return value;
return value.replaceAll("\\s+", " ").trim();
}
@NotNull
public static StringBuilder getTunePatch2(Msq customOldTune, Msq lessOldDefaultTune, IniFileModel ini) throws IOException {
List<String> options = Files.readAllLines(Paths.get(RootHolder.ROOT + "../" + ConfigDefinition.CONFIG_PATH));
String[] totalArgs = options.toArray(new String[0]);
ReaderStateImpl state = new ReaderStateImpl();
ConfigDefinition.doJob(totalArgs, state);
StringBuilder sb = new StringBuilder();
for (DialogModel.Field f : ini.fieldsInUiOrder) {
String name = f.getKey();
Constant customValue = customOldTune.getConstantsAsMap().get(name);
Constant defaultValue = lessOldDefaultTune.getConstantsAsMap().get(name);
if (defaultValue == null) {
// no longer present
continue;
}
boolean isSameValue = simplerSpaces(defaultValue.getValue()).equals(simplerSpaces(customValue.getValue()));
if (!isSameValue) {
ConfigStructure s = state.getStructures().get("engine_configuration_s");
System.out.println(s);
ConfigField cf = s.getTsFieldByName(name);
if (cf == null) {
System.out.println("Not found " + name);
continue;
}
if (cf.getType().equals("boolean")) {
sb.append(TuneTools.getAssignmentCode(customValue.getName(), unquote(customValue.getValue())));
continue;
}
if (!Node.isNumeric(customValue.getValue())) {
// todo: smarter logic for enums
String type = cf.getType();
if (isHardwareEnum(type)) {
continue;
}
EnumsReader.EnumState sourceCodeEnum = state.getEnumsReader().getEnums().get(type);
if (sourceCodeEnum == null) {
System.out.println("No info for " + type);
continue;
}
String customEnum = state.getTsCustomLine().get(type);
int ordinal;
try {
ordinal = TuneTools.resolveEnumByName(customEnum, unquote(customValue.getValue()));
} catch (IllegalStateException e) {
System.out.println("Looks like things were renamed: " + customValue.getValue() + " not found in " + customEnum);
continue;
}
System.out.println(cf + " " + sourceCodeEnum + " " + customEnum + " " + ordinal);
String sourceCodeValue = sourceCodeEnum.findByValue(ordinal);
sb.append(TuneTools.getAssignmentCode(customValue.getName(), sourceCodeValue));
continue;
}
sb.append(TuneTools.getAssignmentCode(customValue.getName(), customValue.getValue()));
}
}
return sb;
}
}

View File

@ -39,5 +39,8 @@ public class WriteSimulatorConfiguration {
throw new IllegalStateException("Not found " + INI_FILE_FOR_SIMULATOR);
Msq m = MsqFactory.valueOf(configuration, ini);
m.writeXmlFile(TuneCanTool.DEFAULT_TUNE);
Msq newTuneJustToValidate = Msq.readTune(TuneCanTool.DEFAULT_TUNE);
System.out.println("Looks valid " + newTuneJustToValidate);
}
}

View File

@ -1,23 +1,14 @@
package com.rusefi.tune;
import com.opensr5.ini.DialogModel;
import com.opensr5.ini.IniFileModel;
import com.rusefi.*;
import com.rusefi.core.preferences.storage.Node;
import com.rusefi.output.ConfigStructure;
import com.rusefi.tools.tune.TuneCanTool;
import com.rusefi.tools.tune.TuneTools;
import com.rusefi.tune.xml.Constant;
import com.rusefi.tune.xml.Msq;
import com.rusefi.tune.xml.Page;
import org.junit.Assert;
import org.junit.Test;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import static com.rusefi.ConfigFieldImpl.unquote;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@ -33,71 +24,9 @@ public class LoadOlderTuneTest {
IniFileModel ini = new IniFileModel().readIniFile(TuneReadWriteTest.TEST_INI);
assertFalse(ini.fieldsInUiOrder.isEmpty());
List<String> options = Files.readAllLines(Paths.get("../../" + ConfigDefinition.CONFIG_PATH));
String[] totalArgs = options.toArray(new String[0]);
RootHolder.ROOT = "../../firmware/";
ReaderStateImpl state = new ReaderStateImpl();
ConfigDefinition.doJob(totalArgs, state);
StringBuilder sb = new StringBuilder();
for (DialogModel.Field f : ini.fieldsInUiOrder) {
String name = f.getKey();
Constant customValue = customOldTune.getConstantsAsMap().get(name);
Constant defaultValue = lessOldDefaultTune.getConstantsAsMap().get(name);
if (defaultValue == null) {
// no longer present
continue;
}
boolean isSameValue = simplerSpaces(defaultValue.getValue()).equals(simplerSpaces(customValue.getValue()));
if (!isSameValue) {
ConfigStructure s = state.getStructures().get("engine_configuration_s");
System.out.println(s);
ConfigField cf = s.getTsFieldByName(name);
if (cf == null) {
System.out.println("Not found " + name);
continue;
}
if (cf.getType().equals("boolean")) {
sb.append(TuneTools.getAssignmentCode(customValue.getName(), unquote(customValue.getValue())));
continue;
}
if (!Node.isNumeric(customValue.getValue())) {
// todo: smarter logic for enums
String type = cf.getType();
if (isHardwareEnum(type)) {
continue;
}
EnumsReader.EnumState sourceCodeEnum = state.getEnumsReader().getEnums().get(type);
if (sourceCodeEnum == null) {
System.out.println("No info for " + type);
continue;
}
String customEnum = state.getTsCustomLine().get(type);
int ordinal;
try {
ordinal = TuneTools.resolveEnumByName(customEnum, unquote(customValue.getValue()));
} catch (IllegalStateException e) {
System.out.println("Looks like things were renamed: " + customValue.getValue() + " not found in " + customEnum);
continue;
}
System.out.println(cf + " " + sourceCodeEnum + " " + customEnum + " " + ordinal);
String sourceCodeValue = sourceCodeEnum.findByValue(ordinal);
sb.append(TuneTools.getAssignmentCode(customValue.getName(), sourceCodeValue));
continue;
}
sb.append(TuneTools.getAssignmentCode(customValue.getName(), customValue.getValue()));
}
}
StringBuilder sb = TuneCanTool.getTunePatch2(customOldTune, lessOldDefaultTune, ini);
assertEquals("\tengineConfiguration->ignitionMode = IM_ONE_COIL;\n" +
"\tengineConfiguration->cylindersCount = 4;\n" +
@ -132,23 +61,6 @@ public class LoadOlderTuneTest {
"\tengineConfiguration->hip9011CsPinMode = ", sb.substring(0, 1500));
}
private static boolean isHardwareEnum(String type) {
switch (type) {
case "output_pin_e":
case "brain_input_pin_e":
case "adc_channel_e":
case "Gpio":
return true;
}
return false;
}
private static Object simplerSpaces(String value) {
if (value == null)
return value;
return value.replaceAll("\\s+", " ").trim();
}
@Test(expected = IllegalStateException.class)
public void testLegacyCustomEnumOrdinal() {
String tsCustomLine = "bits, U08, @OFFSET@, [0:1], \"Single Coil\", \"Individual Coils\", \"Wasted Spark\", \"Two Distributors\"";