Skip to content

Commit 2f19ee7

Browse files
committed
Improve rendering performance; Dataflow bugfixes
1 parent 3bdbb6c commit 2f19ee7

File tree

8 files changed

+125
-118
lines changed

8 files changed

+125
-118
lines changed

src/main/java/com/romraider/dataflowSimulation/CalculationAction.java

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
import com.romraider.util.JEPUtil;
2828

2929
public class CalculationAction extends GenericAction {
30-
private String expression;
30+
private final String expression;
3131
private String currentOutputText;
32-
private String currentInputText;
32+
private String currentInputText = "";
3333
private String currentCenterText;
3434

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

53-
private void updateCurrentCenterText(HashMap<String, Double> variables) {
53+
private void updateCurrentCenterText(Map<String, Double> variables) {
5454
currentCenterText = expression;
55-
for (Map.Entry<String, Double> entry : variables.entrySet()) {
56-
String newValue = DEFAULT_FORMATTER.format(entry.getValue());
57-
currentCenterText = currentCenterText.replaceAll("(\\b)" + entry.getKey() + "(\\b)", entry.getKey() + "(" + newValue + ")");
58-
}
55+
synchronized (variables) {
56+
for (Map.Entry<String, Double> entry : variables.entrySet()) {
57+
String newValue = DEFAULT_FORMATTER.format(entry.getValue());
58+
currentCenterText = currentCenterText.replaceAll("(\\b)" + entry.getKey() + "(\\b)",
59+
entry.getKey() + "(" + newValue + ")");
60+
}
61+
}
5962
}
6063

61-
public Double calculate(HashMap<String, Double> variables) {
62-
Double output = JEPUtil.evaluate(expression, variables);
64+
public Double calculate(Map<String, Double> variables) {
65+
Double output = 0.0;
66+
synchronized (variables) {
67+
output = JEPUtil.evaluate(expression, variables);
68+
}
6369
currentOutputText = super.getOutputName() + ": "
6470
+ (Double.isNaN(output) || Double.isInfinite(output) ? "Error" : DEFAULT_FORMATTER.format(output));
65-
updateCurrentInputText(variables);
71+
72+
// Not needed with replaced center value I think
73+
// updateCurrentInputText(variables);
6674
updateCurrentCenterText(variables);
6775
return output;
6876
}
@@ -71,9 +79,15 @@ public boolean isSetupValid() {
7179
return super.isSetupValid() && !expression.isEmpty();
7280
}
7381

74-
public boolean isCurrentlyValid(HashMap<String, Double> variables) {
75-
Double value = JEPUtil.evaluate(expression, variables);
76-
return !Double.isNaN(value) && !Double.isInfinite(value);
82+
public boolean isCurrentlyValid(Map<String, Double> variables) {
83+
try {
84+
synchronized (variables) {
85+
Double value = JEPUtil.evaluate(expression, variables);
86+
return !Double.isNaN(value) && !Double.isInfinite(value);
87+
}
88+
} catch (NullPointerException e) {
89+
return false;
90+
}
7791
}
7892

7993
@Override

src/main/java/com/romraider/dataflowSimulation/DataflowSimulation.java

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
import java.util.LinkedList;
2525
import java.util.Map;
2626

27+
import javax.swing.SwingUtilities;
28+
2729
import org.apache.log4j.Logger;
30+
import static java.util.Collections.synchronizedMap;
2831

2932
import com.romraider.maps.Rom;
3033
import com.romraider.swing.DataflowFrame;
@@ -37,7 +40,7 @@ public class DataflowSimulation {
3740
private String description = "";
3841
private HashSet<String> inputsWithLogParams = new HashSet<String>();
3942
private LinkedList<String> inputs = new LinkedList<String>();
40-
private HashMap<String, Double> variables = new HashMap<String, Double>();
43+
private Map<String, Double> variables = synchronizedMap(new HashMap<String, Double>());
4144
private LinkedList<GenericAction> dataflow = new LinkedList<GenericAction>();
4245
private boolean updateFromLogger = false;
4346
private DataflowFrame frame = null;
@@ -72,8 +75,7 @@ public void addInput(String name, boolean hasLogParam) {
7275
if (!inputs.contains(name)) {
7376
inputs.add(name);
7477
variables.put(name, 0.0);
75-
if(hasLogParam)
76-
{
78+
if (hasLogParam) {
7779
inputsWithLogParams.add(name);
7880
}
7981
} else {
@@ -93,7 +95,7 @@ public void addAction(GenericAction action) {
9395
}
9496
}
9597

96-
public int getDoubleOfActions() {
98+
public int getNumberOfActions() {
9799
return dataflow.size();
98100
}
99101

@@ -104,7 +106,7 @@ public LinkedList<String> getInputs() {
104106
public HashSet<String> getInputsWithLogParam() {
105107
return inputsWithLogParams;
106108
}
107-
109+
108110
public Double getVariableValue(String varName) {
109111
return variables.get(varName);
110112
}
@@ -122,20 +124,6 @@ public Rom getRom() {
122124
}
123125

124126
public Double simulate(int index) {
125-
// Reset simulation
126-
if (index == 0) {
127-
LinkedList<String> toRemove = new LinkedList<String>();
128-
for (Map.Entry<String, Double> entry : variables.entrySet()) {
129-
// Dont remove inputs
130-
if (!inputs.contains(entry.getKey())) {
131-
toRemove.add(entry.getKey());
132-
}
133-
}
134-
for (String entry : toRemove) {
135-
variables.remove(entry);
136-
}
137-
}
138-
139127
Double result = 0.0;
140128
GenericAction a = dataflow.get(index);
141129
if (a.isCurrentlyValid(variables)) {
@@ -152,7 +140,12 @@ public void updateVariableFromLogger(String key, double dataValue) {
152140
if (updateFromLogger) {
153141
setVariableValue(key, dataValue);
154142
if (frame != null) {
155-
frame.updateContentPanel();
143+
SwingUtilities.invokeLater(new Runnable() {
144+
@Override
145+
public void run() {
146+
frame.updateContentPanel();
147+
}
148+
});
156149
}
157150
}
158151
}

src/main/java/com/romraider/dataflowSimulation/GenericAction.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.text.DecimalFormat;
2323
import java.util.HashMap;
2424
import java.util.LinkedList;
25+
import java.util.Map;
2526

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

4748
public abstract void init(Rom rom);
4849

49-
public abstract Double calculate(HashMap<String, Double> variables);
50+
public abstract Double calculate(Map<String, Double> variables);
5051

51-
public abstract boolean isCurrentlyValid(HashMap<String, Double> variables);
52+
public abstract boolean isCurrentlyValid(Map<String, Double> variables);
5253

5354
public boolean isSetupValid() {
5455
return !outputName.isEmpty();

src/main/java/com/romraider/dataflowSimulation/TableAction.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
package com.romraider.dataflowSimulation;
2121

2222
import java.text.DecimalFormat;
23-
import java.util.HashMap;
2423
import java.util.LinkedList;
24+
import java.util.Map;
2525

2626
import com.romraider.maps.Rom;
2727
import com.romraider.maps.Table;
@@ -49,7 +49,7 @@ public boolean isSetupValid() {
4949
return super.isSetupValid() && !refTable.isEmpty() && (!input_x.isEmpty() || !input_y.isEmpty());
5050
}
5151

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

73-
public Double calculate(HashMap<String, Double> variables) {
73+
public Double calculate(Map<String, Double> variables) {
7474
if (resolvedTable != null) {
7575
Double inputXValue = variables.get(input_x);
7676
Double inputYValue = variables.get(input_y);
@@ -81,7 +81,7 @@ public Double calculate(HashMap<String, Double> variables) {
8181
if (!input_y.isEmpty() && inputYValue != null)
8282
currentInputs.add(inputYValue);
8383

84-
Double output = resolvedTable.queryTable((Double)inputXValue, (Double)inputYValue);
84+
Double output = resolvedTable.queryTable((Double) inputXValue, (Double) inputYValue);
8585
currentInputText = updateInputText(inputXValue, inputYValue);
8686
currentOutputText = super.getOutputName() + ": " + TABLE_FORMATTER.format(output);
8787
return output;

src/main/java/com/romraider/maps/DataCellView.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,15 @@ public void drawCell() {
110110
}
111111

112112
tableView.updatePresetPanel();
113-
this.invalidate();
113+
//this.invalidate();
114114
setFont(getSettings().getTableFont());
115115
setText(getCellText());
116116
setToolTipText(getCellToolTip());
117117
setBackground(getCellBackgroundColor());
118118
setForeground(getCellTextColor());
119119
setBorder(getCellBorder());
120-
this.validate();
121-
super.repaint();
120+
//this.validate();
121+
//super.repaint();
122122
}
123123

124124
private Color getCellBackgroundColor() {

src/main/java/com/romraider/maps/Table.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -442,23 +442,11 @@ public double getMinBin() {
442442
}
443443

444444
public double getMaxReal() {
445-
double minReal = JEPUtil.evaluate(getCurrentScale().getExpression(), getMinBin());
446-
double maxReal = JEPUtil.evaluate(getCurrentScale().getExpression(), getMaxBin());
447-
if(minReal > maxReal) {
448-
return minReal;
449-
} else {
450-
return maxReal;
451-
}
445+
return JEPUtil.evaluate(getCurrentScale().getExpression(), getMaxBin());
452446
}
453447

454448
public double getMinReal() {
455-
double minReal = JEPUtil.evaluate(getCurrentScale().getExpression(), getMinBin());
456-
double maxReal = JEPUtil.evaluate(getCurrentScale().getExpression(), getMaxBin());
457-
if(minReal < maxReal) {
458-
return minReal;
459-
} else {
460-
return maxReal;
461-
}
449+
return JEPUtil.evaluate(getCurrentScale().getExpression(), getMinBin());
462450
}
463451

464452
public void setMaxBin(double maxBin) {

src/main/java/com/romraider/swing/DataflowFrame.java

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import java.util.LinkedList;
3333
import java.util.Map;
3434

35+
import javax.swing.Box;
36+
import javax.swing.BoxLayout;
3537
import javax.swing.ImageIcon;
3638
import javax.swing.JCheckBox;
3739
import javax.swing.JLabel;
@@ -77,18 +79,20 @@ private void initUserInterface() {
7779
// setup main panel
7880
JPanel mainPanel = new JPanel(new BorderLayout());
7981
JPanel contentPanel = buildContentPanel();
80-
mainPanel.add(new JScrollPane(contentPanel), BorderLayout.SOUTH);
81-
mainPanel.add(buildInputPanel(), BorderLayout.CENTER);
82-
if (!sim.getDescription().isEmpty())
83-
mainPanel.add(buildDescriptionPanel(), BorderLayout.NORTH);
82+
mainPanel.add(new JScrollPane(contentPanel), BorderLayout.CENTER);
83+
mainPanel.add(buildInputPanel(), BorderLayout.NORTH);
84+
85+
// Causes scrolling issues...
86+
// if (!sim.getDescription().isEmpty())
87+
// mainPanel.add(buildDescriptionPanel(), BorderLayout.NORTH);
8488
updateContentPanel();
8589

8690
// add to container
8791
getContentPane().add(mainPanel);
8892
}
8993

9094
private JPanel buildDescriptionPanel() {
91-
JPanel descPanel = new JPanel(new GridLayout(sim.getDoubleOfActions(), 3));
95+
JPanel descPanel = new JPanel(new GridLayout(sim.getNumberOfActions(), 3));
9296
descPanel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED), "Description"));
9397
JLabel desc = new JLabel(sim.getDescription());
9498
descPanel.add(desc);
@@ -97,45 +101,50 @@ private JPanel buildDescriptionPanel() {
97101
}
98102

99103
private JPanel buildContentPanel() {
100-
JPanel contentPanel = new JPanel(new GridLayout(sim.getDoubleOfActions(), 3));
104+
JPanel contentPanel = new JPanel();
105+
contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
101106
contentPanel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED), "Simulation"));
102107

103-
for (int i = 0; i < sim.getDoubleOfActions(); i++) {
108+
for (int i = 0; i < sim.getNumberOfActions(); i++) {
104109
GenericAction a = sim.getAction(i);
105110
a.init(sim.getRom());
106111

112+
JPanel line = new JPanel(new GridLayout(1, 3));
113+
107114
JLabel input = new JLabel("");
108115
input.setHorizontalAlignment(JLabel.CENTER);
109116
input.setFont(boldFont);
110117
inputs.add(input);
111-
contentPanel.add(input);
118+
line.add(input);
112119

113120
if (a.getType() == GenericActionType.CALCULATION) {
114121
JLabel center = new JLabel("");
115122
center.setHorizontalAlignment(JLabel.CENTER);
116123
center.setFont(boldFont);
117124
centerDisplay.add(center);
118-
contentPanel.add(center);
125+
line.add(center);
119126
} else if (a.getType() == GenericActionType.TABLE) {
120127
TableView v = ECUEditor.getTableViewForTable(a.getTable());
121128
if (v != null) {
122129
centerDisplay.add(v);
123-
contentPanel.add(v);
130+
line.add(v);
124131
v.populateTableVisual();
125132
} else {
126133
JLabel error = new JLabel("Failed to find table!");
127134
error.setFont(boldFont);
128135
error.setHorizontalAlignment(JLabel.CENTER);
129136
centerDisplay.add(error);
130-
contentPanel.add(error);
137+
line.add(error);
131138
}
132139
}
133140

134141
JLabel output = new JLabel();
135142
output.setHorizontalAlignment(JLabel.CENTER);
136143
output.setFont(boldFont);
137144
outputs.add(output);
138-
contentPanel.add(output);
145+
line.add(output);
146+
contentPanel.add(line);
147+
contentPanel.add(Box.createVerticalStrut(20));
139148
}
140149

141150
return contentPanel;
@@ -157,7 +166,7 @@ public void run() {
157166
});
158167
}
159168

160-
for (int i = 0; i < sim.getDoubleOfActions(); i++) {
169+
for (int i = 0; i < sim.getNumberOfActions(); i++) {
161170
GenericAction a = sim.getAction(i);
162171
sim.simulate(i);
163172

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

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

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

198207
enableLogButton = new JCheckBox("Update from Logger");
199-
enableLogButton.setEnabled(!sim.getInputsWithLogParam().isEmpty());
208+
enableLogButton.setEnabled(!sim.getInputsWithLogParam().isEmpty());
200209
fieldPanel.add(enableLogButton);
201210
enableLogButton.addActionListener(new ActionListener() {
202211
@Override

0 commit comments

Comments
 (0)