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));
}
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());
}
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());
}
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());
}
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;
}
Aggregations