Search in sources :

Example 1 with EvalExpression

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 WorkingMemory workingMemory) {
    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, workingMemory).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, WorkingMemory.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.getOriginalIndex();
                declarationsParamsPos[i] = objAstorePos;
                currentTuple = traverseTuplesUntilDeclaration(currentTuple, matcher.getRootDistance(), 5);
                mv.visitVarInsn(ALOAD, 2);
                push(i);
                // declarations[i]
                mv.visitInsn(AALOAD);
                // workingMemory
                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());
}
Also used : WorkingMemory(org.drools.core.WorkingMemory) DeclarationMatcher(org.drools.core.rule.builder.dialect.asm.GeneratorHelper.DeclarationMatcher) LeftTuple(org.drools.core.reteoo.LeftTuple) MethodVisitor(org.mvel2.asm.MethodVisitor) EvalExpression(org.drools.core.spi.EvalExpression) GeneratorHelper.createInvokerClassGenerator(org.drools.core.rule.builder.dialect.asm.GeneratorHelper.createInvokerClassGenerator) InternalFactHandle(org.drools.core.common.InternalFactHandle) GeneratorHelper.matchDeclarationsToTuple(org.drools.core.rule.builder.dialect.asm.GeneratorHelper.matchDeclarationsToTuple) LeftTuple(org.drools.core.reteoo.LeftTuple) Tuple(org.drools.core.spi.Tuple)

Example 2 with EvalExpression

use of org.drools.core.spi.EvalExpression in project drools by kiegroup.

the class KiePackagesBuilder method buildConditionalConsequence.

private ConditionalBranch buildConditionalConsequence(RuleContext ctx, ConditionalNamedConsequenceImpl consequence) {
    // This makes the assumption the variable is for a pattern (not field) binding, and this binding must already exist.
    // It's used for the implicit bindings
    // The main issue with all of this, is it doesn't allow access to other previous variables. And it should work with
    // with pattern bindings and field bindings. What if getVariables() returns an array.length > 1?
    EvalCondition evalCondition;
    if (consequence.getExpr() != null) {
        Pattern pattern = ctx.getPattern(consequence.getExpr().getVariables()[0]);
        if (pattern.getDeclaration() != null) {
            // pattern has a root binding, so use that.
            EvalExpression eval = new LambdaEvalExpression(pattern, consequence.getExpr());
            evalCondition = new EvalCondition(eval, new Declaration[] { pattern.getDeclaration() });
        } else {
            // Pattern does not have root binding, so use field bindings.
            Declaration[] declarations = pattern.getDeclarations().values().toArray(new Declaration[pattern.getDeclarations().size()]);
            EvalExpression eval = new LambdaEvalExpression(declarations, consequence.getExpr());
            evalCondition = new EvalCondition(eval, declarations);
        }
    } else {
        evalCondition = new EvalCondition(LambdaEvalExpression.EMPTY, null);
    }
    return new ConditionalBranch(evalCondition, new NamedConsequence(consequence.getThenConsequence().getName(), consequence.getThenConsequence().isBreaking()), consequence.getElseBranch() != null ? buildConditionalConsequence(ctx, consequence.getElseBranch()) : null);
}
Also used : LambdaEvalExpression(org.drools.modelcompiler.constraints.LambdaEvalExpression) EvalExpression(org.drools.core.spi.EvalExpression) QueryCallPattern(org.drools.model.patterns.QueryCallPattern) AccumulatePattern(org.drools.model.AccumulatePattern) Pattern(org.drools.core.rule.Pattern) GroupByPattern(org.drools.model.GroupByPattern) ConditionalBranch(org.drools.core.rule.ConditionalBranch) LambdaEvalExpression(org.drools.modelcompiler.constraints.LambdaEvalExpression) Declaration(org.drools.core.rule.Declaration) WindowDeclaration(org.drools.core.rule.WindowDeclaration) TypeDeclarationUtil.createTypeDeclaration(org.drools.modelcompiler.util.TypeDeclarationUtil.createTypeDeclaration) TypeDeclaration(org.drools.core.rule.TypeDeclaration) EvalCondition(org.drools.core.rule.EvalCondition) NamedConsequence(org.drools.core.rule.NamedConsequence)

Example 3 with EvalExpression

use of org.drools.core.spi.EvalExpression in project drools by kiegroup.

the class KiePackagesBuilder method buildEval.

private EvalCondition buildEval(RuleContext ctx, EvalImpl eval) {
    Declaration[] declarations = Stream.of(eval.getExpr().getVariables()).map(ctx::getDeclaration).toArray(Declaration[]::new);
    EvalExpression evalExpr = new LambdaEvalExpression(declarations, eval.getExpr());
    return new EvalCondition(evalExpr, declarations);
}
Also used : LambdaEvalExpression(org.drools.modelcompiler.constraints.LambdaEvalExpression) EvalExpression(org.drools.core.spi.EvalExpression) LambdaEvalExpression(org.drools.modelcompiler.constraints.LambdaEvalExpression) Declaration(org.drools.core.rule.Declaration) WindowDeclaration(org.drools.core.rule.WindowDeclaration) TypeDeclarationUtil.createTypeDeclaration(org.drools.modelcompiler.util.TypeDeclarationUtil.createTypeDeclaration) TypeDeclaration(org.drools.core.rule.TypeDeclaration) EvalCondition(org.drools.core.rule.EvalCondition)

Example 4 with EvalExpression

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())));
}
Also used : EvalExpression(org.drools.core.spi.EvalExpression) Pattern(org.drools.core.rule.Pattern) KiePackage(org.kie.api.definition.KiePackage) Constraint(org.drools.core.spi.Constraint) PredicateConstraint(org.drools.core.rule.PredicateConstraint) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PredicateConstraint(org.drools.core.rule.PredicateConstraint) PredicateExpression(org.drools.core.spi.PredicateExpression) EvalCondition(org.drools.core.rule.EvalCondition) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 5 with EvalExpression

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())));
}
Also used : EvalExpression(org.drools.core.spi.EvalExpression) Pattern(org.drools.core.rule.Pattern) KiePackage(org.kie.api.definition.KiePackage) Constraint(org.drools.core.spi.Constraint) PredicateConstraint(org.drools.core.rule.PredicateConstraint) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PredicateConstraint(org.drools.core.rule.PredicateConstraint) PredicateExpression(org.drools.core.spi.PredicateExpression) EvalCondition(org.drools.core.rule.EvalCondition) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Aggregations

EvalExpression (org.drools.core.spi.EvalExpression)7 EvalCondition (org.drools.core.rule.EvalCondition)4 Pattern (org.drools.core.rule.Pattern)3 InternalFactHandle (org.drools.core.common.InternalFactHandle)2 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)2 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)2 LeftTuple (org.drools.core.reteoo.LeftTuple)2 Declaration (org.drools.core.rule.Declaration)2 PredicateConstraint (org.drools.core.rule.PredicateConstraint)2 TypeDeclaration (org.drools.core.rule.TypeDeclaration)2 WindowDeclaration (org.drools.core.rule.WindowDeclaration)2 Constraint (org.drools.core.spi.Constraint)2 PredicateExpression (org.drools.core.spi.PredicateExpression)2 Tuple (org.drools.core.spi.Tuple)2 LambdaEvalExpression (org.drools.modelcompiler.constraints.LambdaEvalExpression)2 TypeDeclarationUtil.createTypeDeclaration (org.drools.modelcompiler.util.TypeDeclarationUtil.createTypeDeclaration)2 Test (org.junit.Test)2 KiePackage (org.kie.api.definition.KiePackage)2 MethodVisitor (org.mvel2.asm.MethodVisitor)2 WorkingMemory (org.drools.core.WorkingMemory)1