火力规则:装备类型规则、目标规则实现
This commit is contained in:
@@ -5,14 +5,14 @@ import org.kie.api.KieServices;
|
||||
import org.kie.api.builder.KieBuilder;
|
||||
import org.kie.api.builder.KieFileSystem;
|
||||
import org.kie.api.builder.KieRepository;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.kie.api.builder.Message;
|
||||
import org.kie.api.runtime.KieContainer;
|
||||
import org.kie.api.runtime.KieSession;
|
||||
import org.kie.internal.io.ResourceFactory;
|
||||
import org.kie.spring.KModuleBeanFactoryPostProcessor;
|
||||
import org.kie.spring.annotations.KModuleAnnotationPostProcessor;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
||||
import org.springframework.core.io.support.ResourcePatternResolver;
|
||||
|
||||
@@ -21,48 +21,71 @@ import java.io.IOException;
|
||||
@Configuration
|
||||
public class DroolsConfig {
|
||||
|
||||
//规则文件存放目录
|
||||
private static final String RULE_PATH = "rules/";
|
||||
private static final String RULE_PATH = "classpath*:rules/**/*.drl";
|
||||
|
||||
private final KieServices kieServices = KieServices.Factory.get();
|
||||
|
||||
//扫描规则文件
|
||||
/**
|
||||
* 加载规则文件
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public KieFileSystem kieFileSystem() throws IOException {
|
||||
KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
|
||||
ResourcePatternResolver resourcePatternResolver =
|
||||
new PathMatchingResourcePatternResolver();
|
||||
Resource[] files = resourcePatternResolver.getResources("classpath*:" + RULE_PATH + "*.*");
|
||||
String path = null;
|
||||
for (Resource file : files) {
|
||||
path = RULE_PATH + file.getFilename();
|
||||
kieFileSystem.write(ResourceFactory.newClassPathResource(path, "UTF-8"));
|
||||
|
||||
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
|
||||
Resource[] resources = resolver.getResources(RULE_PATH);
|
||||
|
||||
for (Resource resource : resources) {
|
||||
kieFileSystem.write(
|
||||
ResourceFactory.newInputStreamResource(resource.getInputStream())
|
||||
.setSourcePath("rules/" + resource.getFilename())
|
||||
);
|
||||
}
|
||||
return kieFileSystem;
|
||||
|
||||
return kieFileSystem;
|
||||
}
|
||||
|
||||
//创建KieContainer
|
||||
/**
|
||||
* 构建 KieContainer
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public KieContainer kieContainer () throws IOException {
|
||||
KieRepository kieRepository = kieServices.getRepository();
|
||||
kieRepository.addKieModule(kieRepository::getDefaultReleaseId);
|
||||
KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem());
|
||||
public KieContainer kieContainer(KieFileSystem kieFileSystem) {
|
||||
|
||||
KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
|
||||
kieBuilder.buildAll();
|
||||
return kieServices.newKieContainer(kieRepository.getDefaultReleaseId());
|
||||
|
||||
// 检查错误
|
||||
if (kieBuilder.getResults().hasMessages(Message.Level.ERROR)) {
|
||||
throw new RuntimeException("Drools规则编译错误:\n" +
|
||||
kieBuilder.getResults().toString());
|
||||
}
|
||||
|
||||
KieRepository repository = kieServices.getRepository();
|
||||
|
||||
return kieServices.newKieContainer(repository.getDefaultReleaseId());
|
||||
}
|
||||
|
||||
/**
|
||||
* KieBase
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public KieBase kieBase() throws IOException {
|
||||
return kieContainer().getKieBase();
|
||||
public KieBase kieBase(KieContainer kieContainer) {
|
||||
return kieContainer.getKieBase();
|
||||
}
|
||||
|
||||
/**
|
||||
* KieSession(推荐加)
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public KModuleBeanFactoryPostProcessor kiePostProcessor() throws IOException {
|
||||
return new KModuleAnnotationPostProcessor();
|
||||
public KieSession kieSession(KieContainer kieContainer) {
|
||||
return kieContainer.newKieSession();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 必须是 static
|
||||
*/
|
||||
@Bean
|
||||
public static KModuleBeanFactoryPostProcessor kiePostProcessor() {
|
||||
return new KModuleBeanFactoryPostProcessor();
|
||||
}
|
||||
}
|
||||
@@ -19,5 +19,5 @@ public class RequestDTO {
|
||||
//装备列表
|
||||
private List<Equipments> equipments;
|
||||
|
||||
private List<Tasks> tasks;
|
||||
//private List<Tasks> tasks;
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ public class SubComponents {
|
||||
//设备名称
|
||||
private String deviceName;
|
||||
|
||||
//设备组件参数
|
||||
private List<ComponentParam> componentParams;
|
||||
|
||||
}
|
||||
|
||||
@@ -33,4 +33,10 @@ public class Task {
|
||||
|
||||
//任务目标id
|
||||
private String targetId;
|
||||
|
||||
//作战区经纬度
|
||||
private Coordinate warZoneLocation;
|
||||
|
||||
//防区经纬度
|
||||
private Coordinate defZoneLocation;
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ public class FireRuleStrategyFactory {
|
||||
* @throws IllegalArgumentException 如果编码无效或策略未注册
|
||||
*/
|
||||
public FireRuleStrategy getStrategy(int code) {
|
||||
SceneType type = SceneType.fromCode(code);
|
||||
FireRUleType type = FireRUleType.fromCode(code);
|
||||
FireRuleStrategy strategy = strategyMap.get(type);
|
||||
if (strategy == null) {
|
||||
throw new IllegalArgumentException("未找到任务 " + code + " 对应的策略实现");
|
||||
|
||||
@@ -9,6 +9,9 @@ import org.kie.api.runtime.KieSession;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Component
|
||||
public class BlowStrategy implements FireRuleStrategy {
|
||||
|
||||
@@ -24,6 +27,9 @@ public class BlowStrategy implements FireRuleStrategy {
|
||||
redTask.setTargetId(task.getId());
|
||||
redTask.setSide("红方");
|
||||
|
||||
Map<String, Object> globalParams = new HashMap<>();
|
||||
kieSession.setGlobal("globalParams", globalParams);
|
||||
|
||||
factTask.setRedTask(redTask);
|
||||
factTask.setBlueTask(task);
|
||||
kieSession.insert(factTask);
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user