import { NFNode } from "./node"; import { NFNodeCfg, NFNodeType } from "../types"; import { NFSequenceNode } from "./sequencenode"; import { NFProcess } from "../process"; import { NFError } from "../error"; /** * 并行网关(AND) * - 分叉:无条件激活全部出口 * - 汇聚:等待全部入边到达后再继续 */ export class NFParallelNode extends NFNode{ /** * 出口顺序流 */ outSequences:NFSequenceNode[]; /** * 入口顺序流 */ inSequences:NFSequenceNode[]; /** * 汇聚剩余待到达数量 */ inCount:number; /** * @param cfg - 节点配置 * @param process - 所属流程 */ constructor(cfg:NFNodeCfg,process:NFProcess){ super(cfg,process); this.type = NFNodeType.PARALLEL; } /** * 汇聚全部入边后无条件激活全部出口。 */ async run() { await super.run(); // 多入边:汇聚,必须等全部到达 if(this.inSequences.length > 1){ if(--this.inCount > 0){ await this.process.saveGatewayNodeState(this.id,{inCount:this.inCount}); return; } await this.process.clearGatewayNodeState(this.id); this.inCount = this.inSequences.length; } // 分叉/继续:无条件执行全部出口 for(let seq of this.outSequences){ await seq.run(true); } } /** * 绑定入边 / 出边并初始化汇聚计数。 * * @throws `1101` 当缺少入边或出边 */ init(){ this.outSequences = this.process.nodeManager.getSequenceNodes(this.id); this.inSequences = this.process.nodeManager.getSequenceNodes(this.id,true); if(!this.outSequences || this.outSequences.length === 0 || !this.inSequences || this.inSequences.length === 0){ throw new NFError("1101",[this.name || this.id]); } this.inCount = this.inSequences.length; } /** * 恢复运行时计数(`getInstance`)。 * * @param state - 剩余待到达数量 */ restoreState(state:{inCount?:number}){ if(state && typeof state.inCount === 'number'){ this.inCount = state.inCount; } } }