|
| 1 | +function toContractJson(model) { |
| 2 | + const processes = (model.body || []).filter((node) => node.type === "process"); |
| 3 | + const stateflows = (model.body || []).filter((node) => node.type === "stateflow"); |
| 4 | + |
| 5 | + if (processes.length === 0 && stateflows.length === 0) { |
| 6 | + throw new Error( |
| 7 | + "No contract-exportable blocks found. Supported block types: process, stateflow." |
| 8 | + ); |
| 9 | + } |
| 10 | + |
| 11 | + const contract = { |
| 12 | + version: "0.1", |
| 13 | + type: "contract", |
| 14 | + processes: processes.map(toProcessContract), |
| 15 | + stateflows: stateflows.map(toStateflowContract), |
| 16 | + }; |
| 17 | + |
| 18 | + return `${JSON.stringify(contract, null, 2)}\n`; |
| 19 | +} |
| 20 | + |
| 21 | +function toProcessContract(processNode) { |
| 22 | + const triggers = (processNode.body || []) |
| 23 | + .filter((statement) => statement.type === "when") |
| 24 | + .map((statement) => statement.trigger || "unknown"); |
| 25 | + |
| 26 | + const steps = flattenStatements(processNode.body || []); |
| 27 | + |
| 28 | + return { |
| 29 | + name: processNode.name, |
| 30 | + triggers, |
| 31 | + steps: steps.map((step) => ({ |
| 32 | + kind: step.kind, |
| 33 | + label: step.label, |
| 34 | + condition: step.condition || null, |
| 35 | + })), |
| 36 | + }; |
| 37 | +} |
| 38 | + |
| 39 | +function toStateflowContract(stateflow) { |
| 40 | + return { |
| 41 | + name: stateflow.name, |
| 42 | + states: stateflow.states || [], |
| 43 | + transitions: (stateflow.transitions || []).map((edge) => ({ |
| 44 | + from: edge.from, |
| 45 | + to: edge.to, |
| 46 | + })), |
| 47 | + }; |
| 48 | +} |
| 49 | + |
| 50 | +function flattenStatements(statements) { |
| 51 | + const steps = []; |
| 52 | + |
| 53 | + for (const statement of statements) { |
| 54 | + if (statement.type === "when") { |
| 55 | + steps.push({ |
| 56 | + kind: "when", |
| 57 | + label: `when ${statement.trigger || "unknown"}`, |
| 58 | + }); |
| 59 | + continue; |
| 60 | + } |
| 61 | + |
| 62 | + if (statement.type === "if") { |
| 63 | + const condition = formatCondition(statement.condition); |
| 64 | + steps.push({ |
| 65 | + kind: "if", |
| 66 | + label: `if ${condition}`, |
| 67 | + condition, |
| 68 | + }); |
| 69 | + steps.push(...flattenStatements(statement.then || [])); |
| 70 | + for (const branch of statement.elseIf || []) { |
| 71 | + const branchCondition = formatCondition(branch.condition); |
| 72 | + steps.push({ |
| 73 | + kind: "else-if", |
| 74 | + label: `else if ${branchCondition}`, |
| 75 | + condition: branchCondition, |
| 76 | + }); |
| 77 | + steps.push(...flattenStatements(branch.then || [])); |
| 78 | + } |
| 79 | + if (statement.else && (statement.else.body || []).length > 0) { |
| 80 | + steps.push({ kind: "else", label: "else", condition: null }); |
| 81 | + steps.push(...flattenStatements(statement.else.body || [])); |
| 82 | + } |
| 83 | + continue; |
| 84 | + } |
| 85 | + |
| 86 | + steps.push({ |
| 87 | + kind: statement.type, |
| 88 | + label: formatAction(statement), |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + return steps; |
| 93 | +} |
| 94 | + |
| 95 | +function formatAction(statement) { |
| 96 | + if (statement.type === "assign") { |
| 97 | + return `assign ${statement.target || "?"} = ${formatExpression(statement.value)}`; |
| 98 | + } |
| 99 | + |
| 100 | + if (statement.type === "transition") { |
| 101 | + return `transition ${statement.target || "?"} to ${formatExpression(statement.value)}`; |
| 102 | + } |
| 103 | + |
| 104 | + if (statement.type === "notify") { |
| 105 | + return `notify ${statement.target} "${statement.message}"`; |
| 106 | + } |
| 107 | + |
| 108 | + if (statement.type === "create") { |
| 109 | + return `create ${statement.entity}`; |
| 110 | + } |
| 111 | + |
| 112 | + if (statement.type === "update") { |
| 113 | + return `update ${statement.target || "?"} = ${formatExpression(statement.value)}`; |
| 114 | + } |
| 115 | + |
| 116 | + if (statement.type === "require") { |
| 117 | + return `require ${statement.requirement}`; |
| 118 | + } |
| 119 | + |
| 120 | + if (statement.type === "stop") { |
| 121 | + return "stop"; |
| 122 | + } |
| 123 | + |
| 124 | + return statement.type; |
| 125 | +} |
| 126 | + |
| 127 | +function formatCondition(condition) { |
| 128 | + if (!condition) { |
| 129 | + return "unknown condition"; |
| 130 | + } |
| 131 | + |
| 132 | + if (condition.type === "logical") { |
| 133 | + return condition.conditions.map(formatCondition).join(` ${condition.operator} `); |
| 134 | + } |
| 135 | + |
| 136 | + return `${formatExpression(condition.left)} ${condition.operator} ${formatExpression(condition.right)}`; |
| 137 | +} |
| 138 | + |
| 139 | +function formatExpression(expression) { |
| 140 | + if (!expression) { |
| 141 | + return "?"; |
| 142 | + } |
| 143 | + |
| 144 | + if (expression.type === "field") { |
| 145 | + return expression.path; |
| 146 | + } |
| 147 | + |
| 148 | + if (expression.type === "identifier") { |
| 149 | + return expression.value; |
| 150 | + } |
| 151 | + |
| 152 | + if (expression.type === "string") { |
| 153 | + return `"${expression.value}"`; |
| 154 | + } |
| 155 | + |
| 156 | + if (expression.type === "boolean") { |
| 157 | + return expression.value ? "true" : "false"; |
| 158 | + } |
| 159 | + |
| 160 | + return String(expression.value); |
| 161 | +} |
| 162 | + |
| 163 | +module.exports = { |
| 164 | + toContractJson, |
| 165 | +}; |
0 commit comments