Hi,
I am looking for a way to update drools rules files in our production
environment without having to go through a full application rebuild and
redeploy to pick up .drl modifications.
To make this seamless, I'm investigating using change sets but I can't get a
KnowledgeBase to update when a .drl is updated.
Here is the code I have, all files are in the /root/com/me/drools package:
*1. DroolsConfig.java - Helper class to configure and start a
KnowledgeAgent. It references a local change-set.xml to manage the Drools
resources and scan for resource changes every 2 seconds*
package com.me.drools;
import org.drools.KnowledgeBase;
import org.drools.agent.KnowledgeAgent;
import org.drools.agent.KnowledgeAgentConfiguration;
import org.drools.agent.KnowledgeAgentFactory;
import org.drools.builder.KnowledgeBuilderConfiguration;
import org.drools.io.ResourceChangeScannerConfiguration;
import org.drools.io.ResourceFactory;
public class DroolsConfig {
public KnowledgeBase getAgentKnowledgeBase() {
ResourceFactory.getResourceChangeNotifierService().start();
ResourceFactory.getResourceChangeScannerService().start();
ResourceChangeScannerConfiguration sconf =
ResourceFactory.getResourceChangeScannerService().newResourceChangeScannerConfiguration();
sconf.setProperty("drools.resource.scanner.interval", "2");
ResourceFactory.getResourceChangeScannerService().configure(sconf);
KnowledgeAgentConfiguration aconf =
KnowledgeAgentFactory.newKnowledgeAgentConfiguration();
aconf.setProperty("drools.agent.scanDirectories", "true");
aconf.setProperty("drools.agent.scanResources", "true");
aconf.setProperty("drools.agent.newInstance", "true");
KnowledgeAgent knowledgeAgent =
KnowledgeAgentFactory.newKnowledgeAgent("CS", aconf);
knowledgeAgent.applyChangeSet(ResourceFactory.newFileResource("/root/com/me/drools/change-set.xml"));
return knowledgeAgent.getKnowledgeBase();
}
}
*2. change-set.xml - Referenced from DroolsConfig and manages a local rules
file*
<change-set
xmlns='http://drools.org/drools-5.0/change-set'
xmlns:xs='http://www.w3.org/2001/XMLSchema-instance'
xs:schemaLocation='http://drools.org/drools-5.0/change-set.xsd
http://anonsvn.jboss.org/repos/labs/labs/jbossrules/trunk/drools-api/src/...
<add>
<resource source='file:/root/com/me/drools/personrules.drl'
type='DRL' />
</add>
</change-set>
To test this out I have the following JUnit. The aim is to run the test and
during the loop execution, change the condition in the drl so that the
KnowledgeBase updates and rule results differ.
*3. DroolsConfigTest.java - Tests DroolsConfig. Rule results should change
during loop execution if drl is updated*
package com.me.drools;
import org.drools.KnowledgeBase;
import org.drools.runtime.StatelessKnowledgeSession;
import org.junit.Test;
public class DroolsConfigTest {
@Test
public void testGetAgentKnowledgeBase() throws Exception {
Person person = new Person();
DroolsConfig droolsConfig = new DroolsConfig();
KnowledgeBase kb = droolsConfig.getAgentKnowledgeBase();
for (int i = 0; i < 10; i++) {
StatelessKnowledgeSession session =
kb.newStatelessKnowledgeSession();
session.execute(person);
Thread.currentThread().sleep(3000);
System.out.println(person.getErrors().size());
}
System.out.println("Errors: " + person.getErrors());
}
}
Just for reference here is PersonTO and the person.drl rules file.
*4. Person.java - Simple object to fire rules against*
package com.me.drools;
import java.util.ArrayList;
import java.util.List;
public class Person {
private String name;
private List<String> errors = new ArrayList<String>();
public void addError(final String error) {
errors.add(error);
}
public List<String> getErrors() {
return errors;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
*5. personrules.drl*
package com.me.drools
import com.me.drools.Person
dialect "java"
rule "name_is_null"
when
$p : Person( name == null )
then
$p.addError("Name is null");
end
So, as mentioned when the for loop in DroolsConfigTest is running, I'm
changing the condition in the personrules.drl file from name == null to name
!= null which should mean that errors are no longer added to the errors List
in Person.class but the error count keeps on going up.
Can anybody see why the KnowledgeAgent isn't updating and how I can make
this configuration work so that I can change the drl on the fly and have the
rules behave accordingly?
Thanks
--
View this message in context:
http://drools.46999.n3.nabble.com/Drools-KnowledgeAgent-not-picking-up-dr...
Sent from the Drools: User forum mailing list archive at
Nabble.com.