Compare commits
3 Commits
873c1a1d20
...
d6b70b1750
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d6b70b1750 | ||
|
|
c92a374591 | ||
|
|
a2aba82b6e |
@@ -0,0 +1,16 @@
|
|||||||
|
package com.lanyuanxiaoyao.service.ai.web.flow;
|
||||||
|
|
||||||
|
import com.yomahub.liteflow.core.NodeComponent;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author lanyuanxiaoyao
|
||||||
|
* @version 20250625
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public abstract class BaseNode extends NodeComponent {
|
||||||
|
@Override
|
||||||
|
public void process() throws Exception {
|
||||||
|
log.info(getClass().getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package com.lanyuanxiaoyao.service.ai.web.flow;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author lanyuanxiaoyao
|
||||||
|
* @version 20250625
|
||||||
|
*/
|
||||||
|
public class EndNode extends BaseNode {
|
||||||
|
@Override
|
||||||
|
public void process() throws Exception {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,304 @@
|
|||||||
|
package com.lanyuanxiaoyao.service.ai.web.flow;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.datatype.eclipsecollections.EclipseCollectionsModule;
|
||||||
|
import com.yomahub.liteflow.builder.LiteFlowNodeBuilder;
|
||||||
|
import com.yomahub.liteflow.core.NodeComponent;
|
||||||
|
import com.yomahub.liteflow.enums.NodeTypeEnum;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.Queue;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.eclipse.collections.api.factory.Lists;
|
||||||
|
import org.eclipse.collections.api.factory.Maps;
|
||||||
|
import org.eclipse.collections.api.list.ImmutableList;
|
||||||
|
import org.eclipse.collections.api.list.MutableList;
|
||||||
|
import org.eclipse.collections.api.map.ImmutableMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author lanyuanxiaoyao
|
||||||
|
* @version 20250625
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class LiteFlowService {
|
||||||
|
public LiteFlowService() {
|
||||||
|
createNode("start-amis-node", NodeTypeEnum.COMMON, StartNode.class);
|
||||||
|
createNode("end-amis-node", NodeTypeEnum.COMMON, EndNode.class);
|
||||||
|
createNode("llm-amis-node", NodeTypeEnum.COMMON, LlmNode.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void createNode(String name, NodeTypeEnum type, Class<? extends NodeComponent> clazz) {
|
||||||
|
LiteFlowNodeBuilder.createNode()
|
||||||
|
.setId(name)
|
||||||
|
.setName(name)
|
||||||
|
.setType(type)
|
||||||
|
.setClazz(clazz)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws JsonProcessingException {
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
mapper.registerModule(new EclipseCollectionsModule());
|
||||||
|
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||||
|
// language=JSON
|
||||||
|
String source = """
|
||||||
|
{
|
||||||
|
"nodes": [
|
||||||
|
{
|
||||||
|
"id": "A",
|
||||||
|
"type": "start",
|
||||||
|
"position": {
|
||||||
|
"x": 8,
|
||||||
|
"y": 272
|
||||||
|
},
|
||||||
|
"data": {},
|
||||||
|
"measured": {
|
||||||
|
"width": 256,
|
||||||
|
"height": 75
|
||||||
|
},
|
||||||
|
"selected": false,
|
||||||
|
"dragging": false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "F",
|
||||||
|
"type": "end",
|
||||||
|
"position": {
|
||||||
|
"x": 1439.5556937134281,
|
||||||
|
"y": 282.2797340760818
|
||||||
|
},
|
||||||
|
"data": {},
|
||||||
|
"measured": {
|
||||||
|
"width": 256,
|
||||||
|
"height": 75
|
||||||
|
},
|
||||||
|
"selected": false,
|
||||||
|
"dragging": false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "C",
|
||||||
|
"type": "normal",
|
||||||
|
"position": {
|
||||||
|
"x": 902.6781018665707,
|
||||||
|
"y": 115.31234529524048
|
||||||
|
},
|
||||||
|
"data": {},
|
||||||
|
"measured": {
|
||||||
|
"width": 256,
|
||||||
|
"height": 75
|
||||||
|
},
|
||||||
|
"selected": false,
|
||||||
|
"dragging": false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "B",
|
||||||
|
"type": "normal",
|
||||||
|
"position": {
|
||||||
|
"x": 338,
|
||||||
|
"y": 287
|
||||||
|
},
|
||||||
|
"data": {},
|
||||||
|
"measured": {
|
||||||
|
"width": 256,
|
||||||
|
"height": 75
|
||||||
|
},
|
||||||
|
"selected": false,
|
||||||
|
"dragging": false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "E",
|
||||||
|
"type": "normal",
|
||||||
|
"position": {
|
||||||
|
"x": 1086.6322978498904,
|
||||||
|
"y": 371.3061114283591
|
||||||
|
},
|
||||||
|
"data": {},
|
||||||
|
"measured": {
|
||||||
|
"width": 256,
|
||||||
|
"height": 75
|
||||||
|
},
|
||||||
|
"selected": true,
|
||||||
|
"dragging": false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "D",
|
||||||
|
"type": "normal",
|
||||||
|
"position": {
|
||||||
|
"x": 700.0944461714178,
|
||||||
|
"y": 369.84258971430364
|
||||||
|
},
|
||||||
|
"data": {},
|
||||||
|
"measured": {
|
||||||
|
"width": 256,
|
||||||
|
"height": 75
|
||||||
|
},
|
||||||
|
"selected": false,
|
||||||
|
"dragging": false
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"edges": [
|
||||||
|
{
|
||||||
|
"source": "A",
|
||||||
|
"target": "B",
|
||||||
|
"id": "xy-edge__A-B"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"source": "B",
|
||||||
|
"target": "C",
|
||||||
|
"id": "xy-edge__B-C"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"source": "C",
|
||||||
|
"target": "F",
|
||||||
|
"id": "xy-edge__C-F"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"source": "D",
|
||||||
|
"target": "E",
|
||||||
|
"id": "xy-edge__D-E"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"source": "B",
|
||||||
|
"target": "D",
|
||||||
|
"id": "xy-edge__B-D"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"source": "E",
|
||||||
|
"target": "F",
|
||||||
|
"id": "xy-edge__E-F"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"data": {
|
||||||
|
"A": {
|
||||||
|
"inputs": {
|
||||||
|
"name": {
|
||||||
|
"type": "text"
|
||||||
|
},
|
||||||
|
"description": {
|
||||||
|
"type": "text",
|
||||||
|
"description": "文件描述"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"C": {
|
||||||
|
"model": "qwen3",
|
||||||
|
"outputs": {
|
||||||
|
"text": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"systemPrompt": "你是个沙雕"
|
||||||
|
},
|
||||||
|
"B": {
|
||||||
|
"count": 3,
|
||||||
|
"score": 0.75,
|
||||||
|
"knowledgeId": 3585368238960640,
|
||||||
|
"query": "hello world"
|
||||||
|
},
|
||||||
|
"E": {
|
||||||
|
"type": "python",
|
||||||
|
"content": "code='hello'\\nprint(code)"
|
||||||
|
},
|
||||||
|
"D": {
|
||||||
|
"model": "qwen3",
|
||||||
|
"outputs": {
|
||||||
|
"text": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"systemPrompt": "你是个聪明人"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
FlowData root = mapper.readValue(StrUtil.trim(source), FlowData.class);
|
||||||
|
log.info("\n{}", buildEl(root.nodes, root.edges));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String buildEl(ImmutableList<FlowData.Node> nodes, ImmutableList<FlowData.Edge> edges) {
|
||||||
|
var nodeMap = nodes.toMap(FlowData.Node::getId, node -> node);
|
||||||
|
var adjacencyGraph = Maps.mutable.<String, MutableList<String>>empty();
|
||||||
|
var reverseAdjacencyGraph = Maps.mutable.<String, MutableList<String>>empty();
|
||||||
|
var inDegree = Maps.mutable.<String, Integer>empty();
|
||||||
|
|
||||||
|
nodes.forEach(node -> {
|
||||||
|
adjacencyGraph.put(node.getId(), Lists.mutable.empty());
|
||||||
|
reverseAdjacencyGraph.put(node.getId(), Lists.mutable.empty());
|
||||||
|
inDegree.put(node.getId(), 0);
|
||||||
|
});
|
||||||
|
edges.forEach(edge -> {
|
||||||
|
adjacencyGraph.get(edge.getSource()).add(edge.getTarget());
|
||||||
|
reverseAdjacencyGraph.get(edge.getTarget()).add(edge.getSource());
|
||||||
|
inDegree.put(edge.getTarget(), inDegree.get(edge.getTarget()) + 1);
|
||||||
|
});
|
||||||
|
|
||||||
|
Queue<String> queue = new LinkedList<>();
|
||||||
|
var topologicalSortedList = Lists.mutable.<String>empty();
|
||||||
|
|
||||||
|
inDegree.forEachKeyValue((id, count) -> {
|
||||||
|
if (count == 0) {
|
||||||
|
queue.offer(id);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
while (!queue.isEmpty()) {
|
||||||
|
String id = queue.poll();
|
||||||
|
topologicalSortedList.add(id);
|
||||||
|
for (var neighborId : adjacencyGraph.get(id)) {
|
||||||
|
inDegree.put(neighborId, inDegree.get(neighborId) - 1);
|
||||||
|
if (inDegree.get(neighborId) == 0) {
|
||||||
|
queue.offer(neighborId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
topologicalSortedList.forEach(id -> log.info("{} {}", id, adjacencyGraph.get(id)));
|
||||||
|
topologicalSortedList.forEach(id -> log.info("{} {}", id, reverseAdjacencyGraph.get(id)));
|
||||||
|
|
||||||
|
var nodeQueue = new LinkedList<>(topologicalSortedList);
|
||||||
|
var chains = Lists.mutable.<MutableList<String>>empty();
|
||||||
|
while (!nodeQueue.isEmpty()) {
|
||||||
|
String currentId = nodeQueue.poll();
|
||||||
|
var subChain = Lists.mutable.<String>empty();
|
||||||
|
while (true) {
|
||||||
|
subChain.add(currentId);
|
||||||
|
nodeQueue.remove(currentId);
|
||||||
|
if (adjacencyGraph.get(currentId).size() != 1) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
String nextId = adjacencyGraph.get(currentId).get(0);
|
||||||
|
if (reverseAdjacencyGraph.get(nextId).size() > 1) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
currentId = nextId;
|
||||||
|
}
|
||||||
|
chains.add(subChain);
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info("{}", chains);
|
||||||
|
|
||||||
|
return StrUtil.join(",", topologicalSortedList);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class FlowData {
|
||||||
|
private ImmutableList<Node> nodes;
|
||||||
|
private ImmutableList<Edge> edges;
|
||||||
|
private ImmutableMap<String, Object> data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class Node {
|
||||||
|
private String id;
|
||||||
|
private String type;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class Edge {
|
||||||
|
private String id;
|
||||||
|
private String source;
|
||||||
|
private String target;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package com.lanyuanxiaoyao.service.ai.web.flow;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author lanyuanxiaoyao
|
||||||
|
* @version 20250625
|
||||||
|
*/
|
||||||
|
public class LlmNode extends BaseNode {
|
||||||
|
@Override
|
||||||
|
public void process() throws Exception {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package com.lanyuanxiaoyao.service.ai.web.flow;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author lanyuanxiaoyao
|
||||||
|
* @version 20250625
|
||||||
|
*/
|
||||||
|
public class StartNode extends BaseNode {
|
||||||
|
@Override
|
||||||
|
public void process() throws Exception {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,7 +4,7 @@ import {
|
|||||||
BackgroundVariant,
|
BackgroundVariant,
|
||||||
type Connection,
|
type Connection,
|
||||||
Controls,
|
Controls,
|
||||||
type Edge,
|
getIncomers,
|
||||||
getOutgoers,
|
getOutgoers,
|
||||||
MiniMap,
|
MiniMap,
|
||||||
type Node,
|
type Node,
|
||||||
@@ -18,15 +18,16 @@ import {arrToMap, find, findIdx, isEqual, isNil, randomId} from 'licia'
|
|||||||
import {type JSX, useState} from 'react'
|
import {type JSX, useState} from 'react'
|
||||||
import styled from 'styled-components'
|
import styled from 'styled-components'
|
||||||
import '@xyflow/react/dist/style.css'
|
import '@xyflow/react/dist/style.css'
|
||||||
|
import Queue from 'yocto-queue'
|
||||||
import {amisRender, commonInfo, horizontalFormOptions} from '../../../util/amis.tsx'
|
import {amisRender, commonInfo, horizontalFormOptions} from '../../../util/amis.tsx'
|
||||||
import CodeNode from './node/CodeNode.tsx'
|
import CodeNode from './node/CodeNode.tsx'
|
||||||
import EndNode from './node/EndNode.tsx'
|
import EndNode from './node/EndNode.tsx'
|
||||||
import KnowledgeNode from './node/KnowledgeNode.tsx'
|
import KnowledgeNode from './node/KnowledgeNode.tsx'
|
||||||
import LlmNode from './node/LlmNode.tsx'
|
import LlmNode from './node/LlmNode.tsx'
|
||||||
|
import ParallelNode from './node/ParallelNode.tsx'
|
||||||
import StartNode from './node/StartNode.tsx'
|
import StartNode from './node/StartNode.tsx'
|
||||||
import {useDataStore} from './store/DataStore.ts'
|
import {useDataStore} from './store/DataStore.ts'
|
||||||
import {useFlowStore} from './store/FlowStore.ts'
|
import {useFlowStore} from './store/FlowStore.ts'
|
||||||
import Queue from 'yocto-queue'
|
|
||||||
|
|
||||||
const FlowableDiv = styled.div`
|
const FlowableDiv = styled.div`
|
||||||
height: 100%;
|
height: 100%;
|
||||||
@@ -73,34 +74,39 @@ function FlowEditor() {
|
|||||||
component: (props: NodeProps) => JSX.Element
|
component: (props: NodeProps) => JSX.Element
|
||||||
}[]>([
|
}[]>([
|
||||||
{
|
{
|
||||||
key: 'start-amis-node',
|
key: 'start-node',
|
||||||
name: '开始',
|
name: '开始',
|
||||||
component: StartNode,
|
component: StartNode,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: 'end-amis-node',
|
key: 'end-node',
|
||||||
name: '结束',
|
name: '结束',
|
||||||
component: EndNode,
|
component: EndNode,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: 'llm-amis-node',
|
key: 'parallel-node',
|
||||||
|
name: '并行',
|
||||||
|
component: ParallelNode,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'llm-node',
|
||||||
name: '大模型',
|
name: '大模型',
|
||||||
component: LlmNode,
|
component: LlmNode,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: 'knowledge-amis-node',
|
key: 'knowledge-node',
|
||||||
name: '知识库',
|
name: '知识库',
|
||||||
component: KnowledgeNode,
|
component: KnowledgeNode,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: 'code-amis-node',
|
key: 'code-node',
|
||||||
name: '代码执行',
|
name: '代码执行',
|
||||||
component: CodeNode,
|
component: CodeNode,
|
||||||
},
|
},
|
||||||
])
|
])
|
||||||
const [open, setOpen] = useState(false)
|
const [open, setOpen] = useState(false)
|
||||||
|
|
||||||
const {setData, getDataById, setDataById} = useDataStore()
|
const {data, setData, getDataById, setDataById} = useDataStore()
|
||||||
const {
|
const {
|
||||||
nodes,
|
nodes,
|
||||||
getNodeById,
|
getNodeById,
|
||||||
@@ -185,10 +191,10 @@ function FlowEditor() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const checkNode = (type: string) => {
|
const checkNode = (type: string) => {
|
||||||
if (isEqual(type, 'start-amis-node') && findIdx(nodes, (node: Node) => isEqual(type, node.type)) > -1) {
|
if (isEqual(type, 'start-node') && findIdx(nodes, (node: Node) => isEqual(type, node.type)) > -1) {
|
||||||
throw new Error('只能存在1个开始节点')
|
throw new Error('只能存在1个开始节点')
|
||||||
}
|
}
|
||||||
if (isEqual(type, 'end-amis-node') && findIdx(nodes, (node: Node) => isEqual(type, node.type)) > -1) {
|
if (isEqual(type, 'end-node') && findIdx(nodes, (node: Node) => isEqual(type, node.type)) > -1) {
|
||||||
throw new Error('只能存在1个结束节点')
|
throw new Error('只能存在1个结束节点')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -203,12 +209,12 @@ function FlowEditor() {
|
|||||||
throw new Error('连线目标节点未找到')
|
throw new Error('连线目标节点未找到')
|
||||||
}
|
}
|
||||||
// 禁止短路整个流程
|
// 禁止短路整个流程
|
||||||
if (isEqual('start-amis-node', sourceNode.type) && isEqual('end-amis-node', targetNode.type)) {
|
if (isEqual('start-node', sourceNode.type) && isEqual('end-node', targetNode.type)) {
|
||||||
throw new Error('开始节点不能直连结束节点')
|
throw new Error('开始节点不能直连结束节点')
|
||||||
}
|
}
|
||||||
|
|
||||||
// 禁止流程出现环,必须是有向无环图
|
// 禁止流程出现环,必须是有向无环图
|
||||||
const hasCycle = (node: Node, visited = new Set()) => {
|
const hasCycle = (node: Node, visited = new Set<string>()) => {
|
||||||
if (visited.has(node.id)) return false
|
if (visited.has(node.id)) return false
|
||||||
visited.add(node.id)
|
visited.add(node.id)
|
||||||
for (const outgoer of getOutgoers(node, nodes, edges)) {
|
for (const outgoer of getOutgoers(node, nodes, edges)) {
|
||||||
@@ -221,11 +227,35 @@ function FlowEditor() {
|
|||||||
} else if (hasCycle(targetNode)) {
|
} else if (hasCycle(targetNode)) {
|
||||||
throw new Error('禁止流程循环')
|
throw new Error('禁止流程循环')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const hasRedundant = (source: Node, target: Node) => {
|
||||||
|
const visited = new Set<string>()
|
||||||
|
const queue = new Queue<Node>()
|
||||||
|
queue.enqueue(source)
|
||||||
|
visited.add(source.id)
|
||||||
|
while (queue.size > 0) {
|
||||||
|
const current = queue.dequeue()!
|
||||||
|
console.log(current.id)
|
||||||
|
for (const incomer of getIncomers(current, nodes, edges)) {
|
||||||
|
if (isEqual(incomer.id, target.id)) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if (!visited.has(incomer.id)) {
|
||||||
|
visited.add(incomer.id)
|
||||||
|
queue.enqueue(incomer)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if (hasRedundant(sourceNode, targetNode)) {
|
||||||
|
throw new Error('出现冗余边')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
useMount(() => {
|
useMount(() => {
|
||||||
// language=JSON
|
// language=JSON
|
||||||
let initialData = JSON.parse('{\n "nodes": [\n {\n "id": "BMFP3Eov94",\n "type": "start-amis-node",\n "position": {\n "x": 8,\n "y": 272\n },\n "data": {},\n "measured": {\n "width": 256,\n "height": 75\n },\n "selected": false,\n "dragging": false\n },\n {\n "id": "PYK8LjduQ1",\n "type": "end-amis-node",\n "position": {\n "x": 1439.5556937134281,\n "y": 282.2797340760818\n },\n "data": {},\n "measured": {\n "width": 256,\n "height": 75\n },\n "selected": false,\n "dragging": false\n },\n {\n "id": "nCm-ij5I6o",\n "type": "llm-amis-node",\n "position": {\n "x": 902.6781018665707,\n "y": 115.31234529524048\n },\n "data": {},\n "measured": {\n "width": 256,\n "height": 75\n },\n "selected": false,\n "dragging": false\n },\n {\n "id": "9RIg65O0YQ",\n "type": "knowledge-amis-node",\n "position": {\n "x": 338,\n "y": 287\n },\n "data": {},\n "measured": {\n "width": 256,\n "height": 75\n },\n "selected": false,\n "dragging": false\n },\n {\n "id": "2vTyjP0Gu9",\n "type": "code-amis-node",\n "position": {\n "x": 1086.6322978498904,\n "y": 371.3061114283591\n },\n "data": {},\n "measured": {\n "width": 256,\n "height": 75\n },\n "selected": true,\n "dragging": false\n },\n {\n "id": "s9VfZpvb4P",\n "type": "llm-amis-node",\n "position": {\n "x": 700.0944461714178,\n "y": 369.84258971430364\n },\n "data": {},\n "measured": {\n "width": 256,\n "height": 75\n },\n "selected": false,\n "dragging": false\n }\n ],\n "edges": [\n {\n "source": "BMFP3Eov94",\n "target": "9RIg65O0YQ",\n "id": "xy-edge__BMFP3Eov94-9RIg65O0YQ"\n },\n {\n "source": "9RIg65O0YQ",\n "target": "nCm-ij5I6o",\n "id": "xy-edge__9RIg65O0YQ-nCm-ij5I6o"\n },\n {\n "source": "nCm-ij5I6o",\n "target": "PYK8LjduQ1",\n "id": "xy-edge__nCm-ij5I6o-PYK8LjduQ1"\n },\n {\n "source": "s9VfZpvb4P",\n "target": "2vTyjP0Gu9",\n "id": "xy-edge__s9VfZpvb4P-2vTyjP0Gu9"\n },\n {\n "source": "9RIg65O0YQ",\n "target": "s9VfZpvb4P",\n "id": "xy-edge__9RIg65O0YQ-s9VfZpvb4P"\n },\n {\n "source": "2vTyjP0Gu9",\n "target": "PYK8LjduQ1",\n "id": "xy-edge__2vTyjP0Gu9-PYK8LjduQ1"\n }\n ],\n "data": {\n "BMFP3Eov94": {\n "inputs": {\n "name": {\n "type": "text"\n },\n "description": {\n "type": "text",\n "description": "文件描述"\n }\n }\n },\n "nCm-ij5I6o": {\n "model": "qwen3",\n "outputs": {\n "text": {\n "type": "string"\n }\n },\n "systemPrompt": "你是个沙雕"\n },\n "9RIg65O0YQ": {\n "count": 3,\n "score": 0.75,\n "knowledgeId": 3585368238960640,\n "query": "hello world"\n },\n "2vTyjP0Gu9": {\n "type": "python",\n "content": "code=\\"hello\\"\\nprint(code)"\n },\n "s9VfZpvb4P": {\n "model": "qwen3",\n "outputs": {\n "text": {\n "type": "string"\n }\n },\n "systemPrompt": "你是个聪明人"\n }\n }\n}')
|
let initialData = JSON.parse('{\n "nodes": [\n {\n "id": "lurod0PM-J",\n "type": "parallel-node",\n "position": {\n "x": -156,\n "y": 77\n },\n "data": {},\n "measured": {\n "width": 256,\n "height": 75\n },\n "selected": false,\n "dragging": false\n },\n {\n "id": "ldoKAzHnKF",\n "type": "llm-node",\n "position": {\n "x": 207,\n "y": -38\n },\n "data": {},\n "measured": {\n "width": 256,\n "height": 75\n },\n "selected": false,\n "dragging": false\n },\n {\n "id": "1eJtMoJWs6",\n "type": "llm-node",\n "position": {\n "x": 207,\n "y": 172.5\n },\n "data": {},\n "measured": {\n "width": 256,\n "height": 75\n },\n "selected": false,\n "dragging": false\n }\n ],\n "edges": [\n {\n "source": "lurod0PM-J",\n "target": "1eJtMoJWs6",\n "id": "xy-edge__lurod0PM-J-1eJtMoJWs6"\n },\n {\n "source": "lurod0PM-J",\n "target": "ldoKAzHnKF",\n "id": "xy-edge__lurod0PM-J-ldoKAzHnKF"\n }\n ],\n "data": {}\n}')
|
||||||
let initialNodes = initialData['nodes'] ?? []
|
let initialNodes = initialData['nodes'] ?? []
|
||||||
let initialEdges = initialData['edges'] ?? []
|
let initialEdges = initialData['edges'] ?? []
|
||||||
|
|
||||||
@@ -278,68 +308,8 @@ function FlowEditor() {
|
|||||||
</Button>
|
</Button>
|
||||||
</Dropdown>
|
</Dropdown>
|
||||||
<Button type="primary" onClick={() => {
|
<Button type="primary" onClick={() => {
|
||||||
// let saveData = {nodes, edges, data}
|
let saveData = {nodes, edges, data}
|
||||||
// console.log(JSON.stringify(saveData, null, 2))
|
console.log(JSON.stringify(saveData, null, 2))
|
||||||
|
|
||||||
const topologicalSort: (nodes: Node[], edges: Edge[]) => void = (nodes, edges) => {
|
|
||||||
const adjacency: Record<string, string[]> = {}
|
|
||||||
const reverseAdjacency: Record<string, string[]> = {}
|
|
||||||
const inDegree: Record<string, number> = {}
|
|
||||||
const nodeMap: Record<string, Node> = {}
|
|
||||||
|
|
||||||
// 初始化所有节点的邻接表和入度
|
|
||||||
for (const node of nodes) {
|
|
||||||
const id = node.id
|
|
||||||
adjacency[id] = []
|
|
||||||
reverseAdjacency[id] = []
|
|
||||||
inDegree[id] = 0
|
|
||||||
nodeMap[id] = node
|
|
||||||
}
|
|
||||||
|
|
||||||
// 填充邻接表并更新入度
|
|
||||||
for (const edge of edges) {
|
|
||||||
const {source, target} = edge
|
|
||||||
adjacency[source].push(target)
|
|
||||||
reverseAdjacency[target].push(source)
|
|
||||||
inDegree[target] = (inDegree[target] || 0) + 1
|
|
||||||
}
|
|
||||||
|
|
||||||
// 使用队列进行拓扑排序
|
|
||||||
const queue = new Queue<string>()
|
|
||||||
const topologicalList: string[] = []
|
|
||||||
|
|
||||||
// 寻找所有入度为0的节点(起点)
|
|
||||||
for (const nodeId in inDegree) {
|
|
||||||
if (inDegree[nodeId] === 0) {
|
|
||||||
queue.enqueue(nodeId)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 开始处理节点
|
|
||||||
while (queue.size > 0) {
|
|
||||||
const currentNode = queue.dequeue()!
|
|
||||||
topologicalList.push(currentNode)
|
|
||||||
|
|
||||||
// 处理当前节点的所有邻居
|
|
||||||
for (const neighbor of adjacency[currentNode]) {
|
|
||||||
// 减少邻居的入度
|
|
||||||
inDegree[neighbor]--
|
|
||||||
|
|
||||||
// 如果邻居的入度变为0,加入队列
|
|
||||||
if (inDegree[neighbor] === 0) {
|
|
||||||
queue.enqueue(neighbor)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查环 - 如果结果数量小于节点数,说明存在环
|
|
||||||
if (topologicalList.length !== nodes.length) {
|
|
||||||
throw new Error('图中存在环,无法完成拓扑排序')
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log(topologicalList)
|
|
||||||
}
|
|
||||||
topologicalSort(nodes, edges)
|
|
||||||
}}>
|
}}>
|
||||||
<SaveFilled/>
|
<SaveFilled/>
|
||||||
保存
|
保存
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import {DeleteFilled, EditFilled} from '@ant-design/icons'
|
import {DeleteFilled, EditFilled} from '@ant-design/icons'
|
||||||
import {Handle, type NodeProps, Position} from '@xyflow/react'
|
import {Handle, type HandleProps, type NodeProps, Position, useNodeConnections} from '@xyflow/react'
|
||||||
import type {Schema} from 'amis'
|
import type {Schema} from 'amis'
|
||||||
import {Card, Dropdown} from 'antd'
|
import {Card, Dropdown} from 'antd'
|
||||||
import {isEmpty, isEqual} from 'licia'
|
import {isEmpty, isEqual, isNil} from 'licia'
|
||||||
import type {JSX} from 'react'
|
import {type JSX} from 'react'
|
||||||
import {horizontalFormOptions} from '../../../../util/amis.tsx'
|
import {horizontalFormOptions} from '../../../../util/amis.tsx'
|
||||||
|
|
||||||
export type AmisNodeType = 'normal' | 'start' | 'end'
|
export type AmisNodeType = 'normal' | 'start' | 'end'
|
||||||
@@ -55,15 +55,38 @@ export function outputsFormColumns(editable: boolean = false, required: boolean
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
const AmisNode = (
|
export const LimitHandler = (props: HandleProps & { limit: number }) => {
|
||||||
props: NodeProps,
|
const connections = useNodeConnections({
|
||||||
type: AmisNodeType,
|
handleType: props.type,
|
||||||
defaultNodeName: String,
|
})
|
||||||
defaultNodeDescription?: String,
|
return (
|
||||||
extraNodeDescription?: (nodeData: any) => JSX.Element,
|
<Handle
|
||||||
columnSchema?: Schema[],
|
{...props}
|
||||||
) => {
|
isConnectable={connections.length < props.limit}
|
||||||
const {id, data} = props
|
/>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
type AmisNodeProps = {
|
||||||
|
nodeProps: NodeProps
|
||||||
|
type: AmisNodeType
|
||||||
|
defaultNodeName: String
|
||||||
|
defaultNodeDescription?: String
|
||||||
|
extraNodeDescription?: (nodeData: any) => JSX.Element
|
||||||
|
handlers?: (nodeData: any) => JSX.Element
|
||||||
|
columnSchema?: Schema[]
|
||||||
|
}
|
||||||
|
|
||||||
|
const AmisNode: (props: AmisNodeProps) => JSX.Element = ({
|
||||||
|
nodeProps,
|
||||||
|
type,
|
||||||
|
defaultNodeName,
|
||||||
|
defaultNodeDescription,
|
||||||
|
extraNodeDescription,
|
||||||
|
handlers,
|
||||||
|
columnSchema,
|
||||||
|
}) => {
|
||||||
|
const {id, data} = nodeProps
|
||||||
const {getDataById, removeNode, editNode} = data
|
const {getDataById, removeNode, editNode} = data
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const nodeData = getDataById(id)
|
const nodeData = getDataById(id)
|
||||||
@@ -133,10 +156,14 @@ const AmisNode = (
|
|||||||
</div>
|
</div>
|
||||||
</Card>
|
</Card>
|
||||||
</Dropdown>
|
</Dropdown>
|
||||||
|
{isNil(handlers)
|
||||||
|
? <>
|
||||||
{isEqual(type, 'start') || isEqual(type, 'normal')
|
{isEqual(type, 'start') || isEqual(type, 'normal')
|
||||||
? <Handle type="source" position={Position.Right}/> : undefined}
|
? <LimitHandler type="source" position={Position.Right} limit={1}/> : undefined}
|
||||||
{isEqual(type, 'end') || isEqual(type, 'normal')
|
{isEqual(type, 'end') || isEqual(type, 'normal')
|
||||||
? <Handle type="target" position={Position.Left}/> : undefined}
|
? <Handle type="target" position={Position.Left}/> : undefined}
|
||||||
|
</>
|
||||||
|
: handlers?.(nodeData)}
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,13 +2,12 @@ import type {NodeProps} from '@xyflow/react'
|
|||||||
import {horizontalFormOptions} from '../../../../util/amis.tsx'
|
import {horizontalFormOptions} from '../../../../util/amis.tsx'
|
||||||
import AmisNode, {outputsFormColumns} from './AmisNode.tsx'
|
import AmisNode, {outputsFormColumns} from './AmisNode.tsx'
|
||||||
|
|
||||||
const CodeNode = (props: NodeProps) => AmisNode(
|
const CodeNode = (props: NodeProps) => AmisNode({
|
||||||
props,
|
nodeProps: props,
|
||||||
'normal',
|
type: 'normal',
|
||||||
'代码执行',
|
defaultNodeName: '代码执行',
|
||||||
'执行自定义的处理代码',
|
defaultNodeDescription: '执行自定义的处理代码',
|
||||||
undefined,
|
columnSchema: [
|
||||||
[
|
|
||||||
{
|
{
|
||||||
type: 'input-kvs',
|
type: 'input-kvs',
|
||||||
name: 'inputs',
|
name: 'inputs',
|
||||||
@@ -68,6 +67,6 @@ const CodeNode = (props: NodeProps) => AmisNode(
|
|||||||
},
|
},
|
||||||
...outputsFormColumns(true, true, {result: {type: 'string'}}),
|
...outputsFormColumns(true, true, {result: {type: 'string'}}),
|
||||||
],
|
],
|
||||||
)
|
})
|
||||||
|
|
||||||
export default CodeNode
|
export default CodeNode
|
||||||
@@ -1,13 +1,12 @@
|
|||||||
import type {NodeProps} from '@xyflow/react'
|
import type {NodeProps} from '@xyflow/react'
|
||||||
import AmisNode, {outputsFormColumns} from './AmisNode.tsx'
|
import AmisNode, {outputsFormColumns} from './AmisNode.tsx'
|
||||||
|
|
||||||
const EndNode = (props: NodeProps) => AmisNode(
|
const EndNode = (props: NodeProps) => AmisNode({
|
||||||
props,
|
nodeProps: props,
|
||||||
'end',
|
type: 'end',
|
||||||
'结束节点',
|
defaultNodeName: '结束节点',
|
||||||
'定义输出变量',
|
defaultNodeDescription: '定义输出变量',
|
||||||
undefined,
|
columnSchema: outputsFormColumns(true),
|
||||||
outputsFormColumns(true),
|
})
|
||||||
)
|
|
||||||
|
|
||||||
export default EndNode
|
export default EndNode
|
||||||
@@ -2,13 +2,12 @@ import type {NodeProps} from '@xyflow/react'
|
|||||||
import {commonInfo} from '../../../../util/amis.tsx'
|
import {commonInfo} from '../../../../util/amis.tsx'
|
||||||
import AmisNode from './AmisNode.tsx'
|
import AmisNode from './AmisNode.tsx'
|
||||||
|
|
||||||
const KnowledgeNode = (props: NodeProps) => AmisNode(
|
const KnowledgeNode = (props: NodeProps) => AmisNode({
|
||||||
props,
|
nodeProps: props,
|
||||||
'normal',
|
type: 'normal',
|
||||||
'知识库',
|
defaultNodeName: '知识库',
|
||||||
'查询知识库获取外部知识',
|
defaultNodeDescription: '查询知识库获取外部知识',
|
||||||
undefined,
|
columnSchema: [
|
||||||
[
|
|
||||||
{
|
{
|
||||||
type: 'select',
|
type: 'select',
|
||||||
name: 'knowledgeId',
|
name: 'knowledgeId',
|
||||||
@@ -24,7 +23,7 @@ const KnowledgeNode = (props: NodeProps) => AmisNode(
|
|||||||
...payload,
|
...payload,
|
||||||
data: {
|
data: {
|
||||||
items: payload.data.items.map((item: any) => ({value: item['id'], label: item['name']})),
|
items: payload.data.items.map((item: any) => ({value: item['id'], label: item['name']})),
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -53,6 +52,6 @@ const KnowledgeNode = (props: NodeProps) => AmisNode(
|
|||||||
step: 0.05,
|
step: 0.05,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
)
|
})
|
||||||
|
|
||||||
export default KnowledgeNode
|
export default KnowledgeNode
|
||||||
@@ -1,13 +1,12 @@
|
|||||||
import type {NodeProps} from '@xyflow/react'
|
import type {NodeProps} from '@xyflow/react'
|
||||||
import AmisNode, {outputsFormColumns} from './AmisNode.tsx'
|
import AmisNode, {outputsFormColumns} from './AmisNode.tsx'
|
||||||
|
|
||||||
const LlmNode = (props: NodeProps) => AmisNode(
|
const LlmNode = (props: NodeProps) => AmisNode({
|
||||||
props,
|
nodeProps: props,
|
||||||
'normal',
|
type: 'normal',
|
||||||
'大模型',
|
defaultNodeName: '大模型',
|
||||||
'使用大模型对话',
|
defaultNodeDescription: '使用大模型对话',
|
||||||
undefined,
|
columnSchema: [
|
||||||
[
|
|
||||||
{
|
{
|
||||||
type: 'select',
|
type: 'select',
|
||||||
name: 'model',
|
name: 'model',
|
||||||
@@ -36,6 +35,6 @@ const LlmNode = (props: NodeProps) => AmisNode(
|
|||||||
},
|
},
|
||||||
...outputsFormColumns(false, true, {text: {type: 'string'}}),
|
...outputsFormColumns(false, true, {text: {type: 'string'}}),
|
||||||
],
|
],
|
||||||
)
|
})
|
||||||
|
|
||||||
export default LlmNode
|
export default LlmNode
|
||||||
19
service-web/client/src/pages/ai/flow/node/ParallelNode.tsx
Normal file
19
service-web/client/src/pages/ai/flow/node/ParallelNode.tsx
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
import {Handle, type NodeProps, Position} from '@xyflow/react'
|
||||||
|
import AmisNode, {LimitHandler} from './AmisNode.tsx'
|
||||||
|
|
||||||
|
const ParallelNode = (props: NodeProps) => AmisNode({
|
||||||
|
nodeProps: props,
|
||||||
|
type: 'normal',
|
||||||
|
defaultNodeName: '并行节点',
|
||||||
|
defaultNodeDescription: '允许开启并行流程',
|
||||||
|
handlers: () => {
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<Handle type="source" position={Position.Right}/>
|
||||||
|
<LimitHandler type="target" position={Position.Left} limit={1}/>
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
export default ParallelNode
|
||||||
@@ -2,13 +2,12 @@ import type {NodeProps} from '@xyflow/react'
|
|||||||
import {horizontalFormOptions} from '../../../../util/amis.tsx'
|
import {horizontalFormOptions} from '../../../../util/amis.tsx'
|
||||||
import AmisNode from './AmisNode.tsx'
|
import AmisNode from './AmisNode.tsx'
|
||||||
|
|
||||||
const StartNode = (props: NodeProps) => AmisNode(
|
const StartNode = (props: NodeProps) => AmisNode({
|
||||||
props,
|
nodeProps: props,
|
||||||
'start',
|
type: 'start',
|
||||||
'开始节点',
|
defaultNodeName: '开始节点',
|
||||||
'定义输入变量',
|
defaultNodeDescription: '定义输入变量',
|
||||||
undefined,
|
columnSchema: [
|
||||||
[
|
|
||||||
{
|
{
|
||||||
type: 'input-kvs',
|
type: 'input-kvs',
|
||||||
name: 'inputs',
|
name: 'inputs',
|
||||||
@@ -51,6 +50,6 @@ const StartNode = (props: NodeProps) => AmisNode(
|
|||||||
],
|
],
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
)
|
})
|
||||||
|
|
||||||
export default StartNode
|
export default StartNode
|
||||||
@@ -31,7 +31,7 @@ const queueCrud = (name: string) => {
|
|||||||
{
|
{
|
||||||
name: 'data.flinkJobId',
|
name: 'data.flinkJobId',
|
||||||
label: '任务 ID',
|
label: '任务 ID',
|
||||||
width: 190,
|
width: 200,
|
||||||
...copyField('data.flinkJobId'),
|
...copyField('data.flinkJobId'),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,84 +0,0 @@
|
|||||||
type Node = {
|
|
||||||
id: string
|
|
||||||
type: 'start' | 'end' | 'normal'
|
|
||||||
}
|
|
||||||
|
|
||||||
type Edge = {
|
|
||||||
source: string
|
|
||||||
target: string
|
|
||||||
}
|
|
||||||
|
|
||||||
// language=JSON
|
|
||||||
let data = JSON.parse('{\n "nodes": [\n {\n "id": "A",\n "type": "start",\n "position": {\n "x": 8,\n "y": 272\n },\n "data": {},\n "measured": {\n "width": 256,\n "height": 75\n },\n "selected": false,\n "dragging": false\n },\n {\n "id": "F",\n "type": "end",\n "position": {\n "x": 1439.5556937134281,\n "y": 282.2797340760818\n },\n "data": {},\n "measured": {\n "width": 256,\n "height": 75\n },\n "selected": false,\n "dragging": false\n },\n {\n "id": "C",\n "type": "normal",\n "position": {\n "x": 902.6781018665707,\n "y": 115.31234529524048\n },\n "data": {},\n "measured": {\n "width": 256,\n "height": 75\n },\n "selected": false,\n "dragging": false\n },\n {\n "id": "B",\n "type": "normal",\n "position": {\n "x": 338,\n "y": 287\n },\n "data": {},\n "measured": {\n "width": 256,\n "height": 75\n },\n "selected": false,\n "dragging": false\n },\n {\n "id": "E",\n "type": "normal",\n "position": {\n "x": 1086.6322978498904,\n "y": 371.3061114283591\n },\n "data": {},\n "measured": {\n "width": 256,\n "height": 75\n },\n "selected": true,\n "dragging": false\n },\n {\n "id": "D",\n "type": "normal",\n "position": {\n "x": 700.0944461714178,\n "y": 369.84258971430364\n },\n "data": {},\n "measured": {\n "width": 256,\n "height": 75\n },\n "selected": false,\n "dragging": false\n }\n ],\n "edges": [\n {\n "source": "A",\n "target": "B",\n "id": "xy-edge__A-B"\n },\n {\n "source": "B",\n "target": "C",\n "id": "xy-edge__B-C"\n },\n {\n "source": "C",\n "target": "F",\n "id": "xy-edge__C-F"\n },\n {\n "source": "D",\n "target": "E",\n "id": "xy-edge__D-E"\n },\n {\n "source": "B",\n "target": "D",\n "id": "xy-edge__B-D"\n },\n {\n "source": "E",\n "target": "F",\n "id": "xy-edge__E-F"\n }\n ],\n "data": {\n "A": {\n "inputs": {\n "name": {\n "type": "text"\n },\n "description": {\n "type": "text",\n "description": "文件描述"\n }\n }\n },\n "C": {\n "model": "qwen3",\n "outputs": {\n "text": {\n "type": "string"\n }\n },\n "systemPrompt": "你是个沙雕"\n },\n "B": {\n "count": 3,\n "score": 0.75,\n "knowledgeId": 3585368238960640,\n "query": "hello world"\n },\n "E": {\n "type": "python",\n "content": "code=\'hello\'\\nprint(code)"\n },\n "D": {\n "model": "qwen3",\n "outputs": {\n "text": {\n "type": "string"\n }\n },\n "systemPrompt": "你是个聪明人"\n }\n }\n}')
|
|
||||||
|
|
||||||
// THEN(A, B, WHEN(C, THEN(D, E)), F)
|
|
||||||
|
|
||||||
function buildEL(nodes: Node[], edges: Edge[]): string {
|
|
||||||
// Build adjacency list and in-degree map
|
|
||||||
const adjList = new Map<string, string[]>()
|
|
||||||
const inDegree = new Map<string, number>()
|
|
||||||
for (const node of nodes) {
|
|
||||||
adjList.set(node.id, [])
|
|
||||||
inDegree.set(node.id, 0)
|
|
||||||
}
|
|
||||||
for (const edge of edges) {
|
|
||||||
adjList.get(edge.source)!.push(edge.target)
|
|
||||||
inDegree.set(edge.target, inDegree.get(edge.target)! + 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Compute levels (longest path from start)
|
|
||||||
const levelMap = new Map<string, number>()
|
|
||||||
|
|
||||||
function computeLevel(nodeId: string): number {
|
|
||||||
if (levelMap.has(nodeId)) return levelMap.get(nodeId)!
|
|
||||||
const preds = edges.filter(e => e.target === nodeId).map(e => e.source)
|
|
||||||
const level = preds.length === 0 ? 0 : Math.max(...preds.map(p => computeLevel(p))) + 1
|
|
||||||
levelMap.set(nodeId, level)
|
|
||||||
return level
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const node of nodes) computeLevel(node.id)
|
|
||||||
|
|
||||||
// Group nodes by level
|
|
||||||
const maxLevel = Math.max(...Array.from(levelMap.values()))
|
|
||||||
const levels: string[][] = Array.from({length: maxLevel + 1}, () => [])
|
|
||||||
for (const node of nodes) levels[levelMap.get(node.id)!].push(node.id)
|
|
||||||
|
|
||||||
// Build EL expression
|
|
||||||
const expressions: string[] = []
|
|
||||||
for (let i = 0; i <= maxLevel; i++) {
|
|
||||||
const nodesAtLevel = levels[i]
|
|
||||||
if (nodesAtLevel.length === 0) continue
|
|
||||||
|
|
||||||
// Identify serial chains starting from this level
|
|
||||||
const serialChains: string[] = []
|
|
||||||
for (const nodeId of nodesAtLevel) {
|
|
||||||
let chain = [nodeId]
|
|
||||||
let current = nodeId
|
|
||||||
while (adjList.get(current)?.length === 1) {
|
|
||||||
const next = adjList.get(current)![0]
|
|
||||||
if (inDegree.get(next) === 1 && levelMap.get(next) === i + chain.length) {
|
|
||||||
chain.push(next)
|
|
||||||
current = next
|
|
||||||
} else break
|
|
||||||
}
|
|
||||||
if (chain.length > 1) {
|
|
||||||
serialChains.push(`THEN(${chain.join(', ')})`)
|
|
||||||
// Remove processed nodes from their levels
|
|
||||||
for (let j = 1; j < chain.length; j++) {
|
|
||||||
const level = levelMap.get(chain[j])!
|
|
||||||
levels[level] = levels[level].filter(n => n !== chain[j])
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
serialChains.push(nodeId)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Combine chains or nodes at this level
|
|
||||||
expressions.push(serialChains.length > 1 ? `WHEN(${serialChains.join(', ')})` : serialChains[0])
|
|
||||||
}
|
|
||||||
|
|
||||||
return `THEN(${expressions.join(', ')})`
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log(buildEL(data.nodes, data.edges))
|
|
||||||
Reference in New Issue
Block a user