Solon v3.1.2

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

</> markdown

1、Solon 集成模式

在 solon 容器环境,会有一个 FlowEngine 单例,且会自动加载 solon.flow 配置的链资源。

  • 配置示例
solon.flow:
  - "classpath:folw/*" #内部资源
  - "file:folw/*" #外部文件
  • 代码应用
@Component
public class DemoCom implements LifecycleBean{
    @Inject 
    private FlowEngine flowEngine;
    
    @Override
    public void start() throws Throwable {
        //执行配置加载后 id=c1 的链
        flowEngine.eval("c1"); 
    }
}

2、非集成模式

就是自己构建引擎实例,自己加载链配置。

@Component
public class DemoCom implements LifecycleBean{
    @Override
    public void start() throws Throwable {
        FlowEngine flowEngine = FlowEngine.newInstance();
        
        //加载链
        flowEngine.load("classpath:flow/c1.chain.json");
        
        //执行加载后 id=c1 的链
        flowEngine.eval("c1"); 
    }
}

3、流引擎接口参考

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

    /**
     * 添加拦截器
     *
     * @param index       顺序位
     * @param interceptor 拦截器
     */
    void addInterceptor(ChainInterceptor interceptor, int index);


    /**
     * 注册链驱动器
     *
     * @param name   名字
     * @param driver 驱动器
     */
    void register(String name, FlowDriver driver);

    /**
     * 注册默认链驱动器
     *
     * @param driver 默认驱动器
     */
    default void register(FlowDriver driver) {
        register("", driver);
    }

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


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

    /**
     * 加载链
     *
     * @param chain 链
     */
    void load(Chain chain);

    /**
     * 卸载链
     *
     * @param chainId 链Id
     */
    void unload(String chainId);

    /**
     * 获取所有链
     */
    Collection<Chain> getChains();

    /**
     * 获取链
     */
    Chain getChain(String chainId);

    /**
     * 评估
     *
     * @param chainId 链Id
     */
    default void eval(String chainId) throws FlowException {
        eval(chainId, new FlowContext());
    }

    /**
     * 评估
     *
     * @param chainId 链Id
     * @param context 上下文
     */
    default void eval(String chainId, FlowContext context) throws FlowException {
        eval(chainId, null, -1, context);
    }

    /**
     * 评估
     *
     * @param chainId 链Id
     * @param startId 开始Id
     * @param context 上下文
     */
    default void eval(String chainId, String startId, FlowContext context) throws FlowException {
        eval(chainId, startId, -1, context);
    }

    /**
     * 评估
     *
     * @param chainId 链Id
     * @param startId 开始Id
     * @param depth   执行深度
     * @param context 上下文
     */
    void eval(String chainId, String startId, int depth, FlowContext context) throws FlowException;

    /**
     * 评估
     *
     * @param chain 链
     */
    default void eval(Chain chain) throws FlowException {
        eval(chain, new FlowContext());
    }

    /**
     * 评估
     *
     * @param chain   链
     * @param context 上下文
     */
    default void eval(Chain chain, FlowContext context) throws FlowException {
        eval(chain.getStart(), -1, context);
    }

    /**
     * 评估
     *
     * @param startNode 开始节点
     */
    default void eval(Node startNode) throws FlowException {
        eval(startNode, -1, new FlowContext());
    }

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

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