Search in sources :

Example 16 with ObjectUpdatedEvent

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

the class OOPathTest method testRemoveIfSupport.

@Test
public void testRemoveIfSupport() {
    final String drl = "import org.drools.mvel.compiler.oopath.model.*;\n" + "\n" + "rule R2 when\n" + "  TMDirectory( $id: name, $p: /files[size >= 100] )\n" + "then\n" + "  insertLogical(      $id + \".\" + $p.getName() );\n" + "end\n";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
    KieSession ksession = kbase.newKieSession();
    ksession.addEventListener(new DefaultRuleRuntimeEventListener() {

        @Override
        public void objectDeleted(ObjectDeletedEvent event) {
            System.out.println(event.getOldObject() + " -> " + "_");
        }

        @Override
        public void objectInserted(ObjectInsertedEvent event) {
            System.out.println("_" + " -> " + event.getObject());
        }

        @Override
        public void objectUpdated(ObjectUpdatedEvent event) {
            System.out.println(event.getOldObject() + " -> " + event.getObject());
        }
    });
    final TMDirectory x = new TMDirectory("X");
    final TMDirectory y = new TMDirectory("Y");
    ksession.insert(x);
    ksession.insert(y);
    ksession.fireAllRules();
    assertFalse(factsCollection(ksession).contains("X.File0"));
    assertFalse(factsCollection(ksession).contains("X.File1"));
    assertFalse(factsCollection(ksession).contains("X.File2"));
    assertFalse(factsCollection(ksession).contains("Y.File0"));
    assertFalse(factsCollection(ksession).contains("Y.File1"));
    assertFalse(factsCollection(ksession).contains("Y.File2"));
    final TMFile file0 = new TMFile("File0", 1000);
    final TMFile file1 = new TMFile("File1", 1001);
    final TMFile file2 = new TMFile("File2", 1002);
    x.getFiles().addAll(Arrays.asList(file0, file1, file2));
    y.getFiles().addAll(Arrays.asList(file0, file1, file2));
    ksession.fireAllRules();
    assertTrue(factsCollection(ksession).contains("X.File0"));
    assertTrue(factsCollection(ksession).contains("X.File1"));
    assertTrue(factsCollection(ksession).contains("X.File2"));
    assertTrue(factsCollection(ksession).contains("Y.File0"));
    assertTrue(factsCollection(ksession).contains("Y.File1"));
    assertTrue(factsCollection(ksession).contains("Y.File2"));
    x.getFiles().removeIf(f -> f.getSize() % 2 == 0);
    y.getFiles().removeIf(f -> f.getSize() % 2 == 1);
    ksession.fireAllRules();
    assertFalse(factsCollection(ksession).contains("X.File0"));
    assertTrue(factsCollection(ksession).contains("X.File1"));
    assertFalse(factsCollection(ksession).contains("X.File2"));
    assertTrue(factsCollection(ksession).contains("Y.File0"));
    assertFalse(factsCollection(ksession).contains("Y.File1"));
    assertTrue(factsCollection(ksession).contains("Y.File2"));
}
Also used : TMFile(org.drools.mvel.compiler.oopath.model.TMFile) ObjectDeletedEvent(org.kie.api.event.rule.ObjectDeletedEvent) KieBase(org.kie.api.KieBase) TMDirectory(org.drools.mvel.compiler.oopath.model.TMDirectory) KieSession(org.kie.api.runtime.KieSession) ObjectUpdatedEvent(org.kie.api.event.rule.ObjectUpdatedEvent) ObjectInsertedEvent(org.kie.api.event.rule.ObjectInsertedEvent) DefaultRuleRuntimeEventListener(org.kie.api.event.rule.DefaultRuleRuntimeEventListener) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)16 ObjectUpdatedEvent (org.kie.api.event.rule.ObjectUpdatedEvent)16 ObjectDeletedEvent (org.kie.api.event.rule.ObjectDeletedEvent)15 ObjectInsertedEvent (org.kie.api.event.rule.ObjectInsertedEvent)15 KieSession (org.kie.api.runtime.KieSession)14 KieBase (org.kie.api.KieBase)12 RuleRuntimeEventListener (org.kie.api.event.rule.RuleRuntimeEventListener)10 ArrayList (java.util.ArrayList)9 List (java.util.List)8 FactHandle (org.kie.api.runtime.rule.FactHandle)7 Cheese (org.drools.core.test.model.Cheese)6 DefaultRuleRuntimeEventListener (org.kie.api.event.rule.DefaultRuleRuntimeEventListener)6 TMDirectory (org.drools.compiler.oopath.model.TMDirectory)3 TMFile (org.drools.compiler.oopath.model.TMFile)3 TMDirectory (org.drools.mvel.compiler.oopath.model.TMDirectory)3 TMFile (org.drools.mvel.compiler.oopath.model.TMFile)3 KieHelper (org.kie.internal.utils.KieHelper)3 AfterMatchFiredEvent (org.kie.api.event.rule.AfterMatchFiredEvent)2 AgendaEventListener (org.kie.api.event.rule.AgendaEventListener)2 AgendaGroupPoppedEvent (org.kie.api.event.rule.AgendaGroupPoppedEvent)2