流程圖中的菱形判斷節點,根據運行時上下文動態決定流程走向 (選擇哪條 Transition),是工作流靈活性的關鍵。
若客戶端 (前端/控制器) 已知業務邏輯結果,直接發送 signal 指定目標 Transition 名稱:
// 客戶端代碼
token.signal("approve"); // 或 "reject",直接驅動流程轉向
引擎無需執行決策邏輯,直接按指定 Transition 流轉。
在流程定義文件中內嵌 BeanShell 腳本,腳本必須將選擇的 Transition 名稱賦值給 transitionName 變數。
可用變數:
token:當前執行令牌,可獲取流程變數 token.getVariable("varName")。spottedOnTheRight, beauty)。變數聲明優先級:環境變數定義 > 腳本內聲明 (前者效能更好)。
transitionName = "left"; // 預設路徑
if ( (spottedOnTheRight.equals("woman")) &&
(beauty.equals("in the eye of the beholder")) ) {
transitionName = "right"; // 滿足條件走右邊
}
實現 org.jbpm.graph.def.ActionHandler 介面,在 execute(ExecutionContext) 中編寫 Java 決策邏輯,最終呼叫 executionContext.getToken().signal("transitionName") 或設置 transitionName 變數。
public class MyDecisionHandler implements ActionHandler {
public void execute(ExecutionContext ctx) {
String userRole = (String) ctx.getVariable("userRole");
String transition = "manager".equals(userRole) ? "approve" : "reject";
ctx.getToken().signal(transition); // 直接驅動流轉
}
}
流程定義中引用:<action class="com.myapp.MyDecisionHandler"/>。