Skip to content
This repository was archived by the owner on Sep 19, 2023. It is now read-only.

Commit 8b9e455

Browse files
committed
hw4 code finished
1 parent f4c8702 commit 8b9e455

File tree

11 files changed

+2899665
-81
lines changed

11 files changed

+2899665
-81
lines changed

EpidemicSimulation/src/com/luox6/epidemic/gui/GUIController.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public void resetButtonPressed() {
5656
Dialog.genericWarningDialog(guiViewer, new Exception("Please set graph before reset graph!"));
5757
} else {
5858
collector.reset();
59+
updateGraphView();
5960
}
6061
}
6162

@@ -68,6 +69,7 @@ public void openFile() {
6869
if (fc.showOpenDialog(guiViewer) == JFileChooser.APPROVE_OPTION) {
6970
File selectedFile = fc.getSelectedFile();
7071
Graph g = Parser.loadGraph(new FileReader(selectedFile));
72+
// Apply new Graph
7173
setNewGraph(g);
7274
Dialog.genericSuccessDialog(guiViewer, "Graph has loaded successfully!");
7375
}
@@ -96,7 +98,7 @@ public void updateGraphView() {
9698
guiViewer.statusPanel.updateSimulationStatus(collector.getStatus());
9799

98100
guiViewer.statusPanel.updateSimulation(
99-
0,
101+
collector.getDataCollection().getTick(),
100102
collector.getDataCollection().getCurrentSusceptibleCount(),
101103
collector.getDataCollection().getCurrentInfectedCount(),
102104
collector.getDataCollection().getCurrentDeadCount(),
@@ -235,7 +237,7 @@ public void setNumThread(String text) {
235237
throw new Exception("Number of thread should be at least 1");
236238
}
237239

238-
UserSetting.setValueK(i);
240+
UserSetting.setThread(i);
239241
} catch (NumberFormatException e) {
240242
Dialog.numberParseDialog(guiViewer, e);
241243
} catch (Exception e) {
@@ -252,7 +254,7 @@ public void setNumStep(String text) {
252254
throw new Exception("Number of step should be at least 0");
253255
}
254256

255-
UserSetting.setValueK(i);
257+
UserSetting.setStep(i);
256258
} catch (NumberFormatException e) {
257259
Dialog.numberParseDialog(guiViewer, e);
258260
} catch (Exception e) {
@@ -283,8 +285,12 @@ public void pauseButtonPressed() {
283285
public void infectButtonPressed(Graph.INITIAL_INFECTED_MODE m) {
284286
if (collector.isInitInfected()) {
285287
Dialog.genericWarningDialog(guiViewer, new Exception("Graph has already init infected: reset to re-infected"));
288+
} else if (collector.getStatus() == StatusPanel.SIMULATION_STATUS.AWAIT_DATA){
289+
Dialog.genericWarningDialog(guiViewer, new Exception("Please load graph first!"));
286290
} else {
287291
collector.setInitInfected(m);
292+
updateGraphView();
293+
Dialog.genericSuccessDialog(guiViewer, "Infected set!");
288294
}
289295
}
290296
}

EpidemicSimulation/src/com/luox6/epidemic/gui/components/ConfigurationPanel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,6 @@ private void setActions() {
126126
valueT.addActionListener(e -> controller.setValueT(valueT.getText()));
127127
valueLambda.addActionListener(e -> controller.setValueLambda(valueLambda.getText()));
128128
numThread.addActionListener(e -> controller.setNumThread(numThread.getText()));
129-
numStep.addActionListener(e -> controller.setNumStep(numThread.getText()));
129+
numStep.addActionListener(e -> controller.setNumStep(numStep.getText()));
130130
}
131131
}

EpidemicSimulation/src/com/luox6/epidemic/gui/components/GraphPanel.java

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.knowm.xchart.XChartPanel;
77
import org.knowm.xchart.XYChart;
88
import org.knowm.xchart.XYChartBuilder;
9+
import org.knowm.xchart.style.Styler;
910

1011
import javax.swing.*;
1112
import java.awt.*;
@@ -14,26 +15,40 @@
1415
public class GraphPanel extends JPanel {
1516
private GUIController guiController;
1617
private XYChart chart = new XYChartBuilder().title("Epidemic Simulation").xAxisTitle("Ticks").yAxisTitle("Number").build();
17-
JPanel chartPanel = new XChartPanel<>(chart);
18+
JPanel chartPanel;
1819

1920
public GraphPanel(GUIController guiController) {
2021
this.guiController = guiController;
21-
setLayout(new GridLayout(1, 1));
22-
add(chartPanel);
2322

24-
double [] initialX = new double[] { 1 ,2, 3, 4, 5};
25-
double [] initialY = new double[] { 0, 0, 0, 0, 0};
26-
chart.addSeries("Susceptible Count", initialX, initialY);
27-
chart.addSeries("Infected Count", initialX, initialY);
28-
chart.addSeries("Dead Count", initialX, initialY);
29-
chart.addSeries("Recovered Count", initialX, initialY);
23+
chart.getStyler().setLegendPosition(Styler.LegendPosition.OutsideS);
24+
chart.getStyler().setLegendLayout(Styler.LegendLayout.Horizontal);
3025

3126
chart.getStyler().setSeriesColors(new Color[] {
3227
UserSetting.getSusceptibleColor(),
3328
UserSetting.getInfectedColor(),
3429
UserSetting.getDeadColor(),
3530
UserSetting.getRecoveredColor()
3631
});
32+
33+
chart.getStyler().setZoomEnabled(true);
34+
chart.getStyler().setToolTipsEnabled(true);
35+
// chart.getStyler().setCursorEnabled(true);
36+
chart.getStyler().setZoomResetByDoubleClick(true);
37+
chart.getStyler().setZoomResetByButton(true);
38+
39+
// Placeholder data
40+
double [] initialX = new double[] { 1 ,2, 3, 4, 5 };
41+
double [] initialY = new double[] { 0, 0, 0, 0, 0 };
42+
chart.addSeries("Susceptible Count", initialX, initialY);
43+
chart.addSeries("Infected Count", initialX, initialY);
44+
chart.addSeries("Dead Count", initialX, initialY);
45+
chart.addSeries("Recovered Count", initialX, initialY);
46+
47+
// Chart need to be fully initialized then put
48+
// into panel, or there may be paint conflict
49+
chartPanel = new XChartPanel<>(chart);
50+
setLayout(new GridLayout(1, 1));
51+
add(chartPanel);
3752
}
3853

3954
public void updateData(Collector collector) {
@@ -43,5 +58,7 @@ public void updateData(Collector collector) {
4358
chart.updateXYSeries("Infected Count", data.get(0), data.get(2), null);
4459
chart.updateXYSeries("Dead Count", data.get(0), data.get(3), null);
4560
chart.updateXYSeries("Recovered Count", data.get(0), data.get(4), null);
61+
chartPanel.revalidate();
62+
chartPanel.repaint();
4663
}
4764
}

EpidemicSimulation/src/com/luox6/epidemic/gui/models/Collector.java

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,55 +5,83 @@
55
import com.luox6.epidemic.model.DataCollection;
66
import com.luox6.epidemic.model.Graph;
77

8+
import java.util.concurrent.ExecutionException;
9+
import java.util.concurrent.atomic.AtomicReference;
10+
811
public class Collector {
912
private Graph graph;
1013
private DataCollection dataCollection;
1114
private Thread simulate;
12-
private StatusPanel.SIMULATION_STATUS status = StatusPanel.SIMULATION_STATUS.AWAIT_DATA;
15+
private AtomicReference<StatusPanel.SIMULATION_STATUS> status;
1316

1417
private GUIController guiController;
1518

1619
public Collector(GUIController g) {
1720
guiController = g;
21+
status = new AtomicReference<>(StatusPanel.SIMULATION_STATUS.AWAIT_DATA);
22+
simulate = getNewThread();
23+
simulate.start();
1824
}
1925

2026
public DataCollection getDataCollection() {
2127
return dataCollection;
2228
}
2329

2430
public void start() {
25-
simulate.start();
26-
status = StatusPanel.SIMULATION_STATUS.IN_PROGRESS;
31+
status.compareAndSet(StatusPanel.SIMULATION_STATUS.READY, StatusPanel.SIMULATION_STATUS.IN_PROGRESS);
32+
status.compareAndSet(StatusPanel.SIMULATION_STATUS.PAUSED, StatusPanel.SIMULATION_STATUS.IN_PROGRESS);
2733
}
2834

2935
public void pause() {
30-
simulate.interrupt();
31-
status = StatusPanel.SIMULATION_STATUS.PAUSED;
32-
simulate = getNewThread();
36+
status.compareAndSet(StatusPanel.SIMULATION_STATUS.IN_PROGRESS, StatusPanel.SIMULATION_STATUS.PAUSED);
3337
}
3438

3539
public StatusPanel.SIMULATION_STATUS getStatus() {
36-
return status;
40+
return status.get();
3741
}
3842

3943
public void reset() {
4044
Graph cpg = new Graph(graph);
41-
status = StatusPanel.SIMULATION_STATUS.READY;
45+
// Init graph with parameters
46+
cpg.setD(UserSetting.getValueD());
47+
cpg.setK(UserSetting.getValueK());
48+
cpg.setN(UserSetting.getValueN());
49+
cpg.setLambda(UserSetting.getLambda());
50+
cpg.setS(UserSetting.getValueS());
51+
cpg.setT(UserSetting.getValueT());
52+
cpg.setNumThread(UserSetting.getThread());
4253
dataCollection = new DataCollection(cpg);
43-
simulate = getNewThread();
54+
status.set(StatusPanel.SIMULATION_STATUS.READY);
4455
}
4556

4657
private Thread getNewThread() {
4758
return new Thread(() -> {
48-
while (!Thread.currentThread().isInterrupted()) {
49-
if (dataCollection.getTick() < UserSetting.getStep()) {
50-
// Do one per time, until interrupted or target reach
51-
// to ensure state integrity
52-
dataCollection.simulate();
59+
while (true) {
60+
if (status.get() == StatusPanel.SIMULATION_STATUS.IN_PROGRESS) {
61+
if (dataCollection.getTick() < UserSetting.getStep()) {
62+
// Do one per time, until interrupted or target reach
63+
// to ensure state integrity
64+
try {
65+
dataCollection.simulate();
66+
} catch (ExecutionException | InterruptedException e) {
67+
e.printStackTrace();
68+
}
69+
} else {
70+
status.set(StatusPanel.SIMULATION_STATUS.FINISHED);
71+
}
5372
guiController.updateGraphView();
5473
} else {
55-
status = StatusPanel.SIMULATION_STATUS.FINISHED;
56-
break;
74+
/**
75+
* @TODO
76+
* It is actually better to submit to executor instead of wait,
77+
* but it is working anyway, so let's do it.
78+
* https://stackoverflow.com/a/19025596/13843585
79+
*/
80+
try {
81+
Thread.sleep(500);
82+
} catch (InterruptedException e) {
83+
e.printStackTrace();
84+
}
5785
}
5886
}
5987
});
@@ -66,13 +94,13 @@ public void setGraph(Graph graph) {
6694
}
6795

6896
public boolean isInitInfected() {
69-
return getDataCollection().getGraph().getInfected();
97+
if (getStatus() != StatusPanel.SIMULATION_STATUS.AWAIT_DATA)
98+
return getDataCollection().getInfected();
99+
else
100+
return false;
70101
}
71102

72103
public void setInitInfected(Graph.INITIAL_INFECTED_MODE m) {
73-
if (isInitInfected()) {
74-
throw new RuntimeException("Graph has infected!");
75-
}
76-
getDataCollection().getGraph().setInitialInfected(m);
104+
getDataCollection().setInitialInfected(m);
77105
}
78106
}

EpidemicSimulation/src/com/luox6/epidemic/model/DataCollection.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.ArrayList;
44
import java.util.List;
5+
import java.util.concurrent.ExecutionException;
56

67
public class DataCollection {
78
private List<Integer> infectedCount;
@@ -13,6 +14,7 @@ public class DataCollection {
1314

1415
public DataCollection(Graph g) {
1516
graph = g;
17+
graph.init();
1618
infectedCount = new ArrayList<>();
1719
recoveredCount = new ArrayList<>();
1820
deadCount = new ArrayList<>();
@@ -22,7 +24,7 @@ public DataCollection(Graph g) {
2224
syncGraphData();
2325
}
2426

25-
public void simulate() {
27+
public void simulate() throws ExecutionException, InterruptedException {
2628
graph.simulate();
2729
syncGraphData();
2830
}
@@ -81,10 +83,6 @@ public int getCurrentDeadCount() {
8183
return deadCount.get(deadCount.size() - 1);
8284
}
8385

84-
public Graph getGraph() {
85-
return graph;
86-
}
87-
8886
public int getTick() {
8987
return graph.getTick();
9088
}
@@ -103,4 +101,18 @@ public int[] getData(int index) {
103101

104102
return null;
105103
}
104+
105+
/** Proxy of container **/
106+
public void setInitialInfected(Graph.INITIAL_INFECTED_MODE m) {
107+
// When setting initial Infected, we need to update the infected count as well
108+
graph.setInitialInfected(m);
109+
infectedCount.remove(0);
110+
susceptibleCount.remove(0);
111+
infectedCount.add(0, graph.getInfectedCount());
112+
susceptibleCount.add(0, graph.getSusceptibleCount());
113+
}
114+
115+
public boolean getInfected() {
116+
return graph.getInfected();
117+
}
106118
}

0 commit comments

Comments
 (0)