feat():规则引擎简易版
parent
e10817cb1b
commit
1abb6e89b9
|
@ -111,11 +111,11 @@ public class ScopeController extends BaseController
|
||||||
/**
|
/**
|
||||||
* 测试
|
* 测试
|
||||||
*/
|
*/
|
||||||
@RequiresPermissions("engine:scope:test")
|
@RequiresPermissions("engine:scope:testEngine")
|
||||||
@Log(title = "规则引擎", businessType = BusinessType.DELETE)
|
@Log(title = "规则引擎", businessType = BusinessType.DELETE)
|
||||||
@GetMapping("test/{id}")
|
@GetMapping("testEngine/{id}")
|
||||||
public Result test(@PathVariable Long id) throws ServletException {
|
public Result test(@PathVariable Long id) throws Exception {
|
||||||
return toAjax(scopeService.test(id));
|
return toAjax(scopeService.testEngine(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package com.muyu.engine.service;
|
package com.muyu.engine.service;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.extension.service.IService;
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
@ -72,5 +74,5 @@ public interface ScopeService extends IService<Scope>
|
||||||
|
|
||||||
void deleteScopeByRuleIds(Long[] ids);
|
void deleteScopeByRuleIds(Long[] ids);
|
||||||
|
|
||||||
boolean test(Long id) throws ServletException;
|
boolean testEngine(Long id) throws Exception;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
package com.muyu.engine.service.impl;
|
package com.muyu.engine.service.impl;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -18,6 +21,7 @@ import org.springframework.stereotype.Service;
|
||||||
import com.muyu.engine.mapper.ScopeMapper;
|
import com.muyu.engine.mapper.ScopeMapper;
|
||||||
import com.muyu.engine.domain.Scope;
|
import com.muyu.engine.domain.Scope;
|
||||||
import com.muyu.engine.service.ScopeService;
|
import com.muyu.engine.service.ScopeService;
|
||||||
|
import org.springframework.util.ReflectionUtils;
|
||||||
|
|
||||||
import javax.servlet.ServletException;
|
import javax.servlet.ServletException;
|
||||||
|
|
||||||
|
@ -29,8 +33,7 @@ import javax.servlet.ServletException;
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
@Log4j2
|
@Log4j2
|
||||||
public class ScopeServiceImpl extends ServiceImpl<ScopeMapper,Scope> implements ScopeService
|
public class ScopeServiceImpl extends ServiceImpl<ScopeMapper, Scope> implements ScopeService {
|
||||||
{
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ScopeMapper scopeMapper;
|
private ScopeMapper scopeMapper;
|
||||||
@Autowired
|
@Autowired
|
||||||
|
@ -43,8 +46,7 @@ public class ScopeServiceImpl extends ServiceImpl<ScopeMapper,Scope> implements
|
||||||
* @return 规则引擎代码
|
* @return 规则引擎代码
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Scope selectScopeById(Long id)
|
public Scope selectScopeById(Long id) {
|
||||||
{
|
|
||||||
return scopeMapper.selectScopeById(id);
|
return scopeMapper.selectScopeById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,8 +57,7 @@ public class ScopeServiceImpl extends ServiceImpl<ScopeMapper,Scope> implements
|
||||||
* @return 规则引擎代码
|
* @return 规则引擎代码
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public List<Scope> selectScopeList(Scope scope)
|
public List<Scope> selectScopeList(Scope scope) {
|
||||||
{
|
|
||||||
return scopeMapper.selectScopeList(scope);
|
return scopeMapper.selectScopeList(scope);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,8 +68,7 @@ public class ScopeServiceImpl extends ServiceImpl<ScopeMapper,Scope> implements
|
||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int insertScope(Scope scope)
|
public int insertScope(Scope scope) {
|
||||||
{
|
|
||||||
scope.setCreateTime(DateUtils.getNowDate());
|
scope.setCreateTime(DateUtils.getNowDate());
|
||||||
return scopeMapper.insertScope(scope);
|
return scopeMapper.insertScope(scope);
|
||||||
}
|
}
|
||||||
|
@ -80,8 +80,7 @@ public class ScopeServiceImpl extends ServiceImpl<ScopeMapper,Scope> implements
|
||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int updateScope(Scope scope)
|
public int updateScope(Scope scope) {
|
||||||
{
|
|
||||||
scope.setUpdateTime(DateUtils.getNowDate());
|
scope.setUpdateTime(DateUtils.getNowDate());
|
||||||
Scope one = this.getOne(new LambdaQueryWrapper<>() {{
|
Scope one = this.getOne(new LambdaQueryWrapper<>() {{
|
||||||
eq(Scope::getValue, scope.getValue());
|
eq(Scope::getValue, scope.getValue());
|
||||||
|
@ -98,8 +97,7 @@ public class ScopeServiceImpl extends ServiceImpl<ScopeMapper,Scope> implements
|
||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int deleteScopeByIds(Long[] ids)
|
public int deleteScopeByIds(Long[] ids) {
|
||||||
{
|
|
||||||
return scopeMapper.deleteScopeByIds(ids);
|
return scopeMapper.deleteScopeByIds(ids);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -110,8 +108,7 @@ public class ScopeServiceImpl extends ServiceImpl<ScopeMapper,Scope> implements
|
||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int deleteScopeById(Long id)
|
public int deleteScopeById(Long id) {
|
||||||
{
|
|
||||||
return scopeMapper.deleteScopeById(id);
|
return scopeMapper.deleteScopeById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,12 +127,48 @@ public class ScopeServiceImpl extends ServiceImpl<ScopeMapper,Scope> implements
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean test(Long id) throws ServletException {
|
public boolean testEngine(Long id) throws Exception {
|
||||||
|
|
||||||
|
RuleEngine ruleEngine = ruleEngineService.getById(id);
|
||||||
|
if (ruleEngine.getIsActivate().contains("no")) throw new ServletException("未激活");
|
||||||
|
if (ruleEngine.getStatus().contains("1")) throw new ServletException("已停用");
|
||||||
|
Scope scope = this.getOne(new LambdaQueryWrapper<Scope>() {{
|
||||||
|
eq(Scope::getRuleEngineId, ruleEngine.getId());
|
||||||
|
eq(Scope::getValue, "taskContext");
|
||||||
|
}});
|
||||||
|
String code = scope.getCode();
|
||||||
|
String path = code.substring(code.indexOf("com"), code.indexOf(";")).replaceAll("/.", "/").trim();
|
||||||
|
String fileName = code.substring(code.indexOf("class") + 6, code.indexOf("{")).trim();
|
||||||
|
String name = path+"."+fileName;
|
||||||
|
String javaPackageName = name.replace(".",File.separator)+".java";
|
||||||
|
String javaAbsolutePath = "D:/ruoyi/FinallyTest/muyu-modules/muyu-ruleEngine/muyu-ruleEngine-service/src/main/java/" +javaPackageName;
|
||||||
|
String jarAbsolutePath = "D:/ruoyi/FinallyTest/muyu-modules/muyu-ruleEngine/muyu-ruleEngine-service/target/classes/com/muyu/engine/controller";
|
||||||
|
Process process = Runtime.getRuntime().exec("javac -classpath "+ jarAbsolutePath+ " " + javaAbsolutePath);
|
||||||
|
ClassLoader classLoader = ReflectionUtils.class.getClassLoader();
|
||||||
|
// Class aClass = Class.forName(fileName);
|
||||||
|
Class aClass = classLoader.loadClass(name);
|
||||||
|
Object o = aClass.newInstance();
|
||||||
|
Method test = aClass.getMethod("test",null);
|
||||||
|
test.invoke(o,null);
|
||||||
|
try {
|
||||||
|
InputStream errorStream = process.getErrorStream();
|
||||||
|
InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
|
||||||
|
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
|
||||||
|
String line = null;
|
||||||
|
while ((line=bufferedReader.readLine()) != null){
|
||||||
|
System.out.println(line);
|
||||||
|
}
|
||||||
|
int exitVal = process.waitFor();
|
||||||
|
System.out.println("Process exitValue: " + exitVal);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 引擎流程并执行测试
|
* 引擎流程并执行测试
|
||||||
|
*
|
||||||
* @param id
|
* @param id
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
|
@ -147,8 +180,7 @@ public class ScopeServiceImpl extends ServiceImpl<ScopeMapper,Scope> implements
|
||||||
Supplier<Boolean> booleanSupplier = () -> {
|
Supplier<Boolean> booleanSupplier = () -> {
|
||||||
try {
|
try {
|
||||||
return testEngine(ruleEngine);
|
return testEngine(ruleEngine);
|
||||||
} catch (ServletException e) {
|
} catch (Exception e) {
|
||||||
log.error(e.getMessage());
|
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -156,10 +188,32 @@ public class ScopeServiceImpl extends ServiceImpl<ScopeMapper,Scope> implements
|
||||||
return booleanSupplier.get();
|
return booleanSupplier.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Boolean testEngine(RuleEngine ruleEngine) throws ServletException {
|
private Boolean testEngine(RuleEngine ruleEngine) throws ServletException, IOException {
|
||||||
System.out.println("初始化");
|
System.out.println("初始化");
|
||||||
System.out.println(ruleEngine);
|
System.out.println(ruleEngine);
|
||||||
if(false)throw new ServletException("读取异常");
|
Scope scope = this.getOne(new LambdaQueryWrapper<Scope>() {{
|
||||||
|
eq(Scope::getRuleEngineId, ruleEngine.getId());
|
||||||
|
eq(Scope::getValue, "taskContext");
|
||||||
|
}});
|
||||||
|
String code = scope.getCode();
|
||||||
|
String path = code.substring(code.indexOf("com"), code.indexOf(";")).replaceAll("\\.", "/").trim();
|
||||||
|
String fileName = code.substring(code.indexOf("class") + 6, code.indexOf("{")).trim();
|
||||||
|
File file = new File("D:/ruoyi/FinallyTest/muyu-modules/muyu-ruleEngine/muyu-ruleEngine-service/src/main/java/" + path + "/" + fileName + ".java");
|
||||||
|
if (!file.exists()) {
|
||||||
|
file.createNewFile();
|
||||||
|
}
|
||||||
|
String[] split = code.split("/n");
|
||||||
|
//OutputStreamWriter对象将字符转换为字节流,指定了文件输出流和编码方式。
|
||||||
|
OutputStreamWriter outputStreamWriter =
|
||||||
|
new OutputStreamWriter(new FileOutputStream(file, true), "UTF-8");
|
||||||
|
//BufferedWriter对象,用于缓冲写入数据,提高写入效率。
|
||||||
|
BufferedWriter bw = new BufferedWriter(outputStreamWriter);
|
||||||
|
for (String str : split) {
|
||||||
|
bw.write(str);
|
||||||
|
bw.newLine();
|
||||||
|
}
|
||||||
|
bw.close();
|
||||||
|
outputStreamWriter.close();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue