应用场景:
大数据组在做成交数据分析时,需要根据当前节点预测下个节点可能走的任务节点,这样如果每次都要从工作流读取流程信息并分析后才知道,为了方便
使用代码解析流程图,分析出流程图任务节点直接的上下层关系,这样,根据当前任务节点就可以获取到该节点下的所有任务节点。
实现效果:
如下图(流程图的部分截图),需要查找资金监管下面所有可能存在的任务节点。
创建用于存储流程节点关系表:
CREATE TABLE `CJ_FLOW_NODE` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`NAME` varchar(255) DEFAULT NULL COMMENT '流程名称',
`FLOWKEY` varchar(255) DEFAULT NULL COMMENT '节点名称',
`TASKKEY` varchar(255) DEFAULT NULL COMMENT '节点key',
`PARENT` varchar(255) DEFAULT NULL COMMENT '父节点key',
`VERSION` int(2) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=1197 DEFAULT CHARSET=utf8;
插入数据库后记录效果图如:
测试代码:
import java.io.InputStream; import java.util.HashSet; import java.util.List; import java.util.Set; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.RepositoryService; import org.activiti.engine.impl.RepositoryServiceImpl; import org.activiti.engine.impl.bpmn.behavior.UserTaskActivityBehavior; import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity; import org.activiti.engine.impl.pvm.PvmActivity; import org.activiti.engine.impl.pvm.PvmTransition; import org.activiti.engine.impl.pvm.delegate.ActivityBehavior; import org.activiti.engine.impl.pvm.process.ActivityImpl; import org.activiti.engine.impl.task.TaskDefinition; import org.activiti.engine.repository.Deployment; import org.junit.Test; import cn.itcast.DataConnection; /** *生成工作流节点之间的上下级关系 *1.部署流程 *2.获取流程实例对象 *3.获取流程所有节点对象,并通过节点id和节点 存储到MAP中, * 供查询时使用; *4.循环所有节点,如果节点为用户任务节点,则查找改节点的所有子节点 * 如果子节点为非任务节点,则查找该节点的以下,知道为用户任务节点为止 */ public class FlowNodeRelation{ ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); @Test//部署流程 public void initializeInstance() throws Exception { InputStream inputStreamBpmn = this.getClass().getResourceAsStream("newesflc.bpmn"); InputStream inputStreamPng = this.getClass().getResourceAsStream("newesflc.png"); Deployment deployment = processEngine.getRepositoryService()//与流程定义和部署对象相关的Service .createDeployment()//创建一个部署对象 .name("子流程测试")//添加部署的名称 .addInputStream("newesflc.bpmn", inputStreamBpmn)// .addInputStream("newesflc.png", inputStreamPng)// .deploy();//完成部署 System.out.println("部署ID:"+deployment.getId());// System.out.println("部署名称:"+deployment.getName());// } Set<String> childSet = new HashSet<String>(); /** * 获取流程实例节点 */ @Test public void getActivitiyNode() { RepositoryService rs = processEngine.getRepositoryService(); ProcessDefinitionEntity def = (ProcessDefinitionEntity) ((RepositoryServiceImpl) rs) // act_re_procdef 表的ID .getDeployedProcessDefinition("esflc17:1:73004"); List<ActivityImpl> activitiList = def.getActivities(); // rs是指RepositoryService的实例 // 根据任务获取当前流程执行ID,执行实例以及当前流程节点的ID: String name = null, // 流程keyID flowkey = def.getId().split(":")[0].substring(0, 5), key = null, parentkey = null, version = "17"; for (ActivityImpl activityImpl : activitiList) { childSet = new HashSet<String>(); System.out .println("----------------------------------------------------------"); ActivityBehavior activityBehavior = activityImpl .getActivityBehavior(); // // 用户任务 if (activityBehavior instanceof UserTaskActivityBehavior) { UserTaskActivityBehavior userTaskActivityBehavior = (UserTaskActivityBehavior) activityBehavior; TaskDefinition taskDefinition = userTaskActivityBehavior.getTaskDefinition(); System.out.print("当前节点:" + activityImpl.getId() + "(" + taskDefinition.getNameExpression() + ")"); getChildList(activityImpl, def); System.out.println("下一步任务任务:"); for (String s : childSet) { System.out.println(s); ActivityImpl activityImpl2 = def.findActivity(s); parentkey = activityImpl.getId();// 父节点id // 节点名称 name = ((UserTaskActivityBehavior) activityImpl2 .getActivityBehavior()).getTaskDefinition() .getNameExpression().toString(); key = s; insertNodeInfoToDatabase(name, flowkey, key, parentkey, version); } } } } /** * 获取指定节点的子节点,如果是非用户任务,继续取下面的节点 * @param activityImpl * @return */ public Set<String> getChildList(ActivityImpl activityImpl,ProcessDefinitionEntity def ) { List<PvmTransition> outTransitions = activityImpl.getOutgoingTransitions(); for (PvmTransition tr : outTransitions) { PvmActivity ac = tr.getDestination(); //获取线路的终点节点 ActivityImpl activityImplTemp = def.findActivity(ac.getId());//nodeHashMap.get(ac.getId()); if(!(activityImplTemp.getActivityBehavior() instanceof UserTaskActivityBehavior)){ childSet.addAll(getChildList(activityImplTemp,def)); }else { childSet.add(ac.getId());//+"("+ac.getProperty("name")+")" } } return childSet; }
/** * 将节点信息插入到数据库中 * @param name * @param flowkey * @param key * @param parentkey */ public void insertNodeInfoToDatabase(String name ,String flowkey,String key,String parentkey,String version){ //数据库连接 Connection connection = DataConnection.getconnect(); Statement stat = null; try { stat = connection.createStatement(); stat.execute("INSERT INTO `jjscj`.`CJ_FLOW_NODE` (`NAME`, `FLOWKEY`, `TASKKEY`, `PARENT`,`VERSION`)" + " VALUES ('"+name+"', '"+flowkey+"', '"+key+"', '"+parentkey+"', '"+version+ "');"); } catch (SQLException e) { e.printStackTrace(); } } }