文件关系网状图,增加关系id和描述

This commit is contained in:
s2042968 2025-01-03 10:11:05 +08:00
parent 7151243618
commit 061516f82e
2 changed files with 13 additions and 30 deletions

View File

@ -3,38 +3,18 @@ package com.electromagnetic.industry.software.manage.pojo.models;
import lombok.Data;
import lombok.Getter;
import java.util.Objects;
@Data
@Getter
public class Edge {
String source; // 起点
String target; // 终点
String relationId; //关系id
String relationship; //关系描述
public Edge(String source, String target) {
public Edge(String source, String target, String relationId, String relationship) {
this.source = source;
this.target = target;
}
@Override
public String toString() {
return source + " -- " + target;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Edge edge = (Edge) obj;
// 无向边两个方向都相等即认为是同一条边
return (Objects.equals(source, edge.source) && Objects.equals(target, edge.target)) ||
(Objects.equals(source, edge.target) && Objects.equals(target, edge.source));
}
@Override
public int hashCode() {
// 无向边需要对 source target 排序后计算哈希值
return Objects.hash(Math.min(source.hashCode(), target.hashCode()),
Math.max(source.hashCode(), target.hashCode()));
this.relationId = relationId;
this.relationship = relationship;
}
}

View File

@ -16,6 +16,7 @@ import com.electromagnetic.industry.software.manage.service.EdFileRelationServic
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.*;
@ -64,13 +65,15 @@ public class EdFileRelationServiceImpl extends ServiceImpl<EdFileRelationMapper,
* 获取关系数据
*/
@Override
@Transactional
public FileRelationViewVO listRelations(String startId) {
FileRelationViewVO fileRelationViewVO = new FileRelationViewVO();
List<Edge> visitedEdges = new ArrayList<>();
Set<String> visitedIds = new HashSet<>();
Set<Edge> uniqueEdges = new HashSet<>();
Set<String> uniqueRelationIds = new HashSet<>();
Queue<String> queue = new LinkedList<>();
// 初始化 BFS
@ -84,9 +87,9 @@ public class EdFileRelationServiceImpl extends ServiceImpl<EdFileRelationMapper,
for (Edge edge : neighbors) {
// 添加边信息
if (!uniqueEdges.contains(edge)) {
if (!uniqueRelationIds.contains(edge.getRelationId())) {
visitedEdges.add(edge);
uniqueEdges.add(edge);
uniqueRelationIds.add(edge.getRelationId());
}
// 如果目标节点未访问记录边并继续搜索
if (!visitedIds.contains(edge.getTarget())) {
@ -116,9 +119,9 @@ public class EdFileRelationServiceImpl extends ServiceImpl<EdFileRelationMapper,
List<Edge> edges = new ArrayList<>();
for (EdFileRelation edFileRelation : list) {
if (edFileRelation.getId1().equals(id)) {
edges.add(new Edge(edFileRelation.getId1(), edFileRelation.getId2()));
edges.add(new Edge(edFileRelation.getId1(), edFileRelation.getId2(),edFileRelation.getId(),edFileRelation.getRelationship()));
} else {
edges.add(new Edge(edFileRelation.getId2(), edFileRelation.getId1()));
edges.add(new Edge(edFileRelation.getId2(), edFileRelation.getId1(),edFileRelation.getId(),edFileRelation.getRelationship()));
}
}
return edges;