flow - 引擎的两种使用模式
2026年1月13日 下午8:47:20
1、Solon 自动集成模式
在 solon 容器环境,(什么都不作时)会自动生成一个 FlowEngine 实例,且会自动加载 solon.flow 配置的图资源。
- 配置示例
solon.flow:
- "classpath:folw/*" #内部资源
- "file:folw/*" #外部文件
- 代码应用
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import org.noear.solon.core.bean.LifecycleBean;
import org.noear.solon.flow.FlowEngine;
@Component
public class DemoCom implements LifecycleBean{
@Inject
private FlowEngine flowEngine;
@Override
public void start() throws Throwable {
//执行配置加载后的 c1
flowEngine.eval("c1");
}
}
2、手动集成模式
就是自己构建引擎实例,自己加载图配置。
import org.noear.solon.annotation.Component;
import org.noear.solon.core.bean.LifecycleBean;
import org.noear.solon.flow.FlowEngine;
@Component
public class DemoCom implements LifecycleBean{
@Override
public void start() throws Throwable {
FlowEngine flowEngine = FlowEngine.newInstance();
//加载
flowEngine.load("classpath:flow/c1.json");
//flowEngine.load("classpath:flow/*.yml"); //支持 * 号表达式批量加载
//执行加载后的 c1 图
flowEngine.eval("c1");
}
}
3、流程引擎接口参考
public interface FlowEngine {
/**
* 新实例
*/
static FlowEngine newInstance() {
return new FlowEngineDefault(null, false);
}
/**
* 新实例
*/
static FlowEngine newInstance(FlowDriver driver) {
return new FlowEngineDefault(driver, false);
}
/**
* 新实例
*/
static FlowEngine newInstance(boolean simplified) {
return new FlowEngineDefault(null, simplified);
}
/**
* 然后(构建自己)
*/
default FlowEngine then(Consumer<FlowEngine> consumer) {
consumer.accept(this);
return this;
}
/**
* 获取驱动
*/
FlowDriver getDriver(Graph graph);
/**
* 添加拦截器
*
* @param index 顺序位
* @param interceptor 拦截器
*/
void addInterceptor(FlowInterceptor interceptor, int index);
/**
* 添加拦截器
*
* @param interceptor 拦截器
*/
default void addInterceptor(FlowInterceptor interceptor) {
addInterceptor(interceptor, 0);
}
/**
* 移除拦截器
*/
void removeInterceptor(FlowInterceptor interceptor);
/**
* 注册驱动器
*
* @param name 名字
* @param driver 驱动器
*/
void register(String name, FlowDriver driver);
/**
* 注册默认驱动器
*
* @param driver 默认驱动器
*/
default void register(FlowDriver driver) {
register(null, driver);
}
/**
* 注销驱动器
*/
void unregister(String name);
/**
* 解析配置文件
*
* @param graphUri 图资源地址
*/
default void load(String graphUri) {
if (graphUri.contains("*")) {
for (String u1 : ResourceUtil.scanResources(graphUri)) {
load(Graph.fromUri(u1));
}
} else {
load(Graph.fromUri(graphUri));
}
}
/**
* 加载图
*
* @param graph 图
*/
void load(Graph graph);
/**
* 卸载图
*
* @param graphId 图Id
*/
void unload(String graphId);
/**
* 获取所有图
*/
Collection<Graph> getGraphs();
/**
* 获取图
*/
Graph getGraph(String graphId);
/**
* 获取图
*/
default Graph getGraphOrThrow(String graphId) {
Graph graph = getGraph(graphId);
if (graph == null) {
throw new FlowException("Flow graph not found: " + graphId);
}
return graph;
}
/// ////////////////////
/**
* 运行
*
* @param graphId 图Id
*/
default void eval(String graphId) throws FlowException {
eval(graphId, -1, FlowContext.of());
}
/**
* 运行
*
* @param graphId 图Id
* @param context 上下文
*/
default void eval(String graphId, FlowContext context) throws FlowException {
eval(graphId, -1, context);
}
/**
* 运行
*
* @param graphId 图Id
* @param steps 步数
* @param context 上下文
*/
default void eval(String graphId, int steps, FlowContext context) throws FlowException {
eval(graphId, steps, context, null);
}
/**
* 运行
*
* @param graphId 图Id
* @param steps 步数
* @param context 上下文
* @param options 选项
*/
default void eval(String graphId, int steps, FlowContext context, FlowOptions options) throws FlowException {
Graph graph = getGraphOrThrow(graphId);
eval(graph, steps, context, options);
}
/**
* 运行
*
* @param graph 图
*/
default void eval(Graph graph) throws FlowException {
eval(graph, FlowContext.of());
}
/**
* 运行
*
* @param graph 图
* @param context 上下文
*/
default void eval(Graph graph, FlowContext context) throws FlowException {
eval(graph, -1, context);
}
/**
* 运行
*
* @param graph 图
* @param steps 步数
* @param context 上下文
*/
default void eval(Graph graph, int steps, FlowContext context) throws FlowException {
eval(graph, steps, context, null);
}
/**
* 运行
*
* @param graph 图
* @param steps 步数
* @param context 上下文
* @param options 选项
*/
default void eval(Graph graph, int steps, FlowContext context, FlowOptions options) throws FlowException {
FlowDriver driver = getDriver(graph);
eval(graph, new FlowExchanger(graph, this, driver, context, steps, new AtomicInteger(0)), options);
}
/**
* 运行
*
* @param graph 图
* @param exchanger 交换器
*/
@Internal
void eval(Graph graph, FlowExchanger exchanger, FlowOptions options) throws FlowException;
}