Files
auto-solution/modeler/src/views/decision/graph/data-converter.ts

1105 lines
37 KiB
TypeScript
Raw Normal View History

/*
* This file is part of the kernelstudio package.
*
* (c) 2014-2026 zlin <admin@kernelstudio.com>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/
2026-04-15 13:42:07 +08:00
import type { GraphTaskElement, GraphContainer, GraphEdgeElement } from './';
2026-04-15 10:50:02 +08:00
import type { PlatformWithComponents } from '../types/platform';
2026-04-15 13:42:07 +08:00
import { generateKey } from '@/utils/strings';
import { createLineOptions } from './';
// ==================== 类型定义 ====================
/**
* 布局方向类型
*/
export type LayoutDirection = 'horizontal' | 'vertical';
/**
* 布局配置接口
*/
export interface LayoutConfig {
direction: LayoutDirection;
rootPosition?: number; // 根节点起始位置
colSpacing?: number; // 列间距
rowSpacing?: number; // 行间距
startPosition?: number; // 起始位置
}
/**
* 数据库通信关系记录接口
*/
export interface CommunicationRecord {
id: number;
command_platform: string; // 指挥平台(源节点名称)
subordinate_platform: string; // 下属平台(目标节点名称)
command_comm?: string; // 指挥通信方式
subordinate_comm?: string; // 下属通信方式
scenary_id?: number; // 场景ID
}
interface ParentChildMappings {
parentToChildren: Map<string, string[]>;
childToParent: Map<string, string>;
}
interface LayoutParams {
ROOT_POSITION: number; // 根节点起始位置(horizontal时为X, vertical时为Y)
LEVEL_SPACING: number; // 层级间距(horizontal时为X间距, vertical时为Y间距)
SIBLING_SPACING: number; // 同级间距(horizontal时为Y间距, vertical时为X间距)
START_POSITION: number; // 起始位置(horizontal时为Y, vertical时为X)
direction: LayoutDirection;
}
interface NodePosition {
x: number;
y: number;
}
interface SubtreeRange {
min: number;
max: number;
}
/**
* 坐标映射器接口 - 用于抽象水平和垂直布局的坐标转换
*/
interface CoordinateMapper {
/** 将层级索引映射到主轴坐标 */
mapLevelToAxis(level: number): number;
/** 将同级索引映射到次轴坐标 */
mapSiblingIndexToAxis(index: number, totalInLevel?: number): number;
/** 获取节点的主轴坐标 */
getPrimaryCoord(pos: NodePosition): number;
/** 获取节点的次轴坐标 */
getSecondaryCoord(pos: NodePosition): number;
/** 设置节点坐标 */
setCoords(primary: number, secondary: number): NodePosition;
/** 获取子树范围的主轴坐标 */
getSubtreePrimaryCoord(range: SubtreeRange): { min: number; max: number };
/** 创建子树范围对象 */
createSubtreeRange(min: number, max: number): SubtreeRange;
}
/**
* 创建水平布局的坐标映射器 (X轴表示层级,Y轴表示同级分布)
*/
const createHorizontalMapper = (params: LayoutParams): CoordinateMapper => ({
mapLevelToAxis: (level) => params.ROOT_POSITION + (level + 1) * params.LEVEL_SPACING,
mapSiblingIndexToAxis: (index, totalInLevel) => {
// 均匀分布在START_POSITION开始的区域内
return params.START_POSITION + index * params.SIBLING_SPACING + params.SIBLING_SPACING / 2;
},
getPrimaryCoord: (pos) => pos.x,
getSecondaryCoord: (pos) => pos.y,
setCoords: (primary, secondary) => ({ x: primary, y: secondary }),
getSubtreePrimaryCoord: (range) => ({ min: range.min, max: range.max }),
createSubtreeRange: (min, max) => ({ min, max }),
});
/**
* 创建垂直布局的坐标映射器 (Y轴表示层级,X轴表示同级分布)
*/
const createVerticalMapper = (params: LayoutParams): CoordinateMapper => ({
mapLevelToAxis: (level) => params.ROOT_POSITION + (level + 1) * params.LEVEL_SPACING,
mapSiblingIndexToAxis: (index, totalInLevel) => {
// 均匀分布在START_POSITION开始的区域内
return params.START_POSITION + index * params.SIBLING_SPACING + params.SIBLING_SPACING / 2;
},
getPrimaryCoord: (pos) => pos.y,
getSecondaryCoord: (pos) => pos.x,
setCoords: (primary, secondary) => ({ x: secondary, y: primary }),
getSubtreePrimaryCoord: (range) => ({ min: range.min, max: range.max }),
createSubtreeRange: (min, max) => ({ min, max }),
});
// ==================== 主函数:数据转换 ====================
/**
* 从数据库记录转换为 GraphContainer 格式
* @param records 数据库通信关系记录数组
* @param config 布局配置(可选),默认为水平布局
2026-04-15 10:50:02 +08:00
* @param platformsData 完整的平台数据数组(包含components),如果提供则优先使用
* @param scenarioId 场景ID,用于设置节点的scenarioId字段
* @returns GraphContainer 格式的节点和边数据
*/
export const convertRecordsToGraphContainer = (
records: CommunicationRecord[],
2026-04-15 10:50:02 +08:00
config?: Partial<LayoutConfig>,
platformsData?: PlatformWithComponents[],
scenarioId?: number
): GraphContainer => {
if (!records || records.length === 0) {
return { nodes: [], edges: [] };
}
// 合并默认配置
const finalConfig: LayoutConfig = {
direction: config?.direction || 'horizontal',
rootPosition: typeof config?.rootPosition === 'number' ? config.rootPosition : 50,
colSpacing: typeof config?.colSpacing === 'number' ? config.colSpacing : 280,
rowSpacing: typeof config?.rowSpacing === 'number' ? config.rowSpacing : 140,
startPosition: typeof config?.startPosition === 'number' ? config.startPosition : 80,
};
2026-04-15 10:50:02 +08:00
// 构建平台名称到完整数据的映射
const platformDataMap = new Map<string, PlatformWithComponents>();
if (platformsData && platformsData.length > 0) {
platformsData.forEach(platform => {
if (platform.name) {
platformDataMap.set(platform.name, platform);
}
});
console.log(`[convertRecordsToGraphContainer] 提供了 ${platformDataMap.size} 个平台的完整数据`);
} else {
console.warn('[convertRecordsToGraphContainer] 未提供平台完整数据,将使用简化的组件信息');
}
// 收集所有唯一的平台名称(过滤无效值)
const platformSet = new Set<string>();
records.forEach(record => {
if (record.command_platform && typeof record.command_platform === 'string' && record.command_platform.trim()) {
platformSet.add(record.command_platform);
}
if (record.subordinate_platform && typeof record.subordinate_platform === 'string' && record.subordinate_platform.trim()) {
platformSet.add(record.subordinate_platform);
}
});
const allPlatforms = Array.from(platformSet);
console.log(`收集到 ${allPlatforms.length} 个唯一平台:`, allPlatforms);
if (allPlatforms.length === 0) {
console.warn('没有有效的平台数据');
return { nodes: [], edges: [] };
}
// 构建父子映射关系
const { parentToChildren, childToParent } = buildParentChildMappings(records);
// 检测连通分量(处理断链情况)
const connectedComponents = findConnectedComponents(allPlatforms, parentToChildren, childToParent);
console.log(`检测到 ${connectedComponents.length} 个连通分量`);
// 为每个连通分量生成布局
const allNodes: GraphTaskElement[] = [];
const allEdges: GraphEdgeElement[] = [];
let offset: number = finalConfig.rootPosition ?? 50; // 第一个分量的起始位置
connectedComponents.forEach((component, componentIndex) => {
// 为该分量确定根节点(选择入度为0的节点)
const rootPlatformName = determineRootNode(Array.from(component), childToParent);
if (!rootPlatformName) {
console.warn(`第 ${componentIndex + 1} 个分量未找到根节点,跳过`);
return;
}
// BFS遍历确定该分量中每个节点的层级
const levelMap = computeNodeLevels(
Array.from(component),
rootPlatformName,
parentToChildren,
childToParent
);
console.log(` 分量 ${componentIndex + 1} 的层级映射:`, Object.fromEntries(levelMap));
// 按层级分组
const levelsMap = groupNodesByLevel(levelMap);
console.log(` 分量 ${componentIndex + 1} 的层级分组:`, Object.fromEntries(levelsMap));
// 根据方向创建布局参数
const layoutParams: LayoutParams = {
ROOT_POSITION: offset ?? 50,
LEVEL_SPACING: finalConfig.colSpacing ?? 280,
SIBLING_SPACING: finalConfig.rowSpacing ?? 140,
START_POSITION: finalConfig.startPosition ?? 80,
direction: finalConfig.direction,
};
// 创建坐标映射器
const mapper = finalConfig.direction === 'horizontal'
? createHorizontalMapper(layoutParams)
: createVerticalMapper(layoutParams);
// 计算节点位置
const nodePositions = calculateNodePositions(
levelsMap,
parentToChildren,
childToParent,
layoutParams,
mapper
);
2026-04-15 10:50:02 +08:00
// 创建节点对象(传入完整的平台数据映射和场景ID)
const nodeMap = createNodeObjects(levelsMap, nodePositions, platformDataMap, scenarioId);
console.log(` 分量 ${componentIndex + 1} 创建了 ${nodeMap.size} 个节点:`, Array.from(nodeMap.keys()));
// 创建根节点(如果不存在则添加)
if (!nodeMap.has(rootPlatformName)) {
console.warn(` 根节点 "${rootPlatformName}" 未在 levelsMap 中找到,手动创建`);
}
2026-04-15 10:50:02 +08:00
createRootNode(nodeMap, rootPlatformName, parentToChildren, layoutParams, mapper, platformDataMap, scenarioId);
console.log(` 最终节点数: ${nodeMap.size}`);
// 过滤出属于当前分量的边
const componentEdges = filterEdgesForComponent(records, component);
const edges = createEdges(componentEdges, nodeMap);
2026-04-15 10:50:02 +08:00
// 将GraphTaskElement直接添加到总结果(GraphContainer需要的是GraphTaskElement[])
allNodes.push(...Array.from(nodeMap.values()));
allEdges.push(...edges);
// 计算下一个分量的偏移量(基于当前分量的宽度)
const maxLevel = Math.max(...Array.from(levelsMap.keys()));
offset += (maxLevel + 1) * layoutParams.LEVEL_SPACING + 150; // 分量之间留150px间隔
});
return {
nodes: allNodes,
edges: allEdges,
};
};
// ==================== 辅助函数:检测连通分量 ====================
/**
* 使用并查集或DFS检测图中的连通分量
*/
const findConnectedComponents = (
platforms: string[],
parentToChildren: Map<string, string[]>,
childToParent: Map<string, string>
): Set<string>[] => {
// 过滤掉undefined、null和空字符串
const validPlatforms = platforms.filter(p => p && typeof p === 'string' && p.trim().length > 0);
const visited = new Set<string>();
const components: Set<string>[] = [];
// DFS遍历找出所有连通分量
const dfs = (platform: string, component: Set<string>) => {
if (visited.has(platform)) return;
visited.add(platform);
component.add(platform);
// 访问子节点
const children = parentToChildren.get(platform) || [];
children.forEach(child => {
// 只处理有效的子节点
if (child && typeof child === 'string' && child.trim().length > 0) {
dfs(child, component);
}
});
// 访问父节点
const parent = childToParent.get(platform);
if (parent && !visited.has(parent) && typeof parent === 'string' && parent.trim().length > 0) {
dfs(parent, component);
}
};
// 对每个未访问的有效节点启动一次DFS
validPlatforms.forEach(platform => {
if (!visited.has(platform)) {
const component = new Set<string>();
dfs(platform, component);
if (component.size > 0) {
components.push(component);
}
}
});
return components;
};
/**
* 为连通分量确定根节点
* 策略:选择入度为0的节点(没有父节点的节点),如果有多个则选择第一个
*/
const determineRootNode = (
platforms: string[],
childToParent: Map<string, string>
): string | null => {
// 过滤掉undefined、null和空字符串
const validPlatforms = platforms.filter(p => p && typeof p === 'string' && p.trim().length > 0);
if (validPlatforms.length === 0) return null;
// 优先选择入度为0的节点(没有父节点)
const rootCandidates = validPlatforms.filter(platform => !childToParent.has(platform));
if (rootCandidates.length > 0) {
return rootCandidates[0] || null;
}
// 如果所有节点都有父节点(可能是环形结构),返回第一个有效节点
return validPlatforms[0] || null;
};
/**
* 过滤出属于特定连通分量的边
*/
const filterEdgesForComponent = (
records: CommunicationRecord[],
component: Set<string>
): CommunicationRecord[] => {
return records.filter(record =>
component.has(record.command_platform) &&
component.has(record.subordinate_platform)
);
};
// ==================== 辅助函数:构建父子映射 ====================
const buildParentChildMappings = (records: CommunicationRecord[]): ParentChildMappings => {
const parentToChildren = new Map<string, string[]>();
const childToParent = new Map<string, string>();
records.forEach(record => {
if (!parentToChildren.has(record.command_platform)) {
parentToChildren.set(record.command_platform, []);
}
parentToChildren.get(record.command_platform)!.push(record.subordinate_platform);
childToParent.set(record.subordinate_platform, record.command_platform);
});
return { parentToChildren, childToParent };
};
// ==================== 辅助函数:计算节点层级 ====================
const computeNodeLevels = (
platforms: string[],
rootPlatformName: string,
parentToChildren: Map<string, string[]>,
childToParent: Map<string, string>
): Map<string, number> => {
const levelMap = new Map<string, number>();
const processed = new Set<string>();
const queue: Array<{ platform: string; level: number }> = [];
// 找出所有入度为0的节点(没有父节点的节点),作为BFS的起点
const rootNodes = platforms.filter(p => !childToParent.has(p));
console.log(` BFS起点(入度为0的节点): [${rootNodes.join(', ')}]`);
// 将所有根节点加入队列,层级为0
rootNodes.forEach(root => {
levelMap.set(root, 0);
processed.add(root);
queue.push({ platform: root, level: 0 });
});
// BFS遍历后续层级
while (queue.length > 0) {
const { platform: currentPlatform, level: currentLevel } = queue.shift()!;
const children = parentToChildren.get(currentPlatform) || [];
children.forEach(childPlatform => {
if (!processed.has(childPlatform)) {
levelMap.set(childPlatform, currentLevel + 1);
processed.add(childPlatform);
queue.push({ platform: childPlatform, level: currentLevel + 1 });
}
});
}
// 理论上不应该有未处理的节点,因为所有节点都应该从某个根节点可达
// 如果有,说明图中存在环或者数据有问题
const unprocessed = platforms.filter(p => !processed.has(p));
if (unprocessed.length > 0) {
console.warn(` 警告:以下节点未被BFS遍历到(可能存在环或数据问题): [${unprocessed.join(', ')}]`);
// 给这些节点分配一个合理的层级(基于它们的父节点)
unprocessed.forEach(platform => {
const parent = childToParent.get(platform);
if (parent && levelMap.has(parent)) {
levelMap.set(platform, levelMap.get(parent)! + 1);
} else {
// 如果连父节点都没有,放到最后一层
const maxLevel = Math.max(...Array.from(levelMap.values()), 0);
levelMap.set(platform, maxLevel + 1);
}
});
}
return levelMap;
};
// ==================== 辅助函数:按层级分组 ====================
const groupNodesByLevel = (levelMap: Map<string, number>): Map<number, string[]> => {
const levelsMap = new Map<number, string[]>();
levelMap.forEach((level, platform) => {
if (!levelsMap.has(level)) {
levelsMap.set(level, []);
}
levelsMap.get(level)!.push(platform);
});
return levelsMap;
};
// ==================== 辅助函数:计算节点位置 ====================
const calculateNodePositions = (
levelsMap: Map<number, string[]>,
parentToChildren: Map<string, string[]>,
childToParent: Map<string, string>,
params: LayoutParams,
mapper: CoordinateMapper
): Map<string, NodePosition> => {
const nodePositions = new Map<string, NodePosition>();
const subtreeRanges = new Map<string, SubtreeRange>();
// 第一步:预计算每列需要的总高度
precomputeColumnHeights(levelsMap, parentToChildren, params.SIBLING_SPACING, subtreeRanges, mapper);
// 第二步:初步分配位置
initialPositionAssignment(levelsMap, parentToChildren, params, subtreeRanges, nodePositions, mapper);
// 第三步:调整父节点使其位于子节点中心(处理共享子节点情况)
adjustParentsToCenterOfChildren(levelsMap, parentToChildren, childToParent, nodePositions, mapper);
// 第四步:全局防重叠检测与修正(跨所有层级)
preventGlobalOverlaps(parentToChildren, params.SIBLING_SPACING, nodePositions, mapper);
// 第五步:重新调整父节点恢复等腰三角形
restoreIsoscelesTriangles(levelsMap, parentToChildren, nodePositions, mapper);
// 第六步:最终全局防重叠检查
preventGlobalOverlaps(parentToChildren, params.SIBLING_SPACING, nodePositions, mapper);
return nodePositions;
};
// 预计算每列高度
const precomputeColumnHeights = (
levelsMap: Map<number, string[]>,
parentToChildren: Map<string, string[]>,
siblingSpacing: number,
subtreeRanges: Map<string, SubtreeRange>,
mapper: CoordinateMapper
): void => {
const sortedLevels = Array.from(levelsMap.keys()).sort((a, b) => b - a);
sortedLevels.forEach(level => {
const platformsInLevel = levelsMap.get(level)!;
let totalSize = 0;
platformsInLevel.forEach(platform => {
const children = parentToChildren.get(platform) || [];
if (children.length > 0) {
let maxSubtreeSize = 0;
children.forEach(child => {
const childRange = subtreeRanges.get(child);
if (childRange) {
const range = mapper.getSubtreePrimaryCoord(childRange);
const childSize = range.max - range.min;
maxSubtreeSize = Math.max(maxSubtreeSize, childSize);
}
});
const subtreeSize = maxSubtreeSize;
totalSize += Math.max(siblingSpacing, subtreeSize);
subtreeRanges.set(platform, mapper.createSubtreeRange(
totalSize - subtreeSize,
totalSize
));
} else {
totalSize += siblingSpacing;
subtreeRanges.set(platform, mapper.createSubtreeRange(
totalSize - siblingSpacing,
totalSize
));
}
});
});
};
// 初步分配位置(简化版:所有节点均匀分布,不考虑子树高度)
const initialPositionAssignment = (
levelsMap: Map<number, string[]>,
parentToChildren: Map<string, string[]>,
params: LayoutParams,
subtreeRanges: Map<string, SubtreeRange>,
nodePositions: Map<string, NodePosition>,
mapper: CoordinateMapper
): void => {
const forwardLevels = Array.from(levelsMap.keys()).sort((a, b) => a - b);
forwardLevels.forEach(level => {
const platformsInLevel = levelsMap.get(level)!;
platformsInLevel.forEach((platform, index) => {
const primaryCoord = mapper.mapLevelToAxis(level);
const secondaryCoord = mapper.mapSiblingIndexToAxis(index, platformsInLevel.length);
nodePositions.set(platform, mapper.setCoords(primaryCoord, secondaryCoord));
});
});
};
// 调整父节点到子节点中心
const adjustParentsToCenterOfChildren = (
levelsMap: Map<number, string[]>,
parentToChildren: Map<string, string[]>,
childToParent: Map<string, string>,
nodePositions: Map<string, NodePosition>,
mapper: CoordinateMapper
): void => {
const forwardLevels = Array.from(levelsMap.keys()).sort((a, b) => a - b);
// 从最底层向上遍历,确保子节点位置已经确定后再调整父节点
[...forwardLevels].reverse().forEach(level => {
const platformsInLevel = levelsMap.get(level)!;
platformsInLevel.forEach(platform => {
const children = parentToChildren.get(platform) || [];
if (children.length > 0) {
const childPositions = children.map(child => nodePositions.get(child)).filter(Boolean);
if (childPositions.length > 0) {
// 计算所有子节点的平均次轴坐标(垂直布局时是X,水平布局时是Y)
const avgSecondaryCoord = childPositions.reduce((sum, pos) =>
sum + mapper.getSecondaryCoord(pos!), 0) / childPositions.length;
// 将父节点的次轴坐标移动到子节点的中心
const pos = nodePositions.get(platform)!;
const primaryCoord = mapper.getPrimaryCoord(pos);
nodePositions.set(platform, mapper.setCoords(primaryCoord, avgSecondaryCoord));
}
}
});
});
};
// 全局防重叠检测与修正(跨所有层级,迭代式确保完全无重叠)
const preventGlobalOverlaps = (
parentToChildren: Map<string, string[]>,
siblingSpacing: number,
nodePositions: Map<string, NodePosition>,
mapper: CoordinateMapper
): void => {
// 收集所有节点并按主轴坐标分组(同层的节点)
const nodesByLevel = new Map<number, Array<{ platform: string; pos: NodePosition }>>();
nodePositions.forEach((pos, platform) => {
const primaryCoord = mapper.getPrimaryCoord(pos);
if (!nodesByLevel.has(primaryCoord)) {
nodesByLevel.set(primaryCoord, []);
}
nodesByLevel.get(primaryCoord)!.push({ platform, pos });
});
// 对每一层进行防重叠处理
nodesByLevel.forEach(nodes => {
let hasOverlap = true;
let iterations = 0;
const maxIterations = 30; // 防止无限循环
while (hasOverlap && iterations < maxIterations) {
hasOverlap = false;
iterations++;
// 按次轴坐标排序
nodes.sort((a, b) => mapper.getSecondaryCoord(a.pos) - mapper.getSecondaryCoord(b.pos));
for (let i = 1; i < nodes.length; i++) {
const prev = nodes[i - 1];
const curr = nodes[i];
if (!prev || !curr) continue;
const actualDistance = mapper.getSecondaryCoord(curr.pos) - mapper.getSecondaryCoord(prev.pos);
if (actualDistance < siblingSpacing) {
hasOverlap = true;
const offset = siblingSpacing - actualDistance;
const currSecondary = mapper.getSecondaryCoord(curr.pos);
const currPrimary = mapper.getPrimaryCoord(curr.pos);
curr.pos = mapper.setCoords(currPrimary, currSecondary + offset);
// 递归调整所有后代节点
const adjustDescendants = (platform: string, secondaryOffset: number) => {
const children = parentToChildren.get(platform) || [];
children.forEach(child => {
const childPos = nodePositions.get(child);
if (childPos) {
const childPrimary = mapper.getPrimaryCoord(childPos);
const childSecondary = mapper.getSecondaryCoord(childPos);
const newPos = mapper.setCoords(childPrimary, childSecondary + secondaryOffset);
nodePositions.set(child, newPos);
adjustDescendants(child, secondaryOffset);
}
});
};
adjustDescendants(curr.platform, offset);
// 发现重叠并调整后,跳出内层循环,重新开始检测
break;
}
}
}
if (iterations >= maxIterations) {
console.warn(`某层防重叠达到最大迭代次数(${maxIterations}),可能存在未解决的重叠`);
}
});
};
// 恢复等腰三角形
const restoreIsoscelesTriangles = (
levelsMap: Map<number, string[]>,
parentToChildren: Map<string, string[]>,
nodePositions: Map<string, NodePosition>,
mapper: CoordinateMapper
): void => {
const forwardLevels = Array.from(levelsMap.keys()).sort((a, b) => a - b);
[...forwardLevels].reverse().forEach(level => {
const platformsInLevel = levelsMap.get(level)!;
platformsInLevel.forEach(platform => {
const children = parentToChildren.get(platform) || [];
if (children.length > 0) {
const childPositions = children.map(child => nodePositions.get(child)).filter(Boolean);
if (childPositions.length > 0) {
const avgSecondaryCoord = childPositions.reduce((sum, pos) =>
sum + mapper.getSecondaryCoord(pos!), 0) / childPositions.length;
const pos = nodePositions.get(platform)!;
const primaryCoord = mapper.getPrimaryCoord(pos);
nodePositions.set(platform, mapper.setCoords(primaryCoord, avgSecondaryCoord));
}
}
});
});
};
// ==================== 辅助函数:创建节点对象 ====================
const createNodeObjects = (
levelsMap: Map<number, string[]>,
2026-04-15 10:50:02 +08:00
nodePositions: Map<string, NodePosition>,
platformDataMap: Map<string, PlatformWithComponents>,
scenarioId?: number
): Map<string, GraphTaskElement> => {
const nodeMap = new Map<string, GraphTaskElement>();
levelsMap.forEach((platformsInLevel, level) => {
platformsInLevel.forEach(platform => {
const pos = nodePositions.get(platform)!;
2026-04-15 10:50:02 +08:00
// 从映射表中获取平台完整数据
const platformData = platformDataMap.get(platform);
const platformId = platformData?.id || 0;
// 优先使用platformData自带的scenarioId,其次使用传入的参数
const nodeScenarioId = platformData?.scenarioId ?? scenarioId ?? 0;
// 使用真实的组件数据(保持完整结构),如果没有则创建默认组件
const components = platformData?.components && platformData.components.length > 0
? platformData.components // 直接使用完整组件数组,不做字段筛选
: [
{
id: 1,
name: `${platform}_comm`,
type: 'communication',
description: `通信组件`
}
];
console.log(`[createNodeObjects] 平台 "${platform}" 有 ${components.length} 个组件:`, components.map(c => c.name));
const node: GraphTaskElement = {
id: 0,
key: generateKey(),
type: 'scenario',
name: platform,
2026-04-15 10:50:02 +08:00
platformId: platformId,
scenarioId: nodeScenarioId, // 修复:优先使用platformData.scenarioId
components: components,
template: 0,
templateType: null,
category: null,
multiable: false,
group: null,
2026-04-15 10:50:02 +08:00
description: platformData?.description || platform,
order: 0,
position: {
x: Math.round(pos.x),
y: Math.round(pos.y),
},
width: 250,
2026-04-15 10:50:02 +08:00
height: 120,
inputs: null,
outputs: null,
parameters: [],
variables: [],
};
nodeMap.set(platform, node);
});
});
return nodeMap;
};
// ==================== 辅助函数:创建根节点 ====================
const createRootNode = (
nodeMap: Map<string, GraphTaskElement>,
rootPlatformName: string,
parentToChildren: Map<string, string[]>,
params: LayoutParams,
2026-04-15 10:50:02 +08:00
mapper: CoordinateMapper,
platformDataMap: Map<string, PlatformWithComponents>,
scenarioId?: number
): void => {
if (nodeMap.size === 0 || !rootPlatformName) return;
const directChildren = parentToChildren.get(rootPlatformName) || [];
let centerSecondaryCoord: number;
if (directChildren.length > 0) {
const childSecondaryPositions = directChildren.map(child => {
const childNode = nodeMap.get(child);
return childNode ? mapper.getSecondaryCoord(childNode.position) : 0;
}).filter(coord => coord > 0);
if (childSecondaryPositions.length > 0) {
centerSecondaryCoord = childSecondaryPositions.reduce((sum, coord) => sum + coord, 0) / childSecondaryPositions.length;
} else {
const allNodes = Array.from(nodeMap.values());
const coords = allNodes.map(n => mapper.getSecondaryCoord(n.position));
const minCoord = Math.min(...coords);
const maxCoord = Math.max(...coords);
centerSecondaryCoord = (minCoord + maxCoord) / 2;
}
} else {
const allNodes = Array.from(nodeMap.values());
const coords = allNodes.map(n => mapper.getSecondaryCoord(n.position));
const minCoord = Math.min(...coords);
const maxCoord = Math.max(...coords);
centerSecondaryCoord = (minCoord + maxCoord) / 2;
}
2026-04-15 10:50:02 +08:00
// 从映射表中获取平台完整数据
const platformData = platformDataMap.get(rootPlatformName);
const platformId = platformData?.id || 0;
// 优先使用platformData自带的scenarioId,其次使用传入的参数
const rootNodeScenarioId = platformData?.scenarioId ?? scenarioId ?? 0;
// 使用真实的组件数据(保持完整结构),如果没有则创建默认组件
const components = platformData?.components && platformData.components.length > 0
? platformData.components // 直接使用完整组件数组,不做字段筛选
: [
{
id: 1,
name: `${rootPlatformName}_comm`,
type: 'communication',
description: `通信组件`
}
];
console.log(`[createRootNode] 根节点 "${rootPlatformName}" 有 ${components.length} 个组件:`, components.map(c => c.name));
const rootNode: GraphTaskElement = {
id: 0,
key: generateKey(),
type: 'scenario',
name: rootPlatformName,
2026-04-15 10:50:02 +08:00
platformId: platformId,
scenarioId: rootNodeScenarioId, // 修复:优先使用platformData.scenarioId
components: components,
template: 0,
templateType: null,
category: null,
multiable: false,
group: null,
2026-04-15 10:50:02 +08:00
description: platformData?.description || rootPlatformName,
order: 0,
position: mapper.setCoords(params.ROOT_POSITION, Math.round(centerSecondaryCoord)),
width: 250,
2026-04-15 10:50:02 +08:00
height: 120,
inputs: null,
outputs: null,
parameters: [],
variables: [],
};
nodeMap.set(rootPlatformName, rootNode);
};
// ==================== 辅助函数:创建边 ====================
const createEdges = (
records: CommunicationRecord[],
nodeMap: Map<string, GraphTaskElement>
): GraphEdgeElement[] => {
const edges: GraphEdgeElement[] = [];
records.forEach((record, index) => {
const sourceNode = nodeMap.get(record.command_platform);
const targetNode = nodeMap.get(record.subordinate_platform);
if (sourceNode && targetNode && sourceNode.key && targetNode.key) {
2026-04-15 10:50:02 +08:00
// 根据通信组件名称查找对应的组件
const sourceComp = sourceNode.components?.find(
comp => comp.name === record.command_comm
);
const targetComp = targetNode.components?.find(
comp => comp.name === record.subordinate_comm
);
if (!sourceComp || !targetComp) {
console.warn(`无法找到匹配的组件 - 记录 ${index + 1}:`, {
record,
sourceComps: sourceNode.components?.map(c => c.name),
targetComps: targetNode.components?.map(c => c.name),
});
return;
}
// 根据node.vue的命名规则生成端口ID
// 格式:out-{comp.id} 或 in-{comp.id}
const sourcePortId = `out-${sourceComp.id}`;
const targetPortId = `in-${targetComp.id}`;
edges.push({
id: index + 1,
key: generateKey(),
2026-04-15 10:50:02 +08:00
source: {
cell: sourceNode.key,
port: sourcePortId,
},
target: {
cell: targetNode.key,
port: targetPortId,
},
...createLineOptions(), // 应用标准边样式配置
});
} else {
console.warn(`无法创建边 ${index + 1}:`, {
record,
sourceNodeExists: !!sourceNode,
targetNodeExists: !!targetNode,
sourceKey: sourceNode?.key,
targetKey: targetNode?.key,
});
}
});
return edges;
};
// ==================== 新增功能:平台数据转通信关系 ====================
/**
* 智能推断通信关系的策略类型
*/
type InferenceStrategy =
| 'by-name-pattern' // 根据名称模式推断(如包含"command"、"cmd"等关键词)
| 'all-to-first' // 所有节点连接到第一个节点
| 'chain' // 链式连接(A→B→C→D...)
| 'star'; // 星型连接(中心节点连接所有其他节点)
/**
* 将平台列表转换为通信关系记录
* @param platforms 平台列表
* @param strategy 推断策略,默认为 'by-name-pattern'
* @returns 通信关系记录数组
*/
export const convertPlatformsToCommunicationRecords = (
platforms: PlatformWithComponents[],
strategy: InferenceStrategy = 'by-name-pattern'
): CommunicationRecord[] => {
if (!platforms || platforms.length === 0) {
return [];
}
const records: CommunicationRecord[] = [];
switch (strategy) {
case 'by-name-pattern':
inferByNamPattern(platforms, records);
break;
case 'all-to-first':
connectAllToFirst(platforms, records);
break;
case 'chain':
createChainConnection(platforms, records);
break;
case 'star':
createStarConnection(platforms, records);
break;
default:
console.warn(`未知的推断策略: ${strategy},使用默认策略 'by-name-pattern'`);
inferByNamPattern(platforms, records);
}
return records;
};
/**
* 根据名称模式推断通信关系
* - 识别指挥节点(包含"command"、"cmd"、"指挥"等关键词)
* - 其他节点连接到最近的指挥节点
*/
const inferByNamPattern = (
platforms: PlatformWithComponents[],
records: CommunicationRecord[]
): void => {
// 识别指挥节点
const commandKeywords = ['command', 'cmd', '指挥', 'chief', 'leader'];
const commandNodes = platforms.filter(p => {
const name = (p.name || '').toLowerCase();
const desc = (p.description || '').toLowerCase();
return commandKeywords.some(kw => name.includes(kw) || desc.includes(kw));
});
// 非指挥节点
const otherNodes = platforms.filter(p => !commandNodes.includes(p));
// 如果有指挥节点,其他节点连接到最近的指挥节点
if (commandNodes.length > 0) {
// 按ID排序,取第一个作为主要指挥中心
const mainCommand = commandNodes.sort((a, b) => a.id - b.id)[0];
if (!mainCommand) return; // 安全检查
// 其他指挥节点也连接到主指挥
commandNodes.slice(1).forEach(cmd => {
records.push({
id: records.length + 1,
command_platform: mainCommand.name || `platform_${mainCommand.id}`,
subordinate_platform: cmd.name || `platform_${cmd.id}`,
scenary_id: mainCommand.scenarioId,
});
});
// 非指挥节点连接到主指挥
otherNodes.forEach(node => {
records.push({
id: records.length + 1,
command_platform: mainCommand.name || `platform_${mainCommand.id}`,
subordinate_platform: node.name || `platform_${node.id}`,
scenary_id: mainCommand.scenarioId,
});
});
} else {
// 没有指挥节点,使用链式连接
createChainConnection(platforms, records);
}
};
/**
* 所有节点连接到第一个节点
*/
const connectAllToFirst = (
platforms: PlatformWithComponents[],
records: CommunicationRecord[]
): void => {
if (platforms.length < 2) return;
const first = platforms[0];
if (!first) return; // 安全检查
const firstName = first.name || `platform_${first.id}`;
platforms.slice(1).forEach(platform => {
records.push({
id: records.length + 1,
command_platform: firstName,
subordinate_platform: platform.name || `platform_${platform.id}`,
scenary_id: first.scenarioId,
});
});
};
/**
* 创建链式连接 A→B→C→D...
*/
const createChainConnection = (
platforms: PlatformWithComponents[],
records: CommunicationRecord[]
): void => {
for (let i = 0; i < platforms.length - 1; i++) {
const current = platforms[i];
const next = platforms[i + 1];
if (!current || !next) continue; // 安全检查
records.push({
id: records.length + 1,
command_platform: current.name || `platform_${current.id}`,
subordinate_platform: next.name || `platform_${next.id}`,
scenary_id: current.scenarioId,
});
}
};
/**
* 创建星型连接(中间节点连接所有其他节点)
*/
const createStarConnection = (
platforms: PlatformWithComponents[],
records: CommunicationRecord[]
): void => {
if (platforms.length < 2) return;
// 选择中间的节点作为中心
const centerIndex = Math.floor(platforms.length / 2);
const center = platforms[centerIndex];
if (!center) return; // 安全检查
const centerName = center.name || `platform_${center.id}`;
platforms.forEach((platform, idx) => {
if (idx !== centerIndex) {
records.push({
id: records.length + 1,
command_platform: centerName,
subordinate_platform: platform.name || `platform_${platform.id}`,
scenary_id: center.scenarioId,
});
}
});
};
/**
* 便捷函数:直接将平台列表转换为图容器
* @param platforms 平台列表
* @param strategy 推断策略
* @param layoutConfig 布局配置(可选)
* @returns 图容器对象
*/
export const convertPlatformsToGraphContainer = (
platforms: PlatformWithComponents[],
strategy: InferenceStrategy = 'by-name-pattern',
layoutConfig?: Partial<LayoutConfig>
): GraphContainer => {
const records = convertPlatformsToCommunicationRecords(platforms, strategy);
return convertRecordsToGraphContainer(records, layoutConfig);
};