use of org.kie.api.event.rule.ObjectDeletedEvent in project drools by kiegroup.
the class RuleRuntimeEventSupportTest method testRemoveRuleRuntimeEventListener.
@Test
public void testRemoveRuleRuntimeEventListener() {
KieBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
KieSession ksession = kbase.newKieSession();
final List wmList = new ArrayList();
final RuleRuntimeEventListener eventListener = new RuleRuntimeEventListener() {
public void objectInserted(ObjectInsertedEvent event) {
wmList.add(event);
}
public void objectUpdated(ObjectUpdatedEvent event) {
wmList.add(event);
}
public void objectDeleted(ObjectDeletedEvent event) {
wmList.add(event);
}
};
ksession.addEventListener(eventListener);
ksession.removeEventListener(eventListener);
final Cheese stilton = new Cheese("stilton", 15);
final Cheese cheddar = new Cheese("cheddar", 17);
final FactHandle stiltonHandle = ksession.insert(stilton);
assertTrue(wmList.isEmpty());
ksession.update(stiltonHandle, stilton);
assertTrue(wmList.isEmpty());
ksession.retract(stiltonHandle);
assertTrue(wmList.isEmpty());
ksession.insert(cheddar);
assertTrue(wmList.isEmpty());
}
use of org.kie.api.event.rule.ObjectDeletedEvent in project drools by kiegroup.
the class OOPathTest method testRemoveIfSupport.
@Test
public void testRemoveIfSupport() {
final String drl = "import org.drools.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";
final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().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"));
}
use of org.kie.api.event.rule.ObjectDeletedEvent in project drools by kiegroup.
the class OOPathTest method testListIteratorMisc.
@Test
public void testListIteratorMisc() {
final String drl = "import org.drools.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";
final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().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", 999);
final TMFile file1 = new TMFile("File1", 999);
final TMFile file2 = new TMFile("File2", 999);
final ListIterator<TMFile> xIterator = x.getFiles().listIterator();
final ListIterator<TMFile> yIterator = y.getFiles().listIterator();
xIterator.add(file0);
yIterator.add(file2);
ksession.fireAllRules();
assertTrue(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"));
assertTrue(factsCollection(ksession).contains("Y.File2"));
xIterator.add(file1);
yIterator.previous();
yIterator.add(file1);
ksession.fireAllRules();
assertTrue(factsCollection(ksession).contains("X.File0"));
assertTrue(factsCollection(ksession).contains("X.File1"));
assertFalse(factsCollection(ksession).contains("X.File2"));
assertFalse(factsCollection(ksession).contains("Y.File0"));
assertTrue(factsCollection(ksession).contains("Y.File1"));
assertTrue(factsCollection(ksession).contains("Y.File2"));
xIterator.add(file2);
yIterator.previous();
yIterator.add(file0);
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"));
System.out.println(x.getFiles());
System.out.println(y.getFiles());
xIterator.previous();
xIterator.set(new TMFile("File2R", 999));
yIterator.previous();
yIterator.set(new TMFile("File0R", 999));
ksession.fireAllRules();
assertTrue(factsCollection(ksession).contains("X.File0"));
assertTrue(factsCollection(ksession).contains("X.File1"));
assertFalse(factsCollection(ksession).contains("X.File2"));
assertFalse(factsCollection(ksession).contains("Y.File0"));
assertTrue(factsCollection(ksession).contains("Y.File1"));
assertTrue(factsCollection(ksession).contains("Y.File2"));
assertTrue(factsCollection(ksession).contains("X.File2R"));
assertTrue(factsCollection(ksession).contains("Y.File0R"));
}
use of org.kie.api.event.rule.ObjectDeletedEvent in project drools by kiegroup.
the class SequentialTest method testEvents.
@Test
public void testEvents() throws Exception {
String str = "";
str += "package org.drools.compiler.test\n";
str += "import org.drools.compiler.Message\n";
str += "rule \"Hello World\"\n";
str += "when\n";
str += " Message( )\n";
str += "then\n";
str += " System.out.println( drools.getKieRuntime() );\n";
str += "end\n";
KieBase kbase = loadKnowledgeBaseFromString(kconf, str);
StatelessKieSession ksession = createStatelessKnowledgeSession(kbase);
final List list = new ArrayList();
ksession.addEventListener(new AgendaEventListener() {
public void matchCancelled(MatchCancelledEvent event) {
assertNotNull(event.getKieRuntime());
list.add(event);
}
public void matchCreated(MatchCreatedEvent event) {
assertNotNull(event.getKieRuntime());
list.add(event);
}
public void afterMatchFired(AfterMatchFiredEvent event) {
assertNotNull(event.getKieRuntime());
list.add(event);
}
public void agendaGroupPopped(AgendaGroupPoppedEvent event) {
assertNotNull(event.getKieRuntime());
list.add(event);
}
public void agendaGroupPushed(AgendaGroupPushedEvent event) {
assertNotNull(event.getKieRuntime());
list.add(event);
}
public void beforeMatchFired(BeforeMatchFiredEvent event) {
assertNotNull(event.getKieRuntime());
list.add(event);
}
public void beforeRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
assertNotNull(event.getKieRuntime());
list.add(event);
}
public void afterRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
assertNotNull(event.getKieRuntime());
list.add(event);
}
public void beforeRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event) {
assertNotNull(event.getKieRuntime());
list.add(event);
}
public void afterRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event) {
assertNotNull(event.getKieRuntime());
list.add(event);
}
});
ksession.addEventListener(new RuleRuntimeEventListener() {
public void objectInserted(ObjectInsertedEvent event) {
assertNotNull(event.getKieRuntime());
list.add(event);
}
public void objectDeleted(ObjectDeletedEvent event) {
assertNotNull(event.getKieRuntime());
list.add(event);
}
public void objectUpdated(ObjectUpdatedEvent event) {
assertNotNull(event.getKieRuntime());
list.add(event);
}
});
ksession.execute(new Message("help"));
assertEquals(4, list.size());
}
Aggregations