org.drools.agent
Class KnowledgeAgentFactory

java.lang.Object
  extended by org.drools.agent.KnowledgeAgentFactory

public class KnowledgeAgentFactory
extends Object

The KnowlegeAgent is created by the KnowlegeAgentFactory. The KnowlegeAgent provides automatic loading, caching and re-loading of resources and is configured from a properties files. The KnowledgeAgent can update or rebuild this KnowlegeBase 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.

The Following example constructs an agent that will build a new KnowledgeBase from the files specified in the path String. It will poll those files every 60 seconds, which is the default when polling is enabled and the service started, to see if they are updated. If new files are found it will construct a new KnowledgeBase. If the change set specifies a resource that is a directory it's contents will be scanned for changes too.

 KnowledgeAgent kagent = KnowledgeAgentFactory.newKnowledgeAgent( "MyAgent" );
 kagent.applyChangeSet( ResourceFactory.newUrlResource( url ) );
 KnowledgeBase kbase = kagent.getKnowledgeBase();
 

If you wish to change the polling time of the scanner, this can be done with the ResourceChangeScannerService on the ResourceFactory

 // Set the interval on the ResourceChangeScannerService if the default of 60s is not desirable.
 ResourceChangeScannerConfiguration sconf = ResourceFactory.getResourceChangeScannerService().newResourceChangeScannerConfiguration();
 sconf.setProperty( "drools.resource.scanner.interval",
                    "30" ); // set the disk scanning interval to 30s, default is 60s
 ResourceFactory.getResourceChangeScannerService().configure( sconf );
 

The KnowledgeAgent can accept a configuration that allows for some of the defaults to be changed, see KnowledgeAgentConfiguration for details of all the support configuration properties. An example property is "drools.agent.scanDirectories", by default any specified directories are scanned for new additions, it is possible to disable this.

 KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();

 KnowledgeAgentConfiguration kaconf = KnowledgeAgentFactory.newKnowledgeAgentConfiguration();
 kaconf.setProperty( "drools.agent.scanDirectories",
                     "false" ); // we do not want to scan directories, just files
       
 KnowledgeAgent kagent = KnowledgeAgentFactory.newKnowledgeAgent( "test agent", // the name of the agent
                                                                  kaconf );
 kagent.applyChangeSet( ResourceFactory.newUrlResource( url ) ); // resource to the change-set xml for the resources to add
 

You'll notice the property "drools.agent.newInstance", which defaults to true. This property when true means the KnowledgeBase will be rebuilt as a new instance when changes are detected and that new instance will be available when kagent.getKnowledgeBase() is next called.

KnowledgeAgents can take a empty KnowledgeBase or a populated one. If a populated KnowledgeBase is provided, the KnowledgeAgent will iterate KnowledgeBase and subscribe to the Resource that it finds. While it is possible for the KnowledgeBuilder to build all resources found in a directory, that information is lost by the KnowledgeBuilder so those directories will not be continuously scanned. Only directories specified as part of the applyChangeSet(Resource) method are monitored.

Resource scanning is not on by default, it's a service and must be started, the same is for notification. This can be done via the ResourceFactory.

 ResourceFactory.getResourceChangeNotifierService().start();
 ResourceFactory.getResourceChangeScannerService().start();
 

For resources that are "polled" from a remote source (via http or similar) - sometimes you may want a local file based cache, in case the remote service is not available for whatever reason. To enable this: Set the system property: "drools.resource.urlcache" to a directory which can be written to and read from as a cache - so remote resources will be cached with last known good copies. This will allow the service to be restarted even if the remote source is not available. For example -Ddrools.resource.urlcache=/home/rulecaches

The default implementation of KnowledgeAgent returned by this factory is "org.drools.agent.impl.KnowledgeAgentProviderImpl". You can change it using the system property PROVIDER_CLASS_NAME_PROPERTY_NAME to point to a diverse implementation of "org.drools.agent.KnowledgeAgentProvider".

See Also:
KnowledgeAgent, KnowledgeAgent

Field Summary
static String PROVIDER_CLASS_NAME_PROPERTY_NAME
           
 
Constructor Summary
KnowledgeAgentFactory()
           
 
Method Summary
static KnowledgeAgent newKnowledgeAgent(String name)
           
static KnowledgeAgent newKnowledgeAgent(String name, KnowledgeAgentConfiguration configuration)
           
static KnowledgeAgent newKnowledgeAgent(String name, KnowledgeBase kbase)
           
static KnowledgeAgent newKnowledgeAgent(String name, KnowledgeBase kbase, KnowledgeAgentConfiguration configuration)
           
static KnowledgeAgent newKnowledgeAgent(String name, KnowledgeBase kbase, KnowledgeAgentConfiguration configuration, KnowledgeBuilderConfiguration builderConfiguration)
           
static KnowledgeAgentConfiguration newKnowledgeAgentConfiguration()
           
static KnowledgeAgentConfiguration newKnowledgeAgentConfiguration(Properties properties)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PROVIDER_CLASS_NAME_PROPERTY_NAME

public static final String PROVIDER_CLASS_NAME_PROPERTY_NAME
See Also:
Constant Field Values
Constructor Detail

KnowledgeAgentFactory

public KnowledgeAgentFactory()
Method Detail

newKnowledgeAgentConfiguration

public static KnowledgeAgentConfiguration newKnowledgeAgentConfiguration()

newKnowledgeAgentConfiguration

public static KnowledgeAgentConfiguration newKnowledgeAgentConfiguration(Properties properties)

newKnowledgeAgent

public static KnowledgeAgent newKnowledgeAgent(String name)

newKnowledgeAgent

public static KnowledgeAgent newKnowledgeAgent(String name,
                                               KnowledgeBase kbase)

newKnowledgeAgent

public static KnowledgeAgent newKnowledgeAgent(String name,
                                               KnowledgeAgentConfiguration configuration)

newKnowledgeAgent

public static KnowledgeAgent newKnowledgeAgent(String name,
                                               KnowledgeBase kbase,
                                               KnowledgeAgentConfiguration configuration)

newKnowledgeAgent

public static KnowledgeAgent newKnowledgeAgent(String name,
                                               KnowledgeBase kbase,
                                               KnowledgeAgentConfiguration configuration,
                                               KnowledgeBuilderConfiguration builderConfiguration)


Copyright © 2001-2012 JBoss by Red Hat. All Rights Reserved.