1

Hello dear programmers,

I try to add a DRL to the KieFilesystem after the KnowledgeBase and the KnowledgeSession is built. But the rules actually won't fire.

Could you guys please try to help me?

Thanks to every solution attempt in advance!

MyCode:

public class Playground {
    private String newline = System.getProperty("line.separator");

    private int MIN_VALUE = -2147483648;

    public static void main(String[] args) {
        Playground pg = new Playground();
        pg.doRules();
    }

    private String generateDRLString() {
        StringBuilder sb;
        sb = new StringBuilder();
        sb.append("package performance.drools;" + newline);
        for (int i = start; i <= end; i++) {
            sb.append("rule \"R" + i + "\"" + newline);
            sb.append("when" + newline);
            sb.append("then" + newline);
            sb.append("System.out.println(\"rule" + i + " fired!\");" + newline);
            sb.append("end" + newline);
        }
        return sb.toString();
    }

    KieServices kieServices;
    KieContainer kieContainer;
    KieBaseConfiguration kieBaseConf;
    KieBase kieBase;
    KieSession kieSession;
    KieFileSystem kfs;
    KieBuilder kieBuilder;
    KieScanner kieScanner;
    int start = MIN_VALUE;
    int end = MIN_VALUE;
    int count = 0;

    private void init() {

        kieServices = KieServices.Factory.get();
        // configure module
        KieModuleModel kieModuleModel = kieServices.newKieModuleModel();
        KieBaseModel kieBaseModel1 = kieModuleModel.newKieBaseModel("KBase1 ")
            .setDefault(true)
            .setEqualsBehavior(EqualityBehaviorOption.EQUALITY)
            .setEventProcessingMode(EventProcessingOption.CLOUD);

        KieSessionModel kieSessionModel1 = kieBaseModel1
            .newKieSessionModel("KSession1").setDefault(true)
            .setType(KieSessionModel.KieSessionType.STATEFUL)
            .setClockType(ClockTypeOption.get("realtime"));
        // initialize module
        kieBaseConf = kieServices.newKieBaseConfiguration();
        kfs = kieServices.newKieFileSystem();
        System.out.println(kieModuleModel.toXML());
        kfs.writeKModuleXML(kieModuleModel.toXML());

        // attach kfs to kieBuilder
        kieBuilder = kieServices.newKieBuilder(kfs).buildAll();
        assertEquals(0, kieBuilder.getResults()
            .getMessages(Message.Level.ERROR).size());

        kieContainer = kieServices.newKieContainer(kieServices.getRepository()
            .getDefaultReleaseId());
        kieBase = kieContainer.newKieBase(kieBaseConf);
        kieSession = kieBase.newKieSession();
        kieScanner = kieServices.newKieScanner(kieContainer);
    }

    private void addRules() {
        kfs.write("src/main/resources/KBase1/ruleSet" + count + ".drl", generateDRLString());
        count++;
        kieBuilder.buildAll();
        assertEquals(0, kieBuilder.getResults()
            .getMessages(Message.Level.ERROR).size());
        kieScanner.scanNow();
    }

    public void doRules() {
        init();

        start = 1;
        end = 3;
        addRules();
        kieSession.insert(new Object());

        start = 10;
        end = 12;
        addRules();
        kieSession.fireAllRules();

        System.out.println(kieSession.getFactCount());
    }
}
Dave G
  • 9,639
  • 36
  • 41
MattRossmann
  • 85
  • 2
  • 7
  • Solved the error message, ki-ci.jar was missing on the classpath. With a class not found error message, I would have solved the issue faster. – MattRossmann Mar 05 '14 at 14:30
  • Matt, I reproduced the error with a full stack trace without a problem, and saw that class-not-found exception. Which environment truncates stack traces - that's evil! – laune Mar 05 '14 at 17:41
  • @laune: I think you you didn't include some classes correctly, as I saw on Drools forum you do not use Maven. Give it a try ;-) You could have a ready to use POM.xml, if it helps. – MattRossmann Mar 05 '14 at 18:09
  • OK, thanks, but maven is just a tool to simplify project development. There has to be a way to do what has to be done without maven and internet access. – laune Mar 05 '14 at 18:32
  • SOLVED: core aspects of the question answered here http://stackoverflow.com/questions/22205945/drools-6-add-rules-to-a-running-kiesession/22208775?noredirect=1#22208775 – MattRossmann Mar 05 '14 at 21:06
  • the ki-ci.jar is actually missing from the 6.0.0Final distribution which I used :-{ – laune Mar 06 '14 at 11:39

0 Answers0