use of org.kie.api.runtime.rule.Row in project drools by kiegroup.
the class LiveQueriesBadResultTest method testAccessToNotExistingVariable.
@Test
public void testAccessToNotExistingVariable() {
ViewChangedEventListener listener = new ViewChangedEventListener() {
@Override
public void rowUpdated(Row row) {
updated.add(row.get("bad"));
}
@Override
public void rowInserted(Row row) {
inserted.add(row.get("bad"));
}
@Override
public void rowDeleted(Row row) {
deleted.add(row.get("bad"));
}
};
final KieBase kieBase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "query.drl");
KieSession ksession = kieBase.newKieSession();
ksession.insert(new Person("Petr", 25));
Assertions.assertThatThrownBy(() -> ksession.openLiveQuery("simple query with no parameters", new Object[] {}, listener)).isInstanceOf(RuntimeException.class).hasMessage("The identifier 'bad' does not exist as a bound variable for this query");
}
use of org.kie.api.runtime.rule.Row in project drools by kiegroup.
the class IndexingTest method testFullFastIteratorResume.
@Test(timeout = 10000)
public void testFullFastIteratorResume() {
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();
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);
wm.openLiveQuery("peeps", new Object[] { Variable.v, 99 }, new ViewChangedEventListener() {
@Override
public void rowInserted(final Row row) {
}
@Override
public void rowDeleted(final Row row) {
}
@Override
public void rowUpdated(final Row row) {
}
});
Person p = new Person("x0", 100);
wm.insert(p);
for (int i = 1; i < 100; i++) {
p = new Person("x" + i, 101);
wm.insert(p);
wm.fireAllRules();
}
final List<RightTuple> list = new ArrayList<>(100);
FastIterator it = n.getRightIterator(bm.getRightTupleMemory());
for (RightTuple rt = n.getFirstRightTuple(null, bm.getRightTupleMemory(), it); rt != null; rt = (RightTuple) it.next(rt)) {
list.add(rt);
}
assertEquals(100, list.size());
// check we can resume from each entry in the list above.
for (int i = 0; i < 100; i++) {
final RightTuple rightTuple = list.get(i);
// resumes from the current rightTuple
it = n.getRightIterator(bm.getRightTupleMemory(), rightTuple);
int j = i + 1;
for (RightTuple rt = (RightTuple) it.next(rightTuple); rt != null; rt = (RightTuple) it.next(rt)) {
assertSame(list.get(j), rt);
j++;
}
}
} finally {
wm.dispose();
}
}
use of org.kie.api.runtime.rule.Row in project drools by kiegroup.
the class DroolsEventList method rowDeleted.
public void rowDeleted(Row row) {
int index = this.data.indexOf(row);
// create the change event
updates.beginEvent();
// do the actual remove
Row removed = data.remove(index);
updates.elementDeleted(index, removed);
updates.commitEvent();
}
use of org.kie.api.runtime.rule.Row in project drools by kiegroup.
the class QueryTest method testOpenQueryNoParams.
@Test
public void testOpenQueryNoParams() throws Exception {
// RHDM-717
String str = "";
str += "package org.drools.mvel.compiler.test \n";
str += "import org.drools.mvel.compiler.Cheese \n";
str += "query cheeses \n";
str += " stilton : Cheese(type == 'stilton') \n";
str += " cheddar : Cheese(type == 'cheddar', 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[2];
array[0] = row.get("stilton");
array[1] = row.get("cheddar");
updated.add(array);
}
public void rowDeleted(Row row) {
Object[] array = new Object[2];
array[0] = row.get("stilton");
array[1] = row.get("cheddar");
removed.add(array);
}
public void rowInserted(Row row) {
Object[] array = new Object[2];
array[0] = row.get("stilton");
array[1] = row.get("cheddar");
added.add(array);
}
};
// Open the LiveQuery
LiveQuery query = ksession.openLiveQuery("cheeses", null, listener);
ksession.fireAllRules();
// Assert that on opening we have three rows added
assertEquals(3, added.size());
assertEquals(0, removed.size());
assertEquals(0, updated.size());
// 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());
// 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());
// 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());
// Check a standard retract
ksession.retract(s1Fh);
ksession.fireAllRules();
assertEquals(4, added.size());
assertEquals(2, removed.size());
assertEquals(1, updated.size());
// 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());
// Check that updates no longer have any impact.
ksession.update(c3Fh, cheddar3);
assertEquals(4, added.size());
assertEquals(4, removed.size());
assertEquals(1, updated.size());
}
use of org.kie.api.runtime.rule.Row in project drools by kiegroup.
the class QueryTest method testQueryWithFrom.
@Test
public void testQueryWithFrom() {
final String drl = "import org.drools.mvel.compiler.oopath.model.Thing;\n" + "query isContainedIn( Thing $x, Thing $y )\n" + " $y := Thing() from $x.children\n" + "or\n" + " ( $z := Thing() from $x.children and isContainedIn( $z, $y; ) )\n" + "end\n";
final Thing smartphone = new Thing("smartphone");
final List<String> itemList = Arrays.asList(new String[] { "display", "keyboard", "processor" });
itemList.stream().map(item -> new Thing(item)).forEach((thing) -> smartphone.addChild(thing));
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
KieSession ksession = kbase.newKieSession();
ReteDumper.dumpRete(ksession);
ksession.insert(smartphone);
final QueryResults queryResults = ksession.getQueryResults("isContainedIn", new Object[] { smartphone, Variable.v });
final List<String> resultList = StreamSupport.stream(queryResults.spliterator(), false).map(row -> ((Thing) row.get("$y")).getName()).collect(Collectors.toList());
assertThat(resultList).as("Query does not contain all items").containsAll(itemList);
ksession.dispose();
}
Aggregations