流程图状态机DEMO(附件wf-statemachine-demo.rar下载)
这是我写的一个DEOM,目的在于对?
?
的一个流程支持而写的状态机DEMO
?
表达式使用IK EXPRESSION,以后会支持EL,也可能会支持JS,或者其他的脚本语言
?
没有测试,默认实现还有错误,以后会更新稳定版本的
?
下面是其中的部分代码:
?
?
package keng.core.workflow.state.impl;import java.util.List;import java.util.Stack;import java.util.concurrent.TimeoutException;import keng.core.util.StringUtil;import keng.core.workflow.state.ISMActionProcessor;import keng.core.workflow.state.ISMCondition;import keng.core.workflow.state.ISMContext;import keng.core.workflow.state.ISMNode;import keng.core.workflow.state.ISMTransition;import keng.core.workflow.state.ISMTransitionMatcher;import keng.core.workflow.state.IStateMachine;import keng.core.workflow.state.StateMachineException;/** * 默认的状态机实现 * * @author 5Di * */public class DefaultStateMachine implements IStateMachine {Stack<ISMNode> register = new Stack<ISMNode>();ISMTransitionMatcher matcher;ISMContext context;/** * 根节点 */ISMNode root;boolean strict = false;public DefaultStateMachine(ISMNode root, ISMContext context) {this.context = context;this.root = root;}public DefaultStateMachine(ISMNode root, ISMContext context,ISMTransitionMatcher matcher) {this.root = root;this.context = context;this.matcher = matcher;}public DefaultStateMachine(ISMNode root, ISMContext context,ISMTransitionMatcher matcher, boolean strict) {this.context = context;this.root = root;this.matcher = matcher;this.strict = strict;}@Overridepublic boolean transit(String action, String state, ISMNode node)throws StateMachineException, TimeoutException {if (StringUtil.isEmpty(action))throw new IllegalArgumentException("action can not be empty!");if (StringUtil.isEmpty(state))throw new IllegalArgumentException("state can not be empty!");if (node == null)throw new IllegalArgumentException("node can not be null!");List<ISMTransition> transitions = node.getTransitions();if (transitions == null || transitions.isEmpty())throw new StateMachineException("transitions can not be empty !");// check che nodeif (checkNodeMap(node))return false;// Match the transitionISMTransition transition = matchTransition(action, state, transitions);boolean b = false;ISMCondition condition = transition.getCondition();if (condition == null)if (!strict)b = true;elsethrow new StateMachineException("transition's condition can not be null!");b = condition.calculate(context, strict);if (!b)throw new TimeoutException("no transition was matched!");// process actionISMActionProcessor processor = transition.getActionProcessor();processor.process();register.push(node);// auto process next auto stepsList<ISMNode> children = node.getChildren();if (children != null && !children.isEmpty()) {for (ISMNode child : children) {transit(action, state, child);}}node.setState(transition.getToState());return true;}/** * 匹配transition <br /> * 使用<code>keng.core.workflow.state.ISMTransitionMatcher.match</code>匹配 * * @param action * @param state * @param transitions * @return */ISMTransition matchTransition(String action, String state,List<ISMTransition> transitions) {if (matcher == null)matcher = new DefaultSMTransitionMatcher();transitions = matcher.sort(transitions);int i, len = transitions.size();for (i = 0; i < len; i++) {ISMTransition transition = transitions.get(i);if (matcher.match(action, state, transition))return transition;}return null;}/** * 检查节点图,是否进入了死循环<br /> * 目前还不完备,只是检查了已经执行过的node * * @param node * @return 如果已经执行,返回true */public boolean checkNodeMap(ISMNode node) {return register.indexOf(node) != -1;}}
?
?
目前还处于DEMO 状态,如果大家有想要说的,尽管拍砖,共同进步
最后,给大家提供两个工作流方面的资料
《工作流模型分析》和《工作流管理联盟规范》
?