1.第一步设置DROOLS加载路径
public enum DroolsEnum {
DROOLS_ENUM2_0("drools1_1","rules/drools1_1/"),
DROOLS_ENUM1_1("drools2_0","rules/drools2_0/"),
DROOLS_ENUM_OLD("drools_old","rules/drools_old/");
public static final String DROOLS1_1 = "drools1_1";
public static final String DROOLS2_0 = "drools2_0";
public static final String DROOLS_OLD = "drools_old";
private String name; //drools鍚嶇О
private String rulesPath;//drools璺緞
DroolsEnum(String name,String rulesPath ){
this.name=name;
this.rulesPath=rulesPath;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRulesPath() {
return rulesPath;
}
public void setRulesPath(String rulesPath) {
this.rulesPath = rulesPath;
}
}
2.第二步加载DROOLS
package com.shangyong.configure;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.KieModule;
import org.kie.api.builder.KieRepository;
import org.kie.api.builder.ReleaseId;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.internal.io.ResourceFactory;
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;
import com.shangyong.enums.drools.DroolsEnum;
@Configuration
public class DroolsAutoConfiguration {
@Bean
public Map<String,KieSession> getMapKieSession() throws IOException{
Map<String,KieSession> kieSessionMap=new HashMap<String,KieSession>();
for(DroolsEnum path : DroolsEnum.values()){
KieServices kieServices = KieServices.Factory.get();
KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
Resource[] resource=resourcePatternResolver.getResources("classpath*:" +path.getRulesPath()+ "**/*.*");
for (Resource file : resource) {
kieFileSystem.write(ResourceFactory.newClassPathResource(path.getRulesPath() + file.getFilename(), "UTF-8"));
}
final KieRepository kieRepository = kieServices.getRepository();
kieRepository.addKieModule(new KieModule() {
public ReleaseId getReleaseId() {
return kieRepository.getDefaultReleaseId();
}
});
KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
kieBuilder.buildAll();
KieContainer kieContainer=kieServices.newKieContainer(kieRepository.getDefaultReleaseId());
KieSession kieSession = kieContainer.newKieSession();
kieSessionMap.put(path.getName(), kieSession);
}
return kieSessionMap;
}
}
3.设置DROOLS工具类
package com.shangyong.util;
import java.math.BigDecimal;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import com.shangyong.entity.drools.OldUserRuleEngine;;
public class DroolsUtil {
public synchronized static void executeRule(KieSession kieSession,OldUserRuleEngine oldUserRuleEngine) {
if (oldUserRuleEngine != null) {
oldUserRuleEngine.setScore(BigDecimal.valueOf(0));
kieSession.insert(oldUserRuleEngine);
int count = kieSession.fireAllRules();
}
}
}
4.注入和使用Drools
@Autowired
Map<String, KieSession> kieSessionMap;
DroolsUtil.executeRule(kieSessionMap.get(DroolsEnum.DROOLS_OLD),oldUserRuleEngine);
如有疑问请加我qq:826544809
支付宝扫一扫天天领红包