org.kie.api.event.rule.DebugAgendaEventListener Java Examples

The following examples show how to use org.kie.api.event.rule.DebugAgendaEventListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HelloWorldExample.java    From drools-examples with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example #2
Source File: HelloWorldExample.java    From tools-journey with Apache License 2.0 5 votes vote down vote up
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<>());

    // 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();
}
 
Example #3
Source File: SimpleRuleActivator.java    From servicemix with Apache License 2.0 5 votes vote down vote up
@Override
public void start(BundleContext context) throws Exception {
    KieServices ks = KieServices.Factory.get();
    KieContainer kcont = ks.newKieClasspathContainer(getClass()
            .getClassLoader());
    KieBase kbase = kcont.getKieBase("SimpleRuleKBase");

    logger.info("KieSession newKieSession.");
    ksession = kbase.newKieSession();

    ksession.addEventListener(new DebugAgendaEventListener());
    ksession.addEventListener(new DebugRuleRuntimeEventListener());

    Customer customer = customerPoor();

    logger.info("KieSession fireAllRules. {}", customer);
    FactHandle fh = ksession.insert(customer);
    ksession.fireAllRules();
    ksession.delete(fh);
    logger.info("After rule {}", customer);

    customer = customerNormal();
    logger.info("KieSession fireAllRules. {}", customer);
    fh = ksession.insert(customer);
    ksession.fireAllRules();
    ksession.delete(fh);
    logger.info("After rule {}", customer);

    customer = customerVip();
    logger.info("KieSession fireAllRules. {}", customer);
    fh = ksession.insert(customer);
    ksession.fireAllRules();
    ksession.delete(fh);

    logger.info("After rule {}", customer);
}
 
Example #4
Source File: SimpleTest.java    From servicemix with Apache License 2.0 5 votes vote down vote up
/**
 * beforeClass.
 */
@BeforeClass
public static void beforeClass() {
    KieServices ks = KieServices.Factory.get();
    KieContainer kcont = ks.newKieClasspathContainer();
    KieBase kbase = kcont.getKieBase("SimpleRuleKBase");
    ksession = kbase.newKieSession();
    ksession.addEventListener(new DebugAgendaEventListener());
    ksession.addEventListener(new DebugRuleRuntimeEventListener());
}
 
Example #5
Source File: AgendaFilterTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testActivationCancelled() {
    // JBRULES-3376
    String drl = "package org.jboss.qa.brms.agendafilter\n" +
            "declare CancelFact\n" +
            "   cancel : boolean = true\n" +
            "end\n" +
            "rule NoCancel\n" +
            "   ruleflow-group \"rfg\"\n" +
            "   when\n" +
            "       $fact : CancelFact ( cancel == false )\n" +
            "   then\n" +
            "       System.out.println(\"No cancel...\");\n" +
            "       modify ($fact) {\n" +
            "           setCancel(true);\n" +
            "       }\n" +
            "end\n" +
            "rule PresenceOfBothFacts\n" +
            "   ruleflow-group \"rfg\"\n" +
            "   salience -1\n" +
            "   when\n" +
            "       $fact1 : CancelFact( cancel == false )\n" +
            "       $fact2 : CancelFact( cancel == true )\n" +
            "   then\n" +
            "       System.out.println(\"Both facts!\");\n" +
            "end\n" +
            "rule PresenceOfFact\n" +
            "   ruleflow-group \"rfg\"\n" +
            "   when\n" +
            "       $fact : CancelFact( )\n" +
            "   then\n" +
            "       System.out.println(\"We have a \" + ($fact.isCancel() ? \"\" : \"non-\") + \"cancelling fact!\");\n" +
            "end\n" +
            "rule Cancel\n" +
            "   ruleflow-group \"rfg\"\n" +
            "   when\n" +
            "       $fact : CancelFact ( cancel == true )\n" +
            "   then\n" +
            "       System.out.println(\"Cancel!\");\n" +
            "end";

    String rf = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n" +
            "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" +
            "         xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
            "         xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" +
            "         type=\"RuleFlow\" name=\"flow\" id=\"bz761715\" package-name=\"org.jboss.qa.brms.agendafilter\" >\n" +
            "  <header>\n" +
            "  </header>\n" +
            "  <nodes>\n" +
            "    <start id=\"1\" name=\"Start\" x=\"16\" y=\"16\" width=\"48\" height=\"48\" />\n" +
            "    <ruleSet id=\"2\" name=\"Rule\" x=\"208\" y=\"16\" width=\"80\" height=\"48\" ruleFlowGroup=\"rfg\" />\n" +
            "    <actionNode id=\"3\" name=\"Script\" x=\"320\" y=\"16\" width=\"80\" height=\"48\" >\n" +
            "        <action type=\"expression\" dialect=\"java\" >System.out.println(\"Finishing process...\");</action>\n" +
            "    </actionNode>\n" +
            "    <end id=\"4\" name=\"End\" x=\"432\" y=\"16\" width=\"48\" height=\"48\" />\n" +
            "    <actionNode id=\"5\" name=\"Script\" x=\"96\" y=\"16\" width=\"80\" height=\"48\" >\n" +
            "        <action type=\"expression\" dialect=\"java\" >System.out.println(\"Starting process...\");</action>\n" +
            "    </actionNode>\n" +
            "  </nodes>\n" +
            "  <connections>\n" +
            "    <connection from=\"5\" to=\"2\" />\n" +
            "    <connection from=\"2\" to=\"3\" />\n" +
            "    <connection from=\"3\" to=\"4\" />\n" +
            "    <connection from=\"1\" to=\"5\" />\n" +
            "  </connections>\n" +
            "</process>";

    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add( ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL );
    kbuilder.add( ResourceFactory.newByteArrayResource(rf.getBytes()), ResourceType.DRF );

    if ( kbuilder.hasErrors() ) {
        fail( kbuilder.getErrors().toString() );
    }

    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages( kbuilder.getKnowledgePackages() );
    KieSession ksession = kbase.newKieSession();

    ksession.addEventListener(new DebugAgendaEventListener());
    ksession.addEventListener(new DebugProcessEventListener());

    List<Command<?>> commands = new ArrayList<Command<?>>();
    commands.add(CommandFactory.newInsert(newCancelFact(ksession, false)));
    commands.add(CommandFactory.newInsert(newCancelFact(ksession, true)));
    commands.add(CommandFactory.newStartProcess("bz761715"));
    commands.add(new FireAllRulesCommand(new CancelAgendaFilter()));
    commands.add(new FireAllRulesCommand(new CancelAgendaFilter()));
    commands.add(new FireAllRulesCommand(new CancelAgendaFilter()));

    ksession.execute(CommandFactory.newBatchExecution(commands));
}
 
Example #6
Source File: TransactionEventServiceImpl.java    From drools-workshop with Apache License 2.0 4 votes vote down vote up
@PostConstruct
public void after() {
	kSession.registerChannel("auditing", this);
	kSession.addEventListener(new DebugAgendaEventListener(System.out));
	kSession.addEventListener(new DebugRuleRuntimeEventListener(System.out));
}