use of org.drools.core.spi.EvalExpression in project drools by kiegroup.
the class EvalGenerator method generate.
public static void generate(final EvalStub stub, final Tuple tuple, final Declaration[] declarations, final ReteEvaluator reteEvaluator) {
final String[] globals = stub.getGlobals();
final String[] globalTypes = stub.getGlobalTypes();
// Sort declarations based on their offset, so it can ascend the tuple's parents stack only once
final List<DeclarationMatcher> declarationMatchers = matchDeclarationsToTuple(declarations);
final ClassGenerator generator = createInvokerClassGenerator(stub, "_" + evalId.getAndIncrement(), reteEvaluator).setInterfaces(EvalExpression.class, CompiledInvoker.class);
generator.addMethod(ACC_PUBLIC, "createContext", generator.methodDescr(Object.class), new ClassGenerator.MethodBody() {
public void body(MethodVisitor mv) {
mv.visitInsn(ACONST_NULL);
mv.visitInsn(ARETURN);
}
}).addMethod(ACC_PUBLIC, "clone", generator.methodDescr(EvalExpression.class), new ClassGenerator.MethodBody() {
public void body(MethodVisitor mv) {
mv.visitVarInsn(ALOAD, 0);
mv.visitInsn(ARETURN);
}
}).addMethod(ACC_PUBLIC, "replaceDeclaration", generator.methodDescr(null, Declaration.class, Declaration.class)).addMethod(ACC_PUBLIC, "evaluate", generator.methodDescr(Boolean.TYPE, Tuple.class, Declaration[].class, ReteEvaluator.class, Object.class), new String[] { "java/lang/Exception" }, new GeneratorHelper.EvaluateMethod() {
public void body(MethodVisitor mv) {
objAstorePos = 7;
String[] expectedDeclarations = stub.getExpectedDeclarationTypes();
int[] declarationsParamsPos = new int[declarations.length];
mv.visitVarInsn(ALOAD, 1);
cast(LeftTuple.class);
// LeftTuple
mv.visitVarInsn(ASTORE, 5);
Tuple currentTuple = tuple;
for (DeclarationMatcher matcher : declarationMatchers) {
int i = matcher.getMatcherIndex();
declarationsParamsPos[i] = objAstorePos;
currentTuple = traverseTuplesUntilDeclaration(currentTuple, matcher.getTupleIndex(), 5);
mv.visitVarInsn(ALOAD, 2);
push(i);
// declarations[i]
mv.visitInsn(AALOAD);
// reteEvaluator
mv.visitVarInsn(ALOAD, 3);
mv.visitVarInsn(ALOAD, 5);
invokeInterface(LeftTuple.class, "getFactHandle", InternalFactHandle.class);
// tuple.getFactHandle().getObject()
invokeInterface(InternalFactHandle.class, "getObject", Object.class);
storeObjectFromDeclaration(declarations[i], expectedDeclarations[i]);
}
// @{ruleClassName}.@{methodName}(@foreach{declarations}, @foreach{globals})
StringBuilder evalMethodDescr = new StringBuilder("(");
for (int i = 0; i < declarations.length; i++) {
// declarations[i]
load(declarationsParamsPos[i]);
evalMethodDescr.append(typeDescr(expectedDeclarations[i]));
}
// @foreach{type : globalTypes, identifier : globals} @{type} @{identifier} = ( @{type} ) workingMemory.getGlobal( "@{identifier}" );
parseGlobals(globals, globalTypes, 3, evalMethodDescr);
evalMethodDescr.append(")Z");
mv.visitMethodInsn(INVOKESTATIC, stub.getInternalRuleClassName(), stub.getMethodName(), evalMethodDescr.toString());
mv.visitInsn(IRETURN);
}
});
stub.setEval(generator.<EvalExpression>newInstance());
}
use of org.drools.core.spi.EvalExpression in project drools by kiegroup.
the class JavaDialectBinaryEqualityTest method test1.
@Test
public void test1() {
KiePackage pkg1 = getKnowledgePackage1();
KiePackage pkg2 = getKnowledgePackage1();
KiePackage pkg3 = getKnowledgePackage2();
RuleImpl rule1 = ((InternalKnowledgePackage) pkg1).getRule("rule1");
RuleImpl rule2 = ((InternalKnowledgePackage) pkg2).getRule("rule1");
RuleImpl rule3 = ((InternalKnowledgePackage) pkg3).getRule("rule1");
// test return value
Pattern p1 = (Pattern) rule1.getLhs().getChildren().get(0);
Constraint rvc1 = p1.getConstraints().get(0);
Pattern p2 = (Pattern) rule2.getLhs().getChildren().get(0);
Constraint rvc2 = p2.getConstraints().get(0);
assertNotSame(rvc1, rvc2);
assertEquals(rvc1, rvc2);
Pattern p3 = (Pattern) rule3.getLhs().getChildren().get(0);
Constraint rvc3 = p3.getConstraints().get(0);
assertNotSame(rvc1, rvc3);
assertThat(rvc1, not(equalTo(rvc3)));
// test inline eval
PredicateConstraint pc1 = getPredicateConstraint(p1);
PredicateExpression pe1 = (PredicateExpression) pc1.getPredicateExpression();
PredicateConstraint pc2 = getPredicateConstraint(p2);
PredicateExpression pe2 = (PredicateExpression) pc2.getPredicateExpression();
assertNotSame(pe1, pe2);
assertEquals(pe1, pe2);
PredicateConstraint pc3 = getPredicateConstraint(p3);
PredicateExpression pe3 = (PredicateExpression) pc3.getPredicateExpression();
assertNotSame(pe1, pe3);
assertThat(pe1, not(equalTo(pe3)));
// test eval
EvalCondition ec1 = (EvalCondition) rule1.getLhs().getChildren().get(1);
EvalExpression ee1 = (EvalExpression) ec1.getEvalExpression();
EvalCondition ec2 = (EvalCondition) rule2.getLhs().getChildren().get(1);
EvalExpression ee2 = (EvalExpression) ec2.getEvalExpression();
assertNotSame(ee1, ee2);
assertEquals(ee1, ee2);
EvalCondition ec3 = (EvalCondition) rule3.getLhs().getChildren().get(1);
EvalExpression ee3 = (EvalExpression) ec3.getEvalExpression();
assertNotSame(ee1, ee3);
assertThat(ee1, not(equalTo(ee3)));
// test consequence
assertNotSame(rule1.getConsequence(), rule2.getConsequence());
assertEquals(rule1.getConsequence(), rule2.getConsequence());
assertNotSame(rule1.getConsequence(), rule3.getConsequence());
assertThat(rule1.getConsequence(), not(equalTo(rule3.getConsequence())));
// check LHS equals
assertNotSame(rule1.getLhs(), rule2.getLhs());
assertEquals(rule1.getLhs(), rule2.getLhs());
assertNotSame(rule1.getLhs(), rule3.getLhs());
assertThat(rule1.getLhs(), not(equalTo(rule3.getLhs())));
}
Aggregations