规则界面优化补充

This commit is contained in:
MHW
2026-05-13 14:00:00 +08:00
parent 4f71fc815b
commit 487fffc2d2
4 changed files with 511 additions and 3 deletions

View File

@@ -0,0 +1,502 @@
<template>
<div class="rule-decision-binary-tree">
<div class="rule-decision-binary-tree__toolbar">
<a-popover placement="bottomLeft" trigger="click" overlay-class-name="rule-decision-binary-tree-snapshot-popover">
<template #content>
<pre class="rule-decision-binary-tree__popover-json">{{ previewJson }}</pre>
</template>
<a-button type="link" size="small" class="rule-decision-binary-tree__snapshot-btn">
全局参数快照({{ previewEntryCount }})
</a-button>
</a-popover>
</div>
<div v-if="errorMsg" class="rule-decision-binary-tree__banner rule-decision-binary-tree__banner--error">
{{ errorMsg }}
</div>
<div v-else-if="emptyHint" class="rule-decision-binary-tree__banner">
{{ emptyHint }}
</div>
<div v-else ref="hostRef" class="rule-decision-binary-tree__canvas" />
</div>
</template>
<script setup lang="ts">
import { Graph } from '@antv/g6';
import { message } from 'ant-design-vue';
import { computed, nextTick, onBeforeUnmount, ref, watch } from 'vue';
import { findRuleDecisionTree } from './api';
import type { RuleConfigRequest, RuleDecisionBlock, RuleDecisionNode, RuleDecisionTreePayload } from './types';
const props = defineProps<{
query: RuleConfigRequest,
refreshKey: number,
}>();
type BinaryGraphNode = {
id: string,
label: string,
nodeType: string,
description?: string,
fullText?: string,
};
const hostRef = ref<HTMLDivElement | null>(null);
const errorMsg = ref<string | null>(null);
const emptyHint = ref<string | null>(null);
const payload = ref<RuleDecisionTreePayload | null>(null);
const renderVersion = ref(0);
let graph: Graph | null = null;
let resizeObserver: ResizeObserver | null = null;
const previewEntryCount = computed(() => {
const p = payload.value?.globalParamsPreview;
return p ? Object.keys(p).length : 0;
});
const previewJson = computed(() => {
const p = payload.value?.globalParamsPreview;
if (!p) {
return '';
}
try {
return JSON.stringify(p, null, 2);
} catch {
return String(p);
}
});
const NODE_COLORS: Record<string, string> = {
start: '#4fb3ff',
decision: '#ffd166',
if: '#49c59c',
else: '#ff9c4a',
output: '#9270CA',
end: '#e8684a',
};
const BLOCK_TITLE: Record<string, string> = {
equipment: '装备规则',
target: '目标规则',
position: '阵位规则',
track: '航迹规则',
};
const flatten = (nodes: RuleDecisionNode[] | null | undefined): RuleDecisionNode[] => {
const out: RuleDecisionNode[] = [];
const visit = (list: RuleDecisionNode[] | null | undefined) => {
(list ?? []).forEach((node) => {
out.push(node);
visit(node.children ?? []);
});
};
visit(nodes);
return out;
};
const pickImportantParams = (flatNodes: RuleDecisionNode[]) =>
flatNodes.filter((node) => node.nodeType === 'input_param' || node.nodeType === 'param').slice(0, 2);
const pickConditions = (flatNodes: RuleDecisionNode[]) =>
flatNodes.filter((node) => node.nodeType === 'condition').slice(0, 1);
const pickOutputs = (flatNodes: RuleDecisionNode[]) =>
flatNodes.filter((node) => node.nodeType === 'output' || node.nodeType === 'outcome').slice(0, 2);
const shortText = (text: string | null | undefined, max = 44) => {
const value = String(text ?? '').replace(/\s+/g, ' ').trim();
if (!value) {
return '';
}
return value.length > max ? `${value.slice(0, max)}...` : value;
};
const buildConditionText = (block: RuleDecisionBlock) => {
const flatNodes = flatten(block.nodes);
const condition = pickConditions(flatNodes)[0];
if (condition?.description) {
return shortText(condition.description, 52);
}
const param = pickImportantParams(flatNodes)[0];
if (param) {
return `${param.title} 是否满足当前规则条件`;
}
return `${BLOCK_TITLE[block.blockId] ?? block.title ?? block.blockId} 是否命中`;
};
const buildIfText = (block: RuleDecisionBlock) => {
const flatNodes = flatten(block.nodes);
const outputs = pickOutputs(flatNodes);
if (outputs.length > 0) {
return outputs.map((item) => shortText(item.title || item.description, 30)).join(';');
}
return `进入${BLOCK_TITLE[block.blockId] ?? block.title ?? block.blockId}处理`;
};
const buildElseText = (block: RuleDecisionBlock) =>
`不满足当前条件,跳过${BLOCK_TITLE[block.blockId] ?? block.title ?? block.blockId},继续后续判断`;
const buildBinaryGraph = (tree: RuleDecisionTreePayload) => {
const nodes: BinaryGraphNode[] = [];
const edges: Array<{ source: string; target: string; label?: string }> = [];
const startId = 'binary:start';
nodes.push({
id: startId,
label: '开始',
nodeType: 'start',
description: '载入关键参数,进入规则判断',
fullText: '开始:载入关键参数,进入规则判断',
});
const blocks = [...(tree.blocks ?? [])];
const attachSubtree = (parentId: string, branchLabel: string, branchType: 'if' | 'else', description: string, blockIndex: number) => {
const branchId = `${parentId}:${branchType}:${blockIndex}`;
nodes.push({
id: branchId,
label: branchType === 'if' ? 'If' : 'Else',
nodeType: branchType,
description,
fullText: `${branchType === 'if' ? 'If' : 'Else'}:${description}`,
});
edges.push({ source: parentId, target: branchId, label: branchLabel });
if (blockIndex >= blocks.length) {
const endId = `${branchId}:end`;
nodes.push({
id: endId,
label: '输出 / 结束',
nodeType: 'end',
description: '输出最终规则结果',
});
edges.push({ source: branchId, target: endId });
return;
}
const block = blocks[blockIndex];
if (!block) {
const endId = `${branchId}:end:fallback`;
nodes.push({
id: endId,
label: '输出 / 结束',
nodeType: 'end',
description: '输出最终规则结果',
});
edges.push({ source: branchId, target: endId });
return;
}
const decisionId = `${branchId}:decision:${block.blockId}:${blockIndex}`;
nodes.push({
id: decisionId,
label: `${BLOCK_TITLE[block.blockId] ?? block.title ?? block.blockId}判断`,
nodeType: 'decision',
description: buildConditionText(block),
fullText: `${BLOCK_TITLE[block.blockId] ?? block.title ?? block.blockId}判断:${buildConditionText(block)}`,
});
edges.push({ source: branchId, target: decisionId });
attachSubtree(decisionId, '满足', 'if', buildIfText(block), blockIndex + 1);
attachSubtree(decisionId, '不满足', 'else', buildElseText(block), blockIndex + 1);
};
if (!blocks.length) {
const endId = 'binary:end:empty';
nodes.push({
id: endId,
label: '输出 / 结束',
nodeType: 'end',
description: '当前没有可用规则',
fullText: '输出 / 结束:当前没有可用规则',
});
edges.push({ source: startId, target: endId });
return { nodes, edges };
}
const first = blocks[0];
if (!first) {
const endId = 'binary:end:fallback';
nodes.push({
id: endId,
label: '输出 / 结束',
nodeType: 'end',
description: '当前没有可用规则',
});
edges.push({ source: startId, target: endId });
return { nodes, edges };
}
const firstDecisionId = `binary:decision:${first.blockId}:0`;
nodes.push({
id: firstDecisionId,
label: `${BLOCK_TITLE[first.blockId] ?? first.title ?? first.blockId}判断`,
nodeType: 'decision',
description: buildConditionText(first),
fullText: `${BLOCK_TITLE[first.blockId] ?? first.title ?? first.blockId}判断:${buildConditionText(first)}`,
});
edges.push({ source: startId, target: firstDecisionId });
attachSubtree(firstDecisionId, '满足', 'if', buildIfText(first), 1);
attachSubtree(firstDecisionId, '不满足', 'else', buildElseText(first), 1);
return { nodes, edges };
};
const disposeGraph = () => {
resizeObserver?.disconnect();
resizeObserver = null;
if (graph) {
try {
graph.destroy();
} catch {
/* ignore */
}
graph = null;
}
hostRef.value?.replaceChildren();
};
const renderGraph = async (tree: RuleDecisionTreePayload, version: number) => {
const host = hostRef.value;
if (!host) {
return;
}
disposeGraph();
if (version !== renderVersion.value) {
return;
}
const data = buildBinaryGraph(tree);
graph = new Graph({
container: host,
width: Math.max(host.clientWidth, 320),
height: Math.max(host.clientHeight, 260),
data: {
nodes: data.nodes.map((node) => ({
id: node.id,
data: node,
})),
edges: data.edges.map((edge, index) => ({
id: `edge:${index}:${edge.source}:${edge.target}`,
source: edge.source,
target: edge.target,
data: { label: edge.label ?? '' },
})),
},
layout: {
type: 'antv-dagre',
rankdir: 'TB',
nodesep: 28,
ranksep: 42,
},
node: {
type: (model: { data?: { nodeType?: string } }) => {
const t = model.data?.nodeType;
if (t === 'start') return 'rect';
if (t === 'end' || t === 'decision') return 'diamond';
return 'circle';
},
style: {
size: (model: { data?: { nodeType?: string } }) => {
const t = model.data?.nodeType;
if (t === 'start') return 24;
if (t === 'end') return 22;
if (t === 'decision') return 26;
return 18;
},
fill: (model: { data?: { nodeType?: string } }) => NODE_COLORS[model.data?.nodeType ?? 'decision'] ?? '#5B8FF9',
stroke: '#0d1f2c',
lineWidth: 1.5,
labelText: (model: { data?: { label?: string; description?: string; fullText?: string; nodeType?: string } }) => {
const fullText = model.data?.fullText;
if (fullText) {
return fullText;
}
const label = model.data?.label ?? '';
const description = model.data?.description;
return description ? `${label}\n${description}` : label;
},
labelFill: '#e8f4f8',
labelFontSize: 11,
labelLineHeight: 15,
labelWordWrap: true,
labelMaxWidth: 320,
labelPlacement: (model: { data?: { nodeType?: string } }) => {
const t = model.data?.nodeType;
if (t === 'decision' || t === 'if' || t === 'else' || t === 'output' || t === 'end') {
return 'right';
}
return 'bottom';
},
labelOffsetX: (model: { data?: { nodeType?: string } }) => {
const t = model.data?.nodeType;
if (t === 'decision' || t === 'if' || t === 'else' || t === 'output' || t === 'end') {
return 14;
}
return 0;
},
labelTextAlign: (model: { data?: { nodeType?: string } }) => {
const t = model.data?.nodeType;
if (t === 'decision' || t === 'if' || t === 'else' || t === 'output' || t === 'end') {
return 'left';
}
return 'center';
},
labelBackground: true,
labelBackgroundFill: 'rgba(6, 21, 40, 0.88)',
labelBackgroundRadius: 4,
labelPadding: [6, 8],
},
},
edge: {
style: {
stroke: 'rgba(120, 170, 200, 0.52)',
lineWidth: 1.2,
endArrow: true,
labelText: (edge: { data?: { label?: string } }) => edge.data?.label ?? '',
labelFill: '#9ad0f5',
labelFontSize: 11,
labelBackground: true,
labelBackgroundFill: 'rgba(6, 21, 40, 0.85)',
labelBackgroundRadius: 3,
labelPadding: [2, 6],
},
},
behaviors: ['drag-canvas', 'zoom-canvas'],
});
await graph.render();
if (version !== renderVersion.value) {
disposeGraph();
return;
}
await graph.fitView({ when: 'always' });
resizeObserver = new ResizeObserver(() => {
if (!graph || !hostRef.value) {
return;
}
graph.setSize(Math.max(hostRef.value.clientWidth, 320), Math.max(hostRef.value.clientHeight, 260));
void graph.fitView({ when: 'overflow' });
});
resizeObserver.observe(host);
};
const load = async () => {
errorMsg.value = null;
emptyHint.value = null;
payload.value = null;
renderVersion.value += 1;
disposeGraph();
try {
const r = await findRuleDecisionTree({
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 ?? '加载二叉决策树失败';
return;
}
if (!r.data.blocks?.length) {
emptyHint.value = '当前没有可展示的二叉决策树数据';
return;
}
payload.value = r.data;
await nextTick();
await renderGraph(r.data, renderVersion.value);
} catch (e: unknown) {
const msg = e instanceof Error ? e.message : String(e);
errorMsg.value = `二叉决策树请求失败:${msg}`;
message.error(errorMsg.value);
}
};
watch(
() => [props.query.pageNum, props.query.pageSize, props.refreshKey],
() => {
void load();
},
{ immediate: true },
);
onBeforeUnmount(() => {
renderVersion.value += 1;
disposeGraph();
});
</script>
<style scoped lang="less">
.rule-decision-binary-tree {
flex: 1;
min-height: 0;
width: 100%;
display: flex;
flex-direction: column;
gap: 6px;
background: transparent;
}
.rule-decision-binary-tree__toolbar {
flex: 0 0 auto;
padding: 0 2px;
}
.rule-decision-binary-tree__snapshot-btn {
padding-left: 0;
font-size: 11px;
color: #8fafbd;
}
.rule-decision-binary-tree__banner {
flex: 1;
display: flex;
align-items: center;
justify-content: center;
padding: 12px;
font-size: 13px;
color: #a2b1ba;
text-align: center;
}
.rule-decision-binary-tree__banner--error {
color: #ff9c9c;
}
.rule-decision-binary-tree__canvas {
flex: 1;
min-height: 0;
width: 100%;
border: 1px solid rgba(80, 120, 150, 0.22);
border-radius: 4px;
background: rgba(4, 18, 28, 0.35);
}
</style>
<style lang="less">
.rule-decision-binary-tree-snapshot-popover {
.ant-popover-inner-content {
max-width: min(520px, 90vw);
max-height: 70vh;
overflow: auto;
background: #081e3b;
}
.ant-popover-inner {
background: #081e3b;
}
}
.rule-decision-binary-tree__popover-json {
margin: 0;
font-size: 10px;
line-height: 1.35;
color: #c9dfe8;
white-space: pre-wrap;
}
</style>

View File

@@ -19,6 +19,7 @@
<div class="rule-knowledge-graph__toolbar"> <div class="rule-knowledge-graph__toolbar">
<a-radio-group v-model:value="density" size="small" button-style="solid"> <a-radio-group v-model:value="density" size="small" button-style="solid">
<a-radio-button value="decision-tree-simple">决策树(简化)</a-radio-button> <a-radio-button value="decision-tree-simple">决策树(简化)</a-radio-button>
<a-radio-button value="decision-tree-binary">决策树(二叉树)</a-radio-button>
<a-radio-button value="decision-tree">决策树</a-radio-button> <a-radio-button value="decision-tree">决策树</a-radio-button>
<a-radio-button value="full">完整图谱</a-radio-button> <a-radio-button value="full">完整图谱</a-radio-button>
<a-radio-button value="four-blocks">四块分区</a-radio-button> <a-radio-button value="four-blocks">四块分区</a-radio-button>
@@ -26,6 +27,9 @@
<span v-if="density === 'decision-tree-simple'" class="rule-knowledge-graph__hint"> <span v-if="density === 'decision-tree-simple'" class="rule-knowledge-graph__hint">
决策树(简化):把装备、目标、阵位、航迹四个规则块合并成一棵总树,只保留关键参数、核心规则和阶段输出 决策树(简化):把装备、目标、阵位、航迹四个规则块合并成一棵总树,只保留关键参数、核心规则和阶段输出
</span> </span>
<span v-else-if="density === 'decision-tree-binary'" class="rule-knowledge-graph__hint">
决策树(二叉树):按 if / else 方式展示关键参数条件判断、分支去向与最终输出
</span>
<span v-else-if="density === 'decision-tree'" class="rule-knowledge-graph__hint"> <span v-else-if="density === 'decision-tree'" class="rule-knowledge-graph__hint">
决策树:按装备、目标、阵位、航迹四个规则块展示输入参数、运算步骤、命中规则与结果产出 决策树:按装备、目标、阵位、航迹四个规则块展示输入参数、运算步骤、命中规则与结果产出
</span> </span>
@@ -44,6 +48,7 @@
</a-button> </a-button>
</div> </div>
<RuleDecisionTreeSimplePanel v-if="density === 'decision-tree-simple'" :query="query" :refresh-key="refreshKey" /> <RuleDecisionTreeSimplePanel v-if="density === 'decision-tree-simple'" :query="query" :refresh-key="refreshKey" />
<RuleDecisionBinaryTreePanel v-else-if="density === 'decision-tree-binary'" :query="query" :refresh-key="refreshKey" />
<RuleDecisionTreePanel v-else-if="density === 'decision-tree'" :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" /> <RuleFourBlocksPanel v-else-if="density === 'four-blocks'" :refresh-key="refreshKey" />
<template v-else> <template v-else>
@@ -64,12 +69,13 @@ import { Graph } from '@antv/g6';
import { message } from 'ant-design-vue'; import { message } from 'ant-design-vue';
import { nextTick, onBeforeUnmount, onMounted, ref, watch } from 'vue'; import { nextTick, onBeforeUnmount, onMounted, ref, watch } from 'vue';
import { findRuleConfigGraph } from './api'; import { findRuleConfigGraph } from './api';
import RuleDecisionBinaryTreePanel from './RuleDecisionBinaryTreePanel.vue';
import RuleDecisionTreeSimplePanel from './RuleDecisionTreeSimplePanel.vue'; import RuleDecisionTreeSimplePanel from './RuleDecisionTreeSimplePanel.vue';
import RuleDecisionTreePanel from './RuleDecisionTreePanel.vue'; import RuleDecisionTreePanel from './RuleDecisionTreePanel.vue';
import RuleFourBlocksPanel from './RuleFourBlocksPanel.vue'; import RuleFourBlocksPanel from './RuleFourBlocksPanel.vue';
import type { RuleConfigRequest, RuleGraphPayload } from './types'; import type { RuleConfigRequest, RuleGraphPayload } from './types';
type RuleGraphDensityMode = 'decision-tree-simple' | 'decision-tree' | 'full' | 'four-blocks'; type RuleGraphDensityMode = 'decision-tree-simple' | 'decision-tree-binary' | 'decision-tree' | 'full' | 'four-blocks';
const emit = defineEmits<{ const emit = defineEmits<{
densityChange: [RuleGraphDensityMode], densityChange: [RuleGraphDensityMode],

View File

@@ -316,7 +316,7 @@ const clampRightPanelWidth = (n: number) => {
return Math.min(dynamicMax, Math.max(RIGHT_PANEL_MIN, Math.round(n))); return Math.min(dynamicMax, Math.max(RIGHT_PANEL_MIN, Math.round(n)));
}; };
const onRuleGraphDensityChange = (mode: 'decision-tree-simple' | 'decision-tree' | 'full' | 'four-blocks') => { const onRuleGraphDensityChange = (mode: 'decision-tree-simple' | 'decision-tree-binary' | 'decision-tree' | 'full' | 'four-blocks') => {
if (mode === 'four-blocks') { if (mode === 'four-blocks') {
if (graphPanePercentBeforeFourBlocks.value === null) { if (graphPanePercentBeforeFourBlocks.value === null) {
graphPanePercentBeforeFourBlocks.value = graphPanePercent.value; graphPanePercentBeforeFourBlocks.value = graphPanePercent.value;