|
| 1 | +function toGraphJson(model) { |
| 2 | + const nodes = []; |
| 3 | + const edges = []; |
| 4 | + const supported = (model.body || []).filter( |
| 5 | + (node) => node.type === "process" || node.type === "stateflow" |
| 6 | + ); |
| 7 | + |
| 8 | + if (supported.length === 0) { |
| 9 | + throw new Error( |
| 10 | + "No graph-exportable blocks found. Supported block types: process, stateflow." |
| 11 | + ); |
| 12 | + } |
| 13 | + |
| 14 | + supported.forEach((node, index) => { |
| 15 | + if (node.type === "process") { |
| 16 | + renderProcessGraph(node, index + 1, nodes, edges); |
| 17 | + return; |
| 18 | + } |
| 19 | + |
| 20 | + if (node.type === "stateflow") { |
| 21 | + renderStateflowGraph(node, index + 1, nodes, edges); |
| 22 | + } |
| 23 | + }); |
| 24 | + |
| 25 | + return { |
| 26 | + version: "0.1", |
| 27 | + type: "graph", |
| 28 | + nodes, |
| 29 | + edges, |
| 30 | + }; |
| 31 | +} |
| 32 | + |
| 33 | +function renderProcessGraph(node, index, nodes, edges) { |
| 34 | + const groupId = `process:${node.name}`; |
| 35 | + const prefix = `p${index}`; |
| 36 | + nodes.push({ |
| 37 | + id: groupId, |
| 38 | + type: "process", |
| 39 | + label: node.name, |
| 40 | + }); |
| 41 | + |
| 42 | + const builder = createGraphBuilder(prefix, groupId, nodes, edges); |
| 43 | + const startId = builder.addNode("start", node.name || "start"); |
| 44 | + const exits = builder.renderSequence(node.body || [], [{ id: startId }]); |
| 45 | + |
| 46 | + if (exits.length > 0) { |
| 47 | + const endId = builder.addNode("end", "done"); |
| 48 | + builder.connectIncoming(exits, endId); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +function renderStateflowGraph(node, index, nodes, edges) { |
| 53 | + const groupId = `stateflow:${node.name}`; |
| 54 | + nodes.push({ |
| 55 | + id: groupId, |
| 56 | + type: "stateflow", |
| 57 | + label: node.name, |
| 58 | + }); |
| 59 | + |
| 60 | + const prefix = `s${index}`; |
| 61 | + const stateIds = new Map(); |
| 62 | + (node.states || []).forEach((state, stateIndex) => { |
| 63 | + const id = `${prefix}_state_${stateIndex + 1}`; |
| 64 | + stateIds.set(state, id); |
| 65 | + nodes.push({ |
| 66 | + id, |
| 67 | + type: "state", |
| 68 | + label: state, |
| 69 | + group: groupId, |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + (node.transitions || []).forEach((edge) => { |
| 74 | + const from = stateIds.get(edge.from) || `${prefix}_${sanitizeId(edge.from)}`; |
| 75 | + const to = stateIds.get(edge.to) || `${prefix}_${sanitizeId(edge.to)}`; |
| 76 | + edges.push({ |
| 77 | + from, |
| 78 | + to, |
| 79 | + label: "", |
| 80 | + type: "transition", |
| 81 | + group: groupId, |
| 82 | + }); |
| 83 | + }); |
| 84 | +} |
| 85 | + |
| 86 | +function createGraphBuilder(prefix, groupId, nodes, edges) { |
| 87 | + let counter = 0; |
| 88 | + |
| 89 | + function addNode(type, label) { |
| 90 | + counter += 1; |
| 91 | + const id = `${prefix}_${type}_${counter}`; |
| 92 | + nodes.push({ |
| 93 | + id, |
| 94 | + type, |
| 95 | + label, |
| 96 | + group: groupId, |
| 97 | + }); |
| 98 | + return id; |
| 99 | + } |
| 100 | + |
| 101 | + function connectIncoming(connectors, targetId) { |
| 102 | + connectors.forEach((connector) => { |
| 103 | + edges.push({ |
| 104 | + from: connector.id, |
| 105 | + to: targetId, |
| 106 | + label: connector.label || "", |
| 107 | + type: "flow", |
| 108 | + group: groupId, |
| 109 | + }); |
| 110 | + }); |
| 111 | + } |
| 112 | + |
| 113 | + function renderSequence(statements, incoming) { |
| 114 | + let pending = incoming; |
| 115 | + |
| 116 | + for (const statement of statements) { |
| 117 | + pending = renderStatement(statement, pending); |
| 118 | + } |
| 119 | + |
| 120 | + return pending; |
| 121 | + } |
| 122 | + |
| 123 | + function renderStatement(statement, incoming) { |
| 124 | + if (statement.type === "when") { |
| 125 | + const id = addNode("trigger", `when ${statement.trigger || "unknown"}`); |
| 126 | + connectIncoming(incoming, id); |
| 127 | + return [{ id }]; |
| 128 | + } |
| 129 | + |
| 130 | + if (statement.type === "if") { |
| 131 | + const decisionId = addNode("decision", `if ${formatCondition(statement.condition)}`); |
| 132 | + connectIncoming(incoming, decisionId); |
| 133 | + |
| 134 | + const exits = []; |
| 135 | + exits.push( |
| 136 | + ...renderSequence(statement.then || [], [ |
| 137 | + { id: decisionId, label: `if ${formatCondition(statement.condition)}` }, |
| 138 | + ]) |
| 139 | + ); |
| 140 | + |
| 141 | + for (const branch of statement.elseIf || []) { |
| 142 | + exits.push( |
| 143 | + ...renderSequence(branch.then || [], [ |
| 144 | + { id: decisionId, label: `else if ${formatCondition(branch.condition)}` }, |
| 145 | + ]) |
| 146 | + ); |
| 147 | + } |
| 148 | + |
| 149 | + if (statement.else && (statement.else.body || []).length > 0) { |
| 150 | + exits.push(...renderSequence(statement.else.body || [], [{ id: decisionId, label: "else" }])); |
| 151 | + } else { |
| 152 | + exits.push({ id: decisionId, label: "else" }); |
| 153 | + } |
| 154 | + |
| 155 | + const joinId = addNode("merge", "merge"); |
| 156 | + connectIncoming(exits, joinId); |
| 157 | + return [{ id: joinId }]; |
| 158 | + } |
| 159 | + |
| 160 | + if (statement.type === "stop") { |
| 161 | + const endId = addNode("stop", "stop"); |
| 162 | + connectIncoming(incoming, endId); |
| 163 | + return []; |
| 164 | + } |
| 165 | + |
| 166 | + const id = addNode("action", formatAction(statement)); |
| 167 | + connectIncoming(incoming, id); |
| 168 | + return [{ id }]; |
| 169 | + } |
| 170 | + |
| 171 | + return { |
| 172 | + addNode, |
| 173 | + connectIncoming, |
| 174 | + renderSequence, |
| 175 | + }; |
| 176 | +} |
| 177 | + |
| 178 | +function formatAction(statement) { |
| 179 | + if (statement.type === "assign") { |
| 180 | + return `assign ${statement.target || "?"} = ${formatExpression(statement.value)}`; |
| 181 | + } |
| 182 | + |
| 183 | + if (statement.type === "transition") { |
| 184 | + return `transition ${statement.target || "?"} to ${formatExpression(statement.value)}`; |
| 185 | + } |
| 186 | + |
| 187 | + if (statement.type === "notify") { |
| 188 | + return `notify ${statement.target} "${statement.message}"`; |
| 189 | + } |
| 190 | + |
| 191 | + if (statement.type === "create") { |
| 192 | + return `create ${statement.entity}`; |
| 193 | + } |
| 194 | + |
| 195 | + if (statement.type === "update") { |
| 196 | + return `update ${statement.target || "?"} = ${formatExpression(statement.value)}`; |
| 197 | + } |
| 198 | + |
| 199 | + if (statement.type === "require") { |
| 200 | + return `require ${statement.requirement}`; |
| 201 | + } |
| 202 | + |
| 203 | + return statement.type; |
| 204 | +} |
| 205 | + |
| 206 | +function formatCondition(condition) { |
| 207 | + if (!condition) { |
| 208 | + return "unknown condition"; |
| 209 | + } |
| 210 | + |
| 211 | + if (condition.type === "logical") { |
| 212 | + return condition.conditions.map(formatCondition).join(` ${condition.operator} `); |
| 213 | + } |
| 214 | + |
| 215 | + return `${formatExpression(condition.left)} ${condition.operator} ${formatExpression(condition.right)}`; |
| 216 | +} |
| 217 | + |
| 218 | +function formatExpression(expression) { |
| 219 | + if (!expression) { |
| 220 | + return "?"; |
| 221 | + } |
| 222 | + |
| 223 | + if (expression.type === "field") { |
| 224 | + return expression.path; |
| 225 | + } |
| 226 | + |
| 227 | + if (expression.type === "identifier") { |
| 228 | + return expression.value; |
| 229 | + } |
| 230 | + |
| 231 | + if (expression.type === "string") { |
| 232 | + return `"${expression.value}"`; |
| 233 | + } |
| 234 | + |
| 235 | + if (expression.type === "boolean") { |
| 236 | + return expression.value ? "true" : "false"; |
| 237 | + } |
| 238 | + |
| 239 | + return String(expression.value); |
| 240 | +} |
| 241 | + |
| 242 | +function sanitizeId(value) { |
| 243 | + return String(value) |
| 244 | + .replace(/[^A-Za-z0-9_]/g, "_") |
| 245 | + .replace(/^(\d)/, "_$1"); |
| 246 | +} |
| 247 | + |
| 248 | +module.exports = { |
| 249 | + toGraphJson, |
| 250 | +}; |
0 commit comments