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