private String[] findNextNodeIds(String currentNodeId, Map<String, Set<String>> nodeMap) { Assert.notNull(currentNodeId, "currentNodeId must not be null"); Set<String> set = nodeMap.get(currentNodeId); if (set != null) { String[] array = set.toArray(new String[0]); return array; } else { return null; } }
private boolean checkPath( Map<String, Set<String>> nodeMap, String startNodeId, List<String> accessPath) { Assert.notNull(nodeMap, "nodeMap must not be null"); Assert.notNull(startNodeId, "startNodeId must not be null"); boolean flag = false; if (accessPath == null) accessPath = new ArrayList<String>(); String tmpId = startNodeId; if (accessPath.contains(tmpId)) { logger.error(String.format("Service(%s) is existed!", tmpId)); flag = false; // accessPath.add(tmpId); // if (accessPath != null) { // System.out.println("======"); // // for (String ts : accessPath) { // System.out.print(ts + "->"); // } // System.out.println(""); // System.out.println("======"); // } return flag; } accessPath.add(tmpId); String[] findNextNodeIds = findNextNodeIds(tmpId, nodeMap); if (findNextNodeIds == null && !(this.getEnd().getId().equals(tmpId))) { logger.error(String.format("Service(%s) isn't reach end", tmpId)); flag = false; } else if (findNextNodeIds == null && this.getEnd().getId().equals(tmpId)) { flag = true; } else if (findNextNodeIds != null && this.getEnd().getId().equals(tmpId)) { logger.error(String.format("Service(%s) must not have next service", tmpId)); flag = false; } else { if (findNextNodeIds != null) for (String sn : findNextNodeIds) { List<String> tmpList = new ArrayList<String>(accessPath); flag = checkPath(nodeMap, sn, tmpList); if (flag == false) break; } accessPath = null; } if (accessPath != null) { System.out.println("======"); Collection<SequenceFlowNode> sequenceFlowNodes = this.getServiceFlow().getSequenceFlowNodes(); if (sequenceFlowNodes == null) sequenceFlowNodes = new ArrayList<SequenceFlowNode>(); Map<String, SequenceFlowNode> previousMap = new HashMap<String, SequenceFlowNode>(); // Map<String, SequenceFlowNode> nextMap = new HashMap<String, SequenceFlowNode>(); for (SequenceFlowNode sn : sequenceFlowNodes) { previousMap.put(sn.getSoueceRef().getId(), sn); // Set<String> keySet = sn.getTargetTrueRef().keySet(); // for (String s : keySet) { // nextMap.put(s, sn); // } // // if (sn.getTargetFalseRef() != null) // nextMap.put(sn.getTargetFalseRef().getId(), sn); // // if (sn.getTargetExceptionRef() != null) // nextMap.put(sn.getTargetExceptionRef().getId(), sn); } int forkcount = 0; for (String ts : accessPath) { // 判断fork和join数目 SequenceFlowNode sn = previousMap.get(ts); // System.out.println("ts:" + ts + "," + sn.getId()); if (sn != null && sn.getSequenceFlowType().equals(SequenceFlowType.FORK)) { forkcount++; } else if (sn != null && sn.getSequenceFlowType().equals(SequenceFlowType.JOIN)) { forkcount--; } System.out.print(ts + "->"); } if (forkcount < 0) { flag = false; logger.error( String.format("ServiceFlow(%s) fork num or join num is invalid", this.getId())); } System.out.println(""); System.out.println("======"); } return flag; }
/** * 描述:检查服务流定义的完整性 * * @param * @return * @throws * @see * @since %I% */ public void validate() { // boolean flag = false; // 算法: Map<String, Set<String>> nodeMap = new HashMap<String, Set<String>>(); Collection<SequenceFlowNode> sequenceflowNodes = this.getServiceFlow().getSequenceFlowNodes(); for (SequenceFlowNode node : sequenceflowNodes) { ServiceNode soueceRef = node.getSoueceRef(); Map<String, ServiceNode> targetTrueRef = node.getTargetTrueRef(); ServiceNode targetFalseRef = node.getTargetFalseRef(); ServiceNode targetExceptionRef = node.getTargetExceptionRef(); ConditionNode condition = node.getCondition(); // 有condition出现,必须有 true target service和false target service if (condition != null) { Assert.notEmpty( targetTrueRef, String.format("sequenceFlow(%s) targetTrueRef must not be empty", node.getId())); Assert.notNull( targetFalseRef, String.format("sequenceFlow(%s) targetFalseRef must not be null", node.getId())); } if (node.getSequenceFlowType().equals(SequenceFlowType.SERIAL) || node.getSequenceFlowType().equals(SequenceFlowType.JOIN)) { Assert.isTrue( node.getTargetTrueRef().keySet().size() == 1, String.format("sequenceFlow(%s) getTargetTrueRef must be 1", node.getId())); } if (node.getSequenceFlowType().equals(SequenceFlowType.FORK)) { Set<String> targetTrueRefSet = node.getTargetTrueRef().keySet(); for (String s : targetTrueRefSet) { Assert.isTrue( this.getEnd().getId().equals(s), String.format( "sequenceFlow(%s) fork target service must not be end service", node.getId())); } } // todo 每个节点只能fork一次 Set<String> set = nodeMap.get(soueceRef.getId()); if (set == null) { set = new HashSet<String>(); nodeMap.put(soueceRef.getId(), set); } if (targetTrueRef != null) { set.addAll(targetTrueRef.keySet()); } if (targetFalseRef != null) { set.add(targetFalseRef.getId()); } if (targetExceptionRef != null) { set.add(targetExceptionRef.getId()); } } String startNodeId = this.getStart().getId(); // EndNode endNode = this.getEnd(); // 判断从start能否走到ends 之一 Assert.isTrue(checkPath(nodeMap, startNodeId), "some service flow path is error"); }