Search in sources :

Example 86 with DefaultFactHandle

use of org.drools.core.common.DefaultFactHandle in project drools by kiegroup.

the class RightTupleIndexHashTableTest method testRemove.

@Test
public void testRemove() 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);
    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);
    RightTuple stiltonRightTuple1 = new RightTupleImpl(stiltonHandle1, null);
    map.add(stiltonRightTuple1);
    final Cheese cheddar1 = new Cheese("cheddar", 35);
    final InternalFactHandle cheddarHandle1 = new DefaultFactHandle(2, cheddar1);
    RightTuple cheddarRightTuple1 = new RightTupleImpl(cheddarHandle1, null);
    map.add(cheddarRightTuple1);
    final Cheese stilton2 = new Cheese("stilton", 81);
    final InternalFactHandle stiltonHandle2 = new DefaultFactHandle(3, stilton2);
    RightTuple stiltonRightTuple2 = new RightTupleImpl(stiltonHandle2, null);
    map.add(stiltonRightTuple2);
    assertEquals(3, map.size());
    assertEquals(2, tablePopulationSize(map));
    // cheddar is in its own bucket, which should be removed once empty. We cannot have
    // empty FieldIndexEntries in the Map, as they get their value  from the first FactEntry.
    map.remove(cheddarRightTuple1);
    assertEquals(2, map.size());
    assertEquals(1, tablePopulationSize(map));
    // We remove t he stiltonHandle2, but there is still  one more stilton, so size  should be the same
    map.remove(stiltonRightTuple2);
    assertEquals(1, map.size());
    assertEquals(1, tablePopulationSize(map));
    // No more stiltons, so the table should be empty
    map.remove(stiltonRightTuple1);
    assertEquals(0, map.size());
    assertEquals(0, tablePopulationSize(map));
}
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) 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) Test(org.junit.Test)

Example 87 with DefaultFactHandle

use of org.drools.core.common.DefaultFactHandle 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);
    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) Entry(org.drools.core.util.Entry) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) Declaration(org.drools.core.rule.Declaration) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Example 88 with DefaultFactHandle

use of org.drools.core.common.DefaultFactHandle in project drools by kiegroup.

the class FieldIndexEntryTest method testSingleEntry.

@Test
public void testSingleEntry() {
    final ClassFieldReader extractor = store.getReader(Cheese.class, "type");
    final FieldIndex fieldIndex = new FieldIndex(extractor, new Declaration("id", extractor, null));
    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.IndexTupleList(singleIndex, new AbstractHashTable.SingleHashEntry("stilton".hashCode(), "stilton"));
    // Test initial construction
    assertNull(index.getFirst());
    final Cheese stilton1 = new Cheese("stilton", 35);
    final InternalFactHandle h1 = new DefaultFactHandle(1, stilton1);
    // test add
    RightTuple h1RightTuple = new RightTupleImpl(h1, null);
    index.add(h1RightTuple);
    final Tuple entry1 = index.getFirst();
    assertSame(h1, entry1.getFactHandle());
    assertNull(entry1.getNext());
    assertSame(entry1, index.get(h1));
    // test get
    final Tuple entry2 = index.get(new RightTupleImpl(h1, null));
    assertSame(entry1, entry2);
    // test remove
    index.remove(h1RightTuple);
    assertNull(index.getFirst());
}
Also used : FieldIndex(org.drools.core.util.AbstractHashTable.FieldIndex) Cheese(org.drools.core.test.model.Cheese) RightTupleImpl(org.drools.core.reteoo.RightTupleImpl) RightTuple(org.drools.core.reteoo.RightTuple) TupleList(org.drools.core.util.index.TupleList) SingleIndex(org.drools.core.util.AbstractHashTable.SingleIndex) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) AbstractHashTable(org.drools.core.util.AbstractHashTable) ClassFieldReader(org.drools.mvel.accessors.ClassFieldReader) 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 89 with DefaultFactHandle

use of org.drools.core.common.DefaultFactHandle in project drools by kiegroup.

the class ReloadSessionTest method reloadKnowledgeSessionTest.

@Test
public void reloadKnowledgeSessionTest() {
    // Initialize drools environment stuff
    Environment env = createEnvironment();
    KieBase kbase = initializeKnowledgeBase(simpleRule);
    StatefulKnowledgeSession commandKSession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
    assertTrue("There should be NO facts present in a new (empty) knowledge session.", commandKSession.getFactHandles().isEmpty());
    // Persist a facthandle to the database
    Integer integerFact = (new Random()).nextInt(Integer.MAX_VALUE - 1) + 1;
    commandKSession.insert(integerFact);
    // At this point in the code, the fact has been persisted to the database
    // (within a transaction via the PersistableRunner)
    Collection<FactHandle> factHandles = commandKSession.getFactHandles();
    assertTrue("At least one fact should have been inserted by the ksession.insert() method above.", !factHandles.isEmpty());
    FactHandle origFactHandle = factHandles.iterator().next();
    assertTrue("The stored fact should contain the same number as the value inserted (but does not).", Integer.parseInt(((DefaultFactHandle) origFactHandle).getObject().toString()) == integerFact.intValue());
    // Save the sessionInfo id in order to retrieve it later
    long sessionInfoId = commandKSession.getIdentifier();
    // Clean up the session, environment, etc.
    PersistenceContextManager pcm = (PersistenceContextManager) commandKSession.getEnvironment().get(EnvironmentName.PERSISTENCE_CONTEXT_MANAGER);
    commandKSession.dispose();
    pcm.dispose();
    emf.close();
    // Reload session from the database
    emf = Persistence.createEntityManagerFactory(DROOLS_PERSISTENCE_UNIT_NAME);
    context.put(ENTITY_MANAGER_FACTORY, emf);
    env = createEnvironment();
    // Re-initialize the knowledge session:
    StatefulKnowledgeSession newCommandKSession = JPAKnowledgeService.loadStatefulKnowledgeSession(sessionInfoId, kbase, null, env);
    // Test that the session has been successfully reinitialized
    factHandles = newCommandKSession.getFactHandles();
    assertTrue("At least one fact should have been persisted by the ksession.insert above.", !factHandles.isEmpty() && factHandles.size() == 1);
    FactHandle retrievedFactHandle = factHandles.iterator().next();
    assertTrue("If the retrieved and original FactHandle object are the same, then the knowledge session has NOT been reloaded!", origFactHandle != retrievedFactHandle);
    assertTrue("The retrieved fact should contain the same info as the original (but does not).", Integer.parseInt(((DefaultFactHandle) retrievedFactHandle).getObject().toString()) == integerFact.intValue());
    // Test to see if the (retrieved) facts can be processed
    ArrayList<Object> list = new ArrayList<Object>();
    newCommandKSession.setGlobal("list", list);
    newCommandKSession.fireAllRules();
    assertEquals(1, list.size());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) ArrayList(java.util.ArrayList) PersistenceContextManager(org.drools.persistence.api.PersistenceContextManager) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) Random(java.util.Random) KieBase(org.kie.api.KieBase) Environment(org.kie.api.runtime.Environment) Test(org.junit.Test)

Example 90 with DefaultFactHandle

use of org.drools.core.common.DefaultFactHandle in project drools by kiegroup.

the class DMNAlphaNetworkTemplate method evaluate.

@Override
public Object evaluate(EvaluationContext evaluationContext, DecisionTable decisionTable) {
    // Clean previous results
    Results results = alphaNetworkEvaluationContext.getResultCollector();
    results.clearResults();
    // init CompiledNetwork with object needed for results,
    compiledNetwork.init(alphaNetworkEvaluationContext);
    // create lambda constraints and results
    compiledNetwork.initConstraintsResults();
    // Fire rete network
    compiledNetwork.propagateAssertObject(new DefaultFactHandle(getOrCreatePropertyEvaluator(evaluationContext)), null, null);
    // Find result with Hit Policy applied
    Object result = results.applyHitPolicy(evaluationContext, hitPolicy, decisionTable);
    return result;
}
Also used : DefaultFactHandle(org.drools.core.common.DefaultFactHandle) Results(org.kie.dmn.core.compiler.alphanetbased.Results) Override(java.lang.Override)

Aggregations

DefaultFactHandle (org.drools.core.common.DefaultFactHandle)92 Test (org.junit.Test)78 InternalFactHandle (org.drools.core.common.InternalFactHandle)30 FactHandle (org.kie.api.runtime.rule.FactHandle)29 Cheese (org.drools.core.test.model.Cheese)24 ArrayList (java.util.ArrayList)23 ClassObjectType (org.drools.core.base.ClassObjectType)22 KieSession (org.kie.api.runtime.KieSession)19 FieldIndex (org.drools.core.util.AbstractHashTable.FieldIndex)18 RightTupleImpl (org.drools.core.reteoo.RightTupleImpl)16 Declaration (org.drools.core.rule.Declaration)15 RightTuple (org.drools.core.reteoo.RightTuple)14 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)14 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)13 StatefulKnowledgeSessionImpl (org.drools.kiesession.session.StatefulKnowledgeSessionImpl)13 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)12 Pattern (org.drools.core.rule.Pattern)12 Tuple (org.drools.core.spi.Tuple)12 TupleIndexHashTable (org.drools.core.util.index.TupleIndexHashTable)12 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)11