Core Engine :: KieBase :: KnowledgeBuilder

 

org.kie.internal.builder.KnowledgeBuilder

The KnowledgeBuilder is responsible for taking source files, such as a .drl file, a .bpmn2 file or an .xls file, and turning them into a KnowledgePackage of rule and process definitions which a KnowledgeBase can consume. It uses the ResourceType enum to tell it the type of the resource it is being asked to build.

The ResourceFactory

provides capabilities to load Resources from a number of sources; such as Reader, ClassPath, URL, File, ByteArray. Binaries, such as XLS decision tables, should not use a Reader based Resource handler, which is only suitable for text based resources.

It is best practice to always check the hasErrors() method after an addition, you should not add more resources or get the KnowledgePackages if there are errors. getKnowledgePackages() will return an empty list if there are errors.

You can create a new KnowledgeBase for all the resources that were added using this builder using the newKnowledgeBase() method. This will throw an exception if there are errors for any of the resources.

Simple example showing how to build a KnowledgeBase from an DRL rule resource.

Simple example showing how to build a KnowledgeBase from an XLS decision table resource.

Simple example showing how to build a KnowledgeBase from an BPMN2 process resource.

If there are errors a simple toString can print the errors

The KnowledgeBuilder can also be built from a configuration using the XML change-set format and the ResourceType.CHANGE_SET value. While change-set supports add, remove, and modify as elements. KnowledgeBuilder will only process add. If the resource element provided points to a directory, all files found in that directory will be added. Currently the knowledge type is not derived from the file extension and must be explicitly set in the XML for the resource. It is expected that all
resources in a directory given resource are of the specified type.

 

org.kie.internal.builder.KnowledgeBuilderConfiguration

This class configures the knowledge package builder and compiler. Dialects and their DialectConfigurations are handled by the DialectRegistry Normally you will not need to look at this class, unless you want to override the defaults.

This class will automatically load default values from a number of places, accumulating properties from each location.
This list of locations, in given priority is: System properties, home directory, working directory, META-INF/ of optionally provided classLoader META-INF/ of Thread.currentThread().getContextClassLoader() and META-INF/ of ClassLoader.getSystemClassLoader()

So if you want to set a default configuration value for all your new KnowledgeBuilder, you can simply set the property as
a System property.

This class is not thread safe and it also contains state. After the KnowledgeBuilder is created, it makes the configuration
immutable and there is no way to make it mutable again. This is to avoid inconsistent behaviour inside KnowledgeBuilder.

  • drools.dialect.default = <String>
  • drools.accumulate.function.<function name> = <qualified class>
  • drools.evaluator.<ident> = <qualified class>
  • drools.dump.dir = <String>
  • drools.parser.processStringEscapes = <true|false>

Two dialects are supported, Java and MVEL. Java is the default dialect.
The Java dialect supports the following configurations:

  • drools.dialect.java.compiler = <ECLIPSE|JANINO>
  • drools.dialect.java.compiler.lnglevel = <1.5|1.6>

And MVEL supports the following configurations:

  • drools.dialect.mvel.strict = <true|false>

So for example if we wanted to create a new KnowledgeBuilder that used Janino as the default compiler we would do the following:

Remember the KnowledgeBuilderConfiguration could have taken a Properties instance with that setting in it at constructor time, or it could also discover from a disk based properties file too.

Available pre-configured Accumulate functions are:

  • drools.accumulate.function.average = org.kie.base.accumulators.AverageAccumulateFunction
  • drools.accumulate.function.max = org.kie.base.accumulators.MaxAccumulateFunction
  • drools.accumulate.function.min = org.kie.base.accumulators.MinAccumulateFunction
  • drools.accumulate.function.count = org.kie.base.accumulators.CountAccumulateFunction
  • drools.accumulate.function.sum = org.kie.base.accumulators.SumAccumulateFunction
  • drools.accumulate.function.collectSet = org.kie.base.accumulators.CollectSetAccumulateFunction
  • drools.accumulate.function.collectList = org.kie.base.accumulators.CollectListAccumulateFunction

 

org.kie.internal.builder.KnowledgeBuilderFactory

This factory is used to build the knowledge base resources that are held collectively in KnowledgePackages. The KnowledgePackage also provides the role of ‘namespacing’. An optional KnowlegeBuilderConfiguration can be supplied. The KnowledgeBuilderConfiguration is itself created from this factory. The KnowledgeBuilderConfiguration allows you to set the ClassLoader to be used along with other setting like the default dialect and compiler, as well as many other options.

 

Leave a Reply