Search in sources :

Example 36 with Match

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

the class DeclarativeAgendaTest method testCancelMultipleActivations.

@Test(timeout = 10000)
public void testCancelMultipleActivations() {
    String str = "package org.domain.test\n" + "import " + Match.class.getName() + "\n" + "global java.util.List list\n" + "rule sales1 @department('sales')\n" + "when\n" + "    String( this == 'fireRules' )\n" + "then\n" + "    list.add(\"sales1\");\n" + "end\n" + "\n" + "rule sales2 @department('sales') \n" + "when\n" + "    String( this == 'fireRules' )\n" + "then\n" + "    list.add(\"sales2\");\n" + "end\n" + "\n" + "rule salesCancel @activationListener('direct')\n" + "when\n" + "    $i : Match( department == 'sales' )\n" + "then\n" + "    kcontext.cancelMatch($i);\n" + "end";
    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("fireRules");
    ksession.fireAllRules();
    System.out.println(list);
    assertEquals(0, list.size());
    ksession.dispose();
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) 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 37 with Match

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

the class DeclarativeAgendaTest method testCancelActivationOnInsertAndUpdate.

@Test(timeout = 10000)
public void testCancelActivationOnInsertAndUpdate() {
    String str = "package org.domain.test\n" + "import " + Match.class.getName() + "\n" + "global java.util.List list\n" + "rule sales1 @department('sales') @category('special')\n" + "salience 10\n" + "when\n" + "    String( this == 'fireRules' )\n" + "then\n" + "    list.add(\"sales1\");\n" + "end\n" + "\n" + "rule sales2 @department('sales') \n" + "when\n" + "    String( this == 'fireRules' )\n" + "then\n" + "    list.add(\"sales2\");\n" + "end\n" + "\n" + "rule salesCancel @activationListener('direct')\n" + "when\n" + "    String(this == 'fireCancelRule')\n" + "    $i : Match( department == 'sales', category == 'special' )\n" + "then\n" + "    kcontext.cancelMatch($i);\n" + "end";
    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 fireRules = ksession.insert("fireRules");
    FactHandle fireCancelRule = ksession.insert("fireCancelRule");
    ksession.fireAllRules();
    assertEquals(1, list.size());
    ksession.update(fireRules, "fireRules");
    ksession.update(fireCancelRule, "fireCancelRule");
    ksession.fireAllRules();
    assertEquals(2, list.size());
    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 38 with Match

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

the class DeclarativeAgendaTest method testSimpleBlockingUsingForall.

@Test(timeout = 10000)
public void testSimpleBlockingUsingForall() {
    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) salience -100 \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 salience 200\n";
    str += "when \n";
    str += "     $s : String( this == 'go1' ) \n";
    str += "     exists  Match( department == 'sales' ) \n";
    str += "     forall ( $a : Match( department == 'sales' ) Match( this == $a, active == false ) ) \n";
    str += "then \n";
    str += "    list.add( kcontext.rule.name + ':' + $s ); \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");
    ksession.fireAllRules();
    assertEquals(2, list.size());
    assertTrue(list.contains("rule1:go1"));
    assertTrue(list.contains("rule2:go1"));
    ksession.dispose();
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) 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 39 with Match

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

the class Misc2Test method testQueryAndRetract.

@Test
public void testQueryAndRetract() {
    // DROOLS-7
    String str = "global java.util.List list\n" + "\n" + "query q (String $s)\n" + "    String( this == $s )\n" + "end" + "\n" + "rule R1 when\n" + "    $x : String( this == \"x\" )\n" + "    ?q( \"x\"; )\n" + "then\n" + "    delete( \"x\" );\n" + "end\n";
    KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession();
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    ((RuleEventManager) ksession).addEventListener(new RuleEventListener() {

        @Override
        public void onDeleteMatch(Match match) {
            list.add("test");
        }
    });
    ksession.insert("x");
    ksession.fireAllRules();
    assertEquals(1, list.size());
}
Also used : RuleEventManager(org.kie.internal.event.rule.RuleEventManager) ArrayList(java.util.ArrayList) RuleEventListener(org.kie.internal.event.rule.RuleEventListener) KieHelper(org.kie.internal.utils.KieHelper) 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) Match(org.kie.api.runtime.rule.Match) Test(org.junit.Test)

Example 40 with Match

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

the class RuleCoverageListenerTest method testCoverage.

@Test
public void testCoverage() throws Exception {
    // configuring mock event
    AfterMatchFiredEvent amfe = mock(AfterMatchFiredEvent.class);
    Match match = mock(Match.class);
    Rule rule = mock(Rule.class);
    when(amfe.getMatch()).thenReturn(match);
    when(match.getRule()).thenReturn(rule);
    when(rule.getName()).thenReturn("rule1").thenReturn("rule2").thenReturn("rule3");
    HashSet<String> rules = new HashSet<String>();
    rules.add("rule1");
    rules.add("rule2");
    rules.add("rule3");
    RuleCoverageListener ls = new RuleCoverageListener(rules);
    Assert.assertEquals(3, ls.rules.size());
    Assert.assertEquals(0, ls.getPercentCovered());
    ls.afterMatchFired(amfe);
    Assert.assertEquals(2, ls.rules.size());
    assertTrue(ls.rules.contains("rule2"));
    assertTrue(ls.rules.contains("rule3"));
    assertFalse(ls.rules.contains("rule1"));
    Assert.assertEquals(33, ls.getPercentCovered());
    ls.afterMatchFired(amfe);
    Assert.assertEquals(1, ls.rules.size());
    assertFalse(ls.rules.contains("rule2"));
    assertFalse(ls.rules.contains("rule1"));
    assertTrue(ls.rules.contains("rule3"));
    Assert.assertEquals(66, ls.getPercentCovered());
    ls.afterMatchFired(amfe);
    Assert.assertEquals(0, ls.rules.size());
    assertFalse(ls.rules.contains("rule2"));
    assertFalse(ls.rules.contains("rule1"));
    assertFalse(ls.rules.contains("rule3"));
    Assert.assertEquals(100, ls.getPercentCovered());
}
Also used : Rule(org.kie.api.definition.rule.Rule) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Match(org.kie.api.runtime.rule.Match) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Match (org.kie.api.runtime.rule.Match)68 Test (org.junit.Test)60 ArrayList (java.util.ArrayList)57 List (java.util.List)52 KieSession (org.kie.api.runtime.KieSession)37 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)26 KieBase (org.kie.api.KieBase)26 FactHandle (org.kie.api.runtime.rule.FactHandle)24 SegmentMemory (org.drools.core.reteoo.SegmentMemory)22 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)20 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)18 JoinNode (org.drools.core.reteoo.JoinNode)16 LiaNodeMemory (org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory)16 Arrays.asList (java.util.Arrays.asList)15 BetaMemory (org.drools.core.reteoo.BetaMemory)14 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)13 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)11 RuleEventListener (org.kie.internal.event.rule.RuleEventListener)11 RuleEventManager (org.kie.internal.event.rule.RuleEventManager)11 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)10