use of org.kie.api.runtime.rule.LiveQuery in project drools by kiegroup.
the class QueryTest method testOpenQuery.
@Test
public void testOpenQuery() throws Exception {
String str = "";
str += "package org.drools.mvel.compiler.test \n";
str += "import org.drools.mvel.compiler.Cheese \n";
str += "query cheeses(String $type1, String $type2) \n";
str += " stilton : Cheese(type == $type1, $sprice : price) \n";
str += " cheddar : Cheese(type == $type2, $cprice : price == stilton.price) \n";
str += "end\n";
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
KieSession ksession = kbase.newKieSession();
Cheese stilton1 = new Cheese("stilton", 1);
Cheese cheddar1 = new Cheese("cheddar", 1);
Cheese stilton2 = new Cheese("stilton", 2);
Cheese cheddar2 = new Cheese("cheddar", 2);
Cheese stilton3 = new Cheese("stilton", 3);
Cheese cheddar3 = new Cheese("cheddar", 3);
FactHandle s1Fh = ksession.insert(stilton1);
ksession.insert(stilton2);
ksession.insert(stilton3);
ksession.insert(cheddar1);
ksession.insert(cheddar2);
FactHandle c3Fh = ksession.insert(cheddar3);
final List<Object[]> updated = new ArrayList<Object[]>();
final List<Object[]> removed = new ArrayList<Object[]>();
final List<Object[]> added = new ArrayList<Object[]>();
ViewChangedEventListener listener = new ViewChangedEventListener() {
public void rowUpdated(Row row) {
Object[] array = new Object[6];
array[0] = row.get("stilton");
array[1] = row.get("cheddar");
array[2] = row.get("$sprice");
array[3] = row.get("$cprice");
array[4] = row.get("$type1");
array[5] = row.get("$type2");
updated.add(array);
}
public void rowDeleted(Row row) {
Object[] array = new Object[6];
array[0] = row.get("stilton");
array[1] = row.get("cheddar");
array[2] = row.get("$sprice");
array[3] = row.get("$cprice");
array[4] = row.get("$type1");
array[5] = row.get("$type2");
removed.add(array);
}
public void rowInserted(Row row) {
Object[] array = new Object[6];
array[0] = row.get("stilton");
array[1] = row.get("cheddar");
array[2] = row.get("$sprice");
array[3] = row.get("$cprice");
array[4] = row.get("$type1");
array[5] = row.get("$type2");
added.add(array);
}
};
// Open the LiveQuery
LiveQuery query = ksession.openLiveQuery("cheeses", new Object[] { "stilton", "cheddar" }, listener);
ksession.fireAllRules();
// Assert that on opening we have three rows added
assertEquals(3, added.size());
assertEquals(0, removed.size());
assertEquals(0, updated.size());
// Assert that the identifiers where retrievable
assertSame(stilton1, added.get(2)[0]);
assertSame(cheddar1, added.get(2)[1]);
assertEquals(1, added.get(2)[2]);
assertEquals(1, added.get(2)[3]);
assertEquals("stilton", added.get(2)[4]);
assertEquals("cheddar", added.get(2)[5]);
// And that we have correct values from those rows
assertEquals(3, added.get(0)[3]);
assertEquals(2, added.get(1)[3]);
assertEquals(1, added.get(2)[3]);
// Do an update that causes a match to become untrue, thus triggering a removed
cheddar3.setPrice(4);
ksession.update(c3Fh, cheddar3);
ksession.fireAllRules();
assertEquals(3, added.size());
assertEquals(1, removed.size());
assertEquals(0, updated.size());
assertEquals(4, removed.get(0)[3]);
// Now make that partial true again, and thus another added
cheddar3.setPrice(3);
ksession.update(c3Fh, cheddar3);
ksession.fireAllRules();
assertEquals(4, added.size());
assertEquals(1, removed.size());
assertEquals(0, updated.size());
assertEquals(3, added.get(3)[3]);
// check a standard update
cheddar3.setOldPrice(0);
ksession.update(c3Fh, cheddar3);
ksession.fireAllRules();
assertEquals(4, added.size());
assertEquals(1, removed.size());
assertEquals(1, updated.size());
assertEquals(3, updated.get(0)[3]);
// Check a standard retract
ksession.retract(s1Fh);
ksession.fireAllRules();
assertEquals(4, added.size());
assertEquals(2, removed.size());
assertEquals(1, updated.size());
assertEquals(1, removed.get(1)[3]);
// Close the query, we should get removed events for each row
query.close();
ksession.fireAllRules();
assertEquals(4, added.size());
assertEquals(4, removed.size());
assertEquals(1, updated.size());
assertEquals(2, removed.get(3)[3]);
assertEquals(3, removed.get(2)[3]);
// Check that updates no longer have any impact.
ksession.update(c3Fh, cheddar3);
assertEquals(4, added.size());
assertEquals(4, removed.size());
assertEquals(1, updated.size());
}
Aggregations