Search in sources :

Example 31 with RightTuple

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

the class RightTupleIndexHashTableIteratorTest method testLastBucketInTheTable.

@Test
public void testLastBucketInTheTable() {
    // JBRULES-2574
    // setup the entry array with an element in the first bucket, one
    // in the middle and one in the last bucket
    Entry[] entries = new Entry[10];
    entries[0] = mock(TupleList.class);
    entries[5] = mock(TupleList.class);
    entries[9] = mock(TupleList.class);
    RightTuple[] tuples = new RightTuple[] { mock(RightTuple.class), mock(RightTuple.class), mock(RightTuple.class) };
    // set return values for methods
    when(entries[0].getNext()).thenReturn(null);
    when(((TupleList) entries[0]).getFirst()).thenReturn(tuples[0]);
    when(entries[5].getNext()).thenReturn(null);
    when(((TupleList) entries[5]).getFirst()).thenReturn(tuples[1]);
    when(entries[9].getNext()).thenReturn(null);
    when(((TupleList) entries[9]).getFirst()).thenReturn(tuples[2]);
    // create the mock table for the iterator
    AbstractHashTable table = mock(AbstractHashTable.class);
    when(table.getTable()).thenReturn(entries);
    // create the iterator
    FieldIndexHashTableFullIterator iterator = new FieldIndexHashTableFullIterator(table);
    // test it
    assertThat(iterator.next(), sameInstance((Object) tuples[0]));
    assertThat(iterator.next(), sameInstance((Object) tuples[1]));
    assertThat(iterator.next(), sameInstance((Object) tuples[2]));
    assertThat(iterator.next(), is((Object) null));
}
Also used : TupleList(org.drools.core.util.index.TupleList) FieldIndexHashTableFullIterator(org.drools.core.util.index.TupleIndexHashTable.FieldIndexHashTableFullIterator) RightTuple(org.drools.core.reteoo.RightTuple) Test(org.junit.Test)

Example 32 with RightTuple

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

the class RightTupleIndexHashTableTest method testSingleEntry.

@Test
public void testSingleEntry() throws Exception {
    final InternalReadAccessor extractor = store.getReader(Cheese.class, "type");
    final Pattern pattern = new Pattern(0, new ClassObjectType(Cheese.class));
    final Declaration declaration = new Declaration("typeOfCheese", extractor, pattern);
    final FieldIndex fieldIndex = new FieldIndex(extractor, declaration, MvelConstraint.INDEX_EVALUATOR);
    final TupleIndexHashTable map = new TupleIndexHashTable(new FieldIndex[] { fieldIndex }, false);
    final Cheese cheddar = new Cheese("cheddar", 10);
    final InternalFactHandle cheddarHandle1 = new DefaultFactHandle(0, cheddar);
    assertEquals(0, map.size());
    assertNull(map.getFirst(new LeftTupleImpl(cheddarHandle1, null, true)));
    final Cheese stilton1 = new Cheese("stilton", 35);
    RightTuple stiltonRighTuple = new RightTupleImpl(new DefaultFactHandle(1, stilton1), null);
    map.add(stiltonRighTuple);
    assertEquals(1, map.size());
    assertEquals(1, tablePopulationSize(map));
    final Cheese stilton2 = new Cheese("stilton", 80);
    final InternalFactHandle stiltonHandle2 = new DefaultFactHandle(2, stilton2);
    final Tuple tuple = map.getFirst(new LeftTupleImpl(stiltonHandle2, null, true));
    assertSame(stiltonRighTuple.getFactHandle(), tuple.getFactHandle());
    assertNull(tuple.getNext());
}
Also used : Pattern(org.drools.core.rule.Pattern) FieldIndex(org.drools.core.util.AbstractHashTable.FieldIndex) ClassObjectType(org.drools.core.base.ClassObjectType) Cheese(org.drools.core.test.model.Cheese) RightTupleImpl(org.drools.core.reteoo.RightTupleImpl) TupleIndexHashTable(org.drools.core.util.index.TupleIndexHashTable) RightTuple(org.drools.core.reteoo.RightTuple) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) Declaration(org.drools.core.rule.Declaration) InternalFactHandle(org.drools.core.common.InternalFactHandle) RightTuple(org.drools.core.reteoo.RightTuple) Tuple(org.drools.core.spi.Tuple) Test(org.junit.Test)

Example 33 with RightTuple

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

the class RightTupleIndexHashTableTest method testTwoEqualEntries.

@Test
public void testTwoEqualEntries() throws Exception {
    final InternalReadAccessor extractor = store.getReader(Cheese.class, "type");
    final Pattern pattern = new Pattern(0, new ClassObjectType(Cheese.class));
    final Declaration declaration = new Declaration("typeOfCheese", extractor, pattern);
    final FieldIndex fieldIndex = new FieldIndex(extractor, declaration, MvelConstraint.INDEX_EVALUATOR);
    final TupleIndexHashTable map = new TupleIndexHashTable(new FieldIndex[] { fieldIndex }, false);
    assertEquals(0, map.size());
    final Cheese stilton1 = new Cheese("stilton", 35);
    final InternalFactHandle stiltonHandle1 = new DefaultFactHandle(1, stilton1);
    map.add(new RightTupleImpl(stiltonHandle1, null));
    final Cheese cheddar1 = new Cheese("cheddar", 35);
    final InternalFactHandle cheddarHandle1 = new DefaultFactHandle(2, cheddar1);
    map.add(new RightTupleImpl(cheddarHandle1, null));
    final Cheese stilton2 = new Cheese("stilton", 81);
    final InternalFactHandle stiltonHandle2 = new DefaultFactHandle(3, stilton2);
    map.add(new RightTupleImpl(stiltonHandle2, null));
    assertEquals(3, map.size());
    assertEquals(2, tablePopulationSize(map));
    // Check they are correctly chained to the same FieldIndexEntry
    final Cheese stilton3 = new Cheese("stilton", 89);
    final InternalFactHandle stiltonHandle3 = new DefaultFactHandle(4, stilton2);
    final Tuple tuple = map.getFirst(new LeftTupleImpl(stiltonHandle3, null, true));
    assertSame(stiltonHandle1, tuple.getFactHandle());
    assertSame(stiltonHandle2, ((RightTuple) tuple.getNext()).getFactHandle());
}
Also used : Pattern(org.drools.core.rule.Pattern) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) FieldIndex(org.drools.core.util.AbstractHashTable.FieldIndex) ClassObjectType(org.drools.core.base.ClassObjectType) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) Cheese(org.drools.core.test.model.Cheese) Declaration(org.drools.core.rule.Declaration) RightTupleImpl(org.drools.core.reteoo.RightTupleImpl) TupleIndexHashTable(org.drools.core.util.index.TupleIndexHashTable) InternalFactHandle(org.drools.core.common.InternalFactHandle) RightTuple(org.drools.core.reteoo.RightTuple) Tuple(org.drools.core.spi.Tuple) Test(org.junit.Test)

Example 34 with RightTuple

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

the class FieldIndexEntryTest method testTwoEntries.

@Test
public void testTwoEntries() {
    final ClassFieldReader extractor = store.getReader(Cheese.class, "type");
    final FieldIndex fieldIndex = new FieldIndex(extractor, null, MvelConstraint.INDEX_EVALUATOR);
    final SingleIndex singleIndex = new SingleIndex(new FieldIndex[] { fieldIndex }, 1);
    Tuple tuple = new RightTupleImpl(new DefaultFactHandle(1, new Cheese("stilton", 10)));
    final TupleList index = new AbstractHashTable.SingleIndexTupleList(singleIndex, tuple, "stilton".hashCode(), false);
    final Cheese stilton1 = new Cheese("stilton", 35);
    final InternalFactHandle h1 = new DefaultFactHandle(1, stilton1);
    final Cheese stilton2 = new Cheese("stilton", 59);
    final InternalFactHandle h2 = new DefaultFactHandle(2, stilton2);
    RightTuple h1RightTuple = new RightTupleImpl(h1, null);
    RightTuple h2RightTuple = new RightTupleImpl(h2, null);
    // test add
    index.add(h1RightTuple);
    index.add(h2RightTuple);
    assertEquals(h1, index.getFirst().getFactHandle());
    assertEquals(h2, ((RightTuple) index.getFirst().getNext()).getFactHandle());
    // test get
    assertEquals(h1, index.get(h1).getFactHandle());
    assertEquals(h2, index.get(h2).getFactHandle());
    // test removal for combinations
    // remove first
    index.remove(h2RightTuple);
    assertEquals(h1RightTuple.getFactHandle(), index.getFirst().getFactHandle());
    // remove second
    index.add(h2RightTuple);
    index.remove(h1RightTuple);
    assertEquals(h2RightTuple.getFactHandle(), index.getFirst().getFactHandle());
    // check index type does not change, as this fact is removed
    stilton1.setType("cheddar");
}
Also used : TupleList(org.drools.core.util.index.TupleList) SingleIndex(org.drools.core.util.AbstractHashTable.SingleIndex) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) FieldIndex(org.drools.core.util.AbstractHashTable.FieldIndex) ClassFieldReader(org.drools.core.base.ClassFieldReader) Cheese(org.drools.core.test.model.Cheese) RightTupleImpl(org.drools.core.reteoo.RightTupleImpl) InternalFactHandle(org.drools.core.common.InternalFactHandle) RightTuple(org.drools.core.reteoo.RightTuple) RightTuple(org.drools.core.reteoo.RightTuple) Tuple(org.drools.core.spi.Tuple) Test(org.junit.Test)

Example 35 with RightTuple

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

the class AddRemoveRulesTest method testRemoveChildLeftTupleThatWasLastWithMultipleData.

@Test
public void testRemoveChildLeftTupleThatWasLastWithMultipleData() {
    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.RULE2_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.RULE1_NAME)));
        assertNull(lt1_1.getPeer());
        assertEquals(1, lt1_2.getTupleSink().getAssociatedRuleSize());
        assertTrue(lt1_2.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE1_NAME)));
        assertNull(lt1_2.getPeer());
        assertEquals(1, lt1_3.getTupleSink().getAssociatedRuleSize());
        assertTrue(lt1_3.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE1_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.RULE1_NAME)));
        assertNull(rt1_1.getPeer());
        assertEquals(1, rt1_2.getTupleSink().getAssociatedRuleSize());
        assertTrue(rt1_2.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE1_NAME)));
        assertNull(rt1_2.getPeer());
        assertEquals(1, rt1_3.getTupleSink().getAssociatedRuleSize());
        assertTrue(rt1_3.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE1_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)

Aggregations

RightTuple (org.drools.core.reteoo.RightTuple)60 LeftTuple (org.drools.core.reteoo.LeftTuple)41 TupleMemory (org.drools.core.reteoo.TupleMemory)30 FastIterator (org.drools.core.util.FastIterator)26 InternalFactHandle (org.drools.core.common.InternalFactHandle)20 BetaMemory (org.drools.core.reteoo.BetaMemory)20 BetaConstraints (org.drools.core.common.BetaConstraints)19 ContextEntry (org.drools.core.rule.ContextEntry)18 PhreakJoinNode.updateChildLeftTuple (org.drools.core.phreak.PhreakJoinNode.updateChildLeftTuple)13 Test (org.junit.Test)13 Tuple (org.drools.core.spi.Tuple)11 RightTupleImpl (org.drools.core.reteoo.RightTupleImpl)10 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)9 AccumulateContext (org.drools.core.reteoo.AccumulateNode.AccumulateContext)8 BetaNode (org.drools.core.reteoo.BetaNode)7 FromMemory (org.drools.core.reteoo.FromNode.FromMemory)7 ArrayList (java.util.ArrayList)6 AccumulateMemory (org.drools.core.reteoo.AccumulateNode.AccumulateMemory)6 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)6 Accumulate (org.drools.core.rule.Accumulate)6