-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVisualise.kt
More file actions
258 lines (226 loc) · 7.36 KB
/
Visualise.kt
File metadata and controls
258 lines (226 loc) · 7.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package consensus
import java.awt.*
import java.awt.event.*
import javax.swing.*
import javax.swing.event.*
import javax.swing.plaf.*
import kotlin.math.*
fun main() = SwingUtilities.invokeAndWait {
val model = Model(4)
val control = ControlPane(model)
JFrame("Visualisation").apply {
add(control, BorderLayout.NORTH)
contentPane.add(VScrollPane(model), BorderLayout.CENTER)
defaultCloseOperation = JFrame.EXIT_ON_CLOSE
pack()
isLocationByPlatform = true
isVisible = true
}
control.load()
control.installActionListeners()
}
val IMPLS = listOf(
"ProcessNaiveConsensus"
)
val BG_COLOR: Color = Color.WHITE
val FG_COLOR: Color = Color.BLACK
val HOVER_COLOR: Color = Color.GRAY
class VScrollPane(model: Model) :
JScrollPane(VComponent(model), VERTICAL_SCROLLBAR_ALWAYS, HORIZONTAL_SCROLLBAR_NEVER),
ChangeListener
{
private val vm = verticalScrollBar.model
private var wasAtBottom = true
private var oldMax = vm.maximum
init {
vm.addChangeListener(this)
}
override fun stateChanged(e: ChangeEvent?) {
if (wasAtBottom && vm.maximum != oldMax) {
SwingUtilities.invokeLater { moveToBottom() }
}
wasAtBottom = isBottom()
}
private fun isBottom(): Boolean = vm.value + vm.extent >= vm.maximum
private fun moveToBottom() {
vm.value = vm.maximum - vm.extent
wasAtBottom = true
oldMax = vm.maximum
}
}
class VComponent(model: Model) : JComponent() {
init {
val ui = VComponentUI(model, this)
addMouseMotionListener(ui)
addMouseListener(ui)
model.updateListener = {
revalidate()
repaint()
}
this.ui = ui
background = BG_COLOR
foreground = FG_COLOR
isOpaque = true
}
}
const val LEFT_PAD = 50
const val RIGHT_PAD = 250
const val X_SPACE = 250
const val Y_TOP = 50
const val Y_MINIMUM = 800
const val TEXT_PAD = 10
const val R = 10
const val R1 = 7
fun xProcess(processId: Int) = LEFT_PAD + X_SPACE * (processId - 1)
fun yTime(time: Int) = Y_TOP + time
fun processByX(x: Int) = ((x - LEFT_PAD) / X_SPACE.toDouble()).roundToInt() + 1
fun timeByY(y: Int) = y - Y_TOP
class VComponentUI(
private val model: Model,
private val c: JComponent
) : ComponentUI(), MouseMotionListener, MouseListener {
private var hover: Action? = null
private var dragT0 = 0
private var dragStart = Point(0, 0)
override fun paint(g: Graphics, c: JComponent) {
g as Graphics2D
g.color = FG_COLOR
for ((i, process) in model.processes) {
val x = xProcess(i)
val y = yTime(0)
g.drawLine(x, y, x, yTime(model.time + T_STEP))
val s = "$i: $process"
val b = g.fontMetrics.getStringBounds(s, g)
g.drawString(s, x - (b.width / 2).toFloat(), y.toFloat() - TEXT_PAD)
}
for (rcvd in model.actions.filterIsInstance<Rcvd>()) {
val send = rcvd.from
g.drawLine(
xProcess(send.processId), yTime(send.time),
xProcess(rcvd.processId), yTime(rcvd.time)
)
}
for (p in model.ps.values) {
for (a in p.actions) {
when (a) {
is Consensus -> paintConsensus(g, a)
is Send, is Rcvd -> paintSendRcvd(g, a)
}
}
}
hover?.let { hover ->
val x = xProcess(hover.processId)
val y = yTime(hover.time)
g.color = HOVER_COLOR
g.fillOval(x - R1, y - R1, 2 * R1, 2 * R1)
}
}
private fun paintConsensus(g: Graphics2D, a: Consensus) {
val x = xProcess(a.processId)
val y = yTime(a.time)
g.color = FG_COLOR
g.drawLine(x - R, y, x + R, y)
g.drawString(a.toString(), x + R + TEXT_PAD, y)
}
private fun paintSendRcvd(g: Graphics2D, a: Action) {
val x = xProcess(a.processId)
val y = yTime(a.time)
g.color = if (a is Send) BG_COLOR else FG_COLOR
g.fillOval(x - R, y - R, 2 * R, 2 * R)
g.color = FG_COLOR
g.drawOval(x - R, y - R, 2 * R, 2 * R)
if (a is Send && a !in model.pending) {
g.color = FG_COLOR
g.fillOval(x - R1, y - R1, 2 * R1, 2 * R1)
}
g.color = FG_COLOR
val s = a.toString()
val sx = x + R + TEXT_PAD.toFloat()
g.drawString(s, sx, y.toFloat())
}
override fun getPreferredSize(c: JComponent?): Dimension =
Dimension(
LEFT_PAD + X_SPACE * (model.nProcesses - 1) + RIGHT_PAD,
maxOf(yTime(model.time + T_STEP), Y_MINIMUM)
)
private fun MouseEvent.action(): Action? {
val i = processByX(x)
val time = timeByY(y)
val action = model.ps[i]?.actions?.minBy { abs(time - it.time) } ?: return null
if (point.distance(Point(xProcess(i), yTime(action.time))) > R) return null
return action
}
private fun hover(hover: Action?) {
if (this.hover == hover) return
this.hover = hover
c.repaint()
}
override fun mouseClicked(e: MouseEvent) {
val send = e.action() as? Send ?: return
if (send in model.pending) model.perform(send)
}
override fun mouseMoved(e: MouseEvent) = hover(e.action()?.takeIf { it.time > 0 })
override fun mouseExited(e: MouseEvent) = hover(null)
override fun mousePressed(e: MouseEvent) {
val hover = hover ?: return
dragT0 = hover.time
dragStart = e.point
}
override fun mouseDragged(e: MouseEvent) {
val hover = hover ?: return
val t1 = dragT0 + e.y - dragStart.y
if (t1 > dragT0) {
val next = model.nextAction(hover)
var time = next?.time ?: Int.MAX_VALUE
hover.to?.let { a -> time = minOf(time, a.time) }
if (t1 >= time - R) return
} else {
val prev = model.prevAction(hover)
var time = prev?.time ?: 0
hover.from?.let { a -> time = maxOf(time, a.time) }
if (t1 <= time + R) return
}
hover.time = t1
c.revalidate()
c.repaint()
}
override fun mouseReleased(e: MouseEvent) {}
override fun mouseEntered(e: MouseEvent) {}
}
class ControlPane(private val model: Model) : JPanel(FlowLayout()), ActionListener {
private val name = JTextField(15)
private val impl = JComboBox(IMPLS.toTypedArray())
private val restart = JButton("Restart")
private val save = JButton("Save")
init {
add(JLabel("Name"))
add(name)
add(JLabel("Impl"))
add(impl)
add(restart)
add(save)
}
override fun actionPerformed(e: ActionEvent) {
when (e.source) {
save -> model.save(name.text, SOLUTION_FILE)
else -> model.restart(name.text, impl.item)
}
}
fun load() {
if (!SOLUTION_FILE.exists()) {
model.restart(name.text, impl.item)
return
}
model.load(SOLUTION_FILE)
name.text = model.name
impl.selectedItem = model.impl
}
fun installActionListeners() {
name.addActionListener(this)
impl.addActionListener(this)
restart.addActionListener(this)
save.addActionListener(this)
}
}
private val <E> JComboBox<E>.item: E
get() = getItemAt(selectedIndex)