Interface KnowledgeBuilder

All Superinterfaces:
ProcessBuilder, RuleBuilder

public interface KnowledgeBuilder extends RuleBuilder, ProcessBuilder

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.

 KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
 kbuilder.add( ResourceFactory.newUrlResource( "file://myrules.drl" ),
                       ResourceType.DRL);
 assertFalse( kbuilder.hasErrors() );
 KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
 

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

 KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
 DecisionTableConfiguration dtconf = KnowledgeBuilderFactory.newDecisionTableConfiguration();
 dtconf.setInputType( DecisionTableInputType.XLS );
 dtconf.setWorksheetName( "Tables_2" );
 kbuilder.add( ResourceFactory.newUrlResource( "file://IntegrationExampleTest.xls" ),
               ResourceType.DTABLE,
                 dtconf );
 assertFalse( kbuilder.hasErrors() );
 KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
 

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

 KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
 kbuilder.add( ResourceFactory.newUrlResource( "file://myProcess.bpmn2" ),
               ResourceType.BPMN2 );
 KnowledgeBase kbase = kbuilder.newKnowledgeBase();
 

If there are errors a simple toString can print the errors

 if ( kbuilder.hasErrors() ) {
     log.exception( kbuilder.getErrors().toString() )
 }
 
  • Method Details

    • add

      void add(Resource resource, ResourceType type)
      Add a resource of the given ResourceType, using the default resource configuration.
      Parameters:
      resource - the Resource to add
      type - the resource type
    • add

      void add(Resource resource, ResourceType type, ResourceConfiguration configuration)
      Add a resource of the given ResourceType, using the provided ResourceConfiguration. Resources can be created by calling any of the "newX" factory methods of ResourceFactory. The kind of resource (DRL, XDRL, DSL,... ) must be indicated by the second argument.
      Parameters:
      resource - the Resource to add
      type - the resource type
      configuration - the resource configuration
    • getKnowledgePackages

      Collection<KiePackage> getKnowledgePackages()
      Returns the built packages. If the KnowledgeBuilder has errors the Collection will be empty. The hasErrors() method should always be checked first, to make sure you are getting the packages that you wanted built.
      Returns:
      The Collection of KnowledgePackages
    • newKieBase

      KieBase newKieBase()
      Creates a new KnowledgeBase from the knowledge packages that have been added to this builder. An exception is thrown if there are any errors.
    • hasErrors

      boolean hasErrors()
      If errors occurred during the build process they are added here
      Returns:
    • getErrors

      Return errors that occurred during the build process.
      Returns:
    • getResults

      KnowledgeBuilderResults getResults(ResultSeverity... severities)
      Return the knowledge builder results for the listed severities.
      Parameters:
      severities -
      Returns:
    • hasResults

      boolean hasResults(ResultSeverity... severities)
      Checks if the builder generated any results of the listed severities
      Parameters:
      severities -
      Returns:
    • undo

      void undo()
      Remove the last added Resource. Can be useful in case this last addition generated some compilation problem. If multiple Resources have been added in batch using a CompositeKnowledgeBuilder, it removes all of them.
    • batch

      Return a CompositeKnowledgeBuilder allowing to add multiple Resources at the same time, without worrying about cross dependencies among them.
      Returns: