Search in sources :

Example 6 with NotNode

use of org.drools.core.reteoo.NotNode in project drools by kiegroup.

the class IndexingTest method testFullFastIteratorResume.

@Test(timeout = 10000)
public void testFullFastIteratorResume() throws Exception {
    String str = "";
    str += "package org.drools.compiler.test  \n";
    str += "import org.drools.compiler.Person \n";
    str += "query peeps( String $name, int $age ) \n";
    str += "    not $p2 : Person( $name := name, age > $age ) \n";
    str += "end\n";
    KieBase kbase = loadKnowledgeBaseFromString(str);
    List<ObjectTypeNode> nodes = ((KnowledgeBaseImpl) kbase).getRete().getObjectTypeNodes();
    ObjectTypeNode node = null;
    for (ObjectTypeNode n : nodes) {
        if (((ClassObjectType) n.getObjectType()).getClassType() == DroolsQuery.class) {
            node = n;
            break;
        }
    }
    StatefulKnowledgeSessionImpl wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
    AlphaNode alphanode = (AlphaNode) node.getObjectSinkPropagator().getSinks()[0];
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) alphanode.getObjectSinkPropagator().getSinks()[0];
    NotNode n = (NotNode) liaNode.getSinkPropagator().getSinks()[0];
    DoubleNonIndexSkipBetaConstraints c = (DoubleNonIndexSkipBetaConstraints) n.getRawConstraints();
    // assertEquals( "$name", ((VariableConstraint)c.getConstraint()).getRequiredDeclarations()[0].getIdentifier() );
    assertTrue(c.isIndexed());
    BetaMemory bm = (BetaMemory) wm.getNodeMemory(n);
    System.out.println(bm.getLeftTupleMemory().getClass());
    System.out.println(bm.getRightTupleMemory().getClass());
    assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
    assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
    final Map<String, Integer> map = new HashMap<String, Integer>();
    map.put("inserted", new Integer(0));
    map.put("deleted", new Integer(0));
    map.put("updated", new Integer(0));
    wm.openLiveQuery("peeps", new Object[] { Variable.v, 99 }, new ViewChangedEventListener() {

        @Override
        public void rowInserted(Row row) {
        }

        @Override
        public void rowDeleted(Row row) {
        }

        @Override
        public void rowUpdated(Row row) {
        }
    });
    Map<String, InternalFactHandle> peeps = new HashMap<String, InternalFactHandle>();
    Person p = new Person("x0", 100);
    InternalFactHandle fh = (InternalFactHandle) wm.insert(p);
    peeps.put(p.getName(), fh);
    for (int i = 1; i < 100; i++) {
        p = new Person("x" + i, 101);
        fh = (InternalFactHandle) wm.insert(p);
        wm.fireAllRules();
        peeps.put(p.getName(), fh);
    }
    List<RightTuple> list = new ArrayList<RightTuple>(100);
    FastIterator it = n.getRightIterator(bm.getRightTupleMemory());
    for (RightTuple rt = n.getFirstRightTuple(null, bm.getRightTupleMemory(), null, 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++) {
        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++;
        }
    }
}
Also used : NotNode(org.drools.core.reteoo.NotNode) HashMap(java.util.HashMap) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ArrayList(java.util.ArrayList) DoubleNonIndexSkipBetaConstraints(org.drools.core.common.DoubleNonIndexSkipBetaConstraints) KieBase(org.kie.api.KieBase) FastIterator(org.drools.core.util.FastIterator) InternalFactHandle(org.drools.core.common.InternalFactHandle) 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) IndexableConstraint(org.drools.core.rule.IndexableConstraint) ViewChangedEventListener(org.kie.api.runtime.rule.ViewChangedEventListener) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) Row(org.kie.api.runtime.rule.Row) Person(org.drools.compiler.Person) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 7 with NotNode

use of org.drools.core.reteoo.NotNode in project drools by kiegroup.

the class LinkingTest method testNotNodeDoesNotUnlinksWithConstriants.

@Test
public void testNotNodeDoesNotUnlinksWithConstriants() {
    String str = "";
    str += "package org.kie \n";
    str += "import " + A.class.getCanonicalName() + "\n";
    str += "import " + B.class.getCanonicalName() + "\n";
    str += "import " + C.class.getCanonicalName() + "\n";
    str += "import " + D.class.getCanonicalName() + "\n";
    str += "import " + E.class.getCanonicalName() + "\n";
    str += "import " + F.class.getCanonicalName() + "\n";
    str += "import " + G.class.getCanonicalName() + "\n";
    str += "global java.util.List list \n";
    str += "rule rule1 when \n";
    str += "   $a : A() \n";
    str += "   not B( value == $a.value ) \n";
    str += "   $c : C() \n";
    str += "then \n";
    str += "  list.add( 'x' ); \n";
    str += "end \n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    ObjectTypeNode aotn = getObjectTypeNode(kbase, A.class);
    InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
    List list = new ArrayList();
    wm.setGlobal("list", list);
    LeftInputAdapterNode aNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
    NotNode bNode = (NotNode) aNode.getSinkPropagator().getSinks()[0];
    JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[0];
    SegmentUtilities.createSegmentMemory(cNode, wm);
    LiaNodeMemory amem = (LiaNodeMemory) wm.getNodeMemory(aNode);
    // Only NotNode is linked in
    assertEquals(2, amem.getSegmentMemory().getLinkedNodeMask());
    FactHandle fha = wm.insert(new A());
    FactHandle fhb = wm.insert(new B(1));
    FactHandle fhc = wm.insert(new C());
    wm.flushPropagations();
    // All nodes are linked in
    assertEquals(7, amem.getSegmentMemory().getLinkedNodeMask());
    // NotNode does not unlink, due to variable constraint
    wm.retract(fhb);
    wm.flushPropagations();
    assertEquals(7, amem.getSegmentMemory().getLinkedNodeMask());
}
Also used : NotNode(org.drools.core.reteoo.NotNode) FactHandle(org.kie.api.runtime.rule.FactHandle) JoinNode(org.drools.core.reteoo.JoinNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ArrayList(java.util.ArrayList) LiaNodeMemory(org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ArrayList(java.util.ArrayList) List(java.util.List) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 8 with NotNode

use of org.drools.core.reteoo.NotNode in project drools by kiegroup.

the class IndexingTest method testIndexingOnQueryUnificationWithNot.

@Test(timeout = 10000)
public void testIndexingOnQueryUnificationWithNot() throws Exception {
    String str = "";
    str += "package org.drools.compiler.test  \n";
    str += "import org.drools.compiler.Person \n";
    str += "query peeps( String $name, int $age ) \n";
    str += "    not $p2 : Person( $name := name, age > $age ) \n";
    str += "end\n";
    KieBase kbase = loadKnowledgeBaseFromString(str);
    List<ObjectTypeNode> nodes = ((KnowledgeBaseImpl) kbase).getRete().getObjectTypeNodes();
    ObjectTypeNode node = null;
    for (ObjectTypeNode n : nodes) {
        if (((ClassObjectType) n.getObjectType()).getClassType() == DroolsQuery.class) {
            node = n;
            break;
        }
    }
    StatefulKnowledgeSessionImpl wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
    AlphaNode alphanode = (AlphaNode) node.getObjectSinkPropagator().getSinks()[0];
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) alphanode.getObjectSinkPropagator().getSinks()[0];
    NotNode n = (NotNode) liaNode.getSinkPropagator().getSinks()[0];
    DoubleNonIndexSkipBetaConstraints c = (DoubleNonIndexSkipBetaConstraints) n.getRawConstraints();
    // assertEquals( "$name", ((VariableConstraint)c.getConstraint()).getRequiredDeclarations()[0].getIdentifier() );
    assertTrue(c.isIndexed());
    BetaMemory bm = (BetaMemory) wm.getNodeMemory(n);
    System.out.println(bm.getLeftTupleMemory().getClass());
    System.out.println(bm.getRightTupleMemory().getClass());
    assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
    assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
    final Map<String, Integer> map = new HashMap<String, Integer>();
    map.put("inserted", new Integer(0));
    map.put("deleted", new Integer(0));
    map.put("updated", new Integer(0));
    wm.openLiveQuery("peeps", new Object[] { Variable.v, 99 }, new ViewChangedEventListener() {

        @Override
        public void rowInserted(Row row) {
            System.out.println("inserted");
            Integer integer = map.get("inserted");
            map.put("inserted", integer.intValue() + 1);
        }

        @Override
        public void rowDeleted(Row row) {
            System.out.println("deleted");
            Integer integer = map.get("deleted");
            map.put("deleted", integer.intValue() + 1);
        }

        @Override
        public void rowUpdated(Row row) {
            System.out.println("updated");
            Integer integer = map.get("updated");
            map.put("updated", integer.intValue() + 1);
        }
    });
    System.out.println("inserted: " + map.get("inserted"));
    System.out.println("deleted: " + map.get("deleted"));
    System.out.println("updated: " + map.get("updated"));
    Map<String, InternalFactHandle> peeps = new HashMap<String, InternalFactHandle>();
    Person p = null;
    InternalFactHandle fh = null;
    int max = 3;
    // 1 matched, prior to any insertions
    assertEquals(1, map.get("inserted").intValue());
    assertEquals(0, map.get("deleted").intValue());
    assertEquals(0, map.get("updated").intValue());
    // 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
    assertEquals(1, map.get("inserted").intValue());
    assertEquals(1, map.get("deleted").intValue());
    assertEquals(0, map.get("updated").intValue());
    // 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
    assertEquals(1, map.get("inserted").intValue());
    assertEquals(1, map.get("deleted").intValue());
    assertEquals(0, map.get("updated").intValue());
    // 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
    assertEquals(1, map.get("inserted").intValue());
    assertEquals(1, map.get("deleted").intValue());
    assertEquals(0, map.get("updated").intValue());
    // 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());
}
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) IndexableConstraint(org.drools.core.rule.IndexableConstraint) DoubleNonIndexSkipBetaConstraints(org.drools.core.common.DoubleNonIndexSkipBetaConstraints) ViewChangedEventListener(org.kie.api.runtime.rule.ViewChangedEventListener) KieBase(org.kie.api.KieBase) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) Row(org.kie.api.runtime.rule.Row) InternalFactHandle(org.drools.core.common.InternalFactHandle) Person(org.drools.compiler.Person) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 9 with NotNode

use of org.drools.core.reteoo.NotNode in project drools by kiegroup.

the class LinkingTest method testNotNodeMasksWithoutConstraints.

@Test
public void testNotNodeMasksWithoutConstraints() throws Exception {
    String str = "";
    str += "package org.kie \n";
    str += "import " + A.class.getCanonicalName() + "\n";
    str += "import " + B.class.getCanonicalName() + "\n";
    str += "import " + C.class.getCanonicalName() + "\n";
    str += "import " + D.class.getCanonicalName() + "\n";
    str += "import " + E.class.getCanonicalName() + "\n";
    str += "import " + F.class.getCanonicalName() + "\n";
    str += "import " + G.class.getCanonicalName() + "\n";
    str += "global java.util.List list \n";
    str += "rule rule1 when \n";
    str += "   A() \n";
    str += "   not( B( ) ) \n";
    str += "   C() \n";
    str += "then \n";
    str += "end \n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    ObjectTypeNode node = getObjectTypeNode(kbase, A.class);
    InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) node.getObjectSinkPropagator().getSinks()[0];
    assertEquals(1, liaNode.getSinkPropagator().size());
    wm.insert(new A());
    wm.flushPropagations();
    NotNode notNode = (NotNode) liaNode.getSinkPropagator().getSinks()[0];
    JoinNode cNode = (JoinNode) notNode.getSinkPropagator().getSinks()[0];
    RuleTerminalNode rtn = (RuleTerminalNode) cNode.getSinkPropagator().getSinks()[0];
    PathMemory pmem = (PathMemory) wm.getNodeMemory(rtn);
    assertEquals(1, pmem.getSegmentMemories().length);
    assertEquals(1, pmem.getAllLinkedMaskTest());
    SegmentMemory sm = pmem.getSegmentMemories()[0];
    assertEquals(7, sm.getAllLinkedMaskTest());
    assertEquals(3, sm.getLinkedNodeMask());
    assertFalse(sm.isSegmentLinked());
    assertFalse(pmem.isRuleLinked());
    wm.insert(new C());
    wm.flushPropagations();
    assertEquals(7, sm.getLinkedNodeMask());
    assertTrue(sm.isSegmentLinked());
    assertTrue(pmem.isRuleLinked());
}
Also used : NotNode(org.drools.core.reteoo.NotNode) SegmentMemory(org.drools.core.reteoo.SegmentMemory) JoinNode(org.drools.core.reteoo.JoinNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) PathMemory(org.drools.core.reteoo.PathMemory) Test(org.junit.Test)

Example 10 with NotNode

use of org.drools.core.reteoo.NotNode in project drools by kiegroup.

the class LinkingTest method testNotNodeMasksWithConstraints.

@Test
public void testNotNodeMasksWithConstraints() throws Exception {
    String str = "";
    str += "package org.kie \n";
    str += "import " + A.class.getCanonicalName() + "\n";
    str += "import " + B.class.getCanonicalName() + "\n";
    str += "import " + C.class.getCanonicalName() + "\n";
    str += "import " + D.class.getCanonicalName() + "\n";
    str += "import " + E.class.getCanonicalName() + "\n";
    str += "import " + F.class.getCanonicalName() + "\n";
    str += "import " + G.class.getCanonicalName() + "\n";
    str += "global java.util.List list \n";
    str += "rule rule1 when \n";
    str += "   $a : A() \n";
    str += "   not( B( value == $a.value ) ) \n";
    str += "   C() \n";
    str += "then \n";
    str += "end \n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    ObjectTypeNode node = getObjectTypeNode(kbase, A.class);
    InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) node.getObjectSinkPropagator().getSinks()[0];
    assertEquals(1, liaNode.getSinkPropagator().size());
    wm.insert(new A());
    wm.flushPropagations();
    NotNode notNode = (NotNode) liaNode.getSinkPropagator().getSinks()[0];
    JoinNode cNode = (JoinNode) notNode.getSinkPropagator().getSinks()[0];
    RuleTerminalNode rtn = (RuleTerminalNode) cNode.getSinkPropagator().getSinks()[0];
    PathMemory pmem = (PathMemory) wm.getNodeMemory(rtn);
    assertEquals(1, pmem.getSegmentMemories().length);
    assertEquals(1, pmem.getAllLinkedMaskTest());
    SegmentMemory sm = pmem.getSegmentMemories()[0];
    assertEquals(5, sm.getAllLinkedMaskTest());
    assertEquals(3, sm.getLinkedNodeMask());
    assertFalse(sm.isSegmentLinked());
    assertFalse(pmem.isRuleLinked());
    wm.insert(new C());
    wm.flushPropagations();
    // only 5 is needed to link, the 'not' turns on but it has no unfleunce either way
    assertEquals(7, sm.getLinkedNodeMask());
    assertTrue(sm.isSegmentLinked());
    assertTrue(pmem.isRuleLinked());
}
Also used : NotNode(org.drools.core.reteoo.NotNode) SegmentMemory(org.drools.core.reteoo.SegmentMemory) JoinNode(org.drools.core.reteoo.JoinNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) PathMemory(org.drools.core.reteoo.PathMemory) Test(org.junit.Test)

Aggregations

NotNode (org.drools.core.reteoo.NotNode)14 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)13 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)12 JoinNode (org.drools.core.reteoo.JoinNode)10 Test (org.junit.Test)10 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)8 BetaMemory (org.drools.core.reteoo.BetaMemory)6 SegmentMemory (org.drools.core.reteoo.SegmentMemory)6 KieBase (org.kie.api.KieBase)6 LiaNodeMemory (org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory)5 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)5 ArrayList (java.util.ArrayList)4 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)4 ExistsNode (org.drools.core.reteoo.ExistsNode)4 RightInputAdapterNode (org.drools.core.reteoo.RightInputAdapterNode)4 HashMap (java.util.HashMap)3 List (java.util.List)3 InternalFactHandle (org.drools.core.common.InternalFactHandle)3 AccumulateNode (org.drools.core.reteoo.AccumulateNode)3 AccumulateMemory (org.drools.core.reteoo.AccumulateNode.AccumulateMemory)3