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