Search in sources :

Example 1 with TrackingAgendaEventListener

use of org.drools.modelcompiler.util.TrackingAgendaEventListener in project drools by kiegroup.

the class QueryTest method checkRecursiveQuery.

private void checkRecursiveQuery(String query) throws InstantiationException, IllegalAccessException {
    String str = "package org.test;\n" + "import " + Person.class.getCanonicalName() + ";" + query + "declare Location\n" + "    thing : String\n" + "    location : String\n" + "end\n" + "// rule values at A11, header at A6\n" + "rule \"testPullQueryRule\" when\n" + "    String(this == \"pull\")\n" + "    Person($l : likes)\n" + "    ?isContainedIn($l, \"office\";)\n" + "then\n" + "end\n" + "\n" + "// rule values at A12, header at A6\n" + "rule \"testPushQueryRule\" when\n" + "    String(this == \"push\")\n" + "    Person($l : likes)\n" + "    isContainedIn($l, \"office\";)\n" + "then\n" + "end";
    KieSession ksession = getKieSession(str);
    FactType locationType = ksession.getKieBase().getFactType("org.test", "Location");
    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("pull");
    ksession.fireAllRules();
    Assertions.assertThat(listener.isRuleFired("testPullQueryRule")).isTrue();
    Assertions.assertThat(listener.isRuleFired("testPushQueryRule")).isFalse();
    listener.clear();
    // when location is changed of what Peter likes, pull query should
    // ignore it
    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("testPullQueryRule")).isFalse();
    Assertions.assertThat(listener.isRuleFired("testPushQueryRule")).isFalse();
    listener.clear();
    final Person paul = new Person("Paul");
    paul.setLikes("steak");
    ksession.insert(paul);
    ksession.fireAllRules();
    Assertions.assertThat(listener.isRuleFired("testPullQueryRule")).isTrue();
    Assertions.assertThat(listener.isRuleFired("testPushQueryRule")).isFalse();
}
Also used : TrackingAgendaEventListener(org.drools.modelcompiler.util.TrackingAgendaEventListener) FactHandle(org.kie.api.runtime.rule.FactHandle) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.modelcompiler.domain.Person) FactType(org.kie.api.definition.type.FactType)

Aggregations

Person (org.drools.modelcompiler.domain.Person)1 TrackingAgendaEventListener (org.drools.modelcompiler.util.TrackingAgendaEventListener)1 FactType (org.kie.api.definition.type.FactType)1 KieSession (org.kie.api.runtime.KieSession)1 FactHandle (org.kie.api.runtime.rule.FactHandle)1