开源工作流Fireflow源码分析之启动流程实例二



KernelManager.java

 /**
     * 在获取工作流网实例的时候,调用createNetInstance方法,创建实例
     * @param processId  流程定义ID
     * @param version    流程版本号
     * @return
     * @throws KernelException
     */
    public INetInstance getNetInstance(String processId, Integer version) throws KernelException {
        INetInstance netInstance = this.netInstanceMap.get(processId + "_V_" + version);
        if (netInstance == null) {
        	//数据流定义在runtimeContext初始化的时候,就被加载了,将流程定义的xml读入到内存中
            WorkflowDefinition def = rtCtx.getDefinitionService().getWorkflowDefinitionByProcessIdAndVersionNumber(processId, version);
            netInstance = this.createNetInstance(def);
        }
        return netInstance;
}



 /**
     * 创建一个工作流网实例
     * @param workflowDef
     * @return
     * @throws KernelException
     */
    public INetInstance createNetInstance(WorkflowDefinition workflowDef) throws KernelException {
        if (workflowDef==null)return null;
        WorkflowProcess workflowProcess = null;
        //解析fpdl
        workflowProcess = workflowDef.getWorkflowProcess();

        if (workflowProcess == null ){
        	throw new KernelException(null,null,"The WorkflowProcess property of WorkflowDefinition[processId="+workflowDef.getProcessId()+"] is null. ");
        }
        //校验工作流定义是否有效
        String validateMsg =  workflowProcess.validate();
        if (validateMsg != null){
        	throw new KernelException(null,null,validateMsg);
        }
        NetInstance netInstance = new NetInstance(workflowProcess, kernelExtensions);
        //设置版本号
        netInstance.setVersion(workflowDef.getVersion());
        //map的key的组成规则:流程定义ID_V_版本号
        netInstanceMap.put(workflowDef.getProcessId() + "_V_" + workflowDef.getVersion(), netInstance);
        return netInstance;
    }


    /**
     * wangmj  初始化一个工作流网实例,将引擎的扩展属性,注入到对应的工作流元素中
     * @param process
     * @param kenelExtensions
     * @throws KernelException
     */
    public NetInstance(WorkflowProcess process, final Map<String, List<IKernelExtension>> kenelExtensions) throws KernelException{
        this.workflowProcess = process;
        
        //开始节点
        StartNode startNode = workflowProcess.getStartNode();
        //生成一个开始节点实例
        startNodeInstance = new StartNodeInstance(startNode);
        //得到开始节点的事件监听器,系统都有默认的事件监听器,也可以进行增加,增加的事件监听器在要spring中定义,
        //在开源工作流Fireflow源码分析之事件驱动.doc中有进行说明
        List<IKernelExtension> extensionList = kenelExtensions.get(startNodeInstance.getExtensionTargetName());
        for (int i = 0; extensionList != null && i < extensionList.size(); i++) {
            IKernelExtension extension = extensionList.get(i);
            //注册开始节点实例的事件监听器
            startNodeInstance.registExtension(extension);
        }
        //在工作流网实例中设置开始节点
        this.setStartNodeInstance(startNodeInstance);
        //将开始节点实例放到工作流网的实例Map中,方便以后的查找
        wfElementInstanceMap.put(startNode.getId(), startNodeInstance);

        //生成环节节点
        //取到流程模型中定义的所有环节,并依次进行loop
        List<Activity> activities = workflowProcess.getActivities();
        for (int i = 0; i < activities.size(); i++) {
            Activity activity = activities.get(i);
            //实例化环节
            ActivityInstance activityInstance = new ActivityInstance(activity);
            //注册环节的事件监听器
            extensionList = kenelExtensions.get(activityInstance.getExtensionTargetName());
            for (int j = 0; extensionList != null && j < extensionList.size(); j++) {
                IKernelExtension extension = extensionList.get(j);
                activityInstance.registExtension(extension);
            }
            //将实例化环节放入到Map中
            wfElementInstanceMap.put(activity.getId(), activityInstance);
        }
        //生成同步器节点
        //取到流程模型中定义的所有同步器,并依次进行loop
        List<Synchronizer> synchronizers = workflowProcess.getSynchronizers();
        for (int i = 0; i < synchronizers.size(); i++) {
            Synchronizer synchronizer = synchronizers.get(i);
            //实例化一个同步器节点
            SynchronizerInstance synchronizerInstance = new SynchronizerInstance(synchronizer);
            //注册同步器的事件监听器
            extensionList = kenelExtensions.get(synchronizerInstance.getExtensionTargetName());
            for (int j = 0; extensionList != null && j < extensionList.size(); j++) {
                IKernelExtension extension = extensionList.get(j);
                synchronizerInstance.registExtension(extension);
            }
            //将同步器节点放入Map里
            wfElementInstanceMap.put(synchronizer.getId(), synchronizerInstance);
        }
        //生成结束节点
      //取到流程模型中定义的所有结束节点,并依次进行loop
        List<EndNode> endNodes = workflowProcess.getEndNodes();

        for (int i = 0; i < endNodes.size(); i++) {
            EndNode endNode = endNodes.get(i);
            //实例化一个节始结点
            EndNodeInstance endNodeInstance = new EndNodeInstance(endNode);
            //注册结束结点的事件监听器
            extensionList = kenelExtensions.get(endNodeInstance.getExtensionTargetName());
            for (int j = 0; extensionList != null && j < extensionList.size(); j++) {
                IKernelExtension extension = extensionList.get(j);
                endNodeInstance.registExtension(extension);
            }
            //将结束节点放入Map里
            wfElementInstanceMap.put(endNode.getId(), endNodeInstance);
        }
        //转移线
        //取到流程模型中定义的所有转移线,并依次进行loop
        List<Transition> transitions = workflowProcess.getTransitions();
        for (int i = 0; i < transitions.size(); i++) {
            Transition transition = transitions.get(i);
            //实例化一个转移线
            TransitionInstance transitionInstance = new TransitionInstance(transition);
            //取到转移线的from,
            String fromNodeId = transition.getFromNode().getId();
            if (fromNodeId != null) {
            	//从map中取到转移线进入节点的实例
                INodeInstance enteringNodeInstance = (INodeInstance) wfElementInstanceMap.get(fromNodeId);
                //如果实例不为null
                if (enteringNodeInstance != null) {
                	//设置转移线进入节点实例的离开转移线
                    enteringNodeInstance.addLeavingTransitionInstance(transitionInstance);
                    //设置转移线的进入节点
                    transitionInstance.setEnteringNodeInstance(enteringNodeInstance);
                }
            }
            //设置转移线的离开节点
            //取到转移线的to
            String toNodeId = transition.getToNode().getId();
            if (toNodeId != null) {
            	//从map中取到转移线离开节点实例
                INodeInstance leavingNodeInstance = (INodeInstance) wfElementInstanceMap.get(toNodeId);
                if (leavingNodeInstance != null) {
                	//设置转移线离开节点的进入转移线
                    leavingNodeInstance.addEnteringTransitionInstance(transitionInstance);
                    //设置转移线的离开节点
                    transitionInstance.setLeavingNodeInstance(leavingNodeInstance);
                }
            }
            //注册转移线事件监听器
            extensionList = kenelExtensions.get(transitionInstance.getExtensionTargetName());
            for (int j = 0; extensionList != null && j < extensionList.size(); j++) {
                IKernelExtension extension = extensionList.get(j);
                transitionInstance.registExtension(extension);
            }
            //将转移线放入Map中
            wfElementInstanceMap.put(transitionInstance.getId(), transitionInstance);
        }
        //循环线
      //取到流程模型中定义的所有循环线,并依次进行loop
        List<Loop> loops = workflowProcess.getLoops();
        for (int i = 0; i < loops.size(); i++) {
            Loop loop = loops.get(i);
            //实例化循环线
            LoopInstance loopInstance = new LoopInstance(loop);
            //取循环线from 
            String fromNodeId = loop.getFromNode().getId();
            if (fromNodeId != null) {
            	//从map中取到循环线进入节点的实例
                INodeInstance enteringNodeInstance = (INodeInstance) wfElementInstanceMap.get(fromNodeId);
                if (enteringNodeInstance != null) {
                	//设置循环线的进入节点的循环线
                    enteringNodeInstance.addLeavingLoopInstance(loopInstance);
                    //设置循环线的进入节点
                    loopInstance.setEnteringNodeInstance(enteringNodeInstance);
                }
            }
            //取循环线to
            String toNodeId = loop.getToNode().getId();
            if (toNodeId != null) {
            	//从map中取到循环线离开节点的实例
                INodeInstance leavingNodeInstance = (INodeInstance) wfElementInstanceMap.get(toNodeId);
                if (leavingNodeInstance != null) {
                	//设置循环线的离开节点的循环线
                    leavingNodeInstance.addEnteringLoopInstance(loopInstance);
                    //设置循环线的离开节点
                    loopInstance.setLeavingNodeInstance(leavingNodeInstance);
                }
            }
            //注册循环线的事件监听器
            extensionList = kenelExtensions.get(loopInstance.getExtensionTargetName());
            for (int j = 0; extensionList != null && j < extensionList.size(); j++) {
                IKernelExtension extension = extensionList.get(j);
                loopInstance.registExtension(extension);
            }
            wfElementInstanceMap.put(loopInstance.getId(), loopInstance);
        }
    }

转载于:https://www.cnblogs.com/mzhanker/archive/2011/06/04/2072834.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值