use of org.drools.core.reteoo.LeftTupleImpl in project drools by kiegroup.
the class KnowledgeBuilderTest method testReload.
@Test
public void testReload() throws Exception {
final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
final PackageDescr packageDescr = new PackageDescr("p1");
final RuleDescr ruleDescr = new RuleDescr("rule-1");
packageDescr.addRule(ruleDescr);
final AndDescr lhs = new AndDescr();
ruleDescr.setLhs(lhs);
packageDescr.addGlobal(new GlobalDescr("map", "java.util.Map"));
ruleDescr.setConsequence("map.put(\"value\", new Integer(1) );");
builder.addPackage(packageDescr);
InternalKnowledgePackage pkg = builder.getPackage(packageDescr.getName());
RuleImpl rule = pkg.getRule("rule-1");
assertLength(0, builder.getErrors().getErrors());
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
kBase.addGlobal("map", Map.class);
final KieSession workingMemory = kBase.newKieSession();
final HashMap map = new HashMap();
workingMemory.setGlobal("map", map);
final LeftTupleImpl tuple = new MockTuple(new HashMap());
tuple.setLeftTupleSink(new RuleTerminalNode(1, new MockBetaNode(), rule, rule.getLhs(), 0, new BuildContext(kBase, Collections.emptyList())));
final Activation activation = new MockActivation(rule, 0, rule.getLhs(), tuple);
DefaultKnowledgeHelper knowledgeHelper = new DefaultKnowledgeHelper(((StatefulKnowledgeSessionImpl) workingMemory));
knowledgeHelper.setActivation(activation);
rule.getConsequence().evaluate(knowledgeHelper, ((StatefulKnowledgeSessionImpl) workingMemory));
assertEquals(new Integer(1), map.get("value"));
ruleDescr.setConsequence("map.put(\"value\", new Integer(2) );");
pkg.removeRule(rule);
// Make sure the compiled classes are also removed
assertEquals(0, ((JavaDialectRuntimeData) pkg.getDialectRuntimeRegistry().getDialectData("java")).getStore().size());
builder.addPackage(packageDescr);
pkg = builder.getPackage(packageDescr.getName());
rule = pkg.getRule("rule-1");
knowledgeHelper = new DefaultKnowledgeHelper(((StatefulKnowledgeSessionImpl) workingMemory));
knowledgeHelper.setActivation(activation);
rule.getConsequence().evaluate(knowledgeHelper, ((StatefulKnowledgeSessionImpl) workingMemory));
assertEquals(new Integer(2), map.get("value"));
}
use of org.drools.core.reteoo.LeftTupleImpl in project drools by kiegroup.
the class IndexedHashtableIteratorTest method testCanReachAllEntriesInLastTableRowLeftTupleIndexHashTable.
@Test
public void testCanReachAllEntriesInLastTableRowLeftTupleIndexHashTable() {
// Construct a table with one row, containing one list, containing three entries.
int numEntries = 3;
TupleList[] table = new TupleList[3];
TupleList rtList = new TupleList();
table[0] = rtList;
for (int i = 0; i < numEntries; i++) {
LeftTupleImpl leftTuple = new LeftTupleImpl();
leftTuple.setMemory(rtList);
rtList.add(leftTuple);
}
rtList = new TupleList();
table[2] = rtList;
for (int i = 0; i < numEntries; i++) {
LeftTupleImpl leftTuple = new LeftTupleImpl();
leftTuple.setMemory(rtList);
rtList.add(leftTuple);
}
rtList = new TupleList();
table[2].setNext(rtList);
for (int i = 0; i < numEntries; i++) {
LeftTupleImpl leftTuple = new LeftTupleImpl();
leftTuple.setMemory(rtList);
rtList.add(leftTuple);
}
// test fast
TupleIndexHashTable.FullFastIterator iter = new TupleIndexHashTable.FullFastIterator(table);
List<LeftTupleImpl> list = new ArrayList<LeftTupleImpl>();
for (LeftTupleImpl leftTuple = (LeftTupleImpl) iter.next(null); leftTuple != null; leftTuple = (LeftTupleImpl) iter.next(leftTuple)) {
// ensure no duplicate
assertFalse(contains(list, leftTuple));
list.add(leftTuple);
}
assertEquals(numEntries * 3, list.size());
// test normal
TupleIndexHashTable lthTable = new TupleIndexHashTable();
lthTable.init(table, 3, numEntries * 3);
TupleIndexHashTable.FieldIndexHashTableFullIterator iter2 = new TupleIndexHashTable.FieldIndexHashTableFullIterator(lthTable);
list = new ArrayList<LeftTupleImpl>();
for (LeftTupleImpl leftTuple = (LeftTupleImpl) iter2.next(); leftTuple != null; leftTuple = (LeftTupleImpl) iter2.next()) {
// ensure no duplicate
assertFalse(contains(list, leftTuple));
list.add(leftTuple);
}
assertEquals(numEntries * 3, list.size());
}
use of org.drools.core.reteoo.LeftTupleImpl 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;
}
use of org.drools.core.reteoo.LeftTupleImpl in project drools by kiegroup.
the class BaseLeftTuplesBuilder method update.
public T update(Object... objects) {
this.testStagedUpdate = 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.addUpdate(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.addUpdate(leftTuple2);
}
}
return (T) this;
}
use of org.drools.core.reteoo.LeftTupleImpl in project drools by kiegroup.
the class RightTupleListTest method testEmptyIterator.
@Test
public void testEmptyIterator() {
final TupleList map = new TupleList();
final Cheese stilton1 = new Cheese("stilton", 35);
final InternalFactHandle h1 = new DefaultFactHandle(1, stilton1);
assertNull(map.getFirst(new LeftTupleImpl(h1, null, true)));
}
Aggregations