Search in sources :

Example 16 with Node

use of org.mvel2.templates.res.Node in project mvel by mikebrock.

the class MacroProcessorTest method testMacroSupportWithDebugging.

public void testMacroSupportWithDebugging() {
    Map<String, Object> vars = new HashMap<String, Object>();
    vars.put("foo", new Foo());
    Map<String, Interceptor> interceptors = new HashMap<String, Interceptor>();
    Map<String, Macro> macros = new HashMap<String, Macro>();
    interceptors.put("Modify", new Interceptor() {

        public int doBefore(ASTNode node, VariableResolverFactory factory) {
            ((WithNode) node).getNestedStatement().getValue(null, factory);
            factory.createVariable("mod", "FOOBAR!");
            return 0;
        }

        public int doAfter(Object val, ASTNode node, VariableResolverFactory factory) {
            return 0;
        }
    });
    macros.put("modify", new Macro() {

        public String doMacro() {
            return "@Modify with";
        }
    });
    ExpressionCompiler compiler = new ExpressionCompiler(parseMacros("System.out.println('hello');\n" + "System.out.println('bye');\n" + "modify (foo) { aValue = 'poo', \n" + " aValue = 'poo' };\n mod", macros));
    // compiler.setDebugSymbols(true);
    ParserContext ctx = new ParserContext(null, interceptors, null);
    ctx.setSourceFile("test.mv");
    ctx.setDebugSymbols(true);
    CompiledExpression compiled = compiler.compile(ctx);
    MVELRuntime.setThreadDebugger(new Debugger() {

        public int onBreak(Frame frame) {
            System.out.println(frame.getSourceName() + ":" + frame.getLineNumber());
            return Debugger.STEP;
        }
    });
    MVELRuntime.registerBreakpoint("test.mv", 3);
    System.out.println(DebugTools.decompile(compiled));
    Assert.assertEquals("FOOBAR!", MVEL.executeDebugger(compiled, null, new MapVariableResolverFactory(vars)));
}
Also used : Debugger(org.mvel2.debug.Debugger) Frame(org.mvel2.debug.Frame) HashMap(java.util.HashMap) Foo(org.mvel2.tests.core.res.Foo) WithNode(org.mvel2.ast.WithNode) CompiledExpression(org.mvel2.compiler.CompiledExpression) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ASTNode(org.mvel2.ast.ASTNode) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) Interceptor(org.mvel2.integration.Interceptor)

Example 17 with Node

use of org.mvel2.templates.res.Node in project mvel by mikebrock.

the class CoreConfidenceTests method testInterceptors.

public void testInterceptors() {
    Interceptor testInterceptor = new Interceptor() {

        public int doBefore(ASTNode node, VariableResolverFactory factory) {
            System.out.println("BEFORE Node: " + node.getName());
            return 0;
        }

        public int doAfter(Object val, ASTNode node, VariableResolverFactory factory) {
            System.out.println("AFTER Node: " + node.getName());
            return 0;
        }
    };
    Map<String, Interceptor> interceptors = new HashMap<String, Interceptor>();
    interceptors.put("test", testInterceptor);
    executeExpression(compileExpression("@test System.out.println('MIDDLE');", null, interceptors));
}
Also used : DefaultLocalVariableResolverFactory(org.mvel2.integration.impl.DefaultLocalVariableResolverFactory) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ASTNode(org.mvel2.ast.ASTNode) Interceptor(org.mvel2.integration.Interceptor)

Example 18 with Node

use of org.mvel2.templates.res.Node in project mvel by mikebrock.

the class TemplateTests method testPluginNode.

public void testPluginNode() {
    Map<String, Class<? extends Node>> plugins = new HashMap<String, Class<? extends Node>>();
    plugins.put("testNode", TestPluginNode.class);
    TemplateCompiler compiler = new TemplateCompiler("Foo:@testNode{}!!", plugins);
    CompiledTemplate compiled = compiler.compile();
    assertEquals("Foo:THIS_IS_A_TEST!!", TemplateRuntime.execute(compiled));
}
Also used : Node(org.mvel2.templates.res.Node) TestPluginNode(org.mvel2.tests.templates.tests.res.TestPluginNode) TemplateCompiler(org.mvel2.templates.TemplateCompiler) CompiledTemplate(org.mvel2.templates.CompiledTemplate)

Example 19 with Node

use of org.mvel2.templates.res.Node in project drools by kiegroup.

the class ConditionAnalyzer method analyzeSingleCondition.

private SingleCondition analyzeSingleCondition(ASTNode node, boolean isNegated) {
    SingleCondition condition = new SingleCondition(isNegated);
    if (node instanceof BinaryOperation) {
        BinaryOperation binaryOperation = (BinaryOperation) node;
        condition.left = analyzeNode(binaryOperation.getLeft());
        condition.operation = BooleanOperator.fromMvelOpCode(binaryOperation.getOperation());
        condition.right = analyzeNode(binaryOperation.getRight());
    } else if (node instanceof RegExMatch) {
        condition.left = analyzeNode(node);
        condition.operation = BooleanOperator.MATCHES;
        RegExMatch regExNode = (RegExMatch) node;
        Pattern pattern = regExNode.getPattern();
        if (pattern != null) {
            condition.right = new FixedExpression(String.class, pattern.pattern());
        } else {
            condition.right = analyzeNode(((ExecutableAccessor) regExNode.getPatternStatement()).getNode());
        }
    } else if (node instanceof Contains) {
        condition.left = analyzeNode(((Contains) node).getFirstStatement());
        condition.operation = BooleanOperator.CONTAINS;
        condition.right = analyzeNode(((Contains) node).getSecondStatement());
    } else if (node instanceof Soundslike) {
        condition.left = analyzeNode(((Soundslike) node).getStatement());
        condition.operation = BooleanOperator.SOUNDSLIKE;
        condition.right = analyzeNode(((Soundslike) node).getSoundslike());
    } else if (node instanceof Instance) {
        condition.left = analyzeNode(((Instance) node).getStatement());
        condition.operation = BooleanOperator.INSTANCEOF;
        condition.right = analyzeNode(((Instance) node).getClassStatement());
    } else {
        condition.left = analyzeNode(node);
    }
    return condition;
}
Also used : Pattern(java.util.regex.Pattern) RegExMatch(org.mvel2.ast.RegExMatch) Soundslike(org.mvel2.ast.Soundslike) Instance(org.mvel2.ast.Instance) BinaryOperation(org.mvel2.ast.BinaryOperation) Contains(org.mvel2.ast.Contains)

Example 20 with Node

use of org.mvel2.templates.res.Node in project drools by kiegroup.

the class ModifyInterceptor method calculateModificationMask.

private void calculateModificationMask(KnowledgeHelper knowledgeHelper, WithNode node) {
    Class<?> nodeClass = node.getEgressType();
    TypeDeclaration typeDeclaration = knowledgeHelper.getWorkingMemory().getKnowledgeBase().getTypeDeclaration(nodeClass);
    if (typeDeclaration == null || !typeDeclaration.isPropertyReactive()) {
        modificationMask = allSetButTraitBitMask();
        return;
    }
    List<String> settableProperties = typeDeclaration.getAccessibleProperties();
    modificationMask = getEmptyPropertyReactiveMask(settableProperties.size());
    // TODO: access parmValuePairs without reflection
    WithNode.ParmValuePair[] parmValuePairs = getFieldValue(WithNode.class, "withExpressions", node);
    for (WithNode.ParmValuePair parmValuePair : parmValuePairs) {
        Method method = extractMethod(parmValuePair);
        if (method == null) {
            modificationMask = allSetButTraitBitMask();
            return;
        }
        String propertyName = setter2property(method.getName());
        if (propertyName != null) {
            int index = settableProperties.indexOf(propertyName);
            if (index >= 0) {
                modificationMask = setPropertyOnMask(modificationMask, index);
            }
        }
        List<String> modifiedProps = typeDeclaration.getTypeClassDef().getModifiedPropsByMethod(method);
        if (modifiedProps != null) {
            for (String modifiedProp : modifiedProps) {
                int index = settableProperties.indexOf(modifiedProp);
                if (index >= 0) {
                    modificationMask = setPropertyOnMask(modificationMask, index);
                }
            }
        }
    }
}
Also used : Method(java.lang.reflect.Method) TypeDeclaration(org.drools.core.rule.TypeDeclaration) WithNode(org.mvel2.ast.WithNode)

Aggregations

ASTNode (org.mvel2.ast.ASTNode)9 CompileException (org.mvel2.CompileException)4 WithNode (org.mvel2.ast.WithNode)4 CompiledExpression (org.mvel2.compiler.CompiledExpression)4 Interceptor (org.mvel2.integration.Interceptor)4 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)4 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)4 HashMap (java.util.HashMap)3 BinaryOperation (org.mvel2.ast.BinaryOperation)3 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)3 CompiledTemplate (org.mvel2.templates.CompiledTemplate)3 Node (org.mvel2.templates.res.Node)3 ParserContext (org.mvel2.ParserContext)2 LiteralNode (org.mvel2.ast.LiteralNode)2 AccessorNode (org.mvel2.compiler.AccessorNode)2 ExecutableLiteral (org.mvel2.compiler.ExecutableLiteral)2 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)2 Debugger (org.mvel2.debug.Debugger)2 Frame (org.mvel2.debug.Frame)2 DynamicGetAccessor (org.mvel2.optimizers.dynamic.DynamicGetAccessor)2