@orcalang/orca-lang 0.1.18 → 0.1.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/compiler/dt-compiler.d.ts +26 -0
- package/dist/compiler/dt-compiler.d.ts.map +1 -0
- package/dist/compiler/dt-compiler.js +387 -0
- package/dist/compiler/dt-compiler.js.map +1 -0
- package/dist/health-check.d.ts +3 -0
- package/dist/health-check.d.ts.map +1 -0
- package/dist/health-check.js +235 -0
- package/dist/health-check.js.map +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/dist/parser/ast-to-markdown.d.ts.map +1 -1
- package/dist/parser/ast-to-markdown.js +3 -1
- package/dist/parser/ast-to-markdown.js.map +1 -1
- package/dist/parser/ast.d.ts +3 -0
- package/dist/parser/ast.d.ts.map +1 -1
- package/dist/parser/dt-ast.d.ts +43 -0
- package/dist/parser/dt-ast.d.ts.map +1 -0
- package/dist/parser/dt-ast.js +3 -0
- package/dist/parser/dt-ast.js.map +1 -0
- package/dist/parser/dt-parser.d.ts +40 -0
- package/dist/parser/dt-parser.d.ts.map +1 -0
- package/dist/parser/dt-parser.js +240 -0
- package/dist/parser/dt-parser.js.map +1 -0
- package/dist/parser/markdown-parser.d.ts.map +1 -1
- package/dist/parser/markdown-parser.js +43 -8
- package/dist/parser/markdown-parser.js.map +1 -1
- package/dist/skills.d.ts +50 -1
- package/dist/skills.d.ts.map +1 -1
- package/dist/skills.js +508 -21
- package/dist/skills.js.map +1 -1
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +49 -0
- package/dist/tools.js.map +1 -1
- package/dist/verifier/dt-verifier.d.ts +32 -0
- package/dist/verifier/dt-verifier.d.ts.map +1 -0
- package/dist/verifier/dt-verifier.js +830 -0
- package/dist/verifier/dt-verifier.js.map +1 -0
- package/dist/verifier/properties.d.ts +4 -0
- package/dist/verifier/properties.d.ts.map +1 -1
- package/dist/verifier/properties.js +56 -20
- package/dist/verifier/properties.js.map +1 -1
- package/dist/verifier/structural.d.ts.map +1 -1
- package/dist/verifier/structural.js +6 -1
- package/dist/verifier/structural.js.map +1 -1
- package/dist/verifier/types.d.ts +4 -0
- package/dist/verifier/types.d.ts.map +1 -1
- package/package.json +3 -2
- package/src/compiler/dt-compiler.ts +454 -0
- package/src/health-check.ts +273 -0
- package/src/index.ts +5 -1
- package/src/parser/ast-to-markdown.ts +2 -1
- package/src/parser/ast.ts +4 -0
- package/src/parser/dt-ast.ts +40 -0
- package/src/parser/dt-parser.ts +289 -0
- package/src/parser/markdown-parser.ts +43 -8
- package/src/skills.ts +591 -22
- package/src/tools.ts +53 -0
- package/src/verifier/dt-verifier.ts +928 -0
- package/src/verifier/properties.ts +78 -23
- package/src/verifier/structural.ts +5 -1
- package/src/verifier/types.ts +4 -0
|
@@ -0,0 +1,454 @@
|
|
|
1
|
+
// Decision Table Compiler
|
|
2
|
+
// Compiles verified decision tables to TypeScript, Python, Go evaluator functions or JSON
|
|
3
|
+
|
|
4
|
+
import { DecisionTableDef, CellValue } from '../parser/dt-ast.js';
|
|
5
|
+
|
|
6
|
+
// Convert camelCase or snake_case to PascalCase
|
|
7
|
+
function toPascalCase(name: string): string {
|
|
8
|
+
return name
|
|
9
|
+
.split(/[_-]/)
|
|
10
|
+
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
|
|
11
|
+
.join('');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// Convert PascalCase or camelCase to snake_case
|
|
15
|
+
export function toSnakeCase(name: string): string {
|
|
16
|
+
return name
|
|
17
|
+
.replace(/([A-Z])/g, '_$1')
|
|
18
|
+
.toLowerCase()
|
|
19
|
+
.replace(/^_/, '');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Generate TypeScript type for input interface
|
|
23
|
+
function generateInputType(dt: DecisionTableDef): string {
|
|
24
|
+
const lines: string[] = [];
|
|
25
|
+
lines.push(`export interface ${toPascalCase(dt.name)}Input {`);
|
|
26
|
+
|
|
27
|
+
for (const cond of dt.conditions) {
|
|
28
|
+
const typeStr = generateTypeScriptType(cond.type, cond.values);
|
|
29
|
+
lines.push(` ${cond.name}: ${typeStr};`);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
lines.push('}');
|
|
33
|
+
return lines.join('\n');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Generate TypeScript type for output interface
|
|
37
|
+
function generateOutputType(dt: DecisionTableDef): string {
|
|
38
|
+
const lines: string[] = [];
|
|
39
|
+
lines.push(`export interface ${toPascalCase(dt.name)}Output {`);
|
|
40
|
+
|
|
41
|
+
for (const action of dt.actions) {
|
|
42
|
+
const typeStr = generateTypeScriptType(action.type, action.values || []);
|
|
43
|
+
lines.push(` ${action.name}: ${typeStr};`);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
lines.push('}');
|
|
47
|
+
return lines.join('\n');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Generate TypeScript type string from condition/action type
|
|
51
|
+
function generateTypeScriptType(type: string, values: string[]): string {
|
|
52
|
+
if (type === 'bool') {
|
|
53
|
+
return 'boolean';
|
|
54
|
+
}
|
|
55
|
+
if (type === 'enum') {
|
|
56
|
+
return values.length > 0 ? values.map(v => `'${v}'`).join(' | ') : 'string';
|
|
57
|
+
}
|
|
58
|
+
if (type === 'int_range') {
|
|
59
|
+
return 'number';
|
|
60
|
+
}
|
|
61
|
+
return 'string';
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Generate condition check code
|
|
65
|
+
function generateConditionCheck(condName: string, condType: string, cell: CellValue): string {
|
|
66
|
+
switch (cell.kind) {
|
|
67
|
+
case 'any':
|
|
68
|
+
return ''; // No condition needed
|
|
69
|
+
|
|
70
|
+
case 'exact':
|
|
71
|
+
// For bool type, compare to boolean; for others, compare to string
|
|
72
|
+
if (condType === 'bool') {
|
|
73
|
+
return `input.${condName} === ${cell.value}`;
|
|
74
|
+
}
|
|
75
|
+
return `input.${condName} === '${cell.value}'`;
|
|
76
|
+
|
|
77
|
+
case 'negated':
|
|
78
|
+
if (condType === 'bool') {
|
|
79
|
+
return `input.${condName} !== ${cell.value}`;
|
|
80
|
+
}
|
|
81
|
+
return `input.${condName} !== '${cell.value}'`;
|
|
82
|
+
|
|
83
|
+
case 'set':
|
|
84
|
+
const checks = cell.values.map(v => {
|
|
85
|
+
if (condType === 'bool') {
|
|
86
|
+
return `input.${condName} === ${v}`;
|
|
87
|
+
}
|
|
88
|
+
return `input.${condName} === '${v}'`;
|
|
89
|
+
}).join(' || ');
|
|
90
|
+
return `(${checks})`;
|
|
91
|
+
|
|
92
|
+
default:
|
|
93
|
+
return '';
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Generate return statement for a rule
|
|
98
|
+
function generateReturnStatement(dt: DecisionTableDef, ruleIndex: number): string {
|
|
99
|
+
const rule = dt.rules[ruleIndex];
|
|
100
|
+
const actionEntries: string[] = [];
|
|
101
|
+
|
|
102
|
+
for (const action of dt.actions) {
|
|
103
|
+
const value = rule.actions.get(action.name);
|
|
104
|
+
if (value !== undefined) {
|
|
105
|
+
if (action.type === 'bool') {
|
|
106
|
+
// Bool actions use true/false
|
|
107
|
+
actionEntries.push(` ${action.name}: ${value}`);
|
|
108
|
+
} else {
|
|
109
|
+
actionEntries.push(` ${action.name}: '${value}'`);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return `return {\n${actionEntries.join(',\n')}\n };`;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Compile to TypeScript evaluator function
|
|
118
|
+
export function compileDecisionTableToTypeScript(dt: DecisionTableDef): string {
|
|
119
|
+
const inputTypeName = `${toPascalCase(dt.name)}Input`;
|
|
120
|
+
const outputTypeName = `${toPascalCase(dt.name)}Output`;
|
|
121
|
+
const functionName = `evaluate${toPascalCase(dt.name)}`;
|
|
122
|
+
|
|
123
|
+
const lines: string[] = [];
|
|
124
|
+
|
|
125
|
+
// Input interface
|
|
126
|
+
lines.push(generateInputType(dt));
|
|
127
|
+
lines.push('');
|
|
128
|
+
|
|
129
|
+
// Output interface
|
|
130
|
+
lines.push(generateOutputType(dt));
|
|
131
|
+
lines.push('');
|
|
132
|
+
|
|
133
|
+
// Evaluator function
|
|
134
|
+
lines.push(`export function ${functionName}(input: ${inputTypeName}): ${outputTypeName} | null {`);
|
|
135
|
+
|
|
136
|
+
// Generate rule checks
|
|
137
|
+
for (let ruleIdx = 0; ruleIdx < dt.rules.length; ruleIdx++) {
|
|
138
|
+
const rule = dt.rules[ruleIdx];
|
|
139
|
+
const ruleNum = rule.number ?? ruleIdx + 1;
|
|
140
|
+
|
|
141
|
+
// Collect condition checks
|
|
142
|
+
const checks: string[] = [];
|
|
143
|
+
for (const [condName, cell] of rule.conditions) {
|
|
144
|
+
const condDef = dt.conditions.find(c => c.name === condName);
|
|
145
|
+
const condType = condDef?.type ?? 'string';
|
|
146
|
+
const check = generateConditionCheck(condName, condType, cell);
|
|
147
|
+
if (check) {
|
|
148
|
+
checks.push(check);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Build the if statement
|
|
153
|
+
if (checks.length === 0) {
|
|
154
|
+
// Rule with all wildcards - always matches
|
|
155
|
+
lines.push(` // Rule ${ruleNum}: always matches`);
|
|
156
|
+
lines.push(` {`);
|
|
157
|
+
lines.push(` ${generateReturnStatement(dt, ruleIdx)}`);
|
|
158
|
+
lines.push(` }`);
|
|
159
|
+
} else {
|
|
160
|
+
lines.push(` // Rule ${ruleNum}`);
|
|
161
|
+
const conditionStr = checks.join(' && ');
|
|
162
|
+
lines.push(` if (${conditionStr}) {`);
|
|
163
|
+
lines.push(` ${generateReturnStatement(dt, ruleIdx)}`);
|
|
164
|
+
lines.push(` }`);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// Default return null
|
|
169
|
+
lines.push(' return null; // no rule matched');
|
|
170
|
+
lines.push('}');
|
|
171
|
+
|
|
172
|
+
return lines.join('\n');
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// Compile to JSON
|
|
176
|
+
export interface DTJSONRule {
|
|
177
|
+
conditions: Record<string, string>;
|
|
178
|
+
actions: Record<string, string>;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export interface DTJSON {
|
|
182
|
+
name: string;
|
|
183
|
+
conditions: Array<{
|
|
184
|
+
name: string;
|
|
185
|
+
type: string;
|
|
186
|
+
values: string[];
|
|
187
|
+
}>;
|
|
188
|
+
actions: Array<{
|
|
189
|
+
name: string;
|
|
190
|
+
type: string;
|
|
191
|
+
values?: string[];
|
|
192
|
+
}>;
|
|
193
|
+
rules: DTJSONRule[];
|
|
194
|
+
policy: 'first-match' | 'all-match';
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export function compileDecisionTableToJSON(dt: DecisionTableDef): string {
|
|
198
|
+
const json: DTJSON = {
|
|
199
|
+
name: dt.name,
|
|
200
|
+
conditions: dt.conditions.map(c => ({
|
|
201
|
+
name: c.name,
|
|
202
|
+
type: c.type,
|
|
203
|
+
values: c.values,
|
|
204
|
+
})),
|
|
205
|
+
actions: dt.actions.map(a => {
|
|
206
|
+
const result: { name: string; type: string; values?: string[] } = {
|
|
207
|
+
name: a.name,
|
|
208
|
+
type: a.type,
|
|
209
|
+
};
|
|
210
|
+
if (a.values) {
|
|
211
|
+
result.values = a.values;
|
|
212
|
+
}
|
|
213
|
+
return result;
|
|
214
|
+
}),
|
|
215
|
+
rules: dt.rules.map(rule => {
|
|
216
|
+
// Omit wildcard conditions (kind: 'any')
|
|
217
|
+
const conditions: Record<string, string> = {};
|
|
218
|
+
for (const [name, cell] of rule.conditions) {
|
|
219
|
+
if (cell.kind === 'exact') {
|
|
220
|
+
conditions[name] = cell.value;
|
|
221
|
+
} else if (cell.kind === 'negated') {
|
|
222
|
+
conditions[name] = `!${cell.value}`;
|
|
223
|
+
} else if (cell.kind === 'set') {
|
|
224
|
+
conditions[name] = cell.values.join(',');
|
|
225
|
+
}
|
|
226
|
+
// 'any' is omitted
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const actions: Record<string, string> = {};
|
|
230
|
+
for (const [name, value] of rule.actions) {
|
|
231
|
+
actions[name] = value;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
return { conditions, actions };
|
|
235
|
+
}),
|
|
236
|
+
policy: dt.policy,
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
return JSON.stringify(json, null, 2);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// ============================================================
|
|
243
|
+
// Python Compiler
|
|
244
|
+
// ============================================================
|
|
245
|
+
|
|
246
|
+
function generatePythonType(type: string): string {
|
|
247
|
+
if (type === 'bool') return 'bool';
|
|
248
|
+
if (type === 'int_range') return 'int';
|
|
249
|
+
return 'str';
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function generatePythonConditionCheck(condName: string, condType: string, cell: CellValue): string {
|
|
253
|
+
switch (cell.kind) {
|
|
254
|
+
case 'any':
|
|
255
|
+
return '';
|
|
256
|
+
case 'exact':
|
|
257
|
+
if (condType === 'bool') return `input.${condName} == ${cell.value}`;
|
|
258
|
+
return `input.${condName} == '${cell.value}'`;
|
|
259
|
+
case 'negated':
|
|
260
|
+
if (condType === 'bool') return `input.${condName} != ${cell.value}`;
|
|
261
|
+
return `input.${condName} != '${cell.value}'`;
|
|
262
|
+
case 'set': {
|
|
263
|
+
const checks = cell.values.map(v =>
|
|
264
|
+
condType === 'bool' ? `input.${condName} == ${v}` : `input.${condName} == '${v}'`
|
|
265
|
+
).join(' or ');
|
|
266
|
+
return `(${checks})`;
|
|
267
|
+
}
|
|
268
|
+
default:
|
|
269
|
+
return '';
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
export function compileDecisionTableToPython(dt: DecisionTableDef): string {
|
|
274
|
+
const inputClassName = `${toPascalCase(dt.name)}Input`;
|
|
275
|
+
const outputClassName = `${toPascalCase(dt.name)}Output`;
|
|
276
|
+
const fnName = `evaluate_${toSnakeCase(dt.name)}`;
|
|
277
|
+
|
|
278
|
+
const lines: string[] = [
|
|
279
|
+
'from typing import Optional',
|
|
280
|
+
'from dataclasses import dataclass',
|
|
281
|
+
'',
|
|
282
|
+
'',
|
|
283
|
+
'@dataclass',
|
|
284
|
+
`class ${inputClassName}:`,
|
|
285
|
+
];
|
|
286
|
+
|
|
287
|
+
for (const cond of dt.conditions) {
|
|
288
|
+
const typeStr = generatePythonType(cond.type);
|
|
289
|
+
const comment = cond.type === 'enum' && cond.values.length > 0
|
|
290
|
+
? ` # ${cond.values.join(', ')}`
|
|
291
|
+
: '';
|
|
292
|
+
lines.push(` ${cond.name}: ${typeStr}${comment}`);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
lines.push('');
|
|
296
|
+
lines.push('');
|
|
297
|
+
lines.push('@dataclass');
|
|
298
|
+
lines.push(`class ${outputClassName}:`);
|
|
299
|
+
|
|
300
|
+
for (const action of dt.actions) {
|
|
301
|
+
const typeStr = generatePythonType(action.type);
|
|
302
|
+
const comment = action.type === 'enum' && action.values && action.values.length > 0
|
|
303
|
+
? ` # ${action.values.join(', ')}`
|
|
304
|
+
: '';
|
|
305
|
+
lines.push(` ${action.name}: ${typeStr}${comment}`);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
lines.push('');
|
|
309
|
+
lines.push('');
|
|
310
|
+
lines.push(`def ${fnName}(input: ${inputClassName}) -> Optional[${outputClassName}]:`);
|
|
311
|
+
|
|
312
|
+
for (let ruleIdx = 0; ruleIdx < dt.rules.length; ruleIdx++) {
|
|
313
|
+
const rule = dt.rules[ruleIdx];
|
|
314
|
+
const ruleNum = rule.number ?? ruleIdx + 1;
|
|
315
|
+
|
|
316
|
+
const checks: string[] = [];
|
|
317
|
+
for (const [condName, cell] of rule.conditions) {
|
|
318
|
+
const condDef = dt.conditions.find(c => c.name === condName);
|
|
319
|
+
const condType = condDef?.type ?? 'string';
|
|
320
|
+
const check = generatePythonConditionCheck(condName, condType, cell);
|
|
321
|
+
if (check) checks.push(check);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
const actionArgs = dt.actions
|
|
325
|
+
.map(a => {
|
|
326
|
+
const value = rule.actions.get(a.name);
|
|
327
|
+
if (value === undefined) return null;
|
|
328
|
+
if (a.type === 'bool') return `${a.name}=${value}`;
|
|
329
|
+
return `${a.name}='${value}'`;
|
|
330
|
+
})
|
|
331
|
+
.filter((v): v is string => v !== null)
|
|
332
|
+
.join(', ');
|
|
333
|
+
|
|
334
|
+
lines.push(` # Rule ${ruleNum}`);
|
|
335
|
+
if (checks.length === 0) {
|
|
336
|
+
lines.push(` return ${outputClassName}(${actionArgs})`);
|
|
337
|
+
} else {
|
|
338
|
+
lines.push(` if ${checks.join(' and ')}:`);
|
|
339
|
+
lines.push(` return ${outputClassName}(${actionArgs})`);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
lines.push(' return None # no rule matched');
|
|
344
|
+
|
|
345
|
+
return lines.join('\n');
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
// ============================================================
|
|
349
|
+
// Go Compiler
|
|
350
|
+
// ============================================================
|
|
351
|
+
|
|
352
|
+
function generateGoType(type: string): string {
|
|
353
|
+
if (type === 'bool') return 'bool';
|
|
354
|
+
if (type === 'int_range') return 'int';
|
|
355
|
+
return 'string';
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
function generateGoConditionCheck(condName: string, condType: string, cell: CellValue): string {
|
|
359
|
+
const goField = toPascalCase(condName);
|
|
360
|
+
switch (cell.kind) {
|
|
361
|
+
case 'any':
|
|
362
|
+
return '';
|
|
363
|
+
case 'exact':
|
|
364
|
+
if (condType === 'bool') return `input.${goField} == ${cell.value}`;
|
|
365
|
+
return `input.${goField} == "${cell.value}"`;
|
|
366
|
+
case 'negated':
|
|
367
|
+
if (condType === 'bool') return `input.${goField} != ${cell.value}`;
|
|
368
|
+
return `input.${goField} != "${cell.value}"`;
|
|
369
|
+
case 'set': {
|
|
370
|
+
const checks = cell.values.map(v =>
|
|
371
|
+
condType === 'bool' ? `input.${goField} == ${v}` : `input.${goField} == "${v}"`
|
|
372
|
+
).join(' || ');
|
|
373
|
+
return `(${checks})`;
|
|
374
|
+
}
|
|
375
|
+
default:
|
|
376
|
+
return '';
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
export function compileDecisionTableToGo(dt: DecisionTableDef): string {
|
|
381
|
+
const inputTypeName = `${toPascalCase(dt.name)}Input`;
|
|
382
|
+
const outputTypeName = `${toPascalCase(dt.name)}Output`;
|
|
383
|
+
const fnName = `Evaluate${toPascalCase(dt.name)}`;
|
|
384
|
+
|
|
385
|
+
const lines: string[] = [];
|
|
386
|
+
|
|
387
|
+
lines.push(`// ${inputTypeName} defines the input conditions for the ${dt.name} decision table`);
|
|
388
|
+
lines.push(`type ${inputTypeName} struct {`);
|
|
389
|
+
for (const cond of dt.conditions) {
|
|
390
|
+
const goType = generateGoType(cond.type);
|
|
391
|
+
const fieldName = toPascalCase(cond.name);
|
|
392
|
+
const comment = cond.type === 'enum' && cond.values.length > 0
|
|
393
|
+
? ` // ${cond.values.join(', ')}`
|
|
394
|
+
: '';
|
|
395
|
+
lines.push(`\t${fieldName} ${goType}${comment}`);
|
|
396
|
+
}
|
|
397
|
+
lines.push('}');
|
|
398
|
+
lines.push('');
|
|
399
|
+
|
|
400
|
+
lines.push(`// ${outputTypeName} defines the decision outputs for the ${dt.name} decision table`);
|
|
401
|
+
lines.push(`type ${outputTypeName} struct {`);
|
|
402
|
+
for (const action of dt.actions) {
|
|
403
|
+
const goType = generateGoType(action.type);
|
|
404
|
+
const fieldName = toPascalCase(action.name);
|
|
405
|
+
const comment = action.type === 'enum' && action.values && action.values.length > 0
|
|
406
|
+
? ` // ${action.values.join(', ')}`
|
|
407
|
+
: '';
|
|
408
|
+
lines.push(`\t${fieldName} ${goType}${comment}`);
|
|
409
|
+
}
|
|
410
|
+
lines.push('}');
|
|
411
|
+
lines.push('');
|
|
412
|
+
|
|
413
|
+
const policy = dt.policy ?? 'first-match';
|
|
414
|
+
lines.push(`// ${fnName} evaluates the ${dt.name} decision table (${policy} policy)`);
|
|
415
|
+
lines.push(`func ${fnName}(input ${inputTypeName}) *${outputTypeName} {`);
|
|
416
|
+
|
|
417
|
+
for (let ruleIdx = 0; ruleIdx < dt.rules.length; ruleIdx++) {
|
|
418
|
+
const rule = dt.rules[ruleIdx];
|
|
419
|
+
const ruleNum = rule.number ?? ruleIdx + 1;
|
|
420
|
+
|
|
421
|
+
const checks: string[] = [];
|
|
422
|
+
for (const [condName, cell] of rule.conditions) {
|
|
423
|
+
const condDef = dt.conditions.find(c => c.name === condName);
|
|
424
|
+
const condType = condDef?.type ?? 'string';
|
|
425
|
+
const check = generateGoConditionCheck(condName, condType, cell);
|
|
426
|
+
if (check) checks.push(check);
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
const actionFields = dt.actions
|
|
430
|
+
.map(a => {
|
|
431
|
+
const value = rule.actions.get(a.name);
|
|
432
|
+
if (value === undefined) return null;
|
|
433
|
+
const fieldName = toPascalCase(a.name);
|
|
434
|
+
if (a.type === 'bool') return `${fieldName}: ${value}`;
|
|
435
|
+
return `${fieldName}: "${value}"`;
|
|
436
|
+
})
|
|
437
|
+
.filter((v): v is string => v !== null)
|
|
438
|
+
.join(', ');
|
|
439
|
+
|
|
440
|
+
lines.push(`\t// Rule ${ruleNum}`);
|
|
441
|
+
if (checks.length === 0) {
|
|
442
|
+
lines.push(`\treturn &${outputTypeName}{${actionFields}}`);
|
|
443
|
+
} else {
|
|
444
|
+
lines.push(`\tif ${checks.join(' && ')} {`);
|
|
445
|
+
lines.push(`\t\treturn &${outputTypeName}{${actionFields}}`);
|
|
446
|
+
lines.push(`\t}`);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
lines.push('\treturn nil // no rule matched');
|
|
451
|
+
lines.push('}');
|
|
452
|
+
|
|
453
|
+
return lines.join('\n');
|
|
454
|
+
}
|