Search in sources :

Example 1 with RuleEventManager

use of org.kie.internal.event.rule.RuleEventManager in project drools by kiegroup.

the class RuleEventListenerTest method testRuleEventListener.

@Test
public void testRuleEventListener() {
    String str = "import " + Person.class.getCanonicalName() + "\n" + "global java.util.List list\n" + "rule R when\n" + "  $p: Person( $age: age < 20 )\n" + "then\n" + "  modify($p) { setAge( $age + 1 ) };" + "end\n";
    KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession();
    List<String> list = new ArrayList<>();
    ((RuleEventManager) ksession).addEventListener(new RuleEventListener() {

        @Override
        public void onBeforeMatchFire(Match match) {
            list.add("onBeforeMatchFire: " + match.getDeclarationValue("$age"));
        }

        @Override
        public void onAfterMatchFire(Match match) {
            list.add("onAfterMatchFire: " + match.getDeclarationValue("$age"));
        }

        @Override
        public void onDeleteMatch(Match match) {
            list.add("onDeleteMatch: " + match.getDeclarationValue("$age"));
        }

        @Override
        public void onUpdateMatch(Match match) {
            list.add("onUpdateMatch: " + match.getDeclarationValue("$age"));
        }
    });
    ksession.insert(new Person("John Smith", 18));
    ksession.fireAllRules();
    List<String> expected = Arrays.asList("onBeforeMatchFire: 18", "onAfterMatchFire: 19", "onUpdateMatch: 19", "onBeforeMatchFire: 19", "onAfterMatchFire: 20", "onDeleteMatch: 20");
    assertEquals(expected, list);
}
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) Person(org.drools.compiler.Person) Match(org.kie.api.runtime.rule.Match) Test(org.junit.Test)

Example 2 with RuleEventManager

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

use of org.kie.internal.event.rule.RuleEventManager 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 4 with RuleEventManager

use of org.kie.internal.event.rule.RuleEventManager 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 5 with RuleEventManager

use of org.kie.internal.event.rule.RuleEventManager 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

ArrayList (java.util.ArrayList)5 Test (org.junit.Test)5 KieSession (org.kie.api.runtime.KieSession)5 Match (org.kie.api.runtime.rule.Match)5 RuleEventListener (org.kie.internal.event.rule.RuleEventListener)5 RuleEventManager (org.kie.internal.event.rule.RuleEventManager)5 List (java.util.List)4 FactHandle (org.kie.api.runtime.rule.FactHandle)3 KieHelper (org.kie.internal.utils.KieHelper)2 Arrays.asList (java.util.Arrays.asList)1 Person (org.drools.compiler.Person)1 EqualityKey (org.drools.core.common.EqualityKey)1 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)1 ObjectHashMap (org.drools.core.util.ObjectHashMap)1 ObjectEntry (org.drools.core.util.ObjectHashMap.ObjectEntry)1 Ignore (org.junit.Ignore)1 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)1