Search in sources :

Example 1 with DebugAgendaEventListener

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

the class TraitTest method testTraitDonLegacyClassWithoutEmptyConstructor.

@Test(timeout = 10000)
public void testTraitDonLegacyClassWithoutEmptyConstructor() {
    String drl = "package org.drools.compiler.trait.test;\n" + "\n" + "import org.drools.compiler.factmodel.traits.TraitTest.TraitableFoo;\n" + "import org.drools.core.factmodel.traits.Traitable;\n" + "\n" + "" + "declare trait Bar\n" + "end\n" + "\n" + "rule \"Don\"\n" + "no-loop \n" + "when\n" + " $f : TraitableFoo( )\n" + "then\n" + "  Bar b = don( $f, Bar.class );\n" + "end";
    KieSession ksession = getSessionFromString(drl);
    TraitFactory.setMode(mode, ksession.getKieBase());
    ksession.addEventListener(new DebugAgendaEventListener());
    ksession.insert(new TraitableFoo("xx", 0, null));
    ksession.fireAllRules();
    for (Object o : ksession.getObjects()) {
        System.out.println(o);
    }
    assertEquals(2, ksession.getObjects().size());
}
Also used : DebugAgendaEventListener(org.kie.api.event.rule.DebugAgendaEventListener) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 2 with DebugAgendaEventListener

use of org.kie.api.event.rule.DebugAgendaEventListener 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 3 with DebugAgendaEventListener

use of org.kie.api.event.rule.DebugAgendaEventListener in project jbpm by kiegroup.

the class AgendaFilterTest method testActivationCancelled.

@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));
}
Also used : DebugAgendaEventListener(org.kie.api.event.rule.DebugAgendaEventListener) DebugProcessEventListener(org.drools.core.event.DebugProcessEventListener) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) FireAllRulesCommand(org.drools.core.command.runtime.rule.FireAllRulesCommand) Command(org.kie.api.command.Command) FireAllRulesCommand(org.drools.core.command.runtime.rule.FireAllRulesCommand) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 4 with DebugAgendaEventListener

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

the class UseOfRuleFlowGroupPlusLockOnTest method test.

@Test
public void test() {
    KieHelper kieHelper = new KieHelper().addContent(drl, ResourceType.DRL);
    KieBase kbase = kieHelper.build();
    KieSession ksession = kbase.newKieSession();
    ReteDumper.dumpRete(ksession);
    ksession.addEventListener(new DebugAgendaEventListener());
    try {
        ksession.insert(new Person());
        ksession.insert(new Cheese("eidam"));
        ((DefaultAgenda) ksession.getAgenda()).activateRuleFlowGroup("group1");
        int rulesFired = ksession.fireAllRules();
        Assert.assertEquals(1, rulesFired);
    } finally {
        ksession.dispose();
    }
}
Also used : DebugAgendaEventListener(org.kie.api.event.rule.DebugAgendaEventListener) KieBase(org.kie.api.KieBase) DefaultAgenda(org.drools.kiesession.agenda.DefaultAgenda) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 5 with DebugAgendaEventListener

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

the class Misc2Test method testLockOnActiveWithModifyNoEager.

@Test
public void testLockOnActiveWithModifyNoEager() {
    // DROOLS-280
    String drl = "" + "package org.drools.test; \n" + "import org.drools.mvel.compiler.Person; \n" + "" + "rule \"Rule1\" \n" + "salience 1 \n" + "lock-on-active true\n" + "when\n" + "  $p: Person()\n" + "then\n" + "  System.out.println( \"Rule1\" ); \n" + "  modify( $p ) { setAge( 44 ); }\n" + "end;\n" + "\n" + "rule \"Rule2\"\n" + "lock-on-active true\n" + "when\n" + "  $p: Person() \n" + "  String() from $p.getName() \n" + "then\n" + "  System.out.println( \"Rule2\" + $p ); " + "  modify ( $p ) { setName( \"john\" ); } \n" + "end";
    KieBase kb = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
    KieSession ks = kb.newKieSession();
    ks.addEventListener(new DebugAgendaEventListener());
    ks.fireAllRules();
    Person p = new Person("mark", 76);
    ks.insert(p);
    ks.fireAllRules();
    assertEquals(44, p.getAge());
    assertEquals("john", p.getName());
}
Also used : DebugAgendaEventListener(org.kie.api.event.rule.DebugAgendaEventListener) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) FactWithString(org.drools.mvel.integrationtests.facts.FactWithString) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Aggregations

DebugAgendaEventListener (org.kie.api.event.rule.DebugAgendaEventListener)13 KieSession (org.kie.api.runtime.KieSession)12 Test (org.junit.Test)11 KieBase (org.kie.api.KieBase)7 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)6 ArrayList (java.util.ArrayList)2 Person (org.drools.compiler.Person)2 Person (org.drools.mvel.compiler.Person)2 FactWithString (org.drools.mvel.integrationtests.facts.FactWithString)2 DebugRuleRuntimeEventListener (org.kie.api.event.rule.DebugRuleRuntimeEventListener)2 KieHelper (org.kie.internal.utils.KieHelper)2 IOException (java.io.IOException)1 ParseException (java.text.ParseException)1 HashMap (java.util.HashMap)1 CommandBasedStatefulKnowledgeSession (org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession)1 FireAllRulesCommand (org.drools.core.command.runtime.rule.FireAllRulesCommand)1 DebugProcessEventListener (org.drools.core.event.DebugProcessEventListener)1 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)1 DefaultAgenda (org.drools.kiesession.agenda.DefaultAgenda)1 StockTick (org.drools.testcoverage.common.model.StockTick)1