Search in sources :

Example 11 with Cheese

use of org.drools.core.test.model.Cheese 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 12 with Cheese

use of org.drools.core.test.model.Cheese 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 13 with Cheese

use of org.drools.core.test.model.Cheese in project drools by kiegroup.

the class RightTupleIndexHashTableTest method testTwoDifferentEntries.

@Test
public void testTwoDifferentEntries() 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));
    assertEquals(2, map.size());
    assertEquals(2, tablePopulationSize(map));
    final Cheese stilton2 = new Cheese("stilton", 77);
    final InternalFactHandle stiltonHandle2 = new DefaultFactHandle(2, stilton2);
    Tuple tuple = map.getFirst(new LeftTupleImpl(stiltonHandle2, null, true));
    assertSame(stiltonHandle1, tuple.getFactHandle());
    assertNull(tuple.getNext());
    final Cheese cheddar2 = new Cheese("cheddar", 5);
    final InternalFactHandle cheddarHandle2 = new DefaultFactHandle(2, cheddar2);
    tuple = map.getFirst(new LeftTupleImpl(cheddarHandle2, null, true));
    assertSame(cheddarHandle1, tuple.getFactHandle());
    assertNull(tuple.getNext());
}
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 14 with Cheese

use of org.drools.core.test.model.Cheese in project drools by kiegroup.

the class FieldConstraintTest method testLiteralConstraint.

/**
 * <pre>
 *
 *                ( Cheese (type &quot;cheddar&quot;) )
 *
 * </pre>
 *
 * This is currently the same as using a ReturnValueConstraint just that it
 * doesn't need any requiredDeclarations
 */
@Test
public void testLiteralConstraint() {
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    final ClassFieldReader extractor = store.getReader(Cheese.class, "type");
    final MvelConstraint constraint = new MvelConstraintTestUtil("type == \"cheddar\"", FieldFactory.getInstance().getFieldValue("cheddar"), extractor);
    final Cheese cheddar = new Cheese("cheddar", 5);
    final InternalFactHandle cheddarHandle = (InternalFactHandle) ksession.insert(cheddar);
    // check constraint
    assertTrue(constraint.isAllowed(cheddarHandle, ksession));
    final Cheese stilton = new Cheese("stilton", 5);
    final InternalFactHandle stiltonHandle = (InternalFactHandle) ksession.insert(stilton);
    // check constraint
    assertFalse(constraint.isAllowed(stiltonHandle, ksession));
}
Also used : ClassFieldReader(org.drools.core.base.ClassFieldReader) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) Cheese(org.drools.core.test.model.Cheese) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 15 with Cheese

use of org.drools.core.test.model.Cheese 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)

Aggregations

Cheese (org.drools.core.test.model.Cheese)35 Test (org.junit.Test)35 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)14 InternalFactHandle (org.drools.core.common.InternalFactHandle)14 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)14 ClassObjectType (org.drools.core.base.ClassObjectType)12 MvelConstraint (org.drools.core.rule.constraint.MvelConstraint)10 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)9 FieldIndex (org.drools.core.util.AbstractHashTable.FieldIndex)9 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)8 RightTupleImpl (org.drools.core.reteoo.RightTupleImpl)8 FactHandle (org.kie.api.runtime.rule.FactHandle)8 ClassFieldReader (org.drools.core.base.ClassFieldReader)7 RightTuple (org.drools.core.reteoo.RightTuple)7 MvelConstraintTestUtil (org.drools.core.rule.MvelConstraintTestUtil)7 Pattern (org.drools.core.rule.Pattern)7 Tuple (org.drools.core.spi.Tuple)7 LeftTupleImpl (org.drools.core.reteoo.LeftTupleImpl)6 Declaration (org.drools.core.rule.Declaration)6 TupleIndexHashTable (org.drools.core.util.index.TupleIndexHashTable)6