Search in sources :

Example 26 with MapVariableResolverFactory

use of org.mvel2.integration.impl.MapVariableResolverFactory in project mvel by mikebrock.

the class DebuggerTests method testBreakpointsAcrossComments.

public void testBreakpointsAcrossComments() {
    String expression = // 1
    "/** This is a comment\n" + // 2
    " *  Second comment line\n" + // 3
    " *  Third Comment Line\n" + // 4
    " */\n" + // 5
    "System.out.println('4');\n" + // 6
    "System.out.println('5');\n" + // 7
    "a = 0;\n" + // 8
    "b = 1;\n" + // 9
    "a + b";
    ExpressionCompiler compiler = new ExpressionCompiler(expression);
    System.out.println("Expression:\n------------");
    System.out.println(expression);
    System.out.println("------------");
    ParserContext ctx = new ParserContext();
    ctx.setSourceFile("test2.mv");
    ctx.setDebugSymbols(true);
    CompiledExpression compiled = compiler.compile(ctx);
    MVELRuntime.registerBreakpoint("test2.mv", 9);
    final Set<Integer> linesEncountered = new HashSet<Integer>();
    Debugger testDebugger = new Debugger() {

        public int onBreak(Frame frame) {
            linesEncountered.add(frame.getLineNumber());
            System.out.println("Breakpoint Encountered [source:" + frame.getSourceName() + "; line:" + frame.getLineNumber() + "]");
            System.out.println("vars:" + frame.getFactory().getKnownVariables());
            System.out.println("Resume Execution");
            return 0;
        }
    };
    MVELRuntime.setThreadDebugger(testDebugger);
    assertEquals(1, MVEL.executeDebugger(compiled, null, new MapVariableResolverFactory(createTestMap())));
    assertTrue("Debugger did not break at line 9", linesEncountered.contains(9));
}
Also used : Debugger(org.mvel2.debug.Debugger) Frame(org.mvel2.debug.Frame) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext) CompiledExpression(org.mvel2.compiler.CompiledExpression) HashSet(java.util.HashSet)

Example 27 with MapVariableResolverFactory

use of org.mvel2.integration.impl.MapVariableResolverFactory 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 28 with MapVariableResolverFactory

use of org.mvel2.integration.impl.MapVariableResolverFactory in project mvel by mikebrock.

the class WithTests method testSataticClassImportViaFactoryAndWithModification.

public void testSataticClassImportViaFactoryAndWithModification() {
    OptimizerFactory.setDefaultOptimizer("ASM");
    MapVariableResolverFactory mvf = new MapVariableResolverFactory(createTestMap());
    ClassImportResolverFactory classes = new ClassImportResolverFactory();
    classes.addClass(Person.class);
    ResolverTools.appendFactory(mvf, classes);
    assertEquals(21, executeExpression(compileExpression("p = new Person('tom'); p.age = 20; " + "with( p ) { age = p.age + 1 }; return p.age;", classes.getImportedClasses()), mvf));
}
Also used : MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ClassImportResolverFactory(org.mvel2.integration.impl.ClassImportResolverFactory)

Example 29 with MapVariableResolverFactory

use of org.mvel2.integration.impl.MapVariableResolverFactory in project drools by kiegroup.

the class DefaultGenerator method generate.

/*
     * (non-Javadoc)
     * 
     * @see org.kie.decisiontable.parser.Generator#generate(java.lang.String,
     *      org.kie.decisiontable.parser.Row)
     */
public void generate(String templateName, Row row) {
    try {
        CompiledTemplate template = getTemplate(templateName);
        VariableResolverFactory factory = new MapVariableResolverFactory();
        Map<String, Object> vars = new HashMap<String, Object>();
        initializePriorCommaConstraints(vars);
        initializeHasPriorJunctionConstraint(vars);
        vars.put("row", row);
        for (Cell cell : row.getCells()) {
            cell.addValue(vars);
        }
        String drl = String.valueOf(TemplateRuntime.execute(template, vars, factory, registry));
        rules.add(drl);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) HashMap(java.util.HashMap) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) IOException(java.io.IOException) CompiledTemplate(org.mvel2.templates.CompiledTemplate)

Example 30 with MapVariableResolverFactory

use of org.mvel2.integration.impl.MapVariableResolverFactory in project drools by kiegroup.

the class JavaFunctionBuilder method build.

/* (non-Javadoc)
     * @see org.kie.rule.builder.dialect.java.JavaFunctionBuilder#build(org.kie.rule.Package, org.kie.lang.descr.FunctionDescr, org.codehaus.jfdi.interpreter.TypeResolver, java.util.Map)
     */
public String build(final InternalKnowledgePackage pkg, final FunctionDescr functionDescr, final TypeResolver typeResolver, final Map<String, LineMappings> lineMappings, final List<KnowledgeBuilderResult> errors) {
    final Map<String, Object> vars = new HashMap<String, Object>();
    vars.put("package", pkg.getName());
    vars.put("imports", pkg.getImports().keySet());
    final List<String> staticImports = new LinkedList<String>();
    for (String staticImport : pkg.getStaticImports()) {
        if (!staticImport.endsWith(functionDescr.getName())) {
            staticImports.add(staticImport);
        }
    }
    vars.put("staticImports", staticImports);
    vars.put("className", StringUtils.ucFirst(functionDescr.getName()));
    vars.put("methodName", functionDescr.getName());
    vars.put("returnType", functionDescr.getReturnType());
    vars.put("parameterTypes", functionDescr.getParameterTypes());
    vars.put("parameterNames", functionDescr.getParameterNames());
    vars.put("hashCode", functionDescr.getText().hashCode());
    // Check that all the parameters are resolvable
    final List<String> names = functionDescr.getParameterNames();
    final List<String> types = functionDescr.getParameterTypes();
    for (int i = 0, size = names.size(); i < size; i++) {
        try {
            typeResolver.resolveType(types.get(i));
        } catch (final ClassNotFoundException e) {
            errors.add(new FunctionError(functionDescr, e, "Unable to resolve type " + types.get(i) + " while building function."));
            break;
        }
    }
    vars.put("text", functionDescr.getText());
    final String text = String.valueOf(TemplateRuntime.eval(template, null, new MapVariableResolverFactory(vars)));
    final BufferedReader reader = new BufferedReader(new StringReader(text));
    final String lineStartsWith = "    public static " + functionDescr.getReturnType() + " " + functionDescr.getName();
    try {
        String line;
        int offset = 0;
        while ((line = reader.readLine()) != null) {
            offset++;
            if (line.startsWith(lineStartsWith)) {
                break;
            }
        }
        functionDescr.setOffset(offset);
    } catch (final IOException e) {
        // won't ever happen, it's just reading over a string.
        throw new RuntimeException("Error determining start offset with function");
    }
    final String name = pkg.getName() + "." + StringUtils.ucFirst(functionDescr.getName());
    final LineMappings mapping = new LineMappings(name);
    mapping.setStartLine(functionDescr.getLine());
    mapping.setOffset(functionDescr.getOffset());
    lineMappings.put(name, mapping);
    return text;
}
Also used : HashMap(java.util.HashMap) FunctionError(org.drools.compiler.compiler.FunctionError) IOException(java.io.IOException) LinkedList(java.util.LinkedList) LineMappings(org.drools.core.rule.LineMappings) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory)

Aggregations

MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)35 HashMap (java.util.HashMap)19 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)10 ParserContext (org.mvel2.ParserContext)8 CompiledExpression (org.mvel2.compiler.CompiledExpression)8 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)8 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 Debugger (org.mvel2.debug.Debugger)6 Frame (org.mvel2.debug.Frame)6 SimpleTemplateRegistry (org.mvel2.templates.SimpleTemplateRegistry)6 HashSet (java.util.HashSet)5 CompiledTemplate (org.mvel2.templates.CompiledTemplate)5 TemplateRegistry (org.mvel2.templates.TemplateRegistry)5 TemplateRuntimeError (org.mvel2.templates.TemplateRuntimeError)5 Serializable (java.io.Serializable)3 List (java.util.List)3 Map (java.util.Map)3 ClassObjectType (org.drools.core.base.ClassObjectType)3 Declaration (org.drools.core.rule.Declaration)3