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

Netinstance.java

/* 运行流程

     * @see org.fireflow.kernel.INetInstance#run(org.fireflow.engine.IProcessInstance)

     */

    public void run(IProcessInstance processInstance) throws KernelException {

        if (startNodeInstance == null) {

            KernelException exception = new KernelException(processInstance,

                    this.getWorkflowProcess(),

                    "Error:NetInstance is illegal ,the startNodeInstance can NOT be NULL ");

            throw exception;

        }

         

        //实例化一个token

        Token token = new Token();

        //设置token的状态活动的

        token.setAlive(true); 

        //设置流程实例

        token.setProcessInstance(processInstance); 

        //设置token容量

        token.setValue(startNodeInstance.getVolume()); 

        //设置步骤号,开始节点的第一步默认为0

        token.setStepNumber(0); 

        //设置从哪个节点来 "FROM_START_NODE" 规定的节点。

        token.setFromActivityId(IToken.FROM_START_NODE); 

        //启动开始节点

        startNodeInstance.fire(token); 

}

 StartNodeInstance.java

/*

     * 开始节点触发

     * @see org.fireflow.kernel.INodeInstance#fire(org.fireflow.kernel.IToken)

     */

    public void fire(IToken tk) throws KernelException {

    	//如果token不是活动,那么直接返回,注意这个应该是不必要的,因为永远不可能是不是活动的

        if (!tk.isAlive()) {

            return;//

        }

        //检查当前token容易是不是与开始节点一致.这个是否有必要????

        if (tk.getValue() != volume) {

            KernelException exception = new KernelException(tk.getProcessInstance(),

                    this.startNode,

                    "Error:Illegal StartNodeInstance,the tokenValue MUST be equal to the volume ");

            throw exception;



        }

        //在token中设置当前的节点ID

        tk.setNodeId(this.getSynchronizer().getId());

        ////从token中获得流程实例对象

        IProcessInstance processInstance = tk.getProcessInstance();



        //触发enter事件

        NodeInstanceEvent event1 = new NodeInstanceEvent(this);

        event1.setToken(tk);

        event1.setEventType(NodeInstanceEvent.NODEINSTANCE_TOKEN_ENTERED); //token进入

        fireNodeEvent(event1);



        //触发fired事件

        NodeInstanceEvent event2 = new NodeInstanceEvent(this);

        event2.setToken(tk);

        event2.setEventType(NodeInstanceEvent.NODEINSTANCE_FIRED);//token 触发

        fireNodeEvent(event2);



        //触发leaving事件

        NodeInstanceEvent event4 = new NodeInstanceEvent(this);

        event4.setToken(tk);

        event4.setEventType(NodeInstanceEvent.NODEINSTANCE_LEAVING); //token 离开

        fireNodeEvent(event4);



        //激活默认弧线的标志

        boolean activateDefaultCondition = true;

        ITransitionInstance defaultTransInst = null;

        //找到所有开始节点的输出弧 

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

        	//开始节点的边的类型只能是transition

            ITransitionInstance transInst = leavingTransitionInstances.get(i); 

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

            //如果弧线的条件!=null 并且 =“default” ,那么弧线实例就是default的弧线了。

            if (condition != null && condition.equals(ConditionConstant.DEFAULT)) {

            	////记录default转移线,其他条件都未false,才执行它

                defaultTransInst = transInst;

                continue;

            }

            //产生新的token

            Token token = new Token();

            //设置状态为alive

            token.setAlive(true);

            //设置流程实例

            token.setProcessInstance(processInstance);

            //设置token的from节点ID

            token.setFromActivityId(tk.getFromActivityId());

            //步骤号+1

            token.setStepNumber(tk.getStepNumber()+1);

            //触发弧线的token,开源工作流Fireflow源码分析之运行流程二中有更详细的说明

            boolean alive = transInst.take(token);

            //如果有一个有触发,就不走default

            if (alive) {

                activateDefaultCondition = false;

            }



        }

        //如果defaultTransInst!=null ,走的是default值的弧线 

        if (defaultTransInst != null) {

        	 //产生新的token

            Token token = new Token();

            //设置alive状态

            token.setAlive(activateDefaultCondition);

            //设置流程实例

            token.setProcessInstance(processInstance);

            //设置token的from节点ID

            token.setFromActivityId(token.getFromActivityId());

            //步骤号+1

            token.setStepNumber(tk.getStepNumber()+1);

            //触发弧线的token

            defaultTransInst.take(token);

        }

        //触发completed事件

        NodeInstanceEvent event3 = new NodeInstanceEvent(this);

        event3.setToken(tk);

        event3.setEventType(NodeInstanceEvent.NODEINSTANCE_COMPLETED);

        fireNodeEvent(event3);

    }

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