1.新增创建规则和链路方法

This commit is contained in:
chenweilong 2022-12-08 18:40:30 +08:00
parent ed9b7789b8
commit 4e501863e3
17 changed files with 464 additions and 39 deletions

View File

@ -100,5 +100,10 @@
<version>3.22.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -20,7 +20,7 @@ import java.util.ArrayList;
import java.util.List;
@Component
public class LiteFlowCommand implements CommandLineRunner {
public class LiteFlowCommand {
private final Logger log = LoggerFactory.getLogger(this.getClass());
@ -32,7 +32,7 @@ public class LiteFlowCommand implements CommandLineRunner {
private final List<String> costCenterList = Lists.newArrayList("cost01AndCost02", "cost01", "cost02");
@Override
// @Override
public void run(String... args) throws Exception {
// 事实对象
// CostCenter costCenter = new CostCenter();

View File

@ -13,6 +13,7 @@ import com.example.liteflow.mysql.enums.BaseDataAttributionEnum;
import com.example.liteflow.mysql.model.BaseCenter;
import com.example.liteflow.mysql.model.CostCenter;
import com.example.liteflow.mysql.service.BaseDataService;
import com.example.liteflow.mysql.service.BaseRoleService;
import com.example.liteflow.mysql.util.ConditionParser;
import com.googlecode.aviator.AviatorEvaluator;
import com.yomahub.liteflow.annotation.LiteflowComponent;
@ -31,14 +32,18 @@ import java.lang.reflect.Field;
public class CostCenter01Cmp extends NodeIfComponent {
@Resource
private BaseDataService baseDataService;
@Resource
private BaseRoleService baseRoleService;
private static final String drool = "[{\"$or\": [{\"$and\": [{\"$contains\": [sectionClassification, sectionClassificationList]}, {\"$contains\": [businessType, businessTypeList]}, {\"$contains\": [salesman, salesmanList]}, {\"$contains\": [businessCategory, businessCategoryList]}]}]}]";
private static final String baseRoleName = "costCenter01Cmp";
@Override
public boolean processIf() throws Exception {
BaseCenter requestData = this.getRequestData();
log.info("requestData = {}", JSONUtil.toJsonStr(requestData));
String droolStr = drool;
String droolStr = baseRoleService.getByName(baseRoleName);
log.info("droolStr = {}", droolStr);
Field[] fields = ReflectUtil.getFields(BaseCenter.class);
for (Field field : fields) {
Object fieldValue = ReflectUtil.getFieldValue(requestData, field);
@ -49,7 +54,11 @@ public class CostCenter01Cmp extends NodeIfComponent {
String contentName = baseDataEntity.getContentName();
droolStr = StrUtil.replace(droolStr, contentName, content, false);
}
droolStr = StrUtil.replace(droolStr, name, "\"" + fieldValue + "\"", false);
String filedValueStr = "";
if (ObjectUtil.isNotNull(fieldValue)) {
filedValueStr = fieldValue.toString();
}
droolStr = StrUtil.replace(droolStr, name, filedValueStr, false);
}
log.info("after - droolStr = {}", droolStr);

View File

@ -12,6 +12,7 @@ import com.example.liteflow.mysql.enums.BaseDataAttributionEnum;
import com.example.liteflow.mysql.model.BaseCenter;
import com.example.liteflow.mysql.model.CostCenter;
import com.example.liteflow.mysql.service.BaseDataService;
import com.example.liteflow.mysql.service.BaseRoleService;
import com.example.liteflow.mysql.util.ConditionParser;
import com.googlecode.aviator.AviatorEvaluator;
import com.yomahub.liteflow.annotation.LiteflowComponent;
@ -37,10 +38,15 @@ public class CostCenter02Cmp extends NodeIfComponent {
@Resource
private BaseDataService baseDataService;
@Resource
private BaseRoleService baseRoleService;
private static final String baseRoleName = "costCenter02Cmp";
@Override
public boolean processIf() throws Exception {
BaseCenter requestData = this.getRequestData();
String droolStr = drool;
String droolStr = baseRoleService.getByName(baseRoleName);
log.info("droolStr = {}", droolStr);
log.info("CostCenter02Cmp-droolStr = {}", droolStr);
Field[] fields = ReflectUtil.getFields(BaseCenter.class);
for (Field field : fields) {
Object fieldValue = ReflectUtil.getFieldValue(requestData, field);

View File

@ -0,0 +1,43 @@
package com.example.liteflow.mysql.controller;
import cn.hutool.json.JSONUtil;
import com.example.liteflow.mysql.model.role.Role;
import com.example.liteflow.mysql.model.role.RoleForm;
import com.example.liteflow.mysql.service.BaseRoleService;
import com.example.liteflow.mysql.util.RoleUtil;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.enums.FlowParserTypeEnum;
import com.yomahub.liteflow.flow.FlowBus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author: 陈韦龙
* @date: 2022年12月07日 10:36
*/
@Slf4j
@RestController
@RequestMapping("/baseRole")
public class BaseRoleController {
@Resource
private BaseRoleService baseRoleService;
@Resource
private FlowExecutor flowExecutor;
@PostMapping("/createRole")
public void createRole(@RequestBody RoleForm roleForm) {
baseRoleService.createRole(roleForm);
// List<Role> roles = roleForm.getRoles();
// List<Map<String, Object>> collect = roles.stream().map(RoleUtil::parseRole).collect(Collectors.toList());
// System.out.println(JSONUtil.toJsonStr(collect));
// flowExecutor.reloadRule();
}
}

View File

@ -0,0 +1,31 @@
package com.example.liteflow.mysql.controller;
import com.example.liteflow.mysql.entity.ChainEntity;
import com.example.liteflow.mysql.model.role.RoleForm;
import com.example.liteflow.mysql.service.ChainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
/**
* @author: 陈韦龙
* @date: 2022年12月08日 18:31
*/
@Slf4j
@RestController
@RequestMapping("/chain")
public class ChainController {
@Resource
private ChainService chainService;
@PostMapping("/createChain")
public void createChain(@RequestBody ChainEntity chainEntity) {
chainService.createChain(chainEntity);
}
@GetMapping("/executeChain/{chainName}")
public void executeChain(@PathVariable("chainName") String chainName) {
chainService.executeChain(chainName);
}
}

View File

@ -1,31 +0,0 @@
package com.example.liteflow.mysql.controller;
import cn.hutool.json.JSONUtil;
import com.example.liteflow.mysql.model.role.Role;
import com.example.liteflow.mysql.model.role.RoleForm;
import com.example.liteflow.mysql.util.RoleUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author: 陈韦龙
* @date: 2022年12月07日 10:36
*/
@Slf4j
@RestController
@RequestMapping("/submitRole")
public class SubmitRoleController {
@PostMapping
public void orderTicket(@RequestBody RoleForm roleForm) {
List<Role> roles = roleForm.getRoles();
List<Map<String, Object>> collect = roles.stream().map(RoleUtil::parseRole).collect(Collectors.toList());
System.out.println(JSONUtil.toJsonStr(collect));
}
}

View File

@ -0,0 +1,22 @@
package com.example.liteflow.mysql.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
/**
* @author: 陈韦龙
* @date: 2022年12月06日 09:59
*/
@TableName("base_role")
@Data
public class BaseRoleEntity {
@TableId(value = "ID", type = IdType.ASSIGN_UUID)
private String id;
@TableField("role_str")
private String roleStr;
@TableField("role_name")
private String roleName;
}

View File

@ -0,0 +1,30 @@
package com.example.liteflow.mysql.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.time.LocalDateTime;
/**
* @author: 陈韦龙
* @date: 2022年12月06日 09:59
*/
@TableName("chain")
@Data
public class ChainEntity {
@TableId(value = "ID", type = IdType.AUTO)
private String id;
@TableField("application_name")
private String applicationName;
@TableField("chain_name")
private String chainName;
@TableField("chain_desc")
private String chainDesc;
@TableField("el_data")
private String elData;
@TableField("create_time")
private LocalDateTime createTime = LocalDateTime.now();
}

View File

@ -0,0 +1,13 @@
package com.example.liteflow.mysql.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.liteflow.mysql.entity.BaseRoleEntity;
import org.apache.ibatis.annotations.Mapper;
/**
* @author: 陈韦龙
* @date: 2022年12月06日 10:08
*/
@Mapper
public interface BaseRoleMapper extends BaseMapper<BaseRoleEntity> {
}

View File

@ -0,0 +1,13 @@
package com.example.liteflow.mysql.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.liteflow.mysql.entity.ChainEntity;
import org.apache.ibatis.annotations.Mapper;
/**
* @author: 陈韦龙
* @date: 2022年12月06日 10:08
*/
@Mapper
public interface ChainMapper extends BaseMapper<ChainEntity> {
}

View File

@ -10,5 +10,6 @@ import java.util.List;
*/
@Data
public class RoleForm {
private String roleName;
private List<Role> roles;
}

View File

@ -0,0 +1,13 @@
package com.example.liteflow.mysql.service;
import com.example.liteflow.mysql.model.role.RoleForm;
/**
* @author: 陈韦龙
* @date: 2022年12月08日 16:09
*/
public interface BaseRoleService {
void createRole(RoleForm roleForm);
String getByName(String roleName);
}

View File

@ -0,0 +1,14 @@
package com.example.liteflow.mysql.service;
import com.example.liteflow.mysql.entity.ChainEntity;
/**
* @author: 陈韦龙
* @date: 2022年12月08日 18:23
*/
public interface ChainService {
void createChain(ChainEntity chainEntity);
void executeChain(String chainName);
}

View File

@ -0,0 +1,63 @@
package com.example.liteflow.mysql.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.liteflow.mysql.entity.BaseRoleEntity;
import com.example.liteflow.mysql.mapper.BaseRoleMapper;
import com.example.liteflow.mysql.model.role.Role;
import com.example.liteflow.mysql.model.role.RoleForm;
import com.example.liteflow.mysql.service.BaseRoleService;
import com.example.liteflow.mysql.util.RoleUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author: 陈韦龙
* @date: 2022年12月08日 16:09
*/
@Slf4j
@Service
public class BaseRoleServiceImpl extends ServiceImpl<BaseRoleMapper, BaseRoleEntity> implements BaseRoleService {
@Override
public void createRole(RoleForm roleForm) {
List<Role> roles = roleForm.getRoles();
String roleName = roleForm.getRoleName();
LambdaQueryWrapper<BaseRoleEntity> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(BaseRoleEntity::getRoleName, roleName);
BaseRoleEntity baseRoleEntity = this.getOne(queryWrapper);
if(ObjectUtil.isNull(baseRoleEntity)) {
baseRoleEntity = new BaseRoleEntity();
baseRoleEntity.setRoleStr(JSONUtil.toJsonStr(roles));
baseRoleEntity.setRoleName(JSONUtil.toJsonStr(roleName));
this.save(baseRoleEntity);
} else {
baseRoleEntity.setRoleStr(JSONUtil.toJsonStr(roles));
this.updateById(baseRoleEntity);
}
}
@Override
public String getByName(String roleName) {
LambdaQueryWrapper<BaseRoleEntity> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(BaseRoleEntity::getRoleName, roleName);
BaseRoleEntity baseRoleEntity = this.getOne(queryWrapper);
String roleStr = baseRoleEntity.getRoleStr();
log.info("roleStr = {}", roleStr);
// JSONObject jsonObject = JSONUtil.parseObj(roleStr);
List<Role> roles = JSONUtil.toList(JSONUtil.parseArray(roleStr), Role.class);
List<Map<String, Object>> collect = roles.stream().map(RoleUtil::parseRole).collect(Collectors.toList());
return JSONUtil.toJsonStr(collect);
}
}

View File

@ -0,0 +1,85 @@
package com.example.liteflow.mysql.service.impl;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.liteflow.mysql.entity.ChainEntity;
import com.example.liteflow.mysql.mapper.ChainMapper;
import com.example.liteflow.mysql.model.BaseCenter;
import com.example.liteflow.mysql.model.CostCenter;
import com.example.liteflow.mysql.model.ProfitCenter;
import com.example.liteflow.mysql.service.ChainService;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.flow.LiteflowResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
/**
* @author: 陈韦龙
* @date: 2022年12月08日 18:23
*/
@Service
@Slf4j
public class ChainServiceImpl extends ServiceImpl<ChainMapper, ChainEntity> implements ChainService {
@Resource
private FlowExecutor flowExecutor;
@Override
public void createChain(ChainEntity chainEntity) {
this.save(chainEntity);
flowExecutor.reloadRule();
}
@Override
public void executeChain(String chainName) {
List<BaseCenter> list = this.generateCostCenterTestData();
log.info("before-list = {}", JSONUtil.toJsonStr(list));
list.forEach(item -> {
LiteflowResponse response = flowExecutor.execute2Resp(chainName, item);
if (response.isSuccess()) {
log.info("执行成功");
} else {
log.info("执行失败");
}
});
log.info("after-list = {}", JSONUtil.toJsonStr(list));
}
private List<BaseCenter> generateCostCenterTestData() {
List<BaseCenter> list = new ArrayList<>();
CostCenter costCenter = new CostCenter();
costCenter.setSectionClassification("成本科目");
costCenter.setBusinessType("货代");
costCenter.setSalesman("仓干配物流部(物流)");
costCenter.setBusinessCategory("全货船业务");
list.add(costCenter);
costCenter = new CostCenter();
costCenter.setBusinessType("货代");
costCenter.setSalesman("仓干配物流部(物流)");
list.add(costCenter);
costCenter = new CostCenter();
costCenter.setBusinessType("货代");
list.add(costCenter);
ProfitCenter profitCenter = new ProfitCenter();
profitCenter.setSectionClassification("银行科目");
profitCenter.setBusinessType("货代");
profitCenter.setSalesman("商品车物流部(物流)");
profitCenter.setBusinessCategory("运输业务");
list.add(profitCenter);
profitCenter = new ProfitCenter();
profitCenter.setBusinessType("货代");
profitCenter.setSalesman("商品车物流部(物流)");
list.add(profitCenter);
return list;
}
}

View File

@ -1,13 +1,19 @@
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.example.liteflow.mysql.LiteflowMysqlApplication;
import com.example.liteflow.mysql.enums.MathOperatorEnum;
import com.example.liteflow.mysql.model.role.Match;
import com.example.liteflow.mysql.model.role.Role;
import com.example.liteflow.mysql.model.role.RoleForm;
import com.example.liteflow.mysql.service.BaseRoleService;
import org.assertj.core.util.Lists;
import org.junit.jupiter.api.Test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -18,8 +24,12 @@ import java.util.stream.Collectors;
* @author: 陈韦龙
* @date: 2022年12月07日 16:26
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = LiteflowMysqlApplication.class)
public class MyTest {
@Resource
private BaseRoleService baseRoleService;
@Test
public void test01() {
List<Role> roles = new ArrayList<>();
@ -205,6 +215,104 @@ public class MyTest {
List<? extends Map<String, ?>> collect = roles.stream().map(this::parseRole).collect(Collectors.toList());
System.out.println(JSONUtil.toJsonStr(collect));
}
@Test
public void test7() {
Role role = new Role();
role.setLogicOperators(MathOperatorEnum.or.getOperator());
Role child = new Role();
child.setLogicOperators(MathOperatorEnum.and.getOperator());
List<Match> matches = new ArrayList<>();
Match match = new Match();
match.setConditionalOperators(MathOperatorEnum.contains.getOperator());
match.setMatchingValue("sectionClassification");
match.setMatchingContent("sectionClassificationList");
matches.add(match);
match = new Match();
match.setConditionalOperators(MathOperatorEnum.contains.getOperator());
match.setMatchingValue("businessType");
match.setMatchingContent("businessTypeList");
matches.add(match);
match = new Match();
match.setConditionalOperators(MathOperatorEnum.contains.getOperator());
match.setMatchingValue("salesman");
match.setMatchingContent("salesmanList");
matches.add(match);
match = new Match();
match.setConditionalOperators(MathOperatorEnum.contains.getOperator());
match.setMatchingValue("businessCategory");
match.setMatchingContent("businessCategoryList");
matches.add(match);
child.setMatches(matches);
List<Role> children = Lists.newArrayList(child);
role.setChildren(children);
List<Role> roles = Lists.newArrayList(role);
RoleForm roleForm = new RoleForm();
roleForm.setRoles(roles);
roleForm.setRoleName("costCenter01Cmp");
baseRoleService.createRole(roleForm);
// System.out.println(JSONUtil.toJsonStr(roles));
// List<? extends Map<String, ?>> collect = roles.stream().map(this::parseRole).collect(Collectors.toList());
// System.out.println(JSONUtil.toJsonStr(collect));
}
@Test
public void test8() {
Role role = new Role();
role.setLogicOperators(MathOperatorEnum.or.getOperator());
Role child = new Role();
child.setLogicOperators(MathOperatorEnum.or.getOperator());
Role sun = new Role();
sun.setLogicOperators(MathOperatorEnum.and.getOperator());
List<Match> matches = new ArrayList<>();
Match match = new Match();
match.setConditionalOperators(MathOperatorEnum.contains.getOperator());
match.setMatchingValue("sectionClassification");
match.setMatchingContent("sectionClassificationList");
matches.add(match);
match = new Match();
match.setConditionalOperators(MathOperatorEnum.contains.getOperator());
match.setMatchingValue("salesman");
match.setMatchingContent("salesmanList");
matches.add(match);
sun.setMatches(matches);
List<Role> sunList = Lists.newArrayList(sun);
child.setChildren(sunList);
List<Role> children = Lists.newArrayList(child);
role.setChildren(children);
List<Match> matches2 = new ArrayList<>();
Match match2 = new Match();
match2 = new Match();
match2.setConditionalOperators(MathOperatorEnum.contains.getOperator());
match2.setMatchingValue("businessType");
match2.setMatchingContent("businessTypeList");
matches2.add(match2);
Role role2 = new Role();
role2.setLogicOperators(MathOperatorEnum.or.getOperator());
role2.setMatches(matches2);
List<Role> roles = Lists.newArrayList(role, role2);
RoleForm roleForm = new RoleForm();
roleForm.setRoles(roles);
roleForm.setRoleName("costCenter02Cmp");
baseRoleService.createRole(roleForm);
System.out.println(JSONUtil.toJsonStr(roles));
List<? extends Map<String, ?>> collect = roles.stream().map(this::parseRole).collect(Collectors.toList());
System.out.println(JSONUtil.toJsonStr(collect));
}
private Map<String, Object> parseRole(Role role) {
List<Match> matchList = role.getMatches();
List<Role> roleChildren = role.getChildren();