Search in sources :

Example 1 with InvokerDataProvider

use of org.drools.core.rule.builder.dialect.asm.InvokerDataProvider in project drools by kiegroup.

the class ASMConsequenceStubBuilder method createConsequenceBytecode.

protected byte[] createConsequenceBytecode(final RuleBuildContext ruleContext, final Map<String, Object> consequenceContext) {
    // If the LHS contains an OR we cannot use the optimized consequence invoker that traverses the tuple only once
    if (isOr(ruleContext.getRule().getLhs())) {
        return super.createConsequenceBytecode(ruleContext, consequenceContext);
    }
    final InvokerDataProvider data = new InvokerContext(consequenceContext);
    final ClassGenerator generator = InvokerGenerator.createInvokerStubGenerator(data, ruleContext);
    createStubConsequence(generator, data, consequenceContext);
    return generator.generateBytecode();
}
Also used : ClassGenerator(org.drools.core.rule.builder.dialect.asm.ClassGenerator) InvokerDataProvider(org.drools.core.rule.builder.dialect.asm.InvokerDataProvider)

Example 2 with InvokerDataProvider

use of org.drools.core.rule.builder.dialect.asm.InvokerDataProvider in project drools by kiegroup.

the class ASMPredicateStubBuilder method createPredicateBytecode.

protected byte[] createPredicateBytecode(final RuleBuildContext ruleContext, final Map vars) {
    final InvokerDataProvider data = new InvokerContext(vars);
    final ClassGenerator generator = InvokerGenerator.createInvokerStubGenerator(data, ruleContext);
    createStubPredicate(generator, data, vars);
    return generator.generateBytecode();
}
Also used : ClassGenerator(org.drools.core.rule.builder.dialect.asm.ClassGenerator) InvokerDataProvider(org.drools.core.rule.builder.dialect.asm.InvokerDataProvider)

Example 3 with InvokerDataProvider

use of org.drools.core.rule.builder.dialect.asm.InvokerDataProvider in project drools by kiegroup.

the class ASMConsequenceBuilder method createConsequenceBytecode.

protected byte[] createConsequenceBytecode(RuleBuildContext ruleContext, final Map<String, Object> consequenceContext) {
    final InvokerDataProvider data = new InvokerContext(consequenceContext);
    final String name = (String) consequenceContext.get("consequenceName");
    final Declaration[] declarations = (Declaration[]) consequenceContext.get("declarations");
    final ClassGenerator generator = InvokerGenerator.createInvokerClassGenerator(data, ruleContext).setInterfaces(Consequence.class, CompiledInvoker.class);
    generator.addMethod(ACC_PUBLIC, "getName", generator.methodDescr(String.class), new ClassGenerator.MethodBody() {

        public void body(MethodVisitor mv) {
            push(name);
            mv.visitInsn(ARETURN);
        }
    }).addMethod(ACC_PUBLIC, "evaluate", generator.methodDescr(null, KnowledgeHelper.class, WorkingMemory.class), new String[] { "java/lang/Exception" }, new GeneratorHelper.EvaluateMethod() {

        public void body(MethodVisitor mv) {
            // Tuple tuple = knowledgeHelper.getTuple();
            mv.visitVarInsn(ALOAD, 1);
            invokeInterface(KnowledgeHelper.class, "getTuple", Tuple.class);
            mv.visitVarInsn(ASTORE, 3);
            // Declaration[] declarations = ((RuleTerminalNode)knowledgeHelper.getMatch().getTuple().getTupleSink()).getDeclarations();
            mv.visitVarInsn(ALOAD, 1);
            invokeInterface(KnowledgeHelper.class, "getMatch", Activation.class);
            invokeInterface(Activation.class, "getTuple", Tuple.class);
            invokeInterface(Tuple.class, "getTupleSink", Sink.class);
            cast(RuleTerminalNode.class);
            invokeVirtual(RuleTerminalNode.class, "getRequiredDeclarations", Declaration[].class);
            mv.visitVarInsn(ASTORE, 4);
            final String[] globals = data.getGlobals();
            final String[] globalTypes = data.getGlobalTypes();
            final Boolean[] notPatterns = (Boolean[]) consequenceContext.get("notPatterns");
            int[] paramsPos = new int[declarations.length];
            int offset = 5;
            for (int i = 0; i < declarations.length; i++) {
                int factPos = offset;
                int objPos = ++offset;
                paramsPos[i] = factPos;
                // Object obj[i] = tuple.get(declarations[i]);
                // org.kie.spi.Tuple
                mv.visitVarInsn(ALOAD, 3);
                // org.kie.rule.Declaration[]
                mv.visitVarInsn(ALOAD, 4);
                // i
                push(i);
                // declarations[i]
                mv.visitInsn(AALOAD);
                invokeInterface(Tuple.class, "get", InternalFactHandle.class, Declaration.class);
                // fact[i]
                mv.visitVarInsn(ASTORE, factPos);
                // declarations[i].getValue((org.kie.common.InternalWorkingMemory)workingMemory, obj[i] );
                // org.kie.rule.Declaration[]
                mv.visitVarInsn(ALOAD, 4);
                // i
                push(i);
                // declarations[i]
                mv.visitInsn(AALOAD);
                // WorkingMemory
                mv.visitVarInsn(ALOAD, 2);
                cast(InternalWorkingMemory.class);
                // fact[i]
                mv.visitVarInsn(ALOAD, factPos);
                invokeInterface(InternalFactHandle.class, "getObject", Object.class);
                String readMethod = declarations[i].getNativeReadMethodName();
                boolean isObject = readMethod.equals("getValue");
                String returnedType = isObject ? "Ljava/lang/Object;" : typeDescr(declarations[i].getTypeName());
                mv.visitMethodInsn(INVOKEVIRTUAL, Declaration.class.getName().replace('.', '/'), readMethod, "(L" + InternalWorkingMemory.class.getName().replace('.', '/') + ";Ljava/lang/Object;)" + returnedType);
                if (isObject)
                    mv.visitTypeInsn(CHECKCAST, internalName(declarations[i].getTypeName()));
                // obj[i]
                offset += store(objPos, declarations[i].getTypeName());
                if (notPatterns[i]) {
                    mv.visitVarInsn(ALOAD, 1);
                    invokeInterface(KnowledgeHelper.class, "getWorkingMemory", WorkingMemory.class);
                    loadAsObject(objPos);
                    invokeInterface(WorkingMemory.class, "getFactHandle", FactHandle.class, Object.class);
                    cast(InternalFactHandle.class);
                    mv.visitVarInsn(ASTORE, factPos);
                }
            }
            // @{ruleClassName}.@{methodName}(KnowledgeHelper, @foreach{declr : declarations} Object, FactHandle @end)
            StringBuilder consequenceMethodDescr = new StringBuilder("(L" + KnowledgeHelper.class.getName().replace('.', '/') + ";");
            // KnowledgeHelper
            mv.visitVarInsn(ALOAD, 1);
            for (int i = 0; i < declarations.length; i++) {
                // obj[i]
                load(paramsPos[i] + 1);
                // fact[i]
                mv.visitVarInsn(ALOAD, paramsPos[i]);
                consequenceMethodDescr.append(typeDescr(declarations[i].getTypeName())).append("L" + FactHandle.class.getName().replace('.', '/') + ";");
            }
            // @foreach{type : globalTypes, identifier : globals} @{type} @{identifier} = ( @{type} ) workingMemory.getGlobal( "@{identifier}" );
            for (int i = 0; i < globals.length; i++) {
                // WorkingMemory
                mv.visitVarInsn(ALOAD, 2);
                push(globals[i]);
                invokeInterface(WorkingMemory.class, "getGlobal", Object.class, String.class);
                mv.visitTypeInsn(CHECKCAST, internalName(globalTypes[i]));
                consequenceMethodDescr.append(typeDescr(globalTypes[i]));
            }
            consequenceMethodDescr.append(")V");
            mv.visitMethodInsn(INVOKESTATIC, data.getInternalRuleClassName(), data.getMethodName(), consequenceMethodDescr.toString());
            mv.visitInsn(RETURN);
        }
    });
    return generator.generateBytecode();
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) WorkingMemory(org.drools.core.WorkingMemory) Activation(org.drools.core.spi.Activation) MethodVisitor(org.mvel2.asm.MethodVisitor) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ClassGenerator(org.drools.core.rule.builder.dialect.asm.ClassGenerator) GeneratorHelper(org.drools.core.rule.builder.dialect.asm.GeneratorHelper) Sink(org.drools.core.reteoo.Sink) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper) Declaration(org.drools.core.rule.Declaration) InternalFactHandle(org.drools.core.common.InternalFactHandle) Tuple(org.drools.core.spi.Tuple) InvokerDataProvider(org.drools.core.rule.builder.dialect.asm.InvokerDataProvider) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode)

Example 4 with InvokerDataProvider

use of org.drools.core.rule.builder.dialect.asm.InvokerDataProvider in project drools by kiegroup.

the class ASMEvalStubBuilder method createEvalBytecode.

protected byte[] createEvalBytecode(final RuleBuildContext ruleContext, final Map vars) {
    final InvokerDataProvider data = new InvokerContext(vars);
    final ClassGenerator generator = InvokerGenerator.createInvokerStubGenerator(data, ruleContext);
    createStubEval(generator, data, vars);
    return generator.generateBytecode();
}
Also used : ClassGenerator(org.drools.core.rule.builder.dialect.asm.ClassGenerator) InvokerDataProvider(org.drools.core.rule.builder.dialect.asm.InvokerDataProvider)

Example 5 with InvokerDataProvider

use of org.drools.core.rule.builder.dialect.asm.InvokerDataProvider in project drools by kiegroup.

the class ASMReturnValueStubBuilder method createReturnValueBytecode.

protected byte[] createReturnValueBytecode(RuleBuildContext ruleContext, Map vars, boolean readLocalsFromTuple) {
    final InvokerDataProvider data = new InvokerContext(vars);
    final ClassGenerator generator = InvokerGenerator.createInvokerStubGenerator(data, ruleContext);
    createStubReturnValue(generator, data, vars);
    return generator.generateBytecode();
}
Also used : ClassGenerator(org.drools.core.rule.builder.dialect.asm.ClassGenerator) InvokerDataProvider(org.drools.core.rule.builder.dialect.asm.InvokerDataProvider)

Aggregations

ClassGenerator (org.drools.core.rule.builder.dialect.asm.ClassGenerator)5 InvokerDataProvider (org.drools.core.rule.builder.dialect.asm.InvokerDataProvider)5 WorkingMemory (org.drools.core.WorkingMemory)1 InternalFactHandle (org.drools.core.common.InternalFactHandle)1 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)1 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)1 Sink (org.drools.core.reteoo.Sink)1 Declaration (org.drools.core.rule.Declaration)1 GeneratorHelper (org.drools.core.rule.builder.dialect.asm.GeneratorHelper)1 Activation (org.drools.core.spi.Activation)1 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)1 Tuple (org.drools.core.spi.Tuple)1 FactHandle (org.kie.api.runtime.rule.FactHandle)1 MethodVisitor (org.mvel2.asm.MethodVisitor)1