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));
}
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)));
}
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));
}
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);
}
}
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;
}
Aggregations