Search in sources :

Example 11 with WorkingMemory

use of org.mvel2.tests.core.res.WorkingMemory in project drools by kiegroup.

the class ReturnValueGenerator method generate.

public static void generate(final ReturnValueStub stub, final Tuple tuple, final Declaration[] previousDeclarations, final Declaration[] localDeclarations, 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(previousDeclarations);
    final ClassGenerator generator = createInvokerClassGenerator(stub, workingMemory).setInterfaces(ReturnValueExpression.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, "replaceDeclaration", generator.methodDescr(null, Declaration.class, Declaration.class)).addMethod(ACC_PUBLIC, "evaluate", generator.methodDescr(FieldValue.class, Object.class, Tuple.class, Declaration[].class, Declaration[].class, WorkingMemory.class, Object.class), new String[] { "java/lang/Exception" }, new GeneratorHelper.EvaluateMethod() {

        public void body(MethodVisitor mv) {
            objAstorePos = 9;
            int[] previousDeclarationsParamsPos = new int[previousDeclarations.length];
            mv.visitVarInsn(ALOAD, 2);
            cast(LeftTuple.class);
            // LeftTuple
            mv.visitVarInsn(ASTORE, 7);
            Tuple currentTuple = tuple;
            for (DeclarationMatcher matcher : declarationMatchers) {
                int i = matcher.getOriginalIndex();
                previousDeclarationsParamsPos[i] = objAstorePos;
                currentTuple = traverseTuplesUntilDeclaration(currentTuple, matcher.getRootDistance(), 7);
                mv.visitVarInsn(ALOAD, 3);
                push(i);
                // declarations[i]
                mv.visitInsn(AALOAD);
                // workingMemory
                mv.visitVarInsn(ALOAD, 5);
                mv.visitVarInsn(ALOAD, 7);
                invokeInterface(LeftTuple.class, "getFactHandle", InternalFactHandle.class);
                // tuple.getFactHandle().getObject()
                invokeInterface(InternalFactHandle.class, "getObject", Object.class);
                storeObjectFromDeclaration(previousDeclarations[i], previousDeclarations[i].getTypeName());
            }
            int[] localDeclarationsParamsPos = parseDeclarations(localDeclarations, 4, 2, 5, false);
            // @{ruleClassName}.@{methodName}(@foreach{previousDeclarations}, @foreach{localDeclarations}, @foreach{globals})
            StringBuilder returnValueMethodDescr = new StringBuilder("(");
            for (int i = 0; i < previousDeclarations.length; i++) {
                // previousDeclarations[i]
                load(previousDeclarationsParamsPos[i]);
                returnValueMethodDescr.append(typeDescr(previousDeclarations[i].getTypeName()));
            }
            for (int i = 0; i < localDeclarations.length; i++) {
                // localDeclarations[i]
                load(localDeclarationsParamsPos[i]);
                returnValueMethodDescr.append(typeDescr(localDeclarations[i].getTypeName()));
            }
            // @foreach{type : globalTypes, identifier : globals} @{type} @{identifier} = ( @{type} ) workingMemory.getGlobal( "@{identifier}" );
            parseGlobals(globals, globalTypes, 5, returnValueMethodDescr);
            returnValueMethodDescr.append(")Lorg/drools/core/spi/FieldValue;");
            mv.visitMethodInsn(INVOKESTATIC, stub.getInternalRuleClassName(), stub.getMethodName(), returnValueMethodDescr.toString());
            mv.visitInsn(ARETURN);
        }
    });
    stub.setReturnValue(generator.<ReturnValueExpression>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) GeneratorHelper.createInvokerClassGenerator(org.drools.core.rule.builder.dialect.asm.GeneratorHelper.createInvokerClassGenerator) FieldValue(org.drools.core.spi.FieldValue) 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 12 with WorkingMemory

use of org.mvel2.tests.core.res.WorkingMemory in project drools by kiegroup.

the class MvelConditionEvaluator method evaluateIfNecessary.

private void evaluateIfNecessary(InternalFactHandle handle, InternalWorkingMemory workingMemory, Tuple tuple, ASTNode node) {
    if (!isEvaluated(node)) {
        ASTNode next = node.nextASTNode;
        node.nextASTNode = null;
        evaluate(asCompiledExpression(node), handle, workingMemory, tuple);
        node.nextASTNode = next;
    }
}
Also used : ASTNode(org.mvel2.ast.ASTNode)

Example 13 with WorkingMemory

use of org.mvel2.tests.core.res.WorkingMemory in project drools by kiegroup.

the class MvelConstraint method createMvelConditionEvaluator.

protected ConditionEvaluator createMvelConditionEvaluator(InternalWorkingMemory workingMemory) {
    if (compilationUnit != null) {
        MVELDialectRuntimeData data = getMVELDialectRuntimeData(workingMemory);
        ExecutableStatement statement = (ExecutableStatement) compilationUnit.getCompiledExpression(data, evaluationContext);
        ParserConfiguration configuration = statement instanceof CompiledExpression ? ((CompiledExpression) statement).getParserConfiguration() : data.getParserConfiguration();
        return new MvelConditionEvaluator(compilationUnit, configuration, statement, declarations, operators, getAccessedClass());
    } else {
        return new MvelConditionEvaluator(getParserConfiguration(workingMemory), expression, declarations, operators, getAccessedClass());
    }
}
Also used : MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) ExecutableStatement(org.mvel2.compiler.ExecutableStatement) CompiledExpression(org.mvel2.compiler.CompiledExpression) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 14 with WorkingMemory

use of org.mvel2.tests.core.res.WorkingMemory in project mvel by mvel.

the class CoreConfidenceTests method testsequentialAccessorsThenMethodCall.

public void testsequentialAccessorsThenMethodCall() {
    String expr = "System.out.println(drools.workingMemory); " + "drools.workingMemory.ruleBase.removeRule(\"org.drools.examples\", \"some rule\"); ";
    ParserContext context = new ParserContext();
    context.setStrictTypeEnforcement(true);
    context.addInput("drools", KnowledgeHelper.class);
    RuleBase ruleBase = new RuleBaseImpl();
    WorkingMemory wm = new WorkingMemoryImpl(ruleBase);
    KnowledgeHelper drools = new DefaultKnowledgeHelper(wm);
    Map vars = new HashMap();
    vars.put("drools", drools);
    ExpressionCompiler compiler = new ExpressionCompiler(expr, context);
    executeExpression(compiler.compile(), vars);
}
Also used : WorkingMemoryImpl(org.mvel2.tests.core.res.WorkingMemoryImpl) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) WorkingMemory(org.mvel2.tests.core.res.WorkingMemory) DefaultKnowledgeHelper(org.mvel2.tests.core.res.DefaultKnowledgeHelper) DefaultKnowledgeHelper(org.mvel2.tests.core.res.DefaultKnowledgeHelper) KnowledgeHelper(org.mvel2.tests.core.res.KnowledgeHelper) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext) RuleBaseImpl(org.mvel2.tests.core.res.RuleBaseImpl) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) RuleBase(org.mvel2.tests.core.res.RuleBase)

Example 15 with WorkingMemory

use of org.mvel2.tests.core.res.WorkingMemory in project drools by kiegroup.

the class ASMConsequenceStubBuilder method createStubConsequence.

private void createStubConsequence(final ClassGenerator generator, final InvokerDataProvider data, final Map<String, Object> vars) {
    generator.setInterfaces(ConsequenceStub.class, CompiledInvoker.class).addField(ACC_PRIVATE + ACC_VOLATILE, "consequence", Consequence.class);
    generator.addMethod(ACC_PUBLIC, "getName", generator.methodDescr(String.class), new ClassGenerator.MethodBody() {

        public void body(MethodVisitor mv) {
            push((String) vars.get("consequenceName"));
            // return the first object on the stack
            mv.visitInsn(ARETURN);
        }
    }).addMethod(ACC_PUBLIC, "getNotPatterns", generator.methodDescr(Boolean[].class), new ClassGenerator.MethodBody() {

        public void body(MethodVisitor mv) {
            returnAsArray((Boolean[]) vars.get("notPatterns"));
        }
    }).addMethod(ACC_PUBLIC, "evaluate", generator.methodDescr(null, KnowledgeHelper.class, WorkingMemory.class), new String[] { "java/lang/Exception" }, new ClassGenerator.MethodBody() {

        public void body(MethodVisitor mv) {
            Label syncStart = new Label();
            Label syncEnd = new Label();
            Label l1 = new Label();
            Label l2 = new Label();
            mv.visitTryCatchBlock(syncStart, l1, l2, null);
            Label l3 = new Label();
            mv.visitTryCatchBlock(l2, l3, l2, null);
            getFieldFromThis("consequence", Consequence.class);
            mv.visitJumpInsn(IFNONNULL, syncEnd);
            mv.visitVarInsn(ALOAD, 0);
            mv.visitInsn(DUP);
            mv.visitVarInsn(ASTORE, 3);
            // synchronized(this) {
            mv.visitInsn(MONITORENTER);
            mv.visitLabel(syncStart);
            getFieldFromThis("consequence", Consequence.class);
            // if (consequence == null) ...
            Label ifNotInitialized = new Label();
            mv.visitJumpInsn(IFNONNULL, ifNotInitialized);
            mv.visitVarInsn(ALOAD, 0);
            mv.visitVarInsn(ALOAD, 1);
            mv.visitVarInsn(ALOAD, 2);
            // ... ConsequenceGenerator.generate(this, knowledgeHelper, workingMemory)
            invokeStatic(ConsequenceGenerator.class, "generate", null, ConsequenceStub.class, KnowledgeHelper.class, WorkingMemory.class);
            mv.visitLabel(ifNotInitialized);
            mv.visitVarInsn(ALOAD, 3);
            mv.visitInsn(MONITOREXIT);
            mv.visitLabel(l1);
            mv.visitJumpInsn(GOTO, syncEnd);
            mv.visitLabel(l2);
            mv.visitVarInsn(ASTORE, 4);
            mv.visitVarInsn(ALOAD, 3);
            mv.visitInsn(MONITOREXIT);
            mv.visitLabel(l3);
            mv.visitVarInsn(ALOAD, 4);
            mv.visitInsn(ATHROW);
            mv.visitLabel(syncEnd);
            // } end of synchronized
            getFieldFromThis("consequence", Consequence.class);
            mv.visitVarInsn(ALOAD, 1);
            mv.visitVarInsn(ALOAD, 2);
            invokeInterface(Consequence.class, "evaluate", null, KnowledgeHelper.class, WorkingMemory.class);
            mv.visitInsn(RETURN);
        }
    }).addMethod(ACC_PUBLIC, "setConsequence", generator.methodDescr(null, Consequence.class), new ClassGenerator.MethodBody() {

        public void body(MethodVisitor mv) {
            putFieldInThisFromRegistry("consequence", Consequence.class, 1);
            mv.visitInsn(RETURN);
        }
    });
}
Also used : ClassGenerator(org.drools.core.rule.builder.dialect.asm.ClassGenerator) Label(org.mvel2.asm.Label) ConsequenceStub(org.drools.core.rule.builder.dialect.asm.ConsequenceStub) Consequence(org.drools.core.spi.Consequence) CompiledInvoker(org.drools.core.spi.CompiledInvoker) MethodVisitor(org.mvel2.asm.MethodVisitor)

Aggregations

VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)14 MethodVisitor (org.mvel2.asm.MethodVisitor)9 MVELDialectRuntimeData (org.drools.core.rule.MVELDialectRuntimeData)8 InternalFactHandle (org.drools.core.common.InternalFactHandle)7 WorkingMemory (org.drools.core.WorkingMemory)6 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)6 Tuple (org.drools.core.spi.Tuple)6 LeftTuple (org.drools.core.reteoo.LeftTuple)5 Declaration (org.drools.core.rule.Declaration)5 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)4 ClassGenerator (org.drools.core.rule.builder.dialect.asm.ClassGenerator)4 DeclarationMatcher (org.drools.core.rule.builder.dialect.asm.GeneratorHelper.DeclarationMatcher)4 GeneratorHelper.createInvokerClassGenerator (org.drools.core.rule.builder.dialect.asm.GeneratorHelper.createInvokerClassGenerator)4 GeneratorHelper.matchDeclarationsToTuple (org.drools.core.rule.builder.dialect.asm.GeneratorHelper.matchDeclarationsToTuple)4 Label (org.mvel2.asm.Label)4 DroolsVarFactory (org.drools.core.base.mvel.MVELCompilationUnit.DroolsVarFactory)3 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)3 CompiledInvoker (org.drools.core.spi.CompiledInvoker)3 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)3 HashMap (java.util.HashMap)2