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 CompositeObjectSinkAdapterTest.MockBetaNode(), rule, rule.getLhs(), 0, new BuildContext(kBase)));
final Activation activation = new MockActivation(rule, 0, rule.getLhs(), tuple);
DefaultKnowledgeHelper knowledgeHelper = new org.drools.core.base.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 org.drools.core.base.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 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.reteoo.LeftTupleImpl in project drools by kiegroup.
the class FieldConstraintTest method testPredicateConstraint.
/**
* <pre>
*
* (Cheese (price ?price1 )
* (Cheese (price ?price2&:(= ?price2 (* 2 ?price1) )
*
* </pre>
*/
@Test
public void testPredicateConstraint() {
InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
final InternalReadAccessor priceExtractor = store.getReader(Cheese.class, "price");
Pattern pattern = new Pattern(0, new ClassObjectType(Cheese.class));
// Bind the extractor to a decleration
// Declarations know the pattern they derive their value form
final Declaration price1Declaration = new Declaration("price1", priceExtractor, pattern);
pattern = new Pattern(1, new ClassObjectType(Cheese.class));
// Bind the extractor to a decleration
// Declarations know the pattern they derive their value form
final Declaration price2Declaration = new Declaration("price2", priceExtractor, pattern);
final PredicateExpression evaluator = new PredicateExpression() {
private static final long serialVersionUID = 510l;
public boolean evaluate(InternalFactHandle handle, Tuple tuple, Declaration[] previousDeclarations, Declaration[] localDeclarations, WorkingMemory workingMemory, Object context) {
int price1 = previousDeclarations[0].getIntValue((InternalWorkingMemory) workingMemory, tuple.getObject(previousDeclarations[0]));
int price2 = localDeclarations[0].getIntValue((InternalWorkingMemory) workingMemory, handle.getObject());
return (price2 == (price1 * 2));
}
public Object createContext() {
return null;
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
}
public void writeExternal(ObjectOutput out) throws IOException {
}
};
final PredicateConstraint constraint1 = new PredicateConstraint(evaluator, new Declaration[] { price1Declaration }, new Declaration[] { price2Declaration });
final Cheese cheddar0 = new Cheese("cheddar", 5);
final InternalFactHandle f0 = (InternalFactHandle) ksession.insert(cheddar0);
LeftTupleImpl tuple = new LeftTupleImpl(f0, null, true);
final Cheese cheddar1 = new Cheese("cheddar", 10);
final InternalFactHandle f1 = (InternalFactHandle) ksession.insert(cheddar1);
tuple = new LeftTupleImpl(tuple, new RightTupleImpl(f1, null), null, true);
final PredicateContextEntry context = (PredicateContextEntry) constraint1.createContextEntry();
context.updateFromTuple(ksession, tuple);
assertTrue(constraint1.isAllowedCachedLeft(context, f1));
}
use of org.drools.core.reteoo.LeftTupleImpl in project drools by kiegroup.
the class LeftLeftTupleIndexHashTableIteratorTest method testLastBucketInTheTable.
@Test
public void testLastBucketInTheTable() {
// JBRULES-2574
// setup the entry array with an element in the first bucket, one
// in the middle and one in the last bucket
Entry[] entries = new Entry[10];
entries[0] = mock(TupleList.class);
entries[5] = mock(TupleList.class);
entries[9] = mock(TupleList.class);
LeftTupleImpl[] tuples = new LeftTupleImpl[] { mock(LeftTupleImpl.class), mock(LeftTupleImpl.class), mock(LeftTupleImpl.class) };
// set return values for methods
when(entries[0].getNext()).thenReturn(null);
when(((TupleList) entries[0]).getFirst()).thenReturn(tuples[0]);
when(entries[5].getNext()).thenReturn(null);
when(((TupleList) entries[5]).getFirst()).thenReturn(tuples[1]);
when(entries[9].getNext()).thenReturn(null);
when(((TupleList) entries[9]).getFirst()).thenReturn(tuples[2]);
// create the mock table for the iterator
AbstractHashTable table = mock(AbstractHashTable.class);
when(table.getTable()).thenReturn(entries);
// create the iterator
FieldIndexHashTableFullIterator iterator = new FieldIndexHashTableFullIterator(table);
// test it
assertThat(iterator.next(), sameInstance((Object) tuples[0]));
assertThat(iterator.next(), sameInstance((Object) tuples[1]));
assertThat(iterator.next(), sameInstance((Object) tuples[2]));
assertThat(iterator.next(), is((Object) null));
}
use of org.drools.core.reteoo.LeftTupleImpl in project drools by kiegroup.
the class LeftLeftTupleIndexHashTableIteratorTest method test1.
@Test
public void test1() {
BetaNodeFieldConstraint constraint0 = getConstraint("d", Operator.EQUAL, "this", Foo.class);
BetaNodeFieldConstraint[] constraints = new BetaNodeFieldConstraint[] { constraint0 };
RuleBaseConfiguration config = new RuleBaseConfiguration();
BetaConstraints betaConstraints = null;
betaConstraints = new SingleBetaConstraints(constraints, config);
BetaMemory betaMemory = betaConstraints.createBetaMemory(config, NodeTypeEnums.JoinNode);
InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
KieSession ss = kBase.newKieSession();
InternalFactHandle fh1 = (InternalFactHandle) ss.insert(new Foo("brie", 1));
InternalFactHandle fh2 = (InternalFactHandle) ss.insert(new Foo("brie", 1));
InternalFactHandle fh3 = (InternalFactHandle) ss.insert(new Foo("soda", 1));
InternalFactHandle fh4 = (InternalFactHandle) ss.insert(new Foo("soda", 1));
InternalFactHandle fh5 = (InternalFactHandle) ss.insert(new Foo("bread", 3));
InternalFactHandle fh6 = (InternalFactHandle) ss.insert(new Foo("bread", 3));
InternalFactHandle fh7 = (InternalFactHandle) ss.insert(new Foo("cream", 3));
InternalFactHandle fh8 = (InternalFactHandle) ss.insert(new Foo("gorda", 15));
InternalFactHandle fh9 = (InternalFactHandle) ss.insert(new Foo("beer", 16));
InternalFactHandle fh10 = (InternalFactHandle) ss.insert(new Foo("mars", 0));
InternalFactHandle fh11 = (InternalFactHandle) ss.insert(new Foo("snicker", 0));
InternalFactHandle fh12 = (InternalFactHandle) ss.insert(new Foo("snicker", 0));
InternalFactHandle fh13 = (InternalFactHandle) ss.insert(new Foo("snicker", 0));
betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh1, null, true));
betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh2, null, true));
betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh3, null, true));
betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh4, null, true));
betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh5, null, true));
betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh6, null, true));
betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh7, null, true));
betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh8, null, true));
betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh9, null, true));
TupleIndexHashTable hashTable = (TupleIndexHashTable) betaMemory.getLeftTupleMemory();
// can't create a 0 hashCode, so forcing
TupleList leftTupleList = new TupleList();
leftTupleList.add(new LeftTupleImpl(fh10, null, true));
hashTable.getTable()[0] = leftTupleList;
leftTupleList = new TupleList();
leftTupleList.add(new LeftTupleImpl(fh11, null, true));
leftTupleList.add(new LeftTupleImpl(fh12, null, true));
leftTupleList.add(new LeftTupleImpl(fh13, null, true));
((TupleList) hashTable.getTable()[0]).setNext(leftTupleList);
Entry[] table = hashTable.getTable();
List list = new ArrayList();
for (int i = 0; i < table.length; i++) {
if (table[i] != null) {
List entries = new ArrayList();
entries.add(i);
Entry entry = table[i];
while (entry != null) {
entries.add(entry);
entry = entry.getNext();
}
list.add(entries.toArray());
}
}
assertEquals(5, list.size());
// This tests the hashcode index allocation. If the rehash function (or any other way hashcodes are computed) changes, these numbers will change.
Object[] entries = (Object[]) list.get(0);
assertEquals(0, entries[0]);
assertEquals(3, entries.length);
entries = (Object[]) list.get(1);
assertEquals(102, entries[0]);
assertEquals(2, entries.length);
entries = (Object[]) list.get(2);
assertEquals(103, entries[0]);
assertEquals(2, entries.length);
entries = (Object[]) list.get(3);
assertEquals(115, entries[0]);
assertEquals(3, entries.length);
entries = (Object[]) list.get(4);
assertEquals(117, entries[0]);
assertEquals(3, entries.length);
// System.out.println( entries );
list = new ArrayList<LeftTupleImpl>();
Iterator it = betaMemory.getLeftTupleMemory().iterator();
for (LeftTupleImpl leftTuple = (LeftTupleImpl) it.next(); leftTuple != null; leftTuple = (LeftTupleImpl) it.next()) {
list.add(leftTuple);
}
assertEquals(13, list.size());
}
Aggregations