Search in sources :

Example 56 with LeftTuple

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

the class AddRemoveRulesTest method testRemoveChildLeftTupleThatWasMiddle.

@Test
public void testRemoveChildLeftTupleThatWasMiddle() {
    final String[] rules = getRules3Pattern();
    final KieSession kieSession = TestUtil.createSession(rules);
    try {
        final List resultsList = new ArrayList();
        kieSession.setGlobal("list", resultsList);
        TestUtil.insertFacts(kieSession, 3);
        kieSession.fireAllRules();
        Assertions.assertThat(resultsList).containsOnly(TestUtil.RULE1_NAME, TestUtil.RULE2_NAME, TestUtil.RULE3_NAME);
        resultsList.clear();
        TestUtil.removeRules(kieSession, TestUtil.RULES_PACKAGE_NAME, TestUtil.RULE2_NAME);
        kieSession.fireAllRules();
        final Map<String, Rule> rulesMap = rulestoMap(kieSession.getKieBase());
        final InternalFactHandle fh1 = (InternalFactHandle) kieSession.getFactHandle(3);
        final LeftTuple lt = fh1.getFirstLeftTuple().getFirstChild();
        assertSame(lt, fh1.getFirstLeftTuple().getLastChild());
        assertEquals(1, lt.getTupleSink().getAssociatedRuleSize());
        assertTrue(lt.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE1_NAME)));
        final LeftTuple peer = lt.getPeer();
        assertEquals(1, peer.getTupleSink().getAssociatedRuleSize());
        assertTrue(peer.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE3_NAME)));
    } finally {
        kieSession.dispose();
    }
}
Also used : ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Rule(org.kie.api.definition.rule.Rule) InternalFactHandle(org.drools.core.common.InternalFactHandle) LeftTuple(org.drools.core.reteoo.LeftTuple) Test(org.junit.Test)

Example 57 with LeftTuple

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

the class AddRemoveRulesTest method testRemoveChildLeftTupleThatWasFirst.

@Test
public void testRemoveChildLeftTupleThatWasFirst() {
    final String[] rules = getRules3Pattern();
    final KieSession kieSession = TestUtil.createSession(rules[0], rules[1]);
    try {
        final List resultsList = new ArrayList();
        kieSession.setGlobal("list", resultsList);
        TestUtil.insertFacts(kieSession, 3);
        kieSession.fireAllRules();
        Assertions.assertThat(resultsList).containsOnly(TestUtil.RULE1_NAME, TestUtil.RULE2_NAME);
        resultsList.clear();
        TestUtil.removeRules(kieSession, TestUtil.RULES_PACKAGE_NAME, TestUtil.RULE1_NAME);
        kieSession.fireAllRules();
        final Map<String, Rule> rulesMap = rulestoMap(kieSession.getKieBase());
        final InternalFactHandle fh1 = (InternalFactHandle) kieSession.getFactHandle(3);
        final LeftTuple lt = fh1.getFirstLeftTuple().getFirstChild().getFirstChild();
        assertSame(lt, fh1.getFirstLeftTuple().getFirstChild().getLastChild());
        assertNull(lt.getPeer());
        assertEquals(1, lt.getTupleSink().getAssociatedRuleSize());
        assertTrue(lt.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE2_NAME)));
    } finally {
        kieSession.dispose();
    }
}
Also used : ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Rule(org.kie.api.definition.rule.Rule) InternalFactHandle(org.drools.core.common.InternalFactHandle) LeftTuple(org.drools.core.reteoo.LeftTuple) Test(org.junit.Test)

Example 58 with LeftTuple

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

the class AddRemoveRulesTest method testRemoveChildLeftTupleThatWasLast.

@Test
public void testRemoveChildLeftTupleThatWasLast() {
    final String[] rules = getRules3Pattern();
    final KieSession kieSession = TestUtil.createSession(rules[0], rules[1]);
    try {
        final List resultsList = new ArrayList();
        kieSession.setGlobal("list", resultsList);
        TestUtil.insertFacts(kieSession, 3);
        kieSession.fireAllRules();
        Assertions.assertThat(resultsList).containsOnly(TestUtil.RULE1_NAME, TestUtil.RULE2_NAME);
        resultsList.clear();
        TestUtil.removeRules(kieSession, TestUtil.RULES_PACKAGE_NAME, TestUtil.RULE2_NAME);
        kieSession.fireAllRules();
        final Map<String, Rule> rulesMap = rulestoMap(kieSession.getKieBase());
        final InternalFactHandle fh1 = (InternalFactHandle) kieSession.getFactHandle(3);
        final LeftTuple lt = fh1.getFirstLeftTuple().getFirstChild().getFirstChild();
        assertSame(lt, fh1.getFirstLeftTuple().getFirstChild().getLastChild());
        assertNull(lt.getPeer());
        assertEquals(1, lt.getTupleSink().getAssociatedRuleSize());
        assertTrue(lt.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE1_NAME)));
    } finally {
        kieSession.dispose();
    }
}
Also used : ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Rule(org.kie.api.definition.rule.Rule) InternalFactHandle(org.drools.core.common.InternalFactHandle) LeftTuple(org.drools.core.reteoo.LeftTuple) Test(org.junit.Test)

Example 59 with LeftTuple

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

the class AddRemoveRulesTest method testRemoveChildLeftTupleThatWasFirstWithMultipleData.

@Test
public void testRemoveChildLeftTupleThatWasFirstWithMultipleData() {
    final String[] rules = getRules3Pattern();
    final KieSession kieSession = TestUtil.createSession(rules[0], rules[1]);
    try {
        final List resultsList = new ArrayList();
        kieSession.setGlobal("list", resultsList);
        TestUtil.insertFacts(kieSession, 3, 4, 5);
        kieSession.fireAllRules();
        Assertions.assertThat(resultsList).containsOnly(TestUtil.RULE1_NAME, TestUtil.RULE2_NAME);
        resultsList.clear();
        TestUtil.removeRules(kieSession, TestUtil.RULES_PACKAGE_NAME, TestUtil.RULE1_NAME);
        kieSession.fireAllRules();
        final Map<String, Rule> rulesMap = rulestoMap(kieSession.getKieBase());
        final InternalFactHandle fh1 = (InternalFactHandle) kieSession.getFactHandle(3);
        final InternalFactHandle fh2 = (InternalFactHandle) kieSession.getFactHandle(4);
        final InternalFactHandle fh3 = (InternalFactHandle) kieSession.getFactHandle(5);
        final LeftTuple lt1 = fh1.getFirstLeftTuple();
        final LeftTuple lt1_1 = lt1.getFirstChild();
        final LeftTuple lt1_2 = lt1_1.getHandleNext();
        final LeftTuple lt1_3 = lt1_2.getHandleNext();
        assertNotNull(lt1_1);
        assertNotNull(lt1_2);
        assertNotNull(lt1_3);
        assertSame(lt1_3, lt1.getLastChild());
        assertSame(lt1_2, lt1_3.getHandlePrevious());
        assertSame(lt1_1, lt1_2.getHandlePrevious());
        assertEquals(1, lt1_1.getTupleSink().getAssociatedRuleSize());
        assertTrue(lt1_1.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE2_NAME)));
        assertNull(lt1_1.getPeer());
        assertEquals(1, lt1_2.getTupleSink().getAssociatedRuleSize());
        assertTrue(lt1_2.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE2_NAME)));
        assertNull(lt1_2.getPeer());
        assertEquals(1, lt1_3.getTupleSink().getAssociatedRuleSize());
        assertTrue(lt1_3.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE2_NAME)));
        assertNull(lt1_3.getPeer());
        final RightTuple rt1 = fh3.getFirstRightTuple();
        final LeftTuple rt1_1 = rt1.getLastChild();
        assertSame(lt1_1, rt1_1);
        final LeftTuple rt1_2 = rt1_1.getRightParentPrevious();
        final LeftTuple rt1_3 = rt1_2.getRightParentPrevious();
        assertNotNull(rt1_1);
        assertNotNull(rt1_2);
        assertNotNull(rt1_3);
        assertSame(rt1_2, rt1_3.getRightParentNext());
        assertSame(rt1_1, rt1_2.getRightParentNext());
        assertEquals(1, rt1_1.getTupleSink().getAssociatedRuleSize());
        assertTrue(rt1_1.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE2_NAME)));
        assertNull(rt1_1.getPeer());
        assertEquals(1, rt1_2.getTupleSink().getAssociatedRuleSize());
        assertTrue(rt1_2.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE2_NAME)));
        assertNull(rt1_2.getPeer());
        assertEquals(1, rt1_3.getTupleSink().getAssociatedRuleSize());
        assertTrue(rt1_3.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE2_NAME)));
        assertNull(rt1_3.getPeer());
    } finally {
        kieSession.dispose();
    }
}
Also used : ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Rule(org.kie.api.definition.rule.Rule) InternalFactHandle(org.drools.core.common.InternalFactHandle) LeftTuple(org.drools.core.reteoo.LeftTuple) RightTuple(org.drools.core.reteoo.RightTuple) Test(org.junit.Test)

Example 60 with LeftTuple

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

the class BaseLeftTuplesBuilder method insert.

public T insert(Object... objects) {
    this.testStagedInsert = true;
    if (objects == null) {
        objects = new Object[0];
    }
    for (int i = 0; i < objects.length; i++) {
        if (!(objects[i] instanceof Pair)) {
            Object o1 = objects[i];
            InternalFactHandle fh1 = (InternalFactHandle) wm.getFactHandle(o1);
            LeftTuple leftTuple = new LeftTupleImpl(fh1, sink, true);
            leftTuples.addInsert(leftTuple);
        } else {
            Pair p = (Pair) objects[i];
            InternalFactHandle fh1 = (InternalFactHandle) wm.getFactHandle(p.getO1());
            LeftTuple leftTuple1 = new LeftTupleImpl(fh1, sink, true);
            InternalFactHandle fh2 = (InternalFactHandle) wm.getFactHandle(p.getO2());
            LeftTuple leftTuple2 = sink.createLeftTuple(leftTuple1, new RightTupleImpl(fh2), sink);
            leftTuples.addInsert(leftTuple2);
        }
    }
    return (T) this;
}
Also used : LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) RightTupleImpl(org.drools.core.reteoo.RightTupleImpl) InternalFactHandle(org.drools.core.common.InternalFactHandle) LeftTuple(org.drools.core.reteoo.LeftTuple)

Aggregations

LeftTuple (org.drools.core.reteoo.LeftTuple)125 RightTuple (org.drools.core.reteoo.RightTuple)41 TupleMemory (org.drools.core.reteoo.TupleMemory)37 InternalFactHandle (org.drools.core.common.InternalFactHandle)34 FastIterator (org.drools.core.util.FastIterator)22 BetaConstraints (org.drools.core.common.BetaConstraints)21 BetaMemory (org.drools.core.reteoo.BetaMemory)21 ContextEntry (org.drools.core.rule.ContextEntry)20 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)18 PhreakJoinNode.updateChildLeftTuple (org.drools.core.phreak.PhreakJoinNode.updateChildLeftTuple)16 AccumulateContext (org.drools.core.reteoo.AccumulateNode.AccumulateContext)13 SegmentMemory (org.drools.core.reteoo.SegmentMemory)13 Test (org.junit.Test)13 KieSession (org.kie.api.runtime.KieSession)13 Tuple (org.drools.core.spi.Tuple)12 WorkingMemory (org.drools.core.WorkingMemory)11 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)11 FromMemory (org.drools.core.reteoo.FromNode.FromMemory)10 AccumulateMemory (org.drools.core.reteoo.AccumulateNode.AccumulateMemory)9 ArrayList (java.util.ArrayList)8