Reputation: 251
I'm working on a project where we process different files and for each different file, we need to run some specific rules which we fetch from database. Now, if we compile the rules for each row of the file, it takes lot of time and hence the performance issues. So, what we did, we are compiling all the rules on the startup of my application and create the kieContainer. However, I don't know how to execute file specific rules based on my file id. Can you please help?
For example, I have 2 files with id 1 and 2. I have total 10 rules in which 5 are for file 1 and rest 5 are for file 2.
With the below code, it fires all 10 rules on both the files.
Here is my sample code:
@Bean
public KieContainer kieContainer() throws IOException {
KieServices kieServices = KieServices.Factory.get();
KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
// To get rules from DB
List<String> rules = loadRules();
int i=0;
for (String file : rules) {
kieFileSystem.write("src/main/resources/rules/" + "a"+i+".drl", kieServices.getResources().newReaderResource(new StringReader(file)));
i++;
}
KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
kieBuilder.buildAll();
KieModule kieModule = kieBuilder.getKieModule();
return kieServices.newKieContainer(kieModule.getReleaseId());
}
//When I execute rules
KieSession kieSession = kieContainer.newKieSession();
kieSession.insert(myObject);
kieSession.fireAllRules();
kieSession.dispose();
Upvotes: 1
Views: 3681
Reputation: 31300
Subdivide your rules into agenda-groups:
agenda-group "a" // or add this to each rule a1, a2,...
rule a1 ...
rule a2 ...
agenda-group "b"
rule b1 ...
rule b2 ...
Before you call fireAllRules you need to decide which group to use and set the focus:
String group = ...;
kieSession.getAgenda().getAgendaGroup( group ).setFocus();
kieSession.insert(myObject);
kieSession.fireAllRules();
Upvotes: 1