Search in sources :

Example 1 with MatchCreatedEvent

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

the class Misc2Test method testLockOnActive1.

@Test
public void testLockOnActive1() {
    // the modify changes the hashcode of TradeHeader
    // this forces the 'from' to think it's new. This results in an insert and a delete propagation from the 'from'
    // With Property Reactivity enabled by default this also required adding a @watch(*) annotation
    String drl = "" + "package org.drools.test; \n" + "import org.drools.compiler.integrationtests.Misc2Test.TradeBooking;\n" + "import org.drools.compiler.integrationtests.Misc2Test.TradeHeader;\n" + "rule \"Rule1\" \n" + "salience 1 \n" + "when\n" + "  $booking: TradeBooking() @watch(*) \n" + "  $trade: TradeHeader() from $booking.getTrade()\n" + "  not String()\n" + "then\n" + "  $trade.setAction(\"New\");\n" + "  modify($booking) {}\n" + "  insert (\"run\");\n" + "end;\n" + "\n" + "rule \"Rule2\"\n" + "lock-on-active true\n" + "when\n" + "  $booking: TradeBooking( ) @watch(*) \n" + "  $trade: Object( ) from $booking.getTrade()\n" + "then\n" + "end";
    KieBase kb = loadKnowledgeBaseFromString(drl);
    KieSession ks = kb.newKieSession();
    ReteDumper.dumpRete(kb);
    final List created = new ArrayList();
    final List cancelled = new ArrayList();
    final List fired = new ArrayList();
    ks.addEventListener(new DefaultAgendaEventListener() {

        public void matchCreated(MatchCreatedEvent event) {
            created.add(event.getMatch().getRule().getName());
        }

        public void matchCancelled(MatchCancelledEvent event) {
            cancelled.add(event.getMatch().getRule().getName());
        }

        public void afterMatchFired(AfterMatchFiredEvent event) {
            fired.add(event.getMatch().getRule().getName());
        }
    });
    ks.fireAllRules();
    TradeBooking tb = new TradeBookingImpl(new TradeHeaderImpl());
    ks.insert(tb);
    assertEquals(1, ks.fireAllRules());
    assertEquals(3, created.size());
    assertEquals(2, cancelled.size());
    assertEquals(1, fired.size());
    assertEquals("Rule2", created.get(0));
    assertEquals("Rule1", created.get(1));
    assertEquals("Rule2", created.get(2));
    assertEquals("Rule2", cancelled.get(0));
    assertEquals("Rule2", cancelled.get(1));
    assertEquals("Rule1", fired.get(0));
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) MatchCancelledEvent(org.kie.api.event.rule.MatchCancelledEvent) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) List(java.util.List) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Test(org.junit.Test)

Example 2 with MatchCreatedEvent

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

the class ActivateAndDeleteOnListenerTest method testActivateOnMatchAndUpdate.

@Test
public void testActivateOnMatchAndUpdate() throws Exception {
    String str = "";
    str += "package org.drools.compiler.integrationtests \n";
    str += "import " + Alarm.class.getCanonicalName() + " \n";
    str += "import " + Sensor.class.getCanonicalName() + " \n";
    str += "rule StringRule  @Propagation(EAGER) ruleflow-group \"DROOLS_SYSTEM\"\n";
    str += " when \n";
    str += " $a : Alarm() \n";
    str += " $s : Sensor() \n";
    str += " then \n";
    str += "end \n";
    KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(ForceEagerActivationOption.YES);
    KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession(conf, null);
    ksession.addEventListener(new DefaultAgendaEventListener() {

        @Override
        public void matchCreated(MatchCreatedEvent event) {
            Collection<? extends FactHandle> alarms = event.getKieRuntime().getFactHandles(new ClassObjectFilter(Alarm.class));
            for (FactHandle alarm : alarms) {
                event.getKieRuntime().update(alarm, new Alarm());
            }
        }
    });
    // go !
    Alarm alarm = new Alarm();
    alarm.setMessage("test");
    alarm.setNumber(123);
    ksession.insert(alarm);
    Sensor sensor = new Sensor();
    sensor.setPressure(1);
    sensor.setTemperature(25);
    ksession.insert(sensor);
}
Also used : ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) FactHandle(org.kie.api.runtime.rule.FactHandle) Alarm(org.drools.compiler.Alarm) KieHelper(org.kie.internal.utils.KieHelper) DefaultAgendaEventListener(org.drools.core.event.DefaultAgendaEventListener) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Sensor(org.drools.compiler.Sensor) Test(org.junit.Test)

Example 3 with MatchCreatedEvent

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

the class ActivateAndDeleteOnListenerTest method testActivateOnMatchAndDelete.

@Test
public void testActivateOnMatchAndDelete() throws Exception {
    String str = "";
    str += "package org.drools.compiler.integrationtests \n";
    str += "import " + Alarm.class.getCanonicalName() + " \n";
    str += "import " + Sensor.class.getCanonicalName() + " \n";
    str += "rule StringRule  @Propagation(EAGER) ruleflow-group \"DROOLS_SYSTEM\"\n";
    str += " when \n";
    str += " $a : Alarm() \n";
    str += " $s : Sensor() \n";
    str += " then \n";
    str += "end \n";
    KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(ForceEagerActivationOption.YES);
    KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession(conf, null);
    ksession.addEventListener(new DefaultAgendaEventListener() {

        @Override
        public void matchCreated(MatchCreatedEvent event) {
            Collection<? extends FactHandle> alarms = event.getKieRuntime().getFactHandles(new ClassObjectFilter(Alarm.class));
            for (FactHandle alarm : alarms) {
                event.getKieRuntime().delete(alarm);
            }
        }
    });
    // go !
    Alarm alarm = new Alarm();
    alarm.setMessage("test");
    alarm.setNumber(123);
    ksession.insert(alarm);
    Sensor sensor = new Sensor();
    sensor.setPressure(1);
    sensor.setTemperature(25);
    ksession.insert(sensor);
    ksession.fireAllRules();
}
Also used : ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) FactHandle(org.kie.api.runtime.rule.FactHandle) Alarm(org.drools.compiler.Alarm) KieHelper(org.kie.internal.utils.KieHelper) DefaultAgendaEventListener(org.drools.core.event.DefaultAgendaEventListener) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Sensor(org.drools.compiler.Sensor) Test(org.junit.Test)

Example 4 with MatchCreatedEvent

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

the class Misc2Test method testLockOnActive2.

@Test
public void testLockOnActive2() {
    // the modify changes the hashcode of TradeHeader
    // this forces the 'from' to think it's new. This results in an insert and a delete propagation from the 'from'
    // With Property Reactivity enabled by default this also required adding a @watch(*) annotation
    String drl = "" + "package org.drools.test; \n" + "import org.drools.mvel.integrationtests.Misc2Test.TradeBooking;\n" + "import org.drools.mvel.integrationtests.Misc2Test.TradeHeader;\n" + "rule \"Rule1\" \n" + "lock-on-active true\n" + "salience 1 \n" + "when\n" + "  $booking: TradeBooking()\n" + "  $trade: TradeHeader() @watch(*) from $booking.getTrade()\n" + "then\n" + "  $trade.setAction(\"New\");\n" + "  modify($booking) {}\n" + "end;\n" + "\n" + "rule \"Rule2\"\n" + "when\n" + "  $booking: TradeBooking( )\n" + "  $trade: Object( ) @watch(*) from $booking.getTrade()\n" + "then\n" + "end";
    KieBase kb = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
    KieSession ks = kb.newKieSession();
    final List created = new ArrayList();
    final List cancelled = new ArrayList();
    final List fired = new ArrayList();
    ks.addEventListener(new DefaultAgendaEventListener() {

        public void matchCreated(MatchCreatedEvent event) {
            created.add(event.getMatch().getRule().getName());
        }

        public void matchCancelled(MatchCancelledEvent event) {
            cancelled.add(event.getMatch().getRule().getName());
        }

        public void afterMatchFired(AfterMatchFiredEvent event) {
            fired.add(event.getMatch().getRule().getName());
        }
    });
    ks.fireAllRules();
    TradeBooking tb = new TradeBookingImpl(new TradeHeaderImpl());
    ks.insert(tb);
    assertEquals(2, ks.fireAllRules());
    assertEquals(3, created.size());
    assertEquals(1, cancelled.size());
    assertEquals(2, fired.size());
    assertEquals("Rule1", created.get(0));
    assertEquals("Rule1", created.get(1));
    assertEquals("Rule2", created.get(2));
    assertEquals("Rule1", cancelled.get(0));
    assertEquals("Rule1", fired.get(0));
    assertEquals("Rule2", fired.get(1));
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) MatchCancelledEvent(org.kie.api.event.rule.MatchCancelledEvent) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) FactWithList(org.drools.mvel.integrationtests.facts.FactWithList) List(java.util.List) FactWithString(org.drools.mvel.integrationtests.facts.FactWithString) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Test(org.junit.Test)

Example 5 with MatchCreatedEvent

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

the class Misc2Test method testLockOnActive1.

@Test
public void testLockOnActive1() {
    // the modify changes the hashcode of TradeHeader
    // this forces the 'from' to think it's new. This results in an insert and a delete propagation from the 'from'
    // With Property Reactivity enabled by default this also required adding a @watch(*) annotation
    String drl = "" + "package org.drools.test; \n" + "import org.drools.mvel.integrationtests.Misc2Test.TradeBooking;\n" + "import org.drools.mvel.integrationtests.Misc2Test.TradeHeader;\n" + "rule \"Rule1\" \n" + "salience 1 \n" + "when\n" + "  $booking: TradeBooking()\n" + "  $trade: TradeHeader() @watch(*) from $booking.getTrade()\n" + "  not String()\n" + "then\n" + "  $trade.setAction(\"New\");\n" + "  modify($booking) {}\n" + "  insert (\"run\");\n" + "end;\n" + "\n" + "rule \"Rule2\"\n" + "lock-on-active true\n" + "when\n" + "  $booking: TradeBooking( )\n" + "  $trade: Object( ) @watch(*) from $booking.getTrade()\n" + "then\n" + "end";
    KieBase kb = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
    KieSession ks = kb.newKieSession();
    // ReteDumper.dumpRete(kb);
    final List created = new ArrayList();
    final List cancelled = new ArrayList();
    final List fired = new ArrayList();
    ks.addEventListener(new DefaultAgendaEventListener() {

        public void matchCreated(MatchCreatedEvent event) {
            created.add(event.getMatch().getRule().getName());
        }

        public void matchCancelled(MatchCancelledEvent event) {
            cancelled.add(event.getMatch().getRule().getName());
        }

        public void afterMatchFired(AfterMatchFiredEvent event) {
            fired.add(event.getMatch().getRule().getName());
        }
    });
    ks.fireAllRules();
    TradeBooking tb = new TradeBookingImpl(new TradeHeaderImpl());
    ks.insert(tb);
    assertEquals(1, ks.fireAllRules());
    assertEquals(3, created.size());
    assertEquals(2, cancelled.size());
    assertEquals(1, fired.size());
    assertEquals("Rule2", created.get(0));
    assertEquals("Rule1", created.get(1));
    assertEquals("Rule2", created.get(2));
    assertEquals("Rule2", cancelled.get(0));
    assertEquals("Rule2", cancelled.get(1));
    assertEquals("Rule1", fired.get(0));
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) MatchCancelledEvent(org.kie.api.event.rule.MatchCancelledEvent) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) FactWithList(org.drools.mvel.integrationtests.facts.FactWithList) List(java.util.List) FactWithString(org.drools.mvel.integrationtests.facts.FactWithString) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Test(org.junit.Test)

Aggregations

MatchCreatedEvent (org.kie.api.event.rule.MatchCreatedEvent)18 Test (org.junit.Test)16 MatchCancelledEvent (org.kie.api.event.rule.MatchCancelledEvent)14 KieBase (org.kie.api.KieBase)13 KieSession (org.kie.api.runtime.KieSession)13 ArrayList (java.util.ArrayList)11 List (java.util.List)11 AfterMatchFiredEvent (org.kie.api.event.rule.AfterMatchFiredEvent)10 DefaultAgendaEventListener (org.kie.api.event.rule.DefaultAgendaEventListener)9 AgendaEventListener (org.kie.api.event.rule.AgendaEventListener)8 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)8 FactHandle (org.kie.api.runtime.rule.FactHandle)7 BeforeMatchFiredEvent (org.kie.api.event.rule.BeforeMatchFiredEvent)6 RuleFlowGroupDeactivatedEvent (org.kie.api.event.rule.RuleFlowGroupDeactivatedEvent)6 AgendaGroupPoppedEvent (org.kie.api.event.rule.AgendaGroupPoppedEvent)5 AgendaGroupPushedEvent (org.kie.api.event.rule.AgendaGroupPushedEvent)5 RuleFlowGroupActivatedEvent (org.kie.api.event.rule.RuleFlowGroupActivatedEvent)5 Arrays.asList (java.util.Arrays.asList)4 Collection (java.util.Collection)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2