import { NFNode } from "./node"; import { NFNodeCfg, NFNodeType } from "../types"; import { NFSequenceNode } from "./sequencenode"; import { NFProcess } from "../process"; import { NFError } from "../error"; /** * 排他网关(XOR) * - 分叉:按顺序取第一条条件为真的出口;若皆不满足则走 default / 无条件出口 * - 汇聚:任一入边到达即通过(不同步等待) */ export class NFExclusiveNode extends NFNode{ /** * 出口顺序流 */ outSequences:NFSequenceNode[]; /** * @param cfg - 节点配置 * @param process - 所属流程 */ constructor(cfg:NFNodeCfg,process:NFProcess){ super(cfg,process); this.type = NFNodeType.EXCLUSIVE; } /** * 按条件选一条出口执行。 * * @throws `1101` 当无匹配出口且无默认 / 无条件流 */ async run() { await super.run(); let selected:NFSequenceNode = null; let defaultSeq:NFSequenceNode = null; for(let seq of this.outSequences){ if(seq.isDefaultFlow()){ defaultSeq = defaultSeq || seq; continue; } if(seq.match()){ selected = seq; break; } } // 无匹配时:显式 default > 无条件出口 if(!selected){ selected = defaultSeq || this.outSequences.find(s=>!s.expr && !s.isDefaultFlow()); } if(!selected){ throw new NFError("1101",[this.name || this.id]); } await selected.run(true); } /** * 绑定出口顺序流。 * * @throws `1101` 当没有出口 */ init(){ this.outSequences = this.process.nodeManager.getSequenceNodes(this.id); if(!this.outSequences || this.outSequences.length === 0){ throw new NFError("1101",[this.name || this.id]); } } }