use of org.drools.core.rule.Declaration in project drools by kiegroup.
the class RightTupleIndexHashTableTest method testTwoEqualEntries.
@Test
public void testTwoEqualEntries() 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);
map.add(new RightTupleImpl(stiltonHandle1, null));
final Cheese cheddar1 = new Cheese("cheddar", 35);
final InternalFactHandle cheddarHandle1 = new DefaultFactHandle(2, cheddar1);
map.add(new RightTupleImpl(cheddarHandle1, null));
final Cheese stilton2 = new Cheese("stilton", 81);
final InternalFactHandle stiltonHandle2 = new DefaultFactHandle(3, stilton2);
map.add(new RightTupleImpl(stiltonHandle2, null));
assertEquals(3, map.size());
assertEquals(2, tablePopulationSize(map));
// Check they are correctly chained to the same FieldIndexEntry
final Cheese stilton3 = new Cheese("stilton", 89);
final InternalFactHandle stiltonHandle3 = new DefaultFactHandle(4, stilton2);
final Tuple tuple = map.getFirst(new LeftTupleImpl(stiltonHandle3, null, true));
assertSame(stiltonHandle1, tuple.getFactHandle());
assertSame(stiltonHandle2, ((RightTuple) tuple.getNext()).getFactHandle());
}
use of org.drools.core.rule.Declaration in project drools by kiegroup.
the class RightTupleIndexHashTableTest method testTwoDifferentEntries.
@Test
public void testTwoDifferentEntries() 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);
map.add(new RightTupleImpl(stiltonHandle1, null));
final Cheese cheddar1 = new Cheese("cheddar", 35);
final InternalFactHandle cheddarHandle1 = new DefaultFactHandle(2, cheddar1);
map.add(new RightTupleImpl(cheddarHandle1, null));
assertEquals(2, map.size());
assertEquals(2, tablePopulationSize(map));
final Cheese stilton2 = new Cheese("stilton", 77);
final InternalFactHandle stiltonHandle2 = new DefaultFactHandle(2, stilton2);
Tuple tuple = map.getFirst(new LeftTupleImpl(stiltonHandle2, null, true));
assertSame(stiltonHandle1, tuple.getFactHandle());
assertNull(tuple.getNext());
final Cheese cheddar2 = new Cheese("cheddar", 5);
final InternalFactHandle cheddarHandle2 = new DefaultFactHandle(2, cheddar2);
tuple = map.getFirst(new LeftTupleImpl(cheddarHandle2, null, true));
assertSame(cheddarHandle1, tuple.getFactHandle());
assertNull(tuple.getNext());
}
use of org.drools.core.rule.Declaration in project drools by kiegroup.
the class ASMConditionEvaluatorJitter method jitEvaluator.
public static ConditionEvaluator jitEvaluator(String expression, Condition condition, Declaration[] declarations, EvaluatorWrapper[] operators, ClassLoader classLoader, Tuple tuple) {
ClassGenerator generator = new ClassGenerator(getUniqueClassName(), classLoader).setInterfaces(ConditionEvaluator.class).addStaticField(ACC_PRIVATE | ACC_FINAL, "EXPRESSION", String.class, expression).addField(ACC_PRIVATE | ACC_FINAL, "declarations", Declaration[].class);
generator.addMethod(ACC_PUBLIC, "evaluate", generator.methodDescr(boolean.class, InternalFactHandle.class, InternalWorkingMemory.class, Tuple.class), new EvaluateMethodGenerator(condition, declarations, operators, tuple));
if (operators.length == 0) {
generator.addDefaultConstructor(new ClassGenerator.MethodBody() {
public void body(MethodVisitor mv) {
putFieldInThisFromRegistry("declarations", Declaration[].class, 1);
mv.visitInsn(RETURN);
}
}, Declaration[].class);
return generator.newInstance(Declaration[].class, declarations);
}
generator.addField(ACC_PRIVATE | ACC_FINAL, "operators", EvaluatorWrapper[].class);
generator.addDefaultConstructor(new ClassGenerator.MethodBody() {
public void body(MethodVisitor mv) {
putFieldInThisFromRegistry("declarations", Declaration[].class, 1);
putFieldInThisFromRegistry("operators", EvaluatorWrapper[].class, 2);
mv.visitInsn(RETURN);
}
}, Declaration[].class, EvaluatorWrapper[].class);
return generator.newInstance(Declaration[].class, declarations, EvaluatorWrapper[].class, operators);
}
use of org.drools.core.rule.Declaration in project drools by kiegroup.
the class EvaluatorHelper method valuesAsMap.
public static Map<String, Object> valuesAsMap(Object object, InternalWorkingMemory workingMemory, Tuple tuple, Declaration[] declarations) {
if (declarations.length == 0) {
return null;
}
Map<String, Object> map = new HashMap<String, Object>();
for (Declaration declaration : declarations) {
if (tuple == null) {
map.put(declaration.getBindingName(), declaration.getExtractor().getValue(workingMemory, object));
} else {
Object fact = tuple.getObject(declaration);
map.put(declaration.getBindingName(), declaration.getExtractor().getValue(workingMemory, fact != null ? fact : object));
}
}
return map;
}
use of org.drools.core.rule.Declaration in project drools by kiegroup.
the class RowAdapter method get.
public Object get(String identifier) {
if (factHandles == null) {
this.factHandles = this.tuple.toFactHandles();
}
Declaration declr = this.rule.getDeclaration(identifier);
if (declr == null) {
throw new RuntimeException("The identifier '" + identifier + "' does not exist as a bound variable for this query");
}
InternalFactHandle factHandle = getFactHandle(declr);
return declr.getValue(null, factHandle.getObject());
}
Aggregations