enhancement #I5OFMU 优化 Operator 代码

This commit is contained in:
tangkc
2022-08-28 14:58:41 +08:00
parent a7697c48d1
commit 7df9799439
17 changed files with 484 additions and 589 deletions

View File

@@ -1,55 +1,33 @@
package com.yomahub.liteflow.builder.el.operator;
import cn.hutool.core.util.ArrayUtil;
import com.ql.util.express.Operator;
import com.ql.util.express.exception.QLException;
import com.yomahub.liteflow.exception.ELParseException;
import com.yomahub.liteflow.builder.el.operator.base.BaseOperator;
import com.yomahub.liteflow.builder.el.operator.base.OperatorHelper;
import com.yomahub.liteflow.flow.element.condition.WhenCondition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* EL规则中的any的操作符
*
* @author Bryan.Zhang
* @since 2.8.0
*/
public class AnyOperator extends Operator {
public class AnyOperator extends BaseOperator {
private final Logger LOG = LoggerFactory.getLogger(this.getClass());
@Override
public Object buildCondition(Object[] objects) throws Exception {
OperatorHelper.checkObjectSizeEqTwo(objects);
@Override
public WhenCondition executeInner(Object[] objects) throws Exception {
try {
if (ArrayUtil.isEmpty(objects)) {
throw new QLException("parameter is empty");
}
WhenCondition whenCondition = OperatorHelper.convert(objects[0], WhenCondition.class);
if (objects.length != 2) {
throw new QLException("parameter error");
}
boolean any = false;
if (objects[1] instanceof Boolean) {
any = Boolean.parseBoolean(objects[1].toString().toLowerCase());
} else {
throw new QLException("the parameter must be boolean type");
}
WhenCondition whenCondition;
if (objects[0] instanceof WhenCondition) {
whenCondition = (WhenCondition) objects[0];
} else {
throw new QLException("The caller must be when condition item");
}
whenCondition.setAny(any);
boolean any = false;
if (objects[1] instanceof Boolean) {
any = Boolean.parseBoolean(objects[1].toString());
} else {
throw new QLException("the parameter must be boolean type");
}
whenCondition.setAny(any);
return whenCondition;
}catch (QLException e){
throw e;
}catch (Exception e){
throw new ELParseException("errors occurred in EL parsing");
}
}
return whenCondition;
}
}

View File

@@ -1,81 +1,61 @@
package com.yomahub.liteflow.builder.el.operator;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ArrayUtil;
import com.ql.util.express.Operator;
import com.ql.util.express.exception.QLException;
import com.yomahub.liteflow.builder.el.operator.base.BaseOperator;
import com.yomahub.liteflow.builder.el.operator.base.OperatorHelper;
import com.yomahub.liteflow.enums.NodeTypeEnum;
import com.yomahub.liteflow.exception.ELParseException;
import com.yomahub.liteflow.flow.element.Executable;
import com.yomahub.liteflow.flow.element.Node;
import com.yomahub.liteflow.flow.element.condition.IfCondition;
import java.util.List;
/**
* EL规则中的ELIF的操作符
*
* @author Bryan.Zhang
* @since 2.8.5
*/
public class ElifOperator extends Operator {
public class ElifOperator extends BaseOperator {
@Override
public Object executeInner(Object[] objects) throws Exception {
try {
if (ArrayUtil.isEmpty(objects)) {
throw new QLException("parameter is empty");
}
@Override
public Object buildCondition(Object[] objects) throws Exception {
OperatorHelper.checkObjectSizeEqThree(objects);
//参数只能是3个第一个是caller后面只能跟2个参数
if (objects.length != 3) {
throw new QLException("parameter error");
}
//解析caller
IfCondition ifCondition = OperatorHelper.convert(objects[0], IfCondition.class);
//解析caller
IfCondition ifCondition;
if (objects[0] instanceof IfCondition){
ifCondition = (IfCondition) objects[0];
}else{
throw new QLException("elif caller must be IfCondition");
}
//解析第一个参数
Node ifNode;
if (objects[1] instanceof Node) {
ifNode = (Node) objects[1];
//解析第一个参数
Node ifNode;
if (objects[1] instanceof Node) {
ifNode = (Node) objects[1];
if (!ListUtil.toList(NodeTypeEnum.IF, NodeTypeEnum.IF_SCRIPT).contains(ifNode.getType())) {
throw new QLException("The first parameter must be If item");
}
} else {
throw new QLException("The first parameter must be Node item");
}
if (!ListUtil.toList(NodeTypeEnum.IF, NodeTypeEnum.IF_SCRIPT).contains(ifNode.getType())) {
throw new QLException("The first parameter must be If item");
}
} else {
throw new QLException("The first parameter must be Node item");
}
//解析第二个参数
Executable trueCaseExecutableItem = (Executable) objects[2];
//解析第二个参数
Executable trueCaseExecutableItem = (Executable) objects[2];
//构建一个内部的IfCondition
IfCondition ifConditionItem = new IfCondition();
ifConditionItem.setExecutableList(ListUtil.toList(ifNode));
ifConditionItem.setTrueCaseExecutableItem(trueCaseExecutableItem);
//构建一个内部的IfCondition
IfCondition ifConditionItem = new IfCondition();
ifConditionItem.setExecutableList(ListUtil.toList(ifNode));
ifConditionItem.setTrueCaseExecutableItem(trueCaseExecutableItem);
//因为可能会有多个ELIF所以每一次拿到的caller总是最开始大的if需要遍历到没有falseCaseExecutable的地方。
//塞进去是一个新的IfCondition
IfCondition loopIfCondition = ifCondition;
while (true) {
if (loopIfCondition.getFalseCaseExecutableItem() == null) {
loopIfCondition.setFalseCaseExecutableItem(ifConditionItem);
break;
} else {
loopIfCondition = (IfCondition) loopIfCondition.getFalseCaseExecutableItem();
}
}
//因为可能会有多个ELIF所以每一次拿到的caller总是最开始大的if需要遍历到没有falseCaseExecutable的地方。
//塞进去是一个新的IfCondition
IfCondition loopIfCondition = ifCondition;
while (true){
if (loopIfCondition.getFalseCaseExecutableItem() == null){
loopIfCondition.setFalseCaseExecutableItem(ifConditionItem);
break;
}else{
loopIfCondition = (IfCondition) loopIfCondition.getFalseCaseExecutableItem();
}
}
return ifCondition;
}catch (QLException e){
throw e;
}catch (Exception e){
throw new ELParseException("errors occurred in EL parsing");
}
}
return ifCondition;
}
}

View File

@@ -1,61 +1,40 @@
package com.yomahub.liteflow.builder.el.operator;
import cn.hutool.core.util.ArrayUtil;
import com.ql.util.express.Operator;
import com.ql.util.express.exception.QLException;
import com.yomahub.liteflow.exception.ELParseException;
import com.yomahub.liteflow.builder.el.operator.base.BaseOperator;
import com.yomahub.liteflow.builder.el.operator.base.OperatorHelper;
import com.yomahub.liteflow.flow.element.Executable;
import com.yomahub.liteflow.flow.element.condition.IfCondition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* EL规则中的ELSE的操作符
*
* @author Bryan.Zhang
* @since 2.8.5
*/
public class ElseOperator extends Operator {
public class ElseOperator extends BaseOperator {
private final Logger LOG = LoggerFactory.getLogger(this.getClass());
@Override
public IfCondition executeInner(Object[] objects) throws Exception {
try {
if (ArrayUtil.isEmpty(objects)) {
throw new QLException("parameter is empty");
}
@Override
public Object buildCondition(Object[] objects) throws Exception {
// 参数只能是1个但这里为什么是2个呢第一个是caller第二个才是参数
OperatorHelper.checkObjectSizeEqTwo(objects);
//参数只能是1个但这里为什么是2个呢第一个是caller第二个才是参数
if (objects.length != 2) {
throw new QLException("parameter error");
}
IfCondition ifCondition = OperatorHelper.convert(objects[0], IfCondition.class);
IfCondition ifCondition;
if (objects[0] instanceof IfCondition) {
ifCondition = (IfCondition) objects[0];
} else {
throw new QLException("The caller must be IfCondition item");
}
Executable elseExecutableItem = (Executable) objects[1];
Executable elseExecutableItem = (Executable) objects[1];
// 因为当中可能会有多个ELIF所以并不知道这个ELSE前面有没有ELIF
// 每一次拿到的caller总是最开始大的if需要遍历到没有falseCaseExecutable的地方。
// 塞进去是一个elseExecutableItem
IfCondition loopIfCondition = ifCondition;
while (true) {
if (loopIfCondition.getFalseCaseExecutableItem() == null) {
loopIfCondition.setFalseCaseExecutableItem(elseExecutableItem);
break;
} else {
loopIfCondition = (IfCondition) loopIfCondition.getFalseCaseExecutableItem();
}
}
//因为当中可能会有多个ELIF所以并不知道这个ELSE前面有没有ELIF
//每一次拿到的caller总是最开始大的if需要遍历到没有falseCaseExecutable的地方。
//塞进去是一个elseExecutableItem
IfCondition loopIfCondition = ifCondition;
while (true){
if (loopIfCondition.getFalseCaseExecutableItem() == null){
loopIfCondition.setFalseCaseExecutableItem(elseExecutableItem);
break;
}else{
loopIfCondition = (IfCondition) loopIfCondition.getFalseCaseExecutableItem();
}
}
return ifCondition;
}catch (QLException e){
throw e;
}catch (Exception e){
throw new ELParseException("errors occurred in EL parsing");
}
}
return ifCondition;
}
}

View File

@@ -1,43 +1,31 @@
package com.yomahub.liteflow.builder.el.operator;
import com.ql.util.express.Operator;
import com.ql.util.express.exception.QLException;
import com.yomahub.liteflow.exception.ELParseException;
import com.yomahub.liteflow.builder.el.operator.base.BaseOperator;
import com.yomahub.liteflow.builder.el.operator.base.OperatorHelper;
import com.yomahub.liteflow.flow.element.Executable;
import com.yomahub.liteflow.flow.element.condition.FinallyCondition;
import com.yomahub.liteflow.flow.element.condition.PreCondition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* EL规则中的THEN的操作符
*
* @author Bryan.Zhang
* @since 2.8.0
*/
public class FinallyOperator extends Operator {
public class FinallyOperator extends BaseOperator {
private final Logger LOG = LoggerFactory.getLogger(this.getClass());
@Override
public Object buildCondition(Object[] objects) throws Exception {
OperatorHelper.checkObjectSizeGtZero(objects);
@Override
public FinallyCondition executeInner(Object[] objects) throws Exception {
try {
if (objects.length == 0) {
throw new QLException("parameter is empty");
}
FinallyCondition finallyCondition = new FinallyCondition();
for (Object obj : objects) {
if (obj instanceof Executable) {
finallyCondition.addExecutable((Executable) obj);
} else {
throw new QLException("parameter must be executable item!");
}
}
return finallyCondition;
}catch (QLException e){
throw e;
}catch (Exception e){
throw new ELParseException("errors occurred in EL parsing");
}
}
FinallyCondition finallyCondition = new FinallyCondition();
for (Object obj : objects) {
if (obj instanceof Executable) {
finallyCondition.addExecutable((Executable) obj);
} else {
throw new QLException("parameter must be executable item!");
}
}
return finallyCondition;
}
}

View File

@@ -1,56 +1,38 @@
package com.yomahub.liteflow.builder.el.operator;
import cn.hutool.core.util.ArrayUtil;
import com.ql.util.express.Operator;
import com.ql.util.express.exception.QLException;
import com.yomahub.liteflow.exception.ELParseException;
import com.yomahub.liteflow.builder.el.operator.base.BaseOperator;
import com.yomahub.liteflow.builder.el.operator.base.OperatorHelper;
import com.yomahub.liteflow.flow.element.condition.Condition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* EL规则中的id的操作符,只有condition可加id
*
* @author Bryan.Zhang
* @since 2.8.0
*/
public class IdOperator extends Operator {
public class IdOperator extends BaseOperator {
private final Logger LOG = LoggerFactory.getLogger(this.getClass());
private final Logger LOG = LoggerFactory.getLogger(this.getClass());
@Override
public Condition executeInner(Object[] objects) throws Exception {
try {
if (ArrayUtil.isEmpty(objects)) {
throw new QLException("parameter is empty");
}
@Override
public Object buildCondition(Object[] objects) throws Exception {
OperatorHelper.checkObjectSizeEqTwo(objects);
if (objects.length != 2) {
throw new QLException("parameter error");
}
Condition condition = OperatorHelper.convert(objects[0], Condition.class);
Condition condition;
if (objects[0] instanceof Condition) {
condition = (Condition) objects[0];
} else {
throw new QLException("The caller must be condition item");
}
String id;
if (objects[1] instanceof String) {
id = objects[1].toString();
} else {
LOG.error("the parameter must be String type!");
throw new QLException("the parameter must be String type");
}
String id;
if (objects[1] instanceof String) {
id = objects[1].toString();
} else {
LOG.error("the parameter must be String type!");
throw new QLException("the parameter must be String type");
}
condition.setId(id);
condition.setId(id);
return condition;
}catch (QLException e){
throw e;
}catch (Exception e){
throw new ELParseException("errors occurred in EL parsing");
}
}
return condition;
}
}

View File

@@ -1,68 +1,51 @@
package com.yomahub.liteflow.builder.el.operator;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ArrayUtil;
import com.ql.util.express.Operator;
import com.ql.util.express.exception.QLException;
import com.yomahub.liteflow.builder.el.operator.base.BaseOperator;
import com.yomahub.liteflow.builder.el.operator.base.OperatorHelper;
import com.yomahub.liteflow.enums.NodeTypeEnum;
import com.yomahub.liteflow.exception.ELParseException;
import com.yomahub.liteflow.flow.element.Executable;
import com.yomahub.liteflow.flow.element.Node;
import com.yomahub.liteflow.flow.element.condition.IfCondition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* EL规则中的IF的操作符
*
* @author Bryan.Zhang
* @since 2.8.5
*/
public class IfOperator extends Operator {
public class IfOperator extends BaseOperator {
private final Logger LOG = LoggerFactory.getLogger(this.getClass());
@Override
public Object buildCondition(Object[] objects) throws Exception {
OperatorHelper.checkObjectSizeEq(objects, 2, 3);
@Override
public IfCondition executeInner(Object[] objects) throws Exception {
try {
if (ArrayUtil.isEmpty(objects)) {
throw new QLException("parameter is empty");
}
//解析第一个参数
Node ifNode;
if (objects[0] instanceof Node) {
ifNode = (Node) objects[0];
//参数只能是2个或者3个
if (objects.length != 2 && objects.length != 3) {
throw new QLException("parameter error");
}
if (!ListUtil.toList(NodeTypeEnum.IF, NodeTypeEnum.IF_SCRIPT).contains(ifNode.getType())) {
throw new QLException("The first parameter must be If item");
}
} else {
throw new QLException("The first parameter must be Node item");
}
//解析第个参数
Node ifNode;
if (objects[0] instanceof Node) {
ifNode = (Node) objects[0];
//解析第个参数
Executable trueCaseExecutableItem = (Executable) objects[1];
if (!ListUtil.toList(NodeTypeEnum.IF, NodeTypeEnum.IF_SCRIPT).contains(ifNode.getType())) {
throw new QLException("The first parameter must be If item");
}
} else {
throw new QLException("The first parameter must be Node item");
}
//解析第三个参数,如果有的话
Executable falseCaseExecutableItem = null;
if (objects.length == 3) {
falseCaseExecutableItem = (Executable) objects[2];
}
//解析第二个参数
Executable trueCaseExecutableItem = (Executable) objects[1];
//解析第三个参数,如果有的话
Executable falseCaseExecutableItem = null;
if (objects.length == 3) {
falseCaseExecutableItem = (Executable) objects[2];
}
IfCondition ifCondition = new IfCondition();
ifCondition.setExecutableList(ListUtil.toList(ifNode));
ifCondition.setTrueCaseExecutableItem(trueCaseExecutableItem);
ifCondition.setFalseCaseExecutableItem(falseCaseExecutableItem);
return ifCondition;
}catch (QLException e){
throw e;
}catch (Exception e){
throw new ELParseException("errors occurred in EL parsing");
}
}
IfCondition ifCondition = new IfCondition();
ifCondition.setExecutableList(ListUtil.toList(ifNode));
ifCondition.setTrueCaseExecutableItem(trueCaseExecutableItem);
ifCondition.setFalseCaseExecutableItem(falseCaseExecutableItem);
return ifCondition;
}
}

View File

@@ -1,57 +1,33 @@
package com.yomahub.liteflow.builder.el.operator;
import cn.hutool.core.util.ArrayUtil;
import com.ql.util.express.Operator;
import com.ql.util.express.exception.QLException;
import com.yomahub.liteflow.exception.ELParseException;
import com.yomahub.liteflow.flow.element.Executable;
import com.yomahub.liteflow.flow.element.condition.Condition;
import com.yomahub.liteflow.builder.el.operator.base.BaseOperator;
import com.yomahub.liteflow.builder.el.operator.base.OperatorHelper;
import com.yomahub.liteflow.flow.element.condition.WhenCondition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* EL规则中的ignoreError的操作符
*
* @author Bryan.Zhang
* @since 2.8.0
*/
public class IgnoreErrorOperator extends Operator {
public class IgnoreErrorOperator extends BaseOperator {
private final Logger LOG = LoggerFactory.getLogger(this.getClass());
@Override
public Object buildCondition(Object[] objects) throws Exception {
OperatorHelper.checkObjectSizeEqTwo(objects);
@Override
public Condition executeInner(Object[] objects) throws Exception {
try {
if (ArrayUtil.isEmpty(objects)) {
throw new QLException("parameter is empty");
}
WhenCondition condition = OperatorHelper.convert(objects[0], WhenCondition.class);
if (objects.length != 2) {
throw new QLException("parameter error");
}
boolean ignoreError = false;
if (objects[1] instanceof Boolean) {
ignoreError = Boolean.parseBoolean(objects[1].toString().toLowerCase());
} else {
throw new QLException("The parameter must be boolean type");
}
WhenCondition condition;
if (objects[0] instanceof WhenCondition) {
condition = (WhenCondition) objects[0];
} else {
throw new QLException("The caller must be executable item");
}
condition.setErrorResume(ignoreError);
boolean ignoreError = false;
if (objects[1] instanceof Boolean) {
ignoreError = Boolean.parseBoolean(objects[1].toString());
} else {
throw new QLException("The parameter must be boolean type");
}
condition.setErrorResume(ignoreError);
return condition;
}catch (QLException e){
throw e;
}catch (Exception e){
throw new ELParseException("errors occurred in EL parsing");
}
}
return condition;
}
}

View File

@@ -1,69 +1,51 @@
package com.yomahub.liteflow.builder.el.operator;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.ql.util.express.Operator;
import com.ql.util.express.exception.QLException;
import com.yomahub.liteflow.exception.ELParseException;
import com.yomahub.liteflow.builder.el.operator.base.BaseOperator;
import com.yomahub.liteflow.builder.el.operator.base.OperatorHelper;
import com.yomahub.liteflow.flow.FlowBus;
import com.yomahub.liteflow.flow.element.Node;
import com.yomahub.liteflow.property.LiteflowConfig;
import com.yomahub.liteflow.property.LiteflowConfigGetter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.function.Predicate;
/**
* EL规则中的node的操作符
*
* @author Bryan.Zhang
* @since 2.8.3
*/
public class NodeOperator extends Operator {
public class NodeOperator extends BaseOperator {
private final Logger LOG = LoggerFactory.getLogger(this.getClass());
@Override
public Object buildCondition(Object[] objects) throws Exception {
OperatorHelper.checkObjectSizeNeqOne(objects);
@Override
public Node executeInner(Object[] objects) throws Exception {
try{
if (ArrayUtil.isEmpty(objects)){
throw new QLException("parameter is empty");
}
String nodeId;
if (objects[0] instanceof String) {
nodeId = (String) objects[0];
} else {
throw new QLException("The value must be Node item!");
}
if (objects.length != 1){
throw new QLException("parameter error");
}
String nodeId;
if (objects[0] instanceof String){
nodeId = (String) objects[0];
}else{
throw new QLException("The value must be Node item!");
}
if (FlowBus.containNode(nodeId)){
return FlowBus.getNode(nodeId);
}else{
LiteflowConfig liteflowConfig = LiteflowConfigGetter.get();
if (StrUtil.isNotBlank(liteflowConfig.getSubstituteCmpClass())){
Node substituteNode = FlowBus.getNodeMap().values().stream().filter(node
-> node.getInstance().getClass().getName().equals(liteflowConfig.getSubstituteCmpClass())).findFirst().orElse(null);
if (ObjectUtil.isNotNull(substituteNode)){
return substituteNode;
}else{
String error = StrUtil.format("This node[{}] cannot be found", nodeId);
throw new QLException(error);
}
}else{
String error = StrUtil.format("This node[{}] cannot be found, or you can configure an substitute node", nodeId);
throw new QLException(error);
}
}
}catch (QLException e){
throw e;
}catch (Exception e){
throw new ELParseException("errors occurred in EL parsing");
}
}
if (FlowBus.containNode(nodeId)) {
return FlowBus.getNode(nodeId);
} else {
LiteflowConfig liteflowConfig = LiteflowConfigGetter.get();
if (StrUtil.isNotBlank(liteflowConfig.getSubstituteCmpClass())) {
Node substituteNode = FlowBus.getNodeMap().values().stream().filter(node
-> node.getInstance().getClass().getName().equals(liteflowConfig.getSubstituteCmpClass())).findFirst().orElse(null);
if (ObjectUtil.isNotNull(substituteNode)) {
return substituteNode;
} else {
String error = StrUtil.format("This node[{}] cannot be found", nodeId);
throw new QLException(error);
}
} else {
String error = StrUtil.format("This node[{}] cannot be found, or you can configure an substitute node", nodeId);
throw new QLException(error);
}
}
}
}

View File

@@ -1,43 +1,31 @@
package com.yomahub.liteflow.builder.el.operator;
import com.ql.util.express.Operator;
import com.ql.util.express.exception.QLException;
import com.yomahub.liteflow.exception.ELParseException;
import com.yomahub.liteflow.builder.el.operator.base.BaseOperator;
import com.yomahub.liteflow.builder.el.operator.base.OperatorHelper;
import com.yomahub.liteflow.flow.element.Executable;
import com.yomahub.liteflow.flow.element.condition.PreCondition;
import com.yomahub.liteflow.flow.element.condition.ThenCondition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* EL规则中的THEN的操作符
*
* @author Bryan.Zhang
* @since 2.8.0
*/
public class PreOperator extends Operator {
public class PreOperator extends BaseOperator {
private final Logger LOG = LoggerFactory.getLogger(this.getClass());
@Override
public Object buildCondition(Object[] objects) throws Exception {
OperatorHelper.checkObjectSizeGtZero(objects);
@Override
public PreCondition executeInner(Object[] objects) throws Exception {
try {
if (objects.length == 0) {
throw new QLException("parameter is empty");
}
PreCondition preCondition = new PreCondition();
for (Object obj : objects) {
if (obj instanceof Executable) {
preCondition.addExecutable((Executable) obj);
} else {
throw new QLException("parameter must be executable item");
}
}
return preCondition;
}catch (QLException e){
throw e;
}catch (Exception e){
throw new ELParseException("errors occurred in EL parsing");
}
}
PreCondition preCondition = new PreCondition();
for (Object obj : objects) {
if (obj instanceof Executable) {
preCondition.addExecutable((Executable) obj);
} else {
throw new QLException("parameter must be executable item");
}
}
return preCondition;
}
}

View File

@@ -1,56 +1,39 @@
package com.yomahub.liteflow.builder.el.operator;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ArrayUtil;
import com.ql.util.express.Operator;
import com.ql.util.express.exception.QLException;
import com.yomahub.liteflow.builder.el.operator.base.BaseOperator;
import com.yomahub.liteflow.builder.el.operator.base.OperatorHelper;
import com.yomahub.liteflow.enums.NodeTypeEnum;
import com.yomahub.liteflow.exception.ELParseException;
import com.yomahub.liteflow.flow.element.Node;
import com.yomahub.liteflow.flow.element.condition.SwitchCondition;
import com.yomahub.liteflow.flow.element.condition.WhenCondition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* EL规则中的SWITCH的操作符
*
* @author Bryan.Zhang
* @since 2.8.0
*/
public class SwitchOperator extends Operator {
public class SwitchOperator extends BaseOperator {
private final Logger LOG = LoggerFactory.getLogger(this.getClass());
@Override
public Object buildCondition(Object[] objects) throws Exception {
OperatorHelper.checkObjectSizeNeqOne(objects);
@Override
public SwitchCondition executeInner(Object[] objects) throws Exception {
try {
if (ArrayUtil.isEmpty(objects)) {
throw new QLException("parameter is empty");
}
Node switchNode;
if (objects[0] instanceof Node) {
switchNode = (Node) objects[0];
if (objects.length != 1) {
throw new QLException("parameter error");
}
if (!ListUtil.toList(NodeTypeEnum.SWITCH, NodeTypeEnum.SWITCH_SCRIPT).contains(switchNode.getType())) {
throw new QLException("The caller must be Switch item");
}
} else {
throw new QLException("The caller must be Switch item");
}
Node switchNode;
if (objects[0] instanceof Node) {
switchNode = (Node) objects[0];
SwitchCondition switchCondition = new SwitchCondition();
switchCondition.setSwitchNode(switchNode);
if (!ListUtil.toList(NodeTypeEnum.SWITCH, NodeTypeEnum.SWITCH_SCRIPT).contains(switchNode.getType())) {
throw new QLException("The caller must be Switch item");
}
} else {
throw new QLException("The caller must be Switch item");
}
SwitchCondition switchCondition = new SwitchCondition();
switchCondition.setSwitchNode(switchNode);
return switchCondition;
}catch (QLException e){
throw e;
}catch (Exception e){
throw new ELParseException("errors occurred in EL parsing");
}
}
return switchCondition;
}
}

View File

@@ -1,61 +1,38 @@
package com.yomahub.liteflow.builder.el.operator;
import cn.hutool.core.util.ArrayUtil;
import com.ql.util.express.Operator;
import com.ql.util.express.exception.QLException;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.exception.ELParseException;
import com.yomahub.liteflow.builder.el.operator.base.BaseOperator;
import com.yomahub.liteflow.builder.el.operator.base.OperatorHelper;
import com.yomahub.liteflow.flow.FlowBus;
import com.yomahub.liteflow.flow.element.Node;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* EL规则中的tag的操作符
*
* @author Bryan.Zhang
* @since 2.8.0
*/
public class TagOperator extends Operator {
public class TagOperator extends BaseOperator {
private final Logger LOG = LoggerFactory.getLogger(this.getClass());
@Override
public Object buildCondition(Object[] objects) throws Exception {
OperatorHelper.checkObjectSizeEqTwo(objects);
@Override
public Node executeInner(Object[] objects) throws Exception {
try {
if (ArrayUtil.isEmpty(objects)) {
throw new QLException("parameter is empty");
}
Node node = OperatorHelper.convert(objects[0], Node.class);
if (objects.length != 2) {
throw new QLException("parameter error");
}
String tag = null;
if (objects[1] instanceof String) {
tag = objects[1].toString();
} else {
throw new QLException("the parameter must be String type");
}
Node node;
if (objects[0] instanceof Node) {
node = (Node) objects[0];
} else {
throw new QLException("The caller must be Node item");
}
//这里为什么要clone一个呢
//因为tag是跟着chain走的。而在el上下文里的放的都是同一个node如果多个同样的node tag不同则这里必须copy
Node copyNode = FlowBus.copyNode(node.getId());
String tag = null;
if (objects[1] instanceof String) {
tag = objects[1].toString();
} else {
throw new QLException("the parameter must be String type");
}
copyNode.setTag(tag);
//这里为什么要clone一个呢
//因为tag是跟着chain走的。而在el上下文里的放的都是同一个node如果多个同样的node tag不同则这里必须copy
Node copyNode = FlowBus.copyNode(node.getId());
copyNode.setTag(tag);
return copyNode;
}catch (QLException e){
throw e;
}catch (Exception e){
throw new ELParseException("errors occurred in EL parsing");
}
}
return copyNode;
}
}

View File

@@ -1,42 +1,31 @@
package com.yomahub.liteflow.builder.el.operator;
import com.ql.util.express.Operator;
import com.ql.util.express.exception.QLException;
import com.yomahub.liteflow.exception.ELParseException;
import com.yomahub.liteflow.builder.el.operator.base.BaseOperator;
import com.yomahub.liteflow.builder.el.operator.base.OperatorHelper;
import com.yomahub.liteflow.flow.element.Executable;
import com.yomahub.liteflow.flow.element.condition.ThenCondition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* EL规则中的THEN的操作符
*
* @author Bryan.Zhang
* @since 2.8.0
*/
public class ThenOperator extends Operator {
public class ThenOperator extends BaseOperator {
private final Logger LOG = LoggerFactory.getLogger(this.getClass());
@Override
public Object buildCondition(Object[] objects) throws Exception {
OperatorHelper.checkObjectSizeGtZero(objects);
@Override
public ThenCondition executeInner(Object[] objects) throws Exception {
try {
if (objects.length == 0) {
throw new QLException("parameter is empty");
}
ThenCondition thenCondition = new ThenCondition();
for (Object obj : objects) {
if (obj instanceof Executable) {
thenCondition.addExecutable((Executable) obj);
} else {
throw new QLException("parameter must be executable item");
}
}
return thenCondition;
}catch (QLException e){
throw e;
}catch (Exception e){
throw new ELParseException("errors occurred in EL parsing");
}
}
ThenCondition thenCondition = new ThenCondition();
for (Object obj : objects) {
if (obj instanceof Executable) {
thenCondition.addExecutable((Executable) obj);
} else {
throw new QLException("parameter must be executable item");
}
}
return thenCondition;
}
}

View File

@@ -1,55 +1,33 @@
package com.yomahub.liteflow.builder.el.operator;
import cn.hutool.core.util.ArrayUtil;
import com.ql.util.express.Operator;
import com.ql.util.express.exception.QLException;
import com.yomahub.liteflow.exception.ELParseException;
import com.yomahub.liteflow.builder.el.operator.base.BaseOperator;
import com.yomahub.liteflow.builder.el.operator.base.OperatorHelper;
import com.yomahub.liteflow.flow.element.condition.WhenCondition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* EL规则中的threadPool的操作符
*
* @author Bryan.Zhang
* @since 2.8.0
*/
public class ThreadPoolOperator extends Operator {
public class ThreadPoolOperator extends BaseOperator {
private final Logger LOG = LoggerFactory.getLogger(this.getClass());
@Override
public Object buildCondition(Object[] objects) throws Exception {
OperatorHelper.checkObjectSizeEqTwo(objects);
@Override
public WhenCondition executeInner(Object[] objects) throws Exception {
try {
if (ArrayUtil.isEmpty(objects)) {
throw new QLException("parameter is empty");
}
WhenCondition whenCondition = OperatorHelper.convert(objects[0], WhenCondition.class);
if (objects.length != 2) {
throw new QLException("parameter error");
}
String threadPoolClazz = null;
if (objects[1] instanceof String) {
threadPoolClazz = objects[1].toString();
} else {
throw new QLException("the parameter must be String type");
}
WhenCondition whenCondition;
if (objects[0] instanceof WhenCondition) {
whenCondition = (WhenCondition) objects[0];
} else {
throw new QLException("The caller must be when condition item");
}
whenCondition.setThreadExecutorClass(threadPoolClazz);
String threadPoolClazz = null;
if (objects[1] instanceof String) {
threadPoolClazz = objects[1].toString();
} else {
throw new QLException("the parameter must be String type");
}
whenCondition.setThreadExecutorClass(threadPoolClazz);
return whenCondition;
}catch (QLException e){
throw e;
}catch (Exception e){
throw new ELParseException("errors occurred in EL parsing");
}
}
return whenCondition;
}
}

View File

@@ -1,54 +1,33 @@
package com.yomahub.liteflow.builder.el.operator;
import cn.hutool.core.util.ArrayUtil;
import com.ql.util.express.Operator;
import com.ql.util.express.exception.QLException;
import com.yomahub.liteflow.exception.ELParseException;
import com.yomahub.liteflow.builder.el.operator.base.BaseOperator;
import com.yomahub.liteflow.builder.el.operator.base.OperatorHelper;
import com.yomahub.liteflow.flow.element.Executable;
import com.yomahub.liteflow.flow.element.condition.SwitchCondition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* EL规则中的TO的操作符用法须和SWITCH联合使用
*
* @author Bryan.Zhang
* @since 2.8.0
*/
public class ToOperator extends Operator {
public class ToOperator extends BaseOperator {
private final Logger LOG = LoggerFactory.getLogger(this.getClass());
@Override
public Object buildCondition(Object[] objects) throws Exception {
OperatorHelper.checkObjectSizeGtTwo(objects);
@Override
public SwitchCondition executeInner(Object[] objects) throws Exception {
try {
if (ArrayUtil.isEmpty(objects)) {
throw new QLException("parameter is empty");
}
SwitchCondition switchCondition = OperatorHelper.convert(objects[0], SwitchCondition.class);
if (objects.length <= 1) {
throw new QLException("parameter error");
}
SwitchCondition switchCondition;
if (objects[0] instanceof SwitchCondition) {
switchCondition = (SwitchCondition) objects[0];
} else {
throw new QLException("The caller must be SwitchCondition item");
}
for (int i = 1; i < objects.length; i++) {
if (objects[i] instanceof Executable) {
Executable target = (Executable) objects[i];
switchCondition.addTargetItem(target);
} else {
throw new QLException("The parameter must be Executable item");
}
}
return switchCondition;
}catch (QLException e){
throw e;
}catch (Exception e){
throw new ELParseException("errors occurred in EL parsing");
}
}
for (int i = 1; i < objects.length; i++) {
if (objects[i] instanceof Executable) {
Executable target = (Executable) objects[i];
switchCondition.addTargetItem(target);
} else {
throw new QLException("The parameter must be Executable item");
}
}
return switchCondition;
}
}

View File

@@ -1,42 +1,31 @@
package com.yomahub.liteflow.builder.el.operator;
import com.ql.util.express.Operator;
import com.ql.util.express.exception.QLException;
import com.yomahub.liteflow.exception.ELParseException;
import com.yomahub.liteflow.builder.el.operator.base.BaseOperator;
import com.yomahub.liteflow.builder.el.operator.base.OperatorHelper;
import com.yomahub.liteflow.flow.element.Executable;
import com.yomahub.liteflow.flow.element.condition.WhenCondition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* EL规则中的WHEN的操作符
*
* @author Bryan.Zhang
* @since 2.8.0
*/
public class WhenOperator extends Operator {
public class WhenOperator extends BaseOperator {
private final Logger LOG = LoggerFactory.getLogger(this.getClass());
@Override
public Object buildCondition(Object[] objects) throws Exception {
OperatorHelper.checkObjectSizeGtZero(objects);
@Override
public WhenCondition executeInner(Object[] objects) throws Exception {
try {
if (objects.length == 0) {
throw new QLException("parameter error");
}
WhenCondition whenCondition = new WhenCondition();
for (Object obj : objects) {
if (obj instanceof Executable) {
whenCondition.addExecutable((Executable) obj);
} else {
throw new QLException("parameter error");
}
}
return whenCondition;
}catch (QLException e){
throw e;
}catch (Exception e){
throw new ELParseException("errors occurred in EL parsing");
}
}
WhenCondition whenCondition = new WhenCondition();
for (Object obj : objects) {
if (obj instanceof Executable) {
whenCondition.addExecutable((Executable) obj);
} else {
throw new QLException("parameter error");
}
}
return whenCondition;
}
}

View File

@@ -0,0 +1,34 @@
package com.yomahub.liteflow.builder.el.operator.base;
import com.ql.util.express.Operator;
import com.ql.util.express.exception.QLException;
import com.yomahub.liteflow.exception.ELParseException;
/**
* BaseOperator 为了强化 executeInner 方法,会捕获抛出的 QLException 错误,输出友好的错误提示
*
* @author gaibu
* @date 2022/8/28 14:32
*/
public abstract class BaseOperator extends Operator {
@Override
public Object executeInner(Object[] objects) throws Exception {
try {
return buildCondition(objects);
} catch (QLException e) {
throw e;
} catch (Exception e) {
throw new ELParseException("errors occurred in EL parsing");
}
}
/**
* 构建 EL 条件
*
* @param objects objects
* @return Condition
* @throws Exception Exception
*/
public abstract Object buildCondition(Object[] objects) throws Exception;
}

View File

@@ -0,0 +1,130 @@
package com.yomahub.liteflow.builder.el.operator.base;
import com.ql.util.express.exception.QLException;
/**
* Operator 常用工具类
*
* @author gaibu
* @ate 2022/8/28 12:58
*/
public class OperatorHelper {
/**
* 检查参数数量不等于1
*
* @param objects objects
* @throws QLException QLException
*/
public static void checkObjectSizeNeqOne(Object[] objects) throws QLException {
checkObjectSizeNeq(objects, 1);
}
/**
* 检查参数数量,不等于 size
*
* @param objects objects
* @param size 参数数量
* @throws QLException QLException
*/
public static void checkObjectSizeNeq(Object[] objects, int size) throws QLException {
checkObjectSizeGtZero(objects);
if (objects.length != size) {
throw new QLException("parameter error");
}
}
/**
* 检查参数数量,大于 0
*
* @param objects objects
* @throws QLException QLException
*/
public static void checkObjectSizeGtZero(Object[] objects) throws QLException {
if (objects.length == 0) {
throw new QLException("parameter is empty");
}
}
/**
* 检查参数数量,大于 2
*
* @param objects objects
* @throws QLException QLException
*/
public static void checkObjectSizeGtTwo(Object[] objects) throws QLException {
checkObjectSizeGtZero(objects);
if (objects.length <= 1) {
throw new QLException("parameter error");
}
}
/**
* 检查参数数量,等于 2
*
* @param objects objects
* @throws QLException QLException
*/
public static void checkObjectSizeEqTwo(Object[] objects) throws QLException {
checkObjectSizeEq(objects, 2);
}
/**
* 检查参数数量,等于 3
*
* @param objects objects
* @throws QLException QLException
*/
public static void checkObjectSizeEqThree(Object[] objects) throws QLException {
checkObjectSizeEq(objects, 3);
}
/**
* 检查参数数量,等于 size
*
* @param objects objects
* @param size 参数数量
* @throws QLException QLException
*/
public static void checkObjectSizeEq(Object[] objects, int size) throws QLException {
checkObjectSizeGtZero(objects);
if (objects.length != size) {
throw new QLException("parameter error");
}
}
/**
* 检查参数数量,等于 size1 或 size2
*
* @param objects objects
* @param size1 参数数量1
* @param size2 参数数量2
* @throws QLException QLException
*/
public static void checkObjectSizeEq(Object[] objects, int size1, int size2) throws QLException {
checkObjectSizeGtZero(objects);
if (objects.length != size1 && objects.length != size2) {
throw new QLException("parameter error");
}
}
/**
* 转换 object 为指定的类型
*
* @param object object
* @param tClass 指定类型
* @param <T> 返回类型
* @return T
* @throws QLException QLException
*/
public static <T> T convert(Object object, Class<T> tClass) throws QLException {
if (tClass.isInstance(object)) {
return (T) object;
}
throw new QLException("The caller must be " + tClass.getName() + " item");
}
}