use of org.drools.testcoverage.common.listener.TrackingAgendaEventListener in project drools by kiegroup.
the class DecisionTableTest method testEvalTable.
/**
* test for various evaluations, file sample_eval_dt.xls need to rewrite xls
* table and maybe add some classes to be able to do the test
*/
@Test
public void testEvalTable() {
final Resource decisionTable = ResourceUtil.getDecisionTableResourceFromClasspath("eval_dt.xls", getClass(), DecisionTableInputType.XLS);
final KieBase kbase = KieBaseUtil.getKieBaseFromResources(kieBaseTestConfiguration, decisionTable);
Assertions.assertThat(2).isEqualTo(kbase.getKiePackages().size());
KieSession session = kbase.newKieSession();
final TrackingAgendaEventListener rulesFired = new TrackingAgendaEventListener();
session.addEventListener(rulesFired);
rulesFired.clear();
// eval test 1
final Subject mary = new Subject("Mary");
mary.setDummy(1);
session.insert(mary);
session.fireAllRules();
Assertions.assertThat(rulesFired.isRuleFired("evalTest1")).isTrue();
Assertions.assertThat(rulesFired.isRuleFired("evalTest2")).isFalse();
Assertions.assertThat(rulesFired.isRuleFired("evalTest3")).isFalse();
Assertions.assertThat(rulesFired.isRuleFired("evalTest4")).isFalse();
Assertions.assertThat(rulesFired.isRuleFired("simpleBindingTest")).isFalse();
session.dispose();
// eval test 2
session = kbase.newKieSession();
session.addEventListener(rulesFired);
rulesFired.clear();
final Subject inge = new Subject("Inge");
inge.setAge(7);
inge.setSex("F");
final Subject jochen = new Subject("Jochen");
jochen.setAge(9);
jochen.setSex("M");
session.insert(inge);
session.insert(jochen);
session.fireAllRules();
Assertions.assertThat(rulesFired.isRuleFired("evalTest1")).isFalse();
Assertions.assertThat(rulesFired.isRuleFired("evalTest2")).isTrue();
Assertions.assertThat(rulesFired.isRuleFired("evalTest3")).isFalse();
Assertions.assertThat(rulesFired.isRuleFired("evalTest4")).isFalse();
Assertions.assertThat(rulesFired.isRuleFired("simpleBindingTest")).isFalse();
session.dispose();
// eval test 3, will run four times, there are four combinations
session = kbase.newKieSession();
session.addEventListener(rulesFired);
rulesFired.clear();
final Subject karl = new Subject("Karl");
karl.setSex("male");
final Subject egon = new Subject("Egon");
egon.setSex("male");
session.insert(karl);
session.insert(egon);
session.fireAllRules();
Assertions.assertThat(rulesFired.isRuleFired("evalTest1")).isFalse();
Assertions.assertThat(rulesFired.isRuleFired("evalTest2")).isFalse();
Assertions.assertThat(rulesFired.isRuleFired("evalTest3")).isTrue();
Assertions.assertThat(rulesFired.isRuleFired("evalTest4")).isFalse();
Assertions.assertThat(rulesFired.isRuleFired("simpleBindingTest")).isFalse();
session.dispose();
// eval test 4
session = kbase.newKieSession();
session.addEventListener(rulesFired);
rulesFired.clear();
final Subject gerda = new Subject("Gerda");
gerda.setSex("female");
gerda.setAge(9);
gerda.setDummy(-10);
session.insert(gerda);
session.fireAllRules();
Assertions.assertThat(rulesFired.isRuleFired("evalTest1")).isFalse();
Assertions.assertThat(rulesFired.isRuleFired("evalTest2")).isFalse();
Assertions.assertThat(rulesFired.isRuleFired("evalTest3")).isFalse();
Assertions.assertThat(rulesFired.isRuleFired("evalTest4")).isTrue();
Assertions.assertThat(rulesFired.isRuleFired("simpleBindingTest")).isFalse();
session.dispose();
// eval test 5 - simple binding
session = kbase.newKieSession();
session.addEventListener(rulesFired);
rulesFired.clear();
final List<Sample> results = new ArrayList<>();
session.setGlobal("results", results);
final Sample sample = new Sample();
session.insert(sample);
session.fireAllRules();
Assertions.assertThat(rulesFired.isRuleFired("evalTest1")).isFalse();
Assertions.assertThat(rulesFired.isRuleFired("evalTest2")).isFalse();
Assertions.assertThat(rulesFired.isRuleFired("evalTest3")).isFalse();
Assertions.assertThat(rulesFired.isRuleFired("evalTest4")).isFalse();
Assertions.assertThat(rulesFired.isRuleFired("simpleBindingTest")).isTrue();
session.dispose();
}
use of org.drools.testcoverage.common.listener.TrackingAgendaEventListener in project drools by kiegroup.
the class DecisionTableTest method testPushQueryWithFactDeclaration.
@Test
public void testPushQueryWithFactDeclaration() throws IllegalAccessException, InstantiationException {
final Resource decisionTable = ResourceUtil.getDecisionTableResourceFromClasspath("queries.xls", getClass(), DecisionTableInputType.XLS);
final KieBase kbase = KieBaseUtil.getKieBaseFromResources(kieBaseTestConfiguration, decisionTable);
final FactType locationType = kbase.getFactType(TestConstants.PACKAGE_FUNCTIONAL, "Location");
final KieSession ksession = kbase.newKieSession();
final TrackingAgendaEventListener listener = new TrackingAgendaEventListener();
ksession.addEventListener(listener);
final Person peter = new Person("Peter");
peter.setLikes("steak");
final Object steakLocation = locationType.newInstance();
locationType.set(steakLocation, "thing", "steak");
locationType.set(steakLocation, "location", "table");
final Object tableLocation = locationType.newInstance();
locationType.set(tableLocation, "thing", "table");
locationType.set(tableLocation, "location", "office");
ksession.insert(peter);
final FactHandle steakHandle = ksession.insert(steakLocation);
final FactHandle tableHandle = ksession.insert(tableLocation);
ksession.insert("push");
ksession.fireAllRules();
Assertions.assertThat(listener.isRuleFired("testPushQueryRule")).isTrue();
Assertions.assertThat(listener.isRuleFired("testPullQueryRule")).isFalse();
listener.clear();
// when location is changed of what Peter likes, push query should fire
// rule
final Object steakLocation2 = locationType.newInstance();
locationType.set(steakLocation2, "thing", "steak");
locationType.set(steakLocation2, "location", "desk");
final Object deskLocation = locationType.newInstance();
locationType.set(deskLocation, "thing", "desk");
locationType.set(deskLocation, "location", "office");
ksession.insert(steakLocation2);
ksession.insert(deskLocation);
ksession.delete(steakHandle);
ksession.delete(tableHandle);
ksession.fireAllRules();
Assertions.assertThat(listener.isRuleFired("testPushQueryRule")).isTrue();
Assertions.assertThat(listener.isRuleFired("testPullQueryRule")).isFalse();
listener.clear();
final Person paul = new Person("Paul");
paul.setLikes("steak");
ksession.insert(paul);
ksession.fireAllRules();
Assertions.assertThat(listener.isRuleFired("testPushQueryRule")).isTrue();
Assertions.assertThat(listener.isRuleFired("testPullQueryRule")).isFalse();
listener.clear();
ksession.dispose();
}
use of org.drools.testcoverage.common.listener.TrackingAgendaEventListener in project drools by kiegroup.
the class GuidedDecisionTableTest method initKieSession.
private void initKieSession(String... resourceNames) {
KieResources kieResources = KieServices.get().getResources();
final Resource[] resources = Stream.of(resourceNames).map(resource -> kieResources.newClassPathResource(resource, GuidedDecisionTableTest.class)).toArray(Resource[]::new);
final KieBase kBase = KieBaseUtil.getKieBaseFromResources(kieBaseTestConfiguration, resources);
kSession = kBase.newKieSession();
rulesFired = new TrackingAgendaEventListener();
kSession.addEventListener(rulesFired);
rulesFired.clear();
}
Aggregations