cppcoder
cppcoder

Reputation: 23095

Maintain separate rule files in drools

I have a use-case where I need to maintain separate drl file for separate entities.
For example,
entity 1 has a set of facts which should be driven by drl1
entity 2 has a set of facts which should be driven by drl2

How should I implement this?

  1. Load the drl's and corresponding facts in separate Knowledge base and sessions
  2. Load all drl's and facts in single session.

I do not know how to achieve 2

Upvotes: 0

Views: 2633

Answers (1)

laune
laune

Reputation: 31290

If you build your Knowledge Base along the lines given below, you can repeat adding another FileInputStream to the KieFileSystem.

KieServices kieServices = KieServices.Factory.get();
KieFileSystem kfs = kieServices.newKieFileSystem();
// repeat
FileInputStream fis = new FileInputStream( "simple/simple.drl" );
kfs.write( "src/main/resources/simple.drl",
                kieServices.getResources().newInputStreamResource( fis ) );
// end
KieBuilder kieBuilder = kieServices.newKieBuilder( kfs ).buildAll();

Results results = kieBuilder.getResults();
if( results.hasMessages( Message.Level.ERROR ) ){
    System.out.println( results.getMessages() );
    throw new IllegalStateException( "### errors ###" );
}
KieContainer kieContainer =
      kieServices.newKieContainer( kieServices.getRepository().getDefaultReleaseId() );
KieBase kieBase = kieContainer.getKieBase();
kieSession = kieContainer.newKieSession();

Upvotes: 1

Related Questions