Solon v3.7.3

flow - 流程引擎的两种使用模式

</> markdown

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();
    }

    /**
     * 新实例
     */
    static FlowEngine newInstance(FlowDriver driver) {
        return new FlowEngineDefault(driver);
    }

    /**
     * 获取驱动
     */
    FlowDriver getDriver(Graph graph);

    /**
     * 获取驱动
     */
    <T extends FlowDriver> T getDriverAs(Graph graph, Class<T> driverClass);

    /**
     * 为有状态场景
     */
    FlowStatefulService forStateful();

    /**
     * 有状态的服务
     *
     * @deprecated 3.6 {@link #forStateful()}
     */
    @Deprecated
    default FlowStatefulService statefulService(){
        return forStateful();
    }

    /**
     * 添加拦截器
     *
     * @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("", driver);
    }

    /**
     * 注销驱动器
     */
    void unregister(String name);


    /**
     * 解析配置文件
     *
     * @param graphUri 图资源地址
     */
    default void load(String graphUri) {
        if (graphUri.contains("*")) {
            for (String u1 : ResourceUtil.scanResources(graphUri)) {
                load(Graph.parseByUri(u1));
            }
        } else {
            load(Graph.parseByUri(graphUri));
        }
    }

    /**
     * 加载图
     *
     * @param graph 图
     */
    void load(Graph graph);

    /**
     * 卸载图
     *
     * @param graphId 图Id
     */
    void unload(String graphId);

    /**
     * 获取所有图
     */
    Collection<Graph> getGraphs();

    /**
     * 获取图
     */
    Graph getGraph(String graphId);

    /**
     * 运行
     *
     * @param graphId 图Id
     */
    default void eval(String graphId) throws FlowException {
        eval(graphId, FlowContext.of());
    }

    /**
     * 运行
     *
     * @param graphId 图Id
     * @param context 上下文
     */
    default void eval(String graphId, FlowContext context) throws FlowException {
        eval(graphId, null, -1, context);
    }

    /**
     * 运行
     *
     * @param graphId 图Id
     * @param startId 开始Id
     * @param context 上下文
     */
    default void eval(String graphId, String startId, FlowContext context) throws FlowException {
        eval(graphId, startId, -1, context);
    }

    /**
     * 运行
     *
     * @param graphId 图Id
     * @param startId 开始Id
     * @param depth   执行深度
     * @param context 上下文
     */
    default void eval(String graphId, String startId, int depth, FlowContext context) throws FlowException {
        eval(graphId, startId, depth, new FlowExchanger(context));
    }

    /**
     * 运行
     *
     * @param graphId   图Id
     * @param startId   开始Id
     * @param depth     执行深度
     * @param exchanger 交换器
     */
    void eval(String graphId, String startId, int depth, FlowExchanger exchanger) throws FlowException;

    /**
     * 运行
     *
     * @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.getStart(), -1, context);
    }

    /**
     * 运行
     *
     * @param startNode 开始节点
     */
    default void eval(Node startNode) throws FlowException {
        eval(startNode, -1, FlowContext.of());
    }

    /**
     * 运行
     *
     * @param startNode 开始节点
     * @param context   上下文
     */
    default void eval(Node startNode, FlowContext context) throws FlowException {
        eval(startNode, -1, context);
    }

    /**
     * 运行
     *
     * @param startNode 开始节点
     * @param depth     执行深度
     * @param context   上下文
     */
    default void eval(Node startNode, int depth, FlowContext context) throws FlowException {
        eval(startNode, depth, new FlowExchanger(context));
    }

    /**
     * 运行
     *
     * @param startNode 开始节点
     * @param depth     执行深度
     * @param exchanger 交换器
     */
    void eval(Node startNode, int depth, FlowExchanger exchanger) throws FlowException;
}