Skip to content

Commit

Permalink
Merge branch 'dev' into 3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
LostRed committed Mar 21, 2023
2 parents f8d3b66 + 032211c commit 1f3d401
Show file tree
Hide file tree
Showing 18 changed files with 75 additions and 65 deletions.
4 changes: 2 additions & 2 deletions integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>info.lostred.ruler</groupId>
<artifactId>integration-tests</artifactId>
<version>3.2.6</version>
<version>3.2.7</version>
<name>integration tests</name>
<description>tests for ruler project.</description>

Expand Down Expand Up @@ -39,7 +39,7 @@
<dependency>
<groupId>info.lostred.ruler</groupId>
<artifactId>ruler-spring-boot-starter</artifactId>
<version>3.2.6</version>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<groupId>info.lostred.ruler</groupId>
<artifactId>ruler-project</artifactId>
<packaging>pom</packaging>
<version>3.2.6</version>
<version>3.2.7</version>
<name>ruler project</name>
<description>ruler project</description>

Expand Down
2 changes: 1 addition & 1 deletion ruler-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>info.lostred.ruler</groupId>
<artifactId>ruler-core</artifactId>
<version>3.2.6</version>
<version>3.2.7</version>
<name>ruler core</name>
<description>ruler core.</description>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package info.lostred.ruler.annotation;

import info.lostred.ruler.constant.Grade;
import info.lostred.ruler.constant.RulerConstants;

import java.lang.annotation.*;

Expand Down Expand Up @@ -29,7 +30,7 @@
*
* @return 业务类型
*/
String businessType();
String businessType() default RulerConstants.BUSINESS_TYPE_COMMON;

/**
* 规则的严重等级
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import info.lostred.ruler.engine.AbstractRulesEngine;
import info.lostred.ruler.engine.RulesEngine;
import info.lostred.ruler.exception.RulesEnginesException;
import info.lostred.ruler.factory.RuleFactory;
import org.springframework.expression.BeanResolver;

Expand All @@ -28,9 +29,11 @@ private RulesEngineBuilder(Class<? extends AbstractRulesEngine> rulesEngineClass
Constructor<? extends AbstractRulesEngine> constructor = rulesEngineClass.getDeclaredConstructor();
abstractRulesEngine = constructor.newInstance();
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
throw new RulesEnginesException("Please provide a no argument constructor in " + rulesEngineClass.getName() +
", override 'init()' method to initialize its member parameters.", e, rulesEngineClass);
} catch (InvocationTargetException | InstantiationException | IllegalAccessException e) {
throw new IllegalArgumentException(e);
throw new RulesEnginesException("Internal error: " + rulesEngineClass.getName() +
" cannot be instantiated.", e, rulesEngineClass);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,4 @@ public final class RulerConstants {
* 通用业务类型
*/
public static final String BUSINESS_TYPE_COMMON = "COMMON";
/**
* 评估上下文执行结果变量名
*/
public final static String RESULT = "result";
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
public interface Evaluator {
/**
* 根据评估上下文与表达式解析器,评估参数是否满足特定的条件
* 评估参数是否满足特定的条件
*
* @param object 参数
* @return 满足条件返回true,否则返回false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static RuleDefinition of(Rule rule, Class<? extends AbstractRule> ruleCla

public static RuleDefinition of(String description,
Class<? extends AbstractRule> ruleClass, String parameterExp, String conditionExp, String predicateExp) {
return RuleDefinition.of(UUID.randomUUID().toString(), RulerConstants.BUSINESS_TYPE_COMMON, Grade.ILLEGAL, description,
return of(UUID.randomUUID().toString(), RulerConstants.BUSINESS_TYPE_COMMON, Grade.ILLEGAL, description,
0, false, true,
ruleClass, parameterExp, conditionExp, predicateExp);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ protected void initContext(Object rootObject) {
ExecutionContextHolder.setContext(context);
}

/**
* 销毁评估上下文
*/
protected void destroyContext() {
ExecutionContextHolder.clear();
}

/**
* 规则执行
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package info.lostred.ruler.engine;

import info.lostred.ruler.core.ExecutionContextHolder;
import info.lostred.ruler.domain.Result;
import info.lostred.ruler.domain.RuleDefinition;
import info.lostred.ruler.exception.RulesEnginesException;
Expand All @@ -22,12 +21,14 @@ public Result execute(Object rootObject) {
this.executeInternal(rootObject, rule, result);
} catch (Exception e) {
RuleDefinition ruleDefinition = rule.getRuleDefinition();
throw new RulesEnginesException("rule[" + ruleDefinition.getRuleCode() + " " + ruleDefinition.getGrade() + "] has occurred an exception: " + e.getMessage(), e, this.getBusinessType(), this.getClass());
throw new RulesEnginesException("rule[" + ruleDefinition.getRuleCode() +
" " + ruleDefinition.getGrade() + "] has occurred an exception: " +
e.getMessage(), e, this.getBusinessType(), this.getClass());
}
}
return result;
} finally {
ExecutionContextHolder.clear();
this.destroyContext();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package info.lostred.ruler.engine;

import info.lostred.ruler.core.ExecutionContextHolder;
import info.lostred.ruler.domain.Result;
import info.lostred.ruler.domain.RuleDefinition;
import info.lostred.ruler.exception.RulesEnginesException;
import info.lostred.ruler.rule.AbstractRule;

Expand All @@ -18,16 +18,19 @@ public Result execute(Object rootObject) {
Result result = Result.newInstance();
for (AbstractRule rule : rules) {
try {
if (this.executeInternal(ExecutionContextHolder.getContext(), rule, result)) {
if (this.executeInternal(rootObject, rule, result)) {
return result;
}
} catch (Exception e) {
throw new RulesEnginesException("rule[" + rule.getRuleDefinition().getRuleCode() + "] has occurred an exception: " + e.getMessage(), this.getBusinessType(), this.getClass());
RuleDefinition ruleDefinition = rule.getRuleDefinition();
throw new RulesEnginesException("rule[" + ruleDefinition.getRuleCode() +
" " + ruleDefinition.getGrade() + "] has occurred an exception: " +
e.getMessage(), e, this.getBusinessType(), this.getClass());
}
}
return result;
} finally {
ExecutionContextHolder.clear();
this.destroyContext();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @author lostred
*/
public class RulesEnginesException extends RuntimeException {
private final String businessType;
private String businessType;
private final Class<?> rulesEngineType;

public RulesEnginesException(String businessType,
Expand All @@ -29,6 +29,11 @@ public RulesEnginesException(String message, Throwable cause, String businessTyp
this.rulesEngineType = rulesEngineType;
}

public RulesEnginesException(String message, Throwable cause, Class<?> rulesEngineClass) {
super(message, cause);
this.rulesEngineType = rulesEngineClass;
}

public RulesEnginesException(Throwable cause, String businessType,
Class<?> rulesEngineType) {
super(cause);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ protected AbstractRule createRule(String ruleCode, RuleDefinition ruleDefinition
}
abstractRule.init();
return abstractRule;
} catch (InvocationTargetException | InstantiationException | IllegalAccessException e) {
throw new RulesException("Internal error: " + ruleClass.getName() +
" cannot be instantiated.", e, ruleDefinition);
} catch (NoSuchMethodException e) {
throw new RulesException("Please provide a no argument constructor in " + ruleClass.getName() +
", override 'init()' method to initialize its member parameters.", e, ruleDefinition);
} catch (InvocationTargetException | InstantiationException | IllegalAccessException e) {
throw new RulesException("Internal error: " + ruleClass.getName() +
" cannot be instantiated.", e, ruleDefinition);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void init() {
}

/**
* 在给定的评估上下文与表达式解析器下,评估接口是否支持对该参数进行判断
* 规则是否支持对该参数进行判断
*
* @param object 参数
* @return 支持返回true,否则返回false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ public boolean supports(Object object) {
return Boolean.TRUE.equals(flag);
}

@Override
public Object getValue(Object object) {
String parameterExp = this.getRuleDefinition().getParameterExp();
return this.getExpressionParser()
.parseExpression(parameterExp)
.getValue(ExecutionContextHolder.getContext());
}

@Override
public boolean evaluate(Object object) {
String predicateExp = this.getRuleDefinition().getPredicateExp();
Expand All @@ -35,14 +43,6 @@ public boolean evaluate(Object object) {
return Boolean.TRUE.equals(flag);
}

@Override
public Object getValue(Object object) {
String parameterExp = this.getRuleDefinition().getParameterExp();
return this.getExpressionParser()
.parseExpression(parameterExp)
.getValue(ExecutionContextHolder.getContext());
}

public ExpressionParser getExpressionParser() {
return expressionParser;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package info.lostred.ruler.rule;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
* 编程式规则
* <p>由规则的校验对象为评估上下文的根对象,作为supportsInternal、evaluateInternal和getValueInternal方法的入参,
Expand All @@ -13,25 +10,10 @@
* @since 2.2.0
*/
public abstract class ProgrammaticRule<T> extends AbstractRule {
protected Class<T> type;

@SuppressWarnings("unchecked")
public ProgrammaticRule() {
Type type = this.getClass().getGenericSuperclass();
if (type instanceof ParameterizedType) {
Type[] types = ((ParameterizedType) type).getActualTypeArguments();
Type superType = types[0];
if (superType instanceof Class) {
this.type = (Class<T>) superType;
} else if (superType instanceof ParameterizedType) {
Type rawType = ((ParameterizedType) superType).getRawType();
if (rawType instanceof Class) {
this.type = (Class<T>) rawType;
}
} else {
throw new IllegalArgumentException("ProgrammaticRule must be a parameterized type");
}
}
@Override
public boolean supports(Object object) {
return this.supportsInternal((T) object);
}

@SuppressWarnings("unchecked")
Expand All @@ -40,21 +22,33 @@ public Object getValue(Object object) {
return this.getValueInternal((T) object);
}

@SuppressWarnings("unchecked")
@Override
public boolean supports(Object object) {
return this.supportsInternal((T) object);
}

@SuppressWarnings("unchecked")
@Override
public boolean evaluate(Object object) {
return this.evaluateInternal((T) object);
}

public abstract Object getValueInternal(T object);

/**
* 规则是否支持对该参数进行判断
*
* @param object 转换成泛型类后的参数
* @return 支持返回true,否则返回false
*/
public abstract boolean supportsInternal(T object);

/**
* 获取需要记录的值
*
* @param object 转换成泛型类后的参数
* @return 需要记录的值
*/
public abstract Object getValueInternal(T object);

/**
* 评估参数是否满足特定的条件
*
* @param object 转换成泛型类后的参数
* @return 满足条件返回true,否则返回false
*/
public abstract boolean evaluateInternal(T object);
}
4 changes: 2 additions & 2 deletions ruler-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>info.lostred.ruler</groupId>
<artifactId>ruler-spring-boot-starter</artifactId>
<version>3.2.6</version>
<version>3.2.7</version>
<name>ruler spring boot starter</name>
<description>ruler's spring boot starter.</description>

Expand Down Expand Up @@ -52,7 +52,7 @@
<dependency>
<groupId>info.lostred.ruler</groupId>
<artifactId>ruler-core</artifactId>
<version>3.2.6</version>
<version>3.2.7</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ protected Set<BeanDefinitionHolder> doScan(@NonNull String... basePackages) {
@Override
protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
AnnotationMetadata metadata = beanDefinition.getMetadata();
return metadata.isConcrete();
return metadata.isConcrete() && metadata.isIndependent();
}
}

0 comments on commit 1f3d401

Please sign in to comment.