Files
auto-solution/modeler/src/views/decision/rule-config/RuleKnowledgeGraph.vue

382 lines
11 KiB
Vue
Raw Normal View History

2026-04-17 14:52:10 +08:00
<!--
- 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.
-->
<template>
2026-04-20 09:38:58 +08:00
<div
ref="graphShellRef"
class="rule-knowledge-graph"
:class="{
'rule-knowledge-graph--four-blocks': density === 'four-blocks',
2026-05-07 16:56:16 +08:00
'rule-knowledge-graph--decision-tree': density === 'decision-tree',
2026-04-20 09:38:58 +08:00
'rule-knowledge-graph--fullscreen': isFullscreen,
}"
>
2026-04-17 14:52:10 +08:00
<div class="rule-knowledge-graph__toolbar">
<a-radio-group v-model:value="density" size="small" button-style="solid">
2026-05-07 16:56:16 +08:00
<a-radio-button value="decision-tree-simple">决策树简化</a-radio-button>
<a-radio-button value="decision-tree">决策树</a-radio-button>
<a-radio-button value="full">完整图谱</a-radio-button>
2026-04-20 09:38:58 +08:00
<a-radio-button value="four-blocks">四块分区</a-radio-button>
2026-04-17 14:52:10 +08:00
</a-radio-group>
2026-05-07 16:56:16 +08:00
<span v-if="density === 'decision-tree-simple'" class="rule-knowledge-graph__hint">
决策树简化把装备目标阵位航迹四个规则块合并成一棵总树只保留关键参数核心规则和阶段输出
</span>
<span v-else-if="density === 'decision-tree'" class="rule-knowledge-graph__hint">
决策树按装备目标阵位航迹四个规则块展示输入参数运算步骤命中规则与结果产出
</span>
<span v-else-if="density === 'full'" class="rule-knowledge-graph__hint">
完整图谱包含参数任务类型与执行顺序
2026-04-17 14:52:10 +08:00
</span>
2026-04-20 09:38:58 +08:00
<span v-else class="rule-knowledge-graph__hint rule-knowledge-graph__hint--compact">
2026-05-07 16:56:16 +08:00
四块分区可拖动画布滚轮缩放
2026-04-20 09:38:58 +08:00
</span>
<a-button type="default" size="small" class="rule-knowledge-graph__fullscreen-btn" @click="toggleFullscreen">
<template #icon>
<FullscreenExitOutlined v-if="isFullscreen" />
<FullscreenOutlined v-else />
</template>
{{ isFullscreen ? '退出全屏' : '全屏' }}
</a-button>
2026-04-17 14:52:10 +08:00
</div>
2026-05-07 16:56:16 +08:00
<RuleDecisionTreeSimplePanel v-if="density === 'decision-tree-simple'" :query="query" :refresh-key="refreshKey" />
<RuleDecisionTreePanel v-else-if="density === 'decision-tree'" :query="query" :refresh-key="refreshKey" />
<RuleFourBlocksPanel v-else-if="density === 'four-blocks'" :refresh-key="refreshKey" />
2026-04-20 09:38:58 +08:00
<template v-else>
<div v-if="errorMsg" class="rule-knowledge-graph__banner rule-knowledge-graph__banner--error">
{{ errorMsg }}
</div>
<div v-else-if="emptyHint" class="rule-knowledge-graph__banner">
{{ emptyHint }}
</div>
<div v-show="!errorMsg && !emptyHint" ref="hostRef" class="rule-knowledge-graph__host" />
</template>
2026-04-17 14:52:10 +08:00
</div>
</template>
<script setup lang="ts">
2026-04-20 09:38:58 +08:00
import { FullscreenExitOutlined, FullscreenOutlined } from '@ant-design/icons-vue';
2026-04-17 14:52:10 +08:00
import { Graph } from '@antv/g6';
import { message } from 'ant-design-vue';
2026-04-20 09:38:58 +08:00
import { nextTick, onBeforeUnmount, onMounted, ref, watch } from 'vue';
2026-04-17 14:52:10 +08:00
import { findRuleConfigGraph } from './api';
2026-05-07 16:56:16 +08:00
import RuleDecisionTreeSimplePanel from './RuleDecisionTreeSimplePanel.vue';
import RuleDecisionTreePanel from './RuleDecisionTreePanel.vue';
2026-04-20 09:38:58 +08:00
import RuleFourBlocksPanel from './RuleFourBlocksPanel.vue';
2026-05-07 16:56:16 +08:00
import type { RuleConfigRequest, RuleGraphPayload } from './types';
2026-04-17 14:52:10 +08:00
2026-05-07 16:56:16 +08:00
type RuleGraphDensityMode = 'decision-tree-simple' | 'decision-tree' | 'full' | 'four-blocks';
2026-04-20 09:38:58 +08:00
const emit = defineEmits<{
densityChange: [RuleGraphDensityMode],
}>();
2026-04-17 14:52:10 +08:00
const props = defineProps<{
query: RuleConfigRequest,
refreshKey: number,
}>();
2026-04-20 09:38:58 +08:00
const graphShellRef = ref<HTMLElement | null>(null);
2026-04-17 14:52:10 +08:00
const hostRef = ref<HTMLDivElement | null>(null);
2026-04-20 09:38:58 +08:00
const isFullscreen = ref(false);
2026-04-17 14:52:10 +08:00
const errorMsg = ref<string | null>(null);
const emptyHint = ref<string | null>(null);
2026-05-07 16:56:16 +08:00
const density = ref<RuleGraphDensityMode>('decision-tree-simple');
2026-04-17 14:52:10 +08:00
const lastPayload = ref<RuleGraphPayload | null>(null);
let graph: Graph | null = null;
let resizeObserver: ResizeObserver | null = null;
const NODE_COLORS: Record<string, string> = {
level: '#5B8FF9',
kind: '#61DDAA',
module: '#65789B',
rule: '#F6903D',
param: '#9270CA',
taskType: '#269A99',
};
const toGraphData = (payload: RuleGraphPayload) => ({
nodes: payload.nodes.map((n) => ({
id: n.id,
data: {
label: n.label,
nodeType: n.nodeType,
payload: n.payload ?? {},
},
})),
edges: payload.edges.map((e) => ({
id: e.id,
source: e.source,
target: e.target,
data: {
edgeType: e.edgeType ?? '',
label: e.label ?? '',
},
})),
});
2026-04-20 09:38:58 +08:00
const syncFullscreenState = () => {
const el = graphShellRef.value;
isFullscreen.value = Boolean(el && document.fullscreenElement === el);
};
const refitMainGraphAfterLayout = () => {
if (!graph || !hostRef.value) {
return;
}
const w = Math.max(hostRef.value.clientWidth, 280);
const h = Math.max(hostRef.value.clientHeight, 240);
graph.setSize(w, h);
void graph.fitView();
};
const onFullscreenChange = () => {
syncFullscreenState();
void nextTick(() => {
refitMainGraphAfterLayout();
window.dispatchEvent(new Event('resize'));
window.setTimeout(() => {
refitMainGraphAfterLayout();
}, 120);
});
};
const toggleFullscreen = async () => {
const el = graphShellRef.value;
if (!el) {
return;
}
try {
if (document.fullscreenElement === el) {
await document.exitFullscreen();
} else {
await el.requestFullscreen();
}
} catch {
message.warning('无法切换全屏,请检查浏览器权限或使用 Chrome / Edge');
}
};
2026-04-17 14:52:10 +08:00
const disposeGraph = async () => {
resizeObserver?.disconnect();
resizeObserver = null;
if (graph) {
try {
graph.destroy();
} catch {
/* ignore */
}
graph = null;
}
};
2026-05-07 16:56:16 +08:00
const buildGraph = async (payload: RuleGraphPayload) => {
2026-04-17 14:52:10 +08:00
await disposeGraph();
const el = hostRef.value;
if (!el) {
return;
}
2026-05-07 16:56:16 +08:00
const data = toGraphData(payload);
if (!payload.nodes.length) {
2026-04-17 14:52:10 +08:00
return;
}
const width = Math.max(el.clientWidth, 280);
const height = Math.max(el.clientHeight, 240);
graph = new Graph({
container: el,
width,
height,
data,
2026-05-07 16:56:16 +08:00
layout: {
type: 'd3-force' as const,
},
2026-04-17 14:52:10 +08:00
node: {
style: {
2026-05-07 16:56:16 +08:00
size: (d: { data?: { nodeType?: string } }) => (d.data?.nodeType === 'param' ? 5 : 11),
2026-04-17 14:52:10 +08:00
fill: (d: { data?: { nodeType?: string } }) => NODE_COLORS[d.data?.nodeType ?? ''] ?? '#8B8B8B',
labelText: (d: { data?: { label?: string }; id: string }) => d.data?.label ?? String(d.id),
labelFill: '#e8f4f8',
2026-05-07 16:56:16 +08:00
labelFontSize: 9,
labelMaxWidth: 100,
2026-04-17 14:52:10 +08:00
labelWordWrap: true,
lineWidth: 1.5,
stroke: '#0d1f2c',
},
},
edge: {
style: {
2026-05-07 16:56:16 +08:00
stroke: (d: { data?: { edgeType?: string } }) =>
(d.data?.edgeType === 'rule_exec_before' ? '#faad14' : 'rgba(150, 175, 190, 0.35)'),
2026-04-17 14:52:10 +08:00
lineWidth: (d: { data?: { edgeType?: string } }) =>
(d.data?.edgeType === 'rule_exec_before' ? 2 : 1),
2026-05-07 16:56:16 +08:00
endArrow: true,
2026-04-17 14:52:10 +08:00
},
},
behaviors: ['drag-canvas', 'zoom-canvas', 'drag-element'],
});
await graph.render();
await graph.fitView();
resizeObserver = new ResizeObserver(() => {
if (!graph || !hostRef.value) {
return;
}
const w = Math.max(hostRef.value.clientWidth, 280);
const h = Math.max(hostRef.value.clientHeight, 240);
graph.setSize(w, h);
void graph.fitView();
});
resizeObserver.observe(el);
};
const renderFromCache = async () => {
2026-05-07 16:56:16 +08:00
if (density.value !== 'full' || !lastPayload.value || !lastPayload.value.nodes?.length) {
2026-04-17 14:52:10 +08:00
return;
}
await nextTick();
2026-05-07 16:56:16 +08:00
await buildGraph(lastPayload.value);
2026-04-17 14:52:10 +08:00
};
const load = async () => {
errorMsg.value = null;
emptyHint.value = null;
await disposeGraph();
try {
const r = await findRuleConfigGraph({
pageNum: props.query.pageNum,
pageSize: props.query.pageSize,
ruleCode: props.query.ruleCode ?? undefined,
ruleName: props.query.ruleName ?? undefined,
levelCode: props.query.levelCode ?? undefined,
kindCode: props.query.kindCode ?? undefined,
moduleCode: props.query.moduleCode ?? undefined,
enabled: props.query.enabled ?? undefined,
});
if (r.code !== 200 || !r.data) {
errorMsg.value = r.msg ?? '加载知识图谱失败';
lastPayload.value = null;
return;
}
2026-05-07 16:56:16 +08:00
if (!r.data.nodes?.length) {
2026-04-17 14:52:10 +08:00
emptyHint.value = '当前页无规则数据,图谱为空';
lastPayload.value = null;
return;
}
2026-05-07 16:56:16 +08:00
lastPayload.value = r.data;
2026-04-17 14:52:10 +08:00
await renderFromCache();
} catch (e: unknown) {
const msg = e instanceof Error ? e.message : String(e);
errorMsg.value = `图谱请求失败:${msg}`;
lastPayload.value = null;
message.error(errorMsg.value);
}
};
watch(
() => [props.query.pageNum, props.query.pageSize, props.refreshKey],
() => {
2026-05-07 16:56:16 +08:00
if (density.value !== 'full') {
2026-04-20 09:38:58 +08:00
return;
}
2026-04-17 14:52:10 +08:00
void load();
},
{ immediate: true },
);
2026-04-20 09:38:58 +08:00
watch(density, async (mode, prev) => {
emit('densityChange', mode);
2026-05-07 16:56:16 +08:00
if (mode !== 'full') {
2026-04-20 09:38:58 +08:00
await disposeGraph();
return;
}
2026-05-07 16:56:16 +08:00
if (prev !== 'full') {
2026-04-20 09:38:58 +08:00
await load();
return;
}
await renderFromCache();
});
onMounted(() => {
syncFullscreenState();
document.addEventListener('fullscreenchange', onFullscreenChange);
2026-04-17 14:52:10 +08:00
});
onBeforeUnmount(() => {
2026-04-20 09:38:58 +08:00
document.removeEventListener('fullscreenchange', onFullscreenChange);
2026-04-17 14:52:10 +08:00
void disposeGraph();
});
</script>
<style scoped lang="less">
.rule-knowledge-graph {
flex: 1;
min-height: 0;
width: 100%;
display: flex;
flex-direction: column;
gap: 6px;
}
.rule-knowledge-graph__toolbar {
flex: 0 0 auto;
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
padding: 0 2px 4px;
}
2026-04-20 09:38:58 +08:00
.rule-knowledge-graph__fullscreen-btn {
margin-left: auto;
flex-shrink: 0;
color: #b8ccd6;
border-color: rgba(120, 170, 200, 0.45);
background: rgba(10, 28, 40, 0.65);
}
2026-04-17 14:52:10 +08:00
.rule-knowledge-graph__hint {
font-size: 11px;
color: #7a8d96;
line-height: 1.35;
}
2026-04-20 09:38:58 +08:00
.rule-knowledge-graph__hint--compact {
2026-05-07 16:56:16 +08:00
letter-spacing: 0.2px;
2026-04-17 14:52:10 +08:00
}
.rule-knowledge-graph__banner {
flex: 1;
display: flex;
align-items: center;
justify-content: center;
padding: 12px;
font-size: 13px;
color: #a2b1ba;
text-align: center;
}
.rule-knowledge-graph__banner--error {
color: #ff9c9c;
}
2026-05-07 16:56:16 +08:00
.rule-knowledge-graph__host {
flex: 1;
min-height: 0;
width: 100%;
border: 1px solid rgba(80, 120, 150, 0.18);
border-radius: 4px;
background: rgba(4, 18, 28, 0.35);
}
.rule-knowledge-graph--fullscreen {
background: #061522;
padding: 12px;
}
2026-04-17 14:52:10 +08:00
</style>