-
Notifications
You must be signed in to change notification settings - Fork 0
/
child_circuit.rb
355 lines (301 loc) · 7.62 KB
/
child_circuit.rb
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
require "set"
# if RUBY_ENGINE == "opal"
# require_remote "./tuden.rb"
# else
# ;
# end
class ChildCircuit
class StateHistory
attr_reader :name
def initialize(size, name)
@size = size
@cur = 0 # cursor
@name = name
@data = Array.new(size, false)
end
def update(val)
@data[@cur] = val
end
def next_index(i)
if i == @size - 1
0
else
i + 1
end
end
def shift_cursor!
@cur = next_index(@cur)
end
def make_indexes
if @cur == @size - 1
(0...@size).to_a
else
((@cur + 1)...@size).to_a + (0..@cur).to_a
end
end
def to_blocks
blocks = []
prev_state = @data[next_index(@cur)]
start_i = 0
make_indexes().each_with_index { |real_i, i|
state = @data[real_i]
if state != prev_state
blocks << [start_i, i - 1, prev_state]
start_i = i
prev_state = state
end
}
if start_i <= @size - 1
blocks << [start_i, @size - 1, prev_state]
end
blocks
end
end
attr_reader :edges
attr_reader :plus_poles
attr_reader :minus_poles
attr_reader :switches
attr_reader :lamps
attr_reader :equal_relays
attr_reader :not_relays
attr_reader :state_histories
def initialize(
edges,
plus_poles,
minus_poles,
switches,
lamps,
equal_relays,
not_relays
)
@edges = edges
@plus_poles = plus_poles
@minus_poles = minus_poles
@switches = switches
@lamps = lamps
@equal_relays = equal_relays
@not_relays = not_relays
@state_histories = []
end
def init_state_histories(size)
names =
@lamps
.select { |lamp| lamp.name }
.map { |lamp| lamp.name }
@state_histories = names.map { |name|
StateHistory.new(size, name)
}
end
def to_plain
{
edges: @edges .map { |it| it.to_plain },
plus_poles: @plus_poles .map { |it| it.to_plain },
minus_poles: @minus_poles .map { |it| it.to_plain },
switches: @switches .map { |it| it.to_plain },
lamps: @lamps .map { |it| it.to_plain },
equal_relays: @equal_relays.map { |it| it.to_plain },
not_relays: @not_relays .map { |it| it.to_plain }
}
end
def self.from_plain(plain)
edges = plain["edges" ].map { |it| Unit::Edge .from_plain(it) }
plus_poles = plain["plus_poles" ].map { |it| Unit::PlusPole .from_plain(it) }
minus_poles = plain["minus_poles" ].map { |it| Unit::MinusPole .from_plain(it) }
switches = plain["switches" ].map { |it| Unit::Switch .from_plain(it) }
lamps = plain["lamps" ].map { |it| Unit::Lamp .from_plain(it) }
equal_relays = plain["equal_relays"].map { |it| Unit::EqualRelay.from_plain(it) }
not_relays = plain["not_relays" ].map { |it| Unit::NotRelay .from_plain(it) }
ChildCircuit.new(
edges,
plus_poles,
minus_poles,
switches,
lamps,
equal_relays,
not_relays
)
end
def tuden?(switches)
switches.all? { |switch| switch.on? }
end
def update_4_edges
edges_connected_to_plus =
@edges.select { |edge|
edge.connected_to?(@plus_poles[0].pos)
}
edges_connected_to_minus =
@edges.select { |edge|
edge.connected_to?(@minus_poles[0].pos)
}
if edges_connected_to_plus.size == 1 &&
edges_connected_to_minus.size == 1
# OK
else
raise "not yet implemented"
end
edge_connected_to_plus = edges_connected_to_plus[0]
edge_connected_to_minus = edges_connected_to_minus[0]
center_edges =
@edges.reject { |edge|
edge == edge_connected_to_plus ||
edge == edge_connected_to_minus
}
center_edges.each { |edge|
switches = @switches.select { |switch|
edge.include_pos?(switch.pos)
}
is_tuden = tuden?(switches)
edge.update(is_tuden)
}
is_tuden_center =
center_edges.any? { |edge| edge.on? }
edge_connected_to_plus.update(is_tuden_center)
edge_connected_to_minus.update(is_tuden_center)
end
def switches_for_edge(edge)
@switches.select { |switch|
edge.include_pos?(switch.pos)
}
end
def pos_set_all
pos_set = Set.new
@edges.each { |edge|
pos_set << edge.pos1
pos_set << edge.pos2
}
pos_set
end
# def prepare_tuden_nodes
# nid_plus = nil
# nid_minus = nil
#
# pos_nid_map = {}
#
# tnodes = []
# pos_set_all.each_with_index { |pos, index|
# nid = index + 1
#
# tnodes << Tuden::Node.new(nid)
#
# pos_nid_map[pos] = nid
#
# if pos == @plus_poles[0].pos
# nid_plus = nid
# elsif pos == @minus_poles[0].pos
# nid_minus = nid
# end
# }
#
# [tnodes, pos_nid_map, nid_plus, nid_minus]
# end
# def prepare_tuden_edges(pos_nid_map)
# eid_edge_map = {}
#
# tedges = []
# @edges.each_with_index { |edge, index|
# eid = index + 1
#
# nid1 = pos_nid_map[edge.pos1]
# nid2 = pos_nid_map[edge.pos2]
#
# switches = switches_for_edge(edge)
#
# tedges <<
# Tuden::Edge.new(
# eid, nid1, nid2,
# switches.all? { |switch| switch.on? }
# )
#
# eid_edge_map[eid] = edge
# }
#
# [tedges, eid_edge_map]
# end
# def update_many_edges
# tnodes, pos_nid_map, nid_plus, nid_minus =
# prepare_tuden_nodes()
#
# tedges, eid_edge_map =
# prepare_tuden_edges(pos_nid_map)
#
# Tuden.update(
# tedges,
# tnodes,
# nid_plus,
# nid_minus
# )
#
# tedges.each { |tedge|
# edge = eid_edge_map[tedge.id]
# edge.update(tedge.on?)
# }
# end
def update_edges
case @edges.size
when 1
is_tuden = tuden?(@switches)
@edges[0].update(is_tuden)
when 4
update_4_edges()
else
# update_many_edges()
raise "Not supported"
end
end
def update_lamps
@lamps.each { |lamp|
edge = @edges.find { |edge| edge.include_pos?(lamp.pos) }
lamp.update(edge.on?)
}
end
def neighbor?(pos1, pos2)
pos1 == pos2.translate( 0, -1) ||
pos1 == pos2.translate( 1, 0) ||
pos1 == pos2.translate( 0, 1) ||
pos1 == pos2.translate(-1, 0)
end
def find_neighbor_switch(pos)
@switches.find { |switch|
neighbor?(switch.pos, pos)
}
end
def update_equal_relays(circuit)
switch_changed = false
@equal_relays.each { |equal_relay|
edge = @edges.find { |edge| edge.include_pos?(equal_relay.pos) }
equal_relay.update(edge.on?)
neighbor_switch = circuit.find_neighbor_switch(equal_relay.pos)
state_before_update = neighbor_switch.on?
neighbor_switch.update(equal_relay.on?)
if neighbor_switch.on? != state_before_update
switch_changed = true
end
}
switch_changed
end
def update_not_relays(circuit)
switch_changed = false
@not_relays.each { |not_relay|
edge = @edges.find { |edge| edge.include_pos?(not_relay.pos) }
not_relay.update(edge.on?)
neighbor_switch = circuit.find_neighbor_switch(not_relay.pos)
state_before_update = neighbor_switch.on?
neighbor_switch.update(! not_relay.on?)
if neighbor_switch.on? != state_before_update
switch_changed = true
end
}
switch_changed
end
def find_edge_including_pos(pos)
@edges.find { |edge|
edge.include_pos?(pos)
}
end
def find_lamp(name)
@lamps.find { |lamp| lamp.name == name }
end
def pretty_inspect
to_plain.pretty_inspect
end
end