Search in sources :

Example 26 with BetaMemory

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

the class RightTupleIndexHashTableIteratorTest method test1.

@Test
public void test1() {
    BetaNodeFieldConstraint constraint0 = getConstraint("d", Operator.EQUAL, "this", Foo.class);
    BetaNodeFieldConstraint[] constraints = new BetaNodeFieldConstraint[] { constraint0 };
    RuleBaseConfiguration config = new RuleBaseConfiguration();
    BetaConstraints betaConstraints = null;
    betaConstraints = new SingleBetaConstraints(constraints, config);
    BetaMemory betaMemory = betaConstraints.createBetaMemory(config, NodeTypeEnums.JoinNode);
    KieBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    KieSession ss = kBase.newKieSession();
    InternalFactHandle fh1 = (InternalFactHandle) ss.insert(new Foo("brie", 1));
    InternalFactHandle fh2 = (InternalFactHandle) ss.insert(new Foo("brie", 1));
    InternalFactHandle fh3 = (InternalFactHandle) ss.insert(new Foo("soda", 1));
    InternalFactHandle fh4 = (InternalFactHandle) ss.insert(new Foo("soda", 1));
    InternalFactHandle fh5 = (InternalFactHandle) ss.insert(new Foo("bread", 3));
    InternalFactHandle fh6 = (InternalFactHandle) ss.insert(new Foo("bread", 3));
    InternalFactHandle fh7 = (InternalFactHandle) ss.insert(new Foo("cream", 3));
    InternalFactHandle fh8 = (InternalFactHandle) ss.insert(new Foo("gorda", 15));
    InternalFactHandle fh9 = (InternalFactHandle) ss.insert(new Foo("beer", 16));
    InternalFactHandle fh10 = (InternalFactHandle) ss.insert(new Foo("mars", 0));
    InternalFactHandle fh11 = (InternalFactHandle) ss.insert(new Foo("snicker", 0));
    InternalFactHandle fh12 = (InternalFactHandle) ss.insert(new Foo("snicker", 0));
    InternalFactHandle fh13 = (InternalFactHandle) ss.insert(new Foo("snicker", 0));
    betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh1, null));
    betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh2, null));
    betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh3, null));
    betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh4, null));
    betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh5, null));
    betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh6, null));
    betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh7, null));
    betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh8, null));
    betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh9, null));
    TupleIndexHashTable hashTable = (TupleIndexHashTable) betaMemory.getRightTupleMemory();
    // can't create a 0 hashCode, so forcing
    TupleList rightTupleList = new TupleList();
    rightTupleList.add(new RightTupleImpl(fh10, null));
    hashTable.getTable()[0] = rightTupleList;
    rightTupleList = new TupleList();
    rightTupleList.add(new RightTupleImpl(fh11, null));
    rightTupleList.add(new RightTupleImpl(fh12, null));
    rightTupleList.add(new RightTupleImpl(fh13, null));
    ((TupleList) hashTable.getTable()[0]).setNext(rightTupleList);
    Entry[] table = hashTable.getTable();
    List list = new ArrayList();
    for (int i = 0; i < table.length; i++) {
        if (table[i] != null) {
            List entries = new ArrayList();
            entries.add(i);
            Entry entry = table[i];
            while (entry != null) {
                entries.add(entry);
                entry = entry.getNext();
            }
            list.add(entries.toArray());
        }
    }
    assertEquals(5, list.size());
    Object[] entries = (Object[]) list.get(0);
    assertEquals(0, entries[0]);
    assertEquals(3, entries.length);
    entries = (Object[]) list.get(1);
    assertEquals(102, entries[0]);
    assertEquals(2, entries.length);
    entries = (Object[]) list.get(2);
    assertEquals(103, entries[0]);
    assertEquals(2, entries.length);
    entries = (Object[]) list.get(3);
    assertEquals(115, entries[0]);
    assertEquals(3, entries.length);
    entries = (Object[]) list.get(4);
    assertEquals(117, entries[0]);
    assertEquals(3, entries.length);
    // System.out.println( entries );
    list = new ArrayList<LeftTupleImpl>();
    Iterator it = betaMemory.getRightTupleMemory().iterator();
    for (RightTuple rightTuple = (RightTuple) it.next(); rightTuple != null; rightTuple = (RightTuple) it.next()) {
        list.add(rightTuple);
    }
    assertEquals(13, list.size());
}
Also used : SingleBetaConstraints(org.drools.core.common.SingleBetaConstraints) BetaConstraints(org.drools.core.common.BetaConstraints) SingleBetaConstraints(org.drools.core.common.SingleBetaConstraints) ArrayList(java.util.ArrayList) BetaMemory(org.drools.core.reteoo.BetaMemory) RightTupleImpl(org.drools.core.reteoo.RightTupleImpl) TupleIndexHashTable(org.drools.core.util.index.TupleIndexHashTable) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) RightTuple(org.drools.core.reteoo.RightTuple) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) TupleList(org.drools.core.util.index.TupleList) KieBase(org.kie.api.KieBase) FieldIndexHashTableFullIterator(org.drools.core.util.index.TupleIndexHashTable.FieldIndexHashTableFullIterator) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) TupleList(org.drools.core.util.index.TupleList) List(java.util.List) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Example 27 with BetaMemory

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

the class BetaNodeVisitor method doVisit.

@Override
protected void doVisit(NetworkNode node, Stack<NetworkNode> nodeStack, StatefulKnowledgeSessionInfo info) {
    BetaNode bn = (BetaNode) node;
    DefaultNodeInfo ni = (DefaultNodeInfo) info.getNodeInfo(node);
    final BetaMemory memory = (BetaMemory) info.getSession().getNodeMemory(bn);
    ni.setMemoryEnabled(true);
    if (bn.isObjectMemoryEnabled()) {
        ni.setFactMemorySize(memory.getRightTupleMemory().size());
    }
    if (bn.isLeftTupleMemoryEnabled()) {
        ni.setTupleMemorySize(memory.getLeftTupleMemory().size());
    }
}
Also used : BetaNode(org.drools.core.reteoo.BetaNode) BetaMemory(org.drools.core.reteoo.BetaMemory)

Example 28 with BetaMemory

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

the class RightInputAdapterNodeVisitor method doVisit.

@Override
protected void doVisit(NetworkNode node, Stack<NetworkNode> nodeStack, StatefulKnowledgeSessionInfo info) {
    RightInputAdapterNode an = (RightInputAdapterNode) node;
    DefaultNodeInfo ni = info.getNodeInfo(node);
    BetaNode betaNode = (BetaNode) an.getObjectSinkPropagator().getSinks()[0];
    Memory childMemory = info.getSession().getNodeMemory(betaNode);
    BetaMemory bm;
    if (betaNode.getType() == NodeTypeEnums.AccumulateNode) {
        bm = ((AccumulateMemory) childMemory).getBetaMemory();
    } else {
        bm = (BetaMemory) childMemory;
    }
    ni.setMemoryEnabled(true);
    ni.setTupleMemorySize(bm.getRightTupleMemory().size());
    ni.setCreatedFactHandles(bm.getRightTupleMemory().size());
}
Also used : BetaNode(org.drools.core.reteoo.BetaNode) Memory(org.drools.core.common.Memory) BetaMemory(org.drools.core.reteoo.BetaMemory) AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) BetaMemory(org.drools.core.reteoo.BetaMemory) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode)

Example 29 with BetaMemory

use of org.drools.core.reteoo.BetaMemory 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 30 with BetaMemory

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

the class LinkingTest method testNonReactiveSubNetworkInShareMasks.

@Test
public void testNonReactiveSubNetworkInShareMasks() 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 += "   exists( B() and C() ) \n";
    str += "   exists( eval(1==1) ) \n";
    str += "   D() \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(2, liaNode.getSinkPropagator().size());
    JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getSinks()[0];
    JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[0];
    ExistsNode exists1n = (ExistsNode) liaNode.getSinkPropagator().getSinks()[1];
    EvalConditionNode evalNode = (EvalConditionNode) exists1n.getSinkPropagator().getSinks()[0];
    ExistsNode exists2n = (ExistsNode) exists1n.getSinkPropagator().getSinks()[1];
    JoinNode dNode = (JoinNode) exists2n.getSinkPropagator().getSinks()[0];
    RuleTerminalNode rtn = (RuleTerminalNode) dNode.getSinkPropagator().getSinks()[0];
    wm.insert(new A());
    PathMemory pmem = (PathMemory) wm.getNodeMemory(rtn);
    assertEquals(3, pmem.getSegmentMemories().length);
    // D is in the exists segment
    assertEquals(7, pmem.getAllLinkedMaskTest());
    BetaMemory bm = (BetaMemory) wm.getNodeMemory(dNode);
    // check lazy initialization
    assertNull(bm.getSegmentMemory());
    wm.insert(new D());
    wm.flushPropagations();
    // only D can be linked in
    assertEquals(2, bm.getSegmentMemory().getAllLinkedMaskTest());
}
Also used : EvalConditionNode(org.drools.core.reteoo.EvalConditionNode) JoinNode(org.drools.core.reteoo.JoinNode) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) BetaMemory(org.drools.core.reteoo.BetaMemory) ExistsNode(org.drools.core.reteoo.ExistsNode) 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

BetaMemory (org.drools.core.reteoo.BetaMemory)49 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)25 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)24 Test (org.junit.Test)24 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)23 RightTuple (org.drools.core.reteoo.RightTuple)20 JoinNode (org.drools.core.reteoo.JoinNode)19 LeftTuple (org.drools.core.reteoo.LeftTuple)19 ArrayList (java.util.ArrayList)16 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)14 SegmentMemory (org.drools.core.reteoo.SegmentMemory)13 TupleMemory (org.drools.core.reteoo.TupleMemory)13 BetaNode (org.drools.core.reteoo.BetaNode)12 List (java.util.List)11 FastIterator (org.drools.core.util.FastIterator)11 LiaNodeMemory (org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory)10 KieBase (org.kie.api.KieBase)10 InternalFactHandle (org.drools.core.common.InternalFactHandle)9 AccumulateMemory (org.drools.core.reteoo.AccumulateNode.AccumulateMemory)9 BetaConstraints (org.drools.core.common.BetaConstraints)8