forked from laminne/rwire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparserRuby.ts
More file actions
289 lines (265 loc) · 7.95 KB
/
Copy pathparserRuby.ts
File metadata and controls
289 lines (265 loc) · 7.95 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
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
import { build } from "./build.ts";
import { DebugNode } from "./definitions/debug.ts";
import { InjectNode } from "./definitions/inject.ts";
import { LEDNode } from "./definitions/led.ts";
import { TriggerNode } from "./definitions/trigger.ts";
import { Debug } from "./types/nodes/debug.ts";
import { Inject } from "./types/nodes/inject.ts";
import { MrubyLED } from "./types/nodes/mruby-led.ts";
import { Trigger } from "./types/nodes/trigger.ts";
import { NodeOutput } from "./types/output.ts";
import { MrubyGPIOREAD } from "./types/nodes/gpio_read.ts";
import { GPIOREADNode } from "./definitions/gpio_read.ts";
import { MrubyPWM } from "./types/nodes/mruby-pwm.ts";
import { PWMNode } from "./definitions/pwm.ts";
import { Delay } from "./types/nodes/delay.ts";
import { DelayNode } from "./definitions/delay.ts";
import { Switch } from "./types/nodes/switch.ts";
import { SwitchNode } from "./definitions/switch.ts";
import { MrubyGPIOWRITE } from "./types/nodes/gpio_write.ts";
import { GPIOWRITENode } from "./definitions/gpio_write.ts";
import { MrubyADC } from "./types/nodes/mruby-adc.ts";
import { ADCNode } from "./definitions/adc.ts";
import { MrubyConstant } from "./types/nodes/mruby-constant.ts";
import { ConstantNode } from "./definitions/constant.ts";
import { MrubyFunctionRuby } from "./types/nodes/mruby-function-ruby.ts";
import { FunctionRubyNode } from "./definitions/function_ruby.ts";
import { MrubyBUTTON } from "./types/nodes/mruby-button.ts";
import { BUTTONNode } from "./definitions/button.ts";
import { MrubyI2C } from "./types/nodes/mruby-i2c.ts";
import { I2CNode } from "./definitions/i2c.ts";
type flow =
| Debug
| Inject
| MrubyLED
| Trigger
| MrubyGPIOREAD
| MrubyPWM
| Delay
| Switch
| MrubyGPIOWRITE
| MrubyADC
| MrubyConstant
| MrubyFunctionRuby
| MrubyBUTTON
| MrubyI2C;
type flows = flow[];
export const parseJSON = (json: string): flows => {
const parsed = JSON.parse(json) as flows;
return parsed.filter((n) => {
const nodeType = [
"debug",
"inject",
"LED",
"trigger",
"GPIO-Read",
"PWM",
"delay",
"switch",
"GPIO-Write-1",
"ADC",
"Constant",
"function-Code",
"Button",
"initLCD",
"I2C",
];
return nodeType.includes(n.type);
});
};
type InputNode = {
id: string;
type: string;
data: flow;
wires: string[] | string[][];
};
const parsed = parseJSON(Deno.readTextFileSync("flows.json"));
const input: InputNode[] = parsed.map((n) => {
return {
id: n.id,
type: n.type,
data: n,
wires: n.wires,
};
});
type Node = {
id: string;
type: string;
data: flow;
wires: Node[][];
};
// wires配列を正規化する
function normalizeWires(wires: string[] | string[][]): string[][] {
if (wires.length === 0) return [];
// 最初の要素が文字列なら一次元配列として扱う
if (typeof wires[0] === "string") {
return [wires as string[]];
}
// 既に二次元配列の場合はそのまま返す
return wires as string[][];
}
function transformToNode(inputNodes: InputNode[]): Node[] {
// 入力データを検索しやすいようにマップに変換
const nodeMap = new Map<string, InputNode>();
inputNodes.forEach((node) => nodeMap.set(node.id, node));
/**
* 再帰的にノードを構築
*/
const buildNode = (id: string): Node => {
const inputNode = nodeMap.get(id);
if (inputNode === undefined) {
throw new Error(`Node with id ${id} not found`);
}
const normalizedWires = normalizeWires(inputNode.wires);
// 各出力ポートの接続先ノードを構築
const wireNodes: Node[][] = normalizedWires.map((outputWires) =>
outputWires.map(buildNode)
);
return {
id: inputNode.id,
type: inputNode.type,
wires: wireNodes,
data: inputNode.data,
};
};
const isRootNode = (node: InputNode): boolean => {
const nodeId = node.id;
return !inputNodes.some((otherNode) => {
const normalizedWires = normalizeWires(otherNode.wires);
return normalizedWires.some((outputWires) =>
outputWires.includes(nodeId)
);
});
};
// 入力データのルートノードを処理
return inputNodes
.filter(isRootNode)
.map((rootNode) => buildNode(rootNode.id));
}
const toNodeOutput = (
node: Node
):
| InjectNode
| TriggerNode
| LEDNode
| DebugNode
| GPIOREADNode
| PWMNode
| DelayNode
| SwitchNode
| GPIOWRITENode
| ADCNode
| ConstantNode
| FunctionRubyNode
| BUTTONNode
| I2CNode => {
const allConnectedNodes = node.wires.flat().map(toNodeOutput);
switch (node.type) {
case "inject":
return new InjectNode(node.data as Inject, allConnectedNodes);
case "trigger":
return new TriggerNode(node.data as Trigger, allConnectedNodes);
case "LED":
return new LEDNode(node.data as MrubyLED);
case "debug":
return new DebugNode(node.data as Debug);
case "GPIO-Read":
return new GPIOREADNode(node.data as MrubyGPIOREAD, allConnectedNodes);
case "PWM":
return new PWMNode(node.data as MrubyPWM, allConnectedNodes);
case "delay":
return new DelayNode(node.data as Delay, allConnectedNodes);
case "switch": {
const portNodes = node.wires.map((outputWires) =>
outputWires.map(toNodeOutput)
);
return new SwitchNode(node.data as Switch, allConnectedNodes, portNodes);
}
case "GPIO-Write-1":
return new GPIOWRITENode(node.data as MrubyGPIOWRITE);
case "ADC":
return new ADCNode(node.data as MrubyADC, allConnectedNodes);
case "Constant":
return new ConstantNode(node.data as MrubyConstant, allConnectedNodes);
case "function-Code":
return new FunctionRubyNode(
node.data as MrubyFunctionRuby,
allConnectedNodes
);
case "Button":
return new BUTTONNode(node.data as MrubyBUTTON, allConnectedNodes);
case "initLCD":
return new I2CNode(node.data as MrubyI2C, allConnectedNodes);
case "I2C":
return new I2CNode(node.data as MrubyI2C, allConnectedNodes);
default:
throw new Error(`Unknown node type: ${node.type}`);
}
};
type codeOutput = {
nodeID: string;
code: string;
initialisationCode: string;
initialisationCodes: string[];
nodeName: string;
};
const collectCode = (node: NodeOutput): codeOutput[] => {
const code: codeOutput[] = [
{
code: node.getNodeCodeOutput(),
nodeID: node.getNodeID(),
initialisationCode: node.getNodeInitialisationCode(),
initialisationCodes: node.getInitialisationCodes(),
nodeName: node.getTaskName(),
},
];
for (const child of node.getNextConnectedNodes()) {
code.push(...collectCode(child));
}
return code;
};
// 実行
const result = transformToNode(input);
// ノードのデータ受け渡しに必要な関数を生成
const dataPass = `
$data = {}
def getData (id)
a = $data[id]
return a
end
def sendData(id, data)
return $data[id]= data
end
`;
console.log(dataPass);
// それぞれのノードに対して、getNodeCodeOutput()を呼び出し、コードを生成
for (let i = 0; i < result.length; i++) {
const res = toNodeOutput(result[i]);
const codes = collectCode(res);
const taskCode = async (id: string, nodeName: string) => {
const readData = await Deno.readTextFile(`build/${id}.rb`);
return `$${nodeName} = Task.create("${await build(id, readData)}")`;
};
const buildTaskCodes = async () => {
const res: string[] = [];
for (const code of codes) {
res.push(await taskCode(code.nodeID, code.nodeName));
}
return res;
};
const c = await buildTaskCodes();
// 初期宣言コードを集めて重複を削除
const initialisationCodes = codes.map((v) => v.initialisationCodes).flat();
const uniqueinits: string[] = Array.from(new Set(initialisationCodes));
const output = [
,
uniqueinits.join("\n"),
"",
c.join("\n"),
"",
codes.map((v) => v.initialisationCode).join("\n"),
"",
res.getCallCodes(),
].join("\n");
console.log(output);
}