feat:版本测试新增规则引擎动作
parent
7e8eb968c0
commit
38d52196d2
|
@ -1,6 +1,7 @@
|
||||||
package com.ruoyi.dataTransform.controller;
|
package com.ruoyi.dataTransform.controller;
|
||||||
|
|
||||||
import com.ruoyi.common.core.domain.Result;
|
import com.ruoyi.common.core.domain.Result;
|
||||||
|
import com.ruoyi.common.security.annotation.RequiresPermissions;
|
||||||
import com.ruoyi.dataTransform.domain.req.RandomDataReq;
|
import com.ruoyi.dataTransform.domain.req.RandomDataReq;
|
||||||
import com.ruoyi.dataTransform.service.DataExtractService;
|
import com.ruoyi.dataTransform.service.DataExtractService;
|
||||||
import com.ruoyi.ruleEngine.client.model.DataModel;
|
import com.ruoyi.ruleEngine.client.model.DataModel;
|
||||||
|
@ -28,7 +29,7 @@ public class DataExtractController {
|
||||||
* 获取随机数据
|
* 获取随机数据
|
||||||
*/
|
*/
|
||||||
@ApiOperation("获取随机数据")
|
@ApiOperation("获取随机数据")
|
||||||
//@RequiresPermissions("dataTransform:extract:query")
|
@RequiresPermissions("dataTransform:extract:query")
|
||||||
@GetMapping("/getRandomData")
|
@GetMapping("/getRandomData")
|
||||||
public Result<List<List<DataModel>>> getRandomData(RandomDataReq randomDataReq) {
|
public Result<List<List<DataModel>>> getRandomData(RandomDataReq randomDataReq) {
|
||||||
return Result.success(dataExtractService.getRandomData(randomDataReq));
|
return Result.success(dataExtractService.getRandomData(randomDataReq));
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package com.ruoyi.dataTransform.controller;
|
package com.ruoyi.dataTransform.controller;
|
||||||
|
|
||||||
import com.ruoyi.common.core.domain.Result;
|
import com.ruoyi.common.core.domain.Result;
|
||||||
|
import com.ruoyi.common.security.annotation.RequiresPermissions;
|
||||||
import com.ruoyi.dataTransform.domain.req.TestDataReq;
|
import com.ruoyi.dataTransform.domain.req.TestDataReq;
|
||||||
import com.ruoyi.dataTransform.service.EngineOperationService;
|
import com.ruoyi.dataTransform.service.EngineOperationService;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
|
@ -26,9 +27,9 @@ public class EngineOperationController {
|
||||||
* 测试引擎
|
* 测试引擎
|
||||||
*/
|
*/
|
||||||
@ApiOperation("测试引擎")
|
@ApiOperation("测试引擎")
|
||||||
//@RequiresPermissions("dataTransform:operation:add")
|
@RequiresPermissions("dataTransform:operation:add")
|
||||||
@PostMapping("/testVersion")
|
@PostMapping("/testVersion")
|
||||||
public Result<Object> testVersion(@RequestBody TestDataReq testDataReq) {
|
public Result<Object> testVersion(@RequestBody TestDataReq testDataReq) {
|
||||||
return Result.success(engineOperationService.testVersion(testDataReq),"测试成功");
|
return engineOperationService.testVersion(testDataReq);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package com.ruoyi.dataTransform.service;
|
package com.ruoyi.dataTransform.service;
|
||||||
|
|
||||||
|
import com.ruoyi.common.core.domain.Result;
|
||||||
import com.ruoyi.dataTransform.domain.req.TestDataReq;
|
import com.ruoyi.dataTransform.domain.req.TestDataReq;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -15,6 +16,6 @@ public interface EngineOperationService {
|
||||||
* @param testDataReq 测试数据
|
* @param testDataReq 测试数据
|
||||||
* @return 测试结果
|
* @return 测试结果
|
||||||
*/
|
*/
|
||||||
Object testVersion(TestDataReq testDataReq);
|
Result<Object> testVersion(TestDataReq testDataReq);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package com.ruoyi.dataTransform.service.impl;
|
package com.ruoyi.dataTransform.service.impl;
|
||||||
|
|
||||||
|
import com.ruoyi.common.core.domain.Result;
|
||||||
import com.ruoyi.common.core.exception.ServiceException;
|
import com.ruoyi.common.core.exception.ServiceException;
|
||||||
import com.ruoyi.dataTransform.domain.req.TestDataReq;
|
import com.ruoyi.dataTransform.domain.req.TestDataReq;
|
||||||
import com.ruoyi.dataTransform.service.EngineOperationService;
|
import com.ruoyi.dataTransform.service.EngineOperationService;
|
||||||
|
@ -8,6 +9,8 @@ import com.ruoyi.ruleEngine.client.context.DataModelContextHolder;
|
||||||
import com.ruoyi.ruleEngine.client.context.DataSetContextHolder;
|
import com.ruoyi.ruleEngine.client.context.DataSetContextHolder;
|
||||||
import com.ruoyi.ruleEngine.client.context.RecordContextHolder;
|
import com.ruoyi.ruleEngine.client.context.RecordContextHolder;
|
||||||
import com.ruoyi.ruleEngine.client.dynamicLoad.DynamicLoader;
|
import com.ruoyi.ruleEngine.client.dynamicLoad.DynamicLoader;
|
||||||
|
import com.ruoyi.ruleEngine.client.engine.action.ActionRemove;
|
||||||
|
import com.ruoyi.ruleEngine.client.engine.action.ActionReplace;
|
||||||
import com.ruoyi.ruleEngine.client.model.DataModel;
|
import com.ruoyi.ruleEngine.client.model.DataModel;
|
||||||
import com.ruoyi.ruleEngine.client.model.DataSetModel;
|
import com.ruoyi.ruleEngine.client.model.DataSetModel;
|
||||||
import com.ruoyi.ruleEngine.client.model.RecordModel;
|
import com.ruoyi.ruleEngine.client.model.RecordModel;
|
||||||
|
@ -41,7 +44,7 @@ public class EngineOperationServiceImpl implements EngineOperationService {
|
||||||
private RemoteRuleEngineService remoteRuleEngineService;
|
private RemoteRuleEngineService remoteRuleEngineService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object testVersion(TestDataReq testDataReq) {
|
public Result<Object> testVersion(TestDataReq testDataReq) {
|
||||||
// 根据作用域编号放入对应的上下文中
|
// 根据作用域编号放入对应的上下文中
|
||||||
switch (testDataReq.getScope()){
|
switch (testDataReq.getScope()){
|
||||||
case 2: {
|
case 2: {
|
||||||
|
@ -66,49 +69,96 @@ public class EngineOperationServiceImpl implements EngineOperationService {
|
||||||
// 将数据放入模型上下文中
|
// 将数据放入模型上下文中
|
||||||
DataModelContextHolder.set(dataModelProcessModel);
|
DataModelContextHolder.set(dataModelProcessModel);
|
||||||
}break;
|
}break;
|
||||||
default:throw new ServiceException("此作用域暂无法测试");
|
default:throw new ServiceException("此作用域暂无法测试",Result.SUCCESS);
|
||||||
}
|
}
|
||||||
|
StringBuffer actionRecords=new StringBuffer("数据发生动作[");
|
||||||
// 获取版本对应class文件的字节数组
|
// 获取版本对应class文件的字节数组
|
||||||
Map<String, byte[]> bytecode = ruleEngineVersionFactory.get(testDataReq.getVersionId());
|
Map<String, byte[]> bytecode = ruleEngineVersionFactory.get(testDataReq.getVersionId());
|
||||||
|
try {
|
||||||
// 加载class文件到虚拟机中,然后通过反射执行
|
// 加载class文件到虚拟机中,然后通过反射执行
|
||||||
@SuppressWarnings("resource")
|
@SuppressWarnings("resource")
|
||||||
DynamicLoader.MemoryClassLoader classLoader = new DynamicLoader.MemoryClassLoader(bytecode);
|
DynamicLoader.MemoryClassLoader classLoader = new DynamicLoader.MemoryClassLoader(bytecode);
|
||||||
try {
|
|
||||||
Class<?> clazz = classLoader.loadClass(testDataReq.getVersionClass());
|
Class<?> clazz = classLoader.loadClass(testDataReq.getVersionClass());
|
||||||
// 调用execution方法
|
// 调用execution方法
|
||||||
Method mainMethod = clazz.getDeclaredMethod("execution");
|
Method mainMethod = clazz.getDeclaredMethod("execution");
|
||||||
mainMethod.invoke(clazz.newInstance());
|
mainMethod.invoke(clazz.newInstance());
|
||||||
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException |
|
}catch (InvocationTargetException e){
|
||||||
InstantiationException e) {
|
// 获取调用的映射方法内的异常
|
||||||
|
this.actionHandler(e.getTargetException(),actionRecords ,testDataReq.getVersionId());
|
||||||
|
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InstantiationException | NullPointerException e) {
|
||||||
log.info("测试失败,{}",e.getMessage());
|
log.info("测试失败,{}",e.getMessage());
|
||||||
//修改测试状态
|
//修改测试状态
|
||||||
remoteRuleEngineService.edit(testDataReq.getVersionId(),"0");
|
remoteRuleEngineService.edit(testDataReq.getVersionId(),"0");
|
||||||
throw new ServiceException("测试失败,"+e.getMessage());
|
throw new ServiceException("测试失败,"+e.getMessage(), Result.SUCCESS);
|
||||||
}
|
}
|
||||||
//修改测试状态
|
//修改测试状态
|
||||||
remoteRuleEngineService.edit(testDataReq.getVersionId(),"1");
|
remoteRuleEngineService.edit(testDataReq.getVersionId(),"1");
|
||||||
// 处理后的测试数据
|
// 返回结果
|
||||||
|
String res="测试成功";
|
||||||
|
if(actionRecords.toString().length()!=7){
|
||||||
|
res+=","+actionRecords+"]";
|
||||||
|
}
|
||||||
switch (testDataReq.getScope()) {
|
switch (testDataReq.getScope()) {
|
||||||
case 2 -> {
|
case 2 -> {
|
||||||
//获取处理完的数据
|
//获取处理完的数据
|
||||||
DataSetModel dataSetModel = DataSetContextHolder.get().getDataSetModel();
|
DataSetModel dataSetModel = DataSetContextHolder.get().getDataSetModel();
|
||||||
//删除线程变量
|
//删除线程变量
|
||||||
DataSetContextHolder.remove();
|
DataSetContextHolder.remove();
|
||||||
return dataSetModel;
|
return Result.success(dataSetModel,res);
|
||||||
}
|
}
|
||||||
case 3 -> {
|
case 3 -> {
|
||||||
RecordModel recordModel = RecordContextHolder.get().getRecordModel();
|
RecordModel recordModel = RecordContextHolder.get().getRecordModel();
|
||||||
RecordContextHolder.remove();
|
RecordContextHolder.remove();
|
||||||
return recordModel;
|
return Result.success(recordModel,res);
|
||||||
}
|
}
|
||||||
case 4 -> {
|
case 4 -> {
|
||||||
DataModel dataModel = DataModelContextHolder.get().getDataModel();
|
DataModel dataModel = DataModelContextHolder.get().getDataModel();
|
||||||
DataModelContextHolder.remove();
|
DataModelContextHolder.remove();
|
||||||
return dataModel;
|
return Result.success(dataModel,res);
|
||||||
}
|
}
|
||||||
default -> {
|
default -> {
|
||||||
return null;
|
throw new ServiceException("此作用域暂无法测试",Result.SUCCESS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 动作处理
|
||||||
|
* @param e 异常
|
||||||
|
* @param actionRecords 动作记录字符串
|
||||||
|
* @param versionId 版本编号
|
||||||
|
*/
|
||||||
|
private void actionHandler(Throwable e,StringBuffer actionRecords,Long versionId) {
|
||||||
|
String records = actionRecords.toString();
|
||||||
|
//判断动作
|
||||||
|
if(e instanceof ActionReplace actionReplace){
|
||||||
|
String newValue = actionReplace.getNewValue();
|
||||||
|
log.info("匹配到替换动作,新值是:{}",newValue);
|
||||||
|
//处理动作
|
||||||
|
actionReplace.getDataModel().setValue(newValue);
|
||||||
|
//记录发生的动作
|
||||||
|
if(!records.endsWith("[")){
|
||||||
|
if(!records.contains("替换")){
|
||||||
|
actionRecords.append("/替换");
|
||||||
|
}
|
||||||
|
}else {
|
||||||
|
actionRecords.append("替换");
|
||||||
|
}
|
||||||
|
}else if (e instanceof ActionRemove actionRemove){
|
||||||
|
log.info("匹配到移除动作");
|
||||||
|
actionRemove.getDataModel().setValue(null);
|
||||||
|
if(!actionRecords.toString().endsWith("[")){
|
||||||
|
if(!records.contains("移除")){
|
||||||
|
actionRecords.append("/移除");
|
||||||
|
}
|
||||||
|
}else {
|
||||||
|
actionRecords.append("移除");
|
||||||
|
}
|
||||||
|
}else {
|
||||||
|
log.info("测试失败,{}",e.getMessage());
|
||||||
|
//修改测试状态
|
||||||
|
remoteRuleEngineService.edit(versionId,"0");
|
||||||
|
throw new ServiceException("测试失败,"+e.getMessage(),Result.SUCCESS);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +0,0 @@
|
||||||
package com.ruoyi.ruleEngine.client.engine.action;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 忽略动作
|
|
||||||
* @ClassName ActionIgnore
|
|
||||||
* @Author: 森静若林
|
|
||||||
* @Date: 2024/5/6 13:48
|
|
||||||
*/
|
|
||||||
public class ActionIgnore extends RuntimeException{
|
|
||||||
}
|
|
|
@ -4,7 +4,8 @@ package com.ruoyi.ruleEngine.client.engine.action;
|
||||||
* 记录动作
|
* 记录动作
|
||||||
* @ClassName ActionRecords
|
* @ClassName ActionRecords
|
||||||
* @Author 森静若林
|
* @Author 森静若林
|
||||||
* @Date 2024/5/12 22:31
|
* @Date 2024/5/13 22:31
|
||||||
*/
|
*/
|
||||||
public class ActionRecords extends RuntimeException{
|
public class ActionRecords {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,29 @@
|
||||||
package com.ruoyi.ruleEngine.client.engine.action;
|
package com.ruoyi.ruleEngine.client.engine.action;
|
||||||
|
|
||||||
|
import com.ruoyi.ruleEngine.client.model.DataModel;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 移除动作
|
* 移除动作
|
||||||
* @ClassName ActionRemove
|
* @ClassName ActionRemove
|
||||||
* @Author: 森静若林
|
* @Author: 森静若林
|
||||||
* @Date: 2024/5/6 13:48
|
* @Date: 2024/5/6 13:48
|
||||||
*/
|
*/
|
||||||
|
@Setter
|
||||||
|
@Getter
|
||||||
public class ActionRemove extends RuntimeException{
|
public class ActionRemove extends RuntimeException{
|
||||||
|
/**
|
||||||
|
* 数据模型
|
||||||
|
*/
|
||||||
|
private DataModel dataModel;
|
||||||
|
|
||||||
|
public ActionRemove(DataModel dataModel) {
|
||||||
|
this.dataModel = dataModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void remove(){
|
||||||
|
this.dataModel.setValue(null);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,37 @@
|
||||||
|
package com.ruoyi.ruleEngine.client.engine.action;
|
||||||
|
|
||||||
|
import com.ruoyi.ruleEngine.client.model.DataModel;
|
||||||
|
import lombok.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 替换动作
|
||||||
|
* @ClassName ActionReplace
|
||||||
|
* @Author: 森静若林
|
||||||
|
* @Date: 2024/5/6 13:48
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class ActionReplace extends RuntimeException{
|
||||||
|
/**
|
||||||
|
* 新值
|
||||||
|
*/
|
||||||
|
private String newValue;
|
||||||
|
/**
|
||||||
|
* 数据模型
|
||||||
|
*/
|
||||||
|
private DataModel dataModel;
|
||||||
|
|
||||||
|
public ActionReplace(String newValue,DataModel dataModel) {
|
||||||
|
super();
|
||||||
|
this.newValue = newValue;
|
||||||
|
this.dataModel = dataModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ActionReplace() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void replace(){
|
||||||
|
this.dataModel.setValue(this.newValue);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue