问题
I'm very new to Drools and adapted the Spring Boot configuration i found here to enable me to read a bunch of rules from a Database as String's instead of reading them from DRL Files in an application folder.
When I startup my application using the code below i'm get the following exception using Spring Boot, Drools 6.5.Final, Java 1.8:
Caused by: java.lang.RuntimeException: Resource does not have neither a source nor a target path. Impossible to add it to the bundle. Please set either the source or target name of the resource before adding it.null at org.drools.compiler.kie.builder.impl.KieFileSystemImpl.write(KieFileSystemImpl.java:95) ~[drools-compiler-6.5.0.Final.jar:6.5.0.Final]
package com.example.demo;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import org.kie.api.KieBase;
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.io.Resource;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.internal.io.ResourceFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class DroolsDatabaseConfig {
@Autowired
private DataService dataService;
@Bean
public KieFileSystem kieFileSystem() throws IOException {
KieFileSystem kieFileSystem = getKieServices().newKieFileSystem();
for (Resource resource : getRulesFromDB()) {
kieFileSystem.write(resource);
}
return kieFileSystem;
}
private List<Resource> getRulesFromDB() throws IOException {
List<Resource> resources = new ArrayList<Resource>();
List<String> rules = dataService.getRulesFromDB();
for (String rule : rules){
Resource r = ResourceFactory.newInputStreamResource(new ByteArrayInputStream(rule.getBytes(StandardCharsets.UTF_8)));
resources.add(r);
}
return resources;
}
@Bean
public KieContainer kieContainer() throws IOException {
final KieRepository kieRepository = getKieServices().getRepository();
kieRepository.addKieModule(new KieModule() {
public ReleaseId getReleaseId() {
return kieRepository.getDefaultReleaseId();
}
});
KieBuilder kieBuilder = getKieServices().newKieBuilder(kieFileSystem());
kieBuilder.buildAll();
return getKieServices().newKieContainer(kieRepository.getDefaultReleaseId());
}
private KieServices getKieServices() {
return KieServices.Factory.get();
}
@Bean
public KieBase kieBase() throws IOException {
return kieContainer().getKieBase();
}
@Bean
public KieSession kieSession() throws IOException {
return kieContainer().newKieSession();
}
}
Any ideas on how i can resolve this to allow me load the Rules from a String?
Thanks in advance
来源:https://stackoverflow.com/questions/45265687/drools-resource-does-not-have-neither-a-source-nor-a-target-path