Skip to content

Commit

Permalink
Improve rendering performance; Dataflow bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
GTO2013 committed Nov 12, 2023
1 parent 3bdbb6c commit 2f19ee7
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
import com.romraider.util.JEPUtil;

public class CalculationAction extends GenericAction {
private String expression;
private final String expression;
private String currentOutputText;
private String currentInputText;
private String currentInputText = "";
private String currentCenterText;

public CalculationAction(String output, String expression) {
Expand All @@ -50,19 +50,27 @@ private void updateCurrentInputText(HashMap<String, Double> variables) {
}
}

private void updateCurrentCenterText(HashMap<String, Double> variables) {
private void updateCurrentCenterText(Map<String, Double> variables) {
currentCenterText = expression;
for (Map.Entry<String, Double> entry : variables.entrySet()) {
String newValue = DEFAULT_FORMATTER.format(entry.getValue());
currentCenterText = currentCenterText.replaceAll("(\\b)" + entry.getKey() + "(\\b)", entry.getKey() + "(" + newValue + ")");
}
synchronized (variables) {
for (Map.Entry<String, Double> entry : variables.entrySet()) {
String newValue = DEFAULT_FORMATTER.format(entry.getValue());
currentCenterText = currentCenterText.replaceAll("(\\b)" + entry.getKey() + "(\\b)",
entry.getKey() + "(" + newValue + ")");
}
}
}

public Double calculate(HashMap<String, Double> variables) {
Double output = JEPUtil.evaluate(expression, variables);
public Double calculate(Map<String, Double> variables) {
Double output = 0.0;
synchronized (variables) {
output = JEPUtil.evaluate(expression, variables);
}
currentOutputText = super.getOutputName() + ": "
+ (Double.isNaN(output) || Double.isInfinite(output) ? "Error" : DEFAULT_FORMATTER.format(output));
updateCurrentInputText(variables);

// Not needed with replaced center value I think
// updateCurrentInputText(variables);
updateCurrentCenterText(variables);
return output;
}
Expand All @@ -71,9 +79,15 @@ public boolean isSetupValid() {
return super.isSetupValid() && !expression.isEmpty();
}

public boolean isCurrentlyValid(HashMap<String, Double> variables) {
Double value = JEPUtil.evaluate(expression, variables);
return !Double.isNaN(value) && !Double.isInfinite(value);
public boolean isCurrentlyValid(Map<String, Double> variables) {
try {
synchronized (variables) {
Double value = JEPUtil.evaluate(expression, variables);
return !Double.isNaN(value) && !Double.isInfinite(value);
}
} catch (NullPointerException e) {
return false;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
import java.util.LinkedList;
import java.util.Map;

import javax.swing.SwingUtilities;

import org.apache.log4j.Logger;
import static java.util.Collections.synchronizedMap;

import com.romraider.maps.Rom;
import com.romraider.swing.DataflowFrame;
Expand All @@ -37,7 +40,7 @@ public class DataflowSimulation {
private String description = "";
private HashSet<String> inputsWithLogParams = new HashSet<String>();
private LinkedList<String> inputs = new LinkedList<String>();
private HashMap<String, Double> variables = new HashMap<String, Double>();
private Map<String, Double> variables = synchronizedMap(new HashMap<String, Double>());
private LinkedList<GenericAction> dataflow = new LinkedList<GenericAction>();
private boolean updateFromLogger = false;
private DataflowFrame frame = null;
Expand Down Expand Up @@ -72,8 +75,7 @@ public void addInput(String name, boolean hasLogParam) {
if (!inputs.contains(name)) {
inputs.add(name);
variables.put(name, 0.0);
if(hasLogParam)
{
if (hasLogParam) {
inputsWithLogParams.add(name);
}
} else {
Expand All @@ -93,7 +95,7 @@ public void addAction(GenericAction action) {
}
}

public int getDoubleOfActions() {
public int getNumberOfActions() {
return dataflow.size();
}

Expand All @@ -104,7 +106,7 @@ public LinkedList<String> getInputs() {
public HashSet<String> getInputsWithLogParam() {
return inputsWithLogParams;
}

public Double getVariableValue(String varName) {
return variables.get(varName);
}
Expand All @@ -122,20 +124,6 @@ public Rom getRom() {
}

public Double simulate(int index) {
// Reset simulation
if (index == 0) {
LinkedList<String> toRemove = new LinkedList<String>();
for (Map.Entry<String, Double> entry : variables.entrySet()) {
// Dont remove inputs
if (!inputs.contains(entry.getKey())) {
toRemove.add(entry.getKey());
}
}
for (String entry : toRemove) {
variables.remove(entry);
}
}

Double result = 0.0;
GenericAction a = dataflow.get(index);
if (a.isCurrentlyValid(variables)) {
Expand All @@ -152,7 +140,12 @@ public void updateVariableFromLogger(String key, double dataValue) {
if (updateFromLogger) {
setVariableValue(key, dataValue);
if (frame != null) {
frame.updateContentPanel();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
frame.updateContentPanel();
}
});
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import com.romraider.maps.Rom;
import com.romraider.maps.Table;
Expand All @@ -46,9 +47,9 @@ public String getOutputName() {

public abstract void init(Rom rom);

public abstract Double calculate(HashMap<String, Double> variables);
public abstract Double calculate(Map<String, Double> variables);

public abstract boolean isCurrentlyValid(HashMap<String, Double> variables);
public abstract boolean isCurrentlyValid(Map<String, Double> variables);

public boolean isSetupValid() {
return !outputName.isEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
package com.romraider.dataflowSimulation;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import com.romraider.maps.Rom;
import com.romraider.maps.Table;
Expand Down Expand Up @@ -49,7 +49,7 @@ public boolean isSetupValid() {
return super.isSetupValid() && !refTable.isEmpty() && (!input_x.isEmpty() || !input_y.isEmpty());
}

public boolean isCurrentlyValid(HashMap<String, Double> variables) {
public boolean isCurrentlyValid(Map<String, Double> variables) {
return (input_x.isEmpty() ? true : variables.containsKey(input_x))
&& (input_y.isEmpty() ? true : variables.containsKey(input_y));
}
Expand All @@ -70,7 +70,7 @@ private String updateInputText(Double inputXValue, Double inputYValue) {
return currentInputText;
}

public Double calculate(HashMap<String, Double> variables) {
public Double calculate(Map<String, Double> variables) {
if (resolvedTable != null) {
Double inputXValue = variables.get(input_x);
Double inputYValue = variables.get(input_y);
Expand All @@ -81,7 +81,7 @@ public Double calculate(HashMap<String, Double> variables) {
if (!input_y.isEmpty() && inputYValue != null)
currentInputs.add(inputYValue);

Double output = resolvedTable.queryTable((Double)inputXValue, (Double)inputYValue);
Double output = resolvedTable.queryTable((Double) inputXValue, (Double) inputYValue);
currentInputText = updateInputText(inputXValue, inputYValue);
currentOutputText = super.getOutputName() + ": " + TABLE_FORMATTER.format(output);
return output;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/romraider/maps/DataCellView.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,15 @@ public void drawCell() {
}

tableView.updatePresetPanel();
this.invalidate();
//this.invalidate();
setFont(getSettings().getTableFont());
setText(getCellText());
setToolTipText(getCellToolTip());
setBackground(getCellBackgroundColor());
setForeground(getCellTextColor());
setBorder(getCellBorder());
this.validate();
super.repaint();
//this.validate();
//super.repaint();
}

private Color getCellBackgroundColor() {
Expand Down
16 changes: 2 additions & 14 deletions src/main/java/com/romraider/maps/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -442,23 +442,11 @@ public double getMinBin() {
}

public double getMaxReal() {
double minReal = JEPUtil.evaluate(getCurrentScale().getExpression(), getMinBin());
double maxReal = JEPUtil.evaluate(getCurrentScale().getExpression(), getMaxBin());
if(minReal > maxReal) {
return minReal;
} else {
return maxReal;
}
return JEPUtil.evaluate(getCurrentScale().getExpression(), getMaxBin());
}

public double getMinReal() {
double minReal = JEPUtil.evaluate(getCurrentScale().getExpression(), getMinBin());
double maxReal = JEPUtil.evaluate(getCurrentScale().getExpression(), getMaxBin());
if(minReal < maxReal) {
return minReal;
} else {
return maxReal;
}
return JEPUtil.evaluate(getCurrentScale().getExpression(), getMinBin());
}

public void setMaxBin(double maxBin) {
Expand Down
39 changes: 24 additions & 15 deletions src/main/java/com/romraider/swing/DataflowFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import java.util.LinkedList;
import java.util.Map;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
Expand Down Expand Up @@ -77,18 +79,20 @@ private void initUserInterface() {
// setup main panel
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel contentPanel = buildContentPanel();
mainPanel.add(new JScrollPane(contentPanel), BorderLayout.SOUTH);
mainPanel.add(buildInputPanel(), BorderLayout.CENTER);
if (!sim.getDescription().isEmpty())
mainPanel.add(buildDescriptionPanel(), BorderLayout.NORTH);
mainPanel.add(new JScrollPane(contentPanel), BorderLayout.CENTER);
mainPanel.add(buildInputPanel(), BorderLayout.NORTH);

// Causes scrolling issues...
// if (!sim.getDescription().isEmpty())
// mainPanel.add(buildDescriptionPanel(), BorderLayout.NORTH);
updateContentPanel();

// add to container
getContentPane().add(mainPanel);
}

private JPanel buildDescriptionPanel() {
JPanel descPanel = new JPanel(new GridLayout(sim.getDoubleOfActions(), 3));
JPanel descPanel = new JPanel(new GridLayout(sim.getNumberOfActions(), 3));
descPanel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED), "Description"));
JLabel desc = new JLabel(sim.getDescription());
descPanel.add(desc);
Expand All @@ -97,45 +101,50 @@ private JPanel buildDescriptionPanel() {
}

private JPanel buildContentPanel() {
JPanel contentPanel = new JPanel(new GridLayout(sim.getDoubleOfActions(), 3));
JPanel contentPanel = new JPanel();
contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
contentPanel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED), "Simulation"));

for (int i = 0; i < sim.getDoubleOfActions(); i++) {
for (int i = 0; i < sim.getNumberOfActions(); i++) {
GenericAction a = sim.getAction(i);
a.init(sim.getRom());

JPanel line = new JPanel(new GridLayout(1, 3));

JLabel input = new JLabel("");
input.setHorizontalAlignment(JLabel.CENTER);
input.setFont(boldFont);
inputs.add(input);
contentPanel.add(input);
line.add(input);

if (a.getType() == GenericActionType.CALCULATION) {
JLabel center = new JLabel("");
center.setHorizontalAlignment(JLabel.CENTER);
center.setFont(boldFont);
centerDisplay.add(center);
contentPanel.add(center);
line.add(center);
} else if (a.getType() == GenericActionType.TABLE) {
TableView v = ECUEditor.getTableViewForTable(a.getTable());
if (v != null) {
centerDisplay.add(v);
contentPanel.add(v);
line.add(v);
v.populateTableVisual();
} else {
JLabel error = new JLabel("Failed to find table!");
error.setFont(boldFont);
error.setHorizontalAlignment(JLabel.CENTER);
centerDisplay.add(error);
contentPanel.add(error);
line.add(error);
}
}

JLabel output = new JLabel();
output.setHorizontalAlignment(JLabel.CENTER);
output.setFont(boldFont);
outputs.add(output);
contentPanel.add(output);
line.add(output);
contentPanel.add(line);
contentPanel.add(Box.createVerticalStrut(20));
}

return contentPanel;
Expand All @@ -157,7 +166,7 @@ public void run() {
});
}

for (int i = 0; i < sim.getDoubleOfActions(); i++) {
for (int i = 0; i < sim.getNumberOfActions(); i++) {
GenericAction a = sim.getAction(i);
sim.simulate(i);

Expand All @@ -169,7 +178,7 @@ public void run() {

if (centerText != null) {
// Add linebreak if needed
((JLabel) centerDisplay.get(i)).setText("<html>" + centerText.replaceAll("(.{50})", "$1<br>"));
((JLabel) centerDisplay.get(i)).setText("<html>" + centerText.replaceAll("(.{80})", "$1<br>"));
} else if (table != null) {
TableView v = ((TableView) centerDisplay.get(i));

Expand All @@ -196,7 +205,7 @@ private Component buildInputPanel() {
JPanel fieldPanel = new JPanel(new FlowLayout());

enableLogButton = new JCheckBox("Update from Logger");
enableLogButton.setEnabled(!sim.getInputsWithLogParam().isEmpty());
enableLogButton.setEnabled(!sim.getInputsWithLogParam().isEmpty());
fieldPanel.add(enableLogButton);
enableLogButton.addActionListener(new ActionListener() {
@Override
Expand Down
Loading

0 comments on commit 2f19ee7

Please sign in to comment.