Search in sources :

Example 1 with DoubleNonIndexSkipBetaConstraints

use of org.drools.core.common.DoubleNonIndexSkipBetaConstraints 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();
    }
}
Also used : NotNode(org.drools.core.reteoo.NotNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ArrayList(java.util.ArrayList) BetaMemory(org.drools.core.reteoo.BetaMemory) TupleIndexHashTable(org.drools.core.util.index.TupleIndexHashTable) AlphaNode(org.drools.core.reteoo.AlphaNode) RightTuple(org.drools.core.reteoo.RightTuple) DoubleNonIndexSkipBetaConstraints(org.drools.core.common.DoubleNonIndexSkipBetaConstraints) ViewChangedEventListener(org.kie.api.runtime.rule.ViewChangedEventListener) KieBase(org.kie.api.KieBase) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) Row(org.kie.api.runtime.rule.Row) FastIterator(org.drools.core.util.FastIterator) Person(org.drools.testcoverage.common.model.Person) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 2 with DoubleNonIndexSkipBetaConstraints

use of org.drools.core.common.DoubleNonIndexSkipBetaConstraints 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();
    }
}
Also used : NotNode(org.drools.core.reteoo.NotNode) HashMap(java.util.HashMap) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) BetaMemory(org.drools.core.reteoo.BetaMemory) TupleIndexHashTable(org.drools.core.util.index.TupleIndexHashTable) AlphaNode(org.drools.core.reteoo.AlphaNode) DoubleNonIndexSkipBetaConstraints(org.drools.core.common.DoubleNonIndexSkipBetaConstraints) ViewChangedEventListener(org.kie.api.runtime.rule.ViewChangedEventListener) KieBase(org.kie.api.KieBase) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) Row(org.kie.api.runtime.rule.Row) InternalFactHandle(org.drools.core.common.InternalFactHandle) Person(org.drools.testcoverage.common.model.Person) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Aggregations

DoubleNonIndexSkipBetaConstraints (org.drools.core.common.DoubleNonIndexSkipBetaConstraints)2 AlphaNode (org.drools.core.reteoo.AlphaNode)2 BetaMemory (org.drools.core.reteoo.BetaMemory)2 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)2 NotNode (org.drools.core.reteoo.NotNode)2 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)2 TupleIndexHashTable (org.drools.core.util.index.TupleIndexHashTable)2 StatefulKnowledgeSessionImpl (org.drools.kiesession.session.StatefulKnowledgeSessionImpl)2 Person (org.drools.testcoverage.common.model.Person)2 Test (org.junit.Test)2 KieBase (org.kie.api.KieBase)2 Row (org.kie.api.runtime.rule.Row)2 ViewChangedEventListener (org.kie.api.runtime.rule.ViewChangedEventListener)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 InternalFactHandle (org.drools.core.common.InternalFactHandle)1 RightTuple (org.drools.core.reteoo.RightTuple)1 FastIterator (org.drools.core.util.FastIterator)1