From 7ccaaef408358895e8af1904308f485ae614cd75 Mon Sep 17 00:00:00 2001 From: rusefillc Date: Sun, 2 Jan 2022 02:03:07 -0500 Subject: [PATCH] live data progress --- .../main/java/com/rusefi/CodeWalkthrough.java | 19 +++++++++--- .../rusefi/livedata/LiveDataParserPanel.java | 31 +++++++++---------- .../ui/livedata/VariableValueSource.java | 22 ++++++++++++- .../livedata/LiveDataConventionTest.java | 2 +- .../livedata/LiveDataParserSandbox.java | 22 ++++++++++--- .../ui/livedata/LiveDataParserTest.java | 18 +++++------ 6 files changed, 78 insertions(+), 36 deletions(-) diff --git a/java_console/ui/src/main/java/com/rusefi/CodeWalkthrough.java b/java_console/ui/src/main/java/com/rusefi/CodeWalkthrough.java index ff33ff65f7..f7cce492e0 100644 --- a/java_console/ui/src/main/java/com/rusefi/CodeWalkthrough.java +++ b/java_console/ui/src/main/java/com/rusefi/CodeWalkthrough.java @@ -29,6 +29,7 @@ public class CodeWalkthrough { public static final Color BROKEN_CODE = Color.orange; public static final Color TRUE_CONDITION = Color.GREEN; public static final Color FALSE_CONDITION = Color.RED; + public static final Color CONFIG = Color.BLUE; static { log.configureDebugEnabled(false); @@ -76,7 +77,11 @@ public class CodeWalkthrough { @Override public void visitTerminal(TerminalNode node) { allTerminals.add(node); - if ("else".equalsIgnoreCase(node.getSymbol().getText())) { + + String text = node.getSymbol().getText(); + valueSource.getValue(text); + + if ("else".equalsIgnoreCase(text)) { if (log.debugEnabled()) log.debug("CONDITIONAL ELSE terminal, flipping condition"); @@ -98,8 +103,14 @@ public class CodeWalkthrough { if (currentState.isEmpty()) { painter.paintBackground(PASSIVE_CODE, new Range(ctx)); } else { - Boolean state = (Boolean) valueSource.getValue(conditionVariable); - BranchingState branchingState = BranchingState.valueOf(state); + VariableValueSource.VariableState state = valueSource.getValue(conditionVariable); + Boolean value; + if (state == null) { + value = null; + } else { + value = state.getValue() != 0; + } + BranchingState branchingState = BranchingState.valueOf(value); if (log.debugEnabled()) log.debug("CURRENT STATE ADD " + state); currentState.add(branchingState); @@ -154,7 +165,7 @@ public class CodeWalkthrough { allTerminals.get(i + 1).getText().equals("->") ) { Token token = allTerminals.get(i + 2).getSymbol(); - painter.paintForeground(Color.BLUE, new Range(token, token)); + painter.paintForeground(CONFIG, new Range(token, token)); configTokens.add(token); } } diff --git a/java_console/ui/src/main/java/com/rusefi/livedata/LiveDataParserPanel.java b/java_console/ui/src/main/java/com/rusefi/livedata/LiveDataParserPanel.java index a1457863ce..0169686ba4 100644 --- a/java_console/ui/src/main/java/com/rusefi/livedata/LiveDataParserPanel.java +++ b/java_console/ui/src/main/java/com/rusefi/livedata/LiveDataParserPanel.java @@ -170,24 +170,21 @@ public class LiveDataParserPanel { private static LiveDataParserPanel createLiveDataParserPanel(UIContext uiContext, final live_data_e live_data_e, final Field[] values, String fileName) { AtomicReference reference = new AtomicReference<>(); - LiveDataParserPanel livePanel = new LiveDataParserPanel(uiContext, new VariableValueSource() { - @Override - public Object getValue(String name) { - byte[] bytes = reference.get(); - if (bytes == null) - return null; - Field f = Field.findFieldOrNull(values, "", name); - if (f == null) { - log.error("BAD condition, should be variable: " + name); - return null; - } - int number = f.getValue(new ConfigurationImage(bytes)).intValue(); - if (log.debugEnabled()) { - log.debug("getValue(" + name + "): " + number); - } - // convert Number to Boolean - return number != 0; + LiveDataParserPanel livePanel = new LiveDataParserPanel(uiContext, name -> { + byte[] bytes = reference.get(); + if (bytes == null) + return null; + Field f = Field.findFieldOrNull(values, "", name); + if (f == null) { + //log.error("BAD condition, should be variable: " + name); + return null; } + double number = f.getValue(new ConfigurationImage(bytes)).doubleValue(); + if (log.debugEnabled()) { + log.debug("getValue(" + name + "): " + number); + } + // convert Number to Boolean + return new VariableValueSource.VariableState(f, number); }, fileName); RefreshActions refreshAction = new RefreshActions() { @Override diff --git a/java_console/ui/src/main/java/com/rusefi/ui/livedata/VariableValueSource.java b/java_console/ui/src/main/java/com/rusefi/ui/livedata/VariableValueSource.java index 6517737b33..385dc17b3a 100644 --- a/java_console/ui/src/main/java/com/rusefi/ui/livedata/VariableValueSource.java +++ b/java_console/ui/src/main/java/com/rusefi/ui/livedata/VariableValueSource.java @@ -1,7 +1,27 @@ package com.rusefi.ui.livedata; +import com.rusefi.config.Field; + public interface VariableValueSource { VariableValueSource VOID = name -> null; - Object getValue(String name); + VariableState getValue(String name); + + class VariableState { + private final Field field; + private final double value; + + public VariableState(Field field, double value) { + this.field = field; + this.value = value; + } + + public Field getField() { + return field; + } + + public double getValue() { + return value; + } + } } diff --git a/java_console/ui/src/test/java/com/rusefi/livedata/LiveDataConventionTest.java b/java_console/ui/src/test/java/com/rusefi/livedata/LiveDataConventionTest.java index 1b7a89c886..ab35be3a03 100644 --- a/java_console/ui/src/test/java/com/rusefi/livedata/LiveDataConventionTest.java +++ b/java_console/ui/src/test/java/com/rusefi/livedata/LiveDataConventionTest.java @@ -29,7 +29,7 @@ public class LiveDataConventionTest { return null; } System.out.println("getValue"); - return true; + return new VariableValueSource.VariableState(null, 0); }; diff --git a/java_console/ui/src/test/java/com/rusefi/livedata/LiveDataParserSandbox.java b/java_console/ui/src/test/java/com/rusefi/livedata/LiveDataParserSandbox.java index de06b093f6..1c1a529c05 100644 --- a/java_console/ui/src/test/java/com/rusefi/livedata/LiveDataParserSandbox.java +++ b/java_console/ui/src/test/java/com/rusefi/livedata/LiveDataParserSandbox.java @@ -1,8 +1,11 @@ package com.rusefi.livedata; +import com.rusefi.config.Field; +import com.rusefi.config.FieldType; import com.rusefi.ui.UIContext; import com.rusefi.ui.livedata.VariableValueSource; import com.rusefi.ui.util.FrameHelper; +import org.jetbrains.annotations.Nullable; import javax.swing.*; import java.util.Map; @@ -13,12 +16,23 @@ import java.util.TreeMap; */ public class LiveDataParserSandbox { public static void main(String[] args) { - Map values = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); - values.put("engineTooSlow", Boolean.TRUE); - values.put("engineTooFast", Boolean.FALSE); + Map values = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + values.put("engineTooSlow", 1.0); + values.put("engineTooFast", 0.0); - VariableValueSource valueSource = values::get; + VariableValueSource valueSource = getVariableValueSource(values); new FrameHelper(JDialog.EXIT_ON_CLOSE).showFrame(new LiveDataParserPanel(new UIContext(), valueSource, "ac_control.cpp").getContent()); } + + @Nullable + public static VariableValueSource getVariableValueSource(Map values) { + VariableValueSource valueSource = name -> { + Double value = values.get(name); + if (value == null) + return null; + return new VariableValueSource.VariableState(new Field(name, 0, FieldType.BIT), value); + }; + return valueSource; + } } diff --git a/java_console/ui/src/test/java/com/rusefi/ui/livedata/LiveDataParserTest.java b/java_console/ui/src/test/java/com/rusefi/ui/livedata/LiveDataParserTest.java index 8c7fc4d5b0..b4ad70db3a 100644 --- a/java_console/ui/src/test/java/com/rusefi/ui/livedata/LiveDataParserTest.java +++ b/java_console/ui/src/test/java/com/rusefi/ui/livedata/LiveDataParserTest.java @@ -2,18 +2,19 @@ package com.rusefi.ui.livedata; import com.rusefi.CodeWalkthrough; import com.rusefi.livedata.LiveDataParserPanel; +import com.rusefi.livedata.LiveDataParserSandbox; import com.rusefi.livedata.LiveDataView; import com.rusefi.livedata.ParseResult; import org.antlr.v4.runtime.tree.ParseTree; import org.antlr.v4.runtime.tree.ParseTreeWalker; import org.junit.Test; -import java.awt.*; import java.io.IOException; import java.net.URISyntaxException; import java.util.Map; import java.util.TreeMap; +import static com.rusefi.CodeWalkthrough.TRUE_CONDITION; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.any; @@ -41,11 +42,11 @@ public class LiveDataParserTest { @Test public void testParsing() { - Map values = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); - values.put("engineTooSlow", Boolean.TRUE); - values.put("engineTooFast", Boolean.FALSE); + Map values = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + values.put("engineTooSlow", 1.0); + values.put("engineTooFast", 1.0); - VariableValueSource valueSource = values::get; + VariableValueSource valueSource = LiveDataParserSandbox.getVariableValueSource(values); String sourceCode = "bool AcState::getAcState() {\n" + "\tauto rpm = Sensor::getOrZero(SensorType::Rpm);\n" + @@ -68,13 +69,12 @@ public class LiveDataParserTest { SourceCodePainter painter = run(valueSource, sourceCode); - verify(painter, times(2)).paintForeground(eq(Color.blue), any()); + verify(painter, times(2)).paintForeground(eq(CodeWalkthrough.CONFIG), any()); - verify(painter).paintBackground(eq(Color.red), any()); - verify(painter).paintBackground(eq(Color.green), any()); + verify(painter).paintBackground(eq(TRUE_CONDITION), any()); verify(painter, times(4)).paintBackground(eq(CodeWalkthrough.ACTIVE_STATEMENT), any()); - verify(painter, times(4)).paintBackground(eq(CodeWalkthrough.PASSIVE_CODE), any()); + verify(painter, times(5)).paintBackground(eq(CodeWalkthrough.PASSIVE_CODE), any()); } private SourceCodePainter run(VariableValueSource valueSource, String sourceCode) {