progress: code generator for StateDictionary

This commit is contained in:
Andrey 2023-07-03 11:55:01 -04:00
parent 9e78a3952a
commit e316d7749a
3 changed files with 112 additions and 6 deletions

View File

@ -4,7 +4,6 @@ import com.devexperts.logging.Logging;
import com.rusefi.*;
import com.rusefi.output.*;
import com.rusefi.util.LazyFile;
import com.rusefi.util.LazyFileImpl;
import org.yaml.snakeyaml.Yaml;
import java.io.*;
@ -38,15 +37,18 @@ public class LiveDataProcessor {
private final StringBuilder fancyNewStuff = new StringBuilder();
public final StateDictionaryGenerator stateDictionaryGenerator;
private final StringBuilder fancyNewMenu = new StringBuilder();
private final StringBuilder fragmentsContent = new StringBuilder(header);
private final String extraPrepend = System.getProperty("LiveDataProcessor.extra_prepend");
public LiveDataProcessor(ReaderProvider readerProvider, LazyFile.LazyFileFactory fileFactory) {
public LiveDataProcessor(String yamlFileName, ReaderProvider readerProvider, LazyFile.LazyFileFactory fileFactory) {
this.readerProvider = readerProvider;
this.fileFactory = fileFactory;
stateDictionaryGenerator = new StateDictionaryGenerator(yamlFileName);
}
public static void main(String[] args) throws IOException {
@ -59,7 +61,7 @@ public class LiveDataProcessor {
Map<String, Object> data = getStringObjectMap(new FileReader(yamlFileName));
LiveDataProcessor liveDataProcessor = new LiveDataProcessor(ReaderProvider.REAL, LazyFile.REAL);
LiveDataProcessor liveDataProcessor = new LiveDataProcessor(yamlFileName, ReaderProvider.REAL, LazyFile.REAL);
int sensorTsPosition = liveDataProcessor.handleYaml(data);
liveDataProcessor.writeFiles();
@ -94,7 +96,7 @@ public class LiveDataProcessor {
}
interface EntryHandler {
void onEntry(String name, String javaName, String folder, String prepend, boolean withCDefines, String[] outputNames, String constexpr, String conditional, String engineModule, Boolean isPtr) throws IOException;
void onEntry(String name, String javaName, String folder, String prepend, boolean withCDefines, String[] outputNames, String constexpr, String conditional, String engineModule, Boolean isPtr, String cppFileName) throws IOException;
}
public int handleYaml(Map<String, Object> data) throws IOException {
@ -110,9 +112,11 @@ public class LiveDataProcessor {
EntryHandler handler = new EntryHandler() {
@Override
public void onEntry(String name, String javaName, String folder, String prepend, boolean withCDefines, String[] outputNames, String constexpr, String conditional, String engineModule, Boolean isPtr) throws IOException {
public void onEntry(String name, String javaName, String folder, String prepend, boolean withCDefines, String[] outputNames, String constexpr, String conditional, String engineModule, Boolean isPtr, String cppFileName) throws IOException {
// TODO: use outputNames
stateDictionaryGenerator.onEntry(name, javaName, folder, prepend, withCDefines, outputNames, constexpr, conditional, engineModule, isPtr, cppFileName);
int startingPosition = javaSensorsConsumer.sensorTsPosition;
log.info("Starting " + name + " at " + startingPosition + " with [" + conditional + "]");
@ -182,6 +186,9 @@ public class LiveDataProcessor {
String prepend = (String) entry.get("prepend");
String constexpr = (String) entry.get("constexpr");
String engineModule = (String) entry.get("engineModule");
String cppFileName = (String) entry.get("cppFileName");
if (cppFileName == null)
cppFileName = name;
String conditional = (String) entry.get("conditional_compilation");
Boolean withCDefines = (Boolean) entry.get("withCDefines");
Boolean isPtr = (Boolean) entry.get("isPtr");
@ -203,7 +210,7 @@ public class LiveDataProcessor {
nameList.toArray(outputNamesArr);
}
handler.onEntry(name, java, folder, prepend, withCDefines, outputNamesArr, constexpr, conditional, engineModule, isPtr);
handler.onEntry(name, java, folder, prepend, withCDefines, outputNamesArr, constexpr, conditional, engineModule, isPtr, cppFileName);
String enumName = "LDS_" + name;
String type = name + "_s"; // convention
@ -240,6 +247,8 @@ public class LiveDataProcessor {
outputValueConsumer.endFile();
GetConfigValueConsumer.writeStringToFile("../java_console/io/src/main/java/com/rusefi/enums/StateDictionaryFactory.java", stateDictionaryGenerator.getCompleteClass(), fileFactory);
totalSensors.append(javaSensorsConsumer.getContent());
return javaSensorsConsumer.sensorTsPosition;

View File

@ -0,0 +1,47 @@
package com.rusefi.ldmp;
import com.rusefi.ToolUtil;
import com.rusefi.output.FileJavaFieldsConsumer;
import com.rusefi.util.LazyFile;
import java.io.IOException;
import java.util.Date;
import static com.rusefi.VariableRegistry.quote;
public class StateDictionaryGenerator implements LiveDataProcessor.EntryHandler {
public final StringBuilder content = new StringBuilder();
private final String yamlFileName;
public StateDictionaryGenerator(String yamlFileName) {
this.yamlFileName = yamlFileName;
}
@Override
public void onEntry(String name, String javaName, String folder, String prepend, boolean withCDefines, String[] outputNames, String constexpr, String conditional, String engineModule, Boolean isPtr, String cppFileName) throws IOException {
content.append(" stateDictionary.register(live_data_e.LDS_");
content.append(name).append(", ");
content.append(FileJavaFieldsConsumer.remoteExtension(javaName)).append(".VALUES, ");
content.append(quote(cppFileName));
content.append(");\n");
}
public String getCompleteClass() {
ToolUtil.TOOL = getClass().getSimpleName();
return "package com.rusefi.enums;\n" +
"//" + ToolUtil.getGeneratedAutomaticallyTag() + yamlFileName + " on " + new Date() + "n" +
"\n" +
"import com.rusefi.config.generated.*;\n" +
"import com.rusefi.ldmp.StateDictionary;\n" +
"\n" +
"public class StateDictionaryFactory {\n" +
" public static void initialize(StateDictionary stateDictionary) {\n"
+ content +
" }\n" +
"}";
}
}

View File

@ -0,0 +1,50 @@
package com.rusefi.ldmp;
import com.rusefi.util.LazyFile;
import org.junit.Test;
import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.assertEquals;
public class StateDictionaryGeneratorTest {
@Test
public void test() throws IOException {
String testYaml = "Usages:\n" +
"# output_channels always goes first at least because it has protocol version at well-known offset\n" +
" - name: output_channels\n" +
" java: TsOutputs.java\n" +
" folder: console/binary\n" +
" cppFileName: status_loop\n" +
" constexpr: \"engine->outputChannels\"\n" +
"\n" +
" - name: fuel_computer\n" +
" java: FuelComputer.java\n" +
" folder: controllers/algo/fuel\n" +
" constexpr: \"engine->fuelComputer\"\n" +
" conditional_compilation: \"EFI_ENGINE_CONTROL\"\n";
Map<String, Object> data = LiveDataProcessor.getStringObjectMap(new StringReader(testYaml));
Map<String, StringBufferLazyFile> fileCapture = new HashMap<>();
LiveDataProcessor liveDataProcessor = new LiveDataProcessor("test", fileName -> new StringReader(""), new LazyFile.LazyFileFactory() {
@Override
public LazyFile create(String fileName) {
StringBufferLazyFile file = new StringBufferLazyFile();
fileCapture.put(fileName, file);
return file;
}
});
liveDataProcessor.handleYaml(data);
assertEquals(6, fileCapture.size());
assertEquals(" stateDictionary.register(live_data_e.LDS_output_channels, TsOutputs.VALUES, \"status_loop\");\n" +
" stateDictionary.register(live_data_e.LDS_fuel_computer, FuelComputer.VALUES, \"fuel_computer\");\n", liveDataProcessor.stateDictionaryGenerator.content.toString());
}
}