Reputation: 43
I integrated Drools with my Java Spring application via Maven dependencies. I created KieContainer bean:
@Bean
public KieContainer kieContainer (){
KieServices kieServices = KieServices.Factory.get();
KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
File dir = new File("src/main/resources/rules");
File[] directoryListing = dir.listFiles();
if (directoryListing != null) {
for (File child : directoryListing) {
kieFileSystem.write(child.getName(), "UTF-8");
}
}
KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
kieBuilder.buildAll();
KieModule kieModule = kieBuilder.getKieModule();
return kieServices.newKieContainer(kieModule.getReleaseId());
}
When I debug this, I can see that both my .drl files under /src/main/resources/rules location have been added successfully!
Furthermore, I've created KieSession:
public String executeRule(FieldCDO field, ContractCDO contract, FieldValueCDO fieldValue){
KieSession kieSession = kieContainer.newKieSession();
kieSession.setGlobal("fieldValue", fieldValue);
kieSession.insert(field);
kieSession.fireAllRules();
kieSession.dispose();}
Then, I created jUnit test from which I called method from above executeRule(), and passed objects FieldCDO, ContractCDO, FieldValueCDO.
My Rules look like this. Rule1.drl:
import com.calliduscloud.clma.domain.FieldCDO;
import com.calliduscloud.clma.domain.ContractCDO;
import com.calliduscloud.clma.domain.FieldValueCDO;
global com.calliduscloud.clma.domain.FieldValueCDO fieldValue;
dialect "mvel"
rule "Change contract field value 1"
when
field: FieldCDO(id == 1)
contract: ContractCDO(id == 1);
then
fieldValue.setFieldValue("11111");
end
Rule2.drl:
import com.calliduscloud.clma.domain.FieldCDO;
import com.calliduscloud.clma.domain.ContractCDO;
import com.calliduscloud.clma.domain.FieldValueCDO;
global com.calliduscloud.clma.domain.FieldValueCDO fieldValue;
dialect "mvel"
rule "Change contract field value 2"
when
field: FieldCDO(id == 1)
contract: ContractCDO(id == 1);
then
fieldValue.setFieldValue("22222");
end
But, when I call executeRule() method, it breaks at the line where I add global variable into the KieSession:
kieSession.setGlobal("fieldValue", fieldValue);
The error is:
java.lang.RuntimeException: Unexpected global [fieldValue]
When I execute just one rule by calling the same code, it all passes without the problems. But when I have these two rules in chain, this problem occurs.
If someone has some idea, please write if you have time.
Thanks, Dejan
Upvotes: 2
Views: 4150
Reputation: 11
Your process of loading drool files in to KieFileSystem
is wrong.
Change it to the following to get it working.
@Bean
public KieContainer kieContainer() {
KieServices kieServices = KieServices.Factory.get();
KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
File dir = new File("src/main/resources/rules");
File[] directoryListing = dir.listFiles();
if (directoryListing != null) {
for (File child : directoryListing) {
kieFileSystem.write(ResourceFactory.newFileResource(child));
}
}
KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
kieBuilder.buildAll();
KieModule kieModule = kieBuilder.getKieModule();
return kieServices.newKieContainer(kieModule.getReleaseId());
}
Upvotes: 1
Reputation: 4133
The problem might be related to define the global in both DRL files. Notice that the KieBuilder will load both DRL files to construct the KieBase. I would try to put the global and the import in a separate DRL file to make sure that it get loaded first. If you create a reproducer and put it in GitHub I can give it a try.
Upvotes: 0