开源工作流Fireflow源码分析之运行流程二

TranstionInstance.java

 /* 触发弧线

     * @see org.fireflow.kernel.IEdgeInstance#take(org.fireflow.kernel.IToken)

     */

    public boolean take(IToken token) throws KernelException {

    	//触发弧线taking事件

        EdgeInstanceEvent e = new EdgeInstanceEvent(this);

        e.setToken(token);

        e.setEventType(EdgeInstanceEvent.ON_TAKING_THE_TOKEN);



        for (int i = 0; this.eventListeners != null && i < this.eventListeners.size(); i++) {

            IEdgeInstanceEventListener listener =  this.eventListeners.get(i);

            //调用TransitionInstanceExtension监听器来计算弧线上的条件表达式,在后面有说明

            listener.onEdgeInstanceEventFired(e); 

        }

        //获取到流向哪个节点     

        INodeInstance nodeInst = this.getLeavingNodeInstance(); 

        //设置弧线上的权值,对于同步器到环节,权值没有什么作用,而对于activity到同步器,权值作用于同步器的汇聚算法

        token.setValue(this.getWeight());

        boolean alive = token.isAlive();

        //下一个节点触发,

//如果是同步器到activity

//详看是开源工作流Fireflow源码分析之ActivityInstance fired

//如果是activity到同步器

//详看是开源工作流Fireflow源码分析之SynchronizerInstance fired

        nodeInst.fire(token);

        return alive;

}

/* 获取转移线上的权值

     * @see org.fireflow.kernel.IEdgeInstance#getWeight()

     */

    public int getWeight() {

        if (weight == 0) {

            if (enteringNodeInstance instanceof StartNodeInstance) {

                weight=1;

                return weight;

                //如果前驱结点是开始节点,那么权值规定为1

            } else if (leavingNodeInstance instanceof EndNodeInstance) {

                weight=1;

                return weight;

                //如果后继结点为结束节点,那么权值规定为1   

            } else if (leavingNodeInstance instanceof ActivityInstance) {

                SynchronizerInstance synchronizerInstance = (SynchronizerInstance) enteringNodeInstance;

                int weight = synchronizerInstance.getVolume() / enteringNodeInstance.getLeavingTransitionInstances().size();

                return weight;//s->t (s)

                //如果弧线的后继结点 是 activity结点,那么弧线的权值=前驱同步器结点的容量/输出弧线的数量

            } else if (leavingNodeInstance instanceof SynchronizerInstance) {

                SynchronizerInstance synchronizerInstance = (SynchronizerInstance) leavingNodeInstance;

                int weight = synchronizerInstance.getVolume() / leavingNodeInstance.getEnteringTransitionInstances().size();

                return weight;

                //如果后继结点是同步器节点,那么权值=同步器的容量/同步器的输入弧线的数量  // t-s

            }

        }



        return weight;

    }

TransitionInstanceExtension.java

 

 /* TransitionInstance的监听器的监听方法

     * @see org.fireflow.kernel.event.IEdgeInstanceEventListener#onEdgeInstanceEventFired(org.fireflow.kernel.event.EdgeInstanceEvent)

     */

    public void onEdgeInstanceEventFired(EdgeInstanceEvent e) throws KernelException {

    	//如果当前事件是taking

        if (e.getEventType() == EdgeInstanceEvent.ON_TAKING_THE_TOKEN) {

            IToken token = e.getToken();

            //取到事件的发生源

            ITransitionInstance transInst = (ITransitionInstance) e.getSource();

            //取到事件的发生条件

            String condition = transInst.getTransition().getCondition();

            //计算条件

            calculateTheAliveValue(token, condition);

            //如果系统设置为允许保存运行轨迹

            if (rtCtx.isEnableTrace() && token.isAlive()) {

                Transition transition = transInst.getTransition();

                IWFElement fromNode = transition.getFromNode();

                int minorNumber = 1;

                //如果是环节到同步器

                if (fromNode instanceof Activity){

                    minorNumber=2;

                }else{

                    minorNumber =1;

                }

                //创建一个运行轨迹实例

                ProcessInstanceTrace trace = new ProcessInstanceTrace();

                //设置流程实例

                trace.setProcessInstanceId(e.getToken().getProcessInstanceId());

                //设置步骤

                trace.setStepNumber(e.getToken().getStepNumber());

                //设置事件的发生类型

                trace.setType(ProcessInstanceTrace.TRANSITION_TYPE);

                //设置from节点

                trace.setFromNodeId(transInst.getTransition().getFromNode().getId());

               //设置to节点

                trace.setToNodeId(transInst.getTransition().getToNode().getId());

                //设置转移张的id

                trace.setEdgeId(transInst.getTransition().getId());

                trace.setMinorNumber(minorNumber);

                rtCtx.getPersistenceService().saveOrUpdateProcessInstanceTrace(trace);

            }

        }



    }

 

  /**

     * 计算value值

     * @param token

     * @param condition

     * @throws EngineException

     */

    public void calculateTheAliveValue(IToken token, String condition)throws EngineException {



        if (!token.isAlive()) {

            return;//如果token是dead状态,表明synchronizer的joinpoint是dead状态,不需要重新计算。

        }



        //1、如果没有转移条件,默认为true

        if (condition == null || condition.trim().equals("")) {

            token.setAlive(true);

            return;

        }

        //2、default类型的不需要计算其alive值,该值由synchronizer决定

        if (condition.trim().equals(ConditionConstant.DEFAULT)) {

            return;

        }



                try{

        //3、计算EL表达式

        //表达式的变量用的是流程变量,表达式是在流程定义的时候就设置好  

	        boolean alive = determineTheAliveOfToken(token.getProcessInstance().getProcessInstanceVariables(), condition);

	        token.setAlive(alive);

        }catch(Exception ex){

        	throw new EngineException(token.getProcessInstanceId(),token.getProcessInstance().getWorkflowProcess(),token.getNodeId(),ex.getMessage());

        }



    }

  /**

     * 执行分支判断策略,即设置token的alive属性 首先,如果this.alive==false,则所有的token的Alive属性皆为false

     * 然后,如果在nexttransitionList中有值,则直接执行列表中的tansition

     * 否则,通过计算Transition的表达式来确定下一个transition,

     * vars:表达式变量

     * condition:表达式

     * @param transInst

     * @return

     */

    private boolean determineTheAliveOfToken(Map<String ,Object> vars, String condition) throws Exception{



        

        //注意这里调用spring中定义的的表达式解析器,

        IConditionResolver elResolver = this.rtCtx.getConditionResolver();

        //通过计算transition上的表达式来确定alive的值

        Boolean b = elResolver.resolveBooleanExpression(vars, condition);



        return b;

    }

你可能感兴趣的:(源码分析)