So far, the programmatic API has been used to build a
Knowledge Base. Quite often it's more desirable to do this via
configuration. To facilitate this, Drools supports the "Changeset"
feature. The file changeset.xml
contains a
list of resources, and it
may also point recursively to another changeset XML file. Currently the
changeset has only a single "add" element, but support for remove
and modify will be added in the future, for more powerful incremental
changes over time. Currently there is no XML schema for the changeset
XML, but we hope to add one soon. A few examples will be
shown to give you the gist of things. A resource approach is employed
that uses a prefix to indicate the protocol. All the protocols provided
by java.net.URL
, such as "file" and "http", are supported, as well as an
additional "classpath". Currently the type attribute must always be
specified for a resource, as it is not inferred from the file name extension.
Here is a simple example that points to a http location for some rules.
<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 drools-change-set-5.0.xsd' > <add> <resource source='http:org/domain/myrules.drl' type='DRL' /> </add> </change-set>
To use the above XML, the code is almost identical as before,
except we change the resource type to CHANGE_SET
.
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newClasspathResource( "myChangeSet.xml", getClass() ), ResourceType.CHANGE_SET ); if ( kbuilder.hasErrors() ) { System.err.println( builder.getErrors().toString() ); }
Changesets can include any number of resources, and they even support additional configuration information, which currently is only needed for decision tables. The example below is expanded to load the rules from a http URL location, and an Excel decision table from the classpath.
<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' > <add> <resource source='http:org/domain/myrules.drl' type='DRL' /> <resource source='classpath:data/IntegrationExampleTest.xls' type="DTABLE"> <decisiontable-conf input-type="XLS" worksheet-name="Tables_2" /> </resource> </add> </change-set>
It is also possible to specify a directory, to add the contents of that directory. It is expected that all the files are of the specified type, since type is not yet inferred from the file name extensions.
<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' > <add> <resource source='file://myfolder/' type='DRL' /> </add> </change-set>
The Knowlege Agent provides automatic loading, caching and re-loading of resources and is configured from a properties files. The Knowledge Agent can update or rebuild this Knowlege Base as the resources it uses are changed. The strategy for this is determined by the configuration given to the factory, but it is typically pull-based using regular polling. We hope to add push-based updates and rebuilds in future versions.
KnowledgeAgent kagent = KnowledgeAgentFactory.newKnowledgeAgent( "MyAgent" ); kagent.applyChangeSet( ResourceFactory.newUrlResource( url ) ); KnowledgeBase kbase = kagent.getKnowledgeBase();
A KnowledgeAgent
object will continuously scan all the
added resources, using a default polling interval of 60 seconds and, when
some last modification date is updated, it will rebuild the cached
Knowledge Base using the new resources. Note that the previous
KnowledgeBase
reference will still exist
and you'll have to call getKnowledgeBase()
to access the newly built
KnowledgeBase
. If a directory is specified as part of the change set,
the entire contents of that directory will be scanned for
changes.