Search in sources :

Example 21 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 22 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)

Example 23 with Match

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

the class JTMSTest method testPosNegNonConflictingInsertions.

@Test(timeout = 10000)
public void testPosNegNonConflictingInsertions() {
    String s = "package org.drools.core.beliefsystem.jtms;\n" + "\n" + "import java.util.List \n" + "import org.drools.core.common.AgendaItem;" + "global java.util.List list;\n" + "\n" + "rule \"go1\"\n" + "when\n" + "    String( this == 'go1' )\n" + "then\n" + "    insertLogical( 'neg', 'neg' );\n" + "end\n" + "\n" + "rule \"go2\"\n" + "when\n" + "    String( this == 'go2' )\n" + "then\n" + "    insertLogical( 'pos' );\n" + "end\n" + "\n" + "rule \"Positive\"\n" + "when\n" + "    $n : String( this != 'go1' || == 'go2' ) \n" + "then\n" + "    final String s = '+' + $n;" + "    final List l = list;" + "    l.add( s );\n" + "end\n" + "rule \"Negative\"\n" + "when\n" + "    $n : String(   _.neg, this != 'go1' || == 'go2' ) \n" + "then\n" + "    final String s = '-' + $n; \n" + "    final List l = list; \n" + "    l.add( s ); \n" + "end\n";
    KieSession kSession = getSessionFromString(s);
    List list = new ArrayList();
    kSession.setGlobal("list", list);
    ((RuleEventManager) kSession).addEventListener(new RuleEventListener() {

        @Override
        public void onDeleteMatch(Match match) {
            String rule = match.getRule().getName();
            if (rule.equals("Positive")) {
                list.remove("+" + match.getDeclarationValue("$n"));
            } else if (rule.equals("Negative")) {
                list.remove("-" + match.getDeclarationValue("$n"));
            }
        }
    });
    FactHandle fhGo1 = kSession.insert("go1");
    kSession.fireAllRules();
    assertTrue(list.contains("-neg"));
    // just go1
    assertEquals(1, kSession.getEntryPoint("DEFAULT").getObjects().size());
    assertEquals(1, getNegativeObjects(kSession).size());
    FactHandle fhGo2 = kSession.insert("go2");
    kSession.fireAllRules();
    assertTrue(list.contains("-neg"));
    assertTrue(list.contains("+pos"));
    // go1, go2, pos
    assertEquals(3, kSession.getEntryPoint("DEFAULT").getObjects().size());
    assertEquals(1, getNegativeObjects(kSession).size());
    kSession.retract(fhGo1);
    kSession.fireAllRules();
    assertFalse(list.contains("-neg"));
    assertTrue(list.contains("+pos"));
    // go2, pos
    assertEquals(2, kSession.getEntryPoint("DEFAULT").getObjects().size());
    assertEquals(0, getNegativeObjects(kSession).size());
    kSession.retract(fhGo2);
    kSession.fireAllRules();
    assertFalse(list.contains("-neg"));
    assertFalse(list.contains("+pos"));
    assertEquals(0, kSession.getEntryPoint("DEFAULT").getObjects().size());
    assertEquals(0, getNegativeObjects(kSession).size());
}
Also used : RuleEventManager(org.kie.internal.event.rule.RuleEventManager) FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) RuleEventListener(org.kie.internal.event.rule.RuleEventListener) 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 24 with Match

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

the class JTMSTest method testConflictToggleWithoutGoingEmpty.

@Test(timeout = 10000)
public void testConflictToggleWithoutGoingEmpty() {
    String s = "package org.drools.core.beliefsystem.jtms;\n" + "\n" + "import java.util.List \n" + "import org.drools.core.common.AgendaItem;" + "global java.util.List list;\n" + "\n" + "rule \"go1\"\n" + "when\n" + "    String( this == 'go1' )\n" + "then\n" + "    insertLogical( 'xxx' );\n" + "end\n" + "rule \"go2\"\n" + "when\n" + "    String( this == 'go2' )\n" + "then\n" + "    insertLogical( 'xxx');\n" + "end\n" + "rule \"go3\"\n" + "when\n" + "    String( this == 'go3' )\n" + "then\n" + "    insertLogical( 'xxx');\n" + "end\n" + "\n" + "rule \"go4\"\n" + "when\n" + "    String( this == 'go4' )\n" + "then\n" + "    insertLogical( 'xxx', 'neg' );\n" + "end\n" + "\n" + "rule \"Positive\"\n" + "when\n" + "    $n : String( this == 'xxx' ) \n" + "then\n" + "    final String s = '+' + $n;" + "    final List l = list;" + "    l.add( s );\n" + "end\n" + "rule \"Negative\"\n" + "when\n" + "    $n : String( _.neg, this == 'xxx' )\n" + "then\n" + "    final String s = '-' + $n; \n" + "    final List l = list; \n" + "    l.add( s ); \n" + "end\n" + "";
    KieSession kSession = getSessionFromString(s);
    List list = new ArrayList();
    kSession.setGlobal("list", list);
    ((RuleEventManager) kSession).addEventListener(new RuleEventListener() {

        @Override
        public void onDeleteMatch(Match match) {
            String rule = match.getRule().getName();
            if (rule.equals("Positive")) {
                list.remove("+" + match.getDeclarationValue("$n"));
            } else if (rule.equals("Negative")) {
                list.remove("-" + match.getDeclarationValue("$n"));
            }
        }
    });
    FactHandle fhGo1 = kSession.insert("go1");
    FactHandle fhGo2 = kSession.insert("go2");
    FactHandle fhGo3 = kSession.insert("go3");
    kSession.fireAllRules();
    System.out.println(list);
    assertTrue(list.contains("+xxx"));
    FactHandle fhGo4 = kSession.insert("go4");
    kSession.fireAllRules();
    assertTrue(list.isEmpty());
    kSession.delete(fhGo4);
    kSession.fireAllRules();
    assertTrue(list.contains("+xxx"));
}
Also used : RuleEventManager(org.kie.internal.event.rule.RuleEventManager) FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) RuleEventListener(org.kie.internal.event.rule.RuleEventListener) 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 25 with Match

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

the class JTMSTest method testRetractHandleWhenOnlyNeg.

@Test(timeout = 10000)
@Ignore("Currently cannot support updates")
public void testRetractHandleWhenOnlyNeg() {
    String s = "package org.drools.core.beliefsystem.jtms;\n" + "\n" + "import java.util.List \n" + "import org.drools.core.common.AgendaItem;" + "global java.util.List list;\n" + "\n" + "rule \"go1_1\"\n" + "when\n" + "    String( this == 'go1' )\n" + "then\n" + "    insertLogical( new String( 'neg' ), 'neg' );\n" + "end\n" + "rule \"go1_2\"\n" + "when\n" + "    String( this == 'go1' )\n" + "then\n" + "    insertLogical( new String( 'neg' ), 'neg' );\n" + "end\n" + "rule \"go1_3\"\n" + "when\n" + "    String( this == 'go1' )\n" + "then\n" + "    insertLogical( new String( 'neg' ), 'neg' );\n" + "end\n" + "\n" + "rule \"Negative\"\n" + "when\n" + "    $n : String(  _.neg, this != 'go1' || == 'go2' ) \n" + "then\n" + "    final String s = '-' + $n; \n" + "    final List l = list; \n" + "    l.add( s ); \n" + "end\n";
    KieSession kSession = getSessionFromString(s);
    List list = new ArrayList();
    kSession.setGlobal("list", list);
    ((RuleEventManager) kSession).addEventListener(new RuleEventListener() {

        @Override
        public void onDeleteMatch(Match match) {
            String rule = match.getRule().getName();
            if (rule.equals("Negative")) {
                list.remove("-" + match.getDeclarationValue("$n"));
            }
        }
    });
    FactHandle fhGo1 = kSession.insert("go1");
    kSession.fireAllRules();
    assertTrue(list.contains("-neg"));
    // just go1
    assertEquals(1, kSession.getEntryPoint("DEFAULT").getObjects().size());
    assertEquals(1, getNegativeObjects(kSession).size());
    NamedEntryPoint ep = (NamedEntryPoint) ((StatefulKnowledgeSessionImpl) kSession).getEntryPoint("DEFAULT");
    ObjectHashMap equalityMap = ep.getTruthMaintenanceSystem().getEqualityKeyMap();
    // go1, neg are two different strings.
    assertEquals(2, equalityMap.size());
    org.drools.core.util.Iterator it = equalityMap.iterator();
    EqualityKey key = (EqualityKey) ((ObjectEntry) it.next()).getValue();
    while (!key.getFactHandle().getObject().equals("neg")) {
        key = (EqualityKey) ((ObjectEntry) it.next()).getValue();
    }
    assertEquals(3, key.getBeliefSet().size());
    ep.getTruthMaintenanceSystem().delete(key.getLogicalFactHandle());
    assertEquals(0, key.getBeliefSet().size());
    // just go1
    assertEquals(1, kSession.getEntryPoint("DEFAULT").getObjects().size());
    assertEquals(0, getNegativeObjects(kSession).size());
    assertEquals(0, key.getBeliefSet().size());
    assertEquals(1, ep.getTruthMaintenanceSystem().getEqualityKeyMap().size());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) ArrayList(java.util.ArrayList) RuleEventListener(org.kie.internal.event.rule.RuleEventListener) ObjectHashMap(org.drools.core.util.ObjectHashMap) ObjectEntry(org.drools.core.util.ObjectHashMap.ObjectEntry) Match(org.kie.api.runtime.rule.Match) RuleEventManager(org.kie.internal.event.rule.RuleEventManager) EqualityKey(org.drools.core.common.EqualityKey) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Ignore(org.junit.Ignore) 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