-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogicSystem.js
258 lines (233 loc) · 7.38 KB
/
LogicSystem.js
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
"use strict";
var initial_values = {
"AND": false,
"OR": false,
"NOT": true,
"NAND": true,
"NOR": true,
"XOR": false,
"XNOR": true,
"SWITCH": false,
"BULB": false
};
var operations = {
"AND": function(a, b) {return a && b;},
"OR": function(a, b) {return a || b;},
"NOT": function(a, b) {return !a;},
"NAND": function(a, b) {return !(a && b);},
"NOR": function(a, b) {return !(a || b);},
"XOR": function(a, b) {return (a || b) && ! (a && b);},
"XNOR": function(a, b) {return (a && b) || (!a && !b);},
"BULB": function(a, b, c) {return a;},
"SWITCH": function(a, b, c) {return c;},
"INPUT": function() {return false;},
"OUTPUT": function() {return false;}
};
function runStep(gates, input_values) {
var outputs = [];
for (var i = 0; i < gates.length; i++) {
var gate = gates[i];
if (gate) {
outputs[i] = operations[gate[0]](input_values[gate[1]], input_values[gate[2]], input_values[i]);
}
}
return outputs;
}
function applyToArrays(func, a, b) {
var output = [];
for (var i = 0; i < a.length; i++) {
output[i] = func(a[i], b[i]);
}
return output;
}
function any(a) {
for (var i = 0; i < a.length; i++) {
if (a[i]) {
return true;
}
}
}
/********************************************************************************************/
function LogicSystem()
/********************************************************************************************/
{
this.gates = [null];
this.output_values = [false];
this.transients = [false];
this.callbacks = [];
}
LogicSystem.prototype.addGate = function(type) {
var id = this.gates.length;
this.gates.push([type, 0, 0]);
this.output_values[id] = initial_values[type];
this.transients[id] = initial_values[type];
return id;
};
LogicSystem.prototype.removeGate = function(id) {
this.gates[id] = null;
};
LogicSystem.prototype.makeConnection = function(output_id, input_id, input_number) {
this.gates[input_id][input_number + 1] = output_id;
this.injectTransient(input_id);
};
LogicSystem.prototype.removeConnection = function(input_id, input_number) {
this.gates[input_id][input_number + 1] = 0;
};
LogicSystem.prototype.registerCallback = function(id, callback) {
this.callbacks[id] = callback;
};
LogicSystem.prototype.dropCallback = function(id) {
this.callbacks[id] = null;
};
LogicSystem.prototype.setOutput = function(id, value) {
this.transients[id] = true;
this.output_values[id] = value;
};
LogicSystem.prototype.getOutput = function(id) {
return this.output_values[id];
};
LogicSystem.prototype.injectTransient = function(id) {
this.transients[id] = true;
};
LogicSystem.prototype.runStep = function() {
var old_output_values = this.output_values;
this.output_values = runStep(this.gates, this.output_values);
this.transients = applyToArrays(operations["XOR"], this.output_values, old_output_values);
};
LogicSystem.prototype.runCallbacks = function() {
for (var i = 0; i < this.transients.length; i++) {
if (this.transients[i] && this.callbacks[i]) {
this.callbacks[i](this.output_values[i]);
}
}
};
LogicSystem.prototype.run = function(steps) {
var counter = 0;
while (any(this.transients) && (counter < steps || !steps)) {
this.runStep();
this.runCallbacks();
counter ++;
}
};
function copyInto(source, destination) {
var node_map = {};
jsnx.forEach(source.nodes_iter(), function(node) {
var id = _.uniqueId();
var node_attrs = source.node.get(node);
if (node_attrs.type && _.contains(["INPUT", "OUTPUT"], node_attrs.type)) {
node_attrs.from_subcircuit = true;
node_attrs.connection_number = node;
}
destination.add_node(id, node_attrs);
node_map[node] = id;
});
jsnx.forEach(source.edges_iter(), function(edge) {
var edge_attrs = source.get_edge_data(edge[0], edge[1]);
destination.add_edge(node_map[edge[0]], node_map[edge[1]], edge_attrs);
});
}
function mergeNodes(graph, keep, go_away) {
jsnx.forEach(graph.in_edges(go_away).concat(graph.out_edges(go_away)), function(edge) {
var edge_attrs = graph.get_edge_data(edge[0], edge[1]);
if (edge[0] == go_away) {
graph.add_edge(keep, edge[1], edge_attrs);
} else {
graph.add_edge(edge[0], keep, edge_attrs);
}
});
graph.remove_node(go_away);
}
/*
node is a node in subcircuit. Returns a new graph the same as circuit but with node replaced by subcircuit.
This makes the assumption that subcircuit is the correct circuit to be replacing node. There is no sanity checking,
just pass it the right thing.
*/
function replaceNode(circuit, subcircuit, node_to_replace) {
copyInto(subcircuit, circuit);
jsnx.forEach(circuit.nodes(), function(node) {
if (circuit.has_node(node)) {
var node_attrs = circuit.node.get(node);
if (node_attrs.type && _.contains(["INPUT", "OUTPUT"], node_attrs.type) && node_attrs.from_subcircuit) {
// This is an input/output we need to get rid of.
var marked_connection; // The connection node that is going to go away
var connections; // The one we're keeping is in here
if (node_attrs.type == "INPUT") {
marked_connection = circuit.successors(node)[0];
connections = circuit.in_edges_iter(node_to_replace)
} else {
marked_connection = circuit.predecessors(node)[0];
connections = circuit.out_edges_iter(node_to_replace)
}
var connection; // The one we're going to keep.
jsnx.forEach(connections, function(edge) {
var edge_attrs = circuit.get_edge_data(edge[0], edge[1]);
if (edge_attrs.connect_to == node_attrs.connection_number) {
connection = (node_attrs.type == "INPUT" ? edge[0] : edge[1]);
}
})
if (marked_connection && connection) {
mergeNodes(circuit, connection, marked_connection);
}
circuit.remove_node(node);
}
}
});
circuit.remove_node(node_to_replace);
}
function replaceAllSubCircuits(circuit, template_manager) {
var new_circuit = new jsnx.DiGraph(circuit);
_.each(new_circuit.nodes(), function(node) {
if (new_circuit.has_node(node)) {
var node_attrs = new_circuit.node.get(node);
if (node_attrs.type == "SUBCIRCIT") {
replaceNode(new_circuit, template_manager.getFlatGraph(node_attrs.schema_id), node);
}
}
});
return new_circuit;
}
function buildLogicSystem(graph, schema_model) {
var logic_system = new LogicSystem();
var inputs = [];
var outputs = [];
var logic_ids = {};
var connection_nodes = [];
jsnx.forEach(graph.nodes_iter(), function(node) {
var node_attrs = graph.node.get(node);
if (node_attrs.type) {
logic_ids[node] = logic_system.addGate(node_attrs.type);
if (node_attrs.type == "SWITCH") {
inputs.push(node);
} else if (node_attrs.type == "BULB") {
outputs.push(node);
}
} else {
connection_nodes.push(node);
}
});
_.each(connection_nodes, function(connection) {
var from = graph.predecessors(connection)[0];
jsnx.forEach(graph.successors_iter(connection), function(node) {
var edge_attrs = graph.get_edge_data(connection, node);
logic_system.makeConnection(logic_ids[from], logic_ids[node], edge_attrs.connect_to);
});
});
_.each(inputs, function(node) {
var object = schema_model.getObjectByNumber(node);
if (object) {
logic_system.setOutput(logic_ids[node], object.getState());
object.stateChanged = function(new_state) {
logic_system.setOutput(logic_ids[node], new_state);
}
}
});
_.each(outputs, function(node) {
var object = schema_model.getObjectByNumber(node);
if (object) {
logic_system.registerCallback(logic_ids[node], function (new_state) {
object.setState(new_state);
});
}
});
return logic_system;
}