Search in sources :

Example 1 with RightTupleImpl

use of org.drools.core.reteoo.RightTupleImpl 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)

Example 2 with RightTupleImpl

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

the class BaseLeftTuplesBuilder method delete.

public T delete(Object... objects) {
    this.testStagedDelete = 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.addDelete(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.addDelete(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)

Example 3 with RightTupleImpl

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

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

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

the class RightTupleIndexHashTableTest method testTwoDifferentEntriesSameHashCode.

@Test
public void testTwoDifferentEntriesSameHashCode() throws Exception {
    final InternalReadAccessor extractor = store.getReader(TestClass.class, "object");
    final Pattern pattern = new Pattern(0, new ClassObjectType(TestClass.class));
    final Declaration declaration = new Declaration("theObject", extractor, pattern);
    final FieldIndex fieldIndex = new FieldIndex(extractor, declaration, MvelConstraint.INDEX_EVALUATOR);
    final TupleIndexHashTable map = new TupleIndexHashTable(new FieldIndex[] { fieldIndex }, false);
    final TestClass c1 = new TestClass(0, new TestClass(20, "stilton"));
    final InternalFactHandle ch1 = new DefaultFactHandle(1, c1);
    map.add(new RightTupleImpl(ch1, null));
    final TestClass c2 = new TestClass(0, new TestClass(20, "cheddar"));
    final InternalFactHandle ch2 = new DefaultFactHandle(2, c2);
    map.add(new RightTupleImpl(ch2, null));
    // same hashcode, but different values, so it should result in  a size of 2
    assertEquals(2, map.size());
    // however both are in the same table bucket
    assertEquals(1, tablePopulationSize(map));
    // this table bucket will have two FieldIndexEntries, as they are actually two different values
    Entry[] entries = getEntries(map);
    assertEquals(1, entries.length);
    TupleList list = (TupleList) entries[0];
    assertSame(ch2, list.getFirst().getFactHandle());
    assertNull(list.getFirst().getNext());
    assertSame(ch1, list.getNext().getFirst().getFactHandle());
    assertNull(list.getNext().getFirst().getNext());
    assertNull(list.getNext().getNext());
}
Also used : Pattern(org.drools.core.rule.Pattern) FieldIndex(org.drools.core.util.AbstractHashTable.FieldIndex) ClassObjectType(org.drools.core.base.ClassObjectType) RightTupleImpl(org.drools.core.reteoo.RightTupleImpl) TupleIndexHashTable(org.drools.core.util.index.TupleIndexHashTable) TupleList(org.drools.core.util.index.TupleList) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) Declaration(org.drools.core.rule.Declaration) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Aggregations

RightTupleImpl (org.drools.core.reteoo.RightTupleImpl)32 InternalFactHandle (org.drools.core.common.InternalFactHandle)30 RightTuple (org.drools.core.reteoo.RightTuple)21 Test (org.junit.Test)21 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)16 FieldIndex (org.drools.core.util.AbstractHashTable.FieldIndex)16 LeftTupleImpl (org.drools.core.reteoo.LeftTupleImpl)15 Tuple (org.drools.core.spi.Tuple)15 Cheese (org.drools.core.test.model.Cheese)15 Declaration (org.drools.core.rule.Declaration)14 TupleIndexHashTable (org.drools.core.util.index.TupleIndexHashTable)13 ClassObjectType (org.drools.core.base.ClassObjectType)12 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)12 Pattern (org.drools.core.rule.Pattern)11 TupleList (org.drools.core.util.index.TupleList)11 LeftTuple (org.drools.core.reteoo.LeftTuple)6 SingleIndex (org.drools.core.util.AbstractHashTable.SingleIndex)6 ArrayList (java.util.ArrayList)5 BetaMemory (org.drools.core.reteoo.BetaMemory)5 PhreakPropagationContext (org.drools.core.common.PhreakPropagationContext)4