145 lines
4.1 KiB
Java
145 lines
4.1 KiB
Java
package com.metis.domain.context;
|
|
|
|
import cn.hutool.core.lang.Assert;
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
import com.metis.runner.FlowRunningContext;
|
|
import lombok.Builder;
|
|
import lombok.Getter;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.expression.ExpressionParser;
|
|
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
|
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.Set;
|
|
|
|
@Slf4j
|
|
@Getter
|
|
@Builder
|
|
public class RunningContext {
|
|
|
|
private final static String SYS_PREFIX = "sys.";
|
|
|
|
/**
|
|
* 系统数据
|
|
*/
|
|
private SysContext sys;
|
|
|
|
/**
|
|
* 自定义数据
|
|
*/
|
|
private JSONObject custom;
|
|
|
|
/**
|
|
* 节点运行上下文, 需要数据进行传递
|
|
*/
|
|
private Map<String, JSONObject> nodeRunningContext;
|
|
|
|
/**
|
|
* 下一个运行节点id集合, 可能是多个, 执行器每一次清空该节点
|
|
*/
|
|
private Set<String> nextRunNodeId;
|
|
|
|
|
|
/**
|
|
* 添加节点运行环境
|
|
*
|
|
* @param nodeId 节点id
|
|
* @param nodeRunningContext 节点运行背景信息
|
|
*/
|
|
public void addNodeRunningContext(String nodeId, JSONObject nodeRunningContext) {
|
|
this.nodeRunningContext.put(nodeId, nodeRunningContext);
|
|
}
|
|
|
|
/**
|
|
* 获取运行上下文
|
|
*
|
|
* @param nodeId 节点id
|
|
* @return {@link JSONObject }
|
|
*/
|
|
public JSONObject getRunningContext(String nodeId) {
|
|
return this.nodeRunningContext.get(nodeId);
|
|
}
|
|
|
|
|
|
/**
|
|
* 获得值
|
|
*
|
|
* @param key 关键
|
|
* @return {@link Object }
|
|
*/
|
|
public Object getValue(String key) {
|
|
Assert.isTrue(StrUtil.isNotBlank(key), "key is blank");
|
|
if (key.startsWith(SYS_PREFIX)) {
|
|
ExpressionParser parser = new SpelExpressionParser();
|
|
StandardEvaluationContext context = new StandardEvaluationContext(this);
|
|
return parser.parseExpression(key).getValue(context);
|
|
}
|
|
|
|
|
|
// try {
|
|
// // 解析 key 中的数字部分并转换为 Long 类型
|
|
// String[] parts = key.split("\\.");
|
|
// if (parts.length == 2 && StrUtil.isNumeric(parts[0])) {
|
|
// String nodeId = Long.valueOf(parts[0]);
|
|
// JSONObject runningContext = getRunningContext(nodeId);
|
|
// if (runningContext != null) {
|
|
// return runningContext.get(parts[1]);
|
|
// }
|
|
// }
|
|
// } catch (Exception e) {
|
|
// log.error("数字类型获取动态参数失败: {}", key);
|
|
// }
|
|
ExpressionParser parser = new SpelExpressionParser();
|
|
StandardEvaluationContext context = new StandardEvaluationContext();
|
|
Map<String, Object> variables = new HashMap<>(this.nodeRunningContext);
|
|
context.setVariables(variables);
|
|
return parser.parseExpression(convertDotToSquareBrackets(key)).getValue(context);
|
|
}
|
|
|
|
|
|
/**
|
|
* 获得价值 不满足条件, 则返回null, 需要业务自行判断
|
|
*
|
|
* @param nodeId 节点id
|
|
* @param key 关键
|
|
* @return {@link Object }
|
|
*/
|
|
public Object getValue(String nodeId, String key) {
|
|
if (ObjectUtil.isNull(nodeId)) {
|
|
return null;
|
|
}
|
|
if (StrUtil.isBlank(key)) {
|
|
return null;
|
|
}
|
|
JSONObject runningContext = getRunningContext(nodeId);
|
|
if (ObjectUtil.isNull(runningContext)) {
|
|
return null;
|
|
}
|
|
return runningContext.get(key);
|
|
}
|
|
|
|
|
|
/**
|
|
* 构建上下文
|
|
*
|
|
* @param context 上下文
|
|
* @return {@link RunningContext }
|
|
*/
|
|
public static RunningContext buildContext(SysContext sysContext, FlowRunningContext context) {
|
|
return RunningContext.builder()
|
|
.sys(sysContext)
|
|
.custom(context.getCustom())
|
|
.nodeRunningContext(new HashMap<>())
|
|
.build();
|
|
}
|
|
|
|
public String convertDotToSquareBrackets(String key) {
|
|
return key.replaceAll("(\\w+)\\.(\\w+)", "#$1['$2']");
|
|
}
|
|
|
|
}
|