fix: 关系图优化

This commit is contained in:
chenjianxing 2021-10-25 21:27:10 +08:00 committed by jianxing
parent f1dba94676
commit 7d0216aaca
5 changed files with 81 additions and 3 deletions

View File

@ -37,6 +37,7 @@ public class RelationshipGraphData {
public static class Edge { public static class Edge {
private Integer source; private Integer source;
private Integer target; private Integer target;
private float curveness;
} }
/** /**

View File

@ -3,6 +3,7 @@ package io.metersphere.service;
import io.metersphere.base.domain.RelationshipEdge; import io.metersphere.base.domain.RelationshipEdge;
import io.metersphere.base.domain.RelationshipEdgeExample; import io.metersphere.base.domain.RelationshipEdgeExample;
import io.metersphere.base.domain.RelationshipEdgeKey;
import io.metersphere.base.mapper.RelationshipEdgeMapper; import io.metersphere.base.mapper.RelationshipEdgeMapper;
import io.metersphere.base.mapper.ext.ExtRelationshipEdgeMapper; import io.metersphere.base.mapper.ext.ExtRelationshipEdgeMapper;
import io.metersphere.commons.exception.MSException; import io.metersphere.commons.exception.MSException;
@ -41,9 +42,85 @@ public class RelationshipEdgeService {
example.createCriteria() example.createCriteria()
.andSourceIdEqualTo(sourceId) .andSourceIdEqualTo(sourceId)
.andTargetIdEqualTo(targetId); .andTargetIdEqualTo(targetId);
String graphId = relationshipEdgeMapper.selectByExample(example).get(0).getGraphId();
updateGraphId(graphId, sourceId, targetId);
relationshipEdgeMapper.deleteByExample(example); relationshipEdgeMapper.deleteByExample(example);
} }
/**
* 删除边后若形成两个不连通子图则拆分图
* @param graphId
* @param sourceId
* @param targetId
*/
public void updateGraphId(String graphId, String sourceId, String targetId) {
RelationshipEdgeExample graphExample = new RelationshipEdgeExample();
graphExample.createCriteria()
.andGraphIdEqualTo(graphId);
List<RelationshipEdge> edges = relationshipEdgeMapper.selectByExample(graphExample);
// 去掉要删除的边
edges = edges.stream()
.filter(i -> i.getSourceId() != sourceId || i.getTargetId() != targetId)
.collect(Collectors.toList());
Set<String> nodes = new HashSet<>();
Set<String> markSet = new HashSet<>();
nodes.addAll(edges.stream().map(RelationshipEdgeKey::getSourceId).collect(Collectors.toSet()));
nodes.addAll(edges.stream().map(RelationshipEdgeKey::getTargetId).collect(Collectors.toSet()));
dfsForMark(sourceId, edges, markSet, true);
dfsForMark(sourceId, edges, markSet, false);
// 如果连通的点减少说明形成了两个不连通子图重新设置graphId
if (markSet.size() != nodes.size()) {
List<String> updateIds = new ArrayList<>(markSet);
RelationshipEdgeExample updateGraphExample = new RelationshipEdgeExample();
updateGraphExample.createCriteria()
.andSourceIdIn(updateIds);
updateGraphExample.or(
updateGraphExample.createCriteria().andTargetIdIn(updateIds)
);
RelationshipEdge edge = new RelationshipEdge();
edge.setGraphId(UUID.randomUUID().toString());
relationshipEdgeMapper.updateByExampleSelective(edge, updateGraphExample);
}
}
/**
* 遍历标记经过的节点
* @param node
* @param edges
* @param markSet
* @param isForwardDirection
*/
public void dfsForMark(String node, List<RelationshipEdge> edges, Set<String> markSet, boolean isForwardDirection) {
markSet.add(node);
Set<String> nextLevelNodes = new HashSet<>();
for (RelationshipEdge edge : edges) {
if (isForwardDirection) {
if (node.equals(edge.getSourceId())) {
nextLevelNodes.add(edge.getTargetId());
}
} else {
if (node.equals(edge.getTargetId())) {
nextLevelNodes.add(edge.getSourceId());
}
}
}
nextLevelNodes.forEach(nextNode -> {
if (!markSet.contains(node)) {
dfsForMark(nextNode, edges, markSet, true);
dfsForMark(nextNode, edges, markSet, false);
}
});
}
public void delete(String sourceIdOrTargetId) { public void delete(String sourceIdOrTargetId) {
RelationshipEdgeExample example = new RelationshipEdgeExample(); RelationshipEdgeExample example = new RelationshipEdgeExample();
example.createCriteria() example.createCriteria()

@ -1 +1 @@
Subproject commit dfc1f22ca4a189ae8d91d5570acf56697cecd933 Subproject commit d9804d8c3d0496423403794f0997b3b596871a27

View File

@ -20,7 +20,7 @@
"axios": "^0.21.1", "axios": "^0.21.1",
"core-js": "^3.4.3", "core-js": "^3.4.3",
"diffable-html": "^4.0.0", "diffable-html": "^4.0.0",
"echarts": "^4.6.0", "echarts": "^5.0.2",
"el-table-infinite-scroll": "^1.0.10", "el-table-infinite-scroll": "^1.0.10",
"el-tree-transfer": "^2.4.7", "el-tree-transfer": "^2.4.7",
"element-ui": "^2.13.0", "element-ui": "^2.13.0",

@ -1 +1 @@
Subproject commit d51827047ff85c2711cb1fe034ea7f1c67b99189 Subproject commit 1ccb9d2795a51f6dc49aaf9fd9960818bca005e3