feat(ai-web): 尝试设计流程引擎
This commit is contained in:
@@ -1,19 +0,0 @@
|
||||
package com.lanyuanxiaoyao.service.ai.web.engine;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
/**
|
||||
* 流程图中的边
|
||||
*
|
||||
* @author lanyuanxiaoyao
|
||||
* @version 20250630
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
|
||||
public class FlowEdge {
|
||||
@EqualsAndHashCode.Include
|
||||
private String id;
|
||||
private String source;
|
||||
private String target;
|
||||
}
|
||||
@@ -1,5 +1,15 @@
|
||||
package com.lanyuanxiaoyao.service.ai.web.engine;
|
||||
|
||||
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowContext;
|
||||
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowEdge;
|
||||
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowGraph;
|
||||
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowNode;
|
||||
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowNodeRunner;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
import org.eclipse.collections.api.map.ImmutableMap;
|
||||
|
||||
/**
|
||||
* 流程执行器
|
||||
*
|
||||
@@ -7,4 +17,25 @@ package com.lanyuanxiaoyao.service.ai.web.engine;
|
||||
* @version 20250630
|
||||
*/
|
||||
public class FlowExecutor {
|
||||
private final ImmutableMap<String, Class<? extends FlowNodeRunner>> runnerMap;
|
||||
|
||||
public FlowExecutor(ImmutableMap<String, Class<? extends FlowNodeRunner>> runnerMap) {
|
||||
this.runnerMap = runnerMap;
|
||||
}
|
||||
|
||||
public void execute(FlowGraph graph) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
|
||||
var nodeInputMap = graph.edges().groupBy(FlowEdge::source);
|
||||
var nodeOutputMap = graph.edges().groupBy(FlowEdge::target);
|
||||
var startNodes = graph.nodes().select(node -> !nodeInputMap.containsKey(node.id()));
|
||||
Queue<FlowNode> queue = new LinkedList<>();
|
||||
for (FlowNode node : startNodes) {
|
||||
queue.offer(node);
|
||||
}
|
||||
while (!queue.isEmpty()) {
|
||||
var node = queue.poll();
|
||||
var clazz = runnerMap.get(node.type());
|
||||
var runner = clazz.getDeclaredConstructor(FlowContext.class).newInstance();
|
||||
runner.run();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
package com.lanyuanxiaoyao.service.ai.web.engine;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import org.eclipse.collections.api.set.ImmutableSet;
|
||||
|
||||
/**
|
||||
* 流程图中的节点
|
||||
*
|
||||
* @author lanyuanxiaoyao
|
||||
* @version 20250630
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
|
||||
public class FlowNode {
|
||||
@EqualsAndHashCode.Include
|
||||
private String id;
|
||||
private ImmutableSet<String> sourceHandlers;
|
||||
private ImmutableSet<String> targetHandlers;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.lanyuanxiaoyao.service.ai.web.engine.entity;
|
||||
|
||||
import lombok.Data;
|
||||
import org.eclipse.collections.api.factory.Maps;
|
||||
import org.eclipse.collections.api.map.MutableMap;
|
||||
|
||||
@Data
|
||||
public class FlowContext {
|
||||
private MutableMap<String, Object> data = Maps.mutable.<String, Object>empty().asSynchronized();
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.lanyuanxiaoyao.service.ai.web.engine.entity;
|
||||
|
||||
/**
|
||||
* 流程图中的边
|
||||
*
|
||||
* @author lanyuanxiaoyao
|
||||
* @version 20250630
|
||||
*/
|
||||
public record FlowEdge(
|
||||
String id,
|
||||
String source,
|
||||
String target,
|
||||
String sourcePoint,
|
||||
String targetPoint
|
||||
) {
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.lanyuanxiaoyao.service.ai.web.engine.entity;
|
||||
|
||||
import org.eclipse.collections.api.set.ImmutableSet;
|
||||
|
||||
/**
|
||||
* 流程图
|
||||
*
|
||||
* @author lanyuanxiaoyao
|
||||
* @version 20250630
|
||||
*/
|
||||
public record FlowGraph(
|
||||
String id,
|
||||
ImmutableSet<FlowNode> nodes,
|
||||
ImmutableSet<FlowEdge> edges
|
||||
) {
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.lanyuanxiaoyao.service.ai.web.engine.entity;
|
||||
|
||||
import org.eclipse.collections.api.set.ImmutableSet;
|
||||
|
||||
/**
|
||||
* 流程图中的节点
|
||||
*
|
||||
* @author lanyuanxiaoyao
|
||||
* @version 20250630
|
||||
*/
|
||||
public record FlowNode(
|
||||
String id,
|
||||
String type,
|
||||
ImmutableSet<String> inputPoints,
|
||||
ImmutableSet<String> outputPoints
|
||||
) {
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.lanyuanxiaoyao.service.ai.web.engine.entity;
|
||||
|
||||
public abstract class FlowNodeRunner implements Runnable {
|
||||
private final FlowContext context;
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package com.lanyuanxiaoyao.service.ai.web.engine.store;
|
||||
|
||||
/**
|
||||
* 数据存储
|
||||
*
|
||||
* @author lanyuanxiaoyao
|
||||
* @version 20250630
|
||||
*/
|
||||
public interface FlowStore {
|
||||
}
|
||||
Reference in New Issue
Block a user