Search in sources :

Example 11 with Match

use of org.kie.api.runtime.rule.Match in project drools by kiegroup.

the class DeclarativeAgendaTest method testActiveInActiveChanges.

@Test(timeout = 10000)
public void testActiveInActiveChanges() {
    String str = "";
    str += "package org.domain.test \n";
    str += "import " + Match.class.getName() + "\n";
    str += "global java.util.List list \n";
    str += "dialect 'mvel' \n";
    str += "rule rule1 @department(sales) \n";
    str += "when \n";
    str += "     $s : String( this == 'go1' ) \n";
    str += "then \n";
    str += "    list.add( kcontext.rule.name + ':' + $s ); \n";
    str += "end \n";
    str += "rule rule2 @department(sales) \n";
    str += "when \n";
    str += "     $s : String( this == 'go1' ) \n";
    str += "then \n";
    str += "    list.add( kcontext.rule.name + ':' + $s ); \n";
    str += "end \n";
    str += "rule rule3 @department(sales) \n";
    str += "when \n";
    str += "     $s : String( this == 'go1' ) \n";
    str += "then \n";
    str += "    list.add( kcontext.rule.name + ':' + $s ); \n";
    str += "end \n";
    str += "rule countActivateInActive @activationListener('direct') \n";
    str += "when \n";
    str += "     $s : String( this == 'go2' ) \n";
    str += "     $active : Number( this == 1 ) from accumulate( $a : Match( department == 'sales', active == true ), count( $a ) )\n";
    str += "     $inActive : Number( this == 2 ) from  accumulate( $a : Match( department == 'sales', active == false ), count( $a ) )\n";
    str += "then \n";
    str += "    list.add( $active + ':' + $inActive  ); \n";
    str += "    kcontext.halt( ); \n";
    str += "end \n";
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(DeclarativeAgendaOption.ENABLED);
    KieBase kbase = loadKnowledgeBaseFromString(kconf, str);
    KieSession ksession = createKnowledgeSession(kbase);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.insert("go1");
    FactHandle go2 = ksession.insert("go2");
    ksession.fireAllRules();
    assertEquals(3, list.size());
    System.out.println(list);
    assertTrue(list.contains("1:2"));
    assertTrue(list.contains("rule1:go1"));
    assertTrue(list.contains("rule2:go1"));
    ksession.dispose();
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Match(org.kie.api.runtime.rule.Match) Test(org.junit.Test)

Example 12 with Match

use of org.kie.api.runtime.rule.Match in project drools by kiegroup.

the class DeclarativeAgendaTest method testMultipleBlockersWithUnblockAll.

@Test(timeout = 10000)
public void testMultipleBlockersWithUnblockAll() {
    // This test is a bit wierd as it recurses. Maybe unblockAll is not feasible...
    String str = "";
    str += "package org.domain.test \n";
    str += "import " + Match.class.getName() + "\n";
    str += "global java.util.List list \n";
    str += "dialect 'mvel' \n";
    str += "rule rule0 @Propagation(EAGER) @department(sales) \n";
    str += "when \n";
    str += "     $s : String( this == 'go0' ) \n";
    str += "then \n";
    str += "    list.add( kcontext.rule.name + ':' + $s ); \n";
    str += "end \n";
    str += "rule blockerAllSalesRules1 @activationListener('direct') \n";
    str += "when \n";
    str += "     $s : String( this == 'go1' ) \n";
    str += "     $i : Match( department == 'sales' ) \n";
    str += "then \n";
    str += "    list.add( kcontext.rule.name + ':' + $i.rule.name + ':' + $s  ); \n";
    str += "    kcontext.blockMatch( $i ); \n";
    str += "end \n";
    str += "rule blockerAllSalesRules2 @activationListener('direct') \n";
    str += "when \n";
    str += "     $s : String( this == 'go2' ) \n";
    str += "     $i : Match( department == 'sales' ) \n";
    str += "then \n";
    str += "    list.add( kcontext.rule.name + ':' + $i.rule.name + ':' + $s  ); \n";
    str += "    kcontext.blockMatch( $i ); \n";
    str += "end \n";
    str += "rule blockerAllSalesRules3 @activationListener('direct') \n";
    str += "when \n";
    str += "     $s : String( this == 'go3' ) \n";
    str += "     $i : Match( department == 'sales' ) \n";
    str += "then \n";
    str += "    list.add( kcontext.rule.name + ':' + $i.rule.name + ':' + $s  ); \n";
    str += "    kcontext.blockMatch( $i ); \n";
    str += "end \n";
    str += "rule unblockAll @activationListener('direct') \n";
    str += "when \n";
    str += "     $s : String( this == 'go4' ) \n";
    str += "     $i : Match( department == 'sales', active == true ) \n";
    str += "then \n";
    str += "    list.add( kcontext.rule.name + ':' + $i.rule.name + ':' + $s  ); \n";
    str += "    kcontext.unblockAllMatches( $i ); \n";
    str += "end \n";
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(DeclarativeAgendaOption.ENABLED);
    KieBase kbase = loadKnowledgeBaseFromString(kconf, str);
    KieSession ksession = createKnowledgeSession(kbase);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    FactHandle go0 = ksession.insert("go0");
    FactHandle go1 = ksession.insert("go1");
    FactHandle go2 = ksession.insert("go2");
    FactHandle go3 = ksession.insert("go3");
    ksession.fireAllRules();
    assertEquals(3, list.size());
    System.out.println(list);
    assertTrue(list.contains("blockerAllSalesRules1:rule0:go1"));
    assertTrue(list.contains("blockerAllSalesRules2:rule0:go2"));
    assertTrue(list.contains("blockerAllSalesRules3:rule0:go3"));
    list.clear();
    FactHandle go4 = ksession.insert("go4");
    ksession.fireAllRules();
    System.out.println(list);
    assertEquals(5, list.size());
    assertTrue(list.contains("unblockAll:rule0:go4"));
    assertTrue(list.contains("rule0:go0"));
    assertTrue(list.contains("blockerAllSalesRules1:rule0:go1"));
    assertTrue(list.contains("blockerAllSalesRules2:rule0:go2"));
    assertTrue(list.contains("blockerAllSalesRules3:rule0:go3"));
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Match(org.kie.api.runtime.rule.Match) Test(org.junit.Test)

Example 13 with Match

use of org.kie.api.runtime.rule.Match in project drools by kiegroup.

the class AccumulateTest method execTestAccumulateMultipleFunctions.

public void execTestAccumulateMultipleFunctions(String fileName) throws Exception {
    KieSession ksession = getKieSessionFromResources(fileName);
    AgendaEventListener ael = mock(AgendaEventListener.class);
    ksession.addEventListener(ael);
    final Cheese[] cheese = new Cheese[] { new Cheese("stilton", 10), new Cheese("stilton", 3), new Cheese("stilton", 5), new Cheese("brie", 15), new Cheese("brie", 17), new Cheese("provolone", 8) };
    final Person bob = new Person("Bob", "stilton");
    final FactHandle[] cheeseHandles = new FactHandle[cheese.length];
    for (int i = 0; i < cheese.length; i++) {
        cheeseHandles[i] = (FactHandle) ksession.insert(cheese[i]);
    }
    final FactHandle bobHandle = (FactHandle) ksession.insert(bob);
    // ---------------- 1st scenario
    ksession.fireAllRules();
    ArgumentCaptor<AfterMatchFiredEvent> cap = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
    Mockito.verify(ael).afterMatchFired(cap.capture());
    Match activation = cap.getValue().getMatch();
    assertThat(((Number) activation.getDeclarationValue("$sum")).intValue(), is(18));
    assertThat(((Number) activation.getDeclarationValue("$min")).intValue(), is(3));
    assertThat(((Number) activation.getDeclarationValue("$avg")).intValue(), is(6));
    Mockito.reset(ael);
    // ---------------- 2nd scenario
    final int index = 1;
    cheese[index].setPrice(9);
    ksession.update(cheeseHandles[index], cheese[index]);
    ksession.fireAllRules();
    Mockito.verify(ael).afterMatchFired(cap.capture());
    activation = cap.getValue().getMatch();
    assertThat(((Number) activation.getDeclarationValue("$sum")).intValue(), is(24));
    assertThat(((Number) activation.getDeclarationValue("$min")).intValue(), is(5));
    assertThat(((Number) activation.getDeclarationValue("$avg")).intValue(), is(8));
    Mockito.reset(ael);
    // ---------------- 3rd scenario
    bob.setLikes("brie");
    ksession.update(bobHandle, bob);
    ksession.fireAllRules();
    Mockito.verify(ael).afterMatchFired(cap.capture());
    activation = cap.getValue().getMatch();
    assertThat(((Number) activation.getDeclarationValue("$sum")).intValue(), is(32));
    assertThat(((Number) activation.getDeclarationValue("$min")).intValue(), is(15));
    assertThat(((Number) activation.getDeclarationValue("$avg")).intValue(), is(16));
    Mockito.reset(ael);
    // ---------------- 4th scenario
    ksession.delete(cheeseHandles[3]);
    ksession.fireAllRules();
    Mockito.verify(ael).afterMatchFired(cap.capture());
    activation = cap.getValue().getMatch();
    assertThat(((Number) activation.getDeclarationValue("$sum")).intValue(), is(17));
    assertThat(((Number) activation.getDeclarationValue("$min")).intValue(), is(17));
    assertThat(((Number) activation.getDeclarationValue("$avg")).intValue(), is(17));
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) Person(org.drools.compiler.Person) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Match(org.kie.api.runtime.rule.Match)

Example 14 with Match

use of org.kie.api.runtime.rule.Match in project drools by kiegroup.

the class AccumulateTest method execTestAccumulateMultipleFunctionsConstraint.

public void execTestAccumulateMultipleFunctionsConstraint(String fileName) throws Exception {
    KieSession ksession = getKieSessionFromResources(fileName);
    AgendaEventListener ael = mock(AgendaEventListener.class);
    ksession.addEventListener(ael);
    final Cheese[] cheese = new Cheese[] { new Cheese("stilton", 10), new Cheese("stilton", 3), new Cheese("stilton", 5), new Cheese("brie", 3), new Cheese("brie", 17), new Cheese("provolone", 8) };
    final Person bob = new Person("Bob", "stilton");
    final FactHandle[] cheeseHandles = new FactHandle[cheese.length];
    for (int i = 0; i < cheese.length; i++) {
        cheeseHandles[i] = (FactHandle) ksession.insert(cheese[i]);
    }
    final FactHandle bobHandle = (FactHandle) ksession.insert(bob);
    // ---------------- 1st scenario
    ksession.fireAllRules();
    ArgumentCaptor<AfterMatchFiredEvent> cap = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
    Mockito.verify(ael).afterMatchFired(cap.capture());
    Match activation = cap.getValue().getMatch();
    assertThat(((Number) activation.getDeclarationValue("$sum")).intValue(), is(18));
    assertThat(((Number) activation.getDeclarationValue("$min")).intValue(), is(3));
    assertThat(((Number) activation.getDeclarationValue("$avg")).intValue(), is(6));
    Mockito.reset(ael);
    // ---------------- 2nd scenario
    final int index = 1;
    cheese[index].setPrice(9);
    ksession.update(cheeseHandles[index], cheese[index]);
    ksession.fireAllRules();
    Mockito.verify(ael, Mockito.never()).afterMatchFired(Mockito.any(AfterMatchFiredEvent.class));
    Mockito.reset(ael);
    // ---------------- 3rd scenario
    bob.setLikes("brie");
    ksession.update(bobHandle, bob);
    ksession.fireAllRules();
    Mockito.verify(ael).afterMatchFired(cap.capture());
    activation = cap.getValue().getMatch();
    assertThat(((Number) activation.getDeclarationValue("$sum")).intValue(), is(20));
    assertThat(((Number) activation.getDeclarationValue("$min")).intValue(), is(3));
    assertThat(((Number) activation.getDeclarationValue("$avg")).intValue(), is(10));
    ksession.dispose();
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) Person(org.drools.compiler.Person) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Match(org.kie.api.runtime.rule.Match)

Example 15 with Match

use of org.kie.api.runtime.rule.Match in project drools by kiegroup.

the class DeclarativeAgendaTest method testCancelActivation.

@Test(timeout = 10000)
public void testCancelActivation() {
    String str = "";
    str += "package org.domain.test \n";
    str += "import " + Match.class.getName() + "\n";
    str += "global java.util.List list \n";
    str += "dialect 'mvel' \n";
    str += "rule rule1 @department(sales) \n";
    str += "when \n";
    str += "     $s : String( this == 'go1' ) \n";
    str += "then \n";
    str += "    list.add( kcontext.rule.name + ':' + $s ); \n";
    str += "end \n";
    str += "rule blockerAllSalesRules @activationListener('direct') \n";
    str += "when \n";
    str += "     $s : String( this == 'go2' ) \n";
    str += "     $i : Match( department == 'sales' ) \n";
    str += "then \n";
    str += "    kcontext.cancelMatch( $i ); \n";
    str += "end \n";
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(DeclarativeAgendaOption.ENABLED);
    KieBase kbase = loadKnowledgeBaseFromString(kconf, str);
    KieSession ksession = createKnowledgeSession(kbase);
    final List cancelled = new ArrayList();
    ksession.addEventListener(new AgendaEventListener() {

        public void beforeMatchFired(BeforeMatchFiredEvent event) {
        }

        public void agendaGroupPushed(AgendaGroupPushedEvent event) {
        }

        public void agendaGroupPopped(AgendaGroupPoppedEvent event) {
        }

        public void afterMatchFired(AfterMatchFiredEvent event) {
        }

        public void matchCreated(MatchCreatedEvent event) {
        }

        public void beforeRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
        }

        public void afterRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
        }

        public void beforeRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event) {
        }

        public void afterRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event) {
        }

        public void matchCancelled(MatchCancelledEvent event) {
            cancelled.add(event);
        }
    });
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.insert("go1");
    FactHandle go2 = ksession.insert("go2");
    ksession.fireAllRules();
    assertEquals(0, list.size());
    assertEquals(1, cancelled.size());
    assertEquals("rule1", ((MatchCancelledEvent) cancelled.get(0)).getMatch().getRule().getName());
    ksession.dispose();
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) RuleFlowGroupDeactivatedEvent(org.kie.api.event.rule.RuleFlowGroupDeactivatedEvent) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Match(org.kie.api.runtime.rule.Match) KieBaseConfiguration(org.kie.api.KieBaseConfiguration) AgendaGroupPoppedEvent(org.kie.api.event.rule.AgendaGroupPoppedEvent) RuleFlowGroupActivatedEvent(org.kie.api.event.rule.RuleFlowGroupActivatedEvent) KieBase(org.kie.api.KieBase) MatchCancelledEvent(org.kie.api.event.rule.MatchCancelledEvent) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) BeforeMatchFiredEvent(org.kie.api.event.rule.BeforeMatchFiredEvent) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) AgendaGroupPushedEvent(org.kie.api.event.rule.AgendaGroupPushedEvent) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) Test(org.junit.Test)

Aggregations

Match (org.kie.api.runtime.rule.Match)33 Test (org.junit.Test)29 ArrayList (java.util.ArrayList)27 List (java.util.List)26 KieSession (org.kie.api.runtime.KieSession)18 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)13 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)13 FactHandle (org.kie.api.runtime.rule.FactHandle)12 SegmentMemory (org.drools.core.reteoo.SegmentMemory)11 KieBase (org.kie.api.KieBase)11 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)11 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)10 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)9 Arrays.asList (java.util.Arrays.asList)8 JoinNode (org.drools.core.reteoo.JoinNode)8 LiaNodeMemory (org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory)8 BetaMemory (org.drools.core.reteoo.BetaMemory)7 AfterMatchFiredEvent (org.kie.api.event.rule.AfterMatchFiredEvent)5 RuleEventListener (org.kie.internal.event.rule.RuleEventListener)5 RuleEventManager (org.kie.internal.event.rule.RuleEventManager)5