Search in sources :

Example 1 with DebugRuleRuntimeEventListener

use of org.kie.api.event.rule.DebugRuleRuntimeEventListener in project drools by kiegroup.

the class HelloWorldExample method execute.

public static void execute(KieContainer kc) {
    // From the container, a session is created based on
    // its definition and configuration in the META-INF/kmodule.xml file
    KieSession ksession = kc.newKieSession("HelloWorldKS");
    // Once the session is created, the application can interact with it
    // In this case it is setting a global as defined in the
    // org/drools/examples/helloworld/HelloWorld.drl file
    ksession.setGlobal("list", new ArrayList<Object>());
    // The application can also setup listeners
    ksession.addEventListener(new DebugAgendaEventListener());
    ksession.addEventListener(new DebugRuleRuntimeEventListener());
    // To setup a file based audit logger, uncomment the next line
    // KieRuntimeLogger logger = ks.getLoggers().newFileLogger( ksession, "./helloworld" );
    // To setup a ThreadedFileLogger, so that the audit view reflects events whilst debugging,
    // uncomment the next line
    // KieRuntimeLogger logger = ks.getLoggers().newThreadedFileLogger( ksession, "./helloworld", 1000 );
    // The application can insert facts into the session
    final Message message = new Message();
    message.setMessage("Hello World");
    message.setStatus(Message.HELLO);
    ksession.insert(message);
    // and fire the rules
    ksession.fireAllRules();
    // Remove comment if using logging
    // logger.close();
    // and then dispose the session
    ksession.dispose();
}
Also used : DebugAgendaEventListener(org.kie.api.event.rule.DebugAgendaEventListener) DebugRuleRuntimeEventListener(org.kie.api.event.rule.DebugRuleRuntimeEventListener) KieSession(org.kie.api.runtime.KieSession)

Example 2 with DebugRuleRuntimeEventListener

use of org.kie.api.event.rule.DebugRuleRuntimeEventListener in project drools by kiegroup.

the class DynamicEvalTest method testDynamicAdd.

@Test
public void testDynamicAdd() {
    String test = "\nrule id3" + "\nwhen" + // this eval works
    "\neval(0 < 1)" + "\nthen" + "\ninsertLogical( \"done\" );" + "\nend";
    loadPackages(ResourceFactory.newByteArrayResource(test.getBytes()), ResourceType.DRL);
    ((KnowledgeBaseImpl) session.getKieBase()).addPackages(kbuilder.getKnowledgePackages());
    session.addEventListener(new DebugRuleRuntimeEventListener());
    // 1
    int fired = session.fireAllRules();
    System.out.println(fired);
    effects = session.getObjects();
    assertTrue("fired", effects.contains("done"));
    // so the above works, let's try it again
    String test2 = "\nrule id4" + "\nwhen" + // this eval doesn't
    "\neval(0 == 0 )" + "\nthen" + "\ninsertLogical( \"done2\" );" + "\nend";
    loadPackages(ResourceFactory.newByteArrayResource(test2.getBytes()), ResourceType.DRL);
    ((KnowledgeBaseImpl) session.getKieBase()).addPackages(kbuilder.getKnowledgePackages());
    // 0
    fired = session.fireAllRules();
    System.out.println(fired);
    effects = session.getObjects();
    // fails
    assertTrue("fired", effects.contains("done2"));
}
Also used : DebugRuleRuntimeEventListener(org.kie.api.event.rule.DebugRuleRuntimeEventListener) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) Test(org.junit.Test)

Example 3 with DebugRuleRuntimeEventListener

use of org.kie.api.event.rule.DebugRuleRuntimeEventListener in project drools by kiegroup.

the class DynamicEvalTest method testDynamicAdd2.

@Test
public void testDynamicAdd2() {
    String test = "rule id3\n" + "when\n" + "eval(0 == 0)\n" + // this eval works
    "String( this == \"go\" )\n" + "then\n" + "insertLogical( \"done\" );\n" + "end\n" + "rule id5\n" + "when\n" + "eval(0 == 0)\n" + // this eval works
    "Integer( this == 7 )\n" + "then\n" + "insertLogical( \"done3\" );\n" + "end\n";
    loadPackages(ResourceFactory.newByteArrayResource(test.getBytes()), ResourceType.DRL);
    ((KnowledgeBaseImpl) session.getKieBase()).addPackages(kbuilder.getKnowledgePackages());
    session.addEventListener(new DebugRuleRuntimeEventListener());
    session.insert("go");
    session.insert(5);
    session.insert(7);
    // 1
    int fired = session.fireAllRules();
    System.out.println(fired);
    effects = session.getObjects();
    assertTrue("fired", effects.contains("done"));
    // so the above works, let's try it again
    String test2 = "\nrule id4" + "\nwhen" + // this eval doesn't
    "\neval(0 == 0 )" + "\nInteger( this == 5 )" + "\nthen" + "\ninsertLogical( \"done2\" );" + "\nend";
    loadPackages(ResourceFactory.newByteArrayResource(test2.getBytes()), ResourceType.DRL);
    ((KnowledgeBaseImpl) session.getKieBase()).addPackages(kbuilder.getKnowledgePackages());
    // 0
    fired = session.fireAllRules();
    System.out.println(fired);
    effects = session.getObjects();
    // fails
    assertTrue("fired", effects.contains("done2"));
    for (Object o : session.getObjects()) {
        System.out.println(o);
    }
}
Also used : DebugRuleRuntimeEventListener(org.kie.api.event.rule.DebugRuleRuntimeEventListener) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) Test(org.junit.Test)

Aggregations

DebugRuleRuntimeEventListener (org.kie.api.event.rule.DebugRuleRuntimeEventListener)3 KnowledgeBaseImpl (org.drools.core.impl.KnowledgeBaseImpl)2 Test (org.junit.Test)2 DebugAgendaEventListener (org.kie.api.event.rule.DebugAgendaEventListener)1 KieSession (org.kie.api.runtime.KieSession)1