416 lines
11 KiB
Vue
416 lines
11 KiB
Vue
<template>
|
||
<div class="rule-decision-tree">
|
||
<div class="rule-decision-tree__toolbar">
|
||
<a-popover placement="bottomLeft" trigger="click" overlay-class-name="rule-decision-tree-snapshot-popover">
|
||
<template #content>
|
||
<pre class="rule-decision-tree__popover-json">{{ previewJson }}</pre>
|
||
</template>
|
||
<a-button type="link" size="small" class="rule-decision-tree__snapshot-btn">
|
||
全局参数快照({{ previewEntryCount }})
|
||
</a-button>
|
||
</a-popover>
|
||
</div>
|
||
|
||
<div v-if="errorMsg" class="rule-decision-tree__banner rule-decision-tree__banner--error">
|
||
{{ errorMsg }}
|
||
</div>
|
||
<div v-else-if="emptyHint" class="rule-decision-tree__banner">
|
||
{{ emptyHint }}
|
||
</div>
|
||
<div v-else-if="payload" class="rule-decision-tree__grid">
|
||
<section
|
||
v-for="block in payload.blocks"
|
||
:key="block.blockId"
|
||
class="rule-decision-tree__pane"
|
||
>
|
||
<header class="rule-decision-tree__pane-header">
|
||
<div class="rule-decision-tree__pane-title">{{ block.title || block.blockId }}</div>
|
||
<div class="rule-decision-tree__pane-meta">
|
||
模块:{{ block.moduleCode || '-' }} / salience:{{ block.salience ?? '-' }}
|
||
</div>
|
||
</header>
|
||
<div
|
||
:ref="(el) => registerHost(block.blockId, el)"
|
||
class="rule-decision-tree__canvas"
|
||
/>
|
||
</section>
|
||
</div>
|
||
</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,
|
||
}>();
|
||
|
||
const payload = ref<RuleDecisionTreePayload | null>(null);
|
||
const errorMsg = ref<string | null>(null);
|
||
const emptyHint = ref<string | null>(null);
|
||
|
||
const hostMap = new Map<string, HTMLDivElement>();
|
||
const graphMap = new Map<string, Graph>();
|
||
const resizeMap = new Map<string, ResizeObserver>();
|
||
const renderVersion = ref(0);
|
||
|
||
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 BLOCK_COLORS: Record<string, string> = {
|
||
equipment: '#5B8FF9',
|
||
target: '#F6903D',
|
||
position: '#61DDAA',
|
||
track: '#9270CA',
|
||
};
|
||
|
||
const NODE_COLORS: Record<string, string> = {
|
||
root: '#5B8FF9',
|
||
input_group: '#5B8FF9',
|
||
input_param: '#4fb3ff',
|
||
step_group: '#61DDAA',
|
||
step: '#49c59c',
|
||
rule_group: '#F6903D',
|
||
rule: '#ff9c4a',
|
||
condition: '#ffd166',
|
||
task_types: '#9ad0f5',
|
||
param_group: '#3db2ff',
|
||
param: '#5ab9ff',
|
||
output_group: '#f08c6c',
|
||
output: '#ffb366',
|
||
outcome_group: '#9270CA',
|
||
outcome: '#a98cf0',
|
||
end: '#e8684a',
|
||
empty: '#6f8194',
|
||
};
|
||
|
||
const registerHost = (blockId: string, el: unknown) => {
|
||
if (!(el instanceof HTMLDivElement)) {
|
||
disposeBlock(blockId);
|
||
hostMap.delete(blockId);
|
||
return;
|
||
}
|
||
hostMap.set(blockId, el);
|
||
};
|
||
|
||
const flattenTree = (block: RuleDecisionBlock) => {
|
||
const nodes: Array<{ id: string; label: string; nodeType: string; valueText?: string | null; description?: string | null }> = [];
|
||
const edges: Array<{ source: string; target: string }> = [];
|
||
const rootId = `block:${block.blockId}:root`;
|
||
|
||
nodes.push({
|
||
id: rootId,
|
||
label: block.title || block.blockId,
|
||
nodeType: 'root',
|
||
valueText: block.salience != null ? `salience=${block.salience}` : undefined,
|
||
description: block.droolsRuleName || undefined,
|
||
});
|
||
|
||
const visit = (node: RuleDecisionNode, parentId: string) => {
|
||
const desc = node.description ? (node.valueText ? `${node.description} | ${node.valueText}` : node.description) : (node.valueText ?? '');
|
||
nodes.push({
|
||
id: node.key,
|
||
label: node.title,
|
||
nodeType: node.nodeType,
|
||
description: desc || undefined,
|
||
});
|
||
edges.push({ source: parentId, target: node.key });
|
||
(node.children ?? []).forEach((child) => visit(child, node.key));
|
||
};
|
||
|
||
(block.nodes ?? []).forEach((node) => visit(node, rootId));
|
||
return { nodes, edges };
|
||
};
|
||
|
||
const disposeBlock = (blockId: string) => {
|
||
resizeMap.get(blockId)?.disconnect();
|
||
resizeMap.delete(blockId);
|
||
const graph = graphMap.get(blockId);
|
||
if (graph) {
|
||
try {
|
||
graph.destroy();
|
||
} catch {
|
||
/* ignore */
|
||
}
|
||
graphMap.delete(blockId);
|
||
}
|
||
const host = hostMap.get(blockId);
|
||
if (host) {
|
||
host.replaceChildren();
|
||
}
|
||
};
|
||
|
||
const renderBlock = async (block: RuleDecisionBlock, version: number) => {
|
||
const host = hostMap.get(block.blockId);
|
||
if (!host) {
|
||
return;
|
||
}
|
||
disposeBlock(block.blockId);
|
||
if (version !== renderVersion.value) {
|
||
return;
|
||
}
|
||
const width = Math.max(host.clientWidth, 320);
|
||
const height = Math.max(host.clientHeight, 240);
|
||
const accent = BLOCK_COLORS[block.blockId] ?? '#5B8FF9';
|
||
const data = flattenTree(block);
|
||
|
||
const graph = new Graph({
|
||
container: host,
|
||
width,
|
||
height,
|
||
data: {
|
||
nodes: data.nodes.map((node) => ({
|
||
id: node.id,
|
||
data: node,
|
||
})),
|
||
edges: data.edges,
|
||
},
|
||
layout: {
|
||
type: 'antv-dagre',
|
||
rankdir: 'TB',
|
||
nodesep: 20,
|
||
ranksep: 42,
|
||
},
|
||
node: {
|
||
type: (model: { data?: { nodeType?: string } }) => {
|
||
const t = model.data?.nodeType;
|
||
if (t === 'input_group') return 'rect';
|
||
if (t === 'end') return 'diamond';
|
||
return 'circle';
|
||
},
|
||
style: {
|
||
size: (model: { data?: { nodeType?: string } }) => {
|
||
const t = model.data?.nodeType;
|
||
if (t === 'root') return 18;
|
||
if (t === 'input_group' || t === 'end') return 20;
|
||
return 14;
|
||
},
|
||
fill: (model: { data?: { nodeType?: string } }) => NODE_COLORS[model.data?.nodeType ?? ''] ?? accent,
|
||
stroke: '#0d1f2c',
|
||
lineWidth: 1.5,
|
||
labelText: (model: { data?: { label?: string; description?: string } }) => {
|
||
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: 160,
|
||
},
|
||
},
|
||
edge: {
|
||
style: {
|
||
stroke: 'rgba(120, 170, 200, 0.52)',
|
||
lineWidth: 1.2,
|
||
endArrow: true,
|
||
},
|
||
},
|
||
behaviors: ['drag-canvas', 'zoom-canvas'],
|
||
});
|
||
|
||
await graph.render();
|
||
if (version !== renderVersion.value) {
|
||
try {
|
||
graph.destroy();
|
||
} catch {
|
||
/* ignore */
|
||
}
|
||
return;
|
||
}
|
||
await graph.fitView({ when: 'always' });
|
||
graphMap.set(block.blockId, graph);
|
||
|
||
const observer = new ResizeObserver(() => {
|
||
const nextWidth = Math.max(host.clientWidth, 320);
|
||
const nextHeight = Math.max(host.clientHeight, 240);
|
||
graph.setSize(nextWidth, nextHeight);
|
||
void graph.fitView({ when: 'overflow' });
|
||
});
|
||
observer.observe(host);
|
||
resizeMap.set(block.blockId, observer);
|
||
};
|
||
|
||
const renderAll = async () => {
|
||
if (!payload.value?.blocks?.length) {
|
||
return;
|
||
}
|
||
await nextTick();
|
||
const version = renderVersion.value;
|
||
for (const block of payload.value.blocks) {
|
||
await renderBlock(block, version);
|
||
}
|
||
};
|
||
|
||
const load = async () => {
|
||
errorMsg.value = null;
|
||
emptyHint.value = null;
|
||
payload.value = null;
|
||
renderVersion.value += 1;
|
||
Array.from(graphMap.keys()).forEach(disposeBlock);
|
||
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 renderAll();
|
||
} 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;
|
||
Array.from(graphMap.keys()).forEach(disposeBlock);
|
||
});
|
||
</script>
|
||
|
||
<style scoped lang="less">
|
||
.rule-decision-tree {
|
||
flex: 1;
|
||
min-height: 0;
|
||
width: 100%;
|
||
display: flex;
|
||
flex-direction: column;
|
||
gap: 6px;
|
||
background: transparent;
|
||
}
|
||
|
||
.rule-decision-tree__toolbar {
|
||
flex: 0 0 auto;
|
||
padding: 0 2px;
|
||
}
|
||
|
||
.rule-decision-tree__snapshot-btn {
|
||
padding-left: 0;
|
||
font-size: 11px;
|
||
color: #8fafbd;
|
||
}
|
||
|
||
.rule-decision-tree__banner {
|
||
flex: 1;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
padding: 12px;
|
||
font-size: 13px;
|
||
color: #a2b1ba;
|
||
text-align: center;
|
||
}
|
||
|
||
.rule-decision-tree__banner--error {
|
||
color: #ff9c9c;
|
||
}
|
||
|
||
.rule-decision-tree__grid {
|
||
flex: 1;
|
||
min-height: 0;
|
||
display: grid;
|
||
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||
grid-template-rows: repeat(2, minmax(0, 1fr));
|
||
gap: 8px;
|
||
}
|
||
|
||
.rule-decision-tree__pane {
|
||
display: flex;
|
||
flex-direction: column;
|
||
min-height: 0;
|
||
border: 1px solid rgba(80, 120, 150, 0.25);
|
||
background: linear-gradient(180deg, rgba(8, 20, 30, 0.78) 0%, rgba(5, 16, 29, 0.9) 100%);
|
||
overflow: hidden;
|
||
}
|
||
|
||
.rule-decision-tree__pane-header {
|
||
flex: 0 0 auto;
|
||
padding: 8px 10px 6px;
|
||
border-bottom: 1px solid rgba(80, 120, 150, 0.2);
|
||
background: rgba(10, 28, 40, 0.78);
|
||
}
|
||
|
||
.rule-decision-tree__pane-title {
|
||
font-size: 14px;
|
||
color: #d8edf6;
|
||
}
|
||
|
||
.rule-decision-tree__pane-meta {
|
||
margin-top: 2px;
|
||
font-size: 11px;
|
||
color: #7f9aaa;
|
||
}
|
||
|
||
.rule-decision-tree__canvas {
|
||
flex: 1;
|
||
min-height: 0;
|
||
width: 100%;
|
||
background: transparent;
|
||
}
|
||
</style>
|
||
|
||
<style lang="less">
|
||
.rule-decision-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-tree__popover-json {
|
||
margin: 0;
|
||
font-size: 10px;
|
||
line-height: 1.35;
|
||
color: #c9dfe8;
|
||
white-space: pre-wrap;
|
||
}
|
||
</style>
|