This repository has been archived on 2026-09-13. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
BPOS-MVP/apps/web/src/shared/gov/flow/pkg-to-graph.ts
T

153 lines
5.9 KiB
TypeScript

/**
* 流程包 → 画布图模型:**连线即映射**的可视一侧。
*
* 画布件(`flow-layout` / `flow-node-paint` / `FlowDiagram`)是框架无关的纯几何与画笔,
* 它认的是 `FlowGraph`;我们的权威模型是 `ProcessPackage`。这里做且只做一次形状转换,
* **不产生任何新语义** —— 节点仍是编排层五要素,审批点仍是系统内置人裁节点(共用方框几何而已)。
*/
import type { ProcessPackage } from '@contracts';
import type { FlowEdge, FlowGraph, FlowNode } from './flow-layout.ts';
/** 节点实例落位:running / waiting / terminal 决定描边高亮,不改几何 */
export type InstanceMark = {
readonly nodeId: string;
readonly running: boolean;
readonly waiting: boolean;
readonly activated: boolean;
readonly terminal?: string | undefined;
};
/** 每行最多几个方框 —— 蛇形折行的行宽;取原型 business 预设的观感 */
const ROW_WIDTH = 4;
export function packageToGraph(
pkg: ProcessPackage,
marks: readonly InstanceMark[] = [],
/** 活动范式(activityId → loop|flow):案例活动画绿 —— 范式住活动库,包里只有引用 */
paradigms?: ReadonlyMap<string, string>,
): FlowGraph {
const markOf = new Map(marks.map((m) => [m.nodeId, m]));
/*
* 手绘图存证优先:编辑器保存过,**手绘几何即呈现真相**。
* 运行标注仍要每次现算(它是运行态,不入存证),合到存证节点的 note 上。
*/
const stored = readStoredDiagram(pkg.diagram);
if (stored !== undefined) {
return {
...stored,
nodes: stored.nodes.map((n) => {
const state = stateOf(markOf.get(n.id));
return state === undefined ? n : { ...n, note: state };
}),
};
}
const nodes: FlowNode[] = pkg.nodes.map((n) => {
const state = stateOf(markOf.get(n.id));
if (n.kind === 'gateway') {
return {
id: n.id,
kind: 'gateway',
label: n.label,
// 并行网关画 +,异或网关画 ×(判断)
mode: n.gateway === 'parallel' ? 'and' : 'x',
...(n.sub === undefined ? (n.mode === 'join' ? { sub: '汇合' } : { sub: '分叉' }) : { sub: n.sub }),
...(n.basis === undefined ? {} : { basis: n.basis }),
...(state === undefined ? {} : { note: state }),
};
}
if (n.kind === 'start') return { id: n.id, kind: 'start', label: n.label };
if (n.kind === 'end') return { id: n.id, kind: 'end', label: n.label };
const base: FlowNode = {
id: n.id,
kind: 'activity',
label: n.label,
code: n.id,
...(n.sub === undefined ? {} : { sub: n.sub }),
...(state === undefined ? {} : { note: state }),
};
if (n.kind === 'approval') {
// approval **不是一种活动**:它挂在 kind:"activity" 下仅因共用同一套方框几何
return { ...base, flavor: 'approval', owner: n.approver.position };
}
return {
...base,
owner: n.owner.position,
...(paradigms?.get(n.activityId) === 'loop' ? { flavor: 'case' as const } : {}),
...(n.in === undefined ? {} : { in: [...n.in] }),
...(n.out === undefined ? {} : { out: [...n.out] }),
};
});
/** 蛇形折行:偶数行 L→R、奇数行 R→L —— 行的划分交给拓扑序 */
const order = topologicalOrder(pkg);
const rows: string[][] = [];
for (let i = 0; i < order.length; i += ROW_WIDTH) rows.push(order.slice(i, i + ROW_WIDTH));
const edges: FlowEdge[] = pkg.edges.map((e) => ({
from: e.from,
to: e.to,
// 回边按声明走「回 A4 重开新实例」,视觉上标红以区分正向流转
...(e.tone !== undefined ? { tone: e.tone } : e.reopen ? { tone: 'bad' as const } : {}),
...(e.label === undefined ? (e.condition === undefined ? {} : { label: e.condition }) : { label: e.label }),
}));
return { nodes, rows, anchors: [], edges };
}
/**
* 手绘图存证的在场校验:`diagram` 由本仓 `graph-to-pkg` 序列化而来,结构即 FlowGraph;
* 这里只验骨架在不在(nodes/edges 数组、非空),不逐字段复检 —— 写入侧是唯一生产者。
*/
function readStoredDiagram(d: Record<string, unknown> | undefined): FlowGraph | undefined {
if (d === undefined) return undefined;
const nodes = d['nodes'];
const edges = d['edges'];
if (!Array.isArray(nodes) || nodes.length === 0 || !Array.isArray(edges)) return undefined;
return {
nodes,
edges,
rows: Array.isArray(d['rows']) ? d['rows'] : [],
anchors: Array.isArray(d['anchors']) ? d['anchors'] : [],
manual: d['manual'] === true,
} as FlowGraph;
}
/** 运行标注:节点实例态 → 画布角注文字;未激活不标 */
function stateOf(mark: InstanceMark | undefined): string | undefined {
if (mark === undefined || !mark.activated) return undefined;
if (mark.terminal !== undefined) return mark.terminal === 'released' ? '已放行' : '已终止';
return mark.running ? '运行中' : mark.waiting ? '等待中' : '已激活';
}
/** 拓扑序:回边不参与定序(否则成环),同层按声明顺序稳定排列 */
function topologicalOrder(pkg: ProcessPackage): string[] {
const ids = pkg.nodes.map((n) => n.id as string);
const forward = pkg.edges.filter((e) => !e.reopen);
const indegree = new Map<string, number>(ids.map((id) => [id, 0]));
for (const e of forward) indegree.set(e.to, (indegree.get(e.to) ?? 0) + 1);
const ready = ids.filter((id) => indegree.get(id) === 0);
const out: string[] = [];
const seen = new Set<string>();
while (ready.length > 0) {
const id = ready.shift()!;
if (seen.has(id)) continue;
seen.add(id);
out.push(id);
for (const e of forward.filter((x) => x.from === id)) {
const left = (indegree.get(e.to) ?? 0) - 1;
indegree.set(e.to, left);
if (left === 0) ready.push(e.to);
}
}
// 环(理论上闭合校验已挡住)或孤点:兜底补齐,画布不因数据异常而空白
for (const id of ids) if (!seen.has(id)) out.push(id);
return out;
}