import { NFError } from "../error"; import { NFExpression } from "../expression"; import { NFProcess } from "../process"; import { NFNodeType } from "../types"; import { NFNode } from "./node"; /** * 顺序流:连接源节点与目标节点,可选条件或默认流标记。 */ export class NFSequenceNode extends NFNode{ /** * 表达式处理器 */ expr:NFExpression; /** * 源节点 */ src:string; /** * 目标节点 */ dst:string; /** * 是否为默认顺序流(排他/包容网关用) */ defaultFlow:boolean = false; /** * 创建顺序流。 * * @param cfg - 需含 `src` / `dst`,可选 `cond`、`default` * @param process - 所属流程 */ constructor(cfg,process:NFProcess){ super(cfg,process); this.src = cfg.src; this.dst = cfg.dst; this.type = NFNodeType.SEQUENCE; this.defaultFlow = !!cfg.default; if(cfg.cond){ this.expr = new NFExpression(cfg.cond); } } /** * 判断条件是否满足。无条件视为 `true`;默认流在此处恒为 `false`。 * * @returns 是否匹配 */ match():boolean{ if(this.defaultFlow){ return false; } if(!this.expr){ return true; } return !!this.expr.val(this.process.getParam()); } /** * 是否默认顺序流(排他 / 包容网关回退用)。 * * @returns 是否默认流 */ isDefaultFlow():boolean{ return this.defaultFlow; } /** * 执行顺序流:条件满足、强制执行或进入汇聚网关时进入目标节点。 * * @param force - 为 `true` 时忽略条件(网关已选定出口) * @returns 是否进入目标节点 * @throws `1100` 当目标节点不存在 */ async run(force?:boolean):Promise{ const node = this.process.nodeManager.getNodeById(this.dst); if(!node){ throw new NFError("1100"); } // 并行/包容汇聚:令牌到达即进入,不再二次判条件 const joinGateway = node.type === NFNodeType.PARALLEL || node.type === NFNodeType.INCLUSIVE; if(force || joinGateway || this.match()){ await node.run(); return true; } return false; } }