use of org.kie.api.runtime.rule.Row in project drools by kiegroup.
the class LiveQueriesBadResultTest method testOfBadParameters.
@Ignore("TODO - check correct exception in this test when DROOLS-2187 is fixed.")
@Test
public void testOfBadParameters() {
ViewChangedEventListener listener = new ViewChangedEventListener() {
@Override
public void rowUpdated(Row row) {
updated.add(row.get("person"));
}
@Override
public void rowInserted(Row row) {
inserted.add(row.get("person"));
}
@Override
public void rowDeleted(Row row) {
deleted.add(row.get("person"));
}
};
final KieBase kieBase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "query.drl");
KieSession ksession = kieBase.newKieSession();
ksession.insert(new Person("Petr", 25));
ksession.openLiveQuery("queryWithParamWithoutType", (Object[]) null, listener);
}
use of org.kie.api.runtime.rule.Row in project drools by kiegroup.
the class LiveQueriesBadResultTest method testCallingLiveQueryWithoutParametersButItHasParams.
@Ignore("TODO - check correct exception in this test when DROOLS-2186 is fixed.")
@Test
public void testCallingLiveQueryWithoutParametersButItHasParams() {
final ViewChangedEventListener listener = new ViewChangedEventListener() {
@Override
public void rowUpdated(Row row) {
updated.add(row.get("person"));
}
@Override
public void rowInserted(Row row) {
inserted.add(row.get("person"));
}
@Override
public void rowDeleted(Row row) {
deleted.add(row.get("person"));
}
};
final KieBase kieBase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "query.drl");
KieSession ksession = kieBase.newKieSession();
ksession.insert(new Person("Petr"));
ksession.openLiveQuery("queryWithParams", new Object[] {}, listener);
}
use of org.kie.api.runtime.rule.Row in project drools by kiegroup.
the class IndexingTest method testIndexingOnQueryUnificationWithNot.
@Test(timeout = 10000)
public void testIndexingOnQueryUnificationWithNot() {
final String drl = "package org.drools.compiler.test \n" + "import " + Person.class.getCanonicalName() + "\n" + "query peeps( String $name, int $age ) \n" + " not $p2 : Person( $name := name, age > $age ) \n" + "end\n";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("indexing-test", kieBaseTestConfiguration, drl);
final StatefulKnowledgeSessionImpl wm = (StatefulKnowledgeSessionImpl) kbase.newKieSession();
ReteDumper.dumpRete(wm);
try {
final List<ObjectTypeNode> nodes = ((RuleBase) kbase).getRete().getObjectTypeNodes();
ObjectTypeNode node = null;
for (final ObjectTypeNode n : nodes) {
if (((ClassObjectType) n.getObjectType()).getClassType() == DroolsQuery.class) {
node = n;
break;
}
}
assertNotNull(node);
final AlphaNode alphanode = (AlphaNode) node.getObjectSinkPropagator().getSinks()[0];
final LeftInputAdapterNode liaNode = (LeftInputAdapterNode) alphanode.getObjectSinkPropagator().getSinks()[0];
final NotNode n = (NotNode) liaNode.getSinkPropagator().getSinks()[0];
final DoubleNonIndexSkipBetaConstraints c = (DoubleNonIndexSkipBetaConstraints) n.getRawConstraints();
assertTrue(c.isIndexed());
final BetaMemory bm = (BetaMemory) wm.getNodeMemory(n);
assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
final Map<String, Integer> map = new HashMap<>();
map.put("inserted", 0);
map.put("deleted", 0);
map.put("updated", 0);
wm.openLiveQuery("peeps", new Object[] { Variable.v, 99 }, new ViewChangedEventListener() {
@Override
public void rowInserted(final Row row) {
final Integer integer = map.get("inserted");
map.put("inserted", integer + 1);
}
@Override
public void rowDeleted(final Row row) {
final Integer integer = map.get("deleted");
map.put("deleted", integer + 1);
}
@Override
public void rowUpdated(final Row row) {
final Integer integer = map.get("updated");
map.put("updated", integer + 1);
}
});
final Map<String, InternalFactHandle> peeps = new HashMap<>();
Person p;
InternalFactHandle fh;
final int max = 3;
// 1 matched, prior to any insertions
assertInsertedUpdatedDeleted(map, 1, 0, 0);
// x0 is the blocker
for (int i = 0; i < max; i++) {
p = new Person("x" + i, 100);
fh = (InternalFactHandle) wm.insert(p);
wm.fireAllRules();
peeps.put(p.getName(), fh);
}
// insertions case 1 deletion
assertInsertedUpdatedDeleted(map, 1, 0, 1);
// each x is blocker in turn up to x99
for (int i = 0; i < (max - 1); i++) {
fh = peeps.get("x" + i);
p = (Person) fh.getObject();
p.setAge(90);
wm.update(fh, p);
wm.fireAllRules();
// make sure this doesn't change
assertEquals("i=" + i, 1, map.get("inserted").intValue());
}
// no change
assertInsertedUpdatedDeleted(map, 1, 0, 1);
// x99 is still the blocker, everything else is just added
for (int i = 0; i < (max - 1); i++) {
fh = peeps.get("x" + i);
p = (Person) fh.getObject();
p.setAge(102);
wm.update(fh, p);
wm.fireAllRules();
// make sure this doesn't change
assertEquals("i=" + i, 1, map.get("inserted").intValue());
}
// no change
assertInsertedUpdatedDeleted(map, 1, 0, 1);
// x99 is still the blocker
for (int i = (max - 2); i >= 0; i--) {
fh = peeps.get("x" + i);
p = (Person) fh.getObject();
p.setAge(90);
wm.update(fh, p);
wm.fireAllRules();
// make sure this doesn't change
assertEquals("i=" + i, 1, map.get("inserted").intValue());
}
// move x99, should no longer be a blocker, now it can increase
fh = peeps.get("x" + (max - 1));
p = (Person) fh.getObject();
p.setAge(90);
wm.update(fh, p);
wm.fireAllRules();
assertEquals(2, map.get("inserted").intValue());
} finally {
wm.dispose();
}
}
use of org.kie.api.runtime.rule.Row in project drools by kiegroup.
the class DroolsEventListTest method testOpenQuery.
@Test
public void testOpenQuery() throws Exception {
String str = "";
str += "package org.kie.test \n";
str += "import " + Cheese.class.getCanonicalName() + "\n";
str += "query cheeses(String $type1, String $type2) \n";
str += " stilton : Cheese(type == $type1, $price : price) \n";
str += " cheddar : Cheese(type == $type2, 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);
FactHandle s2Fh = ksession.insert(stilton2);
FactHandle s3Fh = ksession.insert(stilton3);
FactHandle c1Fh = ksession.insert(cheddar1);
FactHandle c2Fh = ksession.insert(cheddar2);
FactHandle c3Fh = ksession.insert(cheddar3);
DroolsEventList list = new DroolsEventList();
// Open the LiveQuery
LiveQuery query = ksession.openLiveQuery("cheeses", new Object[] { "cheddar", "stilton" }, list);
SortedList<Row> sorted = new SortedList<Row>(list, new Comparator<Row>() {
public int compare(Row r1, Row r2) {
Cheese c1 = (Cheese) r1.get("stilton");
Cheese c2 = (Cheese) r2.get("stilton");
return c1.getPrice() - c2.getPrice();
}
});
assertEquals(3, sorted.size());
assertEquals(1, ((Cheese) sorted.get(0).get("stilton")).getPrice());
assertEquals(2, ((Cheese) sorted.get(1).get("stilton")).getPrice());
assertEquals(3, ((Cheese) sorted.get(2).get("stilton")).getPrice());
// alter the price to remove the last row
stilton3.setPrice(4);
ksession.update(s3Fh, stilton3);
ksession.fireAllRules();
assertEquals(2, sorted.size());
assertEquals(1, ((Cheese) sorted.get(0).get("stilton")).getPrice());
assertEquals(2, ((Cheese) sorted.get(1).get("stilton")).getPrice());
// alter the price to put the last row back in
stilton3.setPrice(3);
ksession.update(s3Fh, stilton3);
ksession.fireAllRules();
assertEquals(3, sorted.size());
assertEquals(1, ((Cheese) sorted.get(0).get("stilton")).getPrice());
assertEquals(2, ((Cheese) sorted.get(1).get("stilton")).getPrice());
assertEquals(3, ((Cheese) sorted.get(2).get("stilton")).getPrice());
// alter the price to remove the middle row
stilton2.setPrice(4);
ksession.update(s2Fh, stilton2);
ksession.fireAllRules();
assertEquals(2, sorted.size());
assertEquals(1, ((Cheese) sorted.get(0).get("stilton")).getPrice());
assertEquals(3, ((Cheese) sorted.get(1).get("stilton")).getPrice());
// alter the price to add the previous middle rows to the end
cheddar2.setPrice(4);
ksession.update(c2Fh, cheddar2);
ksession.fireAllRules();
assertEquals(3, sorted.size());
assertEquals(1, ((Cheese) sorted.get(0).get("stilton")).getPrice());
assertEquals(3, ((Cheese) sorted.get(1).get("stilton")).getPrice());
assertEquals(4, ((Cheese) sorted.get(2).get("stilton")).getPrice());
// Check a standard retract
ksession.retract(s1Fh);
ksession.fireAllRules();
assertEquals(2, sorted.size());
assertEquals(3, ((Cheese) sorted.get(0).get("stilton")).getPrice());
assertEquals(4, ((Cheese) sorted.get(1).get("stilton")).getPrice());
// Close the query, we should get removed events for each row
query.close();
assertEquals(0, sorted.size());
}
Aggregations