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, 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);
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 testEmptyIterator.
@Test
public void testEmptyIterator() {
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 stilton = new Cheese("stilton", 55);
final InternalFactHandle stiltonHandle = new DefaultFactHandle(2, stilton);
assertNull(map.getFirst(new LeftTupleImpl(stiltonHandle, null, true)));
}
use of org.drools.core.common.DefaultFactHandle in project drools by kiegroup.
the class XStreamXMLTest method testQueryResultsConverter.
@Test
public void testQueryResultsConverter() {
final Message msg = new Message("Hello World!");
final FactHandle msgHandle = new DefaultFactHandle(1, null, 10, 10, 20, msg);
Set<String> identifiers = new HashSet<String>() {
{
add("greeting");
}
};
ArrayList<Map<String, FactHandle>> idFactHandleMaps = new ArrayList<Map<String, FactHandle>>() {
{
add(new HashMap<String, FactHandle>() {
{
put("greeting", msgHandle);
}
});
}
};
ArrayList<Map<String, Object>> factHandleResultMap = new ArrayList<Map<String, Object>>() {
{
add(new HashMap<String, Object>() {
{
put("greeting", msg);
}
});
}
};
final String EXPECTED_XML = "<query-results>\n" + " <identifiers>\n" + " <identifier>greeting</identifier>\n" + " </identifiers>\n" + " <row>\n" + " <identifier id=\"greeting\">\n" + " <org.drools.core.runtime.help.impl.XStreamXMLTest_-Message>\n" + " <msg>Hello World!</msg>\n" + " </org.drools.core.runtime.help.impl.XStreamXMLTest_-Message>\n" + " <fact-handle external-form=\"0:1:10:10:20:null:NON_TRAIT:org.drools.core.runtime.help.impl.XStreamXMLTest$Message\"/>\n" + " </identifier>\n" + " </row>\n" + "</query-results>";
QueryResults results = new FlatQueryResults(identifiers, idFactHandleMaps, factHandleResultMap);
String xmlString = xstream.toXML(results);
Assert.assertEquals(EXPECTED_XML, xmlString);
QueryResults results2 = (QueryResults) xstream.fromXML(xmlString);
Assert.assertEquals(results, results2);
}
use of org.drools.core.common.DefaultFactHandle in project drools by kiegroup.
the class FieldIndexEntryTest method testThreeEntries.
@Test
public void testThreeEntries() {
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);
final Cheese stilton3 = new Cheese("stilton", 59);
final InternalFactHandle h3 = new DefaultFactHandle(3, stilton3);
RightTuple h1RightTuple = new RightTupleImpl(h1, null);
RightTuple h2RightTuple = new RightTupleImpl(h2, null);
RightTuple h3RightTuple = new RightTupleImpl(h3, null);
// test add
index.add(h1RightTuple);
index.add(h2RightTuple);
index.add(h3RightTuple);
assertEquals(h1, index.getFirst().getFactHandle());
assertEquals(h2, ((RightTuple) index.getFirst().getNext()).getFactHandle());
assertEquals(h3, ((RightTuple) index.getFirst().getNext().getNext()).getFactHandle());
// test get
assertEquals(h1, index.get(h1).getFactHandle());
assertEquals(h2, index.get(h2).getFactHandle());
assertEquals(h3, index.get(h3).getFactHandle());
// test removal for combinations
// remove first
index.remove(h3RightTuple);
assertEquals(h1, index.getFirst().getFactHandle());
assertEquals(h2, ((RightTuple) index.getFirst().getNext()).getFactHandle());
index.add(h3RightTuple);
index.remove(h2RightTuple);
assertEquals(h1, index.getFirst().getFactHandle());
assertEquals(h3, ((RightTuple) index.getFirst().getNext()).getFactHandle());
index.add(h2RightTuple);
index.remove(h1RightTuple);
assertEquals(h3, index.getFirst().getFactHandle());
assertEquals(h2, ((RightTuple) index.getFirst().getNext()).getFactHandle());
index.remove(index.getFirst());
// check index type does not change, as this fact is removed
stilton2.setType("cheddar");
}
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, 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);
// Test initial construction
assertNull(index.getFirst());
assertEquals("stilton".hashCode(), index.hashCode());
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());
}
Aggregations