Search in sources :

Example 26 with WorkingMemory

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

the class MVELConsequence method evaluate.

public void evaluate(final KnowledgeHelper knowledgeHelper, final WorkingMemory workingMemory) throws Exception {
    VariableResolverFactory factory = unit.getFactory(knowledgeHelper, ((AgendaItem) knowledgeHelper.getMatch()).getTerminalNode().getRequiredDeclarations(), knowledgeHelper.getRule(), knowledgeHelper.getTuple(), null, (InternalWorkingMemory) workingMemory, workingMemory.getGlobalResolver());
    // do we have any functions for this namespace?
    InternalKnowledgePackage pkg = workingMemory.getKnowledgeBase().getPackage("MAIN");
    if (pkg != null) {
        MVELDialectRuntimeData data = (MVELDialectRuntimeData) pkg.getDialectRuntimeRegistry().getDialectData(this.id);
        factory.setNextFactory(data.getFunctionFactory());
    }
    CompiledExpression compexpr = (CompiledExpression) this.expr;
    if (MVELDebugHandler.isDebugMode()) {
        if (MVELDebugHandler.verbose) {
            logger.info(DebugTools.decompile(compexpr));
        }
        MVEL.executeDebugger(compexpr, knowledgeHelper, factory);
    } else {
        MVEL.executeExpression(compexpr, knowledgeHelper, factory);
    }
}
Also used : MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) AgendaItem(org.drools.core.common.AgendaItem) CompiledExpression(org.mvel2.compiler.CompiledExpression) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 27 with WorkingMemory

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

the class MVELSalienceExpression method getValue.

public int getValue(final KnowledgeHelper khelper, final Rule rule, final WorkingMemory workingMemory) {
    VariableResolverFactory factory = unit.getFactory(khelper, khelper != null ? ((AgendaItem) khelper.getMatch()).getTerminalNode().getSalienceDeclarations() : null, rule, null, khelper != null ? (LeftTuple) khelper.getMatch().getTuple() : null, null, (InternalWorkingMemory) workingMemory, workingMemory.getGlobalResolver());
    // do we have any functions for this namespace?
    InternalKnowledgePackage pkg = workingMemory.getKnowledgeBase().getPackage("MAIN");
    if (pkg != null) {
        MVELDialectRuntimeData data = (MVELDialectRuntimeData) pkg.getDialectRuntimeRegistry().getDialectData(this.id);
        factory.setNextFactory(data.getFunctionFactory());
    }
    Object value = MVEL.executeExpression(this.expr, factory);
    if (value instanceof String) {
        value = TimeUtils.parseTimeString((String) value);
    }
    return ((Number) value).intValue();
}
Also used : MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) LeftTuple(org.drools.core.reteoo.LeftTuple) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 28 with WorkingMemory

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

the class MVELReturnValueExpression method evaluate.

public FieldValue evaluate(final InternalFactHandle handle, final Tuple tuple, final Declaration[] previousDeclarations, final Declaration[] requiredDeclarations, final WorkingMemory workingMemory, final Object ctx) throws Exception {
    VariableResolverFactory factory = (VariableResolverFactory) ctx;
    unit.updateFactory(handle, tuple, null, (InternalWorkingMemory) workingMemory, workingMemory.getGlobalResolver(), factory);
    // do we have any functions for this namespace?
    InternalKnowledgePackage pkg = workingMemory.getKnowledgeBase().getPackage("MAIN");
    if (pkg != null) {
        MVELDialectRuntimeData data = (MVELDialectRuntimeData) pkg.getDialectRuntimeRegistry().getDialectData(this.id);
        factory.setNextFactory(data.getFunctionFactory());
    }
    return workingMemory.getKnowledgeBase().getConfiguration().getComponentFactory().getFieldFactory().getFieldValue(MVELSafeHelper.getEvaluator().executeExpression(this.expr, handle, factory));
}
Also used : MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 29 with WorkingMemory

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

the class CoreConfidenceTests method testsequentialAccessorsThenMethodCall.

public void testsequentialAccessorsThenMethodCall() {
    String expr = "System.out.println(drools.workingMemory); " + "drools.workingMemory.ruleBase.removeRule(\"org.drools.examples\", \"some rule\"); ";
    ExpressionCompiler compiler = new ExpressionCompiler(expr);
    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);
    executeExpression(compiler.compile(context), vars);
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler)

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